save from python for net surgery
authorEvan Shelhamer <shelhamer@imaginarynumber.net>
Mon, 26 May 2014 06:49:51 +0000 (23:49 -0700)
committerEvan Shelhamer <shelhamer@imaginarynumber.net>
Thu, 12 Jun 2014 21:27:36 +0000 (14:27 -0700)
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

index 9f190096ace94104d96e43aa1315f5fff81c032b..e9fe5cd3b052c7ef2fd9859c99e63cbd4891f283 100644 (file)
@@ -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_<CaffeBlob, CaffeBlobWrap>(
       "Blob", boost::python::no_init)