2 ///////////////////////////////////////////////////////////////////////////
3 // This program is free software: you can redistribute it and/or modify //
4 // it under the terms of the version 3 of the GNU General Public License //
5 // as published by the Free Software Foundation. //
7 // This program is distributed in the hope that it will be useful, but //
8 // WITHOUT ANY WARRANTY; without even the implied warranty of //
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
10 // General Public License for more details. //
12 // You should have received a copy of the GNU General Public License //
13 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
15 // Written by Francois Fleuret, (C) IDIAP //
16 // Contact <francois.fleuret@idiap.ch> for comments & bug reports //
17 ///////////////////////////////////////////////////////////////////////////
20 #include "fusion_sort.h"
22 Parsing::Parsing(LabelledImagePool *image_pool,
23 PoseCellHierarchy *hierarchy,
24 scalar_t proportion_negative_cells,
27 _image_pool = image_pool;
28 _image_index = image_index;
33 image = _image_pool->grab_image(_image_index);
35 hierarchy->add_root_cells(image, &cell_set);
37 int *kept = new int[cell_set.nb_cells()];
41 for(int c = 0; c < cell_set.nb_cells(); c++) {
42 int l = image->pose_cell_label(cell_set.get_cell(c));
43 kept[c] = (l > 0) || (l < 0 && drand48() < proportion_negative_cells);
44 if(kept[c]) _nb_cells++;
47 _cells = new PoseCell[_nb_cells];
48 _responses = new scalar_t[_nb_cells];
49 _labels = new int[_nb_cells];
54 for(int c = 0; c < cell_set.nb_cells(); c++) {
56 _cells[d] = *(cell_set.get_cell(c));
57 _labels[d] = image->pose_cell_label(&_cells[d]);
61 } else if(_labels[d] > 0) {
70 _image_pool->release_image(_image_index);
79 void Parsing::down_one_level(PoseCellHierarchy *hierarchy,
80 int level, int *sample_nb_occurences, scalar_t *sample_responses) {
85 for(int c = 0; c < _nb_cells; c++) {
86 new_nb_cells += sample_nb_occurences[c];
89 PoseCell *new_cells = new PoseCell[new_nb_cells];
90 scalar_t *new_responses = new scalar_t[new_nb_cells];
91 int *new_labels = new int[new_nb_cells];
93 image = _image_pool->grab_image(_image_index);
96 for(int c = 0; c < _nb_cells; c++) {
98 if(sample_nb_occurences[c] > 0) {
100 cell_set.erase_content();
101 hierarchy->add_subcells(level, _cells + c, &cell_set);
104 ASSERT(sample_nb_occurences[c] == 1);
106 for(int d = 0; d < cell_set.nb_cells(); d++) {
107 if(image->pose_cell_label(cell_set.get_cell(d)) > 0) {
113 ASSERT(b < new_nb_cells);
114 new_cells[b] = *(cell_set.get_cell(e));
115 new_responses[b] = sample_responses[c];
120 else if(_labels[c] < 0) {
121 for(int d = 0; d < sample_nb_occurences[c]; d++) {
122 ASSERT(b < new_nb_cells);
123 new_cells[b] = *(cell_set.get_cell(int(drand48() * cell_set.nb_cells())));
124 new_responses[b] = sample_responses[c];
131 cerr << "INCONSISTENCY" << endl;
137 ASSERT(b == new_nb_cells);
139 _image_pool->release_image(_image_index);
144 _nb_cells = new_nb_cells;
146 _labels = new_labels;
147 _responses = new_responses;
150 void Parsing::update_cell_responses(PiFeatureFamily *pi_feature_family,
151 Classifier *classifier) {
152 LabelledImage *image;
154 image = _image_pool->grab_image(_image_index);
155 image->compute_rich_structure();
157 SampleSet *samples = new SampleSet(pi_feature_family->nb_features(), 1);
159 for(int c = 0; c < _nb_cells; c++) {
160 samples->set_sample(0, pi_feature_family, image, &_cells[c], 0);
161 _responses[c] += classifier->response(samples, 0);
162 ASSERT(!isnan(_responses[c]));
165 _image_pool->release_image(_image_index);
169 void Parsing::collect_samples(SampleSet *samples,
170 PiFeatureFamily *pi_feature_family,
173 LabelledImage *image;
175 image = _image_pool->grab_image(_image_index);
176 image->compute_rich_structure();
178 for(int c = 0; c < _nb_cells; c++) {
180 samples->set_sample(s, pi_feature_family, image, &_cells[c], _labels[c]);
185 _image_pool->release_image(_image_index);