How to form data for SVM training OpenCV3

asked 2015-08-25 14:18:49 -0500

Stefan Isidorovic gravatar image

I am trying to write utility for training svm classifier for image classification in OpenCV3. But I have Floating point exception (core dumped) error during training process.

My main problem is that I don't know, I'm not sure exactly how to form training data to feed svm.train method.

This is code which is forming training data.

TrainingDataType SVMTrainer::prepareDataForTraining() {

    cv::Mat trainingData(m_numOfAllImages, 28*28, CV_32FC1);
    cv::Mat trainingLabels(m_numOfAllImages, 1, CV_32FC1);

    int rowNum = 0;

    // Item is pair of classId (int) and vector of images.
    for(auto item : m_data){
        int classId = item.first;
        for(auto item1 : item.second){
            Mat temp = item1.reshape(1,1);

  <float>(rowNum) = item.first;

    return cv::ml::TrainData::create(trainingData,
                                     trainingLabels) ;


void SVMTrainer::train(std::string& configPath){
    // Read and store images in memory.

    m_classifier = cv::ml::SVM::create();
    // Training parameters:

    TrainingDataType trainData =  prepareDataForTraining();



All images are already prepared with dimensions 28*28, black&white.

And actual train call is in this method

Can somebody tell me what I am doing wrong.


edit retag flag offensive close merge delete



where exactly do you get the error ? (seems nothing wrong with the code)

berak gravatar imageberak ( 2015-08-26 00:06:18 -0500 )edit

During training... Exactly after call of trainAuto. During that method it shows me floating point exception and terminate program.

Stefan Isidorovic gravatar imageStefan Isidorovic ( 2015-08-26 00:13:20 -0500 )edit

might be a problem within the svm. are you able to run a debugger ?

what exactly is TrainingDataType ? is it a Ptr<ml::TrainData> ?

how many images are there ? (if not many, maybe someone can reproduce it with the same trainset)

berak gravatar imageberak ( 2015-08-26 00:15:32 -0500 )edit

using TrainingDataType = cv::Ptr<cv::ml::traindata>; Training set is fairly simple, ~45 images, very small set just to see if I get it done. I can zip it all and send :)

Stefan Isidorovic gravatar imageStefan Isidorovic ( 2015-08-26 00:25:54 -0500 )edit

Here is whole project in .tar.gz...

Stefan Isidorovic gravatar imageStefan Isidorovic ( 2015-08-26 00:30:08 -0500 )edit

I should be able to run debugger, I will give it a try when I get back home..

Stefan Isidorovic gravatar imageStefan Isidorovic ( 2015-08-26 00:34:23 -0500 )edit

unfortunately, i cannot reproduce it

when did you last update your opencv repo ? there were changes wrt. SVM lately

berak gravatar imageberak ( 2015-08-26 01:03:22 -0500 )edit

I downloaded library not long after first stable release, at the end of June I think... Did everything go well? I mean did utility produced successfully .xml classifier file?

Stefan Isidorovic gravatar imageStefan Isidorovic ( 2015-08-26 02:16:11 -0500 )edit

yes, i added a prediction step on the testingDIR, result is always 1 (not surprising)

there was e.g. a bug in trainAuto()

try to add that patch (or do a fresh git pull) .

(also, for 3.0 - good idea to keep an eye on github...)

berak gravatar imageberak ( 2015-08-26 02:29:25 -0500 )edit

Thank you very much :D I had serious headache about this one :D

Stefan Isidorovic gravatar imageStefan Isidorovic ( 2015-08-26 02:36:50 -0500 )edit