2 * folded-ctf is an implementation of the folded hierarchy of
3 * classifiers for object detection, developed by Francois Fleuret
6 * Copyright (c) 2008 Idiap Research Institute, http://www.idiap.ch/
7 * Written by Francois Fleuret <francois.fleuret@idiap.ch>
9 * This file is part of folded-ctf.
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.
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.
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/>.
35 #include "param_parser.h"
37 #include "labelled_image_pool_file.h"
38 #include "labelled_image_pool_subset.h"
41 #include "pose_cell_hierarchy.h"
42 #include "error_rates.h"
43 #include "materials.h"
45 //////////////////////////////////////////////////////////////////////
47 void check(bool condition, const char *message) {
49 cerr << message << endl;
54 //////////////////////////////////////////////////////////////////////
56 int main(int argc, char **argv) {
60 cout << "**********************************************************************" << endl;
61 cout << "** COMPILED IN DEBUG MODE **" << endl;
62 cout << "**********************************************************************" << endl;
69 cout << "-- ARGUMENTS ---------------------------------------------------------" << endl;
71 for(int i = 0; i < argc; i++)
72 cout << (i > 0 ? " " : "") << argv[i] << (i < argc - 1 ? " \\" : "")
77 global.init_parser(&parser);
78 parser.parse_options(argc, argv, false, &new_argc, new_argv);
79 global.read_parser(&parser);
81 << "-- PARAMETERS --------------------------------------------------------"
83 parser.print_all(global.log_stream);
86 nice(global.niceness);
88 (*global.log_stream) << "INFO RANDOM_SEED " << global.random_seed << endl;
89 srand48(global.random_seed);
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;
97 char buffer[buffer_size];
98 gethostname(buffer, buffer_size);
99 (*global.log_stream) << "INFO HOSTNAME " << buffer << endl;
102 for(int c = 1; c < new_argc; c++) {
104 if(strcmp(new_argv[c], "open-pool") == 0) {
106 << "-- OPENING POOL ------------------------------------------------------"
109 check(!main_pool, "Pool already opened.");
110 check(global.pool_name[0], "No pool file.");
112 main_pool = new LabelledImagePoolFile(global.pool_name);
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()];
119 for(int n = 0; n < main_pool->nb_images(); n++) {
121 for_train[n] = false;
122 for_validation[n] = false;
123 scalar_t r = drand48();
124 if(r < global.proportion_for_train)
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)
133 for_hierarchy[n] = for_train[n] || for_validation[n];
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);
140 if(global.test_pool_name[0]) {
141 test_pool = new LabelledImagePoolFile(global.test_pool_name);
143 test_pool = new LabelledImagePoolSubset(main_pool, for_test);
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."
155 //////////////////////////////////////////////////////////////////////
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);
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);
174 //////////////////////////////////////////////////////////////////////
176 else if(strcmp(new_argv[c], "test-detector") == 0) {
177 cout << "-- TEST DETECTOR -----------------------------------------------------" << endl;
179 check(test_pool, "No test pool available.");
180 check(detector, "No detector.");
182 if(test_pool->nb_images() > 0) {
183 print_decimated_error_rate(global.nb_levels - 1, test_pool, detector);
185 cout << "No test image." << endl;
189 else if(strcmp(new_argv[c], "sequence-test-detector") == 0) {
190 cout << "-- SEQUENCE TEST DETECTOR --------------------------------------------" << endl;
192 check(test_pool, "No test pool available.");
193 check(detector, "No detector.");
195 if(test_pool->nb_images() > 0) {
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 << ")"
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);
208 cout << "No test image." << endl;
212 //////////////////////////////////////////////////////////////////////
214 else if(strcmp(new_argv[c], "write-detector") == 0) {
215 cout << "-- WRITE DETECTOR ----------------------------------------------------" << endl;
216 ofstream out(global.detector_name);
218 cerr << "Can not write to " << global.detector_name << endl;
221 check(detector, "No detector available.");
222 detector->write(&out);
225 else if(strcmp(new_argv[c], "read-detector") == 0) {
226 cout << "-- READ DETECTOR -----------------------------------------------------" << endl;
228 check(!detector, "Existing detector, can not load another one.");
230 ifstream in(global.detector_name);
232 cerr << "Can not read from " << global.detector_name << endl;
236 detector = new Detector();
240 //////////////////////////////////////////////////////////////////////
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);
249 //////////////////////////////////////////////////////////////////////
252 cerr << "Unknown action " << new_argv[c] << endl;
256 //////////////////////////////////////////////////////////////////////
263 delete validation_pool;
264 delete hierarchy_pool;
269 cout << "-- FINISHED ----------------------------------------------------------" << endl;