Confusing SVM training error

asked 2015-11-11 18:10:18 -0500

Phazoozoo gravatar image

Hello,

I am attempting to use BoW and SVM to match a query image with its class in the training image set. Right now I am only attempting to match the query into one of two classes (either it is or is not the correct class). After extrating features and clustering, I computed histograms for each of the images in the training set. I am trying to use svm->train() with a Mat of the histograms, and a Mat of the class number (1 or 0). The problem I have is this stubborn error.

    "Assertion failed (samples.type() == CV_32F || samples.type == CV_32S) in cv::ml::TrainDataImpl::setData"

I have code to convert the types of both inputs to CV_32F, yet the error persists. I have tried making the conversion in various ways, and declaring the variables in various ways. Nothing I change seems to get rid of the error.

Here is my code. histograms and classes are declared as shown below. This is done before filling them with the data.

Mat histograms(0, numOfClusters, CV_32FC1);
Mat classes(0, 1,CV_32FC1);

    Ptr<ml::SVM> svm = ml::SVM::create();

svm->setType(ml::SVM::C_SVC);
svm->setKernel(ml::SVM::POLY);
svm->setGamma(3);
cout << "params set" << endl;
Mat trainingDataMat(histograms);


Mat trainingDataMat_32;
Mat classes_32;
trainingDataMat.convertTo(trainingDataMat_32, CV_32F);
trainingDataMat_32 = trainingDataMat_32.reshape(trainingDataMat_32.cols, 1); 
svm->train(trainingDataMat_32, ml::ROW_SAMPLE, classes);    //incorrect types? I think it is a problem with ROW_SAMPLE


Mat res;   // output
svm->predict(Qoutput, res);

I am using opencv 3.0.0 with Visual Studio 2013. Any help would be greatly appreciated.

Thanks

edit retag flag offensive close merge delete

Comments

1

please use CV_32S (integer) for the classes

berak gravatar imageberak ( 2015-11-11 23:02:32 -0500 )edit
1

trainingDataMat_32.reshape(trainingDataMat_32.cols, 1); <-- this looks weird.

again, for N samples, your traindata should have 1 channel, N rows float data (one histogram per row), and your classes 1 channel N rows integers.

berak gravatar imageberak ( 2015-11-12 04:22:51 -0500 )edit

Thanks, I changed the type of classes. I changed trainingDataMat_32.reshape(trainingDataMat_32.cols, 1); to trainingDataMat_32.reshape(1, trainingDataMat_32.rows); TrainingDataMat.cols was not the correct value at all. I am still getting errors, but this time it says "OpenCV error: one of the arguments' values is out of range (the kernel parameter <degree> must be positive)".

Phazoozoo gravatar imagePhazoozoo ( 2015-11-12 22:45:09 -0500 )edit

that's progress !

if you're using a POLY kernel, indeed, "degree" has to be a positive value (think of (x * y)^degree)

try something like 3

berak gravatar imageberak ( 2015-11-13 00:47:45 -0500 )edit

It compiled and ran! My output was not correct, but that could be an issue with my feature extraction. So I still have some things to try to get it working. Thank you very much.

Phazoozoo gravatar imagePhazoozoo ( 2015-11-13 10:20:14 -0500 )edit