automatic commit
[folded-ctf.git] / error_rates.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 "error_rates.h"
27 #include "fusion_sort.h"
28 #include "pose_cell_hierarchy.h"
29 #include "boosted_classifier.h"
30 #include "parsing_pool.h"
31 #include "materials.h"
32
33 void compute_errors_on_one_image(int level,
34                                  LabelledImage *image,
35                                  PoseCellSet *cell_set,
36                                  int *nb_fns, int *nb_fas) {
37
38   int hit[image->nb_targets()];
39
40   for(int t = 0; t < image->nb_targets(); t++) {
41     hit[t] = 0;
42   }
43
44   Pose pose;
45
46   for(int c = 0; c < cell_set->nb_cells(); c++) {
47     cell_set->get_cell(c)->get_centroid(&pose);
48
49     int false_positive = 1;
50
51     for(int t = 0; t < image->nb_targets(); t++) {
52       if(pose.hit(level, image->get_target_pose(t))) {
53         hit[t] = 1;
54         false_positive = 0;
55       }
56     }
57
58     if(false_positive) (*nb_fas)++;
59   }
60
61   for(int t = 0; t < image->nb_targets(); t++) {
62     if(!hit[t]) (*nb_fns)++;
63   }
64 }
65
66 void print_decimated_error_rate(int level, LabelledImagePool *pool, Detector *detector) {
67   LabelledImage *image;
68   PoseCellScoredSet result_cell_set;
69
70   int nb_fns = 0, nb_fas = 0, nb_targets = 0;
71   long int total_surface = 0;
72
73   cout << "Testing the detector." << endl;
74
75   global.bar.init(&cout, pool->nb_images());
76   for(int i = 0; i < pool->nb_images(); i++) {
77     image = pool->grab_image(i);
78     total_surface += image->width() * image->height();
79     image->compute_rich_structure();
80
81     detector->parse(image, &result_cell_set);
82     result_cell_set.decimate_collide(level);
83     result_cell_set.decimate_hit(level);
84
85     compute_errors_on_one_image(level, image, &result_cell_set, &nb_fns, &nb_fas);
86
87     if(global.write_parse_images) {
88       char buffer[buffer_size];
89       sprintf(buffer, "%s/parse-%04d.png", global.result_path, i);
90       write_image_with_detections(buffer,
91                                   image,
92                                   &result_cell_set, level);
93     }
94
95     nb_targets += image->nb_targets();
96     pool->release_image(i);
97     global.bar.refresh(&cout, i);
98   }
99   global.bar.finish(&cout);
100
101   scalar_t fn_rate = scalar_t(nb_fns)/scalar_t(nb_targets);
102   scalar_t nb_fas_per_vga = (scalar_t(nb_fas) / scalar_t(total_surface)) * scalar_t(640 * 480);
103
104   (*global.log_stream)
105     << "INFO DECIMATED_NB_FALSE_NEGATIVES " << nb_fns << endl
106     << "INFO DECIMATED_NB_TARGETS " << nb_targets << endl
107     << "INFO DECIMATED_FALSE_NEGATIVE_RATE " << fn_rate << endl
108     << "INFO DECIMATED_NB_FALSE_POSITIVES " << nb_fas << endl
109     << "INFO DECIMATED_NB_FALSE_POSITIVES_PER_VGA " << nb_fas_per_vga << endl
110     << "INFO NB_SCENES " << pool->nb_images() << endl
111     << "INFO TOTAL_SURFACE " << total_surface << endl;
112   ;
113 }