SVM Prediction

asked 2015-05-04 14:56:51 -0500

Gabriel Cachia gravatar image

updated 2015-05-05 08:18:33 -0500

Hello, this is my first time posting here. I am currently working on age estimation and extracting features using Gabor Filters. I have decided to classify ages using a SVM, the training is successful and it does not take a long time since the feature vectors are only about 3000 in dimensions by a 1000 training samples.

The problem is that when I want to predict an image, the result returned is always 18 (I pass the feature vector of the testing image to the predict function of the SVM). But when I predict an age from the training set the result is always correct. I do not know why this is happening. Any help will be appreciated.

  do {
    sprintf(fullImagePath,"%s\\%s", imageDirectory,;
    string label =;
    float labelSubstring = atof(label.substr(15,2).c_str());

    Mat input = imread(fullImagePath,0);
    featureVector = ExtractFeatures(input);
} while( _findnext( hFile, &c_file ) == 0 );

SVM svm;
CvSVMParams params;

params.svm_type = CvSVM::C_SVC;
params.kernel_type = CvSVM::RBF;

Mat testingSVMBud = imread ("C:\\Users\\Gabriel\\Desktop\\ae2.JPG",0);
Mat fV = ExtractFeatures(testingSVMBud).clone();
float prediction = svm.predict(fV);

Thank you for your time!

edit retag flag offensive close merge delete


I guess your training is not correct. From what you say, it seems that you're overfitting your training data, and therefore failing to generalize over the test data. You could try changing the C parameter to apply regularization

LorenaGdL gravatar imageLorenaGdL ( 2015-05-05 03:30:19 -0500 )edit

Changing the C parameter multiple times did nothing for the prediction. The result is always 18. I don't know what to do any more since I tried multiple solutions.

Gabriel Cachia gravatar imageGabriel Cachia ( 2015-05-05 06:26:56 -0500 )edit

Posting your code here might be helpful to catch errors

LorenaGdL gravatar imageLorenaGdL ( 2015-05-05 06:52:29 -0500 )edit

Question Edited :) in the do while loop i am grabbing the images in the training set and extracting the Feature Vectors one by one and then adding them to the Feature Vector I am using to train the SVM. Thank you for reading my question!

Gabriel Cachia gravatar imageGabriel Cachia ( 2015-05-05 08:20:05 -0500 )edit

I don't know which values of C you have tested, but 1000 seems a way too high value to me. Another question: is there any reason you're using an RBF kernel? I would also try to use the train_auto function with a k_fold setting to use cross-validation while training

LorenaGdL gravatar imageLorenaGdL ( 2015-05-05 09:17:21 -0500 )edit

Just a question: as the code you wrote, prediction is either -1 or 1. So how do you know the test image is predicted to be in 18 year old category?

tuannhtn gravatar imagetuannhtn ( 2015-05-06 06:39:17 -0500 )edit

Why is the prediction either -1 or 1? He's using several different labels (taken from the filename string), and therefore the prediction, as it is multiclass classification, will return the corresponding age

LorenaGdL gravatar imageLorenaGdL ( 2015-05-06 07:00:23 -0500 )edit

Thanks Lorena GdL, I supposed the SVM part of OpenCV just supported binary SVM. Then, in this case I think a polynomial kernel is better.

tuannhtn gravatar imagetuannhtn ( 2015-05-06 08:29:13 -0500 )edit

Hi, thanks for your suggestion! I used train_auto and the results are really off, the results being produced are different for each image so that is a step forward :). Thanks once again

Gabriel Cachia gravatar imageGabriel Cachia ( 2015-05-06 16:42:22 -0500 )edit