我已经训练了 20 个类的 SVM,现在对于一个新图像,我想首先将其分类为 20 个训练类之一,然后显示分类所依据的置信度得分。
问题:当我测试 1 类的一些图像时,它们都返回相同的置信度分数。我知道每个图像应该有不同的分数,并据以进行分类。所以我知道我的代码有问题。
我什至将“opencv”框架上的实例变量float *decision_function从protected更改为public并重建它,但问题仍然没有解决。
我想计算正确的分数,因为我想稍后根据置信度分数对最匹配我的查询图像的图像进行排名。
请告诉我为什么我在同一类中的每个图像都获得相同的置信度分数,以及我在代码中哪里犯了错误。
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased");
Ptr<DescriptorExtractor> extractor = new SurfDescriptorExtractor();
SurfFeatureDetector detector(500);// hemessian threshold
int dictionarySize = 100; //number of clusters=100
TermCriteria tc(CV_TERMCRIT_ITER, 10, 0.001);
int retries = 1;
int flags = KMEANS_PP_CENTERS;
BOWKMeansTrainer bowTrainer(dictionarySize, tc, retries, flags);
BOWImgDescriptorExtractor bowDE(extractor, matcher);
int main( int argc, char** argv )
{
int i,j;
Mat dictionary; // creating a dictionary
FileStorage fs("dictionary_new.yml", FileStorage::READ);
fs ["vocabulary"]>> dictionary;
fs.release();
bowDE.setVocabulary(dictionary);
Mat labels(0, 1, CV_32FC1); //to store labels of images
Mat trainingData(0, dictionarySize, CV_32FC1);
int k=0;
CvSVMParams params;
params.kernel_type=CvSVM::RBF;
params.svm_type=CvSVM::C_SVC;
params.gamma=0.50625000000000009;
params.C=312.50000000000000;
params.term_crit=cvTermCriteria(CV_TERMCRIT_ITER,100,0.000001);
CvSVM svm;
svm.load("svm_new.yml");
cout<<"Processing evaluation data..."<<endl;
string tags[] = {"none","Plane","Bike","Face","Car","Bag","binocular","Gloves","Bread_Maker","Revolver","Ring","Guitar","Elephant","boat","French_horn","Gorilla","Headphone","Flower","Penguin","Tiger","Laptop"};
Mat evalData(0, dictionarySize, CV_32FC1);
k=0;
vector<KeyPoint> keypoint2;
Mat bowDescriptor2;
Mat img2; //to load an image
img2 = imread("3 (14).jpg");
detector.detect(img2, keypoint2);
bowDE.compute(img2, keypoint2, bowDescriptor2);
evalData.push_back(bowDescriptor2);
float response = svm.predict(bowDescriptor2, true);
float confidence=0;
int f_to_i=(int)response;
confidence=1.0/(1.0+exp(-response));
cout<<"\n\nIt's a "<<tags[f_to_i]<<" and its confidence: "<<confidence;
_getch();
return 0;
}
计算分数:
1 (1).jpg,Plane,0.731059
1 (2).jpg,Plane,0.731059
1 (3).jpg,Plane,0.731059
1 (4).jpg,Plane,0.731059
1 (5).jpg,Plane,0.731059
1 (6).jpg,Plane,0.731059
1 (7).jpg,Plane,0.731059
1 (8).jpg,Plane,0.731059
1 (9).jpg,Plane,0.731059
1 (10).jpg,Plane,0.731059
1 (11).jpg,Plane,0.731059
1 (12).jpg,Plane,0.731059
1 (13).jpg,Plane,0.731059
1 (14).jpg,Plane,0.731059
1 (15).jpg,Plane,0.731059
1 (16).jpg,Plane,0.731059
1 (17).jpg,Plane,0.731059
1 (18).jpg,Plane,0.731059
1 (19).jpg,Plane,0.731059
1 (20).jpg,Plane,0.731059
最佳答案
我也遇到了类似的问题。问题是,当您有超过 2 个类时,您无法使用 svm.predict(bowDescriptor2, true) 获得响应。如果问题是多类 OpenCv SVM 将仅返回类标签号。这就是为什么你们有相同的值(value)观。
查看文档:http://docs.opencv.org/modules/ml/doc/support_vector_machines.html#cvsvm-predict
“returnDFVal – 指定返回值的类型。如果为 true 并且问题是 2 类分类,则该方法返回决策函数值,该值是到边距的有符号距离,否则函数返回类标签(分类)或估计函数值(回归)。”
一个可能的解决方案是修改 float CvSVM::predict(const float* row_sample, int row_len, bool returnDFVal) 方法。您可以在此处进行更改以获取每个类的 DecisionFunctionValues。
看部分代码: ...
for (i = 0; i < class_count; i++)
{
for (j = i + 1; j < class_count; j++, df++)
{
sum = -df->rho;
int sv_count = df->sv_count;
for (k = 0; k < sv_count; k++)
sum += df->alpha[k] * buffer[df->sv_index[k]];
vote[sum > 0 ? i : j]++;
}
}
for (i = 1, k = 0; i < class_count; i++)
{
if (vote[i] > vote[k])
k = i;
}
result = returnDFVal && class_count == 2 ? (float)sum : (float)(class_labels->data.i[k]);
关于c++ - OpenCV C++ 的 SVM 对同一类图像返回相同的置信度分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30164899/