1

I am using Visual Studio 2010, with OpenCV 3.0. I'm trying to train a SVM and to save it into a file, but I am having problems.

My purpose is to extract the HOG features of some images and train a SVM with them. All seems to be right, but when I try to save the model in a xml file I obtain the following error:

Unhandled exception in 0x000007fefd9bb16d (KernelBase.dll) in TrainSVM.exe: Exception de MICROSOFT C++: cv::Exception at memory location 0x0026e1b0.

And then this is showed in console:

OpenCV Error: Parsing error (SVM model data is invalid, check sv_count, var_* an d class_count tags) in cv::ml::SVMImpl::write, file C:\builds\master_PackSlave-w in64-vc12-shared\opencv\modules\ml\src\svm.cpp, line 2027

The error seems to appear when the SVM has not been trained properly, but I don't understand where I have failed, because the line

svm->train(auxResult)

has "true" as result.

I have checked the images and they are loaded properly, anybody could help me?

Thanks in advance.

Here is the code:

    String imagesPathPos = "Positivas/*.jpg"; // it has filters, too !
    vector<String> fp;
    glob(imagesPathPos, fp); 
    int tamaño = fp.size();

    std::vector<cv::Point> positions;
    positions.push_back(cv::Point(0,0));
    std::vector<float> descriptor;
    Ptr<TrainData> auxResult;

    for (size_t i=0; i<fp.size(); ++i)
    {
        string nameFile = fp[i];
        Mat img = imread(fp[i]);     
        cv::Mat grayImg;
        cvtColor( img, grayImg, COLOR_BGR2GRAY );           

        hog.compute(grayImg,descriptor,winStride,trainingPadding,positions);

        Mat auxDescriptor = cv::Mat(descriptor);
        Mat descriptorMat(1,auxDescriptor.rows,CV_32FC1);
        transpose(auxDescriptor, descriptorMat);
        trainingData.push_back(descriptorMat);          
        trainingLabels.push_back(labelPositive);        
    }

    String imagesPathNeg = "Negativas/*.jpg";
    vector<String> fn;
    glob(imagesPathNeg, fn, true); 
    for (size_t i=0; i<fn.size(); i++)
    {
        Mat img = imread(fn[i]);
        cv::Mat grayImg;
        cvtColor( img, grayImg, COLOR_BGR2GRAY );
        hog.compute(grayImg,descriptor,Size(),Size(),positions);

        Mat auxDescriptor = cv::Mat(descriptor);
        Mat descriptorMat(1,auxDescriptor.rows,CV_32FC1);
        transpose(auxDescriptor, descriptorMat);
        trainingData.push_back(descriptorMat);
        trainingLabels.push_back(labelPositive);
    }

    auxResult = TrainData::create(trainingData, type, trainingLabels);

    svm->train(auxResult);

    svm->save("output.xml");
JoseleMG
  • 302
  • 5
  • 18
  • @Noripsni I have seen that you posted [here](http://stackoverflow.com/questions/30727369/loading-pretrained-opencv-yml-in-java) a similar error, did you get to fix it? – JoseleMG Jan 22 '16 at 17:25

1 Answers1

0

You are defining "labelPositive" even when the images are negative. Possibly the error is there, within the loop through the vector fn:

trainingLabels.push_back(labelPositive);

You should use a parameter named "labelNegative" defined as -1.

Jose L
  • 322
  • 3
  • 13