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 using cv::ml::SVM;
47 using cv::ml::TrainData;
48
49 //--------------------------------------------------------------------------------------------
50 class CV_SVMTrainAutoTest : public cvtest::BaseTest {
51 public:
CV_SVMTrainAutoTest()52 CV_SVMTrainAutoTest() {}
53 protected:
54 virtual void run( int start_from );
55 };
56
run(int)57 void CV_SVMTrainAutoTest::run( int /*start_from*/ )
58 {
59 int datasize = 100;
60 cv::Mat samples = cv::Mat::zeros( datasize, 2, CV_32FC1 );
61 cv::Mat responses = cv::Mat::zeros( datasize, 1, CV_32S );
62
63 RNG rng(0);
64 for (int i = 0; i < datasize; ++i)
65 {
66 int response = rng.uniform(0, 2); // Random from {0, 1}.
67 samples.at<float>( i, 0 ) = rng.uniform(0.f, 0.5f) + response * 0.5f;
68 samples.at<float>( i, 1 ) = rng.uniform(0.f, 0.5f) + response * 0.5f;
69 responses.at<int>( i, 0 ) = response;
70 }
71
72 cv::Ptr<TrainData> data = TrainData::create( samples, cv::ml::ROW_SAMPLE, responses );
73 cv::Ptr<SVM> svm = SVM::create();
74 svm->trainAuto( data, 10 ); // 2-fold cross validation.
75
76 float test_data0[2] = {0.25f, 0.25f};
77 cv::Mat test_point0 = cv::Mat( 1, 2, CV_32FC1, test_data0 );
78 float result0 = svm->predict( test_point0 );
79 float test_data1[2] = {0.75f, 0.75f};
80 cv::Mat test_point1 = cv::Mat( 1, 2, CV_32FC1, test_data1 );
81 float result1 = svm->predict( test_point1 );
82
83 if ( fabs( result0 - 0 ) > 0.001 || fabs( result1 - 1 ) > 0.001 )
84 {
85 ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
86 }
87 }
88
TEST(ML_SVM,trainauto)89 TEST(ML_SVM, trainauto) { CV_SVMTrainAutoTest test; test.safe_run(); }
90