Convert LibSVM output to vector of floats

前端 未结 2 1321
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-24 01:41

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

2条回答
  •  不要未来只要你来
    2021-01-24 02:14

    But why do you want to classify this "by hand"? OpenCv has a classification routine called predict, which uses found SVs' and alphas'

    float response = SVM.predict(sampleMat);
    

    If you really want to do it by yourself you would not only need SVs and alphas but also a kernel function used for training and comput

    SUM alpha_i K( support_vector_i , data_point ) - rho
    

    I am not sure whether it is possible to extract alphas "by hand" without extending the SVM class, as one can see in the sources - alphas are stored in the CvSVMDecisionFunc structure:

    struct CvSVMDecisionFunc
    {
        double rho;
        int sv_count;
        double* alpha;
        int* sv_index;
    };
    

    while the only reference to this structure is in the protected section:

    protected:
    
        (...)
    
        CvSVMDecisionFunc* decision_func;
    

    From the source code of svm.cpp we can find, that it is only publically accesible through the save routine. So some "hack" would be to save the model and extract alphas from there (it will be located in the "Decision function" section, written in human readable format).

    The simplest extracion technique seems to extent the CvSVM class and include method like

    public:
    
        CvSVMDecisionFunc* get_decision_function() { return decision_func; }
    

    update

    after clarification, that OP actually is trying to use externaly trained model in opencv - the easiest way is to convert the libsvm model crated by other method (libsvm, linearsvm etc.) into opencv compatible format and load it using read method

    void CvSVM::read( CvFileStorage* fs, CvFileNode* svm_node )
    

    see source for more details.

提交回复
热议问题