How to port the MATLAB libSVM parameters in C++

耗尽温柔 提交于 2019-12-24 05:24:28

问题


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');

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

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);

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.


回答1:


Not every parameter has an exact equivalent when porting from LibSVM in matlab to 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).

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.

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.



来源:https://stackoverflow.com/questions/28304569/how-to-port-the-matlab-libsvm-parameters-in-c

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