OPencv SVM predict probability

别说谁变了你拦得住时间么 提交于 2021-02-16 10:30:22

问题


I am developing a image-classification project using BOW model and SVM. I want to find out the SVMs predict probability but there is no such function in opencv svm. Is there any way to do this? I want to find out the predict probability in n-class SVM.


回答1:


No you can't do this with CvSVM. OpenCV's SVM implementation is based on a very old version of libsvm. Download the latest version of libsvm and use it instead. Of course you will have to write a wrapper to convert data formats. See http://www.csie.ntu.edu.tw/~cjlin/libsvm/




回答2:


As suggest my @Bull, prediction probabilities are not implemented in OpenCV. But there great way to access underhood libsvm to get that. Details on the blog and the code snippet is following:

Note: this function loads the model and hence do not load it externally.

#include "svm.h"
...
void predict(string modelPath, Mat& hist) {

    const char *MODEL_FILE = modelPath.c_str();
    if ((this->SVMModel = svm_load_model(MODEL_FILE)) == 0) {
        this->modelLoaded = false;
        fprintf(stderr, "Can't load SVM model %s", MODEL_FILE);
        return;
    }

    struct svm_node *svmVec;
    svmVec = (struct svm_node *)malloc((hist.cols+1)*sizeof(struct svm_node));
    int j;
    for (j = 0; j < hist.cols; j++) {
        svmVec[j].index = j+1;
        svmVec[j].value = hist.at<float>(0, j);
    }
    svmVec[j].index = -1; // this is quite essential. No documentation.

    double scores[8]; // suppose there are eight classes
    if(svm_check_probability_model(SVMModel)) {
        svm_predict_probability(SVMModel, svmVec, scores);
    }
}



回答3:


You could try generating a confusion matrix, this should tell you the probability of each image belonging to any of the classes. Confusion Matrix

And here you have a snippet I found, though it is incomplete it may give you some ideas:

map<string,map<string,int> > confusion_matrix; // confusionMatrix[classA][classB] =   number_of_times_A_voted_for_B;
map<string,CvSVM> classes_classifiers; //This we created earlier

vector<string> files; //load up with images
vector<string> classes; //load up with the respective classes

for(..loop over a directory?..) {
Mat img = imread(files[i]),resposne_hist;

vector<KeyPoint> keypoints;
detector->detect(img,keypoints);
bowide->compute(img, keypoints, response_hist);

float minf = FLT_MAX; string minclass;
for (map<string,CvSVM>::iterator it = classes_classifiers.begin(); it !=       classes_classifiers.end(); ++it) {
  float res = (*it).second.predict(response_hist,true);
  if (res < minf) {
     minf = res;
     minclass = (*it).first;
  }
}
confusion_matrix[minclass][classes[i]]++;  
}

I didn't test it yet, so in the case you get to make it work I would appreciate that you communicate it here :)

source: a-simple-object-classifier-with-bag-of-words



来源:https://stackoverflow.com/questions/16782979/opencv-svm-predict-probability

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!