GestureRecognitionToolkit  Version: 0.2.0
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
MinDistModel.cpp
1 
18 #define GRT_DLL_EXPORTS
19 #include "MinDistModel.h"
20 
21 GRT_BEGIN_NAMESPACE
22 
24  classLabel = 0;
25  numFeatures = 0;
26  numClusters = 0;
27  rejectionThreshold = 0;
28  gamma = 0;
29  trainingMu = 0;
30  trainingSigma = 0;
31 }
32 
34 
35  this->classLabel = rhs.classLabel;
36  this->numFeatures = rhs.numFeatures;
37  this->numClusters = rhs.numClusters;
38  this->rejectionThreshold = rhs.rejectionThreshold;
39  this->gamma = rhs.gamma;
40  this->trainingMu = rhs.trainingMu;
41  this->trainingSigma = rhs.trainingSigma;
42  this->clusters = rhs.clusters;
43 }
44 
46 
48  if( this != &rhs ){
49  //MinDistModel variables
50  this->classLabel = rhs.classLabel;
51  this->numFeatures = rhs.numFeatures;
52  this->numClusters = rhs.numClusters;
53  this->rejectionThreshold = rhs.rejectionThreshold;
54  this->gamma = rhs.gamma;
55  this->trainingMu = rhs.trainingMu;
56  this->trainingSigma = rhs.trainingSigma;
57  this->clusters = rhs.clusters;
58  }
59  return *this;
60 }
61 
62 bool MinDistModel::train(UINT classLabel,MatrixFloat &trainingData,UINT numClusters,Float minChange,UINT maxNumEpochs){
63 
64  if( trainingData.getNumRows() < numClusters ){
65  return false;
66  }
67 
68  this->classLabel = classLabel;
69  this->numFeatures = trainingData.getNumCols();
70  this->numClusters = numClusters;
71 
72  //Find the clusters
73  KMeans kmeans;
74  kmeans.setNumClusters(numClusters);
75  kmeans.setMinChange( minChange );
76  kmeans.setMaxNumEpochs( maxNumEpochs );
77 
78  if( !kmeans.train_(trainingData) ){
79  return false;
80  }
81 
82  clusters = kmeans.getClusters();
83 
84  //Compute the rejection thresholds
85  rejectionThreshold = 0;
86  trainingMu = 0;
87  trainingSigma = 0;
88 
89  //Now compute the rejection threshold
90  const UINT M = trainingData.getNumRows();
91  const UINT N = trainingData.getNumCols();
92  VectorFloat predictions(M);
93  for(UINT i=0; i<M; i++){
94  //Test the ith training example
95  VectorFloat testData(N);
96  for(UINT j=0; j<N; j++) {
97  testData[j] = trainingData[i][j];
98  }
99 
100  predictions[i] = predict( testData );
101  trainingMu += predictions[i];
102  }
103 
104  //Calculate the mean prediction value
105  trainingMu /= Float(M);
106 
107  //Calculate the standard deviation
108  for(UINT i=0; i<M; i++) {
109  trainingSigma += SQR( predictions[i]-trainingMu );
110  }
111  trainingSigma = sqrt( trainingSigma / (Float(M)-1.0) );
112 
113  rejectionThreshold = trainingMu + ( trainingSigma * gamma );
114  return true;
115 
116 }
117 
118 Float MinDistModel::predict(const VectorFloat &inputVector){
119 
120  Float minDist = grt_numeric_limits< Float >::max();
121  Float dist = 0;
122 
123  for(UINT k=0; k<numClusters; k++){
124  dist = 0;
125  for(UINT n=0; n<numFeatures; n++){
126  dist += SQR( clusters[k][n]-inputVector[n] );
127  }
128  if( dist < minDist )
129  minDist = dist;
130  }
131 
132  //Only compute the sqrt for the minimum distance
133  return sqrt( minDist );
134 
135  return minDist;
136 }
137 
138 void MinDistModel::recomputeThresholdValue(){
139  rejectionThreshold = trainingMu + ( trainingSigma * gamma );
140 }
141 
142 UINT MinDistModel::getClassLabel() const{
143  return classLabel;
144 }
145 
146 UINT MinDistModel::getNumFeatures() const{
147  return numFeatures;
148 }
149 
150 UINT MinDistModel::getNumClusters() const{
151  return numClusters;
152 }
153 
154 Float MinDistModel::getRejectionThreshold() const{
155  return rejectionThreshold;
156 }
157 
158 Float MinDistModel::getGamma() const{
159  return gamma;
160 }
161 
162 Float MinDistModel::getTrainingMu() const{
163  return trainingMu;
164 }
165 
166 Float MinDistModel::getTrainingSigma() const{
167  return trainingSigma;
168 }
169 
170 MatrixFloat MinDistModel::getClusters() const{
171  return clusters;
172 }
173 
174 bool MinDistModel::setClassLabel(UINT classLabel){
175  this->classLabel = classLabel;
176  return true;
177 }
178 
179 bool MinDistModel::setClusters(MatrixFloat &clusters){
180  this->clusters = clusters;
181  this->numClusters = clusters.getNumRows();
182  this->numFeatures = clusters.getNumCols();
183  return true;
184 }
185 
186 bool MinDistModel::setGamma(Float gamma){
187  this->gamma = gamma;
188  return true;
189 }
190 
191 bool MinDistModel::setRejectionThreshold(Float rejectionThreshold){
192  this->rejectionThreshold = rejectionThreshold;
193  return true;
194 }
195 
196 bool MinDistModel::setTrainingSigma(Float trainingSigma){
197  this->trainingSigma = trainingSigma;
198  return true;
199 }
200 
201 bool MinDistModel::setTrainingMu(Float trainingMu){
202  this->trainingMu = trainingMu;
203  return true;
204 }
205 
206 GRT_END_NAMESPACE
207 
virtual bool train_(MatrixFloat &data)
Definition: KMeans.cpp:163
bool setMinChange(const Float minChange)
Definition: MLBase.cpp:287
This class implements the MinDist classifier algorithm.
~MinDistModel(void)
unsigned int getNumRows() const
Definition: Matrix.h:542
unsigned int getNumCols() const
Definition: Matrix.h:549
MinDistModel & operator=(const MinDistModel &rhs)
Definition: KMeans.h:41
bool setMaxNumEpochs(const UINT maxNumEpochs)
Definition: MLBase.cpp:273
bool setNumClusters(const UINT numClusters)
Definition: Clusterer.cpp:266