I am doing English digit classification using SVM classifier in opencv.
I am able to predict the classes using predict()
function.
But I want get confidence of prediction between 0-1. Can somebody provide a method to do it using opencv
//svm parameters used
m_params.svm_type = CvSVM::C_SVC;
m_params.kernel_type = CvSVM::RBF;
m_params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 500, 1e-8);
//for training
svmob.train_auto(m_features, m_labels, cv::Mat(), cv::Mat(), m_params, 10);
//for prediction
predicted = svmob.predict(testData);
SVM during training tries to find a separating hyperplane such that trainset examples lay on different sides. There could be many such hyperplanes (or none), so to select the "best" we look for the one for which total distance from all classes are maximized. Indeed, the further away from the hyperplane point is located — the more confident we are in the decision. So what we are interested in is distance to the hyperplane.
As per OpenCV documentation, CvSVM::predict
has a default second arguments which specifies what to return. By default, it returns classification label, but you can pass in true
and it'll return the distance.
The distance itself is pretty ok, but if you want to have a confidence value in a range (0, 1), you can apply sigmoidal function to the result. One of such functions if logistic function.
decision = svmob.predict(testData, true);
confidence = 1.0 / (1.0 + exp(-decision));