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.
MLBase.cpp
1 
21 #define GRT_DLL_EXPORTS
22 #include "MLBase.h"
23 
24 GRT_BEGIN_NAMESPACE
25 
27  trained = false;
28  useScaling = false;
29  baseType = BASE_TYPE_NOT_SET;
30  inputType = DATA_TYPE_UNKNOWN;
31  outputType = DATA_TYPE_UNKNOWN;
32  numInputDimensions = 0;
33  numOutputDimensions = 0;
34  minNumEpochs = 0;
35  maxNumEpochs = 100;
36  validationSetSize = 20;
37  validationSetAccuracy = 0;
38  minChange = 1.0e-5;
39  learningRate = 0.1;
40  useValidationSet = false;
41  randomiseTrainingOrder = true;
42  rootMeanSquaredTrainingError = 0;
43  totalSquaredTrainingError = 0;
44 }
45 
47  clear();
48 }
49 
50 bool MLBase::copyMLBaseVariables(const MLBase *mlBase){
51 
52  if( mlBase == NULL ){
53  errorLog << "copyMLBaseVariables(MLBase *mlBase) - mlBase pointer is NULL!" << std::endl;
54  return false;
55  }
56 
57  if( !copyGRTBaseVariables( mlBase ) ){
58  errorLog << "copyMLBaseVariables(MLBase *mlBase) - Failed to copy GRT Base variables!" << std::endl;
59  return false;
60  }
61 
62  this->trained = mlBase->trained;
63  this->useScaling = mlBase->useScaling;
64  this->baseType = mlBase->baseType;
65  this->inputType = mlBase->inputType;
66  this->outputType = mlBase->outputType;
67  this->numInputDimensions = mlBase->numInputDimensions;
68  this->numOutputDimensions = mlBase->numOutputDimensions;
69  this->minNumEpochs = mlBase->minNumEpochs;
70  this->maxNumEpochs = mlBase->maxNumEpochs;
71  this->validationSetSize = mlBase->validationSetSize;
72  this->validationSetAccuracy = mlBase->validationSetAccuracy;
73  this->validationSetPrecision = mlBase->validationSetPrecision;
74  this->validationSetRecall = mlBase->validationSetRecall;
75  this->minChange = mlBase->minChange;
76  this->learningRate = mlBase->learningRate;
77  this->rootMeanSquaredTrainingError = mlBase->rootMeanSquaredTrainingError;
78  this->totalSquaredTrainingError = mlBase->totalSquaredTrainingError;
79  this->useValidationSet = mlBase->useValidationSet;
80  this->randomiseTrainingOrder = mlBase->randomiseTrainingOrder;
81  this->numTrainingIterationsToConverge = mlBase->numTrainingIterationsToConverge;
82  this->trainingResults = mlBase->trainingResults;
83  this->trainingResultsObserverManager = mlBase->trainingResultsObserverManager;
84  this->testResultsObserverManager = mlBase->testResultsObserverManager;
85 
86  return true;
87 }
88 
89 bool MLBase::train(ClassificationData trainingData){ return train_( trainingData ); }
90 
91 bool MLBase::train_(ClassificationData &trainingData){ return false; }
92 
93 bool MLBase::train(RegressionData trainingData){ return train_( trainingData ); }
94 
95 bool MLBase::train_(RegressionData &trainingData){ return false; }
96 
97 bool MLBase::train(TimeSeriesClassificationData trainingData){ return train_( trainingData ); }
98 
99 bool MLBase::train_(TimeSeriesClassificationData &trainingData){ return false; }
100 
101 bool MLBase::train(ClassificationDataStream trainingData){ return train_( trainingData ); }
102 
103 bool MLBase::train_(ClassificationDataStream &trainingData){ return false; }
104 
105 bool MLBase::train(UnlabelledData trainingData){ return train_( trainingData ); }
106 
107 bool MLBase::train_(UnlabelledData &trainingData){ return false; }
108 
109 bool MLBase::train(MatrixFloat data){ return train_( data ); }
110 
111 bool MLBase::train_(MatrixFloat &data){ return false; }
112 
113 bool MLBase::predict(VectorFloat inputVector){ return predict_( inputVector ); }
114 
115 bool MLBase::predict_(VectorFloat &inputVector){ return false; }
116 
117 bool MLBase::predict(MatrixFloat inputMatrix){ return predict_( inputMatrix ); }
118 
119 bool MLBase::predict_(MatrixFloat &inputMatrix){ return false; }
120 
121 bool MLBase::map(VectorFloat inputVector){ return map_( inputVector ); }
122 
123 bool MLBase::map_(VectorFloat &inputVector){ return false; }
124 
125 bool MLBase::reset(){ return true; }
126 
128  trained = false;
129  numInputDimensions = 0;
130  numOutputDimensions = 0;
131  numTrainingIterationsToConverge = 0;
132  rootMeanSquaredTrainingError = 0;
133  totalSquaredTrainingError = 0;
134  trainingResults.clear();
135  validationSetPrecision.clear();
136  validationSetRecall.clear();
137  validationSetAccuracy = 0;
138  return true;
139 }
140 
141 bool MLBase::print() const { std::cout << getModelAsString(); return true; }
142 
143 bool MLBase::save(const std::string filename) const {
144 
145  if( !trained ) return false;
146 
147  std::fstream file;
148  file.open(filename.c_str(), std::ios::out);
149 
150  if( !save( file ) ){
151  return false;
152  }
153 
154  file.close();
155 
156  return true;
157 }
158 
159 bool MLBase::save(std::fstream &file) const {
160  return false; //The base class returns false, as this should be overwritten by the inheriting class
161 }
162 
163 bool MLBase::saveModelToFile(std::string filename) const { return save( filename ); }
164 
165 bool MLBase::saveModelToFile(std::fstream &file) const { return save( file ); }
166 
167 bool MLBase::load(const std::string filename){
168 
169  std::fstream file;
170  file.open(filename.c_str(), std::ios::in);
171 
172  if( !load( file ) ){
173  return false;
174  }
175 
176  //Close the file
177  file.close();
178 
179  return true;
180 }
181 
182 bool MLBase::load(std::fstream &file) {
183  return false; //The base class returns false, as this should be overwritten by the inheriting class
184 }
185 
186 bool MLBase::loadModelFromFile(std::string filename){ return load( filename ); }
187 
188 bool MLBase::loadModelFromFile(std::fstream &file){ return load( file ); }
189 
190 bool MLBase::getModel(std::ostream &stream) const { return true; }
191 
192 std::string MLBase::getModelAsString() const{
193  std::stringstream stream;
194  if( getModel( stream ) ){
195  return stream.str();
196  }
197  return "";
198 }
199 
200 DataType MLBase::getInputType() const {
201  return inputType;
202 }
203 
204 DataType MLBase::getOutputType() const {
205  return outputType;
206 }
207 
208 UINT MLBase::getBaseType() const{ return baseType; }
209 
211 
212 UINT MLBase::getNumInputDimensions() const{ return numInputDimensions; }
213 
214 UINT MLBase::getNumOutputDimensions() const{ return numOutputDimensions; }
215 
217  if( trained ){
218  return numTrainingIterationsToConverge;
219  }
220  return 0;
221 }
222 
224  return minNumEpochs;
225 }
226 
228  return maxNumEpochs;
229 }
230 
232  return validationSetSize;
233 }
234 
236  return learningRate;
237 }
238 
240  return rootMeanSquaredTrainingError;
241 }
242 
244  return totalSquaredTrainingError;
245 }
246 
248  return validationSetAccuracy;
249 }
250 
252  return validationSetPrecision;
253 }
254 
256  return validationSetRecall;
257 }
258 
259 bool MLBase::getTrained() const{ return trained; }
260 
261 bool MLBase::getModelTrained() const{ return getTrained(); }
262 
263 bool MLBase::getScalingEnabled() const{ return useScaling; }
264 
265 bool MLBase::getIsBaseTypeClassifier() const{ return baseType==CLASSIFIER; }
266 
267 bool MLBase::getIsBaseTypeRegressifier() const{ return baseType==REGRESSIFIER; }
268 
269 bool MLBase::getIsBaseTypeClusterer() const{ return baseType==CLUSTERER; }
270 
271 bool MLBase::enableScaling(bool useScaling){ this->useScaling = useScaling; return true; }
272 
273 bool MLBase::setMaxNumEpochs(const UINT maxNumEpochs){
274  if( maxNumEpochs == 0 ){
275  warningLog << "setMaxNumEpochs(const UINT maxNumEpochs) - The maxNumEpochs must be greater than 0!" << std::endl;
276  return false;
277  }
278  this->maxNumEpochs = maxNumEpochs;
279  return true;
280 }
281 
282 bool MLBase::setMinNumEpochs(const UINT minNumEpochs){
283  this->minNumEpochs = minNumEpochs;
284  return true;
285 }
286 
287 bool MLBase::setMinChange(const Float minChange){
288  if( minChange < 0 ){
289  warningLog << "setMinChange(const Float minChange) - The minChange must be greater than or equal to 0!" << std::endl;
290  return false;
291  }
292  this->minChange = minChange;
293  return true;
294 }
295 
296 bool MLBase::setLearningRate(const Float learningRate){
297  if( learningRate > 0 ){
298  this->learningRate = learningRate;
299  return true;
300  }
301  return false;
302 }
303 
304 bool MLBase::setValidationSetSize(const UINT validationSetSize){
305 
306  if( validationSetSize > 0 && validationSetSize < 100 ){
307  this->validationSetSize = validationSetSize;
308  return true;
309  }
310 
311  warningLog << "setValidationSetSize(const UINT validationSetSize) - The validation size must be in the range [1 99]!" << std::endl;
312 
313  return false;
314 }
315 
316 bool MLBase::setUseValidationSet(const bool useValidationSet){
317  this->useValidationSet = useValidationSet;
318  return true;
319 }
320 
321 bool MLBase::setRandomiseTrainingOrder(const bool randomiseTrainingOrder){
322  this->randomiseTrainingOrder = randomiseTrainingOrder;
323  return true;
324 }
325 
326 bool MLBase::setTrainingLoggingEnabled(const bool loggingEnabled){
327  this->trainingLog.setEnableInstanceLogging( loggingEnabled );
328  return true;
329 }
330 
332  return trainingResultsObserverManager.registerObserver( observer );
333 }
334 
336  return testResultsObserverManager.registerObserver( observer );
337 }
338 
340  return trainingResultsObserverManager.removeObserver( observer );
341 }
342 
344  return testResultsObserverManager.removeObserver( observer );
345 }
346 
348  return trainingResultsObserverManager.removeAllObservers();
349 }
350 
352  return testResultsObserverManager.removeAllObservers();
353 }
354 
355 bool MLBase::notifyTrainingResultsObservers( const TrainingResult &data ){
356  return trainingResultsObserverManager.notifyObservers( data );
357 }
358 
359 bool MLBase::notifyTestResultsObservers( const TestInstanceResult &data ){
360  return testResultsObserverManager.notifyObservers( data );
361 }
362 
364  return this;
365 }
366 
368  return this;
369 }
370 
372  return trainingResults;
373 }
374 
375 bool MLBase::saveBaseSettingsToFile( std::fstream &file ) const{
376 
377  if( !file.is_open() ){
378  errorLog << "saveBaseSettingsToFile(fstream &file) - The file is not open!" << std::endl;
379  return false;
380  }
381 
382  file << "Trained: " << trained << std::endl;
383  file << "UseScaling: " << useScaling << std::endl;
384  file << "NumInputDimensions: " << numInputDimensions << std::endl;
385  file << "NumOutputDimensions: " << numOutputDimensions << std::endl;
386  file << "NumTrainingIterationsToConverge: " << numTrainingIterationsToConverge << std::endl;
387  file << "MinNumEpochs: " << minNumEpochs << std::endl;
388  file << "MaxNumEpochs: " << maxNumEpochs << std::endl;
389  file << "ValidationSetSize: " << validationSetSize << std::endl;
390  file << "LearningRate: " << learningRate << std::endl;
391  file << "MinChange: " << minChange << std::endl;
392  file << "UseValidationSet: " << useValidationSet << std::endl;
393  file << "RandomiseTrainingOrder: " << randomiseTrainingOrder << std::endl;
394 
395  return true;
396 }
397 
398 bool MLBase::loadBaseSettingsFromFile( std::fstream &file ){
399 
400  //Clear any previous setup
401  clear();
402 
403  if( !file.is_open() ){
404  errorLog << "loadBaseSettingsFromFile(fstream &file) - The file is not open!" << std::endl;
405  return false;
406  }
407 
408  std::string word;
409 
410  //Load the trained state
411  file >> word;
412  if( word != "Trained:" ){
413  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read Trained header!" << std::endl;
414  return false;
415  }
416  file >> trained;
417 
418  //Load the scaling state
419  file >> word;
420  if( word != "UseScaling:" ){
421  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read UseScaling header!" << std::endl;
422  return false;
423  }
424  file >> useScaling;
425 
426  //Load the NumInputDimensions
427  file >> word;
428  if( word != "NumInputDimensions:" ){
429  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read NumInputDimensions header!" << std::endl;
430  return false;
431  }
432  file >> numInputDimensions;
433 
434  //Load the NumOutputDimensions
435  file >> word;
436  if( word != "NumOutputDimensions:" ){
437  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read NumOutputDimensions header!" << std::endl;
438  return false;
439  }
440  file >> numOutputDimensions;
441 
442  //Load the numTrainingIterationsToConverge
443  file >> word;
444  if( word != "NumTrainingIterationsToConverge:" ){
445  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read NumTrainingIterationsToConverge header!" << std::endl;
446  return false;
447  }
448  file >> numTrainingIterationsToConverge;
449 
450  //Load the MinNumEpochs
451  file >> word;
452  if( word != "MinNumEpochs:" ){
453  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read MinNumEpochs header!" << std::endl;
454  return false;
455  }
456  file >> minNumEpochs;
457 
458  //Load the maxNumEpochs
459  file >> word;
460  if( word != "MaxNumEpochs:" ){
461  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read MaxNumEpochs header!" << std::endl;
462  return false;
463  }
464  file >> maxNumEpochs;
465 
466  //Load the ValidationSetSize
467  file >> word;
468  if( word != "ValidationSetSize:" ){
469  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read ValidationSetSize header!" << std::endl;
470  return false;
471  }
472  file >> validationSetSize;
473 
474  //Load the LearningRate
475  file >> word;
476  if( word != "LearningRate:" ){
477  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read LearningRate header!" << std::endl;
478  return false;
479  }
480  file >> learningRate;
481 
482  //Load the MinChange
483  file >> word;
484  if( word != "MinChange:" ){
485  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read MinChange header!" << std::endl;
486  return false;
487  }
488  file >> minChange;
489 
490  //Load the UseValidationSet
491  file >> word;
492  if( word != "UseValidationSet:" ){
493  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read UseValidationSet header!" << std::endl;
494  return false;
495  }
496  file >> useValidationSet;
497 
498  //Load the RandomiseTrainingOrder
499  file >> word;
500  if( word != "RandomiseTrainingOrder:" ){
501  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read RandomiseTrainingOrder header!" << std::endl;
502  return false;
503  }
504  file >> randomiseTrainingOrder;
505 
506  return true;
507 }
508 
509 GRT_END_NAMESPACE
bool saveBaseSettingsToFile(std::fstream &file) const
Definition: MLBase.cpp:375
bool setLearningRate(const Float learningRate)
Definition: MLBase.cpp:296
virtual bool predict(VectorFloat inputVector)
Definition: MLBase.cpp:113
bool setRandomiseTrainingOrder(const bool randomiseTrainingOrder)
Definition: MLBase.cpp:321
bool notifyTrainingResultsObservers(const TrainingResult &data)
Definition: MLBase.cpp:355
bool registerTrainingResultsObserver(Observer< TrainingResult > &observer)
Definition: MLBase.cpp:331
virtual bool predict_(VectorFloat &inputVector)
Definition: MLBase.cpp:115
virtual bool reset()
Definition: MLBase.cpp:125
bool setTrainingLoggingEnabled(const bool loggingEnabled)
Definition: MLBase.cpp:326
Float getLearningRate() const
Definition: MLBase.cpp:235
bool removeAllTestObservers()
Definition: MLBase.cpp:351
bool enableScaling(const bool useScaling)
Definition: MLBase.cpp:271
DataType getOutputType() const
Definition: MLBase.cpp:204
virtual bool getModel(std::ostream &stream) const
Definition: MLBase.cpp:190
virtual bool train(ClassificationData trainingData)
Definition: MLBase.cpp:89
bool getTrained() const
Definition: MLBase.cpp:259
Float getRootMeanSquaredTrainingError() const
Definition: MLBase.cpp:239
UINT getMinNumEpochs() const
Definition: MLBase.cpp:223
UINT getNumOutputDimensions() const
Definition: MLBase.cpp:214
bool getScalingEnabled() const
Definition: MLBase.cpp:263
bool registerTestResultsObserver(Observer< TestInstanceResult > &observer)
Definition: MLBase.cpp:335
bool setMinChange(const Float minChange)
Definition: MLBase.cpp:287
UINT getValidationSetSize() const
Definition: MLBase.cpp:231
virtual bool save(const std::string filename) const
Definition: MLBase.cpp:143
virtual bool load(const std::string filename)
Definition: MLBase.cpp:167
UINT getMaxNumEpochs() const
Definition: MLBase.cpp:227
This is the main base class that all GRT machine learning algorithms should inherit from...
bool getModelTrained() const
Definition: MLBase.cpp:261
Float getTotalSquaredTrainingError() const
Definition: MLBase.cpp:243
bool setValidationSetSize(const UINT validationSetSize)
Definition: MLBase.cpp:304
bool copyMLBaseVariables(const MLBase *mlBase)
Definition: MLBase.cpp:50
virtual bool print() const
Definition: MLBase.cpp:141
UINT getBaseType() const
Definition: MLBase.cpp:208
Float getValidationSetAccuracy() const
Definition: MLBase.cpp:247
bool getIsBaseTypeClusterer() const
Definition: MLBase.cpp:269
virtual std::string getModelAsString() const
Definition: MLBase.cpp:192
bool setMinNumEpochs(const UINT minNumEpochs)
Definition: MLBase.cpp:282
MLBase(void)
Definition: MLBase.cpp:26
bool loadBaseSettingsFromFile(std::fstream &file)
Definition: MLBase.cpp:398
MLBase * getMLBasePointer()
Definition: MLBase.cpp:363
bool removeAllTrainingObservers()
Definition: MLBase.cpp:347
UINT getNumInputFeatures() const
Definition: MLBase.cpp:210
virtual bool clear()
Definition: MLBase.cpp:127
virtual ~MLBase(void)
Definition: MLBase.cpp:46
virtual bool train_(ClassificationData &trainingData)
Definition: MLBase.cpp:91
bool notifyTestResultsObservers(const TestInstanceResult &data)
Definition: MLBase.cpp:359
bool copyGRTBaseVariables(const GRTBase *GRTBase)
Definition: GRTBase.cpp:33
bool getIsBaseTypeClassifier() const
Definition: MLBase.cpp:265
VectorFloat getValidationSetPrecision() const
Definition: MLBase.cpp:251
DataType getInputType() const
Definition: MLBase.cpp:200
bool removeTrainingResultsObserver(const Observer< TrainingResult > &observer)
Definition: MLBase.cpp:339
Definition: Vector.h:41
UINT getNumInputDimensions() const
Definition: MLBase.cpp:212
bool removeTestResultsObserver(const Observer< TestInstanceResult > &observer)
Definition: MLBase.cpp:343
virtual bool map_(VectorFloat &inputVector)
Definition: MLBase.cpp:123
UINT getNumTrainingIterationsToConverge() const
Definition: MLBase.cpp:216
bool setUseValidationSet(const bool useValidationSet)
Definition: MLBase.cpp:316
bool setMaxNumEpochs(const UINT maxNumEpochs)
Definition: MLBase.cpp:273
Vector< TrainingResult > getTrainingResults() const
Definition: MLBase.cpp:371
virtual bool map(VectorFloat inputVector)
Definition: MLBase.cpp:121
VectorFloat getValidationSetRecall() const
Definition: MLBase.cpp:255
Definition: MLBase.h:70
bool getIsBaseTypeRegressifier() const
Definition: MLBase.cpp:267