Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to form data for SVM training OpenCV3

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,