5

I'm working on a project where I'm doing multiclass classification with SVM in OpenCV.

My goal is to get the confidence score of the classification as well as the predicted class. How can I do that? Right now I'm doing something like

float result = mysvm.predict(sample);

Having a fairly high amount of classes I prefer to avoid doing a lot of one-vs-all classifications and then calculate the scores.

Since OpenCV SVM is implemented using LibSVM, I'm quite sure that there is a way to do this, but looking at http://docs.opencv.org/modules/ml/doc/support_vector_machines.html doesn't really help.

Thanks for any input provided.

1 Answer 1

3

In opencv/include/opencv2/ml/ml.hpp, there is a struct called CvSVMDecisionFunc.. It has been used in line 546 as a Protected Variable,

CvSVMDecisionFunc* decision_func;

What you need to do is to cut that line and paste it as Public and then do a complete rebuild of OpenCV.. This variable, decision_func contains all the data for specific support vectors (ie, the alpha and rho values)..

4
  • That looks a pretty drastic approach, I'll do it if I can't really find a different way to solve my problem. I've seen that bool returnDFVal=false in the predict() funcion does what I need but only for binary classification. No way to get it for multiclass classification?
    – powder
    Nov 6, 2013 at 15:14
  • 2
    Ok, since I lost enough time trying to figure out a better way to do this, I have done as suggested and rebuilt my OpenCV setting the decision function struct to public. I'm having some difficulties understanding its values though. I have - rho: -0.9667... - sv_count: 1 - alpha: {1.00...} - sv_index: 0 Not quite what I expected..shouldn't I have a number of support vector that is the number of the classes I am using for classification?
    – powder
    Nov 6, 2013 at 16:09
  • Well, I don't know how to use returnDFVal for multiclass problem. Regarding the sv_count, you are right; it should be in accordance with the number of classes (in this case, the labels you provide during the SVM training phase).. In general, I try to use -1.0 for negative samples and single point positive floats for the rest..
    – scap3y
    Nov 6, 2013 at 17:39
  • 2
    @powder You can avoid rebuilding OpenCV. Since decision_func is protected, you can extend CvSVM class and access that variable from the extended class. This way is better.
    – nimcap
    Sep 10, 2015 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.