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.
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 GRT_BEGIN_NAMESPACE
24 
25 bool ANBC_Model::train( UINT classLabel, const MatrixFloat &trainingData, const VectorFloat &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] /= Float(M);
53  }
54 
55  //Calculate the sample standard deviation
56  for(UINT j=0; j<N; j++){
57  sigma[j] = 0.0;
58 
59  for(UINT i=0; i<M; i++){
60  sigma[j] += grt_sqr( trainingData[i][j]-mu[j] );
61  }
62  sigma[j] = grt_sqrt( sigma[j]/Float(M-1) );
63 
64  if( sigma[j] == 0 ){
65  sigma[j] = 0.1; //Set the sigma to a small value so sigma is not zero!
66  }
67  }
68 
69  //Now compute the threshold
70  Float meanPrediction = 0.0;
71  VectorFloat predictions(M);
72  VectorFloat testData(N);
73  for(UINT i=0; i<M; i++){
74  //Test the ith training example
75  for(UINT j=0; j<N; j++) {
76  testData[j] = trainingData[i][j];
77  }
78 
79  predictions[i] = predict( testData );
80  meanPrediction += predictions[i];
81  }
82 
83  //Calculate the mean prediction value
84  meanPrediction /= Float(M);
85 
86  //Calculate the standard deviation
87  Float stdDev = 0.0;
88  for(UINT i=0; i<M; i++) {
89  stdDev += grt_sqr( predictions[i]-meanPrediction );
90  }
91  stdDev = grt_sqrt( stdDev / (Float(M)-1.0) );
92 
93  threshold = meanPrediction-(stdDev*gamma);
94 
95  //Update the training mu and sigma values so the threshold value can be dynamically computed at a later stage
96  trainingMu = meanPrediction;
97  trainingSigma = stdDev;
98 
99  return true;
100 }
101 
102 Float ANBC_Model::predict( const VectorFloat &x ){
103  Float prediction = 0.0;
104  for(UINT j=0; j<N; j++){
105  if(weights[j]>0)
106  prediction += grt_log(gauss(x[j],mu[j],sigma[j]) * weights[j]);
107  }
108  return prediction;
109 }
110 
111 Float ANBC_Model::predictUnnormed( const VectorFloat &x ){
112  Float prediction = 0.0;
113  for(UINT j=0; j<N; j++){
114  if(weights[j]>0)
115  prediction += grt_log( unnormedGauss(x[j], mu[j], sigma[j]) * weights[j] );
116  }
117  return prediction;
118 }
119 
120 inline Float ANBC_Model::gauss(const Float x,const Float mu,const Float sigma){
121  return ( 1.0/(sigma*sqrt(TWO_PI)) ) * exp( - ( ((x-mu)*(x-mu))/(2*(sigma*sigma)) ) );
122 }
123 
124 inline Float ANBC_Model::unnormedGauss(const Float x,const Float mu,const Float sigma){
125  return exp( - ( ((x-mu)*(x-mu))/(2*(sigma*sigma)) ) );
126 }
127 
128 void ANBC_Model::recomputeThresholdValue(const Float gamma){
129  this->gamma = gamma;
130  threshold = trainingMu-(trainingSigma*gamma);
131 }
132 
133 GRT_END_NAMESPACE
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
This class implements a container for an ANBC model.
unsigned int getNumRows() const
Definition: Matrix.h:542
unsigned int getNumCols() const
Definition: Matrix.h:549