Removed the definition of basename, which confuses an existing system one.
[folded-ctf.git] / folding.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 <iostream>
26 #include <fstream>
27 #include <cmath>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 using namespace std;
33
34 #include "misc.h"
35 #include "param_parser.h"
36 #include "global.h"
37 #include "labelled_image_pool_file.h"
38 #include "labelled_image_pool_subset.h"
39 #include "tools.h"
40 #include "detector.h"
41 #include "pose_cell_hierarchy.h"
42 #include "error_rates.h"
43 #include "materials.h"
44
45 //////////////////////////////////////////////////////////////////////
46
47 void check(bool condition, const char *message) {
48   if(!condition) {
49     cerr << message << endl;
50     exit(1);
51   }
52 }
53
54 //////////////////////////////////////////////////////////////////////
55
56 int main(int argc, char **argv) {
57
58 #ifdef DEBUG
59   cout << endl;
60   cout << "**********************************************************************" << endl;
61   cout << "**                     COMPILED IN DEBUG MODE                       **" << endl;
62   cout << "**********************************************************************" << endl;
63   cout << endl;
64 #endif
65
66   char *new_argv[argc];
67   int new_argc = 0;
68
69   cout << "-- ARGUMENTS ---------------------------------------------------------" << endl;
70
71   for(int i = 0; i < argc; i++)
72     cout << (i > 0 ? "  " : "") << argv[i] << (i < argc - 1 ? " \\" : "")
73          << endl;
74
75   {
76     ParamParser parser;
77     global.init_parser(&parser);
78     parser.parse_options(argc, argv, false, &new_argc, new_argv);
79     global.read_parser(&parser);
80     (*global.log_stream)
81       << "-- PARAMETERS --------------------------------------------------------"
82       << endl;
83     parser.print_all(global.log_stream);
84   }
85
86   nice(global.niceness);
87
88   (*global.log_stream) << "INFO RANDOM_SEED " << global.random_seed << endl;
89   srand48(global.random_seed);
90
91   LabelledImagePool *main_pool = 0;
92   LabelledImagePool *train_pool = 0, *validation_pool = 0, *hierarchy_pool = 0;
93   LabelledImagePool *test_pool = 0;
94   Detector *detector = 0;
95
96   {
97     char buffer[buffer_size];
98     gethostname(buffer, buffer_size);
99     (*global.log_stream) << "INFO HOSTNAME " << buffer << endl;
100   }
101
102   for(int c = 1; c < new_argc; c++) {
103
104     if(strcmp(new_argv[c], "open-pool") == 0) {
105       cout
106         << "-- OPENING POOL ------------------------------------------------------"
107         << endl;
108
109       check(!main_pool, "Pool already opened.");
110       check(global.pool_name[0], "No pool file.");
111
112       main_pool = new LabelledImagePoolFile(global.pool_name);
113
114       bool for_test[main_pool->nb_images()];
115       bool for_train[main_pool->nb_images()];
116       bool for_validation[main_pool->nb_images()];
117       bool for_hierarchy[main_pool->nb_images()];
118
119       for(int n = 0; n < main_pool->nb_images(); n++) {
120         for_test[n] = false;
121         for_train[n] = false;
122         for_validation[n] = false;
123         scalar_t r = drand48();
124         if(r < global.proportion_for_train)
125           for_train[n] = true;
126         else if(r < global.proportion_for_train + global.proportion_for_validation)
127           for_validation[n] = true;
128         else if(global.proportion_for_test < 0 ||
129                 r < global.proportion_for_train +
130                 global.proportion_for_validation +
131                 global.proportion_for_test)
132           for_test[n] = true;
133         for_hierarchy[n] = for_train[n] || for_validation[n];
134       }
135
136       train_pool = new LabelledImagePoolSubset(main_pool, for_train);
137       validation_pool = new LabelledImagePoolSubset(main_pool, for_validation);
138       hierarchy_pool = new LabelledImagePoolSubset(main_pool, for_hierarchy);
139
140       if(global.test_pool_name[0]) {
141         test_pool = new LabelledImagePoolFile(global.test_pool_name);
142       } else {
143         test_pool = new LabelledImagePoolSubset(main_pool, for_test);
144       }
145
146       cout << "Using "
147            << train_pool->nb_images() << " images for train, "
148            << validation_pool->nb_images() << " images for validation, "
149            << hierarchy_pool->nb_images() << " images for the hierarchy and "
150            << test_pool->nb_images() << " images for test."
151            << endl;
152
153     }
154
155     //////////////////////////////////////////////////////////////////////
156
157     else if(strcmp(new_argv[c], "train-detector") == 0) {
158       cout << "-- TRAIN DETECTOR ----------------------------------------------------" << endl;
159       check(train_pool, "No train pool available.");
160       check(validation_pool, "No validation pool available.");
161       check(hierarchy_pool, "No hierarchy pool available.");
162       check(!detector, "Existing detector, can not train another one.");
163       detector = new Detector();
164       detector->train(train_pool, validation_pool, hierarchy_pool);
165     }
166
167     else if(strcmp(new_argv[c], "compute-thresholds") == 0) {
168       cout << "-- COMPUTE THRESHOLDS ------------------------------------------------" << endl;
169       check(validation_pool, "No validation pool available.");
170       check(detector, "No detector.");
171       detector->compute_thresholds(validation_pool, global.wanted_true_positive_rate);
172     }
173
174     //////////////////////////////////////////////////////////////////////
175
176     else if(strcmp(new_argv[c], "test-detector") == 0) {
177       cout << "-- TEST DETECTOR -----------------------------------------------------" << endl;
178
179       check(test_pool, "No test pool available.");
180       check(detector, "No detector.");
181
182       if(test_pool->nb_images() > 0) {
183         print_decimated_error_rate(global.nb_levels - 1, test_pool, detector);
184       } else {
185         cout << "No test image." << endl;
186       }
187     }
188
189     else if(strcmp(new_argv[c], "sequence-test-detector") == 0) {
190       cout << "-- SEQUENCE TEST DETECTOR --------------------------------------------" << endl;
191
192       check(test_pool, "No test pool available.");
193       check(detector, "No detector.");
194
195       if(test_pool->nb_images() > 0) {
196
197         for(int n = 0; n < global.nb_wanted_true_positive_rates; n++) {
198           scalar_t r = global.wanted_true_positive_rate *
199             scalar_t(n + 1) / scalar_t(global.nb_wanted_true_positive_rates);
200           cout << "Testint at tp " << r
201                << " (" << n + 1 << "/" << global.nb_wanted_true_positive_rates << ")"
202                << endl;
203           (*global.log_stream) << "INFO THRESHOLD_FOR_TP " << r << endl;
204           detector->compute_thresholds(validation_pool, r);
205           print_decimated_error_rate(global.nb_levels - 1, test_pool, detector);
206         }
207       } else {
208         cout << "No test image." << endl;
209       }
210     }
211
212     //////////////////////////////////////////////////////////////////////
213
214     else if(strcmp(new_argv[c], "write-detector") == 0) {
215       cout << "-- WRITE DETECTOR ----------------------------------------------------" << endl;
216       ofstream out(global.detector_name);
217       if(out.fail()) {
218         cerr << "Can not write to " << global.detector_name << endl;
219         exit(1);
220       }
221       check(detector, "No detector available.");
222       detector->write(&out);
223     }
224
225     else if(strcmp(new_argv[c], "read-detector") == 0) {
226       cout << "-- READ DETECTOR -----------------------------------------------------" << endl;
227
228       check(!detector, "Existing detector, can not load another one.");
229
230       ifstream in(global.detector_name);
231       if(in.fail()) {
232         cerr << "Can not read from " << global.detector_name << endl;
233         exit(1);
234       }
235
236       detector = new Detector();
237       detector->read(&in);
238     }
239
240     //////////////////////////////////////////////////////////////////////
241
242     else if(strcmp(new_argv[c], "write-pool-images") == 0) {
243       cout << "-- WRITING POOL IMAGES -----------------------------------------------" << endl;
244       check(global.nb_images > 0, "You must set nb_images to a positive value.");
245       check(train_pool, "No train pool available.");
246       write_pool_images_with_poses_and_referentials(train_pool, detector);
247     }
248
249     //////////////////////////////////////////////////////////////////////
250
251     else {
252       cerr << "Unknown action " << new_argv[c] << endl;
253       exit(1);
254     }
255
256     //////////////////////////////////////////////////////////////////////
257
258   }
259
260   delete detector;
261
262   delete train_pool;
263   delete validation_pool;
264   delete hierarchy_pool;
265   delete test_pool;
266
267   delete main_pool;
268
269   cout << "-- FINISHED ----------------------------------------------------------" << endl;
270
271 }