X-Boost  2.3.8
WeightedPatternResponse.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 _WEIGHTED_PATTERN_RESPONSE_H
22 #define _WEIGHTED_PATTERN_RESPONSE_H
23 
27 #include <cmath>
28 
31 
32 #include "Classifier/RealDecisionStump.h"
33 
37 template<class ResponseType>
40  double d;
41 
43  ResponseType value;
44 
45 public:
46 
48  int getClass() const { return (d>0.0) ? -1 : 1; }
49 
51  double getWeight() const { return std::abs(d); }
52 
54  float GetWeightedCategory() const { return d; }
55 
57  bool operator < (const BinaryWeightedPatternResponse<ResponseType> & c) const
58  {
59  return value < c.value;
60  }
61 };
62 
65 
67 template<class Set>
68 void __InitializePatternResponse(BinaryWeightedPatternResponse<int> *store, int n_feature, const Set & set)
69 {
70  int n = set.Size();
71  // for each input pattern i0...i1
72  for(unsigned int i=0;i<n;++i)
73  {
74  double d = set.templates[i].category * set.templates[i].d;
75  // variation of weight d associated
76  for(unsigned int j=0;j<n_feature;j++)
77  store[i + j * n].d = d;
78  }
79 }
80 
84 template<class FeatureExtractor, class Set>
85 void ComputeFeaturesResponse(BinaryWeightedPatternResponse<int> *store, const FeatureExtractor * h, int n_feature, const Set & set, int i0, int i1)
86 {
87  int stride = set.Size();
88  // for each input pattern i0...i1
89  for(int i=i0; i<i1; i++)
90  {
91  double d = set.templates[i].category * set.templates[i].d;
92  // evaluate any feature on the h block
93  for(int j=0; j<n_feature; ++j)
94  {
95  // feature value
96  store[i+j*stride].value = h[j].response( getData1(set.templates[i], set), getData2(set.templates[i], set) );
97  store[i+j*stride].d = d;
98  }
99  }
100 }
101 
102 
104 template<class FeatureExtractor, class Set>
106 {
107  for(unsigned int i =0; i<set.Size(); i++)
108  {
109  // feature value: nota la memoria punta al pixel (-1,-1)
110  store[i].value = h.response( getData1(set.templates[i], set), getData2(set.templates[i], set) );
111  // variation of weight d associated
112  // d, peso associato al pattern, value >0 se e' uno di tipo A, altri < 0
113  // note: non gestisce il caso nativo di astensione
114  store[i].d = set.templates[i].category * set.templates[i].d;
115  }
116 }
117 
124 void GenerateBucket(BinaryWeightedPatternResponse<int> *store, int n_bucket, const BinaryWeightedPatternResponse<int> *source, int n_element);
125 
128 template<class FeatureExtractor, class Set>
129 void ExtractFeatureAndBucket(BinaryWeightedPatternResponse<int> *store, const FeatureExtractor & h, const Set & set, int n_bucket, int *value)
130 {
131  int min_resp, max_resp;
132  int bin_size;
133  const int n_element = set.Size();
134  // compute value, min and max:
135  for(int i =0; i < n_element; i++)
136  {
137  value[i] = h.response( getData1(set.templates[i], set), getData2(set.templates[i], set) );
138  if(i==0) { min_resp = max_resp = value[0]; }
139  else {
140  if(min_resp > value[i]) min_resp = value[i];
141  if(max_resp < value[i]) max_resp = value[i];
142  }
143  }
144 
145  // compute bin_size rounded (max - min + 1) elements
146  bin_size = (max_resp - min_resp + 1 + n_bucket - 1) / n_bucket;
147 
148  // avoid division by zero:
149  if(bin_size == 0)
150  bin_size = 1;
151 
152  // setup store[i] memory
153  for(int i = 0;i<n_bucket; ++i)
154  {
155  store[i].value = min_resp + bin_size * i;
156  store[i].d = 0.0;
157  }
158 
159  // fill histogram
160  for(int i =0; i< n_element; i++)
161  {
162  int bin = (value[i] - min_resp) / bin_size;
163  /*
164  if(bin<0 || bin>=n_bucket)
165  std::cerr << "internal error: bin=" << bin << " | value=" << value[i] << " | min=" << min_resp << " | max=" << max_resp << " | bin_size=" << bin_size << std::endl;
166  */
167  store[bin].d += set.templates[i].category * set.templates[i].d; // TODO: precompute
168  }
169 }
170 
171 
172 template<class ResponseType>
174 
176  int category;
177 
179  double d;
180 
182  ResponseType value;
183 
184 public:
185 
187  int getClass() const { return category; }
188 
190  double getWeight() const { return d; }
191 
193  bool operator < (const WeightedPatternResponse<ResponseType> & c) const
194  {
195  return value < c.value;
196  }
197 };
198 
199 
203 template<class FeatureExtractor, class Set>
204 void ExtractFeatureSubPart(WeightedPatternResponse<int> *store, const FeatureExtractor * h, int n_feature, const Set & set, int i0, int i1)
205 {
206  long stride = set.Size();
207  // for each input pattern i0...i1
208  for(unsigned int i=i0; i<i1; i++)
209  {
210  // evaluate any feature on the h block
211  for(unsigned int j=0; j<n_feature; ++j)
212  {
213  // feature value
214  store[i+j*stride].value = h[j].response( getData1( set.templates[i], set), getData2( set.templates[i], set) );
215  // variation of weight d associated
216  store[i+j*stride].d = set.templates[i].d;
217  store[i+j*stride].category = set.templates[i].category;
218  }
219 
220  }
221 }
222 
224 template<class FeatureExtractor, class Set>
225 void ExtractFeature(WeightedPatternResponse<int> *store, const FeatureExtractor & h, const Set & set)
226 {
227  for(unsigned int i =0; i<set.Size(); i++)
228  {
229  // feature value: nota la memoria punta al pixel (-1,-1)
230  store[i].value = h.response( getData1( set.templates[i], set), getData2( set.templates[i], set) );
231  // variation of weight d associated
232  // d, peso associato al pattern, value >0 se e' uno di tipo A, altri < 0
233  // note: non gestisce il caso nativo di astensione
234  store[i].d = set.templates[i].d;
235  store[i].category = set.templates[i].category;
236  }
237 }
238 
240 
243 {
244 public:
245 /* CALLGRIND 04/02/2009
246  * find_threshold 99.96%, (~14.28% self)
247  * sort 54,98% (14,65% self inlined)
248  * eval 30.71% (totally inlined)
249  **/
258 static double optimize(DecisionStump<int> & h, const PatternResponse *store, int n, double sum_d) __attribute__ ((deprecated));
259 
269 static double optimize(DecisionStump<int> & h, const PatternResponse *store, int n, double wp, double wn, bool strictly_growing=false);
270 
273 static double optimize(MultiClassDecisionStumpSingleThreshold<int> & h, const WeightedPatternResponse<int> *store, int n, const double *sum_d);
276 static double optimize(MultiClassDecisionStumpMultiThreshold<int> & h, const WeightedPatternResponse<int> *store, int n, const double *sum_d);
277 
278 };
279 
284 public:
285 static double optimize(RealDecisionStump<int> & h, const PatternResponse *store, int n, double wp, double wn);
286 };
287 
292 public:
293 static double optimize(RealDecisionStump<int> & h, const PatternResponse *store, int n, double wp, double wn);
294 };
295 
297 void sort_pattern(PatternResponse *store, int n);
301 #endif
ResponseType value
feature response associated with this pattern
Definition: WeightedPatternResponse.h:182
Definition: WeightedPatternResponse.h:173
A Weak Classifier based on a Feature Extractor policy.
Definition: DecisionStump.h:45
void ComputeFeaturesResponse(BinaryWeightedPatternResponse< int > *store, const FeatureExtractor *h, int n_feature, const Set &set, int i0, int i1)
Definition: WeightedPatternResponse.h:85
void ExtractFeature(BinaryWeightedPatternResponse< int > *store, const FeatureExtractor &h, const Set &set)
extract from templates feature using h and put in store, and associating the weighted category d ...
Definition: WeightedPatternResponse.h:105
void ExtractFeatureAndBucket(BinaryWeightedPatternResponse< int > *store, const FeatureExtractor &h, const Set &set, int n_bucket, int *value)
Definition: WeightedPatternResponse.h:129
void ExtractFeatureSubPart(WeightedPatternResponse< int > *store, const FeatureExtractor *h, int n_feature, const Set &set, int i0, int i1)
Definition: WeightedPatternResponse.h:204
ResponseType value
feature response associated with this pattern
Definition: WeightedPatternResponse.h:43
BinaryWeightedPatternResponse< int > PatternResponse
A weighted pattern response with integer values.
Definition: WeightedPatternResponse.h:64
void __InitializePatternResponse(BinaryWeightedPatternResponse< int > *store, int n_feature, const Set &set)
set up d field in BinaryWeightedPatternResponse
Definition: WeightedPatternResponse.h:68
A Weak Classifier based on a Feature Extractor policy.
Definition: RealDecisionStump.h:42
double d
Weight associated to this pattern.
Definition: WeightedPatternResponse.h:179
void sort_pattern(PatternResponse *store, int n)
Definition: MultiClassDecisionStump.h:72
a FeatureExtractor return a scalar number without relationship with classification ...
Definition: Types.h:35
Definition: WeightedPatternResponse.h:283
float GetWeightedCategory() const
return weighted category
Definition: WeightedPatternResponse.h:54
AdaBoost W+ metric (or any metric based on weighted pattern sum) for DecisionStumpPolicy (in this cas...
Definition: WeightedPatternResponse.h:242
double getWeight() const
return the weight associated to this class
Definition: WeightedPatternResponse.h:190
A simple decision stump for multiclass problem using only one threshold for all problems.
Definition: MultiClassDecisionStump.h:35
double d
Weight associated to this pattern with sign ( >0 = pos +1, <0 = neg -1)
Definition: WeightedPatternResponse.h:40
Definition: WeightedPatternResponse.h:291
double getWeight() const
return the weight associated to this class
Definition: WeightedPatternResponse.h:51
Definition: WeightedPatternResponse.h:38
A (really) weak classifier. Implements the policy DecisionStump for BinaryClassifier.
int getClass() const
return current class identifier (see above)
Definition: WeightedPatternResponse.h:48
int getClass() const
return current class identifier (see above)
Definition: WeightedPatternResponse.h:187
A (really) weak classifier for Multi-Class Problem Implements some MultiClassDecisionStump.
static double optimize(DecisionStump< int > &h, const PatternResponse *store, int n, double sum_d) __attribute__((deprecated))
void GenerateBucket(BinaryWeightedPatternResponse< int > *store, int n_bucket, const BinaryWeightedPatternResponse< int > *source, int n_element)
int category
category
Definition: WeightedPatternResponse.h:176