Ask Your Question
1

load/save SVM custom kernel

asked 2015-07-27 09:23:24 -0600

sdika gravatar image

Hi,

I am trying to train a SVM on some feauture using a custom kernel with opencv. Everything is ok for the training part: I extract the feature, train the SVM, save the results to a file with SVM::save.

The problem is when I try to use it: when I want to load the SVM from the file I get:

OpenCV Error: Parsing error (Invalid SVM kernel type (or custom kernel)) in read_params, file /myopt/opencv/opencv-3.0.0/modules/ml/src/svm.cpp, line 2112
terminate called after throwing an instance of 'cv::Exception'
  what():  /myopt/opencv/opencv-3.0.0/modules/ml/src/svm.cpp:2112: error: (-212) Invalid SVM kernel type (or custom kernel) in function read_params

and indeed in modules/ml/src/svm.cpp there is
if( kernelType == CUSTOM ) CV_Error( CV_StsParseError, "Invalid SVM kernel type (or custom kernel)" );

I don't understand this. How can I save and then load the result of my training?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2015-07-27 09:58:43 -0600

mshabunin gravatar image

This message means that SVM with custom kernel does not support loading from file. You can try following things:

  1. use one of the standard kernels (obviously)
  2. implement your kernel as standard and optionally contribute it to the mainline
  3. set kernel to standard before saving to file and back to custom after loading from file (workaround)
  4. implement mechanism for saving/loading custom kernels with parameters (can be hard)
edit flag offensive delete link more

Comments

1

thanks for the answers:

  1. obviously
  2. I am starting to work with custom kernels, there is really nothing worthwhile to contribute
  3. I though about it, but I save my data (including the svm object) in a const function
  4. I don't want t go into this.

actually, if the line "( kernelType == CUSTOM ) ... " was not here, it would load the svm object and I would set the kernel to mine.

I will try maybe the your 3 (but messing with const), I will also try another workaound: save/load only the sv and the alpha

thank anyway!

sdika gravatar imagesdika ( 2015-07-27 10:28:56 -0600 )edit

In current scheme there is no way to check if saved custom kernel is the same as one used by the loading application. And it can cause incorrect behaviour even if model is used in the same application: trained and saved with first kernel variant and loaded with second updated kernel variant.

If you have no restrictions on used memory or performance, you can try to save model to memory buffer first, then fix this buffer (change kernelType node) and finally save it to disk. This way seems to be compatible with const method.

mshabunin gravatar imagemshabunin ( 2015-07-28 04:07:03 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2015-07-27 09:23:24 -0600

Seen: 1,400 times

Last updated: Jul 27 '15