我正在尝试使用从图像中获取的 SIFT 描述符来训练 SVM。然后我想以 .xml
格式保存 SVM,以便我可以再次加载它。
我的结构:我有 10 个类(class),每个类(class)有 100 个样本。
问题:如果我为每个类使用 10-50 个样本,那么 SVM 将被保存,我可以在我的文件夹中看到一个 classifer.xml
文件。但是如果我想使用更多的样本,例如每个类大约 100 个样本,那么 SVM 不会被保存。
我以为它可能需要一些时间才能保存,但我已经等了这么久(而且我已经等了好几次)。
我的 SVM 训练代码如下:
void svm::svmTrain()
{
cv::Mat trainme; // it should contain the feature vectors
cv::Mat labels; // it will contain the class labels
createTrainingDateUsingBOW( trainme, labels);
//svm parameters
CvTermCriteria criteria = cvTermCriteria(CV_TERMCRIT_EPS, 1000, FLT_EPSILON);
CvSVMParams svm_params = CvSVMParams (CvSVM::C_SVC, CvSVM::POLY, 10.0 , 8.0 , 1.0 , 10.0 , 0.5 , 0.1 , NULL , criteria); //CvSVMParams --it is a struct
//( svm_type, kernel_type, degree , gamma , coef0 , Cvalue, nu , p , class_weights, term_crit)
cout<<"\n saving SVM \n";
cv::SVM svm;
svm.train(trainme, labels, cv::Mat(), cv::Mat(), svm_params);
svm.save("classifier.xml");
cout<<"\n SVM classifier is saved.";
}
PS: 因此,如果我的样本每类超过 40-60 个,那么我可以从上面的代码中达到 saving SVM
但从未达到保存到 SVM 分类器。
最佳答案
试试这个替换,你会发现,它训练的时间太长了,训练完后,保存文件几乎不需要一分钟。
cout<<"\n training SVM \n";
cv::SVM svm;
svm.train(trainme, labels, cv::Mat(), cv::Mat(), svm_params);
cout<<"\n saving SVM \n";
svm.save("classifier.xml");
cout<<"\n SVM classifier is saved.";
我从未亲 body 验过 SVM,但它有多达 1000 个样本,训练不到一个小时。就我而言,当我尝试为样本数量相似的渔民做事时,花了 2-3 个小时以上。
关于c++ - SVM 分类器未保存在 ".xml"中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21991639/