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.
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 #define GRT_DLL_EXPORTS
22 #include "ANBC_Model.h"
23 
24 GRT_BEGIN_NAMESPACE
25 
26 bool ANBC_Model::train( UINT classLabel, const MatrixFloat &trainingData, const VectorFloat &weightsVector ){
27 
28  //Check to make sure the column sizes match
29  if( trainingData.getNumCols() != weightsVector.size() ){
30  N = 0;
31  return false;
32  }
33 
34  UINT M = trainingData.getNumRows();
35  N = trainingData.getNumCols();
36  this->classLabel = classLabel;
37 
38  //Update the weights buffer
39  weights = weightsVector;
40 
41  //Resize the buffers
42  mu.resize( N );
43  sigma.resize( N );
44 
45  //Calculate the mean for each dimension
46  for(UINT j=0; j<N; j++){
47  mu[j] = 0.0;
48 
49  for(UINT i=0; i<M; i++){
50  mu[j] += trainingData[i][j];
51  }
52 
53  mu[j] /= Float(M);
54  }
55 
56  //Calculate the sample standard deviation
57  for(UINT j=0; j<N; j++){
58  sigma[j] = 0.0;
59 
60  for(UINT i=0; i<M; i++){
61  sigma[j] += grt_sqr( trainingData[i][j]-mu[j] );
62  }
63  sigma[j] = grt_sqrt( sigma[j]/Float(M-1) );
64 
65  if( sigma[j] == 0 ){
66  sigma[j] = 0.1; //Set the sigma to a small value so sigma is not zero!
67  }
68  }
69 
70  //Now compute the threshold
71  Float meanPrediction = 0.0;
72  VectorFloat predictions(M);
73  VectorFloat testData(N);
74  for(UINT i=0; i<M; i++){
75  //Test the ith training example
76  for(UINT j=0; j<N; j++) {
77  testData[j] = trainingData[i][j];
78  }
79 
80  predictions[i] = predict( testData );
81  meanPrediction += predictions[i];
82  }
83 
84  //Calculate the mean prediction value
85  meanPrediction /= Float(M);
86 
87  //Calculate the standard deviation
88  Float stdDev = 0.0;
89  for(UINT i=0; i<M; i++) {
90  stdDev += grt_sqr( predictions[i]-meanPrediction );
91  }
92  stdDev = grt_sqrt( stdDev / (Float(M)-1.0) );
93 
94  threshold = meanPrediction-(stdDev*gamma);
95 
96  //Update the training mu and sigma values so the threshold value can be dynamically computed at a later stage
97  trainingMu = meanPrediction;
98  trainingSigma = stdDev;
99 
100  return true;
101 }
102 
103 Float ANBC_Model::predict( const VectorFloat &x ){
104  Float prediction = 0.0;
105  for(UINT j=0; j<N; j++){
106  if(weights[j]>0)
107  prediction += grt_log(gauss(x[j],mu[j],sigma[j]) * weights[j]);
108  }
109  return prediction;
110 }
111 
112 Float ANBC_Model::predictUnnormed( const VectorFloat &x ){
113  Float prediction = 0.0;
114  for(UINT j=0; j<N; j++){
115  if(weights[j]>0)
116  prediction += grt_log( unnormedGauss(x[j], mu[j], sigma[j]) * weights[j] );
117  }
118  return prediction;
119 }
120 
121 inline Float ANBC_Model::gauss(const Float x,const Float mu,const Float sigma){
122  return ( 1.0/(sigma*sqrt(TWO_PI)) ) * exp( - ( ((x-mu)*(x-mu))/(2*(sigma*sigma)) ) );
123 }
124 
125 inline Float ANBC_Model::unnormedGauss(const Float x,const Float mu,const Float sigma){
126  return exp( - ( ((x-mu)*(x-mu))/(2*(sigma*sigma)) ) );
127 }
128 
129 void ANBC_Model::recomputeThresholdValue(const Float gamma){
130  this->gamma = gamma;
131  threshold = trainingMu-(trainingSigma*gamma);
132 }
133 
134 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:574
unsigned int getNumCols() const
Definition: Matrix.h:581