Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

c++ - Training custom SVM to use with HOGDescriptor in OpenCV

I'm trying to train my own detector for use with OpenCV::HOGDescriptor but I'm having trouble making the existing HOGDescriptor work with my newly trained SVM.

I have calculated HOG features for positive and negative training images, labeled them and trained the SVM using CvSVM. The parameters I have used are:

    CvSVMParams params;
    params.svm_type =CvSVM::EPS_SVR;
    params.kernel_type = CvSVM::LINEAR;
    params.C = 0.01;
    params.p = 0.5;

Then I calculate Primal Form of the support vectors so that I only get one vector instead of many and set the calculated support vector using HOGDescriptor.setSVMDetector(vector);

This is Primal Form

When I use CvSVM.predict() I am able to correctly classify objects with the SVM, but HOGDescriptor.detect() or detectMultiScale() always returns a lot of positive matches and does not give accurate predictions.

CvSVM.predict() uses the original support vectors for classification so there might be something wrong with the way I'm calculating primal form.

Is there anyone who has trained their own detector who can point me in the right direction?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I wrote a child class of CvSVM to extract primal form after a linear svm is trained. Positive samples are labeled 1 and negative samples are labeled -1. It is strange that I have to put negative sign in front of alphas and leaving the sign of rho unchanged in order to get correct results from HogDescriptor.

LinearSVM.h

#ifndef LINEAR_SVM_H_
#define LINEAR_SVM_H_
#include <opencv2/core/core.hpp>
#include <opencv2/ml/ml.hpp>

class LinearSVM: public CvSVM {
public:
  void getSupportVector(std::vector<float>& support_vector) const;
};  

#endif /* LINEAR_SVM_H_ */

LinearSVM.cc

#include "linear_svm.h"    
void LinearSVM::getSupportVector(std::vector<float>& support_vector) const {

    int sv_count = get_support_vector_count();
    const CvSVMDecisionFunc* df = decision_func;
    const double* alphas = df[0].alpha;
    double rho = df[0].rho;
    int var_count = get_var_count();
    support_vector.resize(var_count, 0);
    for (unsigned int r = 0; r < (unsigned)sv_count; r++) {
      float myalpha = alphas[r];
      const float* v = get_support_vector(r);
      for (int j = 0; j < var_count; j++,v++) {
        support_vector[j] += (-myalpha) * (*v);
      }
    }
    support_vector.push_back(rho);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...