2 * svrt is the ``Synthetic Visual Reasoning Test'', an image
3 * generator for evaluating classification performance of machine
4 * learning systems, humans and primates.
6 * Copyright (c) 2009 Idiap Research Institute, http://www.idiap.ch/
7 * Written by Francois Fleuret <francois.fleuret@idiap.ch>
9 * This file is part of svrt.
11 * svrt is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 3 as
13 * published by the Free Software Foundation.
15 * svrt 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 selector. If not, see <http://www.gnu.org/licenses/>.
33 #include "rgb_image.h"
34 #include "param_parser.h"
39 #include "classifier.h"
40 #include "classifier_reader.h"
41 #include "naive_bayesian_classifier.h"
42 #include "boosted_classifier.h"
43 #include "error_rates.h"
45 #include "vision_problem_1.h"
46 #include "vision_problem_2.h"
47 #include "vision_problem_3.h"
48 #include "vision_problem_4.h"
49 #include "vision_problem_5.h"
50 #include "vision_problem_6.h"
51 #include "vision_problem_7.h"
52 #include "vision_problem_8.h"
53 #include "vision_problem_9.h"
54 #include "vision_problem_10.h"
55 #include "vision_problem_11.h"
56 #include "vision_problem_12.h"
57 #include "vision_problem_13.h"
58 #include "vision_problem_14.h"
59 #include "vision_problem_15.h"
60 #include "vision_problem_16.h"
61 #include "vision_problem_17.h"
62 #include "vision_problem_18.h"
63 #include "vision_problem_19.h"
64 #include "vision_problem_20.h"
65 #include "vision_problem_21.h"
66 #include "vision_problem_22.h"
67 #include "vision_problem_23.h"
69 //////////////////////////////////////////////////////////////////////
71 void check(bool condition, const char *message) {
73 cerr << message << endl;
78 int main(int argc, char **argv) {
80 char buffer[buffer_size];
84 cout << "-- ARGUMENTS ---------------------------------------------------------" << endl;
86 for(int i = 0; i < argc; i++)
87 cout << (i > 0 ? " " : "") << argv[i] << (i < argc - 1 ? " \\" : "")
90 cout << "-- PARAMETERS --------------------------------------------------------" << endl;
94 global.init_parser(&parser);
95 parser.parse_options(argc, argv, false, &new_argc, new_argv);
96 global.read_parser(&parser);
97 parser.print_all(&cout);
100 nice(global.niceness);
101 srand48(global.random_seed);
103 VignetteGenerator *generator;
105 switch(global.problem_number) {
107 generator = new VisionProblem_1();
110 generator = new VisionProblem_2();
113 generator = new VisionProblem_3();
116 generator = new VisionProblem_4();
119 generator = new VisionProblem_5();
122 generator = new VisionProblem_6();
125 generator = new VisionProblem_7();
128 generator = new VisionProblem_8();
131 generator = new VisionProblem_9();
134 generator = new VisionProblem_10();
137 generator = new VisionProblem_11();
140 generator = new VisionProblem_12();
143 generator = new VisionProblem_13();
146 generator = new VisionProblem_14();
149 generator = new VisionProblem_15();
152 generator = new VisionProblem_16();
155 generator = new VisionProblem_17();
158 generator = new VisionProblem_18();
161 generator = new VisionProblem_19();
164 generator = new VisionProblem_20();
167 generator = new VisionProblem_21();
170 generator = new VisionProblem_22();
173 generator = new VisionProblem_23();
176 cerr << "Can not find problem "
177 << global.problem_number
182 generator->precompute();
184 //////////////////////////////////////////////////////////////////////
186 Vignette *train_samples;
189 train_samples = new Vignette[global.nb_train_samples];
190 train_labels = new int[global.nb_train_samples];
192 //////////////////////////////////////////////////////////////////////
194 Classifier *classifier = 0;
196 cout << "-- COMPUTATIONS ------------------------------------------------------" << endl;
198 for(int c = 1; c < new_argc; c++) {
200 if(strcmp(new_argv[c], "randomize-train") == 0) {
201 cout << "Generating the training set." << endl;
202 for(int n = 0; n < global.nb_train_samples; n++) {
203 train_labels[n] = int(drand48() * 2);
204 generator->generate(train_labels[n], &train_samples[n]);
208 else if(strcmp(new_argv[c], "adaboost") == 0) {
210 cout << "Building and training adaboost classifier." << endl;
211 classifier = new BoostedClassifier(global.nb_weak_learners);
212 classifier->train(global.nb_train_samples, train_samples, train_labels);
215 else if(strcmp(new_argv[c], "naive-bayesian") == 0) {
217 cout << "Building and training naive bayesian classifier." << endl;
218 classifier = new NaiveBayesianClassifier();
219 classifier->train(global.nb_train_samples, train_samples, train_labels);
222 else if(strcmp(new_argv[c], "read-classifier") == 0) {
224 sprintf(buffer, "%s", global.classifier_name);
225 cout << "Reading classifier from " << buffer << "." << endl;
228 cerr << "Can not open " << buffer << " for reading." << endl;
231 classifier = read_classifier(&in);
234 else if(strcmp(new_argv[c], "write-classifier") == 0) {
235 check(classifier, "No classifier.");
236 sprintf(buffer, "%s/%s", global.result_path, global.classifier_name);
237 cout << "Writing classifier to " << buffer << "." << endl;
238 ofstream out(buffer);
240 cerr << "Can not open " << buffer << " for writing." << endl;
243 classifier->write(&out);
246 else if(strcmp(new_argv[c], "compute-errors-vs-nb-samples") == 0) {
247 for(int t = global.nb_train_samples; t >= 100; t /= 10) {
248 for(int n = 0; n < t; n++) {
249 train_labels[n] = int(drand48() * 2);
250 generator->generate(train_labels[n], &train_samples[n]);
252 Classifier *classifier = 0;
253 cout << "Building and training adaboost classifier with " << t << " samples." << endl;
254 classifier = new BoostedClassifier(global.nb_weak_learners);
255 classifier->train(t, train_samples, train_labels);
256 cout << "ERROR_RATES_VS_NB_SAMPLES "
259 << error_rate(classifier, t, train_samples, train_labels)
261 << test_error_rate(generator, classifier, global.nb_test_samples) << endl;
266 else if(strcmp(new_argv[c], "compute-train-error") == 0) {
267 check(classifier, "No classifier.");
268 cout << "TRAIN_ERROR_RATE "
269 << classifier->name()
271 << error_rate(classifier, global.nb_train_samples, train_samples, train_labels)
275 else if(strcmp(new_argv[c], "compute-test-error") == 0) {
276 check(classifier, "No classifier.");
277 cout << "TEST_ERROR_RATE "
278 << classifier->name()
280 << test_error_rate(generator, classifier, global.nb_test_samples) << endl;
283 else if(strcmp(new_argv[c], "write-samples") == 0) {
285 for(int k = 0; k < global.nb_train_samples; k++) {
286 for(int l = 0; l < 2; l++) {
287 generator->generate(l, &vignette);
288 sprintf(buffer, "%s/sample_%01d_%04d.png", global.result_path, l, k);
289 vignette.write_png(buffer, 1);
290 cout << "Wrote " << buffer << endl;
295 //////////////////////////////////////////////////////////////////////
297 //////////////////////////////////////////////////////////////////////
300 cerr << "Unknown action " << new_argv[c] << endl;
306 cout << "-- FINISHED ----------------------------------------------------------" << endl;
309 delete[] train_labels;
310 delete[] train_samples;