X-Boost  2.3.8
DecisionTree.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 #ifndef _DECISION_TREE_H
22 #define DECISION_TREE_H
23 
27 #include <iostream>
28 #include "Types.h"
29 
33 template<class T, class DataType = int>
34 class DecisionTree {
35 public:
36 
39 
41  DataType th;
42 
45 
47  float category;
48 
49 public:
50 
53 
56 
57 public:
58 
60  template<class P1, class SrcFeature>
61  DecisionTree(const DecisionTree<SrcFeature, DataType> & src, const P1 & p1) : left(0), right(0)
62  {
63  classifier = src.classifier; // TODO: convert
64  th = src.th;
65  category = src.category;
66 
67  if(src.left)
68  left = new DecisionTree<T>(*src.left, p1);
69  if(src.right)
70  right = new DecisionTree<T>(*src.right, p1);
71  }
72 
74  template<class P1, class P2, class SrcFeature>
75  DecisionTree(const DecisionTree<SrcFeature, DataType> & src, P1 p1, const P2 & p2) : classifier(src.classifier, p1, p2), left(0), right(0)
76  {
77  th = src.th;
78  category = src.category;
79 
80  if(src.left)
81  left = new DecisionTree<T>(*src.left, p1, p2);
82  if(src.right)
83  right = new DecisionTree<T>(*src.right, p1, p2);
84  }
85 
86 
88  template<class P1, class P2, class P3>
89  DecisionTree(const DecisionTree<T, DataType> & src, P1 p1, P2 p2, P3 p3) : left(0), right(0)
90  {
91  classifier = src.classifier; // TODO: convert
92  th = src.th;
93  category = src.category;
94 
95  if(src.left)
96  left = new DecisionTree<T>(*src.left, p1, p2, p3);
97  if(src.right)
98  right = new DecisionTree<T>(*src.right, p1, p2, p3);
99  }
100 
101  DecisionTree(const DecisionTree<T, DataType> & src) : left(0), right(0)
102  {
103  classifier = src.classifier;
104  th = src.th;
105  category = src.category;
106 
107  if(src.left)
108  left = new DecisionTree<T>(*src.left);
109  if(src.right)
110  right = new DecisionTree<T>(*src.right);
111  }
112 
113  void operator=(const DecisionTree<T> & src)
114  {
115  delete left;
116  delete right;
117 
118  classifier = src.classifier;
119  th = src.th;
120  category = src.category;
121 
122  if(src.left)
123  left = new DecisionTree<T>(*src.left);
124  else
125  left = 0;
126  if(src.right)
127  right = new DecisionTree<T>(*src.right);
128  else
129  right = 0;
130  }
131 
132  DecisionTree() : left(0), right(0) { }
133  DecisionTree(std::istream &in) : left(0), right(0)
134  {
135  load(in);
136  }
137 
138  bool load(std::istream &in)
139  {
140  std::string c;
141  delete left;
142  delete right;
143 
144  in >> c;
145  if(in.eof())
146  return false;
147 
148  if(c == "n")
149  {
150  left = new DecisionTree<T>();
151  right = new DecisionTree<T>();
152  in >> th;
153  in >> classifier;
154  in >> *left;
155  in >> *right;
156  }
157  else if(c == "l")
158  {
159  in >> category;
160  }
161  else
162  {
163  std::cerr << c << " at " << in.tellg() << std::endl;
164  throw std::runtime_error("wrong file format");
165  }
166  return false;
167  }
168 
169  void save(std::ostream &out) const
170  {
171 
172  if(left!=0)
173  {
174  // node
175  out << 'n' << ' ' << th << ' ' << classifier << ' ' << *left << ' ' << *right;
176  }
177  else
178  {
179  // leaf
180  out << 'l' << ' ' << category;
181  }
182  }
183 
184  ~DecisionTree() {
185  delete left;
186  delete right;
187  }
188 
189  static std::string signature() {
190  return T::signature() + "-decision-tree";
191  }
192 
194  static float max_response() {
195  return 1.0f;
196  }
197 
198  template<class FeatureType>
199  bool export_features(std::vector<FeatureType> & out) const
200  {
201  // unimplementend yet
202  return false;
203  }
204 
206  template<class Param1>
207  float classify(const unsigned int *image, Param1 stride) const {
208  if(left!=0)
209  {
210  return ( classifier.response(image, stride) > th ) ? right->classify(image, stride) : left->classify(image, stride);
211  }
212 
213  return category;
214  }
215 
217  float classify(const unsigned int *image) const {
218  if(left!=0)
219  {
220  return ( classifier.response(image) > th ) ? right->classify(image) : left->classify(image);
221  }
222 
223  return category;
224  }
225 
226  template<class Param1>
227  inline float operator()(const unsigned int *image, Param1 stride) const {
228  return classify(image, stride);
229  }
230 
231  inline float operator()(const unsigned int *image) const {
232  return classify(image);
233  }
234 
236  void scale_response(float factor)
237  {
238  if(left!=0)
239  {
240  left->scale_response(factor);
241  right->scale_response(factor);
242  }
243  else
244  {
245  category *= factor;
246  }
247  }
248 
250  float MeasureAvgDepth() const {
251  if(left!=0)
252  {
253  return 1.0 + (left->MeasureAvgDepth() + right->MeasureAvgDepth()) * 0.5f;
254  }
255  else
256  {
257  return 0.0f;
258  }
259  }
260 
262  void Discrete()
263  {
264  if(left!=0)
265  {
266  left->Discrete();
267  right->Discrete();
268  }
269  else
270  {
271  category = (category > 0.0) ? 1.0 : -1.0;
272  }
273  }
274 
276  float getAlpha() const {
277  return 1.0f;
278  }
279 
280 };
281 
282 template<class T>
283 std::istream & operator >> (std::istream & in, DecisionTree<T>& s);
284 template<class T>
285 std::ostream & operator << (std::ostream & out, const DecisionTree<T> & s);
286 
288 
289 template<class T>
290 std::istream & operator >> (std::istream & in, DecisionTree<T>& s)
291 {
292  s.load(in);
293  return in;
294 }
295 
296 template<class T>
297 std::ostream & operator << (std::ostream & out, const DecisionTree<T> & s)
298 {
299  s.save(out);
300  return out;
301 }
302 
303 
304 #endif
DataType th
Classifier Threshold.
Definition: DecisionTree.h:41
Types involved in boosting.
float classify(const unsigned int *image) const
perform the classification stage (no params)
Definition: DecisionTree.h:217
DecisionTree< T, DataType > * left
sub-nodes (if null is a leaf)
Definition: DecisionTree.h:44
float category
if there are not subnodes, this value -1 .. 1 return the class and probability
Definition: DecisionTree.h:47
void scale_response(float factor)
scale the response of the decision tree
Definition: DecisionTree.h:236
ClassifierType
Definition: Types.h:31
DecisionTree(const DecisionTree< T, DataType > &src, P1 p1, P2 p2, P3 p3)
Copy & convert constructor.
Definition: DecisionTree.h:89
static const ClassifierType Type
DecisionTree is (generic) a RealClassifier.
Definition: DecisionTree.h:55
DecisionTree(const DecisionTree< SrcFeature, DataType > &src, const P1 &p1)
Copy & convert constructor.
Definition: DecisionTree.h:61
DecisionTree(const DecisionTree< SrcFeature, DataType > &src, P1 p1, const P2 &p2)
Copy & convert constructor.
Definition: DecisionTree.h:75
float getAlpha() const
MMMMH?
Definition: DecisionTree.h:276
T classifier
a feature extractor
Definition: DecisionTree.h:38
static float max_response()
internally normalized
Definition: DecisionTree.h:194
a real classifier return a number between -1,+1
Definition: Types.h:34
float classify(const unsigned int *image, Param1 stride) const
perform the classification stage (1 params)
Definition: DecisionTree.h:207
float MeasureAvgDepth() const
estimate the number of layer evaluated (roughly)
Definition: DecisionTree.h:250
void Discrete()
transform a Probabilistic Decision Tree in a Discrete Decision Tree
Definition: DecisionTree.h:262
Definition: DecisionTree.h:34