From 6a5b2c19c75dd9f6965ca9a377164bf8b6c96688 Mon Sep 17 00:00:00 2001 From: Evan Shelhamer Date: Sun, 25 May 2014 23:49:51 -0700 Subject: [PATCH] save from python for net surgery 0. Scheme desired parameters. 1. Do surgery on the net through `net.params['name'][idx].data[...] = `. 2. Save post-operation net params by `net.save('fname')`. Handwoven deep nets, anyone? --- python/caffe/_caffe.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/python/caffe/_caffe.cpp b/python/caffe/_caffe.cpp index 9f19009..e9fe5cd 100644 --- a/python/caffe/_caffe.cpp +++ b/python/caffe/_caffe.cpp @@ -224,6 +224,13 @@ struct CaffeNet { PyArray_DIMS(data_arr)[0]); } + // save the network weights to binary proto for net surgeries. + void save(string filename) { + NetParameter net_param; + net_->ToProto(&net_param, false); + WriteProtoToBinaryFile(net_param, filename.c_str()); + } + // The caffe::Caffe utility functions. void set_mode_cpu() { Caffe::set_mode(Caffe::CPU); } void set_mode_gpu() { Caffe::set_mode(Caffe::GPU); } @@ -315,7 +322,8 @@ BOOST_PYTHON_MODULE(_caffe) { .add_property("layers", &CaffeNet::layers) .add_property("inputs", &CaffeNet::inputs) .add_property("outputs", &CaffeNet::outputs) - .def("_set_input_arrays", &CaffeNet::set_input_arrays); + .def("_set_input_arrays", &CaffeNet::set_input_arrays) + .def("save", &CaffeNet::save); boost::python::class_( "Blob", boost::python::no_init) -- 2.7.4