pycaffe: add unary CaffeNet constructor for uninitialized nets
authorJonathan L Long <jonlong@cs.berkeley.edu>
Fri, 25 Apr 2014 21:50:36 +0000 (14:50 -0700)
committerJonathan L Long <jonlong@cs.berkeley.edu>
Fri, 25 Apr 2014 22:49:06 +0000 (15:49 -0700)
Note that parameters are uninitialized, not zero-filled.

python/caffe/_caffe.cpp

index 0e6bac2..a443f89 100644 (file)
@@ -134,17 +134,28 @@ class CaffeLayer {
 
 // A simple wrapper over CaffeNet that runs the forward process.
 struct CaffeNet {
+  // For cases where parameters will be determined later by the Python user,
+  // create a Net with unallocated parameters (which will not be zero-filled
+  // when accessed).
+  explicit CaffeNet(string param_file) {
+    Init(param_file);
+  }
+
   CaffeNet(string param_file, string pretrained_param_file) {
-    CheckFile(param_file);
+    Init(param_file);
     CheckFile(pretrained_param_file);
-
-    net_.reset(new Net<float>(param_file));
     net_->CopyTrainedLayersFrom(pretrained_param_file);
   }
 
   explicit CaffeNet(shared_ptr<Net<float> > net)
       : net_(net) {}
 
+  void Init(string param_file) {
+    CheckFile(param_file);
+    net_.reset(new Net<float>(param_file));
+  }
+
+
   virtual ~CaffeNet() {}
 
   inline void check_array_against_blob(
@@ -308,6 +319,7 @@ class CaffeSGDSolver {
 BOOST_PYTHON_MODULE(_caffe) {
   boost::python::class_<CaffeNet>(
       "Net", boost::python::init<string, string>())
+      .def(boost::python::init<string>())
       .def("Forward",          &CaffeNet::Forward)
       .def("ForwardPrefilled", &CaffeNet::ForwardPrefilled)
       .def("Backward",         &CaffeNet::Backward)