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 as published
13 * by the Free Software Foundation, either version 3 of the License,
14 * or (at your option) any later version.
16 * folded-ctf is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with folded-ctf. If not, see <http://www.gnu.org/licenses/>.
27 #include "fusion_sort.h"
29 Parsing::Parsing(LabelledImagePool *image_pool,
30 PoseCellHierarchy *hierarchy,
31 scalar_t proportion_negative_cells,
34 _image_pool = image_pool;
35 _image_index = image_index;
40 image = _image_pool->grab_image(_image_index);
42 hierarchy->add_root_cells(image, &cell_set);
44 int *kept = new int[cell_set.nb_cells()];
48 for(int c = 0; c < cell_set.nb_cells(); c++) {
49 int l = image->pose_cell_label(cell_set.get_cell(c));
50 kept[c] = (l > 0) || (l < 0 && drand48() < proportion_negative_cells);
51 if(kept[c]) _nb_cells++;
54 _cells = new PoseCell[_nb_cells];
55 _responses = new scalar_t[_nb_cells];
56 _labels = new int[_nb_cells];
61 for(int c = 0; c < cell_set.nb_cells(); c++) {
63 _cells[d] = *(cell_set.get_cell(c));
64 _labels[d] = image->pose_cell_label(&_cells[d]);
68 } else if(_labels[d] > 0) {
77 _image_pool->release_image(_image_index);
86 void Parsing::down_one_level(PoseCellHierarchy *hierarchy,
87 int level, int *sample_nb_occurences, scalar_t *sample_responses) {
92 for(int c = 0; c < _nb_cells; c++) {
93 new_nb_cells += sample_nb_occurences[c];
96 PoseCell *new_cells = new PoseCell[new_nb_cells];
97 scalar_t *new_responses = new scalar_t[new_nb_cells];
98 int *new_labels = new int[new_nb_cells];
100 image = _image_pool->grab_image(_image_index);
103 for(int c = 0; c < _nb_cells; c++) {
105 if(sample_nb_occurences[c] > 0) {
107 cell_set.erase_content();
108 hierarchy->add_subcells(level, _cells + c, &cell_set);
111 ASSERT(sample_nb_occurences[c] == 1);
113 for(int d = 0; d < cell_set.nb_cells(); d++) {
114 if(image->pose_cell_label(cell_set.get_cell(d)) > 0) {
120 ASSERT(b < new_nb_cells);
121 new_cells[b] = *(cell_set.get_cell(e));
122 new_responses[b] = sample_responses[c];
127 else if(_labels[c] < 0) {
128 for(int d = 0; d < sample_nb_occurences[c]; d++) {
129 ASSERT(b < new_nb_cells);
130 new_cells[b] = *(cell_set.get_cell(int(drand48() * cell_set.nb_cells())));
131 new_responses[b] = sample_responses[c];
138 cerr << "INCONSISTENCY" << endl;
144 ASSERT(b == new_nb_cells);
146 _image_pool->release_image(_image_index);
151 _nb_cells = new_nb_cells;
153 _labels = new_labels;
154 _responses = new_responses;
157 void Parsing::update_cell_responses(PiFeatureFamily *pi_feature_family,
158 Classifier *classifier) {
159 LabelledImage *image;
161 image = _image_pool->grab_image(_image_index);
162 image->compute_rich_structure();
164 SampleSet *samples = new SampleSet(pi_feature_family->nb_features(), 1);
166 for(int c = 0; c < _nb_cells; c++) {
167 samples->set_sample(0, pi_feature_family, image, &_cells[c], 0);
168 _responses[c] += classifier->response(samples, 0);
169 ASSERT(!isnan(_responses[c]));
172 _image_pool->release_image(_image_index);
176 void Parsing::collect_samples(SampleSet *samples,
177 PiFeatureFamily *pi_feature_family,
180 LabelledImage *image;
182 image = _image_pool->grab_image(_image_index);
183 image->compute_rich_structure();
185 for(int c = 0; c < _nb_cells; c++) {
187 samples->set_sample(s, pi_feature_family, image, &_cells[c], _labels[c]);
192 _image_pool->release_image(_image_index);