简体   繁体   English

如何在 C++ 中移植 MATLAB libSVM 参数

[英]How to port the MATLAB libSVM parameters in C++

In my cross-validation in MATLAB with libSVM I found that these are the best parameters to use:在我使用 libSVM 在 MATLAB 中的交叉验证中,我发现这些是最好的参数:

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

Now I want to replicate the classification in C++ with OpenCV.现在我想用 OpenCV 复制 C++ 中的分类。

But I do not understand how to set the C++ parameters to be the same as MATLAB:但我不明白如何将 C++ 参数设置为与 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);

but I get this error:但我收到此错误:

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

Last thing, should I uncomment最后一件事,我应该取消注释吗

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

and try other values or is it not important?并尝试其他值还是不重要? Because I can't even understand in MATLAB how to set the same parameters.因为我什至无法在 MATLAB 中理解如何设置相同的参数。

Not every parameter has an exact equivalent when porting from LibSVM in matlab to OpenCV SVM.从 matlab 中的 LibSVM 移植到 OpenCV SVM 时,并非每个参数都具有完全等效的参数。 The term criteria is one of them.术语标准就是其中之一。 Keep in mind that the SVM of opencv might have some bugs depending on the version you use (not an issue with the latest version).请记住,根据您使用的版本(不是最新版本的问题) ,opencv 的 SVM 可能存在一些错误

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.这一行表示算法应该在执行 10000 次迭代时结束。 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.如果您使用 CV_TERMCRIT_EPS,当每个向量达到小于指定的精度(对您来说,它的 1e-6)时,它将停止。 Use both stopping criteria, and it will stop when either of them completes.使用两个停止条件,当它们中的任何一个完成时它就会停止。

Alternatively, could also try using LibSVM for C++, by linking it as a library.或者,也可以尝试将 LibSVM 用于 C++,将其链接为库。 This will give you the exact same algorithms and functions that you are using in matlab.这将为您提供与您在 matlab 中使用的完全相同的算法和函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM