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