Ask Your Question
1

HOG descriptor - setSVMDetector throws confusing error

asked 2017-06-20 13:48:58 -0600

sleo5 gravatar image

I'm not an expert in machine learning and I don't understand the error that my code throws.

So I train a svm with 15 classes. everything is fine.

// Set up SVM's parameters
Ptr<SVM> svm = SVM::create();    
svm->setType(SVM::C_SVC);
svm->setKernel(SVM::POLY);
svm->setC(100); 
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 100, 1e-6));
svm->setDegree(1);

// Train the SVM with given parameters
Ptr<TrainData> td = TrainData::create(training_mat, ROW_SAMPLE, labelsMat);
svm->train(td);    
svm->save("output/xml/svm_filename.xml");

I implement the HOG descriptor like that HOGDescriptor hog(Scalar(16,16), Scalar(8,8), Scalar(4,4), Scalar(4,4), 9);

The Problem is that the program crashed when I call this method the setSVMDetector:

Ptr<SVM> svm = SVM::create();
svm = svm->load("output/xml/svm_filename.xml");
hog.setSVMDetector(svm->getSupportVectors());

the error is

OpenCV Error: Assertion failed (d == 2 && (sizes[0] == 1 || sizes[1] == 1 || sizes[0]*sizes[1] == 0)) in create, file /home/leo/extra/opencv-3.2.0/modules/core/src/matrix.cpp, line 2410

the same code is working perfectly when I only train the svm with 2 classes.

I don't know what this error message means, I've already looked around, and I found nothing.

Can someone please help me?

edit retag flag offensive close merge delete

Comments

you're already on the way, but it's not only the support vector, you have to add a -rho element at the end, see here

berak gravatar imageberak ( 2017-06-20 14:07:47 -0600 )edit

@berak thank you for your answer, but what is a -rho and how do I can "add" it?

sleo5 gravatar imagesleo5 ( 2017-06-20 16:38:00 -0600 )edit

can you look at the samplecode again ? (it's the decision function for the single support vector)

berak gravatar imageberak ( 2017-06-21 01:33:38 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-06-21 01:38:27 -0600

berak gravatar image

updated 2017-06-21 03:04:14 -0600

So I train a svm with 15 classes. everything is fine.

no, not fine.

the HOGDescriptor needs a 2 class regression , not a multi-class classification,

you have to train it on positive / negative samples of a single class using some variant of SVM::C_SVR.

please, again take a look at train_HOG.cpp !

edit flag offensive delete link more

Comments

thank you @berak

I though SVM in OpenCV also computes multi-class classification, there are a few samples about that and the results of the predict function are for 2-class regression 1 or -1, in case of multi-class the results could be 0,1, 2, 3,...,14 depending on the number of classes.

  • What would you recommand to execute a multi-class classification using OpenCV?

  • Do I implement my code with wrong parameters?

sleo5 gravatar imagesleo5 ( 2017-06-21 04:17:59 -0600 )edit

sure you can do a multi class classification with the SVM, too.

you just cannot use that with the HOGDescriptor, and the SVM on its own won't give you a detection (as in: position), only the closest id from the train set.

in short, you cannot have both at the same time. if you need a multi-class detection & recognition, maybe have a look at yolo

berak gravatar imageberak ( 2017-06-21 04:50:39 -0600 )edit

@berak thanks a lot

I just feel like I get HOG and SVM all the time really bad.

I though I have to use the HOG descriptor for the classification in SVM. When the classification is done, I can compute a detection using the generated vectors of SVM hogDescriptor.setSVMDetector(svm->getSupportVector()) , and call the method HOGDescriptor.detectMultiScale, which compares the given data (current Mat) to the saved one in the support vectors and give the areas that match (with class data). Am I wrong?

I was full of hope as I found HOG - object detection and handwritten classification

sleo5 gravatar imagesleo5 ( 2017-06-21 06:40:31 -0600 )edit

not really. again you cannot train an SVM for classification AND regression at the same time, the support vectors for the classification are different ones, than the single one used for the detection (of a single class)

admittedly, a confusing topic, but the fog will lift, i promise ! ;)

berak gravatar imageberak ( 2017-06-21 06:50:37 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-06-20 13:48:58 -0600

Seen: 1,441 times

Last updated: Jun 21 '17