Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OpenCV autoTrain() with EPS_SVR not working

I am trying to train a svm in OpenCV using the introduction_to_svm.cpp example.

int labels[4] = {1, -1, -1, -1};
float trainingData[4][2] = { {501, 10}, {255, 10}, {501, 255}, {10, 501} };
Mat trainingDataMat(4, 2, CV_32FC1, trainingData);
Mat labelsMat(4, 1, CV_32SC1, labels);

using the default training:

Ptr<SVM> svm = SVM::create();
svm->setType(SVM::C_SVC);
svm->setKernel(SVM::LINEAR);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));
svm->train(trainingDataMat, ROW_SAMPLE, labelsMat);

works fine and using autoTrain with the same type:

Ptr<SVM> svm = SVM::create();
svm->setType(SVM::C_SVC);
svm->setKernel(SVM::LINEAR);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));
cv::Ptr<cv::ml::TrainData> trainDataPtr = cv::ml::TrainData::create(trainingDataMat, cv::ml::ROW_SAMPLE, labelsMat);
svm->trainAuto(trainDataPtr);

also works. But when I try to use a different type of SVM:

    Ptr<SVM> svm = SVM::create();
    svm->setP( 0.1 );
    svm->setType( cv::ml::SVM::EPS_SVR );
    svm->setKernel(SVM::LINEAR);
    svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));
    cv::Ptr<cv::ml::TrainData> trainDataPtr = cv::ml::TrainData::create(trainingDataMat, cv::ml::ROW_SAMPLE, labelsMat);
    svm->trainAuto(trainDataPtr);
    // this didn't work either:
//    svm->trainAuto(
//              trainDataPtr,
//              10,
//              cv::ml::SVM::getDefaultGrid(cv::ml::SVM::C),
//              cv::ml::SVM::getDefaultGrid(cv::ml::SVM::GAMMA),
//              cv::ml::SVM::getDefaultGrid(cv::ml::SVM::P),
//              cv::ml::SVM::getDefaultGrid(cv::ml::SVM::NU),
//              cv::ml::SVM::getDefaultGrid(cv::ml::SVM::COEF),
//              cv::ml::SVM::getDefaultGrid(cv::ml::SVM::DEGREE),
//              true
//          );

I get the following error at runtime:

OpenCV Error: Assertion failed (sv_count != 0) in do_train, file /home/sarah/Software/OpenCV3_1/opencv/modules/ml/src/svm.cpp, line 1393 terminate called after throwing an instance of 'cv::Exception' what(): /home/sarah/Software/OpenCV3_1/opencv/modules/ml/src/svm.cpp:1393: error: (-215) sv_count != 0 in function do_train

Aborted (core dumped)

I actually want to use this to train a SVM as HOG detector for the INRIA dataset and see if I can get better results than with the preset values.