2 * folded-ctf is an implementation of the folded hierarchy of
3 * classifiers for object detection, developed by Francois Fleuret
6 * Copyright (c) 2008 Idiap Research Institute, http://www.idiap.ch/
7 * Written by Francois Fleuret <francois.fleuret@idiap.ch>
9 * This file is part of folded-ctf.
11 * folded-ctf is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 3 as
13 * published by the Free Software Foundation.
15 * folded-ctf is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with folded-ctf. If not, see <http://www.gnu.org/licenses/>.
25 #include "decision_tree.h"
26 #include "fusion_sort.h"
28 DecisionTree::DecisionTree() {
36 DecisionTree::~DecisionTree() {
38 delete _subtree_lesser;
40 delete _subtree_greater;
43 int DecisionTree::nb_leaves() {
44 if(_subtree_lesser ||_subtree_greater)
45 return _subtree_lesser->nb_leaves() + _subtree_greater->nb_leaves();
50 int DecisionTree::depth() {
51 if(_subtree_lesser ||_subtree_greater)
52 return 1 + max(_subtree_lesser->depth(), _subtree_greater->depth());
57 scalar_t DecisionTree::response(SampleSet *sample_set, int n_sample) {
58 if(_subtree_lesser && _subtree_greater) {
59 if(sample_set->feature_value(n_sample, _feature_index) < _threshold)
60 return _subtree_lesser->response(sample_set, n_sample);
62 return _subtree_greater->response(sample_set, n_sample);
68 void DecisionTree::pick_best_split(SampleSet *sample_set, scalar_t *loss_derivatives) {
70 int nb_samples = sample_set->nb_samples();
72 scalar_t *responses = new scalar_t[nb_samples];
73 int *indexes = new int[nb_samples];
74 int *sorted_indexes = new int[nb_samples];
76 scalar_t max_abs_sum = 0;
79 for(int f = 0; f < sample_set->nb_features(); f++) {
82 for(int s = 0; s < nb_samples; s++) {
84 responses[s] = sample_set->feature_value(s, f);
85 sum += loss_derivatives[s];
88 indexed_fusion_sort(nb_samples, indexes, sorted_indexes, responses);
90 int t, u = sorted_indexes[0];
91 for(int s = 0; s < nb_samples - 1; s++) {
93 u = sorted_indexes[s + 1];
94 sum -= 2 * loss_derivatives[t];
96 if(responses[t] < responses[u] && abs(sum) > max_abs_sum) {
97 max_abs_sum = abs(sum);
99 _threshold = (responses[t] + responses[u])/2;
105 delete[] sorted_indexes;
109 void DecisionTree::train(LossMachine *loss_machine,
110 SampleSet *sample_set,
111 scalar_t *current_responses,
112 scalar_t *loss_derivatives,
115 if(_subtree_lesser || _subtree_greater || _feature_index >= 0) {
116 cerr << "You can not re-train a tree." << endl;
120 int nb_samples = sample_set->nb_samples();
122 int nb_pos = 0, nb_neg = 0;
123 for(int s = 0; s < sample_set->nb_samples(); s++) {
124 if(sample_set->label(s) > 0) nb_pos++;
125 else if(sample_set->label(s) < 0) nb_neg++;
128 (*global.log_stream) << "Training tree" << endl;
129 (*global.log_stream) << " nb_samples " << nb_samples << endl;
130 (*global.log_stream) << " depth " << depth << endl;
131 (*global.log_stream) << " nb_pos = " << nb_pos << endl;
132 (*global.log_stream) << " nb_neg = " << nb_neg << endl;
134 if(depth >= global.tree_depth_max)
135 (*global.log_stream) << " Maximum depth reached." << endl;
136 if(nb_pos < min_nb_samples_for_split)
137 (*global.log_stream) << " Not enough positive samples." << endl;
138 if(nb_neg < min_nb_samples_for_split)
139 (*global.log_stream) << " Not enough negative samples." << endl;
141 if(depth < global.tree_depth_max &&
142 nb_pos >= min_nb_samples_for_split &&
143 nb_neg >= min_nb_samples_for_split) {
145 pick_best_split(sample_set, loss_derivatives);
147 if(_feature_index >= 0) {
148 int indexes[nb_samples];
149 scalar_t *parted_current_responses = new scalar_t[nb_samples];
150 scalar_t *parted_loss_derivatives = new scalar_t[nb_samples];
152 int nb_lesser = 0, nb_greater = 0;
153 int nb_lesser_pos = 0, nb_lesser_neg = 0, nb_greater_pos = 0, nb_greater_neg = 0;
155 for(int s = 0; s < nb_samples; s++) {
156 if(sample_set->feature_value(s, _feature_index) < _threshold) {
157 indexes[nb_lesser] = s;
158 parted_current_responses[nb_lesser] = current_responses[s];
159 parted_loss_derivatives[nb_lesser] = loss_derivatives[s];
161 if(sample_set->label(s) > 0)
163 else if(sample_set->label(s) < 0)
170 indexes[nb_samples - nb_greater] = s;
171 parted_current_responses[nb_samples - nb_greater] = current_responses[s];
172 parted_loss_derivatives[nb_samples - nb_greater] = loss_derivatives[s];
174 if(sample_set->label(s) > 0)
176 else if(sample_set->label(s) < 0)
181 if((nb_lesser_pos >= min_nb_samples_for_split ||
182 nb_lesser_neg >= min_nb_samples_for_split) &&
183 (nb_greater_pos >= min_nb_samples_for_split ||
184 nb_greater_neg >= min_nb_samples_for_split)) {
186 _subtree_lesser = new DecisionTree();
189 SampleSet sub_sample_set(sample_set, nb_lesser, indexes);
191 _subtree_lesser->train(loss_machine,
193 parted_current_responses,
194 parted_loss_derivatives,
198 _subtree_greater = new DecisionTree();
201 SampleSet sub_sample_set(sample_set, nb_greater, indexes + nb_lesser);
203 _subtree_greater->train(loss_machine,
205 parted_current_responses + nb_lesser,
206 parted_loss_derivatives + nb_lesser,
211 delete[] parted_current_responses;
212 delete[] parted_loss_derivatives;
214 (*global.log_stream) << "Could not find a feature for split." << endl;
218 if(!(_subtree_greater && _subtree_lesser)) {
219 scalar_t *tmp_responses = new scalar_t[nb_samples];
220 for(int s = 0; s < nb_samples; s++)
221 tmp_responses[s] = 1;
223 _weight = loss_machine->optimal_weight(sample_set, tmp_responses, current_responses);
225 const scalar_t max_weight = 10.0;
227 if(_weight > max_weight) {
228 _weight = max_weight;
229 } else if(_weight < - max_weight) {
230 _weight = - max_weight;
233 (*global.log_stream) << " _weight " << _weight << endl;
235 delete[] tmp_responses;
239 void DecisionTree::train(LossMachine *loss_machine,
240 SampleSet *sample_set,
241 scalar_t *current_responses) {
243 scalar_t *loss_derivatives = new scalar_t[sample_set->nb_samples()];
245 loss_machine->get_loss_derivatives(sample_set, current_responses, loss_derivatives);
247 train(loss_machine, sample_set, current_responses, loss_derivatives, 0);
249 delete[] loss_derivatives;
252 //////////////////////////////////////////////////////////////////////
254 void DecisionTree::tag_used_features(bool *used) {
255 if(_subtree_lesser && _subtree_greater) {
256 used[_feature_index] = true;
257 _subtree_lesser->tag_used_features(used);
258 _subtree_greater->tag_used_features(used);
262 void DecisionTree::re_index_features(int *new_indexes) {
263 if(_subtree_lesser && _subtree_greater) {
264 _feature_index = new_indexes[_feature_index];
265 _subtree_lesser->re_index_features(new_indexes);
266 _subtree_greater->re_index_features(new_indexes);
270 //////////////////////////////////////////////////////////////////////
272 void DecisionTree::read(istream *is) {
273 if(_subtree_lesser || _subtree_greater) {
274 cerr << "You can not read in an existing tree." << endl;
278 read_var(is, &_feature_index);
279 read_var(is, &_threshold);
280 read_var(is, &_weight);
283 read_var(is, &split);
286 _subtree_lesser = new DecisionTree();
287 _subtree_lesser->read(is);
288 _subtree_greater = new DecisionTree();
289 _subtree_greater->read(is);
293 void DecisionTree::write(ostream *os) {
295 write_var(os, &_feature_index);
296 write_var(os, &_threshold);
297 write_var(os, &_weight);
300 if(_subtree_lesser && _subtree_greater) {
302 write_var(os, &split);
303 _subtree_lesser->write(os);
304 _subtree_greater->write(os);
307 write_var(os, &split);