Skip to content
Open
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
22 changes: 18 additions & 4 deletions python/caffe/_caffe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@
#define PyArray_SetBaseObject(arr, x) (PyArray_BASE(arr) = (x))
#endif

/* Fix to avoid registration warnings in pycaffe (#3960) */
#define BP_REGISTER_SHARED_PTR_TO_PYTHON(PTR) do { \
const boost::python::type_info info = \
boost::python::type_id<shared_ptr<PTR > >(); \
const boost::python::converter::registration* reg = \
boost::python::converter::registry::query(info); \
if (reg == NULL) { \
bp::register_ptr_to_python<shared_ptr<PTR > >(); \
} else if ((*reg).m_to_python == NULL) { \
bp::register_ptr_to_python<shared_ptr<PTR > >(); \
} \
} while (0)

namespace bp = boost::python;

namespace caffe {
Expand Down Expand Up @@ -228,6 +241,7 @@ BOOST_PYTHON_MODULE(_caffe) {
.def("__init__", bp::make_constructor(&Net_Init_Load))
.def("_forward", &Net<Dtype>::ForwardFromTo)
.def("_backward", &Net<Dtype>::BackwardFromTo)
.def("_deconv", &Net<Dtype>::DeconvFromTo)
.def("reshape", &Net<Dtype>::Reshape)
// The cast is to select a particular overload.
.def("copy_from", static_cast<void (Net<Dtype>::*)(const string)>(
Expand Down Expand Up @@ -255,7 +269,7 @@ BOOST_PYTHON_MODULE(_caffe) {
.def("_set_input_arrays", &Net_SetInputArrays,
bp::with_custodian_and_ward<1, 2, bp::with_custodian_and_ward<1, 3> >())
.def("save", &Net_Save);
bp::register_ptr_to_python<shared_ptr<Net<Dtype> > >();
BP_REGISTER_SHARED_PTR_TO_PYTHON(Net<Dtype>);

bp::class_<Blob<Dtype>, shared_ptr<Blob<Dtype> >, boost::noncopyable>(
"Blob", bp::no_init)
Expand All @@ -275,7 +289,7 @@ BOOST_PYTHON_MODULE(_caffe) {
NdarrayCallPolicies()))
.add_property("diff", bp::make_function(&Blob<Dtype>::mutable_cpu_diff,
NdarrayCallPolicies()));
bp::register_ptr_to_python<shared_ptr<Blob<Dtype> > >();
BP_REGISTER_SHARED_PTR_TO_PYTHON(Blob<Dtype>);

bp::class_<Layer<Dtype>, shared_ptr<PythonLayer<Dtype> >,
boost::noncopyable>("Layer", bp::init<const LayerParameter&>())
Expand All @@ -284,7 +298,7 @@ BOOST_PYTHON_MODULE(_caffe) {
.def("setup", &Layer<Dtype>::LayerSetUp)
.def("reshape", &Layer<Dtype>::Reshape)
.add_property("type", bp::make_function(&Layer<Dtype>::type));
bp::register_ptr_to_python<shared_ptr<Layer<Dtype> > >();
BP_REGISTER_SHARED_PTR_TO_PYTHON(Layer<Dtype>);

bp::class_<LayerParameter>("LayerParameter", bp::no_init);

Expand All @@ -299,7 +313,7 @@ BOOST_PYTHON_MODULE(_caffe) {
.def("step", &Solver<Dtype>::Step)
.def("restore", &Solver<Dtype>::Restore)
.def("snapshot", &Solver<Dtype>::Snapshot);
bp::register_ptr_to_python<shared_ptr<Solver<Dtype> > >();
BP_REGISTER_SHARED_PTR_TO_PYTHON(Solver<Dtype>);

bp::class_<SGDSolver<Dtype>, bp::bases<Solver<Dtype> >,
shared_ptr<SGDSolver<Dtype> >, boost::noncopyable>(
Expand Down