Ask Your Question
0

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

asked 2016-09-26 23:45:59 -0600

wacky gravatar image

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?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-09-26 23:50:54 -0600

berak gravatar image

SVM::load() is a static function, which returns a new instance.

you have to use it like:

Ptr<SVM> svm = Algorithm::load<SVM>("trained-svm.xml");

in your case, the new instance was simply discarded, and you're querying an uninitialized svm instance for var_count, etc.

edit flag offensive delete link more

Comments

thanks... that works..!

wacky gravatar imagewacky ( 2016-09-27 21:49:38 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-09-26 23:45:59 -0600

Seen: 7,062 times

Last updated: Sep 26 '16