Ask Your Question

SVM predict multiclass

asked 2016-07-20 22:47:56 -0600

trops gravatar image

Im curious about how to go about using the predict method in SVM for a multi class object detector using BoF and SIFT.

Ive trained the svm with 8 classes, and when I call predict it determines the class label. Thats all well and good but I am trying to determine the probability. I am reading video frames and each frame is so closely related that the label alone will not help.

I need to figure out how to calculate the probability so that I can choose the closest match possible after reading all of the frames.

I am looking into getDecisionFunction but the result is always the same (0.986196) for each frame in the video that I am using the SVM to predict.

Is there a step that I need to take in order to fetch the probability of the prediction?

I am using openCV 3.1 if this helps.

Thanks, John

edit retag flag offensive close merge delete


Afaik, there is no out-of-box way to get multiclass probability in OpenCV (it can be retrieved in a 2-class classification problem though)

LorenaGdL gravatar imageLorenaGdL ( 2016-07-21 01:29:33 -0600 )edit

Yeah, that is what I thought too, but then I found this post which got me thinking that it is. I could not find this line of code in the opencv 3.1 file mentioned but I DID find getDecisionFunction method. Im just not sure how to use it, its a bit over my head.

Im trying to detect multiple positions of one human in a video. Someone told me a multi class SVM is the way to go, and I have the code working, just doesn't give this method is moot if I can't get that. When I run the video it will classify the first 100 frames as "1" the next 100 as "2", and so forth. I just need the BEST match for each class that I train in the SVM.

trops gravatar imagetrops ( 2016-07-21 10:21:05 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-07-22 15:35:06 -0600

LorenaGdL gravatar image

OK, let me give you some insights and useful links, but the answer remains the same: you can't get SVM multiclass probability with a pure combination of OpenCV functions and minimum effort.

OpenCV SVM (based on LibSVM) implements multi-class classification in the form of one-vs-one problems, i.e. each class is compared with each other class, so there is N(N-1)/2 decision functions in an N-class problem. Each comparison has a winner class, and such class receives one vote to be the final best match (so yeah, you obviously get the best match). This is coded in svm.cpp, lines 1886-1906:

                    for( i = dfi = 0; i < class_count; i++ )
                        for( j = i+1; j < class_count; j++, dfi++ )
                            const DecisionFunc& df = svm->decision_func[dfi];
                            sum = -df.rho;
                            int sv_count = svm->getSVCount(dfi);
                            const double* alpha = &svm->df_alpha[df.ofs];
                            const int* sv_index = &svm->df_index[df.ofs];
                            for( k = 0; k < sv_count; k++ )
                                sum += alpha[k]*buffer[sv_index[k]];

                            vote[sum > 0 ? i : j]++;

                    for( i = 1, k = 0; i < class_count; i++ )
                        if( vote[i] > vote[k] )
                            k = i;

Regarding probabilities, they're not so easy to compute. In fact, even for 2-class classification problems, no probability is outputted in OpenCV. Instead, we can just get the distance of the sample to the SVM separating hyperplane, which gives a rough indicator of the correctness of the prediction (the larger the distance, the larger the confidence). There are several methods to compute probabilities, and you can read about them in the LibSVM manual, Section 8.

The getDecisionFunction() indeed gives you all necesarry information to predict yourself the output value, but in any case you would have to implement a probability estimator, which I doubt is your goal here.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2016-07-20 22:47:56 -0600

Seen: 4,280 times

Last updated: Jul 22 '16