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