Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OpenCV 3.1 (C++) - SVM.load() does not set var_count correctly

Okay, I have some training data with 35 variables, I trained an SVM classifier and saved it using:

svm->save("trained-svm.xml")

In another application, I was trying to load the classifier using the XML file saved previously, and I encountered an Assertion Failure when I tried to call predict() on a piece of new input data:

OpenCV Error: Assertion failed (samples.cols == var_count && samples.type() == CV_32F) in cv::ml::SVMImpl::predict, file E:\Downloads\opencv\sources\modules\ml\src\svm.cpp, line 1930

I double checked that I have loaded my data as CV_32F type, which means that the assertion failed at samples.cols == var_count. And I double checked that I did have the right number of columns (35) in my test data.

So I tried to print out the var_count as follows:

svm->load<SVM>("trained-svm.xml");
cout << svm->getVarCount() << endl;

The result is that getVarCount() reports value of -842150451, which is clearly wrong. Since my XML does mention var_count as 35. Apparently load() doesn't update this value, causing the assertion.

<?xml version="1.0"?>
<opencv_storage>
<opencv_ml_svm>
  <format>3</format>
  <svmType>C_SVC</svmType>
  <kernel>
    <type>LINEAR</type></kernel>
  <C>1.</C>
  <term_criteria><epsilon>1.1920928955078125e-07</epsilon>
    <iterations>1000</iterations></term_criteria>
  **<var_count>35</var_count>**
  <class_count>6</class_count>
  <class_labels type_id="opencv-matrix">
    <rows>6</rows>
    <cols>1</cols>
    <dt>i</dt>
    <data>
      1 2 3 4 5 6</data></class_labels>
  <sv_total>15</sv_total>
(snipped away rest of XML)

Any solutions to this?