automatic commit
[folded-ctf.git] / error_rates.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                                           //
16 // (C) Idiap Research Institute                                          //
17 //                                                                       //
18 // Contact <francois.fleuret@idiap.ch> for comments & bug reports        //
19 ///////////////////////////////////////////////////////////////////////////
20
21 #include "error_rates.h"
22 #include "fusion_sort.h"
23 #include "pose_cell_hierarchy.h"
24 #include "boosted_classifier.h"
25 #include "parsing_pool.h"
26 #include "chrono.h"
27 #include "materials.h"
28
29 void compute_errors_on_one_image(int level,
30                                  LabelledImage *image,
31                                  PoseCellSet *cell_set,
32                                  int *nb_fns, int *nb_fas) {
33
34   int hit[image->nb_targets()];
35
36   for(int t = 0; t < image->nb_targets(); t++) {
37     hit[t] = 0;
38   }
39
40   Pose pose;
41
42   for(int c = 0; c < cell_set->nb_cells(); c++) {
43     cell_set->get_cell(c)->get_centroid(&pose);
44
45     int false_positive = 1;
46
47     for(int t = 0; t < image->nb_targets(); t++) {
48       if(pose.hit(level, image->get_target_pose(t))) {
49         hit[t] = 1;
50         false_positive = 0;
51       }
52     }
53
54     if(false_positive) (*nb_fas)++;
55   }
56
57   for(int t = 0; t < image->nb_targets(); t++) {
58     if(!hit[t]) (*nb_fns)++;
59   }
60 }
61
62 void print_decimated_error_rate(int level, LabelledImagePool *pool, Detector *detector) {
63   LabelledImage *image;
64   PoseCellScoredSet result_cell_set;
65
66   int nb_fns = 0, nb_fas = 0, nb_targets = 0;
67   long int total_surface = 0;
68
69   cout << "Testing the detector." << endl;
70
71   global.bar.init(&cout, pool->nb_images());
72   for(int i = 0; i < pool->nb_images(); i++) {
73     image = pool->grab_image(i);
74     total_surface += image->width() * image->height();
75     image->compute_rich_structure();
76
77     detector->parse(image, &result_cell_set);
78     result_cell_set.decimate_collide(level);
79     result_cell_set.decimate_hit(level);
80
81     compute_errors_on_one_image(level, image, &result_cell_set, &nb_fns, &nb_fas);
82
83     if(global.write_parse_images) {
84       char buffer[buffer_size];
85       sprintf(buffer, "%s/parse-%04d.png", global.result_path, i);
86       cout << "LEVEL = " << level << endl;
87       write_image_with_detections(buffer,
88                                   image,
89                                   &result_cell_set, level);
90     }
91
92     nb_targets += image->nb_targets();
93     pool->release_image(i);
94     global.bar.refresh(&cout, i);
95   }
96   global.bar.finish(&cout);
97
98   scalar_t fn_rate = scalar_t(nb_fns)/scalar_t(nb_targets);
99   scalar_t nb_fas_per_vga = (scalar_t(nb_fas) / scalar_t(total_surface)) * scalar_t(640 * 480);
100
101   (*global.log_stream)
102     << "INFO DECIMATED_NB_FALSE_NEGATIVES " << nb_fns << endl
103     << "INFO DECIMATED_NB_TARGETS " << nb_targets << endl
104     << "INFO DECIMATED_FALSE_NEGATIVE_RATE " << fn_rate << endl
105     << "INFO DECIMATED_NB_FALSE_POSITIVES " << nb_fas << endl
106     << "INFO DECIMATED_NB_FALSE_POSITIVES_PER_VGA " << nb_fas_per_vga << endl
107     << "INFO NB_SCENES " << pool->nb_images() << endl
108     << "INFO TOTAL_SURFACE " << total_surface << endl;
109   ;
110 }
111
112 void parse_scene(Detector *detector, const char *image_name) {
113   RGBImage tmp;
114   tmp.read_jpg(image_name);
115   RichImage image(tmp.width(), tmp.height());
116
117   for(int y = 0; y < tmp.height(); y++) {
118     for(int x = 0; x < tmp.width(); x++) {
119       image.set_value(x, y, int(scalar_t(tmp.pixel(x, y, 0)) * 0.2989 +
120                                 scalar_t(tmp.pixel(x, y, 1)) * 0.5870 +
121                                 scalar_t(tmp.pixel(x, y, 2)) * 0.1140));
122     }
123   }
124
125   image.compute_rich_structure();
126
127   PoseCellScoredSet cell_set;
128   detector->parse(&image, &cell_set);
129   cell_set.decimate_hit(detector->nb_levels() - 1);
130
131   cout << "RESULT " << image_name << endl;
132   for(int c = 0; c < cell_set.nb_cells(); c++) {
133     cout << "ALARM " << c << endl;
134     Pose alarm;
135     cell_set.get_cell(c)->get_centroid(&alarm);
136     alarm.print(&cout);
137   }
138   cout << "END_RESULT" << endl;
139 }