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.
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 namespace GRT{
23 
24 Regressifier::StringRegressifierMap* Regressifier::stringRegressifierMap = NULL;
25 UINT Regressifier::numRegressifierInstances = 0;
26 
27 Regressifier* Regressifier::createInstanceFromString(string const &regressifierType){
28 
29  StringRegressifierMap::iterator iter = getMap()->find( regressifierType );
30  if( iter == getMap()->end() ){
31  return NULL;
32  }
33  return iter->second();
34 }
35 
37  return createInstanceFromString( regressifierType );
38 }
39 
41 
42  Regressifier *newInstance = createInstanceFromString( regressifierType );
43 
44  if( newInstance == NULL ) return NULL;
45 
46  if( !newInstance->deepCopyFrom( this ) ){
47  delete newInstance;
48  return NULL;
49  }
50  return newInstance;
51 }
52 
54  baseType = MLBase::REGRESSIFIER;
55  regressifierType = "NOT_SET";
56  numOutputDimensions = 0;
57  numRegressifierInstances++;
58 }
59 
61  if( --numRegressifierInstances == 0 ){
62  delete stringRegressifierMap;
63  stringRegressifierMap = NULL;
64  }
65 }
66 
68 
69  if( regressifier == NULL ){
70  errorLog << "copyBaseVariables(Regressifier *regressifier) - regressifier pointer is NULL!" << endl;
71  return false;
72  }
73 
74  if( !this->copyMLBaseVariables( regressifier ) ){
75  return false;
76  }
77 
78  this->regressifierType = regressifier->regressifierType;
79  this->regressionData = regressifier->regressionData;
80  this->inputVectorRanges = regressifier->inputVectorRanges;
81  this->targetVectorRanges = regressifier->targetVectorRanges;
82 
83  return true;
84 }
85 
87 
88  //Reset the base class
89  MLBase::reset();
90 
91  rootMeanSquaredTrainingError = 0;
92  totalSquaredTrainingError = 0;
93  return true;
94 }
95 
97 
98  //Clear the MLBase variables
99  MLBase::clear();
100 
101  //Clear the regressifier variables
102  rootMeanSquaredTrainingError = 0;
103  totalSquaredTrainingError = 0;
104  regressionData.clear();
105  inputVectorRanges.clear();
106  targetVectorRanges.clear();
107 
108  return true;
109 }
110 
112  return regressifierType;
113 }
114 
115 VectorDouble Regressifier::getRegressionData() const{
116  if( trained ){
117  return regressionData;
118  }
119  return VectorDouble();
120 }
121 
122 vector< MinMax > Regressifier::getInputRanges() const{
123  return inputVectorRanges;
124 }
125 
126 vector< MinMax > Regressifier::getOutputRanges() const{
127  return targetVectorRanges;
128 }
129 
131  return *this;
132 }
133 
134 
135 bool Regressifier::saveBaseSettingsToFile(fstream &file) const{
136 
137  if( !file.is_open() ){
138  errorLog << "saveBaseSettingsToFile(fstream &file) - The file is not open!" << endl;
139  return false;
140  }
141 
142  if( !MLBase::saveBaseSettingsToFile( file ) ) return false;
143 
144  //Write the ranges to the file
145  if( useScaling ){
146  file << "InputVectorRanges: \n";
147  for(UINT j=0; j<numInputDimensions; j++){
148  file << inputVectorRanges[j].minValue << "\t" << inputVectorRanges[j].maxValue << endl;
149  }
150 
151  file << "OutputVectorRanges: \n";
152  for(UINT j=0; j<numOutputDimensions; j++){
153  file << targetVectorRanges[j].minValue << "\t" << targetVectorRanges[j].maxValue << endl;
154  }
155  }
156 
157  return true;
158 }
159 
160 
162 
163  if( !file.is_open() ){
164  errorLog << "loadBaseSettingsFromFile(fstream &file) - The file is not open!" << 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  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!" << 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!" << 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 } //End of namespace GRT
210 
virtual bool reset()
Definition: MLBase.cpp:116
static Regressifier * createInstanceFromString(string const &regressifierType)
bool copyBaseVariables(const Regressifier *regressifier)
vector< MinMax > getOutputRanges() const
virtual bool clear()
Definition: MLBase.cpp:118
VectorDouble getRegressionData() const
bool loadBaseSettingsFromFile(fstream &file)
Definition: MLBase.cpp:357
Definition: AdaBoost.cpp:25
bool saveBaseSettingsToFile(fstream &file) const
Definition: MLBase.cpp:334
vector< MinMax > getInputRanges() const
virtual ~Regressifier(void)
virtual bool reset()
bool copyMLBaseVariables(const MLBase *mlBase)
Definition: MLBase.cpp:46
bool loadBaseSettingsFromFile(fstream &file)
std::map< string, Regressifier *(*)() > StringRegressifierMap
Definition: Regressifier.h:119
bool saveBaseSettingsToFile(fstream &file) const
Regressifier * createNewInstance() const
Regressifier * deepCopy() const
string getRegressifierType() const
const Regressifier & getBaseRegressifier() const
virtual bool clear()
virtual bool deepCopyFrom(const Regressifier *regressifier)
Definition: Regressifier.h:63
This is the main base class that all GRT Regression algorithms should inherit from.