Skip to content

Commit

Permalink
initialize temp_train_samples in SVM::autoTrain
Browse files Browse the repository at this point in the history
add smoke test
  • Loading branch information
berak committed Sep 23, 2015
1 parent d2add41 commit 74fc9ac
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
4 changes: 2 additions & 2 deletions modules/ml/src/svm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
31 changes: 31 additions & 0 deletions modules/ml/test/test_svmtrainauto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 74fc9ac

Please sign in to comment.