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