TrainHOG.exe svm trainAuto error

asked 2019-04-16 06:22:20 -0500

theorangeclockwork gravatar image

updated 2019-04-16 10:14:50 -0500

I'm using OpenCV 3.4.3 with C++ and i try to train my svm using TrainHOG.exe(you can find it on opencv docs). I've prepared dataset(combination of INRIA person dataset and a few more my own cropped images) but results of svm i get using params(like below) gives me too many false positives:

    Ptr< SVM > svm = SVM::create();
/* Default values to train SVM */
svm->setTermCriteria(TermCriteria(CV_TERMCRIT_ITER + CV_TERMCRIT_EPS, 1000, 1e-3));
svm->setP(0.1); // for EPSILON_SVR, epsilon in loss function?
svm->setC(0.01); // From paper, soft classifier
svm->setType(SVM::EPS_SVR); // C_SVC; // EPSILON_SVR; // may be also NU_SVR; // do regression task
svm->train(train_data, ROW_SAMPLE, labels);

So i've changed train to trainAuto and it gives me this error multiple times:

OpenCV(3.4.3) Error: Assertion failed (sv_count != 0) in cv::ml::SVMImpl::do_train, file C:\build\3_4_winpack-build-win64-vc14\opencv\modules\ml\src\svm.cpp, line 1404

image description

and then crashes with this error:

OpenCV(3.4.3) Error: Assertion failed (0 <= i && i < (int)decision_func.size()) in cv::ml::SVMImpl::getDecisionFunction, file C:\build\3_4_winpack-build-win64-vc14\opencv\modules\ml\src\svm.cpp, line 2033

image description

I've checked that lines in svm.cpp ( and the last assert giving me error is here:

double getDecisionFunction(int i, OutputArray _alpha, OutputArray _svidx ) const CV_OVERRIDE
    CV_Assert( 0 <= i && i < (int)decision_func.size());
    const DecisionFunc& df = decision_func[i];
    int count = getSVCount(i);
    Mat(1, count, CV_64F, (double*)&df_alpha[df.ofs]).copyTo(_alpha);
    Mat(1, count, CV_32S, (int*)&df_index[df.ofs]).copyTo(_svidx);
    return df.rho;

Since i = 0 (in TrainHog.exe):

vector< float > get_svm_detector(const Ptr< SVM >& svm){
// get the support vectors
Mat sv = svm->getSupportVectors();
const int sv_total = sv.rows;
// get the decision function
Mat alpha, svidx;
double rho = svm->getDecisionFunction(0, alpha, svidx);
CV_Assert( == 1 && == 1 && sv_total == 1);
CV_Assert((alpha.type() == CV_64F &&<double>(0) == 1.) ||
    (alpha.type() == CV_32F &&<float>(0) == 1.f));
CV_Assert(sv.type() == CV_32F);
vector< float > hog_detector(sv.cols + 1);
memcpy(&hog_detector[0], sv.ptr(), sv.cols * sizeof(hog_detector[0]));
hog_detector[sv.cols] = (float)-rho;
return hog_detector;


assertion fails due to second condition (i < (int)decision_func.size()), but i don't know what it means and still don't know what am i doing wrong.

Sorry for my bad eng and thanks in advance.

edit retag flag offensive close merge delete


please, no screeenshots of code or errors here. pleae give us a text version instead,which can be properly quoted, indexed,etc.

berak gravatar imageberak ( 2019-04-16 08:29:23 -0500 )edit

now fixed, sorry for that

theorangeclockwork gravatar imagetheorangeclockwork ( 2019-04-16 10:13:06 -0500 )edit

you need a LINEAR svm for this (default is RBF), else you cannot export a single (1d) support vector for the HOGDetector

berak gravatar imageberak ( 2019-04-16 11:49:48 -0500 )edit

but here: svm->setKernel(SVM::LINEAR); i've set a linear kernel and didn't change it

or i need set that property in somewhere else?

theorangeclockwork gravatar imagetheorangeclockwork ( 2019-04-16 13:04:39 -0500 )edit

hmm, it's unclear, wh at you're doing exactly, there. can yoou try to clean upp your question, and focus on the "trainAuto" part ?

also note, that a linear svm only uses C and coef0 params (all others are bogus)

berak gravatar imageberak ( 2019-04-17 01:30:50 -0500 )edit