Home | History | Annotate | Download | only in test
      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 #include "test_precomp.hpp"
     43 
     44 using namespace cv;
     45 using namespace std;
     46 
     47 CV_AMLTest::CV_AMLTest( const char* _modelName ) : CV_MLBaseTest( _modelName )
     48 {
     49     validationFN = "avalidation.xml";
     50 }
     51 
     52 int CV_AMLTest::run_test_case( int testCaseIdx )
     53 {
     54     int code = cvtest::TS::OK;
     55     code = prepare_test_case( testCaseIdx );
     56 
     57     if (code == cvtest::TS::OK)
     58     {
     59         //#define GET_STAT
     60 #ifdef GET_STAT
     61         const char* data_name = ((CvFileNode*)cvGetSeqElem( dataSetNames, testCaseIdx ))->data.str.ptr;
     62         printf("%s, %s      ", name, data_name);
     63         const int icount = 100;
     64         float res[icount];
     65         for (int k = 0; k < icount; k++)
     66         {
     67 #endif
     68             data->shuffleTrainTest();
     69             code = train( testCaseIdx );
     70 #ifdef GET_STAT
     71             float case_result = get_error();
     72 
     73             res[k] = case_result;
     74         }
     75         float mean = 0, sigma = 0;
     76         for (int k = 0; k < icount; k++)
     77         {
     78             mean += res[k];
     79         }
     80         mean = mean /icount;
     81         for (int k = 0; k < icount; k++)
     82         {
     83             sigma += (res[k] - mean)*(res[k] - mean);
     84         }
     85         sigma = sqrt(sigma/icount);
     86         printf("%f, %f\n", mean, sigma);
     87 #endif
     88     }
     89     return code;
     90 }
     91 
     92 int CV_AMLTest::validate_test_results( int testCaseIdx )
     93 {
     94     int iters;
     95     float mean, sigma;
     96     // read validation params
     97     FileNode resultNode =
     98         validationFS.getFirstTopLevelNode()["validation"][modelName][dataSetNames[testCaseIdx]]["result"];
     99     resultNode["iter_count"] >> iters;
    100     if ( iters > 0)
    101     {
    102         resultNode["mean"] >> mean;
    103         resultNode["sigma"] >> sigma;
    104         model->save(format("/Users/vp/tmp/dtree/testcase_%02d.cur.yml", testCaseIdx));
    105         float curErr = get_test_error( testCaseIdx );
    106         const int coeff = 4;
    107         ts->printf( cvtest::TS::LOG, "Test case = %d; test error = %f; mean error = %f (diff=%f), %d*sigma = %f\n",
    108                                 testCaseIdx, curErr, mean, abs( curErr - mean), coeff, coeff*sigma );
    109         if ( abs( curErr - mean) > coeff*sigma )
    110         {
    111             ts->printf( cvtest::TS::LOG, "abs(%f - %f) > %f - OUT OF RANGE!\n", curErr, mean, coeff*sigma, coeff );
    112             return cvtest::TS::FAIL_BAD_ACCURACY;
    113         }
    114         else
    115             ts->printf( cvtest::TS::LOG, ".\n" );
    116 
    117     }
    118     else
    119     {
    120         ts->printf( cvtest::TS::LOG, "validation info is not suitable" );
    121         return cvtest::TS::FAIL_INVALID_TEST_DATA;
    122     }
    123     return cvtest::TS::OK;
    124 }
    125 
    126 TEST(ML_DTree, regression) { CV_AMLTest test( CV_DTREE ); test.safe_run(); }
    127 TEST(ML_Boost, regression) { CV_AMLTest test( CV_BOOST ); test.safe_run(); }
    128 TEST(ML_RTrees, regression) { CV_AMLTest test( CV_RTREES ); test.safe_run(); }
    129 TEST(DISABLED_ML_ERTrees, regression) { CV_AMLTest test( CV_ERTREES ); test.safe_run(); }
    130 
    131 /* End of file. */
    132