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.
Regressifier.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 #define GRT_DLL_EXPORTS
22 #include "Regressifier.h"
23 
24 GRT_BEGIN_NAMESPACE
25 
26 Regressifier::StringRegressifierMap* Regressifier::stringRegressifierMap = NULL;
27 UINT Regressifier::numRegressifierInstances = 0;
28 
29 Regressifier* Regressifier::createInstanceFromString( const std::string &regressifierType ){
30 
31  StringRegressifierMap::iterator iter = getMap()->find( regressifierType );
32  if( iter == getMap()->end() ){
33  return NULL;
34  }
35  return iter->second();
36 }
37 
39  return createInstanceFromString( regressifierType );
40 }
41 
43 
44  Regressifier *newInstance = createInstanceFromString( regressifierType );
45 
46  if( newInstance == NULL ) return NULL;
47 
48  if( !newInstance->deepCopyFrom( this ) ){
49  delete newInstance;
50  return NULL;
51  }
52  return newInstance;
53 }
54 
56  baseType = MLBase::REGRESSIFIER;
57  regressifierType = "NOT_SET";
58  numOutputDimensions = 0;
59  numRegressifierInstances++;
60 }
61 
63  if( --numRegressifierInstances == 0 ){
64  delete stringRegressifierMap;
65  stringRegressifierMap = NULL;
66  }
67 }
68 
70 
71  if( regressifier == NULL ){
72  errorLog << "copyBaseVariables(Regressifier *regressifier) - regressifier pointer is NULL!" << std::endl;
73  return false;
74  }
75 
76  if( !this->copyMLBaseVariables( regressifier ) ){
77  return false;
78  }
79 
80  this->regressifierType = regressifier->regressifierType;
81  this->regressionData = regressifier->regressionData;
82  this->inputVectorRanges = regressifier->inputVectorRanges;
83  this->targetVectorRanges = regressifier->targetVectorRanges;
84 
85  return true;
86 }
87 
89 
90  //Reset the base class
91  MLBase::reset();
92 
93  rootMeanSquaredTrainingError = 0;
94  totalSquaredTrainingError = 0;
95  return true;
96 }
97 
99 
100  //Clear the MLBase variables
101  MLBase::clear();
102 
103  //Clear the regressifier variables
104  rootMeanSquaredTrainingError = 0;
105  totalSquaredTrainingError = 0;
106  regressionData.clear();
107  inputVectorRanges.clear();
108  targetVectorRanges.clear();
109 
110  return true;
111 }
112 
114  return regressifierType;
115 }
116 
118  if( trained ){
119  return regressionData;
120  }
121  return VectorFloat();
122 }
123 
125  return inputVectorRanges;
126 }
127 
129  return targetVectorRanges;
130 }
131 
133  return *this;
134 }
135 
136 bool Regressifier::saveBaseSettingsToFile( std::fstream &file ) const{
137 
138  if( !file.is_open() ){
139  errorLog << "saveBaseSettingsToFile(fstream &file) - The file is not open!" << std::endl;
140  return false;
141  }
142 
143  if( !MLBase::saveBaseSettingsToFile( file ) ) return false;
144 
145  //Write the ranges to the file
146  if( useScaling ){
147  file << "InputVectorRanges: \n";
148  for(UINT j=0; j<numInputDimensions; j++){
149  file << inputVectorRanges[j].minValue << "\t" << inputVectorRanges[j].maxValue << std::endl;
150  }
151 
152  file << "OutputVectorRanges: \n";
153  for(UINT j=0; j<numOutputDimensions; j++){
154  file << targetVectorRanges[j].minValue << "\t" << targetVectorRanges[j].maxValue << std::endl;
155  }
156  }
157 
158  return true;
159 }
160 
161 bool Regressifier::loadBaseSettingsFromFile( std::fstream &file ){
162 
163  if( !file.is_open() ){
164  errorLog << "loadBaseSettingsFromFile(fstream &file) - The file is not open!" << std::endl;
165  return false;
166  }
167 
168  //Try and load the base settings from the file
169  if( !MLBase::loadBaseSettingsFromFile( file ) ){
170  return false;
171  }
172 
173  std::string word;
174 
175  if( useScaling ){
176  //Load the ranges
177  file >> word;
178  if( word != "InputVectorRanges:" ){
179  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read InputVectorRanges header!" << std::endl;
180  return false;
181  }
182  inputVectorRanges.resize(numInputDimensions);
183  for(UINT j=0; j<numInputDimensions; j++){
184  file >> inputVectorRanges[j].minValue;
185  file >> inputVectorRanges[j].maxValue;
186  }
187 
188  file >> word;
189  if( word != "OutputVectorRanges:" ){
190  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read OutputVectorRanges header!" << std::endl;
191  return false;
192  }
193  targetVectorRanges.resize(numOutputDimensions);
194  for(UINT j=0; j<numOutputDimensions; j++){
195  file >> targetVectorRanges[j].minValue;
196  file >> targetVectorRanges[j].maxValue;
197  }
198  }
199 
200  if( trained ){
201  //Resize the regression data Vector
202  regressionData.clear();
203  regressionData.resize(numOutputDimensions,0);
204  }
205 
206  return true;
207 }
208 
209 GRT_END_NAMESPACE
Vector< MinMax > getOutputRanges() const
bool saveBaseSettingsToFile(std::fstream &file) const
Definition: MLBase.cpp:375
std::map< std::string, Regressifier *(*)() > StringRegressifierMap
Definition: Regressifier.h:119
virtual bool reset()
Definition: MLBase.cpp:125
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
virtual bool reset()
Regressifier * createNewInstance() const
static Regressifier * createInstanceFromString(const std::string &regressifierType)
bool copyBaseVariables(const Regressifier *regressifier)
virtual bool deepCopyFrom(const Regressifier *regressifier)
Definition: Regressifier.h:63
bool saveBaseSettingsToFile(std::fstream &file) const
std::string getRegressifierType() const
bool copyMLBaseVariables(const MLBase *mlBase)
Definition: MLBase.cpp:50
bool loadBaseSettingsFromFile(std::fstream &file)
Vector< MinMax > getInputRanges() const
Regressifier * deepCopy() const
bool loadBaseSettingsFromFile(std::fstream &file)
Definition: MLBase.cpp:398
virtual ~Regressifier(void)
virtual bool clear()
Definition: MLBase.cpp:127
Regressifier(void)
const Regressifier & getBaseRegressifier() const
VectorFloat getRegressionData() const
virtual bool clear()
This is the main base class that all GRT Regression algorithms should inherit from.