将LibSVM输出转换为浮点数 [英] Convert LibSVM output to vector of floats
问题描述
我需要形成 HOGDescriptor :: setSVMDetector()
输入。
我用openCV计算描述符,然后使用libSVM获取模型文件。
要形成输入,我知道我需要得到支持向量的值和元素mul他们与alphas(然后添加-rho在结尾),但我不能得到这些alphas 。
我有一个SV列表,例如:
1 1:-0.0434783 2:0.153846 3:0.194444 4:-0.353712 5:-0.45054
1 1:-0.2173916 2:-0.38461 3:0.222262 4:-0.676686 5:-0.78062
$ 解决方案 c $ c>
< 好吧,现在似乎事情很清楚。
Alphas是我的第一列。
因为我在我的测试模型中都有-1或1(dunno为什么),我认为这些都是标签。
我的解析器(但你需要在文件中只留下SV):
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>阿尔法
Mat_< float> temp(nsv,nfeatures);
int c = 0;
std :: cout<< 加载模型文件... \\\
;
for(int i = 0; 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<< '\\\
';
}
c ++;
}
ifs.close();
std :: cout<< 计算原始形式... \\\
;
for(int i = 0; i
float alpha = alphas [i];
for(int j = 0; 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设置为检测器的阈值。
I need to form HOGDescriptor::setSVMDetector()
input.
I compute descriptors with openCV then use libSVM to get model file.
To form input i know that i need to get support vectors' values and elementwise mul them with alphas (then add -rho at the end), but i don't get where to get these alphas.
I have a list of SVs like:
1 1:-0.0434783 2:0.153846 3:0.194444 4:-0.353712 5:-0.45054
1 1:-0.2173916 2:-0.38461 3:0.222262 4:-0.676686 5:-0.78062
but where to get alphas?
解决方案 Ok, it seems things are clear now.
Alphas are the first column in my case.
Since i had all of them equal to -1 or 1 (dunno why) in my test model, i thought these were labels.
Anyway, here is my parser (but you need to leave only SVs in file):
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();
And you know, it works. You can set rho as threshold of the detector.
这篇关于将LibSVM输出转换为浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!