This commit is contained in:
Evan Shelhamer 2014-04-09 20:27:58 -07:00
Родитель 8da2a3209c
Коммит 872ddf3f81
1 изменённых файлов: 11 добавлений и 11 удалений

Просмотреть файл

@ -1,6 +1,6 @@
// Copyright 2014 BVLC and contributors. // Copyright 2014 BVLC and contributors.
// pycaffe provides a wrapper of the caffe::Net class as well as some // pycaffe provides a wrapper of the caffe::Net class as well as some
// caffe::Caffe functions so that one could easily call it from Python. // caffe::Caffe functions so that one could easily call it from python.
// Note that for python, we will simply use float as the data type. // Note that for python, we will simply use float as the data type.
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
@ -33,7 +33,7 @@ using boost::python::handle;
using boost::python::vector_indexing_suite; using boost::python::vector_indexing_suite;
// for convenience, check that input files can be opened, and raise an // for convenience, check that input files can be opened, and raise an
// exception that boost will send to Python if not (caffe could still crash // exception that boost will send to python if not (caffe could still crash
// later if the input files are disturbed before they are actually used, but // later if the input files are disturbed before they are actually used, but
// this saves frustration in most cases) // this saves frustration in most cases)
static void CheckFile(const string& filename) { static void CheckFile(const string& filename) {
@ -46,7 +46,7 @@ static void CheckFile(const string& filename) {
} }
// wrap shared_ptr<Blob<float> > in a class that we construct in C++ and pass // wrap shared_ptr<Blob<float> > in a class that we construct in C++ and pass
// to Python // to python
class CaffeBlob { class CaffeBlob {
public: public:
CaffeBlob(const shared_ptr<Blob<float> > &blob, const string& name) CaffeBlob(const shared_ptr<Blob<float> > &blob, const string& name)
@ -70,9 +70,9 @@ class CaffeBlob {
}; };
// we need another wrapper (used as boost::python's HeldType) that receives a // We need another wrapper (used as boost::python's HeldType) that receives a
// self PyObject * which we can use as ndarray.base, so that data/diff memory // self PyObject * which we can use as ndarray.base, so that data/diff memory
// is not freed while still being used in Python // is not freed while still being used in python.
class CaffeBlobWrap : public CaffeBlob { class CaffeBlobWrap : public CaffeBlob {
public: public:
CaffeBlobWrap(PyObject *p, const CaffeBlob &blob) CaffeBlobWrap(PyObject *p, const CaffeBlob &blob)
@ -142,8 +142,9 @@ struct CaffeNet {
} }
CaffeNet(string param_file, string pretrained_param_file) { CaffeNet(string param_file, string pretrained_param_file) {
Init(param_file); CheckFile(param_file);
CheckFile(pretrained_param_file); CheckFile(pretrained_param_file);
net_.reset(new Net<float>(param_file));
net_->CopyTrainedLayersFrom(pretrained_param_file); net_->CopyTrainedLayersFrom(pretrained_param_file);
} }
@ -158,8 +159,8 @@ struct CaffeNet {
virtual ~CaffeNet() {} virtual ~CaffeNet() {}
// this function is mostly redundant with the one below, but should go away // Check that an array is acceptable for blob assignment
// with new pycaffe // as described in the preface to Forward().
inline void check_array_against_blob( inline void check_array_against_blob(
PyArrayObject* arr, Blob<float>* blob) { PyArrayObject* arr, Blob<float>* blob) {
CHECK(PyArray_FLAGS(arr) & NPY_ARRAY_C_CONTIGUOUS); CHECK(PyArray_FLAGS(arr) & NPY_ARRAY_C_CONTIGUOUS);
@ -197,8 +198,7 @@ struct CaffeNet {
// The actual forward function. It takes in a python list of numpy arrays as // The actual forward function. It takes in a python list of numpy arrays as
// input and a python list of numpy arrays as output. The input and output // input and a python list of numpy arrays as output. The input and output
// should all have correct shapes, are single-precisionabcdnt- and // should all have correct shapes, be single-precision, and be C-contiguous.
// c contiguous.
void Forward(list bottom, list top) { void Forward(list bottom, list top) {
vector<Blob<float>*>& input_blobs = net_->input_blobs(); vector<Blob<float>*>& input_blobs = net_->input_blobs();
CHECK_EQ(len(bottom), input_blobs.size()); CHECK_EQ(len(bottom), input_blobs.size());