我致力于训练自己的SVM。首先,我要训练它检测人并与opencv中实现的SVM比较结果。训练完成后,SVM看起来不错(我创建了xml文件进行检查),但是我无法将svm检测器设置为HOG。
这是我训练和保存我的svm的方法:
std::cout << "Entering trainAndSave" << std::endl;
cv::Ptr<cv::ml::SVM> svm = cv::ml::SVM::create();
svm->setType(cv::ml::SVM::C_SVC);
svm->setKernel(cv::ml::SVM::RBF);
std::cout << "Creating SVM pointer successfull" << std::endl;
cv::Ptr<cv::ml::TrainData> tData = cv::ml::TrainData::create(trainingData, cv::ml::SampleTypes::ROW_SAMPLE, labels);
std::clog << "Wait until training will get finish! This may take a few minutes...";
svm->trainAuto(tData);
std::clog << "...[done]" << std::endl;
std::cout << "trainAuto successfull" << std::endl;
svm->save(filename);
std::cout << "saving to file successfull" << std::endl;
我认为操作正确,问题在于如何获取支持向量并将smv检测器设置为HOG:
cv::Ptr<cv::ml::SVM> svm = cv::ml::SVM::load(filename);
// get support vectors
cv::Mat supportVectors = svm->getSupportVectors();
const int supportVectorsTotal = supportVectors.rows;
// get the decision function
cv::Mat alpha, sVidx;
double rho = svm->getDecisionFunction(0, alpha, sVidx);
CV_Assert(alpha.total() == 1 && sVidx.total() == 1 && supportVectorsTotal == 1);
CV_Assert((alpha.type() == CV_64F && alpha.at<double>(0) == 1.) ||
(alpha.type() == CV_32F && alpha.at<float>(0) == 1.f));
CV_Assert(supportVectors.type() == CV_32F);
std::vector<float> svmDetector(supportVectors.cols + 1);
memcpy(&svmDetector[0], supportVectors.ptr(), supportVectors.cols * sizeof(svmDetector[0]));
svmDetector[supportVectors.cols] = (float)-rho;
hog.setSVMDetector(svmDetector);
在加载svm之后,我在此行得到错误:
CV_Assert(alpha.total() == 1 && sVidx.total() == 1 && supportVectorsTotal == 1);
我真的不知道问题出在哪里。我将非常感谢所有建议。
答案 0 :(得分:0)
此错误是因为,您在azcopy.exe login --tenant-id=fa89xxxxxxxx
之前拥有HOG检测器实例,并且具有与计算HOG描述符以创建svm分类器相同的参数。
例如,输入参数值
setSVMDetector
然后,再次运行。