GestureRecognitionToolkit  Version: 0.1.0
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 GRT_BEGIN_NAMESPACE
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  std::cout << "Determinate: " << det << std::endl;
54  std::cout << "Mu: ";
55  for(UINT i=0; i<mu.getSize(); i++)
56  std::cout << mu[i] << "\t";
57  std::cout << std::endl;
58 
59  std::cout << "Sigma: \n";
60  for(UINT i=0; i<sigma.getNumRows(); i++){
61  for(UINT j=0; j<sigma.getNumCols(); j++){
62  std::cout << sigma[i][j] << "\t";
63  }std::cout << std::endl;
64  }std::cout << std::endl;
65 
66  std::cout << "InvSigma: \n";
67  for(UINT i=0; i<invSigma.getNumRows(); i++){
68  for(UINT j=0; j<invSigma.getNumCols(); j++){
69  std::cout << invSigma[i][j] << "\t";
70  }std::cout << std::endl;
71  }std::cout << std::endl;
72 
73  return true;
74  }
75 
76  Float det;
77  VectorFloat mu;
78  MatrixFloat sigma;
79  MatrixFloat 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  Float computeMixtureLikelihood( const VectorFloat &x ){
106  Float 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(Float gamma){
125  Float 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.getSize() > 0 ){
149  for(UINT k=0; k<gaussModels.getSize(); 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  Float getTrainingMu() const {
161  return trainingMu;
162  }
163 
164  Float getTrainingSigma() const {
165  return trainingSigma;
166  }
167 
168  Float getNullRejectionThreshold() const {
169  return nullRejectionThreshold;
170  }
171 
172  Float 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 Float normFactor){
182  this->normFactor = normFactor;
183  return true;
184  }
185 
186  bool setTrainingMuAndSigma(const Float trainingMu,const Float trainingSigma){
187  this->trainingMu = trainingMu;
188  this->trainingSigma = trainingSigma;
189  return true;
190  }
191 
192  bool setNullRejectionThreshold(const Float nullRejectionThreshold){
193  this->nullRejectionThreshold = nullRejectionThreshold;
194  return true;
195  }
196 
197 private:
198  Float gauss(const VectorFloat &x,Float det,const VectorFloat &mu,const MatrixFloat &invSigma){
199 
200  Float y = 0;
201  Float sum = 0;
202  const UINT N = x.getSize();
203  VectorFloat 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*grt_exp( -0.5*sum ) );
217  }
218 
219  UINT classLabel;
220  UINT K;
221  Float nullRejectionThreshold;
222  Float gamma; //The number of standard deviations to use for the threshold
223  Float trainingMu; //The average confidence value in the training data
224  Float trainingSigma; //The simga confidence value in the training data
225  Float normFactor;
226  Vector< GuassModel > gaussModels;
227 
228 };
229 
230 GRT_END_NAMESPACE
231 
232 #endif //GRT_MIXTURE_MODEL_HEADER
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
unsigned int getSize() const
Definition: Vector.h:193
unsigned int getNumRows() const
Definition: Matrix.h:542
unsigned int getNumCols() const
Definition: Matrix.h:549