8

我正在使用 BOW 模型和 SVM 开发图像分类项目。我想找出 SVM 预测概率,但 opencv svm 中没有这样的功能。有没有办法做到这一点?我想找出 n 类 SVM 中的预测概率。

4

3 回答 3

3

不,你不能用 CvSVM 做到这一点。OpenCV 的 SVM 实现基于非常旧的 libsvm 版本。下载最新版本的 libsvm 并改用它。当然,您必须编写一个包装器来转换数据格式。见http://www.csie.ntu.edu.tw/~cjlin/libsvm/

于 2013-05-28T04:26:10.473 回答
0

正如我的@Bull 所建议的那样,OpenCV 中没有实现预测概率。但是有很好的方法可以访问引擎盖libsvm来获得它。博客上的详细信息和代码片段如下:

注意:此函数会加载模型,因此不会从外部加载它。

#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);
    }
}
于 2020-07-18T14:20:43.003 回答
-1

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

于 2013-06-29T13:24:57.593 回答