Update to 2.0.0 tree from current Fremantle build
[opencv] / apps / haartraining / haartraining.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of Intel Corporation may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41
42 /*
43  * haartraining.cpp
44  *
45  * Train cascade classifier
46  */
47
48 #include <cstdio>
49 #include <cstring>
50 #include <cstdlib>
51
52 #include "cvhaartraining.h"
53
54 int main( int argc, char* argv[] )
55 {
56     int i = 0;
57     char* nullname = (char*)"(NULL)";
58
59     char* vecname = NULL;
60     char* dirname = NULL;
61     char* bgname  = NULL;
62
63     bool bg_vecfile = false;
64     int npos    = 2000;
65     int nneg    = 2000;
66     int nstages = 14;
67     int mem     = 200;
68     int nsplits = 1;
69     float minhitrate     = 0.995F;
70     float maxfalsealarm  = 0.5F;
71     float weightfraction = 0.95F;
72     int mode         = 0;
73     int symmetric    = 1;
74     int equalweights = 0;
75     int width  = 24;
76     int height = 24;
77     const char* boosttypes[] = { "DAB", "RAB", "LB", "GAB" };
78     int boosttype = 3;
79     const char* stumperrors[] = { "misclass", "gini", "entropy" };
80     int stumperror = 0;
81     int maxtreesplits = 0;
82     int minpos = 500;
83
84     if( argc == 1 )
85     {
86         printf( "Usage: %s\n  -data <dir_name>\n"
87                 "  -vec <vec_file_name>\n"
88                 "  -bg <background_file_name>\n"
89                 "  [-bg-vecfile]\n"
90                 "  [-npos <number_of_positive_samples = %d>]\n"
91                 "  [-nneg <number_of_negative_samples = %d>]\n"
92                 "  [-nstages <number_of_stages = %d>]\n"
93                 "  [-nsplits <number_of_splits = %d>]\n"
94                 "  [-mem <memory_in_MB = %d>]\n"
95                 "  [-sym (default)] [-nonsym]\n"
96                 "  [-minhitrate <min_hit_rate = %f>]\n"
97                 "  [-maxfalsealarm <max_false_alarm_rate = %f>]\n"
98                 "  [-weighttrimming <weight_trimming = %f>]\n"
99                 "  [-eqw]\n"
100                 "  [-mode <BASIC (default) | CORE | ALL>]\n"
101                 "  [-w <sample_width = %d>]\n"
102                 "  [-h <sample_height = %d>]\n"
103                 "  [-bt <DAB | RAB | LB | GAB (default)>]\n"
104                 "  [-err <misclass (default) | gini | entropy>]\n"
105                 "  [-maxtreesplits <max_number_of_splits_in_tree_cascade = %d>]\n"
106                 "  [-minpos <min_number_of_positive_samples_per_cluster = %d>]\n",
107                 argv[0], npos, nneg, nstages, nsplits, mem,
108                 minhitrate, maxfalsealarm, weightfraction, width, height,
109                 maxtreesplits, minpos );
110
111         return 0;
112     }
113
114     for( i = 1; i < argc; i++ )
115     {
116         if( !strcmp( argv[i], "-data" ) )
117         {
118             dirname = argv[++i];
119         }
120         else if( !strcmp( argv[i], "-vec" ) )
121         {
122             vecname = argv[++i];
123         }
124         else if( !strcmp( argv[i], "-bg" ) )
125         {
126             bgname = argv[++i];
127         }
128         else if( !strcmp( argv[i], "-bg-vecfile" ) )
129         {
130             bg_vecfile = true;
131         }
132         else if( !strcmp( argv[i], "-npos" ) )
133         {
134             npos = atoi( argv[++i] );
135         }
136         else if( !strcmp( argv[i], "-nneg" ) )
137         {
138             nneg = atoi( argv[++i] );
139         }
140         else if( !strcmp( argv[i], "-nstages" ) )
141         {
142             nstages = atoi( argv[++i] );
143         }
144         else if( !strcmp( argv[i], "-nsplits" ) )
145         {
146             nsplits = atoi( argv[++i] );
147         }
148         else if( !strcmp( argv[i], "-mem" ) )
149         {
150             mem = atoi( argv[++i] );
151         }
152         else if( !strcmp( argv[i], "-sym" ) )
153         {
154             symmetric = 1;
155         }
156         else if( !strcmp( argv[i], "-nonsym" ) )
157         {
158             symmetric = 0;
159         }
160         else if( !strcmp( argv[i], "-minhitrate" ) )
161         {
162             minhitrate = (float) atof( argv[++i] );
163         }
164         else if( !strcmp( argv[i], "-maxfalsealarm" ) )
165         {
166             maxfalsealarm = (float) atof( argv[++i] );
167         }
168         else if( !strcmp( argv[i], "-weighttrimming" ) )
169         {
170             weightfraction = (float) atof( argv[++i] );
171         }
172         else if( !strcmp( argv[i], "-eqw" ) )
173         {
174             equalweights = 1;
175         }
176         else if( !strcmp( argv[i], "-mode" ) )
177         {
178             char* tmp = argv[++i];
179
180             if( !strcmp( tmp, "CORE" ) )
181             {
182                 mode = 1;
183             }
184             else if( !strcmp( tmp, "ALL" ) )
185             {
186                 mode = 2;
187             }
188             else
189             {
190                 mode = 0;
191             }
192         }
193         else if( !strcmp( argv[i], "-w" ) )
194         {
195             width = atoi( argv[++i] );
196         }
197         else if( !strcmp( argv[i], "-h" ) )
198         {
199             height = atoi( argv[++i] );
200         }
201         else if( !strcmp( argv[i], "-bt" ) )
202         {
203             i++;
204             if( !strcmp( argv[i], boosttypes[0] ) )
205             {
206                 boosttype = 0;
207             }
208             else if( !strcmp( argv[i], boosttypes[1] ) )
209             {
210                 boosttype = 1;
211             }
212             else if( !strcmp( argv[i], boosttypes[2] ) )
213             {
214                 boosttype = 2;
215             }
216             else
217             {
218                 boosttype = 3;
219             }
220         }
221         else if( !strcmp( argv[i], "-err" ) )
222         {
223             i++;
224             if( !strcmp( argv[i], stumperrors[0] ) )
225             {
226                 stumperror = 0;
227             }
228             else if( !strcmp( argv[i], stumperrors[1] ) )
229             {
230                 stumperror = 1;
231             }
232             else
233             {
234                 stumperror = 2;
235             }
236         }
237         else if( !strcmp( argv[i], "-maxtreesplits" ) )
238         {
239             maxtreesplits = atoi( argv[++i] );
240         }
241         else if( !strcmp( argv[i], "-minpos" ) )
242         {
243             minpos = atoi( argv[++i] );
244         }
245     }
246
247     printf( "Data dir name: %s\n", ((dirname == NULL) ? nullname : dirname ) );
248     printf( "Vec file name: %s\n", ((vecname == NULL) ? nullname : vecname ) );
249     printf( "BG  file name: %s, is a vecfile: %s\n", ((bgname == NULL) ? nullname : bgname ), bg_vecfile ? "yes" : "no" );
250     printf( "Num pos: %d\n", npos );
251     printf( "Num neg: %d\n", nneg );
252     printf( "Num stages: %d\n", nstages );
253     printf( "Num splits: %d (%s as weak classifier)\n", nsplits,
254         (nsplits == 1) ? "stump" : "tree" );
255     printf( "Mem: %d MB\n", mem );
256     printf( "Symmetric: %s\n", (symmetric) ? "TRUE" : "FALSE" );
257     printf( "Min hit rate: %f\n", minhitrate );
258     printf( "Max false alarm rate: %f\n", maxfalsealarm );
259     printf( "Weight trimming: %f\n", weightfraction );
260     printf( "Equal weights: %s\n", (equalweights) ? "TRUE" : "FALSE" );
261     printf( "Mode: %s\n", ( (mode == 0) ? "BASIC" : ( (mode == 1) ? "CORE" : "ALL") ) );
262     printf( "Width: %d\n", width );
263     printf( "Height: %d\n", height );
264     //printf( "Max num of precalculated features: %d\n", numprecalculated );
265     printf( "Applied boosting algorithm: %s\n", boosttypes[boosttype] );
266     printf( "Error (valid only for Discrete and Real AdaBoost): %s\n",
267             stumperrors[stumperror] );
268
269     printf( "Max number of splits in tree cascade: %d\n", maxtreesplits );
270     printf( "Min number of positive samples per cluster: %d\n", minpos );
271
272     cvCreateTreeCascadeClassifier( dirname, vecname, bgname,
273                                npos, nneg, nstages, mem,
274                                nsplits,
275                                minhitrate, maxfalsealarm, weightfraction,
276                                mode, symmetric,
277                                equalweights, width, height,
278                                boosttype, stumperror,
279                                maxtreesplits, minpos, bg_vecfile );
280
281     return 0;
282 }