Ask Your Question
0

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"?>
<opencv_storage>
<opencv_ml_svm>
  <format>3</format>
  <svmType>C_SVC</svmType>
  <kernel>
    <type>LINEAR</type></kernel>
  <C>1.0000000000000001e-01</C>
  <term_criteria><epsilon>1.1920928955078125e-07</epsilon>
    <iterations>1000</iterations></term_criteria>
  <var_count>4096</var_count>
  <class_count>2</class_count>
  <class_labels type_id="opencv-matrix">
    <rows>2</rows>
    <cols>1</cols>
    <dt>i</dt>
    <data>
      1 2</data></class_labels>
  <sv_total>1</sv_total>
  <support_vectors>
    <_>
      -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);
Model->setType(SVM);
Model->setKernel(Kernel);
Model->trainAuto(TrainData,5);
Model->save((*path));

Test part in other project :

Ptr<ml::SVM> classifier = ml::SVM::create();
classifier->load(*modelPath);
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
3

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

Comments

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

Stats

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

Seen: 2,747 times

Last updated: Apr 25 '16