Path: blob/master/modules/ml/test/test_svmtrainauto.cpp
16354 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// Intel License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000, Intel Corporation, all rights reserved.13// Third party copyrights are property of their respective owners.14//15// Redistribution and use in source and binary forms, with or without modification,16// are permitted provided that the following conditions are met:17//18// * Redistribution's of source code must retain the above copyright notice,19// this list of conditions and the following disclaimer.20//21// * Redistribution's in binary form must reproduce the above copyright notice,22// this list of conditions and the following disclaimer in the documentation23// and/or other materials provided with the distribution.24//25// * The name of Intel Corporation may not be used to endorse or promote products26// derived from this software without specific prior written permission.27//28// This software is provided by the copyright holders and contributors "as is" and29// any express or implied warranties, including, but not limited to, the implied30// warranties of merchantability and fitness for a particular purpose are disclaimed.31// In no event shall the Intel Corporation or contributors be liable for any direct,32// indirect, incidental, special, exemplary, or consequential damages33// (including, but not limited to, procurement of substitute goods or services;34// loss of use, data, or profits; or business interruption) however caused35// and on any theory of liability, whether in contract, strict liability,36// or tort (including negligence or otherwise) arising in any way out of37// the use of this software, even if advised of the possibility of such damage.38//39//M*/4041#include "test_precomp.hpp"4243namespace opencv_test { namespace {4445using cv::ml::SVM;46using cv::ml::TrainData;4748//--------------------------------------------------------------------------------------------49class CV_SVMTrainAutoTest : public cvtest::BaseTest {50public:51CV_SVMTrainAutoTest() {}52protected:53virtual void run( int start_from );54};5556void CV_SVMTrainAutoTest::run( int /*start_from*/ )57{58int datasize = 100;59cv::Mat samples = cv::Mat::zeros( datasize, 2, CV_32FC1 );60cv::Mat responses = cv::Mat::zeros( datasize, 1, CV_32S );6162RNG rng(0);63for (int i = 0; i < datasize; ++i)64{65int response = rng.uniform(0, 2); // Random from {0, 1}.66samples.at<float>( i, 0 ) = rng.uniform(0.f, 0.5f) + response * 0.5f;67samples.at<float>( i, 1 ) = rng.uniform(0.f, 0.5f) + response * 0.5f;68responses.at<int>( i, 0 ) = response;69}7071cv::Ptr<TrainData> data = TrainData::create( samples, cv::ml::ROW_SAMPLE, responses );72cv::Ptr<SVM> svm = SVM::create();73svm->trainAuto( data, 10 ); // 2-fold cross validation.7475float test_data0[2] = {0.25f, 0.25f};76cv::Mat test_point0 = cv::Mat( 1, 2, CV_32FC1, test_data0 );77float result0 = svm->predict( test_point0 );78float test_data1[2] = {0.75f, 0.75f};79cv::Mat test_point1 = cv::Mat( 1, 2, CV_32FC1, test_data1 );80float result1 = svm->predict( test_point1 );8182if ( fabs( result0 - 0 ) > 0.001 || fabs( result1 - 1 ) > 0.001 )83{84ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );85}86}8788TEST(ML_SVM, trainauto) { CV_SVMTrainAutoTest test; test.safe_run(); }899091TEST(ML_SVM, trainAuto_regression_5369)92{93int datasize = 100;94cv::Mat samples = cv::Mat::zeros( datasize, 2, CV_32FC1 );95cv::Mat responses = cv::Mat::zeros( datasize, 1, CV_32S );9697RNG rng(0); // fixed!98for (int i = 0; i < datasize; ++i)99{100int response = rng.uniform(0, 2); // Random from {0, 1}.101samples.at<float>( i, 0 ) = 0;102samples.at<float>( i, 1 ) = (0.5f - response) * rng.uniform(0.f, 1.2f) + response;103responses.at<int>( i, 0 ) = response;104}105106cv::Ptr<TrainData> data = TrainData::create( samples, cv::ml::ROW_SAMPLE, responses );107cv::Ptr<SVM> svm = SVM::create();108svm->trainAuto( data, 10 ); // 2-fold cross validation.109110float test_data0[2] = {0.25f, 0.25f};111cv::Mat test_point0 = cv::Mat( 1, 2, CV_32FC1, test_data0 );112float result0 = svm->predict( test_point0 );113float test_data1[2] = {0.75f, 0.75f};114cv::Mat test_point1 = cv::Mat( 1, 2, CV_32FC1, test_data1 );115float result1 = svm->predict( test_point1 );116117EXPECT_EQ(0., result0);118EXPECT_EQ(1., result1);119}120121class CV_SVMGetSupportVectorsTest : public cvtest::BaseTest {122public:123CV_SVMGetSupportVectorsTest() {}124protected:125virtual void run( int startFrom );126};127void CV_SVMGetSupportVectorsTest::run(int /*startFrom*/ )128{129int code = cvtest::TS::OK;130131// Set up training data132int labels[4] = {1, -1, -1, -1};133float trainingData[4][2] = { {501, 10}, {255, 10}, {501, 255}, {10, 501} };134Mat trainingDataMat(4, 2, CV_32FC1, trainingData);135Mat labelsMat(4, 1, CV_32SC1, labels);136137Ptr<SVM> svm = SVM::create();138svm->setType(SVM::C_SVC);139svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));140141142// Test retrieval of SVs and compressed SVs on linear SVM143svm->setKernel(SVM::LINEAR);144svm->train(trainingDataMat, cv::ml::ROW_SAMPLE, labelsMat);145146Mat sv = svm->getSupportVectors();147CV_Assert(sv.rows == 1); // by default compressed SV returned148sv = svm->getUncompressedSupportVectors();149CV_Assert(sv.rows == 3);150151152// Test retrieval of SVs and compressed SVs on non-linear SVM153svm->setKernel(SVM::POLY);154svm->setDegree(2);155svm->train(trainingDataMat, cv::ml::ROW_SAMPLE, labelsMat);156157sv = svm->getSupportVectors();158CV_Assert(sv.rows == 3);159sv = svm->getUncompressedSupportVectors();160CV_Assert(sv.rows == 0); // inapplicable for non-linear SVMs161162163ts->set_failed_test_info(code);164}165166167TEST(ML_SVM, getSupportVectors) { CV_SVMGetSupportVectorsTest test; test.safe_run(); }168169}} // namespace170171172