How do I save and upload a random forest once I have trained it?

asked 2015-06-20 21:36:24 -0600

I used OpenCV code to train a random forest classifier. But I cannot find any documentation on how to save and upload it to use. I don't want to have to retrain it each time. Does anyone have the code for doing this?

edit retag flag offensive close merge delete

Comments

  • in opencv2.4, all ml classes inherit from StatModel, so you can use load() and save() to xml/yml files.

  • in opencv3, there is a slight difference - load() will return a new object

berak gravatar imageberak ( 2015-06-21 01:03:25 -0600 )edit

Yes, I am using load() and save(), but how do I use the CvMLData that I get when I load the classifier? I am looking for code that does the job. I have the code to train it and then test it, but I don't see any method for using it for test data once I have loaded a pre-trained model. I have tried to use predict, but it is expecting something else besides a CvMLData.

yvshashoua gravatar imageyvshashoua ( 2015-06-22 20:47:40 -0600 )edit

It baffles me that there is easy, clear sample code in the OpenCV textbook for training and testing a random forest classifier, but there is nothing for just loading a trained classifier and using it, which should be even easier to do. I have gone through the letter recognition example, but they are rewriting the entire read .csv function. There must be an easy way to do it.

yvshashoua gravatar imageyvshashoua ( 2015-06-23 05:15:26 -0600 )edit

hmm, i'm confused.

CvMlData is for getting the traindata into the classifier for training, right ? once you trained your forest, you save the trained classifier, and load it later for prediction.

imho, you do not need CvMlData at that stage at all.

(also, bear with me, not too familiar with 2.4 ml classes anymore...)

berak gravatar imageberak ( 2015-06-23 06:02:10 -0600 )edit

What I mean is that when I am loading a classifier that I already trained and saved, using the load function returns a CvMLData, but using predict requires a CvMat instead. I have tried all kinds of ways of getting this to work, but nothing works.

yvshashoua gravatar imageyvshashoua ( 2015-06-24 20:13:45 -0600 )edit