Removed the definition of basename, which confuses an existing system one.
[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 version 3 as
13  *  published by the Free Software Foundation.
14  *
15  *  folded-ctf is distributed in the hope that it will be useful, but
16  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with folded-ctf.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #include "error_rates.h"
26 #include "fusion_sort.h"
27 #include "pose_cell_hierarchy.h"
28 #include "boosted_classifier.h"
29 #include "parsing_pool.h"
30 #include "materials.h"
31
32 void compute_errors_on_one_image(int level,
33                                  LabelledImage *image,
34                                  PoseCellSet *cell_set,
35                                  int *nb_fns, int *nb_fas) {
36
37   int hit[image->nb_targets()];
38
39   for(int t = 0; t < image->nb_targets(); t++) {
40     hit[t] = 0;
41   }
42
43   Pose pose;
44
45   for(int c = 0; c < cell_set->nb_cells(); c++) {
46     cell_set->get_cell(c)->get_centroid(&pose);
47
48     int false_positive = 1;
49
50     for(int t = 0; t < image->nb_targets(); t++) {
51       if(pose.hit(level, image->get_target_pose(t))) {
52         hit[t] = 1;
53         false_positive = 0;
54       }
55     }
56
57     if(false_positive) (*nb_fas)++;
58   }
59
60   for(int t = 0; t < image->nb_targets(); t++) {
61     if(!hit[t]) (*nb_fns)++;
62   }
63 }
64
65 void print_decimated_error_rate(int level, LabelledImagePool *pool, Detector *detector) {
66   LabelledImage *image;
67   PoseCellScoredSet result_cell_set;
68
69   int nb_fns = 0, nb_fas = 0, nb_targets = 0;
70   long int total_surface = 0;
71
72   cout << "Testing the detector." << endl;
73
74   global.bar.init(&cout, pool->nb_images());
75   for(int i = 0; i < pool->nb_images(); i++) {
76     image = pool->grab_image(i);
77     total_surface += image->width() * image->height();
78     image->compute_rich_structure();
79
80     detector->parse(image, &result_cell_set);
81     result_cell_set.decimate_collide(level);
82     result_cell_set.decimate_hit(level);
83
84     compute_errors_on_one_image(level, image, &result_cell_set, &nb_fns, &nb_fas);
85
86     if(global.write_parse_images) {
87       char buffer[buffer_size];
88       sprintf(buffer, "%s/parse-%04d.png", global.result_path, i);
89       write_image_with_detections(buffer,
90                                   image,
91                                   &result_cell_set, level);
92     }
93
94     nb_targets += image->nb_targets();
95     pool->release_image(i);
96     global.bar.refresh(&cout, i);
97   }
98   global.bar.finish(&cout);
99
100   scalar_t fn_rate = scalar_t(nb_fns)/scalar_t(nb_targets);
101   scalar_t nb_fas_per_vga = (scalar_t(nb_fas) / scalar_t(total_surface)) * scalar_t(640 * 480);
102
103   (*global.log_stream)
104     << "INFO DECIMATED_NB_FALSE_NEGATIVES " << nb_fns << endl
105     << "INFO DECIMATED_NB_TARGETS " << nb_targets << endl
106     << "INFO DECIMATED_FALSE_NEGATIVE_RATE " << fn_rate << endl
107     << "INFO DECIMATED_NB_FALSE_POSITIVES " << nb_fas << endl
108     << "INFO DECIMATED_NB_FALSE_POSITIVES_PER_VGA " << nb_fas_per_vga << endl
109     << "INFO NB_SCENES " << pool->nb_images() << endl
110     << "INFO TOTAL_SURFACE " << total_surface << endl;
111   ;
112 }