Ask Your Question
0

Confidence value for svm model

asked 2016-12-02 03:44:02 -0600

Akash Garg gravatar image

I am using SVM algorithm to recognize Facial Expression using opencv library. I want to get prediction confidence of my test image but the syntax of predict is something like float response = svm->predict(TestDataMat). How can get the confidence value?? Thanks in advance!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
5

answered 2016-12-02 03:51:03 -0600

updated 2016-12-20 08:18:16 -0600

Add the second parameter to return the prediction value

decision = svm.predict(testData, true);
confidence = 1.0 / (1.0 + exp(-decision));

The distance itself is pretty ok, but if you want to have a confidence value in a range (0, 1), you can apply a sigmoidal function to the result, which could be a logistic function.

ABOVE WORKS FOR 2.4


THIS WORKS FOR 3.x

svm->predict(data_test, labels_SVM, ml::StatModel::RAW_OUTPUT);

edit flag offensive delete link more

Comments

1

Thanks a lot!

Akash Garg gravatar imageAkash Garg ( 2016-12-02 09:25:27 -0600 )edit

You are welcome!

StevenPuttemans gravatar imageStevenPuttemans ( 2016-12-05 02:51:34 -0600 )edit

Does this work in OpenCV 3.x? The StatModel docs suggest that the signature of predict() has changed, and I can't find whether it's still possible to get the confidence for SVM.

Joseph Howse gravatar imageJoseph Howse ( 2016-12-20 06:49:33 -0600 )edit

Actually, you might be on to something ... just tried float conf = svm.predict(data_test, labels_SVM, true); cerr << conf << endl; and the value is always 0 ... because the function with parameters described above is indeed missing ...

StevenPuttemans gravatar imageStevenPuttemans ( 2016-12-20 07:26:49 -0600 )edit
1

Just added a solution for 3.x ;)

StevenPuttemans gravatar imageStevenPuttemans ( 2016-12-20 08:18:44 -0600 )edit
1

Thanks, you are correct. Also, it's worth noting that the distance is only returned in the case of 2-class classification, and only when the SVM type is C_SVC or NU_SVC. (This limitation is the same in 2.4 and 3.x.) Otherwise, just the classification is returned.

Joseph Howse gravatar imageJoseph Howse ( 2016-12-20 10:53:04 -0600 )edit

Hi, do you have an Idea of how to get the probability of a sample belonging to each class? A bit like (confidence_i for all i in classes) ? Thank you in advance :)

DRomeroG gravatar imageDRomeroG ( 2018-05-11 03:37:18 -0600 )edit

@berak might be able to hop in here, been some time since I worked on this

StevenPuttemans gravatar imageStevenPuttemans ( 2018-05-14 09:21:07 -0600 )edit
1

only idea i have here is: instead of a multi-class SVM, use N one-against-all-others svm's

berak gravatar imageberak ( 2018-05-14 09:31:50 -0600 )edit

Question Tools

3 followers

Stats

Asked: 2016-12-02 03:44:02 -0600

Seen: 2,703 times

Last updated: Dec 20 '16