forked from BVLC/caffe
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request BVLC#1703 from longjon/pyreformation
Reform the boost::python wrapper, including layers implemented in Python
- Loading branch information
Showing
18 changed files
with
405 additions
and
369 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#ifndef CAFFE_PYTHON_LAYER_HPP_ | ||
#define CAFFE_PYTHON_LAYER_HPP_ | ||
|
||
#include <boost/python.hpp> | ||
#include <vector> | ||
|
||
#include "caffe/layer.hpp" | ||
|
||
namespace bp = boost::python; | ||
|
||
namespace caffe { | ||
|
||
template <typename Dtype> | ||
class PythonLayer : public Layer<Dtype> { | ||
public: | ||
PythonLayer(PyObject* self, const LayerParameter& param) | ||
: Layer<Dtype>(param), self_(self) { } | ||
|
||
virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom, | ||
vector<Blob<Dtype>*>* top) { | ||
try { | ||
bp::call_method<bp::object>(self_, "setup", bottom, top); | ||
} catch (bp::error_already_set) { | ||
PyErr_Print(); | ||
throw; | ||
} | ||
} | ||
|
||
virtual void Reshape(const vector<Blob<Dtype>*>& bottom, | ||
const vector<Blob<Dtype>*>& top) { | ||
try { | ||
bp::call_method<bp::object>(self_, "reshape", bottom, top); | ||
} catch (bp::error_already_set) { | ||
PyErr_Print(); | ||
throw; | ||
} | ||
} | ||
|
||
virtual inline const char* type() const { return "Python"; } | ||
|
||
protected: | ||
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom, | ||
const vector<Blob<Dtype>*>& top) { | ||
try { | ||
bp::call_method<bp::object>(self_, "forward", bottom, top); | ||
} catch (bp::error_already_set) { | ||
PyErr_Print(); | ||
throw; | ||
} | ||
} | ||
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); | ||
} catch (bp::error_already_set) { | ||
PyErr_Print(); | ||
throw; | ||
} | ||
} | ||
|
||
private: | ||
PyObject* self_; | ||
}; | ||
|
||
} // namespace caffe | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
from .pycaffe import Net, SGDSolver | ||
from ._caffe import set_mode_cpu, set_mode_gpu, set_device, \ | ||
set_phase_train, set_phase_test | ||
set_phase_train, set_phase_test, Layer, get_solver | ||
from .classifier import Classifier | ||
from .detector import Detector | ||
import io |
Oops, something went wrong.