automatic commit
[folded-ctf.git] / parsing_pool.cc
1
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.                         //
6 //                                                                       //
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.                              //
11 //                                                                       //
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/>.  //
14 //                                                                       //
15 // Written by Francois Fleuret, (C) IDIAP                                //
16 // Contact <francois.fleuret@idiap.ch> for comments & bug reports        //
17 ///////////////////////////////////////////////////////////////////////////
18
19 #include "parsing_pool.h"
20 #include "tools.h"
21
22 ParsingPool::ParsingPool(LabelledImagePool *image_pool, PoseCellHierarchy *hierarchy, scalar_t proportion_negative_cells) {
23   _nb_images = image_pool->nb_images();
24   _parsings = new Parsing *[_nb_images];
25
26   _nb_cells = 0;
27   _nb_positive_cells = 0;
28   _nb_negative_cells = 0;
29   for(int i = 0; i < _nb_images; i++) {
30     _parsings[i] = new Parsing(image_pool, hierarchy, proportion_negative_cells, i);
31     _nb_cells += _parsings[i]->nb_cells();
32     _nb_positive_cells += _parsings[i]->nb_positive_cells();
33     _nb_negative_cells += _parsings[i]->nb_negative_cells();
34   }
35   (*global.log_stream) << "ParsingPool initialized" << endl;
36   (*global.log_stream) << "  _nb_cells = " << _nb_cells << endl;
37   (*global.log_stream) << "  _nb_positive_cells = " << _nb_positive_cells << endl;
38   (*global.log_stream) << "  _nb_negative_cells = " << _nb_negative_cells << endl;
39 }
40
41 ParsingPool::~ParsingPool() {
42   for(int i = 0; i < _nb_images; i++)
43     delete _parsings[i];
44   delete[] _parsings;
45 }
46
47 void ParsingPool::down_one_level(LossMachine *loss_machine, PoseCellHierarchy *hierarchy, int level) {
48   scalar_t *labels = new scalar_t[_nb_cells];
49   scalar_t *tmp_responses = new scalar_t[_nb_cells];
50
51   int c;
52
53   { ////////////////////////////////////////////////////////////////////
54     // Sanity check
55     scalar_t l = 0;
56     for(int i = 0; i < _nb_images; i++) {
57       for(int d = 0; d < _parsings[i]->nb_cells(); d++) {
58         if(_parsings[i]->label(d) != 0) {
59           l += exp( - _parsings[i]->label(d) * _parsings[i]->response(d));
60         }
61       }
62     }
63     (*global.log_stream) << "* INITIAL LOSS IS " << l << endl;
64   } ////////////////////////////////////////////////////////////////////
65
66   // Put the negative samples with their current responses, and all
67   // others to 0
68
69   c = 0;
70   for(int i = 0; i < _nb_images; i++) {
71     for(int d = 0; d < _parsings[i]->nb_cells(); d++) {
72       if(_parsings[i]->label(d) < 0) {
73         labels[c] = -1;
74         tmp_responses[c] = _parsings[i]->response(d);
75       } else {
76         labels[c] = 0;
77         tmp_responses[c] = 0;
78       }
79       c++;
80     }
81   }
82
83   // Sub-sample among the negative ones
84
85   int *sample_nb_occurences = new int[_nb_cells];
86   scalar_t *sample_responses = new scalar_t[_nb_cells];
87
88   loss_machine->subsample(_nb_cells, labels, tmp_responses,
89                           _nb_negative_cells, sample_nb_occurences, sample_responses,
90                           1);
91
92   c = 0;
93   for(int i = 0; i < _nb_images; i++) {
94     for(int d = 0; d < _parsings[i]->nb_cells(); d++) {
95       if(_parsings[i]->label(d) > 0) {
96         sample_nb_occurences[c + d] = 1;
97         sample_responses[c + d] = _parsings[i]->response(d);
98       }
99     }
100
101     int d = c + _parsings[i]->nb_cells();
102
103     _parsings[i]->down_one_level(hierarchy, level, sample_nb_occurences + c, sample_responses + c);
104
105     c = d;
106   }
107
108   { ////////////////////////////////////////////////////////////////////
109     // Sanity check
110     scalar_t l = 0;
111     for(int i = 0; i < _nb_images; i++) {
112       for(int d = 0; d < _parsings[i]->nb_cells(); d++) {
113         if(_parsings[i]->label(d) != 0) {
114           l += exp( - _parsings[i]->label(d) * _parsings[i]->response(d));
115         }
116       }
117     }
118     (*global.log_stream) << "* FINAL LOSS IS " << l << endl;
119   } ////////////////////////////////////////////////////////////////////
120
121   delete[] sample_responses;
122   delete[] sample_nb_occurences;
123
124   delete[] labels;
125   delete[] tmp_responses;
126 }
127
128 void ParsingPool::update_cell_responses(PiFeatureFamily *pi_feature_family,
129                                         Classifier *classifier) {
130   for(int i = 0; i < _nb_images; i++) {
131     _parsings[i]->update_cell_responses(pi_feature_family, classifier);
132   }
133 }
134
135 void ParsingPool::weighted_sampling(LossMachine *loss_machine,
136                                     PiFeatureFamily *pi_feature_family,
137                                     SampleSet *sample_set,
138                                     scalar_t *responses) {
139
140   int nb_negatives_to_sample = sample_set->nb_samples() - _nb_positive_cells;
141
142   ASSERT(nb_negatives_to_sample > 0);
143
144   scalar_t *labels = new scalar_t[_nb_cells];
145   scalar_t *tmp_responses = new scalar_t[_nb_cells];
146
147   int c, s;
148
149   // Put the negative samples with their current responses, and all
150   // others to 0
151
152   c = 0;
153   for(int i = 0; i < _nb_images; i++) {
154     for(int d = 0; d < _parsings[i]->nb_cells(); d++) {
155       if(_parsings[i]->label(d) < 0) {
156         labels[c] = -1;
157         tmp_responses[c] = _parsings[i]->response(d);
158       } else {
159         labels[c] = 0;
160         tmp_responses[c] = 0;
161       }
162       c++;
163     }
164   }
165
166   // Sub-sample among the negative ones
167
168   int *sample_nb_occurences = new int[_nb_cells];
169   scalar_t *sample_responses = new scalar_t[_nb_cells];
170
171   loss_machine->subsample(_nb_cells, labels, tmp_responses,
172                           nb_negatives_to_sample, sample_nb_occurences, sample_responses,
173                           0);
174
175   for(int k = 0; k < _nb_cells; k++) {
176     if(sample_nb_occurences[k] > 0) {
177       ASSERT(sample_nb_occurences[k] == 1);
178       labels[k] = -1.0;
179       tmp_responses[k] = sample_responses[k];
180     } else {
181       labels[k] = 0;
182     }
183   }
184
185   delete[] sample_responses;
186   delete[] sample_nb_occurences;
187
188   // Put the positive ones
189
190   c = 0;
191   for(int i = 0; i < _nb_images; i++) {
192     for(int d = 0; d < _parsings[i]->nb_cells(); d++) {
193       if(_parsings[i]->label(d) > 0) {
194         labels[c] = 1;
195         tmp_responses[c] = _parsings[i]->response(d);
196       }
197       c++;
198     }
199   }
200
201   // Here we have the responses for the sub-sampled in tmp_responses,
202   // and we have labels[n] set to zero for non-sampled samples
203
204   s = 0;
205   c = 0;
206
207   for(int i = 0; i < _nb_images; i++) {
208
209     int *to_collect = new int[_parsings[i]->nb_cells()];
210
211     for(int d = 0; d < _parsings[i]->nb_cells(); d++) {
212       to_collect[d] = (labels[c + d] != 0);
213     }
214
215     _parsings[i]->collect_samples(sample_set, pi_feature_family, s, to_collect);
216
217     for(int d = 0; d < _parsings[i]->nb_cells(); d++) {
218       if(to_collect[d]) {
219         responses[s++] = tmp_responses[c + d];
220       }
221     }
222
223     delete[] to_collect;
224
225     c += _parsings[i]->nb_cells();
226   }
227
228   delete[] tmp_responses;
229   delete[] labels;
230 }
231
232 void ParsingPool::write_roc(ofstream *out) {
233   int nb_negatives = nb_negative_cells();
234   int nb_positives = nb_positive_cells();
235
236   scalar_t *pos_responses = new scalar_t[nb_positives];
237   scalar_t *neg_responses = new scalar_t[nb_negatives];
238   int np = 0, nn = 0;
239   for(int i = 0; i < _nb_images; i++) {
240     for(int c = 0; c < _parsings[i]->nb_cells(); c++) {
241       if(_parsings[i]->label(c) > 0)
242         pos_responses[np++] = _parsings[i]->response(c);
243       else if(_parsings[i]->label(c) < 0)
244         neg_responses[nn++] = _parsings[i]->response(c);
245     }
246   }
247
248   ASSERT(nn == nb_negatives && np == nb_positives);
249
250   print_roc_small_pos(out,
251                       nb_positives, pos_responses,
252                       nb_negatives, neg_responses,
253                       1.0);
254
255   delete[] pos_responses;
256   delete[] neg_responses;
257 }