Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.