From 74fc9acdd1ae975ecf4d89824d8b0ee329199b68 Mon Sep 17 00:00:00 2001 From: berak Date: Sat, 19 Sep 2015 10:10:33 +0200 Subject: [PATCH] initialize temp_train_samples in SVM::autoTrain add smoke test --- modules/ml/src/svm.cpp | 4 ++-- modules/ml/test/test_svmtrainauto.cpp | 31 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/modules/ml/src/svm.cpp b/modules/ml/src/svm.cpp index 812e2839bb1f..402de3f1df17 100644 --- a/modules/ml/src/svm.cpp +++ b/modules/ml/src/svm.cpp @@ -1795,10 +1795,10 @@ class SVMImpl : public SVM 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); diff --git a/modules/ml/test/test_svmtrainauto.cpp b/modules/ml/test/test_svmtrainauto.cpp index 918d2b7117e8..3c4b72924586 100644 --- a/modules/ml/test/test_svmtrainauto.cpp +++ b/modules/ml/test/test_svmtrainauto.cpp @@ -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( i, 0 ) = 0; + samples.at( i, 1 ) = (0.5f - response) * rng.uniform(0.f, 1.2f) + response; + responses.at( i, 0 ) = response; + } + + cv::Ptr data = TrainData::create( samples, cv::ml::ROW_SAMPLE, responses ); + cv::Ptr 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); +}