c++ - 将LibSVM输出转换为浮点 vector

标签 c++ opencv machine-learning computer-vision libsvm

我需要形成HOGDescriptor::setSVMDetector()输入。

我使用openCV计算描述符,然后使用libSVM获取模型文件。
要形成输入,我知道我需要获取支持 vector 的值并将它们与alpha进行逐个元素化(然后在末尾添加-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

但是在哪里可以得到阿尔法?

最佳答案

好吧,看来情况已经清楚了。
就我而言,Alphas是第一列
由于在我的测试模型中所有参数都等于-1或1(不知道为什么),所以我认为这些都是标签。

无论如何,这是我的解析器(但是您只需要在文件中保留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> 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设置为检测器的阈值。

关于c++ - 将LibSVM输出转换为浮点 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19089256/

相关文章:

python - 查找无人驾驶车道,代码问题

binary - 为什么我们使用 -1 和 +1 作为二进制输入而不是 0 和 1

machine-learning - 训练tensorflow模型会自动保存参数吗?

C++游戏对按键没有时间响应

opencv - 围绕轮廓对象生成颜色直方图

相当于tailq的C++

c++ - OpenCV 人脸检测返回太多人脸

python - 值错误: y contains non binary labels for VotingClassifier with my GradientBoostingClassifier

c++ - Dark GDK 更改显示模式使 Sprite 消失

Linux 中的 C++ GLFW3 全屏拉伸(stretch)问题