GestureRecognitionToolkit  Version: 1.0 Revision: 04-03-15
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
RBMQuantizer.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 "RBMQuantizer.h"
22 
23 namespace GRT{
24 
25 //Register your module with the FeatureExtraction base class
26 RegisterFeatureExtractionModule< RBMQuantizer > RBMQuantizer::registerModule("RBMQuantizer");
27 
28 RBMQuantizer::RBMQuantizer(const UINT numClusters){
29 
30  this->numClusters = numClusters;
31  classType = "RBMQuantizer";
32  featureExtractionType = classType;
33  debugLog.setProceedingText("[DEBUG RBMQuantizer]");
34  errorLog.setProceedingText("[ERROR RBMQuantizer]");
35  warningLog.setProceedingText("[WARNING RBMQuantizer]");
36 }
37 
39 
40  classType = "RBMQuantizer";
41  featureExtractionType = classType;
42  debugLog.setProceedingText("[DEBUG RBMQuantizer]");
43  errorLog.setProceedingText("[ERROR RBMQuantizer]");
44  warningLog.setProceedingText("[WARNING RBMQuantizer]");
45 
46  //Invoke the equals operator to copy the data from the rhs instance to this instance
47  *this = rhs;
48 }
49 
51 }
52 
54  if(this!=&rhs){
55  this->numClusters = rhs.numClusters;
56  this->rbm = rhs.rbm;
57  this->quantizationDistances = rhs.quantizationDistances;
58 
59  //Copy the base variables
61  }
62  return *this;
63 }
64 
65 bool RBMQuantizer::deepCopyFrom(const FeatureExtraction *featureExtraction){
66 
67  if( featureExtraction == NULL ) return false;
68 
69  if( this->getFeatureExtractionType() == featureExtraction->getFeatureExtractionType() ){
70 
71  //invoke the equals operator
72  *this = *(RBMQuantizer*)featureExtraction;
73 
74  return true;
75  }
76 
77  errorLog << "clone(FeatureExtraction *featureExtraction) - FeatureExtraction Types Do Not Match!" << endl;
78 
79  return false;
80 }
81 
82 bool RBMQuantizer::computeFeatures(const VectorDouble &inputVector){
83 
84  //Run the quantize algorithm
85  quantize( inputVector );
86 
87  return true;
88 }
89 
91 
92  //Reset the base class
94 
95  if( trained ){
96  rbm.reset();
97  std::fill(quantizationDistances.begin(),quantizationDistances.end(),0);
98  }
99 
100  return true;
101 }
102 
104 
105  //Clear the base class
107 
108  rbm.clear();
109  quantizationDistances.clear();
110 
111  return true;
112 }
113 
114 bool RBMQuantizer::saveModelToFile(string filename) const{
115 
116  std::fstream file;
117  file.open(filename.c_str(), std::ios::out);
118 
119  if( !saveModelToFile( file ) ){
120  return false;
121  }
122 
123  file.close();
124 
125  return true;
126 }
127 
128 bool RBMQuantizer::loadModelFromFile(string filename){
129 
130  std::fstream file;
131  file.open(filename.c_str(), std::ios::in);
132 
133  if( !loadModelFromFile( file ) ){
134  return false;
135  }
136 
137  //Close the file
138  file.close();
139 
140  return true;
141 }
142 
143 bool RBMQuantizer::saveModelToFile(fstream &file) const{
144 
145  if( !file.is_open() ){
146  errorLog << "saveModelToFile(fstream &file) - The file is not open!" << endl;
147  return false;
148  }
149 
150  //Write the header
151  file << "RBM_QUANTIZER_FILE_V1.0" << endl;
152 
153  //Save the base feature extraction settings to the file
155  errorLog << "saveFeatureExtractionSettingsToFile(fstream &file) - Failed to save base feature extraction settings to file!" << endl;
156  return false;
157  }
158 
159  file << "QuantizerTrained: " << trained << endl;
160  file << "NumClusters: " << numClusters << endl;
161 
162  if( trained ){
163  if( !rbm.saveModelToFile( file ) ){
164  errorLog << "saveModelToFile(fstream &file) - Failed to save RBM settings to file!" << endl;
165  return false;
166  }
167  }
168 
169  return true;
170 }
171 
173 
174  //Clear any previous model
175  clear();
176 
177  if( !file.is_open() ){
178  errorLog << "loadModelFromFile(fstream &file) - The file is not open!" << endl;
179  return false;
180  }
181 
182  string word;
183 
184  //First, you should read and validate the header
185  file >> word;
186  if( word != "RBM_QUANTIZER_FILE_V1.0" ){
187  errorLog << "loadModelFromFile(fstream &file) - Invalid file format!" << endl;
188  return false;
189  }
190 
191  //Second, you should load the base feature extraction settings to the file
193  errorLog << "loadFeatureExtractionSettingsFromFile(fstream &file) - Failed to load base feature extraction settings from file!" << endl;
194  return false;
195  }
196 
197  file >> word;
198  if( word != "QuantizerTrained:" ){
199  errorLog << "loadModelFromFile(fstream &file) - Failed to load QuantizerTrained!" << endl;
200  return false;
201  }
202  file >> trained;
203 
204  file >> word;
205  if( word != "NumClusters:" ){
206  errorLog << "loadModelFromFile(fstream &file) - Failed to load NumClusters!" << endl;
207  return false;
208  }
209  file >> numClusters;
210 
211  if( trained ){
212  if( !rbm.loadModelFromFile( file ) ){
213  errorLog << "loadModelFromFile(fstream &file) - Failed to load SelfOrganizingMap settings from file!" << endl;
214  return false;
215  }
216  initialized = true;
217  featureDataReady = false;
218  quantizationDistances.resize(numClusters,0);
219  }
220 
221  return true;
222 }
223 
225  MatrixDouble data = trainingData.getDataAsMatrixDouble();
226  return train_( data );
227 }
228 
230  MatrixDouble data = trainingData.getDataAsMatrixDouble();
231  return train_( data );
232 }
233 
235  MatrixDouble data = trainingData.getDataAsMatrixDouble();
236  return train_( data );
237 }
238 
240  MatrixDouble data = trainingData.getDataAsMatrixDouble();
241  return train_( data );
242 }
243 
244 bool RBMQuantizer::train_(MatrixDouble &trainingData){
245 
246  //Clear any previous model
247  clear();
248 
249  if( trainingData.getNumRows() == 0 ){
250  errorLog << "train_(MatrixDouble &trainingData) - Failed to train quantizer, the training data is empty!" << endl;
251  return false;
252  }
253 
254  //Train the RBM model
255  rbm.setNumHiddenUnits( numClusters );
256  rbm.setLearningRate( learningRate );
257  rbm.setMinNumEpochs( minNumEpochs );
258  rbm.setMaxNumEpochs( maxNumEpochs );
259  rbm.setMinChange( minChange );
260 
261  if( !rbm.train_( trainingData ) ){
262  errorLog << "train_(MatrixDouble &trainingData) - Failed to train quantizer!" << endl;
263  return false;
264  }
265 
266  //Flag that the feature vector is now initalized
267  initialized = true;
268  trained = true;
269  numInputDimensions = trainingData.getNumCols();
270  numOutputDimensions = 1; //This is always 1 for the quantizer
271  featureVector.resize(numOutputDimensions,0);
272  quantizationDistances.resize(numClusters,0);
273 
274  return true;
275 }
276 
277 UINT RBMQuantizer::quantize(const double inputValue){
278  return quantize( VectorDouble(1,inputValue) );
279 }
280 
281 UINT RBMQuantizer::quantize(const VectorDouble &inputVector){
282 
283  if( !trained ){
284  errorLog << "quantize(const VectorDouble &inputVector) - The quantizer model has not been trained!" << endl;
285  return 0;
286  }
287 
288  if( inputVector.size() != numInputDimensions ){
289  errorLog << "quantize(const VectorDouble &inputVector) - The size of the inputVector (" << inputVector.size() << ") does not match that of the filter (" << numInputDimensions << ")!" << endl;
290  return 0;
291  }
292 
293  if( !rbm.predict( inputVector ) ){
294  errorLog << "quantize(const VectorDouble &inputVector) - Failed to quantize input!" << endl;
295  return 0;
296  }
297 
298  quantizationDistances = rbm.getOutputData();
299 
300  //Search for the neuron with the maximum output
301  UINT quantizedValue = 0;
302  double maxValue = 0;
303  for(UINT k=0; k<numClusters; k++){
304  if( quantizationDistances[k] > maxValue ){
305  maxValue = quantizationDistances[k];
306  quantizedValue = k;
307  }
308  }
309 
310  featureVector[0] = quantizedValue;
311  featureDataReady = true;
312 
313  return quantizedValue;
314 }
315 
317  return trained;
318 }
319 
321  return numClusters;
322 }
323 
325  return (trained ? static_cast<UINT>(featureVector[0]) : 0);
326 }
327 
329  return quantizationDistances;
330 }
331 
333  return rbm;
334 }
335 
336 bool RBMQuantizer::setNumClusters(const UINT numClusters){
337  clear();
338  this->numClusters = numClusters;
339  return true;
340 }
341 
342 }//End of namespace GRT
virtual bool train_(MatrixDouble &data)
RBMQuantizer & operator=(const RBMQuantizer &rhs)
virtual bool clear()
bool setMaxNumEpochs(const UINT maxNumEpochs)
Definition: MLBase.cpp:237
bool getQuantizerTrained() const
Definition: AdaBoost.cpp:25
unsigned int getNumCols() const
Definition: Matrix.h:538
virtual bool clear()
UINT getQuantizedValue() const
bool setMinChange(const double minChange)
Definition: MLBase.cpp:251
virtual bool loadModelFromFile(string filename)
string getFeatureExtractionType() const
BernoulliRBM getBernoulliRBM() const
virtual bool reset()
virtual bool predict(VectorDouble inputVector)
Definition: MLBase.cpp:104
MatrixDouble getDataAsMatrixDouble() const
virtual ~RBMQuantizer()
virtual bool saveModelToFile(string filename) const
bool loadFeatureExtractionSettingsFromFile(fstream &file)
The SOMQuantizer module quantizes the N-dimensional input vector to a 1-dimensional discrete value...
MatrixDouble getDataAsMatrixDouble() const
virtual bool reset()
virtual bool deepCopyFrom(const FeatureExtraction *featureExtraction)
virtual bool saveModelToFile(fstream &file) const
unsigned int getNumRows() const
Definition: Matrix.h:531
VectorDouble getQuantizationDistances() const
RBMQuantizer(const UINT numClusters=10)
virtual bool loadModelFromFile(fstream &file)
bool saveFeatureExtractionSettingsToFile(fstream &file) const
virtual bool computeFeatures(const VectorDouble &inputVector)
UINT quantize(const double inputValue)
bool setLearningRate(double learningRate)
Definition: MLBase.cpp:260
UINT getNumClusters() const
bool train_(ClassificationData &trainingData)
bool setNumClusters(const UINT numClusters)
bool setMinNumEpochs(const UINT minNumEpochs)
Definition: MLBase.cpp:246
bool copyBaseVariables(const FeatureExtraction *featureExtractionModule)