automatic commit
[folded-ctf.git] / error_rates.cc
diff --git a/error_rates.cc b/error_rates.cc
new file mode 100644 (file)
index 0000000..14d4d8c
--- /dev/null
@@ -0,0 +1,137 @@
+
+///////////////////////////////////////////////////////////////////////////
+// This program is free software: you can redistribute it and/or modify  //
+// it under the terms of the version 3 of the GNU General Public License //
+// as published by the Free Software Foundation.                         //
+//                                                                       //
+// This program is distributed in the hope that it will be useful, but   //
+// WITHOUT ANY WARRANTY; without even the implied warranty of            //
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      //
+// General Public License for more details.                              //
+//                                                                       //
+// You should have received a copy of the GNU General Public License     //
+// along with this program. If not, see <http://www.gnu.org/licenses/>.  //
+//                                                                       //
+// Written by Francois Fleuret, (C) IDIAP                                //
+// Contact <francois.fleuret@idiap.ch> for comments & bug reports        //
+///////////////////////////////////////////////////////////////////////////
+
+#include "error_rates.h"
+#include "fusion_sort.h"
+#include "pose_cell_hierarchy.h"
+#include "boosted_classifier.h"
+#include "parsing_pool.h"
+#include "chrono.h"
+#include "materials.h"
+
+void compute_errors_on_one_image(int level,
+                                 LabelledImage *image,
+                                 PoseCellSet *cell_set,
+                                 int *nb_fns, int *nb_fas) {
+
+  int hit[image->nb_targets()];
+
+  for(int t = 0; t < image->nb_targets(); t++) {
+    hit[t] = 0;
+  }
+
+  Pose pose;
+
+  for(int c = 0; c < cell_set->nb_cells(); c++) {
+    cell_set->get_cell(c)->get_centroid(&pose);
+
+    int false_positive = 1;
+
+    for(int t = 0; t < image->nb_targets(); t++) {
+      if(pose.hit(level, image->get_target_pose(t))) {
+        hit[t] = 1;
+        false_positive = 0;
+      }
+    }
+
+    if(false_positive) (*nb_fas)++;
+  }
+
+  for(int t = 0; t < image->nb_targets(); t++) {
+    if(!hit[t]) (*nb_fns)++;
+  }
+}
+
+void print_decimated_error_rate(int level, LabelledImagePool *pool, Detector *detector) {
+  LabelledImage *image;
+  PoseCellScoredSet result_cell_set;
+
+  int nb_fns = 0, nb_fas = 0, nb_targets = 0;
+  long int total_surface = 0;
+
+  cout << "Testing the detector." << endl;
+
+  global.bar.init(&cout, pool->nb_images());
+  for(int i = 0; i < pool->nb_images(); i++) {
+    image = pool->grab_image(i);
+    total_surface += image->width() * image->height();
+    image->compute_rich_structure();
+
+    detector->parse(image, &result_cell_set);
+    result_cell_set.decimate_collide(level);
+    result_cell_set.decimate_hit(level);
+
+    compute_errors_on_one_image(level, image, &result_cell_set, &nb_fns, &nb_fas);
+
+    if(global.write_parse_images) {
+      char buffer[buffer_size];
+      sprintf(buffer, "%s/parse-%04d.png", global.result_path, i);
+      cout << "LEVEL = " << level << endl;
+      write_image_with_detections(buffer,
+                                  image,
+                                  &result_cell_set, level);
+    }
+
+    nb_targets += image->nb_targets();
+    pool->release_image(i);
+    global.bar.refresh(&cout, i);
+  }
+  global.bar.finish(&cout);
+
+  scalar_t fn_rate = scalar_t(nb_fns)/scalar_t(nb_targets);
+  scalar_t nb_fas_per_vga = (scalar_t(nb_fas) / scalar_t(total_surface)) * scalar_t(640 * 480);
+
+  (*global.log_stream)
+    << "INFO DECIMATED_NB_FALSE_NEGATIVES " << nb_fns << endl
+    << "INFO DECIMATED_NB_TARGETS " << nb_targets << endl
+    << "INFO DECIMATED_FALSE_NEGATIVE_RATE " << fn_rate << endl
+    << "INFO DECIMATED_NB_FALSE_POSITIVES " << nb_fas << endl
+    << "INFO DECIMATED_NB_FALSE_POSITIVES_PER_VGA " << nb_fas_per_vga << endl
+    << "INFO NB_SCENES " << pool->nb_images() << endl
+    << "INFO TOTAL_SURFACE " << total_surface << endl;
+  ;
+}
+
+void parse_scene(Detector *detector, const char *image_name) {
+  RGBImage tmp;
+  tmp.read_jpg(image_name);
+  RichImage image(tmp.width(), tmp.height());
+
+  for(int y = 0; y < tmp.height(); y++) {
+    for(int x = 0; x < tmp.width(); x++) {
+      image.set_value(x, y, int(scalar_t(tmp.pixel(x, y, 0)) * 0.2989 +
+                                scalar_t(tmp.pixel(x, y, 1)) * 0.5870 +
+                                scalar_t(tmp.pixel(x, y, 2)) * 0.1140));
+    }
+  }
+
+  image.compute_rich_structure();
+
+  PoseCellScoredSet cell_set;
+  detector->parse(&image, &cell_set);
+  cell_set.decimate_hit(detector->nb_levels() - 1);
+
+  cout << "RESULT " << image_name << endl;
+  for(int c = 0; c < cell_set.nb_cells(); c++) {
+    cout << "ALARM " << c << endl;
+    Pose alarm;
+    cell_set.get_cell(c)->get_centroid(&alarm);
+    alarm.print(&cout);
+  }
+  cout << "END_RESULT" << endl;
+}