Confusing SVM training error
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
please use CV_32S (integer) for the classes
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.
Thanks, I changed the type of classes. I changed
trainingDataMat_32.reshape(trainingDataMat_32.cols, 1);
totrainingDataMat_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)".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