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.
ANBC_Model.cpp
1 /*
2 GRT MIT License
3 Copyright (c) <2012> <Nicholas Gillian, Media Lab, MIT>
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
6 and associated documentation files (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all copies or substantial
12 portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
15 LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
18 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 */
20 
21 #include "ANBC_Model.h"
22 
23 namespace GRT{
24 
25 bool ANBC_Model::train(UINT classLabel,MatrixDouble &trainingData,VectorDouble &weightsVector){
26 
27  //Check to make sure the column sizes match
28  if( trainingData.getNumCols() != weightsVector.size() ){
29  N = 0;
30  return false;
31  }
32 
33  UINT M = trainingData.getNumRows();
34  N = trainingData.getNumCols();
35  this->classLabel = classLabel;
36 
37  //Update the weights buffer
38  weights = weightsVector;
39 
40  //Resize the buffers
41  mu.resize( N );
42  sigma.resize( N );
43 
44  //Calculate the mean for each dimension
45  for(UINT j=0; j<N; j++){
46  mu[j] = 0.0;
47 
48  for(UINT i=0; i<M; i++){
49  mu[j] += trainingData[i][j];
50  }
51 
52  mu[j] /= double(M);
53 
54  if( mu[j] == 0 ){
55  return false;
56  }
57  }
58 
59  //Calculate the sample standard deviation
60  for(UINT j=0; j<N; j++){
61  sigma[j] = 0.0;
62 
63  for(UINT i=0; i<M; i++){
64  sigma[j] += SQR( trainingData[i][j]-mu[j] );
65  }
66  sigma[j] += 0.01; //Add a small amount to the standard deviation to ensure it is not zero
67  sigma[j] = sqrt( sigma[j]/double(M-1) );
68 
69  if( sigma[j] == 0 ){
70  return false;
71  }
72  }
73 
74  //Now compute the threshold
75  double meanPrediction = 0.0;
76  VectorDouble predictions(M);
77  for(UINT i=0; i<M; i++){
78  //Test the ith training example
79  vector<double> testData(N);
80  for(UINT j=0; j<N; j++) {
81  testData[j] = trainingData[i][j];
82  }
83 
84  predictions[i] = predict(testData);
85  meanPrediction += predictions[i];
86  }
87 
88  //Calculate the mean prediction value
89  meanPrediction /= double(M);
90 
91  //Calculate the standard deviation
92  double stdDev = 0.0;
93  for(UINT i=0; i<M; i++) {
94  stdDev += SQR( predictions[i]-meanPrediction );
95  }
96  stdDev = sqrt( stdDev / (double(M)-1.0) );
97 
98  threshold = meanPrediction-(stdDev*gamma);
99 
100  //Update the training mu and sigma values so the threshold value can be dynamically computed at a later stage
101  trainingMu = meanPrediction;
102  trainingSigma = stdDev;
103 
104  return true;
105 }
106 
107 double ANBC_Model::predict(const VectorDouble &x){
108  double prediction = 0.0;
109  for(UINT j=0; j<N; j++){
110  if(weights[j]>0)
111  prediction += log(gauss(x[j],mu[j],sigma[j]) * weights[j]);
112  }
113  return prediction;
114 }
115 
116 double ANBC_Model::predictUnnormed(const VectorDouble &x){
117  double prediction = 0.0;
118  for(UINT j=0; j<N; j++){
119  if(weights[j]>0)
120  prediction += log(unnormedGauss(x[j],mu[j],sigma[j]) * weights[j]);
121  }
122  return prediction;
123 }
124 
125 inline double ANBC_Model::gauss(const double x,const double mu,const double sigma){
126  return ( 1.0/(sigma*sqrt(TWO_PI)) ) * exp( - ( ((x-mu)*(x-mu))/(2*(sigma*sigma)) ) );
127 }
128 
129 inline double ANBC_Model::unnormedGauss(const double x,const double mu,const double sigma){
130  return exp( - ( ((x-mu)*(x-mu))/(2*(sigma*sigma)) ) );
131 }
132 
133 void ANBC_Model::recomputeThresholdValue(const double gamma){
134  this->gamma = gamma;
135  threshold = trainingMu-(trainingSigma*gamma);
136 }
137 
138 } //End of namespace GRT
Definition: AdaBoost.cpp:25
This class implements a container for an ANBC model.