Ask Your Question
0

SVM returning a single value during prediction

asked 2016-08-15 10:19:12 -0600

Angulu gravatar image

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 flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-08-16 03:35:53 -0600

B4silio gravatar image

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

edit flag offensive delete link more

Comments

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

StevenPuttemans gravatar imageStevenPuttemans ( 2016-08-16 07:45:05 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-08-15 10:19:12 -0600

Seen: 1,410 times

Last updated: Aug 16 '16