From 63a5587d0dc2a87e7ba1acb0dae270f147dc6f06 Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Thu, 21 Mar 2013 17:00:08 +0400 Subject: [PATCH] exploring possible bug in optimize_linear_svm --- modules/ml/src/svm.cpp | 4 ++-- modules/ml/test/test_mltests2.cpp | 4 ++++ modules/ml/test/test_save_load.cpp | 28 ++++++++++++++-------------- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/modules/ml/src/svm.cpp b/modules/ml/src/svm.cpp index 3c970f2..f71730a 100644 --- a/modules/ml/src/svm.cpp +++ b/modules/ml/src/svm.cpp @@ -1562,14 +1562,14 @@ void CvSVM::optimize_linear_svm() int j, k, sv_count = df[i].sv_count; for( j = 0; j < sv_count; j++ ) { - const float* src = class_count > 1 ? sv[df[i].sv_index[j]] : sv[j]; + const float* src = class_count > 1 && df[i].sv_index ? sv[df[i].sv_index[j]] : sv[j]; double a = df[i].alpha[j]; for( k = 0; k < var_count; k++ ) dst[k] = (float)(dst[k] + src[k]*a); } df[i].sv_count = 1; df[i].alpha[0] = 1.; - if( class_count > 1 ) + if( class_count > 1 && df[i].sv_index ) df[i].sv_index[0] = i; } diff --git a/modules/ml/test/test_mltests2.cpp b/modules/ml/test/test_mltests2.cpp index 80776b4..0e7892c 100644 --- a/modules/ml/test/test_mltests2.cpp +++ b/modules/ml/test/test_mltests2.cpp @@ -769,7 +769,11 @@ void CV_MLBaseTest::load( const char* filename ) else if( !modelName.compare(CV_KNEAREST) ) knearest->load( filename ); else if( !modelName.compare(CV_SVM) ) + { + delete svm; + svm = new CvSVM; svm->load( filename ); + } else if( !modelName.compare(CV_ANN) ) ann->load( filename ); else if( !modelName.compare(CV_DTREE) ) diff --git a/modules/ml/test/test_save_load.cpp b/modules/ml/test/test_save_load.cpp index 889b98b..fde5410 100644 --- a/modules/ml/test/test_save_load.cpp +++ b/modules/ml/test/test_save_load.cpp @@ -64,11 +64,11 @@ int CV_SLMLTest::run_test_case( int testCaseIdx ) if( code == cvtest::TS::OK ) { get_error( testCaseIdx, CV_TEST_ERROR, &test_resps1 ); - fname1 = tempfile(".yml.gz"); + fname1 = tempfile(".yml"); save( fname1.c_str() ); load( fname1.c_str() ); get_error( testCaseIdx, CV_TEST_ERROR, &test_resps2 ); - fname2 = tempfile(".yml.gz"); + fname2 = tempfile(".yml"); save( fname2.c_str() ); } else @@ -84,30 +84,30 @@ int CV_SLMLTest::validate_test_results( int testCaseIdx ) // 1. compare files ifstream f1( fname1.c_str() ), f2( fname2.c_str() ); string s1, s2; - int lineIdx = 0; + int lineIdx = 1; CV_Assert( f1.is_open() && f2.is_open() ); for( ; !f1.eof() && !f2.eof(); lineIdx++ ) { getline( f1, s1 ); getline( f2, s2 ); - if( s1.compare(s2) ) + if( s1.compare(s2) != 0 ) { - ts->printf( cvtest::TS::LOG, "first and second saved files differ in %n-line; first %n line: %s; second %n-line: %s", - lineIdx, lineIdx, s1.c_str(), lineIdx, s2.c_str() ); + ts->printf( cvtest::TS::LOG, + "in test case %d first (%s) and second (%s) saved files differ in %d-th line:\n%s\n\tvs\n%s\n", + testCaseIdx, fname1.c_str(), fname2.c_str(), + lineIdx, s1.empty() ? "" : s1.c_str(), s2.empty() ? "" : s2.c_str() ); code = cvtest::TS::FAIL_INVALID_OUTPUT; + break; } } - if( !f1.eof() || !f2.eof() ) - { - ts->printf( cvtest::TS::LOG, "in test case %d first and second saved files differ in %n-line; first %n line: %s; second %n-line: %s", - testCaseIdx, lineIdx, lineIdx, s1.c_str(), lineIdx, s2.c_str() ); - code = cvtest::TS::FAIL_INVALID_OUTPUT; - } f1.close(); f2.close(); // delete temporary files - remove( fname1.c_str() ); - remove( fname2.c_str() ); + if( code == cvtest::TS::OK ) + { + remove( fname1.c_str() ); + remove( fname2.c_str() ); + } // 2. compare responses CV_Assert( test_resps1.size() == test_resps2.size() ); -- 2.7.4