Ask Your Question

CvSVM::EPS_SVR train_auto assertion sv_count != 0 failed

asked 2014-10-23 05:29:02 -0600

thomas gravatar image

updated 2014-10-23 06:32:50 -0600

I have the following code:

CvSVMParams params;
params.svm_type = CvSVM::EPS_SVR;
params.kernel_type = CvSVM::RBF;
params.term_crit = TermCriteria(CV_TERMCRIT_ITER, (int)1e7, 1e-6);

CvSVM svm;
svm.train_auto(_data, _resp, _var_idx, _train_idx, params);

Here _data and _resp are Mats holding the feature vectors and responses, _var_idx containing the active features and _train_idx the active samples. For the parameter grids the default values are used. Unfortunately, the code produces the following error:

OpenCV Error: Assertion failed (sv_count != 0) in do_train, file /home/.../opencv-2.4.9/modules/ml/src/svm.cpp, line 1346

When I run a regression one the same data with parameters selected by hand it works fine. And when I switch to a classification problem (and change the corresponding parameters and SVM type) it also works. In that case for single training as well as for auto training. Also, when switching to CvSVM::NU_SVR the auto training works fine.

Another thing that bothers me is that I also have to provide the parameter p (EPS_EVR) or nu (NU_SVR) when I want to do auto training. The documentation says that those are also estimated using their corresponding default grids. Why is that so?

Thanks in advance.


I've made a small example that suffers from this problem. Just in case anyone want to try it out and reproduce the problem:

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/ml/ml.hpp>

using namespace cv;
using namespace std;

int main() {

    Mat X(1000, 2, CV_32FC1);
    Mat Y(1000, 1, CV_32FC1);

    randu(X, -2, 2);

    for(int i = 0; i < 1000; i++){<float>(i,0) = pow(<float>(i,0),2) + pow(<float>(i,1),2) - 1;

    CvSVMParams params;
    params.svm_type = CvSVM::EPS_SVR;
    params.kernel_type = CvSVM::RBF;
    params.term_crit = TermCriteria(CV_TERMCRIT_ITER, (int)1e7, 1e-6);
    params.p = 0.1;

    CvSVM svm;
    svm.train_auto(X, Y, Mat::ones(1,2, CV_8U), Mat::ones(1,1000, CV_8U), params);

    return 0;
edit retag flag offensive close merge delete


I have also used train_auto to get the best SVM and I have found that all the combinations of EPS_SVR and every kernel type are giving the error of sv_count != 0 at some line, and more the NU_SVR with POLY kernel gives me the same problem with sv_count != 0 and another thing: the NU_SVC with SIGMOID kernel finishes the training but it has values of Inf, and the result of predict is nan. Can anyone help us? I have used OpenCV 2.4.9

thdrksdfthmn gravatar imagethdrksdfthmn ( 2014-10-30 04:28:21 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2014-10-30 10:50:03 -0600

ejehardenberg gravatar image

Using your sample code provided I don't get any issue at all, though I did change the maximum number of iterations down to 1000 so I could run it in a reasonable amount of time.

Looking at the source code near that assertion I spy the following:

if( !train1( sample_count, var_count, samples, svm_type == ONE_CLASS ? 0 : responses->data.i, 0, 0, temp_storage, alpha, df->rho ))

for( i = 0; i < sample_count; i++ )
    sv_count += fabs(alpha[i]) > 0;

CV_Assert(sv_count != 0);

So you either have 0 samples and the sv_count is never increasing (it is initialized to 0) or every alpha value is equal to 0 (since the use of fabs precludes negative values here)

edit flag offensive delete link more


For my case, I had 837 pos and 839 neg images and I have used term_iter = 1000 and term_eps = 0.01. So what could it be the problem? The alpha? What is it? How to fix it? It is true that I have labelled the negative class with -1, but I do not think that this could be the problem...

thdrksdfthmn gravatar imagethdrksdfthmn ( 2014-10-30 11:36:55 -0600 )edit

The other thing it could possibly be, is that the subset of training samples within that fold (for the k-fold) has no samples in it. If you look at line 1982 in the modules/ml/src/svm.cpp file you'll see where that comes in. Unfortunately, I'm not entirely sure exactly how the alpha value is calculated (it is done by the solver for the algorithm type from what I understand), but it's more likely the k-fold sample is the problem I think.

ejehardenberg gravatar imageejehardenberg ( 2014-10-30 12:02:27 -0600 )edit

I do not know what file are you looking in, but here is some code of svm.cpp and at line 1346, there is an CV_Assert(sv_count != 0); that is generating the problem. sv_count is computed in the for loop just before the assert. It depends on the fabs(alpha[i]) parameter; if alpha is negative fabs will be 0, maybe that is the problem, but what is wrong? Alpha is somehow dependent on the storage (see line 1795)... ? I did not really got it... why?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2014-10-31 04:57:39 -0600 )edit

Question Tools


Asked: 2014-10-23 05:29:02 -0600

Seen: 2,492 times

Last updated: Oct 30 '14