Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python wrapper fixes and re-arrangement #291

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions python/caffe/_caffe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ struct CaffeNet {
// The boost python module definition.
BOOST_PYTHON_MODULE(_caffe) {
boost::python::class_<CaffeNet>(
"CaffeNet", boost::python::init<string, string>())
"Net", boost::python::init<string, string>())
.def("Forward", &CaffeNet::Forward)
.def("ForwardPrefilled", &CaffeNet::ForwardPrefilled)
.def("Backward", &CaffeNet::Backward)
Expand All @@ -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, CaffeBlobWrap>(
"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)
Expand All @@ -311,7 +312,7 @@ BOOST_PYTHON_MODULE(_caffe) {
.add_property("diff", &CaffeBlobWrap::get_diff);

boost::python::class_<CaffeLayer>(
"CaffeLayer", boost::python::no_init)
"Layer", boost::python::no_init)
.add_property("name", &CaffeLayer::name)
.add_property("blobs", &CaffeLayer::blobs);

Expand Down
39 changes: 22 additions & 17 deletions python/caffe/pycaffe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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