mirror of
https://github.com/opencv/opencv.git
synced 2024-11-29 05:29:54 +08:00
Merge pull request #5370 from berak:fix_svm_autoTrain
This commit is contained in:
commit
dfec99691b
@ -1795,10 +1795,10 @@ public:
|
||||
if( !do_train( temp_train_samples, temp_train_responses ))
|
||||
continue;
|
||||
|
||||
for( i = 0; i < train_sample_count; i++ )
|
||||
for( i = 0; i < test_sample_count; i++ )
|
||||
{
|
||||
j = sidx[(i+start+train_sample_count) % sample_count];
|
||||
memcpy(temp_train_samples.ptr(i), samples.ptr(j), sample_size);
|
||||
memcpy(temp_test_samples.ptr(i), samples.ptr(j), sample_size);
|
||||
}
|
||||
|
||||
predict(temp_test_samples, temp_test_responses, 0);
|
||||
|
@ -87,3 +87,34 @@ void CV_SVMTrainAutoTest::run( int /*start_from*/ )
|
||||
}
|
||||
|
||||
TEST(ML_SVM, trainauto) { CV_SVMTrainAutoTest test; test.safe_run(); }
|
||||
|
||||
|
||||
TEST(ML_SVM, trainAuto_regression_5369)
|
||||
{
|
||||
int datasize = 100;
|
||||
cv::Mat samples = cv::Mat::zeros( datasize, 2, CV_32FC1 );
|
||||
cv::Mat responses = cv::Mat::zeros( datasize, 1, CV_32S );
|
||||
|
||||
RNG rng(0); // fixed!
|
||||
for (int i = 0; i < datasize; ++i)
|
||||
{
|
||||
int response = rng.uniform(0, 2); // Random from {0, 1}.
|
||||
samples.at<float>( i, 0 ) = 0;
|
||||
samples.at<float>( i, 1 ) = (0.5f - response) * rng.uniform(0.f, 1.2f) + response;
|
||||
responses.at<int>( i, 0 ) = response;
|
||||
}
|
||||
|
||||
cv::Ptr<TrainData> data = TrainData::create( samples, cv::ml::ROW_SAMPLE, responses );
|
||||
cv::Ptr<SVM> svm = SVM::create();
|
||||
svm->trainAuto( data, 10 ); // 2-fold cross validation.
|
||||
|
||||
float test_data0[2] = {0.25f, 0.25f};
|
||||
cv::Mat test_point0 = cv::Mat( 1, 2, CV_32FC1, test_data0 );
|
||||
float result0 = svm->predict( test_point0 );
|
||||
float test_data1[2] = {0.75f, 0.75f};
|
||||
cv::Mat test_point1 = cv::Mat( 1, 2, CV_32FC1, test_data1 );
|
||||
float result1 = svm->predict( test_point1 );
|
||||
|
||||
EXPECT_EQ(0., result0);
|
||||
EXPECT_EQ(1., result1);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user