Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/ml/test/test_svmtrainauto.cpp
16354 views
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
namespace opencv_test { namespace {
45
46
using cv::ml::SVM;
47
using cv::ml::TrainData;
48
49
//--------------------------------------------------------------------------------------------
50
class CV_SVMTrainAutoTest : public cvtest::BaseTest {
51
public:
52
CV_SVMTrainAutoTest() {}
53
protected:
54
virtual void run( int start_from );
55
};
56
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
89
TEST(ML_SVM, trainauto) { CV_SVMTrainAutoTest test; test.safe_run(); }
90
91
92
TEST(ML_SVM, trainAuto_regression_5369)
93
{
94
int datasize = 100;
95
cv::Mat samples = cv::Mat::zeros( datasize, 2, CV_32FC1 );
96
cv::Mat responses = cv::Mat::zeros( datasize, 1, CV_32S );
97
98
RNG rng(0); // fixed!
99
for (int i = 0; i < datasize; ++i)
100
{
101
int response = rng.uniform(0, 2); // Random from {0, 1}.
102
samples.at<float>( i, 0 ) = 0;
103
samples.at<float>( i, 1 ) = (0.5f - response) * rng.uniform(0.f, 1.2f) + response;
104
responses.at<int>( i, 0 ) = response;
105
}
106
107
cv::Ptr<TrainData> data = TrainData::create( samples, cv::ml::ROW_SAMPLE, responses );
108
cv::Ptr<SVM> svm = SVM::create();
109
svm->trainAuto( data, 10 ); // 2-fold cross validation.
110
111
float test_data0[2] = {0.25f, 0.25f};
112
cv::Mat test_point0 = cv::Mat( 1, 2, CV_32FC1, test_data0 );
113
float result0 = svm->predict( test_point0 );
114
float test_data1[2] = {0.75f, 0.75f};
115
cv::Mat test_point1 = cv::Mat( 1, 2, CV_32FC1, test_data1 );
116
float result1 = svm->predict( test_point1 );
117
118
EXPECT_EQ(0., result0);
119
EXPECT_EQ(1., result1);
120
}
121
122
class CV_SVMGetSupportVectorsTest : public cvtest::BaseTest {
123
public:
124
CV_SVMGetSupportVectorsTest() {}
125
protected:
126
virtual void run( int startFrom );
127
};
128
void CV_SVMGetSupportVectorsTest::run(int /*startFrom*/ )
129
{
130
int code = cvtest::TS::OK;
131
132
// Set up training data
133
int labels[4] = {1, -1, -1, -1};
134
float trainingData[4][2] = { {501, 10}, {255, 10}, {501, 255}, {10, 501} };
135
Mat trainingDataMat(4, 2, CV_32FC1, trainingData);
136
Mat labelsMat(4, 1, CV_32SC1, labels);
137
138
Ptr<SVM> svm = SVM::create();
139
svm->setType(SVM::C_SVC);
140
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));
141
142
143
// Test retrieval of SVs and compressed SVs on linear SVM
144
svm->setKernel(SVM::LINEAR);
145
svm->train(trainingDataMat, cv::ml::ROW_SAMPLE, labelsMat);
146
147
Mat sv = svm->getSupportVectors();
148
CV_Assert(sv.rows == 1); // by default compressed SV returned
149
sv = svm->getUncompressedSupportVectors();
150
CV_Assert(sv.rows == 3);
151
152
153
// Test retrieval of SVs and compressed SVs on non-linear SVM
154
svm->setKernel(SVM::POLY);
155
svm->setDegree(2);
156
svm->train(trainingDataMat, cv::ml::ROW_SAMPLE, labelsMat);
157
158
sv = svm->getSupportVectors();
159
CV_Assert(sv.rows == 3);
160
sv = svm->getUncompressedSupportVectors();
161
CV_Assert(sv.rows == 0); // inapplicable for non-linear SVMs
162
163
164
ts->set_failed_test_info(code);
165
}
166
167
168
TEST(ML_SVM, getSupportVectors) { CV_SVMGetSupportVectorsTest test; test.safe_run(); }
169
170
}} // namespace
171
172