Update to 2.0.0 tree from current Fremantle build
[opencv] / tests / ml / src / amltests.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////\r
2 //\r
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
4 //\r
5 //  By downloading, copying, installing or using the software you agree to this license.\r
6 //  If you do not agree to this license, do not download, install,\r
7 //  copy or use the software.\r
8 //\r
9 //\r
10 //                        Intel License Agreement\r
11 //                For Open Source Computer Vision Library\r
12 //\r
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.\r
14 // Third party copyrights are property of their respective owners.\r
15 //\r
16 // Redistribution and use in source and binary forms, with or without modification,\r
17 // are permitted provided that the following conditions are met:\r
18 //\r
19 //   * Redistribution's of source code must retain the above copyright notice,\r
20 //     this list of conditions and the following disclaimer.\r
21 //\r
22 //   * Redistribution's in binary form must reproduce the above copyright notice,\r
23 //     this list of conditions and the following disclaimer in the documentation\r
24 //     and/or other materials provided with the distribution.\r
25 //\r
26 //   * The name of Intel Corporation may not be used to endorse or promote products\r
27 //     derived from this software without specific prior written permission.\r
28 //\r
29 // This software is provided by the copyright holders and contributors "as is" and\r
30 // any express or implied warranties, including, but not limited to, the implied\r
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.\r
32 // In no event shall the Intel Corporation or contributors be liable for any direct,\r
33 // indirect, incidental, special, exemplary, or consequential damages\r
34 // (including, but not limited to, procurement of substitute goods or services;\r
35 // loss of use, data, or profits; or business interruption) however caused\r
36 // and on any theory of liability, whether in contract, strict liability,\r
37 // or tort (including negligence or otherwise) arising in any way out of\r
38 // the use of this software, even if advised of the possibility of such damage.\r
39 //\r
40 //M*/\r
41 \r
42 #include "mltest.h"\r
43 \r
44 CV_AMLTest::CV_AMLTest( const char* _modelName, const char* _testName ) :\r
45     CV_MLBaseTest( _modelName, _testName, "train-predict" )\r
46 {\r
47     validationFN = "avalidation.xml";\r
48 }\r
49 \r
50 int CV_AMLTest::run_test_case( int testCaseIdx )\r
51 {\r
52     int code = CvTS::OK;
53     code = prepare_test_case( testCaseIdx );\r
54 \r
55     if (code == CvTS::OK)\r
56     {\r
57         //#define GET_STAT\r
58 #ifdef GET_STAT\r
59         const char* data_name = ((CvFileNode*)cvGetSeqElem( dataSetNames, testCaseIdx ))->data.str.ptr;     \r
60         printf("%s, %s      ", name, data_name);\r
61         const int icount = 100;\r
62         float res[icount];\r
63         for (int k = 0; k < icount; k++)\r
64         {\r
65 #endif\r
66             data.mix_train_and_test_idx();\r
67             code = train( testCaseIdx );            \r
68 #ifdef GET_STAT\r
69             float case_result = get_error();\r
70 \r
71             res[k] = case_result;\r
72         }\r
73         float mean = 0, sigma = 0;\r
74         for (int k = 0; k < icount; k++)\r
75         {\r
76             mean += res[k];\r
77         }\r
78         mean = mean /icount;\r
79         for (int k = 0; k < icount; k++)\r
80         {\r
81             sigma += (res[k] - mean)*(res[k] - mean);\r
82         }\r
83         sigma = sqrt(sigma/icount);\r
84         printf("%f, %f\n", mean, sigma);\r
85 #endif\r
86     }\r
87     return code;\r
88 }\r
89 \r
90 int CV_AMLTest::validate_test_results( int testCaseIdx )\r
91 {\r
92     int iters;
93     float mean, sigma;
94     // read validation params
95     FileNode resultNode = 
96         validationFS.getFirstTopLevelNode()["validation"][modelName][dataSetNames[testCaseIdx]]["result"];
97     resultNode["iter_count"] >> iters; 
98     if ( iters > 0)
99     {
100         resultNode["mean"] >> mean;
101         resultNode["sigma"] >> sigma;
102         if ( abs(get_error( testCaseIdx, CV_TEST_ERROR ) - mean) > 6*sigma )
103         {
104             ts->printf( CvTS::LOG, "in test case %d test error is out of range", testCaseIdx );
105             return CvTS::FAIL_BAD_ACCURACY;
106         }
107     }
108     else\r
109     {\r
110         ts->printf( CvTS::LOG, "validation info is not suitable" );
111         return CvTS::FAIL_INVALID_TEST_DATA;\r
112     }
113     return CvTS::OK;\r
114 }\r
115 \r
116 CV_AMLTest amldtree( CV_DTREE, "adtree" );\r
117 CV_AMLTest amlboost( CV_BOOST, "aboost" );\r
118 CV_AMLTest amlrtrees( CV_RTREES, "artrees" );\r
119 CV_AMLTest amlertrees( CV_ERTREES, "aertrees" );\r
120 \r
121 /* End of file. */\r