GestureRecognitionToolkit  Version: 1.0 Revision: 04-03-15
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
MixtureModel.h
Go to the documentation of this file.
1 
32 #ifndef GRT_MIXTURE_MODEL_HEADER
33 #define GRT_MIXTURE_MODEL_HEADER
34 
35 #include "../../CoreModules/Classifier.h"
36 #include "../../ClusteringModules/GaussianMixtureModels/GaussianMixtureModels.h"
37 
38 namespace GRT {
39 
40 class GuassModel{
41 public:
42  GuassModel(){
43  det = 0;
44  }
45 
46  ~GuassModel(){
47 
48  }
49 
50  bool printModelValues() const{
51  if( mu.size() == 0 ) return false;
52 
53  cout << "Determinate: " << det << endl;
54  cout << "Mu: ";
55  for(UINT i=0; i<mu.size(); i++)
56  cout << mu[i] << "\t";
57  cout << endl;
58 
59  cout << "Sigma: \n";
60  for(UINT i=0; i<sigma.getNumRows(); i++){
61  for(UINT j=0; j<sigma.getNumCols(); j++){
62  cout << sigma[i][j] << "\t";
63  }cout << endl;
64  }cout << endl;
65 
66  cout << "InvSigma: \n";
67  for(UINT i=0; i<invSigma.getNumRows(); i++){
68  for(UINT j=0; j<invSigma.getNumCols(); j++){
69  cout << invSigma[i][j] << "\t";
70  }cout << endl;
71  }cout << endl;
72 
73  return true;
74  }
75 
76  double det;
77  VectorDouble mu;
78  MatrixDouble sigma;
79  MatrixDouble invSigma;
80 };
81 
83 public:
84  MixtureModel(){
85  classLabel = 0;
86  K = 0;
87  normFactor = 1;
88  nullRejectionThreshold = 0;
89  trainingMu = 0;
90  trainingSigma = 0;
91  gamma = 1;
92  }
93  ~MixtureModel(){
94  gaussModels.clear();
95  }
96 
97  inline GuassModel& operator[](const UINT i){
98  return gaussModels[i];
99  }
100 
101  inline const GuassModel& operator[](const UINT i) const{
102  return gaussModels[i];
103  }
104 
105  double computeMixtureLikelihood(const vector<double> &x){
106  double sum = 0;
107  for(UINT k=0; k<K; k++){
108  sum += gauss(x,gaussModels[k].det,gaussModels[k].mu,gaussModels[k].invSigma);
109  }
110  //Normalize the mixture likelihood
111  return sum/normFactor;
112  }
113 
114  bool resize(UINT K){
115  if( K > 0 ){
116  this->K = K;
117  gaussModels.clear();
118  gaussModels.resize(K);
119  return true;
120  }
121  return false;
122  }
123 
124  bool recomputeNullRejectionThreshold(double gamma){
125  double newRejectionThreshold = 0;
126  //TODO - Need a way of improving the null rejection threshold for the GMMs!!!!
127  newRejectionThreshold = trainingMu - (trainingSigma*gamma);
128  newRejectionThreshold = 0.02;
129 
130  //Make sure that the new rejection threshold is greater than zero
131  if( newRejectionThreshold > 0 ){
132  this->gamma = gamma;
133  this->nullRejectionThreshold = newRejectionThreshold;
134  return true;
135  }
136  return false;
137  }
138 
139  bool recomputeNormalizationFactor(){
140  normFactor = 0;
141  for(UINT k=0; k<K; k++){
142  normFactor += gauss(gaussModels[k].mu,gaussModels[k].det,gaussModels[k].mu,gaussModels[k].invSigma);
143  }
144  return true;
145  }
146 
147  bool printModelValues() const{
148  if( gaussModels.size() > 0 ){
149  for(UINT k=0; k<gaussModels.size(); k++){
150  gaussModels[k].printModelValues();
151  }
152  }
153  return false;
154  }
155 
156  UINT getK() const { return K; }
157 
158  UINT getClassLabel() const { return classLabel; }
159 
160  double getTrainingMu() const {
161  return trainingMu;
162  }
163 
164  double getTrainingSigma() const {
165  return trainingSigma;
166  }
167 
168  double getNullRejectionThreshold() const {
169  return nullRejectionThreshold;
170  }
171 
172  double getNormalizationFactor() const {
173  return normFactor;
174  }
175 
176  bool setClassLabel(const UINT classLabel){
177  this->classLabel = classLabel;
178  return true;
179  }
180 
181  bool setNormalizationFactor(const double normFactor){
182  this->normFactor = normFactor;
183  return true;
184  }
185 
186  bool setTrainingMuAndSigma(const double trainingMu,const double trainingSigma){
187  this->trainingMu = trainingMu;
188  this->trainingSigma = trainingSigma;
189  return true;
190  }
191 
192  bool setNullRejectionThreshold(const double nullRejectionThreshold){
193  this->nullRejectionThreshold = nullRejectionThreshold;
194  return true;
195  }
196 
197 private:
198  double gauss(const VectorDouble &x,double det,const VectorDouble &mu,const MatrixDouble &invSigma){
199 
200  double y = 0;
201  double sum = 0;
202  const UINT N = (UINT)x.size();
203  VectorDouble temp(N,0);
204 
205  //Compute the first part of the equation
206  y = (1.0/pow(TWO_PI,N/2.0)) * (1.0/pow(det,0.5));
207 
208  //Compute the later half
209  for(UINT i=0; i<N; i++){
210  for(UINT j=0; j<N; j++){
211  temp[i] += (x[j]-mu[j]) * invSigma[j][i];
212  }
213  sum += (x[i]-mu[i]) * temp[i];
214  }
215 
216  return ( y*exp( -0.5*sum ) );
217  }
218 
219  UINT classLabel;
220  UINT K;
221  double nullRejectionThreshold;
222  double gamma; //The number of standard deviations to use for the threshold
223  double trainingMu; //The average confidence value in the training data
224  double trainingSigma; //The simga confidence value in the training data
225  double normFactor;
226  vector< GuassModel > gaussModels;
227 
228 };
229 
230 }//End of namespace GRT
231 
232 #endif //GRT_MIXTURE_MODEL_HEADER
Definition: AdaBoost.cpp:25
unsigned int getNumCols() const
Definition: Matrix.h:538
unsigned int getNumRows() const
Definition: Matrix.h:531