diff --git a/python/caffe/_caffe.cpp b/python/caffe/_caffe.cpp index 137cc283571..fdac483f0b6 100644 --- a/python/caffe/_caffe.cpp +++ b/python/caffe/_caffe.cpp @@ -287,7 +287,7 @@ struct CaffeNet { // The boost python module definition. BOOST_PYTHON_MODULE(_caffe) { boost::python::class_( - "CaffeNet", boost::python::init()) + "Net", boost::python::init()) .def("Forward", &CaffeNet::Forward) .def("ForwardPrefilled", &CaffeNet::ForwardPrefilled) .def("Backward", &CaffeNet::Backward) @@ -296,11 +296,12 @@ BOOST_PYTHON_MODULE(_caffe) { .def("set_phase_train", &CaffeNet::set_phase_train) .def("set_phase_test", &CaffeNet::set_phase_test) .def("set_device", &CaffeNet::set_device) - .add_property("blobs", &CaffeNet::blobs) + // rename blobs here since the pycaffe.py wrapper will replace it + .add_property("_blobs", &CaffeNet::blobs) .add_property("layers", &CaffeNet::layers); boost::python::class_( - "CaffeBlob", boost::python::no_init) + "Blob", boost::python::no_init) .add_property("name", &CaffeBlob::name) .add_property("num", &CaffeBlob::num) .add_property("channels", &CaffeBlob::channels) @@ -311,7 +312,7 @@ BOOST_PYTHON_MODULE(_caffe) { .add_property("diff", &CaffeBlobWrap::get_diff); boost::python::class_( - "CaffeLayer", boost::python::no_init) + "Layer", boost::python::no_init) .add_property("name", &CaffeLayer::name) .add_property("blobs", &CaffeLayer::blobs); diff --git a/python/caffe/pycaffe.py b/python/caffe/pycaffe.py index 8fbbcf9aac9..093e7603874 100644 --- a/python/caffe/pycaffe.py +++ b/python/caffe/pycaffe.py @@ -6,23 +6,28 @@ from ._caffe import CaffeNet from collections import OrderedDict -class Net(CaffeNet): +# we directly update methods from Net here (rather than using composition or +# inheritance) so that nets created by caffe (e.g., by SGDSolver) will +# automatically have the improved interface + +@property +def _Net_blobs(self): + """ + An OrderedDict (bottom to top, i.e., input to output) of network + blobs indexed by name + """ + return OrderedDict([(bl.name, bl) for bl in self._blobs]) + +Net.blobs = _Net_blobs + +@property +def _Net_params(self): """ - The direct Python interface to caffe, exposing Forward and Backward - passes, data, gradients, and layer parameters + An OrderedDict (bottom to top, i.e., input to output) of network + parameters indexed by name; each is a list of multiple blobs (e.g., + weights and biases) """ - def __init__(self, param_file, pretrained_param_file): - super(Net, self).__init__(param_file, pretrained_param_file) - self._blobs = OrderedDict([(bl.name, bl) - for bl in super(Net, self).blobs]) - self.params = OrderedDict([(lr.name, lr.blobs) - for lr in super(Net, self).layers - if len(lr.blobs) > 0]) + return OrderedDict([(lr.name, lr.blobs) for lr in self.layers + if len(lr.blobs) > 0]) - @property - def blobs(self): - """ - An OrderedDict (bottom to top, i.e., input to output) of network - blobs indexed by name - """ - return self._blobs +Net.params = _Net_params