X-Boost  2.3.8
BoostClassifier.h
Go to the documentation of this file.
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 #ifndef _BOOST_CLASSIFIER_H
22 #define _BOOST_CLASSIFIER_H
23 
27 #include <string>
28 #include <iostream>
29 #include <fstream>
30 #include <stdexcept>
31 #include <algorithm>// sort
32 
33 #include "BoostableClassifier.h"
34 
35 namespace detail {
36 
38 template<ClassifierType type, class T>
39 struct traits;
40 
42 template<class T>
44 {
46 };
47 
49 template<class T>
51 {
52  typedef T Boosted;
53 };
54 
55 }
56 
60 template<class Classifier >
62 
63 public:
64 
67 
69  typedef std::vector< BoostedClassifierType > ClassifierListType;
70 
73 
74 private:
75 
77  ClassifierListType m_weak_classifiers;
78 
79 public:
80 
84  BoostClassifier(std::istream & in);
85 
87  template<class R, class P0, class P1, class P2>
88  explicit BoostClassifier(const BoostClassifier<R> & src, P0 scale, P1 p1, P2 p2)
89  {
90  m_weak_classifiers.reserve(src.size() );
91  for(typename BoostClassifier<R>::ClassifierListType::const_iterator i = src.list().begin(); i!= src.list().end(); ++i)
92  {
93  m_weak_classifiers.push_back( BoostedClassifierType(*i, scale, p1, p2) );
94  }
95  }
96 
98  template<class R, class P1, class P2>
99  explicit BoostClassifier(const BoostClassifier<R> & src, P1 p1, P2 p2)
100  {
101  m_weak_classifiers.reserve(src.size() );
102  for(typename BoostClassifier<R>::ClassifierListType::const_iterator i = src.list().begin(); i!= src.list().end(); ++i)
103  m_weak_classifiers.push_back( BoostedClassifierType(*i, p1, p2) );
104  }
105 
107  template<class R, class P1>
108  explicit BoostClassifier(const BoostClassifier<R> & src, const P1 & p1)
109  {
110  m_weak_classifiers.reserve(src.size() );
111  for(typename BoostClassifier<R>::ClassifierListType::const_iterator i = src.list().begin(); i!= src.list().end(); ++i)
112  m_weak_classifiers.push_back( BoostedClassifierType(*i, p1) );
113  }
114 
117 
119  inline int size() const {
120  return m_weak_classifiers.size();
121  };
122 
123 
126  return m_weak_classifiers;
127  }
128  inline const ClassifierListType & list() const {
129  return m_weak_classifiers;
130  }
131 
133  static std::string signature() {
134  return "additive-" + BoostedClassifierType::signature();
135  }
136 
139  template<class DataType>
140  inline float operator()(const DataType data) const {
141  // return compute(pIntImage, &m_weak_classifiers[0], m_weak_classifiers.size());
142  float acc = 0.0;
143  for(typename ClassifierListType::const_iterator i = m_weak_classifiers.begin(); i != m_weak_classifiers.end(); i++)
144  acc += (*i)(data);
145  return acc;
146  }
147 
150  template<class DataType, class Param1>
151  inline float operator()(const DataType data, Param1 p1) const {
152  // return compute(pIntImage, &m_weak_classifiers[0], m_weak_classifiers.size());
153  float acc = 0.0;
154  for(typename ClassifierListType::const_iterator i = m_weak_classifiers.begin(); i != m_weak_classifiers.end(); i++)
155  acc += (*i)(data, p1);
156  return acc;
157  }
158 
160  template<class FeatureType>
161  void export_features(std::vector<FeatureType> & out) const
162  {
163  for(typename ClassifierListType::const_iterator i = m_weak_classifiers.begin(); i != m_weak_classifiers.end(); i++)
164  i->export_features(out);
165  }
166 
168  void load(std::istream & in);
169 
171  void save(std::ostream & out) const;
172 
175  void sort();
176 
178  void resize(int size);
179 
182  float max_response() const {
183  float sum=0.0f;
184  for(typename ClassifierListType::const_iterator i = m_weak_classifiers.begin(); i != m_weak_classifiers.end(); i++)
185  sum+=i->getAlpha();
186  return sum;
187  }
188 
189  inline void insert(const BoostedClassifierType & classifier)
190  {
191  m_weak_classifiers.push_back(classifier);
192  }
193 
194 };
195 
196 template<class T>
197 std::istream & operator >>(std::istream & in, BoostClassifier<T> & dst);
198 
199 template<class T>
200 std::ostream & operator << (std::ostream & out, const BoostClassifier<T> & src);
201 
203 
204 // IO
205 template<class T>
206 std::istream & operator >>(std::istream & in, BoostClassifier<T> & dst)
207 {
208  dst.load(in);
209  return in;
210 }
211 
212 // IO
213 template<class T>
214 std::ostream & operator << (std::ostream & out, const BoostClassifier<T> & src)
215 {
216  src.save(out);
217  return out;
218 }
219 
220 template<class T>
222 
223 template<class T>
225 
226 template<class T>
228 {
229  load(in);
230 }
231 
232 template<class T>
233 void BoostClassifier<T>::load(std::istream & in)
234 {
235  while(in)
236  {
238  in >> h;
239  if(in)
240  {
241  m_weak_classifiers.push_back(h);
242  }
243  }
244 }
245 
246 template<class T>
247 void BoostClassifier<T>::save(std::ostream & out) const
248 {
249  for(typename BoostClassifier<T>::ClassifierListType::const_iterator i = list().begin(); i != list().end(); i++)
250  out << *i << '\n';
251 }
252 
253 // detail: sort policy
254 template<class T>
255 inline bool sort_by_alpha(const BoostableClassifier<T> & a, const BoostableClassifier<T> & b)
256 {
257  return a.alpha > b.alpha;
258 }
259 
260 template<class T>
262 {
263  std::sort(m_weak_classifiers.begin(), m_weak_classifiers.end(), sort_by_alpha<T> );
264 }
265 
266 template<class T>
268 {
269  m_weak_classifiers.resize(size);
270 }
271 
272 
273 #endif
float operator()(const DataType data) const
Definition: BoostClassifier.h:140
a Voting Boostable classifier
BoostClassifier(const BoostClassifier< R > &src, P0 scale, P1 p1, P2 p2)
genera un BoostClassifier partendo da un altro BoostClassifier, ed eseguendo un rescaling di tutti i ...
Definition: BoostClassifier.h:88
ClassifierListType & list()
Return the inner list of classifier.
Definition: BoostClassifier.h:125
~BoostClassifier()
dtor
Definition: BoostClassifier.h:224
void resize(int size)
Reduce number of classifiers (remove the last or, after sort(), the low influence ones) ...
Definition: BoostClassifier.h:267
Definition: BoostClassifier.h:61
image/size TODO namespace
Definition: Types.h:39
void export_features(std::vector< FeatureType > &out) const
export all features;
Definition: BoostClassifier.h:161
void save(std::ostream &out) const
Definition: BoostClassifier.h:247
a traits to change the inner classifier form, to use or not a weighted one
Definition: BoostClassifier.h:39
int size() const
return the number of weak classifiers
Definition: BoostClassifier.h:119
BoostClassifier(const BoostClassifier< R > &src, const P1 &p1)
create and convert a classifier
Definition: BoostClassifier.h:108
BoostClassifier< typename Classifier::OptimizedType > OptimizedType
An Optimized Classifier.
Definition: BoostClassifier.h:72
float alpha
Weight associated to this classifier.
Definition: BoostableClassifier.h:43
BoostClassifier()
ctor
Definition: BoostClassifier.h:221
void load(std::istream &in)
Definition: BoostClassifier.h:233
float max_response() const
Definition: BoostClassifier.h:182
detail::traits< Classifier::Type, Classifier >::Boosted BoostedClassifierType
A Boosted Classifier (must be a "Real" Classifier)
Definition: BoostClassifier.h:66
static std::string signature()
propagate signature
Definition: BoostClassifier.h:133
void sort()
Definition: BoostClassifier.h:261
BoostClassifier(const BoostClassifier< R > &src, P1 p1, P2 p2)
create and convert a classifier
Definition: BoostClassifier.h:99
a real classifier return a number between -1,+1
Definition: Types.h:34
Definition: BoostableClassifier.h:40
a dicrete classifier returns {-1,+1}
Definition: Types.h:32
std::vector< BoostedClassifierType > ClassifierListType
List of Boosted classifier.
Definition: BoostClassifier.h:69
float operator()(const DataType data, Param1 p1) const
Definition: BoostClassifier.h:151