Ask Your Question

CH's profile - activity

2016-09-20 06:53:51 -0600 received badge  Famous Question (source)
2015-11-15 01:17:35 -0600 received badge  Notable Question (source)
2015-06-09 08:50:58 -0600 received badge  Popular Question (source)
2014-07-31 14:47:39 -0600 commented answer Train SVM with HOG descriptors

Thanks, I posted another question related to the code that you provided. Any idea? http://answers.opencv.org/question/38498/xml-data-to-appropriate-type-of-container/

2014-07-31 14:46:49 -0600 asked a question xml data to appropriate type of container.

Hi, i modified your code little bit, but I am not sure this is correct. I added two lines to load my trained svm values. But it seems to not working for the line const CvSVMDecisionFunc* df = svm1.decision_func;

Any idea? Thank you for your time.


void LinearSVM::get_primal_form_support_vector(std::vector<float>& support_vector) const {

LinearSVM svm1;   // need instance to load trained svm vlaues in result.txt 

svm1.load("result.txt");


int sv_count = svm1.get_support_vector_count();

const CvSVMDecisionFunc* df = svm1.decision_func;

const double* alphas = df[0].alpha;

double rho = df[0].rho;

int var_count = svm1.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 = svm1.get_support_vector(r);

  for (int j = 0; j < var_count; j++,v++) {

    support_vector[j] += (-myalpha) * (*v);

  }

}

support_vector.push_back(rho);

}


you might need this below class as well.

class LinearSVM : public CvSVM{

public:

std::vector<float> get_primal_form() const;

};

2014-07-31 10:36:32 -0600 received badge  Editor (source)
2014-07-31 10:34:05 -0600 asked a question Train SVM with HOG descriptors

Hi,

I have a question. I am trying to detect people from background images, and I have some problem to do it. Upto now, I put 450 positive images, and 1240 negative images to train my SVM after I get HOG Descriptors for them.

To get HOG Descriptors

First, I re-size every image to 64 x 128. Then I use HOGDescriptor::compute function with Size(8,8) for every image. After this, I labeled positive as 1 and negative as -1.

Upto here I get HOGfeaturematrix that has Size(450+1240, 3780) and Label that has Size(450+1240, 1)

Then I trained my SVM as below code.

CvSVMParams params;

params.svm_type = CvSVM::C_SVC;

params.kernel_type = CvSVM::LINEAR;

params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);

// Train the SVM

CvSVM svma;

svma.train(HOGfeaturematrix, Label, Mat(), Mat(), params);

svma.save('result.txt');

When I look into the result.txt file, I can see as below..

%YAML:1.0

my_svm: !!opencv-ml-svm

svm_type: C_SVC

kernel: { type:LINEAR }

C: 1.

term_criteria: { epsilon:2.2204460492503131e-016, iterations:100 }

var_all: 3780

var_count: 3780

class_count: 2

class_labels: !!opencv-matrix

  rows: 1

  cols: 2

  dt: i

  data: [ -1, 1 ]

sv_total: 1

support_vectors: - [ some numbers .. total 3780 numbers ]

decision_functions:

  -

     sv_count: 1

     rho: -2.8606529322674326e+000

     alpha: [ 1. ]

     index: [ 0 ]

And I use values of support_vectors as below

vector<float> getThisPeopleDetector() { static const float detector[] = { all 3780 numbers };

return vector<float>(detector, detector + sizeof(detector)/sizeof(detector[0]));

}

to pass ::setSVMDetector function as below.

hog.setSVMDetector(getThisPeopleDetector());

But the result is bad compared to hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());

What is my problem ?

Thank you for your time.