Ask Your Question
1

Is there a way to save and restore trained neural network weights?

asked 2013-07-02 20:19:53 -0600

everitt gravatar image

http://docs.opencv.org/modules/ml/doc/neural_networks.html

I've been using artificial neural networks implemented in OpenCV, and it works well, but I just realized that the documentation doesn't explain a way to save and load trained weights. I'd like to do this so I don't have to train every time I start the program.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
5

answered 2013-07-03 04:10:25 -0600

berak gravatar image

updated 2013-07-03 04:13:13 -0600

no fear, it's possible:

CvANN_MLP mlp ;

mlp.train ( trainingData , trainingClasses );

cv::FileStorage fs("mlp.yml", cv::FileStorage::WRITE); // or xml
mlp.write(*fs, "mlp"); // don't think too much about the deref, it casts to a FileNode

mlp.load("mlp.yml","mlp");
edit flag offensive delete link more
1

answered 2013-07-03 03:43:14 -0600

The neural networks class uses the CvStatModel class underneath, as does every machine learning technique to create the classifiers. As seen here this class has the save functionality, which will create an xml structure with the correct paramaters.

For recalling these parameters the same class suggests to use the load functionality.

However, from docs I am thinking this may not be possible on the neural network, but you could simply try it out. If it doesn't work out, you could save the data manually to a text file, which is not that hard, since you can get the number of layers and retrieve the weights based on the layer index, as mentioned here. However, it is not suggested that you can use the same information to input data into a new created network. Check if the code supports setters for the corresponding getters. It might be possible that they are not documented.

If this is not the case, I suggest creating a issue ticket, suggesting to add this to the library, which could not be that hard. If you don't know how to, I will do it for you.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-07-02 20:19:53 -0600

Seen: 6,153 times

Last updated: Jul 03 '13