automatic commit
[folded-ctf.git] / parsing.cc
1 /*
2  *  folded-ctf is an implementation of the folded hierarchy of
3  *  classifiers for object detection, developed by Francois Fleuret
4  *  and Donald Geman.
5  *
6  *  Copyright (c) 2008 Idiap Research Institute, http://www.idiap.ch/
7  *  Written by Francois Fleuret <francois.fleuret@idiap.ch>
8  *
9  *  This file is part of folded-ctf.
10  *
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.
15  *
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.
20  *
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/>.
23  *
24  */
25
26 #include "parsing.h"
27 #include "fusion_sort.h"
28
29 Parsing::Parsing(LabelledImagePool *image_pool,
30                  PoseCellHierarchy *hierarchy,
31                  scalar_t proportion_negative_cells,
32                  int image_index) {
33
34   _image_pool = image_pool;
35   _image_index = image_index;
36
37   PoseCellSet cell_set;
38   LabelledImage *image;
39
40   image = _image_pool->grab_image(_image_index);
41
42   hierarchy->add_root_cells(image, &cell_set);
43
44   int *kept = new int[cell_set.nb_cells()];
45
46   _nb_cells = 0;
47
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++;
52   }
53
54   _cells = new PoseCell[_nb_cells];
55   _responses = new scalar_t[_nb_cells];
56   _labels = new int[_nb_cells];
57   _nb_positives = 0;
58   _nb_negatives = 0;
59
60   int d = 0;
61   for(int c = 0; c < cell_set.nb_cells(); c++) {
62     if(kept[c]) {
63       _cells[d] = *(cell_set.get_cell(c));
64       _labels[d] = image->pose_cell_label(&_cells[d]);
65       _responses[d] = 0;
66       if(_labels[d] < 0) {
67         _nb_negatives++;
68       } else if(_labels[d] > 0) {
69         _nb_positives++;
70       }
71       d++;
72     }
73   }
74
75   delete[] kept;
76
77   _image_pool->release_image(_image_index);
78 }
79
80 Parsing::~Parsing() {
81   delete[] _cells;
82   delete[] _responses;
83   delete[] _labels;
84 }
85
86 void Parsing::down_one_level(PoseCellHierarchy *hierarchy,
87                              int level, int *sample_nb_occurences, scalar_t *sample_responses) {
88   PoseCellSet cell_set;
89   LabelledImage *image;
90
91   int new_nb_cells = 0;
92   for(int c = 0; c < _nb_cells; c++) {
93     new_nb_cells += sample_nb_occurences[c];
94   }
95
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];
99
100   image = _image_pool->grab_image(_image_index);
101   int b = 0;
102
103   for(int c = 0; c < _nb_cells; c++) {
104
105     if(sample_nb_occurences[c] > 0) {
106
107       cell_set.erase_content();
108       hierarchy->add_subcells(level, _cells + c, &cell_set);
109
110       if(_labels[c] > 0) {
111         ASSERT(sample_nb_occurences[c] == 1);
112         int e = -1;
113         for(int d = 0; d < cell_set.nb_cells(); d++) {
114           if(image->pose_cell_label(cell_set.get_cell(d)) > 0) {
115             ASSERT(e < 0);
116             e = d;
117           }
118         }
119         ASSERT(e >= 0);
120         ASSERT(b < new_nb_cells);
121         new_cells[b] = *(cell_set.get_cell(e));
122         new_responses[b] = sample_responses[c];
123         new_labels[b] = 1;
124         b++;
125       }
126
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];
132           new_labels[b] = -1;
133           b++;
134         }
135       }
136
137       else {
138         cerr << "INCONSISTENCY" << endl;
139         abort();
140       }
141     }
142   }
143
144   ASSERT(b == new_nb_cells);
145
146   _image_pool->release_image(_image_index);
147
148   delete[] _cells;
149   delete[] _labels;
150   delete[] _responses;
151   _nb_cells = new_nb_cells;
152   _cells = new_cells;
153   _labels = new_labels;
154   _responses = new_responses;
155 }
156
157 void Parsing::update_cell_responses(PiFeatureFamily *pi_feature_family,
158                                     Classifier *classifier) {
159   LabelledImage *image;
160
161   image = _image_pool->grab_image(_image_index);
162   image->compute_rich_structure();
163
164   SampleSet *samples = new SampleSet(pi_feature_family->nb_features(), 1);
165
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]));
170   }
171
172   _image_pool->release_image(_image_index);
173   delete samples;
174 }
175
176 void Parsing::collect_samples(SampleSet *samples,
177                               PiFeatureFamily *pi_feature_family,
178                               int s,
179                               int *to_collect) {
180   LabelledImage *image;
181
182   image = _image_pool->grab_image(_image_index);
183   image->compute_rich_structure();
184
185   for(int c = 0; c < _nb_cells; c++) {
186     if(to_collect[c]) {
187       samples->set_sample(s, pi_feature_family, image, &_cells[c], _labels[c]);
188       s++;
189     }
190   }
191
192   _image_pool->release_image(_image_index);
193 }