GestureRecognitionToolkit  Version: 0.2.5
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 
30 Regressifier* Regressifier::createNewInstance() const{ return create(); } //Legacy
31 Regressifier* Regressifier::createInstanceFromString( const std::string &id ){ return create(id); } //Legacy
32 std::string Regressifier::getRegressifierType() const{ return MLBase::getId(); } //Legacy
33 
34 Regressifier* Regressifier::create( const std::string &id ){
35 
36  StringRegressifierMap::iterator iter = getMap()->find( id );
37  if( iter == getMap()->end() ){
38  return NULL;
39  }
40  return iter->second();
41 }
42 
44  return create( MLBase::getId() );
45 }
46 
48 
49  Regressifier *newInstance = create( classId );
50 
51  if( newInstance == NULL ) return NULL;
52 
53  if( !newInstance->deepCopyFrom( this ) ){
54  delete newInstance;
55  return NULL;
56  }
57  return newInstance;
58 }
59 
60 Regressifier::Regressifier( const std::string &id ) : MLBase( id, MLBase::REGRESSIFIER )
61 {
62  numOutputDimensions = 0;
63  numRegressifierInstances++;
64 }
65 
67  if( --numRegressifierInstances == 0 ){
68  delete stringRegressifierMap;
69  stringRegressifierMap = NULL;
70  }
71 }
72 
74  Vector< std::string > registeredRegressifiers;
75 
76  StringRegressifierMap::iterator iter = getMap()->begin();
77  while( iter != getMap()->end() ){
78  registeredRegressifiers.push_back( iter->first );
79  ++iter; //++iter is faster than iter++ as it does not require a copy/move operator
80  }
81  return registeredRegressifiers;
82 }
83 
85 
86  if( regressifier == NULL ){
87  errorLog << "copyBaseVariables(Regressifier *regressifier) - regressifier pointer is NULL!" << std::endl;
88  return false;
89  }
90 
91  if( !this->copyMLBaseVariables( regressifier ) ){
92  return false;
93  }
94 
95  this->regressifierType = regressifier->regressifierType;
96  this->regressionData = regressifier->regressionData;
97  this->inputVectorRanges = regressifier->inputVectorRanges;
98  this->targetVectorRanges = regressifier->targetVectorRanges;
99 
100  return true;
101 }
102 
104 
105  //Reset the base class
106  MLBase::reset();
107 
108  return true;
109 }
110 
112 
113  //Clear the MLBase variables
114  MLBase::clear();
115 
116  //Clear the regressifier variables
117  regressionData.clear();
118  inputVectorRanges.clear();
119  targetVectorRanges.clear();
120 
121  return true;
122 }
123 
125  if( trained ){
126  return regressionData;
127  }
128  return VectorFloat();
129 }
130 
132  return inputVectorRanges;
133 }
134 
136  return targetVectorRanges;
137 }
138 
140  return *this;
141 }
142 
143 bool Regressifier::saveBaseSettingsToFile( std::fstream &file ) const{
144 
145  if( !file.is_open() ){
146  errorLog << "saveBaseSettingsToFile(fstream &file) - The file is not open!" << std::endl;
147  return false;
148  }
149 
150  if( !MLBase::saveBaseSettingsToFile( file ) ) return false;
151 
152  //Write the ranges to the file
153  if( useScaling ){
154  file << "InputVectorRanges: \n";
155  for(UINT j=0; j<numInputDimensions; j++){
156  file << inputVectorRanges[j].minValue << "\t" << inputVectorRanges[j].maxValue << std::endl;
157  }
158 
159  file << "OutputVectorRanges: \n";
160  for(UINT j=0; j<numOutputDimensions; j++){
161  file << targetVectorRanges[j].minValue << "\t" << targetVectorRanges[j].maxValue << std::endl;
162  }
163  }
164 
165  return true;
166 }
167 
168 bool Regressifier::loadBaseSettingsFromFile( std::fstream &file ){
169 
170  if( !file.is_open() ){
171  errorLog << "loadBaseSettingsFromFile(fstream &file) - The file is not open!" << std::endl;
172  return false;
173  }
174 
175  //Try and load the base settings from the file
176  if( !MLBase::loadBaseSettingsFromFile( file ) ){
177  return false;
178  }
179 
180  std::string word;
181 
182  if( useScaling ){
183  //Load the ranges
184  file >> word;
185  if( word != "InputVectorRanges:" ){
186  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read InputVectorRanges header!" << std::endl;
187  return false;
188  }
189  inputVectorRanges.resize(numInputDimensions);
190  for(UINT j=0; j<numInputDimensions; j++){
191  file >> inputVectorRanges[j].minValue;
192  file >> inputVectorRanges[j].maxValue;
193  }
194 
195  file >> word;
196  if( word != "OutputVectorRanges:" ){
197  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read OutputVectorRanges header!" << std::endl;
198  return false;
199  }
200  targetVectorRanges.resize(numOutputDimensions);
201  for(UINT j=0; j<numOutputDimensions; j++){
202  file >> targetVectorRanges[j].minValue;
203  file >> targetVectorRanges[j].maxValue;
204  }
205  }
206 
207  if( trained ){
208  //Resize the regression data Vector
209  regressionData.clear();
210  regressionData.resize(numOutputDimensions,0);
211  }
212 
213  return true;
214 }
215 
216 GRT_END_NAMESPACE
Vector< MinMax > getOutputRanges() const
bool saveBaseSettingsToFile(std::fstream &file) const
Definition: MLBase.cpp:435
std::string getId() const
Definition: GRTBase.cpp:85
std::map< std::string, Regressifier *(*)() > StringRegressifierMap
Definition: Regressifier.h:113
virtual bool reset()
Definition: MLBase.cpp:147
virtual bool clear() override
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
Regressifier * create() const
bool copyBaseVariables(const Regressifier *regressifier)
virtual bool reset() override
static Vector< std::string > getRegisteredRegressifiers()
virtual bool deepCopyFrom(const Regressifier *regressifier)
Definition: Regressifier.h:64
bool saveBaseSettingsToFile(std::fstream &file) const
bool copyMLBaseVariables(const MLBase *mlBase)
Definition: MLBase.cpp:62
bool loadBaseSettingsFromFile(std::fstream &file)
Vector< MinMax > getInputRanges() const
Regressifier * deepCopy() const
bool loadBaseSettingsFromFile(std::fstream &file)
Definition: MLBase.cpp:458
virtual ~Regressifier(void)
virtual bool clear()
Definition: MLBase.cpp:149
const Regressifier & getBaseRegressifier() const
VectorFloat getRegressionData() const
std::string classId
Stores the name of the class (e.g., MinDist)
Definition: GRTBase.h:197
Definition: Vector.h:41
This is the main base class that all GRT machine learning algorithms should inherit from...
Definition: MLBase.h:72
This is the main base class that all GRT Regression algorithms should inherit from.
Regressifier(const std::string &id="")