-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathToolGmm.h
More file actions
184 lines (141 loc) · 5.55 KB
/
ToolGmm.h
File metadata and controls
184 lines (141 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#if !defined(__ACA_Gmm_HEADER_INCLUDED__)
#define __ACA_Gmm_HEADER_INCLUDED__
#pragma once
#include "ErrorDef.h"
/*! \brief class holding the result/details of a Gaussian Mixture Model created with ::CGmm
*/
class CGmmResult
{
friend class CGmm;
public:
CGmmResult() {};
virtual ~CGmmResult(void);
CGmmResult(const CGmmResult &that);
/*! returns the number of Gaussians in the mixture
\return int
*/
int getNumGaussians() const;
/*! returns the dimensionality of each Gaussian, i.e., the number of features
\return int
*/
int getNumDimensions() const;
/*! returns mean for one Gaussian and one feature
\param iGaussianIdx index of Gaussian
\param iFeatureIdx index of feature
\return float
*/
float getMu(int iGaussianIdx, int iFeatureIdx) const;
/*! returns the prior for one Gaussian
\param iGaussianIdx index of Gaussian
\return float
*/
float getPrior(int iGaussianIdx) const;
/*! returns entry of sigma matrix for one Gaussian,
\param iGaussianIdx index of Gaussian
\param iRowIdx row index (< iNumFeatures)
\param iColIdx col index (< iNumFeatures)
\return float
*/
float getSigma(int iGaussianIdx, int iRowIdx, int iColIdx) const;
/*! writes sigma matrix for one Gaussian
\param ppfSigma to be written (user allocated, dimensions iNumFeatures X iNumFeatures)
\param iGaussianIdx index of Gaussian
*/
void getSigma(float **ppfSigma, int iGaussianIdx) const;
float getProb(const float *pfQuery);
/*! returns initialization state
\return bool true if initialized
*/
bool isInitialized() const;
/*! assignment operator
*/
CGmmResult &operator=(const CGmmResult &that);
protected:
// these are called by our friend
Error_t init(int iK, int iNumFeatures);
Error_t reset();
Error_t setMu(int iGaussianIdx, int iFeatureIdx, float fParamValue);
Error_t setPrior(int iGaussianIdx, float fParamValue);
Error_t setSigma(int iGaussianIdx, float **ppfSigma);
private:
enum Sigma_t
{
kNormal,
kInv,
kSigma
};
float **m_ppfMu = 0; //!< means, dim: cluster x feature
float *m_pfPrior = 0; //!< priors, dim: cluster
float ***m_apppfSigma[kSigma] = { 0 }; //!< sigma, cluster x feature x feature
float *m_apfProc[2] = { 0 }; //!< pre-allocated temporary memory buffer
int m_iK = 0; //!< number of Gaussians
int m_iNumFeatures = 0; //!< number of features
bool m_bIsInitialized = false; //!< indicates whether instance is initialized
};
/*! \brief implements Gaussian mixture model
*/
class CGmm
{
public:
CGmm(void) {};
virtual ~CGmm(void);
/*! initializes the class
\param pCResult class holding resulting mean, sigma, and priors, see class ::CGmmResult (user-allocated)
\param iK target number of Gaussians
\param iNumFeatures number of rows (features) of input matrix
\param iNumObs number of columns (observations) of input matrix
\param iMaxIter maximum number of iterations
\return Error_t
*/
Error_t init(CGmmResult *pCResult, int iK, int iNumFeatures, int iNumObs, int iMaxIter = 300);
/*! resets all internal class members
\return Error_t
*/
Error_t reset();
/*! computes the mixture model
\param pCResult class holding resulting mean, sigma, and priors, see class ::CGmmResult
\param ppfFeatures input matrix of dimensions iNumFeatures X iNumObs
\return Error_t
*/
Error_t compGmm(CGmmResult *pCResult, const float *const *const ppfFeatures);
private:
enum States_t
{
kPrev,
kCurr,
kNumStates
};
CGmm(const CGmm &that); //!< disallow copy construction
CGmm &operator=(const CGmm &c);
/*! randomly initializes the state variables
\param ppfFeatures input matrix of dimensions iNumFeatures X iNumObs
\param pCCurrState class holding the current state variables
*/
void initState_(const float *const *const ppfFeatures, CGmmResult *pCCurrState);
/*! computes probabilities given the current state
\param ppfFeatures input matrix of dimensions iNumFeatures X iNumObs
\param pCCurrState class holding the current state variables
*/
void compProbabilities_(const float *const *const ppfFeatures, CGmmResult *pCCurrState);
/*! update mean, sigma, and prior
\param ppfFeatures input matrix of dimensions iNumFeatures X iNumObs
\param pCCurrState class holding the current state variables
*/
void updateState_(const float *const *const ppfFeatures, CGmmResult *pCCurrState);
/*! returns true if the means are identical compared to previous iteration
\return bool true if identical
*/
bool checkConverged_(CGmmResult *pCCurrState);
CGmmResult PrevState; //!< previous state for convergence
float **m_appfClusterMeans[kNumStates] = { 0 }; //!< contains the current and previous cluster means
float *m_apfProc[2] = { 0 }, //!< temporary pre-allocated memory buffer
**m_appfSigma[2] = { 0 }, //!< temporary sigma matrices (m_iNumFeatures x m_iNumFeatures)
**m_ppfProb = 0; //!< probabilities (temp, dims m_iK x m_iNumObs)
int *m_piClusterSize = 0; //!< number of observations per cluster
int m_iNumFeatures = 0, //!< number of feature dimension (matrix rows)
m_iNumObs = 0, //!< number of observations (matrix cols)
m_iK = 0, //!< number of clusters
m_iMaxIter = 300; //!< maximum number of iterations
bool m_bisInitialized = false; //!< indicates whether instance is initialized
};
#endif // __ACA_Gmm_HEADER_INCLUDED__