Ask Your Question
0

Hello, I'm getting an error while training svm.

asked 2018-03-15 20:55:30 -0500

updated 2018-03-15 21:29:49 -0500

LBerger gravatar image

Error:Bad argument (in the case of classification problem the responses must be categorical; either specify varType when creating TrainData, or pass integer responses) in cv::ml::SVMImpl::train, file C:\build\master_winpack-build-win64-vc15\opencv\modules\ml\src\svm.cpp, line 1624

//Programme:
#include "SVM.h"
#include < stdio.h>
#include < opencv2\opencv.hpp>
//#include < opencv2\gpu\gpu.hpp>

using namespace cv;
using namespace std;
using namespace cv::ml;




void svm1()
{

    //Read Hog feature from XML file
    ///////////////////////////////////////////////////////////////////////////
    printf("1. Feature data xml load\n");
    //create xml to read
    FileStorage read_PositiveXml("Positive.xml", FileStorage::READ);
    FileStorage read_NegativeXml("Negative.xml", FileStorage::READ);

    //Positive Mat
    Mat pMat;
    read_PositiveXml["Descriptor_of_images"] >> pMat;
    //Read Row, Cols
    int pRow, pCol;
    pRow = pMat.rows; pCol = pMat.cols;

    //Negative Mat
    Mat nMat;
    read_NegativeXml["Descriptor_of_images"] >> nMat;
    //Read Row, Cols
    int nRow, nCol;
    nRow = nMat.rows; nCol = nMat.cols;

    //Rows, Cols printf
    printf("   pRow=%d pCol=%d, nRow=%d nCol=%d\n", pRow, pCol, nRow, nCol);
    //release
    read_PositiveXml.release();
    //release
    read_NegativeXml.release();
    /////////////////////////////////////////////////////////////////////////////////

    //Make training data for SVM
 /////////////////////////////////////////////////////////////////////////////////
    printf("2. Make training data for SVM\n");
    //descriptor data set
    Mat PN_Descriptor_mtx(pRow + nRow, pCol, CV_32FC1); //in here pCol and nCol is descriptor number, so two value must be same;
    memcpy(PN_Descriptor_mtx.data, pMat.data, sizeof(float) * pMat.cols * pMat.rows);
    int startP = sizeof(float) * pMat.cols * pMat.rows;
    memcpy(&(PN_Descriptor_mtx.data[startP]), nMat.data, sizeof(float) * nMat.cols * nMat.rows);
    //data labeling
    Mat labels(pRow + nRow, 1, CV_32FC1, Scalar(-1.0));
    labels.rowRange(0, pRow) = Scalar(1.0);
    /////////////////////////////////////////////////////////////////////////////////


    /////////////////////////////////////////////////////////////////////////////////

    //Set svm parameter
    /////////////////////////////////////////////////////////////////////////////////
    printf("4. SVM training\n");
    Ptr<ml::SVM> svm = ml::SVM::create();
    svm->setType(ml::SVM::C_SVC);
    svm->setKernel(ml::SVM:: RBF);
    svm->setGamma(0.50625);
    svm->setC(100);
    //cout << "params set" << endl;
    printf("4. params set\n");


        Ptr<ml::TrainData> tData = ml::TrainData::create(PN_Descriptor_mtx, ml::SampleTypes::ROW_SAMPLE, labels);
    svm->train(tData);
    // ...
    Mat query; // input, 1channel, 1 row (apply reshape(1,1) if nessecary)
    Mat res;   // output
    svm->predict(query, res);
    //Trained data save
    /////////////////////////////////////////////////////////////////////////////////
    printf("5. SVM xml save\n");
    svm->save("trainedSVM.xml");

    // FileStorage hogXml("testXML.xml", FileStorage::WRITE); //FileStorage::READ
    // write(hogXml, "Data", PN_Descriptor_mtx);
    // write(hogXml, "Label", labels);
    // hogXml.release();
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-03-15 21:35:57 -0500

LBerger gravatar image

updated 2018-03-16 05:05:00 -0500

When you create TrainData you must define all parameters TrainData::create(feature, ROW_SAMPLE, responses, noArray(), noArray(), noArray(), typeVariable);

and use a loop to specify variable type :

Mat typeVariable(feature.cols + 1, 1, CV_8U);
for (int i = 0; i < feature.cols; i++)
{
    typeVariable.at<uchar>(i, 0) = VAR_NUMERICAL;

}
typeVariable.at<uchar>(feature.cols, 0) = VAR_CATEGORICAL;
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-03-15 20:55:30 -0500

Seen: 108 times

Last updated: Mar 16 '18