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. //
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. //
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/>. //
15 // Written by Francois Fleuret, (C) IDIAP //
16 // Contact <francois.fleuret@idiap.ch> for comments & bug reports //
17 ///////////////////////////////////////////////////////////////////////////
29 #include "param_parser.h"
31 #include "labelled_image_pool_file.h"
32 #include "labelled_image_pool_subset.h"
35 #include "pose_cell_hierarchy.h"
36 #include "error_rates.h"
37 #include "materials.h"
39 //////////////////////////////////////////////////////////////////////
41 void check(bool condition, const char *message) {
43 cerr << message << endl;
48 //////////////////////////////////////////////////////////////////////
50 int main(int argc, char **argv) {
56 cout << "**********************************************************************" << endl;
57 cout << "** COMPILED IN DEBUG MODE **" << endl;
58 cout << "**********************************************************************" << endl;
62 cout << "-- ARGUMENTS ---------------------------------------------------------" << endl;
63 for(int i = 0; i < argc; i++)
64 cout << (i > 0 ? " " : "") << argv[i] << (i < argc - 1 ? " \\" : "")
69 global.init_parser(&parser);
70 parser.parse_options(argc, argv, false, &new_argc, new_argv);
71 global.read_parser(&parser);
73 << "-- PARAMETERS --------------------------------------------------------"
75 parser.print_all(global.log_stream);
78 nice(global.niceness);
80 (*global.log_stream) << "INFO RANDOM_SEED " << global.random_seed << endl;
81 srand48(global.random_seed);
83 LabelledImagePool *main_pool = 0;
84 LabelledImagePool *train_pool = 0, *validation_pool = 0, *hierarchy_pool = 0;
85 LabelledImagePool *test_pool = 0;
86 Detector *detector = 0;
89 char buffer[buffer_size];
90 gethostname(buffer, buffer_size);
91 (*global.log_stream) << "INFO HOSTNAME " << buffer << endl;
94 for(int c = 1; c < new_argc; c++) {
96 if(strcmp(new_argv[c], "open-pool") == 0) {
98 << "-- OPENING POOL ------------------------------------------------------"
101 check(!main_pool, "Pool already opened.");
102 check(global.pool_name[0], "No pool file.");
104 main_pool = new LabelledImagePoolFile(global.pool_name);
106 bool for_test[main_pool->nb_images()];
107 bool for_train[main_pool->nb_images()];
108 bool for_validation[main_pool->nb_images()];
109 bool for_hierarchy[main_pool->nb_images()];
111 for(int n = 0; n < main_pool->nb_images(); n++) {
113 for_train[n] = false;
114 for_validation[n] = false;
115 scalar_t r = drand48();
116 if(r < global.proportion_for_train)
118 else if(r < global.proportion_for_train + global.proportion_for_validation)
119 for_validation[n] = true;
120 else if(global.proportion_for_test < 0 ||
121 r < global.proportion_for_train +
122 global.proportion_for_validation +
123 global.proportion_for_test)
125 for_hierarchy[n] = for_train[n] || for_validation[n];
128 train_pool = new LabelledImagePoolSubset(main_pool, for_train);
129 validation_pool = new LabelledImagePoolSubset(main_pool, for_validation);
130 hierarchy_pool = new LabelledImagePoolSubset(main_pool, for_hierarchy);
132 if(global.test_pool_name[0]) {
133 test_pool = new LabelledImagePoolFile(global.test_pool_name);
135 test_pool = new LabelledImagePoolSubset(main_pool, for_test);
139 << train_pool->nb_images() << " images for train, "
140 << validation_pool->nb_images() << " images for validation, "
141 << hierarchy_pool->nb_images() << " images for the hierarchy and "
142 << test_pool->nb_images() << " images for test."
147 //////////////////////////////////////////////////////////////////////
149 else if(strcmp(new_argv[c], "train-detector") == 0) {
150 cout << "-- TRAIN DETECTOR ----------------------------------------------------" << endl;
151 check(train_pool, "No train pool available.");
152 check(validation_pool, "No validation pool available.");
153 check(hierarchy_pool, "No hierarchy pool available.");
154 check(!detector, "Existing detector, can not train another one.");
155 detector = new Detector();
156 detector->train(train_pool, validation_pool, hierarchy_pool);
159 else if(strcmp(new_argv[c], "compute-thresholds") == 0) {
160 cout << "-- COMPUTE THRESHOLDS ------------------------------------------------" << endl;
161 check(validation_pool, "No validation pool available.");
162 check(detector, "No detector.");
163 detector->compute_thresholds(validation_pool, global.wanted_true_positive_rate);
166 //////////////////////////////////////////////////////////////////////
168 else if(strcmp(new_argv[c], "test-detector") == 0) {
169 cout << "-- TEST DETECTOR -----------------------------------------------------" << endl;
171 check(test_pool, "No test pool available.");
172 check(detector, "No detector.");
174 if(test_pool->nb_images() > 0) {
175 print_decimated_error_rate(global.nb_levels - 1, test_pool, detector);
177 cout << "No test image." << endl;
181 //////////////////////////////////////////////////////////////////////
183 else if(strcmp(new_argv[c], "sequence-test-detector") == 0) {
184 cout << "-- SEQUENCE TEST DETECTOR --------------------------------------------" << endl;
186 check(test_pool, "No test pool available.");
187 check(detector, "No detector.");
189 if(test_pool->nb_images() > 0) {
191 for(int n = 0; n < global.nb_wanted_true_positive_rates; n++) {
192 scalar_t r = global.wanted_true_positive_rate *
193 scalar_t(n + 1) / scalar_t(global.nb_wanted_true_positive_rates);
194 cout << "Testint at tp " << r
195 << " (" << n + 1 << "/" << global.nb_wanted_true_positive_rates << ")"
197 (*global.log_stream) << "INFO THRESHOLD_FOR_TP " << r << endl;
198 detector->compute_thresholds(validation_pool, r);
199 print_decimated_error_rate(global.nb_levels - 1, test_pool, detector);
202 cout << "No test image." << endl;
206 //////////////////////////////////////////////////////////////////////
208 else if(strcmp(new_argv[c], "write-detector") == 0) {
209 cout << "-- WRITE DETECTOR ----------------------------------------------------" << endl;
210 ofstream out(global.detector_name);
212 cerr << "Can not write to " << global.detector_name << endl;
215 check(detector, "No detector available.");
216 detector->write(&out);
219 //////////////////////////////////////////////////////////////////////
221 else if(strcmp(new_argv[c], "read-detector") == 0) {
222 cout << "-- READ DETECTOR -----------------------------------------------------" << endl;
224 check(!detector, "Existing detector, can not load another one.");
226 ifstream in(global.detector_name);
228 cerr << "Can not read from " << global.detector_name << endl;
232 detector = new Detector();
236 //////////////////////////////////////////////////////////////////////
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);
245 //////////////////////////////////////////////////////////////////////
248 cerr << "Unknown action " << new_argv[c] << endl;
252 //////////////////////////////////////////////////////////////////////
259 delete validation_pool;
260 delete hierarchy_pool;
265 cout << "-- FINISHED ----------------------------------------------------------" << endl;