Ask Your Question

Revision history [back]

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