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