1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-31 00:03:03 +04:00

Refactored SVMSGD class

This commit is contained in:
Marina Noskova
2016-01-20 12:59:44 +03:00
parent a2f0963d66
commit 40bf97c6d1
11 changed files with 980 additions and 241 deletions
+30 -1
View File
@@ -193,6 +193,16 @@ int str_to_boost_type( String& str )
// 8. rtrees
// 9. ertrees
int str_to_svmsgd_type( String& str )
{
if ( !str.compare("SGD") )
return SVMSGD::SGD;
if ( !str.compare("ASGD") )
return SVMSGD::ASGD;
CV_Error( CV_StsBadArg, "incorrect boost type string" );
return -1;
}
// ---------------------------------- MLBaseTest ---------------------------------------------------
CV_MLBaseTest::CV_MLBaseTest(const char* _modelName)
@@ -248,7 +258,9 @@ void CV_MLBaseTest::run( int )
{
string filename = ts->get_data_path();
filename += get_validation_filename();
validationFS.open( filename, FileStorage::READ );
read_params( *validationFS );
int code = cvtest::TS::OK;
@@ -436,6 +448,21 @@ int CV_MLBaseTest::train( int testCaseIdx )
model = m;
}
else if( modelName == CV_SVMSGD )
{
String svmsgdTypeStr;
modelParamsNode["svmsgdType"] >> svmsgdTypeStr;
Ptr<SVMSGD> m = SVMSGD::create();
int type = str_to_svmsgd_type( svmsgdTypeStr );
m->setType(type);
//m->setType(str_to_svmsgd_type( svmsgdTypeStr ));
m->setLambda(modelParamsNode["lambda"]);
m->setGamma0(modelParamsNode["gamma0"]);
m->setC(modelParamsNode["c"]);
m->setTermCriteria(TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 10000, 0.00001));
model = m;
}
if( !model.empty() )
is_trained = model->train(data, 0);
@@ -457,7 +484,7 @@ float CV_MLBaseTest::get_test_error( int /*testCaseIdx*/, vector<float> *resp )
else if( modelName == CV_ANN )
err = ann_calc_error( model, data, cls_map, type, resp );
else if( modelName == CV_DTREE || modelName == CV_BOOST || modelName == CV_RTREES ||
modelName == CV_SVM || modelName == CV_NBAYES || modelName == CV_KNEAREST )
modelName == CV_SVM || modelName == CV_NBAYES || modelName == CV_KNEAREST || modelName == CV_SVMSGD )
err = model->calcError( data, true, _resp );
if( !_resp.empty() && resp )
_resp.convertTo(*resp, CV_32F);
@@ -485,6 +512,8 @@ void CV_MLBaseTest::load( const char* filename )
model = Algorithm::load<Boost>( filename );
else if( modelName == CV_RTREES )
model = Algorithm::load<RTrees>( filename );
else if( modelName == CV_SVMSGD )
model = Algorithm::load<SVMSGD>( filename );
else
CV_Error( CV_StsNotImplemented, "invalid stat model name");
}
+3
View File
@@ -13,6 +13,7 @@
#include <map>
#include "opencv2/ts.hpp"
#include "opencv2/ml.hpp"
#include "opencv2/ml/svmsgd.hpp"
#include "opencv2/core/core_c.h"
#define CV_NBAYES "nbayes"
@@ -24,6 +25,7 @@
#define CV_BOOST "boost"
#define CV_RTREES "rtrees"
#define CV_ERTREES "ertrees"
#define CV_SVMSGD "svmsgd"
enum { CV_TRAIN_ERROR=0, CV_TEST_ERROR=1 };
@@ -38,6 +40,7 @@ using cv::ml::ANN_MLP;
using cv::ml::DTrees;
using cv::ml::Boost;
using cv::ml::RTrees;
using cv::ml::SVMSGD;
class CV_MLBaseTest : public cvtest::BaseTest
{
+16 -1
View File
@@ -150,12 +150,20 @@ int CV_SLMLTest::validate_test_results( int testCaseIdx )
TEST(ML_NaiveBayes, save_load) { CV_SLMLTest test( CV_NBAYES ); test.safe_run(); }
TEST(ML_KNearest, save_load) { CV_SLMLTest test( CV_KNEAREST ); test.safe_run(); }
TEST(ML_SVM, save_load) { CV_SLMLTest test( CV_SVM ); test.safe_run(); }
TEST(ML_SVM, save_load)
{
CV_SLMLTest test( CV_SVM );
test.safe_run();
}
TEST(ML_ANN, save_load) { CV_SLMLTest test( CV_ANN ); test.safe_run(); }
TEST(ML_DTree, save_load) { CV_SLMLTest test( CV_DTREE ); test.safe_run(); }
TEST(ML_Boost, save_load) { CV_SLMLTest test( CV_BOOST ); test.safe_run(); }
TEST(ML_RTrees, save_load) { CV_SLMLTest test( CV_RTREES ); test.safe_run(); }
TEST(DISABLED_ML_ERTrees, save_load) { CV_SLMLTest test( CV_ERTREES ); test.safe_run(); }
TEST(MV_SVMSGD, save_load){
CV_SLMLTest test( CV_SVMSGD );
test.safe_run();
}
class CV_LegacyTest : public cvtest::BaseTest
{
@@ -201,6 +209,8 @@ protected:
model = Algorithm::load<SVM>(filename);
else if (modelName == CV_RTREES)
model = Algorithm::load<RTrees>(filename);
else if (modelName == CV_SVMSGD)
model = Algorithm::load<SVMSGD>(filename);
if (!model)
{
code = cvtest::TS::FAIL_INVALID_TEST_DATA;
@@ -260,6 +270,11 @@ TEST(ML_DTree, legacy_load) { CV_LegacyTest test(CV_DTREE, "_abalone.xml;_mushro
TEST(ML_NBayes, legacy_load) { CV_LegacyTest test(CV_NBAYES, "_waveform.xml"); test.safe_run(); }
TEST(ML_SVM, legacy_load) { CV_LegacyTest test(CV_SVM, "_poletelecomm.xml;_waveform.xml"); test.safe_run(); }
TEST(ML_RTrees, legacy_load) { CV_LegacyTest test(CV_RTREES, "_waveform.xml"); test.safe_run(); }
TEST(ML_SVMSGD, legacy_load)
{
CV_LegacyTest test(CV_SVMSGD, "_waveform.xml");
test.safe_run();
}
/*TEST(ML_SVM, throw_exception_when_save_untrained_model)
{
+182
View File
@@ -0,0 +1,182 @@
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// Intel License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of Intel Corporation may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "test_precomp.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
using namespace cv::ml;
using cv::ml::SVMSGD;
using cv::ml::TrainData;
class CV_SVMSGDTrainTest : public cvtest::BaseTest
{
public:
CV_SVMSGDTrainTest(Mat _weights, float _shift);
private:
virtual void run( int start_from );
float decisionFunction(Mat sample, Mat weights, float shift);
cv::Ptr<TrainData> data;
cv::Mat testSamples;
cv::Mat testResponses;
static const int TEST_VALUE_LIMIT = 50;
};
CV_SVMSGDTrainTest::CV_SVMSGDTrainTest(Mat weights, float shift)
{
int datasize = 100000;
int varCount = weights.cols;
cv::Mat samples = cv::Mat::zeros( datasize, varCount, CV_32FC1 );
cv::Mat responses = cv::Mat::zeros( datasize, 1, CV_32FC1 );
cv::RNG rng(0);
float lowerLimit = -TEST_VALUE_LIMIT;
float upperLimit = TEST_VALUE_LIMIT;
rng.fill(samples, RNG::UNIFORM, lowerLimit, upperLimit);
for (int sampleIndex = 0; sampleIndex < datasize; sampleIndex++)
{
responses.at<float>( sampleIndex ) = decisionFunction(samples.row(sampleIndex), weights, shift) > 0 ? 1 : -1;
}
data = TrainData::create( samples, cv::ml::ROW_SAMPLE, responses );
int testSamplesCount = 100000;
testSamples.create(testSamplesCount, varCount, CV_32FC1);
rng.fill(testSamples, RNG::UNIFORM, lowerLimit, upperLimit);
testResponses.create(testSamplesCount, 1, CV_32FC1);
for (int i = 0 ; i < testSamplesCount; i++)
{
testResponses.at<float>(i) = decisionFunction(testSamples.row(i), weights, shift) > 0 ? 1 : -1;
}
}
void CV_SVMSGDTrainTest::run( int /*start_from*/ )
{
cv::Ptr<SVMSGD> svmsgd = SVMSGD::create();
svmsgd->setOptimalParameters(SVMSGD::ASGD);
svmsgd->train( data );
Mat responses;
svmsgd->predict(testSamples, responses);
int errCount = 0;
int testSamplesCount = testSamples.rows;
for (int i = 0; i < testSamplesCount; i++)
{
if (responses.at<float>(i) * testResponses.at<float>(i) < 0 )
errCount++;
}
float err = (float)errCount / testSamplesCount;
std::cout << "err " << err << std::endl;
if ( err > 0.01 )
{
ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
}
}
float CV_SVMSGDTrainTest::decisionFunction(Mat sample, Mat weights, float shift)
{
return sample.dot(weights) + shift;
}
TEST(ML_SVMSGD, train0)
{
int varCount = 2;
Mat weights;
weights.create(1, varCount, CV_32FC1);
weights.at<float>(0) = 1;
weights.at<float>(1) = 0;
float shift = 5;
CV_SVMSGDTrainTest test(weights, shift);
test.safe_run();
}
TEST(ML_SVMSGD, train1)
{
int varCount = 5;
Mat weights;
weights.create(1, varCount, CV_32FC1);
float lowerLimit = -1;
float upperLimit = 1;
cv::RNG rng(0);
rng.fill(weights, RNG::UNIFORM, lowerLimit, upperLimit);
float shift = rng.uniform(-5.f, 5.f);
CV_SVMSGDTrainTest test(weights, shift);
test.safe_run();
}
TEST(ML_SVMSGD, train2)
{
int varCount = 100;
Mat weights;
weights.create(1, varCount, CV_32FC1);
float lowerLimit = -1;
float upperLimit = 1;
cv::RNG rng(0);
rng.fill(weights, RNG::UNIFORM, lowerLimit, upperLimit);
float shift = rng.uniform(-1000.f, 1000.f);
CV_SVMSGDTrainTest test(weights, shift);
test.safe_run();
}