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.
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 #include "Regressifier.h"
22 
23 GRT_BEGIN_NAMESPACE
24 
25 Regressifier::StringRegressifierMap* Regressifier::stringRegressifierMap = NULL;
26 UINT Regressifier::numRegressifierInstances = 0;
27 
28 Regressifier* Regressifier::createInstanceFromString( const std::string &regressifierType ){
29 
30  StringRegressifierMap::iterator iter = getMap()->find( regressifierType );
31  if( iter == getMap()->end() ){
32  return NULL;
33  }
34  return iter->second();
35 }
36 
38  return createInstanceFromString( regressifierType );
39 }
40 
42 
43  Regressifier *newInstance = createInstanceFromString( regressifierType );
44 
45  if( newInstance == NULL ) return NULL;
46 
47  if( !newInstance->deepCopyFrom( this ) ){
48  delete newInstance;
49  return NULL;
50  }
51  return newInstance;
52 }
53 
55  baseType = MLBase::REGRESSIFIER;
56  regressifierType = "NOT_SET";
57  numOutputDimensions = 0;
58  numRegressifierInstances++;
59 }
60 
62  if( --numRegressifierInstances == 0 ){
63  delete stringRegressifierMap;
64  stringRegressifierMap = NULL;
65  }
66 }
67 
69 
70  if( regressifier == NULL ){
71  errorLog << "copyBaseVariables(Regressifier *regressifier) - regressifier pointer is NULL!" << std::endl;
72  return false;
73  }
74 
75  if( !this->copyMLBaseVariables( regressifier ) ){
76  return false;
77  }
78 
79  this->regressifierType = regressifier->regressifierType;
80  this->regressionData = regressifier->regressionData;
81  this->inputVectorRanges = regressifier->inputVectorRanges;
82  this->targetVectorRanges = regressifier->targetVectorRanges;
83 
84  return true;
85 }
86 
88 
89  //Reset the base class
90  MLBase::reset();
91 
92  rootMeanSquaredTrainingError = 0;
93  totalSquaredTrainingError = 0;
94  return true;
95 }
96 
98 
99  //Clear the MLBase variables
100  MLBase::clear();
101 
102  //Clear the regressifier variables
103  rootMeanSquaredTrainingError = 0;
104  totalSquaredTrainingError = 0;
105  regressionData.clear();
106  inputVectorRanges.clear();
107  targetVectorRanges.clear();
108 
109  return true;
110 }
111 
113  return regressifierType;
114 }
115 
117  if( trained ){
118  return regressionData;
119  }
120  return VectorFloat();
121 }
122 
124  return inputVectorRanges;
125 }
126 
128  return targetVectorRanges;
129 }
130 
132  return *this;
133 }
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 
162 bool Regressifier::loadBaseSettingsFromFile( std::fstream &file ){
163 
164  if( !file.is_open() ){
165  errorLog << "loadBaseSettingsFromFile(fstream &file) - The file is not open!" << std::endl;
166  return false;
167  }
168 
169  //Try and load the base settings from the file
170  if( !MLBase::loadBaseSettingsFromFile( file ) ){
171  return false;
172  }
173 
174  std::string word;
175 
176  if( useScaling ){
177  //Load the ranges
178  file >> word;
179  if( word != "InputVectorRanges:" ){
180  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read InputVectorRanges header!" << std::endl;
181  return false;
182  }
183  inputVectorRanges.resize(numInputDimensions);
184  for(UINT j=0; j<numInputDimensions; j++){
185  file >> inputVectorRanges[j].minValue;
186  file >> inputVectorRanges[j].maxValue;
187  }
188 
189  file >> word;
190  if( word != "OutputVectorRanges:" ){
191  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read OutputVectorRanges header!" << std::endl;
192  return false;
193  }
194  targetVectorRanges.resize(numOutputDimensions);
195  for(UINT j=0; j<numOutputDimensions; j++){
196  file >> targetVectorRanges[j].minValue;
197  file >> targetVectorRanges[j].maxValue;
198  }
199  }
200 
201  if( trained ){
202  //Resize the regression data Vector
203  regressionData.clear();
204  regressionData.resize(numOutputDimensions,0);
205  }
206 
207  return true;
208 }
209 
210 GRT_END_NAMESPACE
Vector< MinMax > getOutputRanges() const
bool saveBaseSettingsToFile(std::fstream &file) const
Definition: MLBase.cpp:370
std::map< std::string, Regressifier *(*)() > StringRegressifierMap
Definition: Regressifier.h:119
virtual bool reset()
Definition: MLBase.cpp:124
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:49
bool loadBaseSettingsFromFile(std::fstream &file)
Vector< MinMax > getInputRanges() const
Regressifier * deepCopy() const
bool loadBaseSettingsFromFile(std::fstream &file)
Definition: MLBase.cpp:393
virtual ~Regressifier(void)
virtual bool clear()
Definition: MLBase.cpp:126
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.