SVM parameter optimization in Opencv

╄→尐↘猪︶ㄣ 提交于 2019-12-18 16:11:25

问题


I want to optimize SVM parameters in Opencv. But, every time I use train_auto I get C=1 and gamma=1. Some people use LibSVM but I could not write a wrapper for that. Both trainingData and labels are taken from an existing code which gives good results so I am trying to get the same parameters for that code with train_auto. In the original code C=312.5 and gamma=0.50625. I saw that somebody used CvStatModel for python, is it necessary for C++? Where do I make a mistake? Thanks in advance.

The Code:

CvParamGrid CvParamGrid_C(pow(2.0,-5), pow(2.0,15), pow(2.0,2));
CvParamGrid CvParamGrid_gamma(pow(2.0,-15), pow(2.0,3), pow(2.0,2));
if (!CvParamGrid_C.check() || !CvParamGrid_gamma.check())
    cout<<"The grid is NOT VALID."<<endl;
CvSVMParams paramz;
paramz.kernel_type = CvSVM::RBF;
paramz.svm_type = CvSVM::C_SVC;
paramz.term_crit = cvTermCriteria(CV_TERMCRIT_ITER,100,0.000001);
svm.train_auto(trainingData, labels, Mat(), Mat(), paramz,10, CvParamGrid_C, CvParamGrid_gamma, CvSVM::get_default_grid(CvSVM::P), CvSVM::get_default_grid(CvSVM::NU), CvSVM::get_default_grid(CvSVM::COEF), CvSVM::get_default_grid(CvSVM::DEGREE), true);
svm.get_params();
cout<<"gamma:"<<paramz.gamma<<endl;
cout<<"C:"<<paramz.C<<endl;

回答1:


I modified the code as follows paramz = svm.get_params() and it worked fine.



来源:https://stackoverflow.com/questions/21918747/svm-parameter-optimization-in-opencv

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