本文介绍了如何在C ++中移植MATLAB libSVM参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我使用libSVM的MATLAB交叉验证中,我发现这些是最好的参数使用:

In my cross-validation in MATLAB with libSVM I found that these are the best parameters to use:

model = svmtrain( labels, training, '-s 0 -t 2 -c 10000 -g 100');

现在我想使用OpenCV复制C ++中的分类。

Now I want to replicate the classification in C++ with OpenCV.

但我不明白如何设置C ++参数与MATLAB相同:

But I do not understand how to set the C++ parameters to be the same as MATLAB:

基于我尝试了以下操作:

Based on this documentation I tried the following:

CvSVMParams params;
params.svm_type    = CvSVM::C_SVC;
params.kernel_type = CvSVM::RBF;
//params.term_crit   = cvTermCriteria(CV_TERMCRIT_ITER, 10000, 1e-6);
params.Cvalue = 10000;
params.gamma = 100;
CvSVM SVM;
SVM.train(train, labels, Mat(), Mat(), params);

但我遇到此错误:

error: no member named 'Cvalue' in 'CvSVMParams' params.Cvalue = 10000;

最后,我应该取消注释

 //params.term_crit   = cvTermCriteria(CV_TERMCRIT_ITER, 10000, 1e-6);

并尝试其他值或者它不重要吗?
因为我在MATLAB中甚至不能理解如何设置相同的参数。

and try other values or is it not important?Because I can't even understand in MATLAB how to set the same parameters.

推荐答案

当从Matlab中的LibSVM移植到OpenCV SVM时,精确等效。注意,opencv的SVM可能会有一些错误,具体取决于您使用的版本(不是最新版本的问题)。

Not every parameter has an exact equivalent when porting from LibSVM in matlab to OpenCV SVM. The term criteria is one of them. (not an issue with the latest version).

您应该取消注释行,以更好地控制终止条件。这行说,算法应该在执行10000次迭代时结束。如果使用CV_TERMCRIT_EPS,当每个向量达到小于指定精度(对于它,它的1e-6)的精度时,它将停止。使用两个停止条件,并且当其中任何一个完成时它将停止。

You should un-comment the line, to have better control of your termination criteria. This line says that the algorithm should end when 10000 iterations are performed. If you use CV_TERMCRIT_EPS, it will stop when a precision less than the specified (for you, its 1e-6) is achieved for each vector. Use both stopping criteria, and it will stop when either of them completes.

或者,也可以通过将其链接为库来尝试使用LibSVM作为C ++。这将为您提供在matlab中使用的完全相同算法和函数。

Alternatively, could also try using LibSVM for C++, by linking it as a library. This will give you the exact same algorithms and functions that you are using in matlab.

这篇关于如何在C ++中移植MATLAB libSVM参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 07:51