How to form data for SVM training OpenCV3

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

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);
            temp.copyTo(trainingData.row(rowNum));

            trainingLabels.at<float>(rowNum) = item.first;
            ++rowNum;
        }
    }

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

}

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

    m_classifier = cv::ml::SVM::create();
    // Training parameters:
    m_classifier->setType(cv::ml::SVM::C_SVC);
    m_classifier->setKernel(cv::ml::SVM::POLY);
    m_classifier->setGamma(3);  
    m_classifier->setDegree(3);

    TrainingDataType trainData =  prepareDataForTraining();

    m_classifier->trainAuto(trainData);

}

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.

Thanks,

edit retag flag offensive close merge delete

Comments

1

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

berak gravatar imageberak ( 2015-08-26 00:06:18 -0600 )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 -0600 )edit
1

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 -0600 )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 -0600 )edit
1

Here is whole project in .tar.gz... https://goo.gl/gWAewI

Stefan Isidorovic gravatar imageStefan Isidorovic ( 2015-08-26 00:30:08 -0600 )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 -0600 )edit
1

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 -0600 )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 -0600 )edit
1

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 -0600 )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 -0600 )edit