Ask Your Question
3

KNearest save problem

asked 2012-08-08 07:15:58 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >50 is welcome to improve it.

I realized that knn.save() and knn.load() function is not implemented yet. I googled so much about that couldn't find any solution for that.

If there is a solution i will be happy to know that. Any help would be appreciated.

edit retag flag offensive close merge delete

Comments

Can you clarify what knn.save() and knn.load() are supposed to do? Is the idea to save and load a trained model that would be produced by CvKNearest::train()?

martyychang gravatar imagemartyychang ( 2016-03-23 19:12:49 -0600 )edit

2 answers

Sort by » oldest newest most voted
1

answered 2016-12-20 11:44:27 -0600

updated 2016-12-20 11:45:35 -0600

kNN does not save a model because there is no model to save. The model is the complete data. It classifies new samples by going through the raw data and verifying the k closest samples and using the classes as votes.

I agree that it shouldn't provide the method if it is redundant.

edit flag offensive delete link more
0

answered 2018-06-10 13:13:22 -0600

I use:

knn.save('knn.yml')

to save the model

And:

fs = cv2.FileStorage('knn.yml', cv2.FILE_STORAGE_READ)
knn_yml = fs.getNode('opencv_ml_knn')

knn_format    = knn_yml.getNode('format').real()
is_classifier = knn_yml.getNode('is_classifier').real()
default_k     = knn_yml.getNode('default_k').real()
samples       = knn_yml.getNode('samples').mat()
responses     = knn_yml.getNode('responses').mat()
fs.release
knn.train(samples,cv2.ml.ROW_SAMPLE,responses)

to recover it. I hope be of some help

edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-08-08 07:15:58 -0600

Seen: 1,581 times

Last updated: Jun 10 '18