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