[pycaffe] use bp::object instead of PyObject* for self in Python layer
authorTakuya Narihira <dvbscb@gmail.com>
Thu, 14 May 2015 04:16:28 +0000 (21:16 -0700)
committerJonathan L Long <jonlong@cs.berkeley.edu>
Thu, 14 May 2015 04:18:47 +0000 (21:18 -0700)
This simply allows direct use of the nicer bp::object interface.

include/caffe/python_layer.hpp

index 816ef45..19cf18c 100644 (file)
@@ -14,12 +14,12 @@ template <typename Dtype>
 class PythonLayer : public Layer<Dtype> {
  public:
   PythonLayer(PyObject* self, const LayerParameter& param)
-      : Layer<Dtype>(param), self_(self) { }
+      : Layer<Dtype>(param), self_(bp::handle<>(bp::borrowed(self))) { }
 
   virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
       const vector<Blob<Dtype>*>& top) {
     try {
-      bp::call_method<bp::object>(self_, "setup", bottom, top);
+      self_.attr("setup")(bottom, top);
     } catch (bp::error_already_set) {
       PyErr_Print();
       throw;
@@ -29,7 +29,7 @@ class PythonLayer : public Layer<Dtype> {
   virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
       const vector<Blob<Dtype>*>& top) {
     try {
-      bp::call_method<bp::object>(self_, "reshape", bottom, top);
+      self_.attr("reshape")(bottom, top);
     } catch (bp::error_already_set) {
       PyErr_Print();
       throw;
@@ -42,7 +42,7 @@ class PythonLayer : public Layer<Dtype> {
   virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
       const vector<Blob<Dtype>*>& top) {
     try {
-      bp::call_method<bp::object>(self_, "forward", bottom, top);
+      self_.attr("forward")(bottom, top);
     } catch (bp::error_already_set) {
       PyErr_Print();
       throw;
@@ -51,8 +51,7 @@ class PythonLayer : public Layer<Dtype> {
   virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
       const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
     try {
-      bp::call_method<bp::object>(self_, "backward", top, propagate_down,
-          bottom);
+      self_.attr("backward")(top, propagate_down, bottom);
     } catch (bp::error_already_set) {
       PyErr_Print();
       throw;
@@ -60,7 +59,7 @@ class PythonLayer : public Layer<Dtype> {
   }
 
  private:
-  PyObject* self_;
+  bp::object self_;
 };
 
 }  // namespace caffe