Ask Your Question
6

Machine learning save/load problem

asked 2013-05-15 14:27:37 -0600

wampir gravatar image

updated 2013-05-15 16:26:21 -0600

Hello,

I have a problem with save() and load() methods in ml module. for example if I run code:

CvSVM *SVM = new CvSVM;
SVM->load("SVM.xml");
SVM->predict(DataRow);

I'll obtain this error:

OpenCV Error: Bad argument (The SVM should be trained first) in CvSVM::predict, file /home/wampir/OpenCV-2.4.3/modules/ml/src/svm.cpp, line 2072
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/wampir/OpenCV-2.4.3/modules/ml/src/svm.cpp:2072: error: (-5) The SVM should be trained first in function CvSVM::predict

Model in "SVM.xml" is saved with: SVM->save("SVN.xml","NET"); of course SVM was previously trained;

When I'm doing the same with CvRTrees, method load() gives me a segmentation fault (core dumped). For CvERTrees, CvGBTrees and CvNormalBayesClassifier it works exactly like for CvSVM.

CvKNearest is bigger problem because I can't even save my trained model to a file, for example when I run this code:

KNearest->save("KNearest.xml");

for trained CvKNearest *KNearest=new KNearest; my "KNearest.xml" will contain only:

<?xml version="1.0"?>
<opencv_storage>

and nothing more.

After that Ill obtain this error:

OpenCV Error: The function/feature is not implemented () in CvStatModel::write, file /home/wampir/OpenCV-2.4.3/modules/ml/src/inner_functions.cpp, line 114
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/wampir/OpenCV-2.4.3/modules/ml/src/inner_functions.cpp:114: error: (-213)  in function CvStatModel::write

Can somebody help me, and tell me how to properly save and load my models or if it is a bug fix it?

Thank you very much ;)

PS: I almost forgot. Im working under ubuntu 12.10 x86, kernel Linux 3.5.0-17-generic.

PS2: Ow - One more thing - Im using OpenCV 2.4.4

edit retag flag offensive close merge delete

Comments

1

yea, you hit some wasp nest there

berak gravatar imageberak ( 2013-05-15 14:56:32 -0600 )edit

3 answers

Sort by » oldest newest most voted
2

answered 2013-05-15 16:03:42 -0600

Ok some thoughts on the matter:

  1. Do you really need to use the C interface, which is depricated and asks you to keep track of all pointer related problems yourself? If not, switch to the more newer C++ API and see many of your problems get solved rather quickly.
  2. Any reason why you would want to use openCV2.4.4? The new release 2.4.5 has many bug fixes, this could be one of them and it could already be solved quite fast. Switch to latest stable release!
  3. 9 chances out of 10, your problem is related to not reading in the correct xml model. Don't use any relative paths before you know an absolete path is working. So call the SVM->load("D:/SVM.xml"); to make sure this doesn't create your problems.
  4. Try to avoid using SVM as variable name, since it is actually also a function. Use something like my_svm instead, in order to not mix both up.

Change this and report back please!

edit flag offensive delete link more

Comments

1

@steven: Good post, two notes: afaik the ML-Part is not ported to C++ yet; point 4: it shouldn't matter as far as he doesn't call it CvSVM. Your are definitely right with point 2 and 3.

Guanta gravatar imageGuanta ( 2013-05-15 16:11:29 -0600 )edit
1

About third point -> I'm sure that I'm reading correct XML model. I've checked it twice;)

wampir gravatar imagewampir ( 2013-05-15 16:25:35 -0600 )edit
2

Then there is a problem with the pointer interfacing I am guessing. @Guanta, there is actually a C++ interface, just contains the same names as C interface BUT it doesn't require you to use pointer structures.

This sample code enabled me to save a naïve bayes classifier perfectly well, so try switching to C++?

    // Train normal bayes classifier
CvNormalBayesClassifier classifier;
// Training data and labels need to be of the CV_32FC1 format!
input_features.convertTo(input_features, CV_32FC1);
label_features.convertTo(label_features, CV_32FC1);
classifier.train(input_features, label_features);

// Save normal bayes classifer
classifier.save("d:/cyclomedia_2/naive_bayes_classifier.xml");

Feature sets are basically the following: a row for input feature, a 0 or 1 for label

StevenPuttemans gravatar imageStevenPuttemans ( 2013-05-16 02:51:25 -0600 )edit
1

Once again. I don't have problems with save() method (except CvKNearest class) - I get good xml file after saving, but I can't properly load those xml files. After loading my classifier is corrupted and when I try to classify I will receive error message which is given in my first post.

wampir gravatar imagewampir ( 2013-05-16 08:47:55 -0600 )edit
1

If you look at the source save and load doesn't seem to be implemented for CvKNearest

pickle27 gravatar imagepickle27 ( 2013-08-03 19:54:01 -0600 )edit
2

answered 2015-07-21 08:50:42 -0600

Andyrey gravatar image

Just check up loading before using your svm classifier:

Blockquote

CvSVM *mySVM = new CvSVM;

mySVM->load("mySVM.xml");

int in= mySVM->get_support_vector_count();

if(in)

mySVM->predict(DataRow);

Blockquote

get_support_vector_count() retuns 0 if your resource isn't loaded.

edit flag offensive delete link more
1

answered 2014-06-10 01:20:47 -0600

la lluvia gravatar image

updated 2014-06-10 01:22:00 -0600

Does anybody else have this problem? Wampir did you manage to fix it?

"Bad argument (The SVM should be trained first) in CvSVM::predict"

Steven, I've seen your post but it doesn't seems to be a problem.

I'm trying to load saved SVM classifier:

mySVM.save("C:\classifier.xml");

mySVM.load("C:\classifier.xml");

I saved it as .xml, file seems to be correctly saved. Thanks!

edit flag offensive delete link more

Comments

I've just solved the problem. Apparently in CvSVM::load function filename must be without path. So this was wrong:

mySVM1.load("C:\classifier.xml");

and changing it to this

mySVM1.load("classifier.xml"); works perfectly. I think this is a bug in OpenCV 2.4.4.0

la lluvia gravatar imagela lluvia ( 2014-06-10 06:22:40 -0600 )edit
1

no that is not a bug, it means that your original path is wrong. Please use the unix like front slashes or a double backslash to avoid problems. Also it seems that some compilers do not have reading rights on your system path C:, but they do inside a folder on that disk.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-06-10 08:48:17 -0600 )edit
1

with front slashes works. my mistake. thanks

la lluvia gravatar imagela lluvia ( 2014-06-10 09:20:48 -0600 )edit
1

You should keep in mind that windows based systems use the backslash as an escape character, resulting in unwanted behaviour. Therefore people need to type double slashes to undo the escape operation.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-06-11 03:04:08 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2013-05-15 14:27:37 -0600

Seen: 18,639 times

Last updated: Jul 21 '15