# SVM returning a single value during prediction

I have trained an SVM for image classification as shown below. The training seem to be going on well but when I predict a row (any row of the training vector), SVM return just one value (60). What could I have done wrong? Is my training and testing approach right as shown in the function below?

void trainSVM(Mat hists, vector<int> labels){
Ptr<TrainData> trainData = TrainData::create(hists, ml::ROW_SAMPLE, labels);
Ptr<SVM> svm = SVM::create();
svm->setKernel(SVM::LINEAR);
cout << "Training SVM..." << endl;
svm->train(trainData);
bool trained = svm->isTrained();
if (trained)
{
cout << "SVM Trained. Saving..." << endl;
svm->save(".\\Trained Models\\LBPSVM.yml");
cout << "SVM Model Saved." << endl;
}
Mat_ <float> output;
svm->predict(hists, output);
float pred = svm->predict(hists.row(34));
cout << "SVM Output = " << output.at<float>(0) << endl;
writeMatToFile(output, "SVNPredict.csv");
}


The value stored in variable pred is the same regardless of the row I try to predict. It is the same value I get in my single column matrix output. All rows are populated with same value. Kindly help.

edit retag close merge delete

Sort by » oldest newest most voted

It may be a problem with the training (and parameters) rather than with the code itself: if the SVM model is simply a horizontal line (e.g. if the epsilon is too large for your data) then it will always output the same value (the bias).

I dont remember off the bat but depending on the default parameters for SVM training, the model nay expect to have data that jumps around in the range of 0-1 instead of hundreds. Try rescaling your input to a 0-1 range, or try to play around with your training parameters (mostly the epsilon if you're doing epsilon SVM).

Good luck! Basilio

more

Jep normalizing your input parameters is a good idea before training the SVM.

( 2016-08-16 07:45:05 -0500 )edit

Official site

GitHub

Wiki

Documentation