X-Boost  2.3.8
AdaBoost.h
1 /* XBoost: Ada-Boost and Friends on Haar/ICF/HOG Features, Library and ToolBox
2  *
3  * Copyright (c) 2008-2014 Paolo Medici <medici@ce.unipr.it>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 
21 
22 #ifndef _ADABOOST_H
23 #define _ADABOOST_H
24 
25 #include "DataSet.h"
26 #include "AdaBoostCommon.h"
27 #include <cmath> // exp
28 
29 #include "Traits/HaarFeature.h" // TODO:
30 
31 template<class WeakClassifier>
32 class AdaBoost;
33 
39 template<class WeakClassifier>
40 class AdaBoost {
41 
42 public:
43 
44  AdaBoost() { }
45 
52  template<class DataSetType>
53  static bool Update(const WeakClassifier & H, DataSetType & training_set, DataSetType & validation_set, bool verbose=true)
54  {
55  int P = 0;
56  int N = 0;
57  int TP = 0;
58  int TN = 0;
59  double wP = 0.0; // sum of weights of positive patterns
60  double wN = 0.0; // sum of weights of negative patterns
61  double wTP = 0.0; // sum of weight of correctly detected positive patterns
62  double wTN = 0.0; // sum of weight of correctly detected negative patterns
63 
64  double z = 0.0; // sum e^-u_i
65 
66  for(unsigned int i=0; i<validation_set.Size(); i++)
67  {
68  double f = H.classify( getData1(validation_set.templates[i], validation_set), getData2(validation_set.templates[i], validation_set) ); // classifier response
69  double u = validation_set.templates[i].category * f; // correctness function
70 
71  if(verbose)
72  {
73  if(validation_set.templates[i].category == +1)
74  {
75  wP += validation_set.templates[i].d;
76  P++;
77  if(u>0.0) {
78  wTP += validation_set.templates[i].d;
79  TP++;
80  }
81  }
82  else
83 // if(set.templates[i].category == -1)
84  {
85  wN += validation_set.templates[i].d;
86  N++;
87  if(u>0.0) {
88  wTN += validation_set.templates[i].d;
89  TN++;
90  }
91  }
92 
93  }
94 
95 
96  // w' = w * e^{-u_i}
97  validation_set.templates[i].d *= exp( - u );
98 
99  z+= validation_set.templates[i].d;
100 
101  }
102 
103  double norm = 1.0/z;
104 
105  // normalize
106  for(typename DataSetType::ListType::iterator i = validation_set.templates.begin(); i != validation_set.templates.end(); ++i)
107  i->d *= norm;
108 
109  if(verbose)
110  {
111  std::cout << "Best Feature | Z = " << z << " | Correct In Number = " << (TP+TN) * 100 / validation_set.Size() << "% (" << 100*TP/P << "% +, " << 100 * TN/N << "% -)" <<
112  " | Correct in Weight = " << (int)( (wTP+wTN) * 100 / (wP+wN)) << "% (" << (int)(100 * wTP/wP) << "% +, " << (int)(100 * wTN/wN) << "% -)" <<
113  std::endl;
114  }
115  return true;
116  }
117 
118 };
119 
120 #endif
a file containing misc type
static bool Update(const WeakClassifier &H, DataSetType &training_set, DataSetType &validation_set, bool verbose=true)
Definition: AdaBoost.h:53
Definition: AdaBoost.h:32
declare a DataSet