From 7aad48b9e0d24435cc24dc0e680e558d993522dc Mon Sep 17 00:00:00 2001 From: barrierye Date: Mon, 18 May 2020 12:46:52 +0800 Subject: [PATCH] try to get numpy array from ModelRes --- core/general-client/include/general_model.h | 14 +++++++++++ .../src/pybind_general_model.cpp | 24 ++++++++++++++----- python/paddle_serving_client/__init__.py | 6 ++--- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/core/general-client/include/general_model.h b/core/general-client/include/general_model.h index 7e04ae11..8a4c5e8c 100644 --- a/core/general-client/include/general_model.h +++ b/core/general-client/include/general_model.h @@ -69,9 +69,15 @@ class ModelRes { const std::vector& get_int64_by_name(const std::string& name) { return _int64_value_map[name]; } + std::vector&& get_int64_by_name_with_rv(const std::string& name) { + return std::move(_int64_value_map[name]); + } const std::vector& get_float_by_name(const std::string& name) { return _float_value_map[name]; } + std::vector&& get_float_by_name_with_rv(const std::string& name) { + return std::move(_float_value_map[name]); + } const std::vector& get_shape(const std::string& name) { return _shape_map[name]; } @@ -121,10 +127,18 @@ class PredictorRes { const std::string& name) { return _models[model_idx].get_int64_by_name(name); } + std::vector&& get_int64_by_name_with_rv(const int model_idx, + const std::string& name) { + return std::move(_models[model_idx].get_int64_by_name_with_rv(name)); + } const std::vector& get_float_by_name(const int model_idx, const std::string& name) { return _models[model_idx].get_float_by_name(name); } + std::vector&& get_float_by_name_with_rv(const int model_idx, + const std::string& name) { + return std::move(_models[model_idx].get_float_by_name_with_rv(name)); + } const std::vector& get_shape(const int model_idx, const std::string& name) { return _models[model_idx].get_shape(name); diff --git a/core/general-client/src/pybind_general_model.cpp b/core/general-client/src/pybind_general_model.cpp index b0d1d2d6..8c59357a 100644 --- a/core/general-client/src/pybind_general_model.cpp +++ b/core/general-client/src/pybind_general_model.cpp @@ -32,14 +32,26 @@ PYBIND11_MODULE(serving_client, m) { .def(py::init()) .def("get_int64_by_name", [](PredictorRes &self, int model_idx, std::string &name) { - return self.get_int64_by_name(model_idx, name); - }, - py::return_value_policy::reference) + // https://github.com/pybind/pybind11/issues/1042 + std::vector *ptr = new std::vector( + std::move(self.get_int64_by_name_with_rv(model_idx, name))); + auto capsule = py::capsule(ptr, [](void *p) { + delete reinterpret_cast *>(p); + }); + return py::array(ptr->size(), ptr->data(), capsule); + }) .def("get_float_by_name", [](PredictorRes &self, int model_idx, std::string &name) { - return self.get_float_by_name(model_idx, name); - }, - py::return_value_policy::reference) + std::vector *ptr = new std::vector( + std::move(self.get_float_by_name_with_rv(model_idx, name))); + auto capsule = py::capsule(ptr, [](void *p) { + delete reinterpret_cast *>(p); + }); + return py::array(ptr->size(), ptr->data(), capsule); + // return self.get_float_by_name(model_idx, name); + }) + //, + // py::return_value_policy::reference) .def("get_shape", [](PredictorRes &self, int model_idx, std::string &name) { return self.get_shape(model_idx, name); diff --git a/python/paddle_serving_client/__init__.py b/python/paddle_serving_client/__init__.py index b68fb8f9..95d74303 100644 --- a/python/paddle_serving_client/__init__.py +++ b/python/paddle_serving_client/__init__.py @@ -319,7 +319,7 @@ class Client(object): if self.fetch_names_to_type_[name] == int_type: result_map[name] = result_batch.get_int64_by_name(mi, name) shape = result_batch.get_shape(mi, name) - result_map[name] = np.array(result_map[name], dtype='int64') + # result_map[name] = np.array(result_map[name], dtype='int64') result_map[name].shape = shape if name in self.lod_tensor_set: result_map["{}.lod".format(name)] = np.array( @@ -327,8 +327,8 @@ class Client(object): elif self.fetch_names_to_type_[name] == float_type: result_map[name] = result_batch.get_float_by_name(mi, name) shape = result_batch.get_shape(mi, name) - result_map[name] = np.array( - result_map[name], dtype='float32') + # result_map[name] = np.array( + # result_map[name], dtype='float32') result_map[name].shape = shape if name in self.lod_tensor_set: result_map["{}.lod".format(name)] = np.array( -- GitLab