I have a problem with load trained SVM from file. I use Python and OpenCv 3.1.0. I create svm object by:
svm = cv2.ml.SVM_create()
Next, I train svm and save to file by:
svm.save('data.xml')
Now i want to load this file in other Python script. In docs i can't find any methods to do it.
Is there a trick to load svm from file? Thanks for any responses.
Use the OpenCV functions cv::ml::SVM::train to build a classifier based on SVMs and cv::ml::SVM::predict to test its performance. What is a SVM? A Support Vector Machine (SVM) is a discriminative classifier formally defined by a separating hyperplane.
After searching for a while I found out that if I have loaded the SVMs and I print the SVM type, kernel type and supportvectors that they are the same as in the SVM .xml file. So I think that the problem is that the prediction is not executed in the right way. I also don't know if I save my SVMs and load them in the appropriate way.
I think it's a litte bit confusing that there is no svm.load (filepath) method as a counterpart of svm.save (filepath), but when I read the module help it makes sense to me that SVM_load is a child of cv2.ml (sibling of SVM_create). Be sure that your opencv master branch is up-to-date (currently version 3.1.0-dev) SVM_create (...)
A Support Vector Machine (SVM) is a discriminative classifier formally defined by a separating hyperplane. In other words, given labeled training data ( supervised learning ), the algorithm outputs an optimal hyperplane which categorizes new examples.
I think it's a litte bit confusing that there is no svm.load(filepath) method as a counterpart of svm.save(filepath), but when I read the module help it makes sense to me that SVM_load is a child of cv2.ml (sibling of SVM_create).
Be sure that your opencv master branch is up-to-date (currently version 3.1.0-dev)
>>> import cv2
>>> cv2.__version__
'3.1.0-dev'
>>> help(cv2.ml)
returns
SVM_create(...)
SVM_create() -> retval
SVM_load(...)
SVM_load(filepath) -> retval
so you can simply use something like:
if not os.path.isfile('svm.dat'):
svm = cv2.ml.SVM_create()
...
svm.save('svm.dat')
else:
svm = cv2.ml.SVM_load('svm.dat')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With