我需要形成HOGDescriptor::setSVMDetector()
的输入。
我使用openCV计算描述符,然后使用libSVM获取模型文件。为了形成输入,我知道我需要获取支持向量的值,并与alphas进行逐元素乘法(然后在末尾加上-rho),但我不知道从哪里获取这些alphas。
我有一份支持向量的列表,如下所示:
1 1:-0.0434783 2:0.153846 3:0.194444 4:-0.353712 5:-0.450541 1:-0.2173916 2:-0.38461 3:0.222262 4:-0.676686 5:-0.78062
但alphas从哪里获取呢?
回答:
好的,现在事情似乎已经清楚了。Alphas是第一列,在我这里是这样。由于我在测试模型中所有的alphas都等于-1或1(不知道为什么),我以为这些是标签。
无论如何,这里是我的解析器(但你需要在文件中只保留SVs):
std::ifstream ifs("cars_model.model"); const int nsv = 90; const int nfeatures = 144; float rho = 12.5459; char ts[4000] = ""; // ! std::vector<float> res(nfeatures,0); std::vector<float> alphas; Mat_<float> temp(nsv, nfeatures); int c = 0; std::cout << "Loading model file...\n"; for (int i=0; i<nsv; i++) { float al = 0; ifs >> al; alphas.push_back(al); for (int j=0; j<nfeatures; j++) { float ind, s; char junk; ifs >> ind >> junk >> s; temp.at<float>(c, j) = s; //std::cout << f << ' ' << s << '\n'; } c++; } ifs.close(); std::cout << "Computing primal form...\n"; for (int i=0; i<nsv; i++) { float alpha = alphas[i]; for (int j=0; j<nfeatures; j++) { res[j] += (temp.at<float>(i,j) * alpha); } } //res.push_back(-rho); std::ofstream ofs("primal.txt"); for (int i=0; i<res.size(); i++) ofs << res[i] << ' '; ofs.close();
你知道吗,它工作了。你可以将rho设置为检测器的阈值。