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.
AdaBoostClassModel.h
Go to the documentation of this file.
1 
29 #ifndef GRT_ADABOOST_CLASS_MODEL_HEADER
30 #define GRT_ADABOOST_CLASS_MODEL_HEADER
31 
33 
34 GRT_BEGIN_NAMESPACE
35 
37 public:
39  classLabel = 0;
40  debugLog.setProceedingText("[DEBUG AdaBoostClassModel]");
41  errorLog.setProceedingText("[ERROR AdaBoostClassModel]");
42  warningLog.setProceedingText("[WARNING AdaBoostClassModel]");
43  }
45  clear();
46  }
47 
49  *this = rhs;
50  }
51 
52  AdaBoostClassModel& operator=(const AdaBoostClassModel &rhs){
53  if( this != &rhs ){
54  clear();
55  this->classLabel = rhs.classLabel;
56 
57  //We need to deep copy the weak classifiers
58  for(UINT i=0; i<rhs.getNumWeakClassifiers(); i++){
59  if( !addClassifierToCommitee(rhs.weakClassifiers[i], rhs.weights[i]) ){
60  clear();
61  errorLog << "operator= Failed to deep copy weak classifiers!" << std::endl;
62  break;
63  }
64  }
65  }
66  return *this;
67  }
68 
69  bool clear(){
70  classLabel = 0;
71  weights.clear();
72  for(UINT i=0; i<weakClassifiers.size(); i++){
73  if( weakClassifiers[i] != NULL ){
74  delete weakClassifiers[i];
75  weakClassifiers[i] = NULL;
76  }
77  }
78  weakClassifiers.clear();
79  return true;
80  }
81 
82  bool setClassLabel(const UINT classLabel){
83  this->classLabel = classLabel;
84  return true;
85  }
86 
87  bool addClassifierToCommitee(const WeakClassifier *weakClassifier,Float weight){
88  if( weakClassifier == NULL ) return false;
89 
90  //Deep copy the weak classifier
91  WeakClassifier *weakClassifierPtr = weakClassifier->createNewInstance();
92  if( !weakClassifierPtr->deepCopyFrom( weakClassifier ) ){
93  delete weakClassifierPtr;
94  weakClassifierPtr = NULL;
95  warningLog << "addClassifierToCommitee(...) Failed to add weak classifier to commitee!" << std::endl;
96  return false;
97  }
98  weights.push_back( weight );
99  weakClassifiers.push_back( weakClassifierPtr );
100 
101  return true;
102  }
103 
104  Float predict(const VectorFloat &inputVector){
105  Float v = 0;
106  UINT N = (UINT)weakClassifiers.getSize();
107  for(UINT i=0; i<N; i++){
108  v += weakClassifiers[i]->predict( inputVector ) * weights[i];
109  }
110  //Return the value, rather than the sign of the value
111  return v;
112  }
113 
114  void print() const{
115  std::cout << "ClassLabel: " << classLabel << std::endl;
116  for(UINT i=0; i<weakClassifiers.getSize(); i++){
117  std::cout << "Weight: " << weights[i] << std::endl;
118  weakClassifiers[i]->print();
119  }
120  }
121 
122  UINT getClassLabel() const{
123  return classLabel;
124  }
125 
126  UINT getNumWeakClassifiers() const{
127  return weakClassifiers.getSize();
128  }
129 
130  Vector< WeakClassifier* > getWeakClassifiers() const{
131 
132  Vector< WeakClassifier* > classifiers;
133 
134  for(UINT i=0; i<getNumWeakClassifiers(); i++){
135 
136  //Deep copy the weak classifier
137  WeakClassifier *weakClassifierPtr = weakClassifiers[i]->createNewInstance();
138  if( !weakClassifierPtr->deepCopyFrom( weakClassifiers[i] ) ){
139  return classifiers;
140  }
141  classifiers.push_back( weakClassifierPtr );
142  }
143 
144  return classifiers;
145  }
146 
147  template< class T > T* getWeakClassifier(const UINT &index){
148 
149  if( index >= weakClassifiers.getSize() ) return NULL;
150 
151  T temp;
152 
153  if( temp.getWeakClassifierType() == weakClassifiers[index]->getWeakClassifierType() ){
154  return (T*)weakClassifiers[index];
155  }
156  return NULL;
157  }
158 
159  VectorFloat getWeights() const{
160  return weights;
161  }
162 
163  bool saveModelToFile( std::fstream &file ) const{
164 
165  if(!file.is_open())
166  {
167  return false;
168  }
169 
170  UINT N = getNumWeakClassifiers();
171 
172  //Write the Model info
173  file << "ClassLabel: " << classLabel << std::endl;
174  file << "NumWeakClassifiers: "<< getNumWeakClassifiers() << std::endl;
175  file << "WeakClassifierTypes: " << std::endl;
176  for(UINT i=0; i<N; i++){
177  if( weakClassifiers[i] == NULL ) return false;
178  file << weakClassifiers[i]->getWeakClassifierType() << std::endl;
179  }
180 
181  file << "Weights: ";
182  for(UINT i=0; i<N; i++){
183  file << weights[i] << " ";
184  }
185  file << std::endl;
186 
187  file << "WeakClassifiers: " << std::endl;
188  for(UINT i=0; i<N; i++){
189  if( weakClassifiers[i] == NULL ) return false;
190  if( !weakClassifiers[i]->saveModelToFile( file ) ) return false;
191  }
192 
193  return true;
194  }
195 
196  bool loadModelFromFile( std::fstream &file ){
197 
198  //Clear any previous models
199  clear();
200 
201  if(!file.is_open())
202  {
203  errorLog <<"loadModelFromFile(fstream &file) - The file is not open!" << std::endl;
204  return false;
205  }
206 
207  std::string word;
208  UINT numWeakClassifiers = 0;
209 
210  file >> word;
211  if( word != "ClassLabel:" ){
212  errorLog <<"loadModelFromFile(fstream &file) - Failed to read ClassLabel header!" << std::endl;
213  return false;
214  }
215  file >> classLabel;
216 
217  file >> word;
218  if( word != "NumWeakClassifiers:" ){
219  errorLog <<"loadModelFromFile(fstream &file) - Failed to read NumWeakClassifiers header!" << std::endl;
220  return false;
221  }
222  file >> numWeakClassifiers;
223 
224  file >> word;
225  if( word != "WeakClassifierTypes:" ){
226  errorLog <<"loadModelFromFile(fstream &file) - Failed to read WeakClassifierTypes header!" << std::endl;
227  return false;
228  }
229 
230  //Load the weak classifier types and setup the weak classifiers
231  if( numWeakClassifiers > 0 ){
232  weights.resize( numWeakClassifiers, 0 );
233  weakClassifiers.resize( numWeakClassifiers, NULL );
234 
235  for(UINT i=0; i<numWeakClassifiers; i++){
236  file >> word;
237  weakClassifiers[i] = WeakClassifier::createInstanceFromString(word);
238  if( weakClassifiers[i] == NULL ){
239  errorLog <<"loadModelFromFile(fstream &file) - WeakClassifier " << i << " is NULL!" << std::endl;
240  return false;
241  }
242  }
243  }
244 
245  //Load the Weights
246  file >> word;
247  if( word != "Weights:" ){
248  errorLog <<"loadModelFromFile(fstream &file) - Failed to read Weights header!" << std::endl;
249  return false;
250  }
251  for(UINT i=0; i<numWeakClassifiers; i++){
252  file >> weights[i];
253  }
254 
255  //Load the WeakClassifiers
256  file >> word;
257  if( word != "WeakClassifiers:" ){
258  errorLog <<"loadModelFromFile(fstream &file) - Failed to read WeakClassifiers header!" << std::endl;
259  errorLog << word << std::endl;
260  return false;
261  }
262  for(UINT i=0; i<numWeakClassifiers; i++){
263  if( !weakClassifiers[i]->loadModelFromFile( file ) ){
264  errorLog <<"loadModelFromFile(fstream &file) - Failed to load weakClassifer: " << i << std::endl;
265  return false;
266  }
267  }
268 
269  //We don't need to close the file as the function that called this function should handle that
270  return true;
271  }
272 
273  bool normalizeWeights(){
274  if( weights.size() == 0 ) return false;
275  Float sum = 0;
276  UINT N = (UINT)weights.size();
277  for(UINT i=0; i<N; i++){
278  sum += weights[i];
279  }
280  for(UINT i=0; i<N; i++){
281  weights[i] /= sum;
282  }
283  return true;
284  }
285 
286 protected:
287  UINT classLabel;
288  VectorFloat weights;
289  Vector< WeakClassifier* > weakClassifiers;
290 
291  DebugLog debugLog;
292  WarningLog warningLog;
293  ErrorLog errorLog;
294 
295 };
296 
297 GRT_END_NAMESPACE
298 
299 #endif// GRT_ADABOOST_CLASS_MODEL_HEADER
virtual bool deepCopyFrom(const WeakClassifier *weakClassifer)
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
unsigned int getSize() const
Definition: Vector.h:193
This is the main base class for all GRT WeakClassifiers.
WeakClassifier * createNewInstance() const
static WeakClassifier * createInstanceFromString(std::string const &weakClassifierType)