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.
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 GRT_BEGIN_NAMESPACE
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!" << std::endl;
78 
79  return false;
80 }
81 
82 bool RBMQuantizer::computeFeatures(const VectorFloat &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( std::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( std::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( std::fstream &file ) const{
144 
145  if( !file.is_open() ){
146  errorLog << "saveModelToFile(fstream &file) - The file is not open!" << std::endl;
147  return false;
148  }
149 
150  //Write the header
151  file << "RBM_QUANTIZER_FILE_V1.0" << std::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!" << std::endl;
156  return false;
157  }
158 
159  file << "QuantizerTrained: " << trained << std::endl;
160  file << "NumClusters: " << numClusters << std::endl;
161 
162  if( trained ){
163  if( !rbm.saveModelToFile( file ) ){
164  errorLog << "saveModelToFile(fstream &file) - Failed to save RBM settings to file!" << std::endl;
165  return false;
166  }
167  }
168 
169  return true;
170 }
171 
172 bool RBMQuantizer::loadModelFromFile( std::fstream &file ){
173 
174  //Clear any previous model
175  clear();
176 
177  if( !file.is_open() ){
178  errorLog << "loadModelFromFile(fstream &file) - The file is not open!" << std::endl;
179  return false;
180  }
181 
182  std::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!" << std::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!" << std::endl;
194  return false;
195  }
196 
197  file >> word;
198  if( word != "QuantizerTrained:" ){
199  errorLog << "loadModelFromFile(fstream &file) - Failed to load QuantizerTrained!" << std::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!" << std::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!" << std::endl;
214  return false;
215  }
216  initialized = true;
217  featureDataReady = false;
218  quantizationDistances.resize(numClusters,0);
219  }
220 
221  return true;
222 }
223 
225  MatrixFloat data = trainingData.getDataAsMatrixFloat();
226  return train_( data );
227 }
228 
230  MatrixFloat data = trainingData.getDataAsMatrixFloat();
231  return train_( data );
232 }
233 
235  MatrixFloat data = trainingData.getDataAsMatrixFloat();
236  return train_( data );
237 }
238 
240  MatrixFloat data = trainingData.getDataAsMatrixFloat();
241  return train_( data );
242 }
243 
244 bool RBMQuantizer::train_(MatrixFloat &trainingData){
245 
246  //Clear any previous model
247  clear();
248 
249  if( trainingData.getNumRows() == 0 ){
250  errorLog << "train_(MatrixFloat &trainingData) - Failed to train quantizer, the training data is empty!" << std::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_(MatrixFloat &trainingData) - Failed to train quantizer!" << std::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 Float inputValue){
278  return quantize( VectorFloat(1,inputValue) );
279 }
280 
281 UINT RBMQuantizer::quantize(const VectorFloat &inputVector){
282 
283  if( !trained ){
284  errorLog << "quantize(const VectorFloat &inputVector) - The quantizer model has not been trained!" << std::endl;
285  return 0;
286  }
287 
288  if( inputVector.getSize() != numInputDimensions ){
289  errorLog << "quantize(const VectorFloat &inputVector) - The size of the inputVector (" << inputVector.getSize() << ") does not match that of the filter (" << numInputDimensions << ")!" << std::endl;
290  return 0;
291  }
292 
293  if( !rbm.predict( inputVector ) ){
294  errorLog << "quantize(const VectorFloat &inputVector) - Failed to quantize input!" << std::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  Float 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 GRT_END_NAMESPACE
343 
bool setLearningRate(const Float learningRate)
Definition: MLBase.cpp:291
virtual bool predict(VectorFloat inputVector)
Definition: MLBase.cpp:112
virtual bool deepCopyFrom(const FeatureExtraction *featureExtraction)
virtual bool loadModelFromFile(std::fstream &file)
UINT getNumClusters() const
bool saveFeatureExtractionSettingsToFile(std::fstream &file) const
virtual bool loadModelFromFile(std::string filename)
virtual bool clear()
MatrixFloat getDataAsMatrixFloat() const
virtual bool reset()
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
UINT quantize(const Float inputValue)
RBMQuantizer & operator=(const RBMQuantizer &rhs)
BernoulliRBM getBernoulliRBM() const
virtual bool clear()
virtual bool saveModelToFile(std::string filename) const
VectorFloat getQuantizationDistances() const
bool setNumClusters(const UINT numClusters)
bool setMinChange(const Float minChange)
Definition: MLBase.cpp:282
virtual bool reset()
unsigned int getSize() const
Definition: Vector.h:193
std::string getFeatureExtractionType() const
virtual bool reset()
virtual bool computeFeatures(const VectorFloat &inputVector)
virtual bool train_(ClassificationData &trainingData)
unsigned int getNumRows() const
Definition: Matrix.h:542
MatrixFloat getDataAsMatrixFloat() const
unsigned int getNumCols() const
Definition: Matrix.h:549
The SOMQuantizer module quantizes the N-dimensional input vector to a 1-dimensional discrete value...
bool setMinNumEpochs(const UINT minNumEpochs)
Definition: MLBase.cpp:277
UINT getQuantizedValue() const
bool loadFeatureExtractionSettingsFromFile(std::fstream &file)
virtual bool saveModelToFile(std::fstream &file) const
MatrixFloat getDataAsMatrixFloat() const
virtual bool train_(MatrixFloat &data)
bool getQuantizerTrained() const
bool copyBaseVariables(const FeatureExtraction *featureExtractionModule)
virtual ~RBMQuantizer()
bool setMaxNumEpochs(const UINT maxNumEpochs)
Definition: MLBase.cpp:268
RBMQuantizer(const UINT numClusters=10)