# HOG descriptor - setSVMDetector throws confusing error

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();
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.

edit retag close merge delete

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

( 2017-06-20 14:07:47 -0500 )edit

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

( 2017-06-20 16:38:00 -0500 )edit

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

( 2017-06-21 01:33:38 -0500 )edit

Sort by » oldest newest most voted

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 !

more

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?

( 2017-06-21 04:17:59 -0500 )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

( 2017-06-21 04:50:39 -0500 )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

( 2017-06-21 06:40:31 -0500 )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 ! ;)

( 2017-06-21 06:50:37 -0500 )edit

Official site

GitHub

Wiki

Documentation