Ask Your Question

SVM model fails to load!

asked 2016-04-25 09:08:18 -0600

Bekhouche gravatar image

Hello, I trained an SVM model and save it in xml file, everythink look good. but when I load the model I print the getvar vaule its equal to 0? The model file looks fine to me?

 <?xml version="1.0"?>
  <class_labels type_id="opencv-matrix">
      1 2</data></class_labels>
      -6.66133815e-15 -4.88498131e-15 -3.33066907e-15 -3.33066907e-15 0.

This is a part of the train code :

Ptr<ml::SVM> Model = ml::SVM::create();
Ptr<ml::TrainData> TrainData = ml::TrainData::create(Data, ml::ROW_SAMPLE, Label);

Test part in other project :

Ptr<ml::SVM> classifier = ml::SVM::create();
Mat features = (*featuresSptr)[i];
features.convertTo(features, CV_32F);
float label = classifier->predict(features);

error :

OpenCV Error: Assertion failed (samples.cols == var_count && samples.type() == CV_32F) in predict

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-04-25 09:26:43 -0600

berak gravatar image

in opencv3, Algorithm::load() creates a new instance, so you have to load your SVM like:

 Ptr<ml::SVM> classifier = Algorithm::load<ml::SVM>(filename);

(in your original code, the newly loaded model was discarded, never transferred to your classifier instance)

edit flag offensive delete link more


btw, please use less pointers in your code ;)

(esp. pointers to Mat are a hazard game)

berak gravatar imageberak ( 2016-04-25 09:28:32 -0600 )edit

Thank you :) Is there any way to pass the load after create the instance?

Bekhouche gravatar imageBekhouche ( 2016-04-25 09:40:55 -0600 )edit

no. (why ?) just use load() instead of create()

(again, it's a static template method, it does not use or need a this pointer)

berak gravatar imageberak ( 2016-04-25 09:42:31 -0600 )edit

I'm working on a framework where I must create the SVM instance in the header ...then load the model in the source, one time in the initializing function

Bekhouche gravatar imageBekhouche ( 2016-04-25 09:53:18 -0600 )edit

you can still overwrite it from load() later.

(but maybe your "framework" design is flawed, then)

berak gravatar imageberak ( 2016-04-25 09:59:18 -0600 )edit

thank you :)

Bekhouche gravatar imageBekhouche ( 2016-04-25 10:05:17 -0600 )edit

Question Tools

1 follower


Asked: 2016-04-25 09:08:18 -0600

Seen: 2,705 times

Last updated: Apr 25 '16