OpenCV Linear SVM does not train

I've been stuck on this for a while. The OpenCV SVM implementation doesn't seem to work for a linear kernel. I'm pretty sure there are no errors in the code: when I change kernel_type

to RBF or POLY while keeping everything else as is, it works.

The reason why I say it doesn't work is I save the generated model and validate it. It shows the number of support vectors as 1. This does not apply to RBF or POLYnomial kernels.

There is nothing special about the code: I used to use the OpenCV SVM implementation but have never been a linear kernel. I tried setting degree

to 1 in POLY core and it results in the same model. It makes me think that something is wrong here.

Code structure, if required:

Mat trainingdata;    //acquire from files. done and correct.
Mat testingdata;     //acquire from files. done and correct again.
Mat labels;          //corresponding labels. checked and correct.

SVM my_svm;
SVMParams my_params;
my_params.svm_type = SVM::C_SVC;
my_params.kernel_type = SVM::LINEAR;    //or poly, with my_params.degree = 1.
my_param.C = 0.02;    //doesn't matter if I set it to 20000, makes no difference.

my_svm.train( trainingdata, labels, Mat(), Mat(), my_params );
//train_auto(..) function with 10-fold cross-validation takes the same time as above (~2sec)!
Mat responses;
my_svm.predict( testingdata, responses );
//responses matrix is all wrong.

      

I have 500 samples from one class and 600 from another class to test, and I get the correct classifications: 1/500 and 597/600.

Craziest part: I did the same experiment with the same data in the libSVM MATLAB wrapper and it works. Just trying to make an OpenCV version.

+3


source to share


1 answer


It is not a bug when you always only get one support vector with linear CvSVM .

OpenCV optimizes linear SVM to one support vector.
The idea here is that the support vectors define the stock, and the actual classification only needs the dividing hyperplane and can only be defined by one vector.



The C parameter doesn't matter if your training data is linearly separable. Perhaps this is your case.

+2


source







All Articles