GestureRecognitionToolkit  Version: 0.2.5
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  return true;
153  }
154  return false;
155  }
156 
157  UINT getK() const { return K; }
158 
159  UINT getClassLabel() const { return classLabel; }
160 
161  Float getTrainingMu() const {
162  return trainingMu;
163  }
164 
165  Float getTrainingSigma() const {
166  return trainingSigma;
167  }
168 
169  Float getNullRejectionThreshold() const {
170  return nullRejectionThreshold;
171  }
172 
173  Float getNormalizationFactor() const {
174  return normFactor;
175  }
176 
177  bool setClassLabel(const UINT classLabel){
178  this->classLabel = classLabel;
179  return true;
180  }
181 
182  bool setNormalizationFactor(const Float normFactor){
183  this->normFactor = normFactor;
184  return true;
185  }
186 
187  bool setTrainingMuAndSigma(const Float trainingMu,const Float trainingSigma){
188  this->trainingMu = trainingMu;
189  this->trainingSigma = trainingSigma;
190  return true;
191  }
192 
193  bool setNullRejectionThreshold(const Float nullRejectionThreshold){
194  this->nullRejectionThreshold = nullRejectionThreshold;
195  return true;
196  }
197 
198 private:
199  Float gauss(const VectorFloat &x,Float det,const VectorFloat &mu,const MatrixFloat &invSigma){
200 
201  Float y = 0;
202  Float sum = 0;
203  const UINT N = x.getSize();
204  VectorFloat temp(N,0);
205 
206  //Compute the first part of the equation
207  y = (1.0/pow(TWO_PI,N/2.0)) * (1.0/pow(det,0.5));
208 
209  //Compute the later half
210  for(UINT i=0; i<N; i++){
211  for(UINT j=0; j<N; j++){
212  temp[i] += (x[j]-mu[j]) * invSigma[j][i];
213  }
214  sum += (x[i]-mu[i]) * temp[i];
215  }
216 
217  return y*grt_exp( -0.5*sum );
218  }
219 
220  UINT classLabel;
221  UINT K;
222  Float nullRejectionThreshold;
223  Float gamma; //The number of standard deviations to use for the threshold
224  Float trainingMu; //The average confidence value in the training data
225  Float trainingSigma; //The simga confidence value in the training data
226  Float normFactor;
227  Vector< GuassModel > gaussModels;
228 
229 };
230 
231 GRT_END_NAMESPACE
232 
233 #endif //GRT_MIXTURE_MODEL_HEADER
UINT getSize() const
Definition: Vector.h:201
unsigned int getNumRows() const
Definition: Matrix.h:574
unsigned int getNumCols() const
Definition: Matrix.h:581