From 8a6116b9145eb626a75d06935749d2832f8eb585 Mon Sep 17 00:00:00 2001 From: jiangjiajun Date: Mon, 8 Aug 2022 15:31:53 +0000 Subject: [PATCH 1/6] modify yolov7 and visualize functions --- csrcs/fastdeploy/pybind/main.cc.in | 11 ++ csrcs/fastdeploy/pybind/main.h | 12 +- csrcs/fastdeploy/vision.h | 6 +- .../fastdeploy/vision/common/processors/mat.h | 6 +- .../contrib}/scaledyolov4.cc | 27 +--- .../contrib}/scaledyolov4.h | 6 +- .../detection/contrib/scaledyolov4_pybind.cc | 41 +++++++ .../contrib}/yolor.cc | 26 +--- .../{wongkinyiu => detection/contrib}/yolor.h | 6 +- .../vision/detection/contrib/yolor_pybind.cc | 37 ++++++ .../contrib}/yolov7.cc | 27 +--- .../contrib}/yolov7.h | 8 +- .../vision/detection/contrib/yolov7_pybind.cc | 37 ++++++ .../vision/detection/detection_pybind.cc | 30 +++++ csrcs/fastdeploy/vision/vision_pybind.cc | 6 +- .../fastdeploy/vision/visualize/detection.cc | 17 +-- .../vision/visualize/face_detection.cc | 21 ++-- .../vision/visualize/matting_alpha.cc | 22 ++-- .../vision/visualize/segmentation.cc | 18 +-- csrcs/fastdeploy/vision/visualize/visualize.h | 19 ++- .../vision/visualize/visualize_pybind.cc | 43 ++++--- .../vision/wongkinyiu/wongkinyiu_pybind.cc | 79 ------------ fastdeploy/vision/__init__.py | 6 +- fastdeploy/vision/detection/__init__.py | 18 +++ fastdeploy/vision/detection/scaled_yolov4.py | 116 ++++++++++++++++++ fastdeploy/vision/detection/yolor.py | 116 ++++++++++++++++++ fastdeploy/vision/detection/yolov7.py | 116 ++++++++++++++++++ fastdeploy/vision/visualize/__init__.py | 21 ++-- model_zoo/vision/retinaface/retinaface.py | 4 +- 29 files changed, 653 insertions(+), 249 deletions(-) rename csrcs/fastdeploy/vision/{wongkinyiu => detection/contrib}/scaledyolov4.cc (94%) rename csrcs/fastdeploy/vision/{wongkinyiu => detection/contrib}/scaledyolov4.h (97%) create mode 100644 csrcs/fastdeploy/vision/detection/contrib/scaledyolov4_pybind.cc rename csrcs/fastdeploy/vision/{wongkinyiu => detection/contrib}/yolor.cc (94%) rename csrcs/fastdeploy/vision/{wongkinyiu => detection/contrib}/yolor.h (97%) create mode 100644 csrcs/fastdeploy/vision/detection/contrib/yolor_pybind.cc rename csrcs/fastdeploy/vision/{wongkinyiu => detection/contrib}/yolov7.cc (94%) rename csrcs/fastdeploy/vision/{wongkinyiu => detection/contrib}/yolov7.h (93%) create mode 100644 csrcs/fastdeploy/vision/detection/contrib/yolov7_pybind.cc create mode 100644 csrcs/fastdeploy/vision/detection/detection_pybind.cc delete mode 100644 csrcs/fastdeploy/vision/wongkinyiu/wongkinyiu_pybind.cc create mode 100644 fastdeploy/vision/detection/__init__.py create mode 100644 fastdeploy/vision/detection/scaled_yolov4.py create mode 100644 fastdeploy/vision/detection/yolor.py create mode 100644 fastdeploy/vision/detection/yolov7.py diff --git a/csrcs/fastdeploy/pybind/main.cc.in b/csrcs/fastdeploy/pybind/main.cc.in index 622c2c82b62..13e0a31c4f7 100644 --- a/csrcs/fastdeploy/pybind/main.cc.in +++ b/csrcs/fastdeploy/pybind/main.cc.in @@ -30,6 +30,8 @@ pybind11::dtype FDDataTypeToNumpyDataType(const FDDataType& fd_dtype) { dt = pybind11::dtype::of(); } else if (fd_dtype == FDDataType::FP64) { dt = pybind11::dtype::of(); + } else if (fd_dtype == FDDataType::UINT8) { + dt = pybind11::dtype::of(); } else { FDASSERT(false, "The function doesn't support data type of " + Str(fd_dtype) + "."); @@ -46,6 +48,8 @@ FDDataType NumpyDataTypeToFDDataType(const pybind11::dtype& np_dtype) { return FDDataType::FP32; } else if (np_dtype.is(pybind11::dtype::of())) { return FDDataType::FP64; + } else if (np_dtype.is(pybind11::dtype::of())) { + return FDDataType::UINT8; } FDASSERT(false, "NumpyDataTypeToFDDataType() only support " @@ -66,6 +70,13 @@ void PyArrayToTensor(pybind11::array& pyarray, FDTensor* tensor, } } +pybind11::array TensorToPyArray(const FDTensor& tensor) { + auto numpy_dtype = FDDataTypeToNumpyDataType(tensor.dtype); + auto out = pybind11::array(numpy_dtype, tensor.shape); + memcpy(out.mutable_data(), tensor.Data(), tensor.Numel() * FDDataTypeSize(tensor.dtype)); + return out; +} + #ifdef ENABLE_VISION int NumpyDataTypeToOpenCvType(const pybind11::dtype& np_dtype) { if (np_dtype.is(pybind11::dtype::of())) { diff --git a/csrcs/fastdeploy/pybind/main.h b/csrcs/fastdeploy/pybind/main.h index 122cb7c2b1f..23f0eccc29d 100644 --- a/csrcs/fastdeploy/pybind/main.h +++ b/csrcs/fastdeploy/pybind/main.h @@ -36,12 +36,14 @@ FDDataType NumpyDataTypeToFDDataType(const pybind11::dtype& np_dtype); void PyArrayToTensor(pybind11::array& pyarray, FDTensor* tensor, bool share_buffer = false); +pybind11::array TensorToPyArray(const FDTensor& tensor); #ifdef ENABLE_VISION cv::Mat PyArrayToCvMat(pybind11::array& pyarray); #endif -template FDDataType CTypeToFDDataType() { +template +FDDataType CTypeToFDDataType() { if (std::is_same::value) { return FDDataType::INT32; } else if (std::is_same::value) { @@ -57,9 +59,9 @@ template FDDataType CTypeToFDDataType() { } template -std::vector -PyBackendInfer(T& self, const std::vector& names, - std::vector& data) { +std::vector PyBackendInfer( + T& self, const std::vector& names, + std::vector& data) { std::vector inputs(data.size()); for (size_t i = 0; i < data.size(); ++i) { // TODO(jiangjiajun) here is considered to use user memory directly @@ -85,4 +87,4 @@ PyBackendInfer(T& self, const std::vector& names, return results; } -} // namespace fastdeploy +} // namespace fastdeploy diff --git a/csrcs/fastdeploy/vision.h b/csrcs/fastdeploy/vision.h index 5670c1f2b69..376aaaf28ae 100644 --- a/csrcs/fastdeploy/vision.h +++ b/csrcs/fastdeploy/vision.h @@ -23,6 +23,9 @@ #include "fastdeploy/vision/deepinsight/partial_fc.h" #include "fastdeploy/vision/deepinsight/scrfd.h" #include "fastdeploy/vision/deepinsight/vpl.h" +#include "fastdeploy/vision/detection/contrib/scaledyolov4.h" +#include "fastdeploy/vision/detection/contrib/yolor.h" +#include "fastdeploy/vision/detection/contrib/yolov7.h" #include "fastdeploy/vision/linzaer/ultraface.h" #include "fastdeploy/vision/megvii/yolox.h" #include "fastdeploy/vision/meituan/yolov6.h" @@ -32,9 +35,6 @@ #include "fastdeploy/vision/ppseg/model.h" #include "fastdeploy/vision/rangilyu/nanodet_plus.h" #include "fastdeploy/vision/ultralytics/yolov5.h" -#include "fastdeploy/vision/wongkinyiu/scaledyolov4.h" -#include "fastdeploy/vision/wongkinyiu/yolor.h" -#include "fastdeploy/vision/wongkinyiu/yolov7.h" #include "fastdeploy/vision/zhkkke/modnet.h" #endif diff --git a/csrcs/fastdeploy/vision/common/processors/mat.h b/csrcs/fastdeploy/vision/common/processors/mat.h index 616a4aabee2..cf4736238cf 100644 --- a/csrcs/fastdeploy/vision/common/processors/mat.h +++ b/csrcs/fastdeploy/vision/common/processors/mat.h @@ -27,7 +27,7 @@ namespace vision { enum Layout { HWC, CHW }; -struct Mat { +struct FASTDEPLOY_DECL Mat { explicit Mat(cv::Mat& mat) { cpu_mat = mat; device = Device::CPU; @@ -76,5 +76,5 @@ struct Mat { Device device = Device::CPU; }; -} // namespace vision -} // namespace fastdeploy +} // namespace vision +} // namespace fastdeploy diff --git a/csrcs/fastdeploy/vision/wongkinyiu/scaledyolov4.cc b/csrcs/fastdeploy/vision/detection/contrib/scaledyolov4.cc similarity index 94% rename from csrcs/fastdeploy/vision/wongkinyiu/scaledyolov4.cc rename to csrcs/fastdeploy/vision/detection/contrib/scaledyolov4.cc index a562c9b2755..dff2118f32e 100644 --- a/csrcs/fastdeploy/vision/wongkinyiu/scaledyolov4.cc +++ b/csrcs/fastdeploy/vision/detection/contrib/scaledyolov4.cc @@ -12,13 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "fastdeploy/vision/wongkinyiu/scaledyolov4.h" +#include "fastdeploy/vision/detection/contrib/scaledyolov4.h" #include "fastdeploy/utils/perf.h" #include "fastdeploy/vision/utils/utils.h" namespace fastdeploy { namespace vision { -namespace wongkinyiu { +namespace detection { void ScaledYOLOv4::LetterBox(Mat* mat, const std::vector& size, const std::vector& color, bool _auto, @@ -65,8 +65,8 @@ ScaledYOLOv4::ScaledYOLOv4(const std::string& model_file, valid_cpu_backends = {Backend::ORT}; // 指定可用的CPU后端 valid_gpu_backends = {Backend::ORT, Backend::TRT}; // 指定可用的GPU后端 } else { - valid_cpu_backends = {Backend::PDINFER, Backend::ORT}; - valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT}; + valid_cpu_backends = {Backend::PDINFER}; + valid_gpu_backends = {Backend::PDINFER}; } runtime_option = custom_option; runtime_option.model_format = model_format; @@ -219,10 +219,6 @@ bool ScaledYOLOv4::Postprocess( bool ScaledYOLOv4::Predict(cv::Mat* im, DetectionResult* result, float conf_threshold, float nms_iou_threshold) { -#ifdef FASTDEPLOY_DEBUG - TIMERECORD_START(0) -#endif - Mat mat(*im); std::vector input_tensors(1); @@ -239,34 +235,21 @@ bool ScaledYOLOv4::Predict(cv::Mat* im, DetectionResult* result, return false; } -#ifdef FASTDEPLOY_DEBUG - TIMERECORD_END(0, "Preprocess") - TIMERECORD_START(1) -#endif - input_tensors[0].name = InputInfoOfRuntime(0).name; std::vector output_tensors; if (!Infer(input_tensors, &output_tensors)) { FDERROR << "Failed to inference." << std::endl; return false; } -#ifdef FASTDEPLOY_DEBUG - TIMERECORD_END(1, "Inference") - TIMERECORD_START(2) -#endif - if (!Postprocess(output_tensors[0], result, im_info, conf_threshold, nms_iou_threshold)) { FDERROR << "Failed to post process." << std::endl; return false; } -#ifdef FASTDEPLOY_DEBUG - TIMERECORD_END(2, "Postprocess") -#endif return true; } -} // namespace wongkinyiu +} // namespace detection } // namespace vision } // namespace fastdeploy diff --git a/csrcs/fastdeploy/vision/wongkinyiu/scaledyolov4.h b/csrcs/fastdeploy/vision/detection/contrib/scaledyolov4.h similarity index 97% rename from csrcs/fastdeploy/vision/wongkinyiu/scaledyolov4.h rename to csrcs/fastdeploy/vision/detection/contrib/scaledyolov4.h index c85b58d4c24..bb7ff0a2831 100644 --- a/csrcs/fastdeploy/vision/wongkinyiu/scaledyolov4.h +++ b/csrcs/fastdeploy/vision/detection/contrib/scaledyolov4.h @@ -19,7 +19,7 @@ namespace fastdeploy { namespace vision { -namespace wongkinyiu { +namespace detection { class FASTDEPLOY_DECL ScaledYOLOv4 : public FastDeployModel { public: @@ -31,7 +31,7 @@ class FASTDEPLOY_DECL ScaledYOLOv4 : public FastDeployModel { const Frontend& model_format = Frontend::ONNX); // 定义模型的名称 - virtual std::string ModelName() const { return "WongKinYiu/ScaledYOLOv4"; } + virtual std::string ModelName() const { return "ScaledYOLOv4"; } // 模型预测接口,即用户调用的接口 // im 为用户的输入数据,目前对于CV均定义为cv::Mat @@ -98,6 +98,6 @@ class FASTDEPLOY_DECL ScaledYOLOv4 : public FastDeployModel { // auto check by fastdeploy after the internal Runtime already initialized. bool is_dynamic_input_; }; -} // namespace wongkinyiu +} // namespace detection } // namespace vision } // namespace fastdeploy diff --git a/csrcs/fastdeploy/vision/detection/contrib/scaledyolov4_pybind.cc b/csrcs/fastdeploy/vision/detection/contrib/scaledyolov4_pybind.cc new file mode 100644 index 00000000000..3e8e43b9e35 --- /dev/null +++ b/csrcs/fastdeploy/vision/detection/contrib/scaledyolov4_pybind.cc @@ -0,0 +1,41 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "fastdeploy/pybind/main.h" + +namespace fastdeploy { +void BindScaledYOLOv4(pybind11::module& m) { + pybind11::class_( + m, "ScaledYOLOv4") + .def(pybind11::init()) + .def("predict", + [](vision::detection::ScaledYOLOv4& self, pybind11::array& data, + float conf_threshold, float nms_iou_threshold) { + auto mat = PyArrayToCvMat(data); + vision::DetectionResult res; + self.Predict(&mat, &res, conf_threshold, nms_iou_threshold); + return res; + }) + .def_readwrite("size", &vision::detection::ScaledYOLOv4::size) + .def_readwrite("padding_value", + &vision::detection::ScaledYOLOv4::padding_value) + .def_readwrite("is_mini_pad", + &vision::detection::ScaledYOLOv4::is_mini_pad) + .def_readwrite("is_no_pad", &vision::detection::ScaledYOLOv4::is_no_pad) + .def_readwrite("is_scale_up", + &vision::detection::ScaledYOLOv4::is_scale_up) + .def_readwrite("stride", &vision::detection::ScaledYOLOv4::stride) + .def_readwrite("max_wh", &vision::detection::ScaledYOLOv4::max_wh); +} +} // namespace fastdeploy diff --git a/csrcs/fastdeploy/vision/wongkinyiu/yolor.cc b/csrcs/fastdeploy/vision/detection/contrib/yolor.cc similarity index 94% rename from csrcs/fastdeploy/vision/wongkinyiu/yolor.cc rename to csrcs/fastdeploy/vision/detection/contrib/yolor.cc index 7de994f2a47..5e6fa2fddb2 100644 --- a/csrcs/fastdeploy/vision/wongkinyiu/yolor.cc +++ b/csrcs/fastdeploy/vision/detection/contrib/yolor.cc @@ -12,13 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "fastdeploy/vision/wongkinyiu/yolor.h" +#include "fastdeploy/vision/detection/contrib/yolor.h" #include "fastdeploy/utils/perf.h" #include "fastdeploy/vision/utils/utils.h" namespace fastdeploy { namespace vision { -namespace wongkinyiu { +namespace detection { void YOLOR::LetterBox(Mat* mat, const std::vector& size, const std::vector& color, bool _auto, @@ -63,8 +63,8 @@ YOLOR::YOLOR(const std::string& model_file, const std::string& params_file, valid_cpu_backends = {Backend::ORT}; // 指定可用的CPU后端 valid_gpu_backends = {Backend::ORT, Backend::TRT}; // 指定可用的GPU后端 } else { - valid_cpu_backends = {Backend::PDINFER, Backend::ORT}; - valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT}; + valid_cpu_backends = {Backend::PDINFER}; + valid_gpu_backends = {Backend::PDINFER}; } runtime_option = custom_option; runtime_option.model_format = model_format; @@ -216,10 +216,6 @@ bool YOLOR::Postprocess( bool YOLOR::Predict(cv::Mat* im, DetectionResult* result, float conf_threshold, float nms_iou_threshold) { -#ifdef FASTDEPLOY_DEBUG - TIMERECORD_START(0) -#endif - Mat mat(*im); std::vector input_tensors(1); @@ -236,21 +232,12 @@ bool YOLOR::Predict(cv::Mat* im, DetectionResult* result, float conf_threshold, return false; } -#ifdef FASTDEPLOY_DEBUG - TIMERECORD_END(0, "Preprocess") - TIMERECORD_START(1) -#endif - input_tensors[0].name = InputInfoOfRuntime(0).name; std::vector output_tensors; if (!Infer(input_tensors, &output_tensors)) { FDERROR << "Failed to inference." << std::endl; return false; } -#ifdef FASTDEPLOY_DEBUG - TIMERECORD_END(1, "Inference") - TIMERECORD_START(2) -#endif if (!Postprocess(output_tensors[0], result, im_info, conf_threshold, nms_iou_threshold)) { @@ -258,12 +245,9 @@ bool YOLOR::Predict(cv::Mat* im, DetectionResult* result, float conf_threshold, return false; } -#ifdef FASTDEPLOY_DEBUG - TIMERECORD_END(2, "Postprocess") -#endif return true; } -} // namespace wongkinyiu +} // namespace detection } // namespace vision } // namespace fastdeploy diff --git a/csrcs/fastdeploy/vision/wongkinyiu/yolor.h b/csrcs/fastdeploy/vision/detection/contrib/yolor.h similarity index 97% rename from csrcs/fastdeploy/vision/wongkinyiu/yolor.h rename to csrcs/fastdeploy/vision/detection/contrib/yolor.h index 05bbd6421c7..2de7a456fd1 100644 --- a/csrcs/fastdeploy/vision/wongkinyiu/yolor.h +++ b/csrcs/fastdeploy/vision/detection/contrib/yolor.h @@ -19,7 +19,7 @@ namespace fastdeploy { namespace vision { -namespace wongkinyiu { +namespace detection { class FASTDEPLOY_DECL YOLOR : public FastDeployModel { public: @@ -30,7 +30,7 @@ class FASTDEPLOY_DECL YOLOR : public FastDeployModel { const Frontend& model_format = Frontend::ONNX); // 定义模型的名称 - virtual std::string ModelName() const { return "WongKinYiu/yolor"; } + virtual std::string ModelName() const { return "YOLOR"; } // 模型预测接口,即用户调用的接口 // im 为用户的输入数据,目前对于CV均定义为cv::Mat @@ -97,6 +97,6 @@ class FASTDEPLOY_DECL YOLOR : public FastDeployModel { // auto check by fastdeploy after the internal Runtime already initialized. bool is_dynamic_input_; }; -} // namespace wongkinyiu +} // namespace detection } // namespace vision } // namespace fastdeploy diff --git a/csrcs/fastdeploy/vision/detection/contrib/yolor_pybind.cc b/csrcs/fastdeploy/vision/detection/contrib/yolor_pybind.cc new file mode 100644 index 00000000000..0e0a21ca56e --- /dev/null +++ b/csrcs/fastdeploy/vision/detection/contrib/yolor_pybind.cc @@ -0,0 +1,37 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "fastdeploy/pybind/main.h" + +namespace fastdeploy { +void BindYOLOR(pybind11::module& m) { + pybind11::class_(m, "YOLOR") + .def(pybind11::init()) + .def("predict", + [](vision::detection::YOLOR& self, pybind11::array& data, + float conf_threshold, float nms_iou_threshold) { + auto mat = PyArrayToCvMat(data); + vision::DetectionResult res; + self.Predict(&mat, &res, conf_threshold, nms_iou_threshold); + return res; + }) + .def_readwrite("size", &vision::detection::YOLOR::size) + .def_readwrite("padding_value", &vision::detection::YOLOR::padding_value) + .def_readwrite("is_mini_pad", &vision::detection::YOLOR::is_mini_pad) + .def_readwrite("is_no_pad", &vision::detection::YOLOR::is_no_pad) + .def_readwrite("is_scale_up", &vision::detection::YOLOR::is_scale_up) + .def_readwrite("stride", &vision::detection::YOLOR::stride) + .def_readwrite("max_wh", &vision::detection::YOLOR::max_wh); +} +} // namespace fastdeploy diff --git a/csrcs/fastdeploy/vision/wongkinyiu/yolov7.cc b/csrcs/fastdeploy/vision/detection/contrib/yolov7.cc similarity index 94% rename from csrcs/fastdeploy/vision/wongkinyiu/yolov7.cc rename to csrcs/fastdeploy/vision/detection/contrib/yolov7.cc index 6f603c87fc5..edc1b90482c 100644 --- a/csrcs/fastdeploy/vision/wongkinyiu/yolov7.cc +++ b/csrcs/fastdeploy/vision/detection/contrib/yolov7.cc @@ -12,13 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "fastdeploy/vision/wongkinyiu/yolov7.h" +#include "fastdeploy/vision/detection/contrib/yolov7.h" #include "fastdeploy/utils/perf.h" #include "fastdeploy/vision/utils/utils.h" namespace fastdeploy { namespace vision { -namespace wongkinyiu { +namespace detection { void YOLOv7::LetterBox(Mat* mat, const std::vector& size, const std::vector& color, bool _auto, @@ -64,13 +64,12 @@ YOLOv7::YOLOv7(const std::string& model_file, const std::string& params_file, valid_cpu_backends = {Backend::ORT}; // 指定可用的CPU后端 valid_gpu_backends = {Backend::ORT, Backend::TRT}; // 指定可用的GPU后端 } else { - valid_cpu_backends = {Backend::PDINFER, Backend::ORT}; - valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT}; + valid_cpu_backends = {Backend::PDINFER}; + valid_gpu_backends = {Backend::PDINFER}; } runtime_option = custom_option; runtime_option.model_format = model_format; runtime_option.model_file = model_file; - runtime_option.params_file = params_file; initialized = Initialize(); } @@ -217,10 +216,6 @@ bool YOLOv7::Postprocess( bool YOLOv7::Predict(cv::Mat* im, DetectionResult* result, float conf_threshold, float nms_iou_threshold) { -#ifdef FASTDEPLOY_DEBUG - TIMERECORD_START(0) -#endif - Mat mat(*im); std::vector input_tensors(1); @@ -237,21 +232,12 @@ bool YOLOv7::Predict(cv::Mat* im, DetectionResult* result, float conf_threshold, return false; } -#ifdef FASTDEPLOY_DEBUG - TIMERECORD_END(0, "Preprocess") - TIMERECORD_START(1) -#endif - input_tensors[0].name = InputInfoOfRuntime(0).name; std::vector output_tensors; if (!Infer(input_tensors, &output_tensors)) { FDERROR << "Failed to inference." << std::endl; return false; } -#ifdef FASTDEPLOY_DEBUG - TIMERECORD_END(1, "Inference") - TIMERECORD_START(2) -#endif if (!Postprocess(output_tensors[0], result, im_info, conf_threshold, nms_iou_threshold)) { @@ -259,12 +245,9 @@ bool YOLOv7::Predict(cv::Mat* im, DetectionResult* result, float conf_threshold, return false; } -#ifdef FASTDEPLOY_DEBUG - TIMERECORD_END(2, "Postprocess") -#endif return true; } -} // namespace wongkinyiu +} // namespace detection } // namespace vision } // namespace fastdeploy diff --git a/csrcs/fastdeploy/vision/wongkinyiu/yolov7.h b/csrcs/fastdeploy/vision/detection/contrib/yolov7.h similarity index 93% rename from csrcs/fastdeploy/vision/wongkinyiu/yolov7.h rename to csrcs/fastdeploy/vision/detection/contrib/yolov7.h index 595530b9c4d..02b874b2c58 100644 --- a/csrcs/fastdeploy/vision/wongkinyiu/yolov7.h +++ b/csrcs/fastdeploy/vision/detection/contrib/yolov7.h @@ -19,18 +19,16 @@ namespace fastdeploy { namespace vision { -namespace wongkinyiu { +namespace detection { class FASTDEPLOY_DECL YOLOv7 : public FastDeployModel { public: - // 当model_format为ONNX时,无需指定params_file - // 当model_format为Paddle时,则需同时指定model_file & params_file YOLOv7(const std::string& model_file, const std::string& params_file = "", const RuntimeOption& custom_option = RuntimeOption(), const Frontend& model_format = Frontend::ONNX); // 定义模型的名称 - virtual std::string ModelName() const { return "WongKinYiu/yolov7"; } + virtual std::string ModelName() const { return "yolov7"; } // 模型预测接口,即用户调用的接口 // im 为用户的输入数据,目前对于CV均定义为cv::Mat @@ -97,6 +95,6 @@ class FASTDEPLOY_DECL YOLOv7 : public FastDeployModel { // auto check by fastdeploy after the internal Runtime already initialized. bool is_dynamic_input_; }; -} // namespace wongkinyiu +} // namespace detection } // namespace vision } // namespace fastdeploy diff --git a/csrcs/fastdeploy/vision/detection/contrib/yolov7_pybind.cc b/csrcs/fastdeploy/vision/detection/contrib/yolov7_pybind.cc new file mode 100644 index 00000000000..bf196fa9f47 --- /dev/null +++ b/csrcs/fastdeploy/vision/detection/contrib/yolov7_pybind.cc @@ -0,0 +1,37 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "fastdeploy/pybind/main.h" + +namespace fastdeploy { +void BindYOLOv7(pybind11::module& m) { + pybind11::class_(m, "YOLOv7") + .def(pybind11::init()) + .def("predict", + [](vision::detection::YOLOv7& self, pybind11::array& data, + float conf_threshold, float nms_iou_threshold) { + auto mat = PyArrayToCvMat(data); + vision::DetectionResult res; + self.Predict(&mat, &res, conf_threshold, nms_iou_threshold); + return res; + }) + .def_readwrite("size", &vision::detection::YOLOv7::size) + .def_readwrite("padding_value", &vision::detection::YOLOv7::padding_value) + .def_readwrite("is_mini_pad", &vision::detection::YOLOv7::is_mini_pad) + .def_readwrite("is_no_pad", &vision::detection::YOLOv7::is_no_pad) + .def_readwrite("is_scale_up", &vision::detection::YOLOv7::is_scale_up) + .def_readwrite("stride", &vision::detection::YOLOv7::stride) + .def_readwrite("max_wh", &vision::detection::YOLOv7::max_wh); +} +} // namespace fastdeploy diff --git a/csrcs/fastdeploy/vision/detection/detection_pybind.cc b/csrcs/fastdeploy/vision/detection/detection_pybind.cc new file mode 100644 index 00000000000..2ab8e20bbbd --- /dev/null +++ b/csrcs/fastdeploy/vision/detection/detection_pybind.cc @@ -0,0 +1,30 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "fastdeploy/pybind/main.h" + +namespace fastdeploy { + +void BindYOLOv7(pybind11::module& m); +void BindScaledYOLOv4(pybind11::module& m); +void BindYOLOR(pybind11::module& m); + +void BindDetection(pybind11::module& m) { + auto detection_module = + m.def_submodule("detection", "Image object detection models."); + BindYOLOv7(detection_module); + BindScaledYOLOv4(detection_module); + BindYOLOR(detection_module); +} +} // namespace fastdeploy diff --git a/csrcs/fastdeploy/vision/vision_pybind.cc b/csrcs/fastdeploy/vision/vision_pybind.cc index dfff80d1fe2..e408732c610 100644 --- a/csrcs/fastdeploy/vision/vision_pybind.cc +++ b/csrcs/fastdeploy/vision/vision_pybind.cc @@ -18,7 +18,6 @@ namespace fastdeploy { void BindPPCls(pybind11::module& m); void BindPPDet(pybind11::module& m); -void BindWongkinyiu(pybind11::module& m); void BindPPSeg(pybind11::module& m); void BindUltralytics(pybind11::module& m); void BindMeituan(pybind11::module& m); @@ -30,6 +29,8 @@ void BindBiubug6(pybind11::module& m); void BindPpogg(pybind11::module& m); void BindDeepInsight(pybind11::module& m); void BindZHKKKe(pybind11::module& m); + +void BindDetection(pybind11::module& m); #ifdef ENABLE_VISION_VISUALIZE void BindVisualize(pybind11::module& m); #endif @@ -88,7 +89,6 @@ void BindVision(pybind11::module& m) { BindPPDet(m); BindPPSeg(m); BindUltralytics(m); - BindWongkinyiu(m); BindMeituan(m); BindMegvii(m); BindDeepCam(m); @@ -98,6 +98,8 @@ void BindVision(pybind11::module& m) { BindPpogg(m); BindDeepInsight(m); BindZHKKKe(m); + + BindDetection(m); #ifdef ENABLE_VISION_VISUALIZE BindVisualize(m); #endif diff --git a/csrcs/fastdeploy/vision/visualize/detection.cc b/csrcs/fastdeploy/vision/visualize/detection.cc index 6d60072447a..147ef6556b0 100644 --- a/csrcs/fastdeploy/vision/visualize/detection.cc +++ b/csrcs/fastdeploy/vision/visualize/detection.cc @@ -23,11 +23,13 @@ namespace vision { // Default only support visualize num_classes <= 1000 // If need to visualize num_classes > 1000 // Please call Visualize::GetColorMap(num_classes) first -void Visualize::VisDetection(cv::Mat* im, const DetectionResult& result, - int line_size, float font_size) { +cv::Mat Visualize::VisDetection(const cv::Mat& im, + const DetectionResult& result, int line_size, + float font_size) { auto color_map = GetColorMap(); - int h = im->rows; - int w = im->cols; + int h = im.rows; + int w = im.cols; + auto vis_im = im.clone(); for (size_t i = 0; i < result.boxes.size(); ++i) { cv::Rect rect(result.boxes[i][0], result.boxes[i][1], result.boxes[i][2] - result.boxes[i][0], @@ -50,10 +52,11 @@ void Visualize::VisDetection(cv::Mat* im, const DetectionResult& result, cv::Rect text_background = cv::Rect(result.boxes[i][0], result.boxes[i][1] - text_size.height, text_size.width, text_size.height); - cv::rectangle(*im, rect, rect_color, line_size); - cv::putText(*im, text, origin, font, font_size, cv::Scalar(255, 255, 255), - 1); + cv::rectangle(vis_im, rect, rect_color, line_size); + cv::putText(vis_im, text, origin, font, font_size, + cv::Scalar(255, 255, 255), 1); } + return vis_im; } } // namespace vision diff --git a/csrcs/fastdeploy/vision/visualize/face_detection.cc b/csrcs/fastdeploy/vision/visualize/face_detection.cc index 8a95a1ad7e8..d9da2778606 100644 --- a/csrcs/fastdeploy/vision/visualize/face_detection.cc +++ b/csrcs/fastdeploy/vision/visualize/face_detection.cc @@ -24,12 +24,14 @@ namespace vision { // Default only support visualize num_classes <= 1000 // If need to visualize num_classes > 1000 // Please call Visualize::GetColorMap(num_classes) first -void Visualize::VisFaceDetection(cv::Mat* im, const FaceDetectionResult& result, - int line_size, float font_size) { +cv::Mat Visualize::VisFaceDetection(const cv::Mat& im, + const FaceDetectionResult& result, + int line_size, float font_size) { auto color_map = GetColorMap(); - int h = im->rows; - int w = im->cols; + int h = im.rows; + int w = im.cols; + auto vis_im = im.clone(); bool vis_landmarks = false; if ((result.landmarks_per_face > 0) && (result.boxes.size() * result.landmarks_per_face == @@ -57,9 +59,9 @@ void Visualize::VisFaceDetection(cv::Mat* im, const FaceDetectionResult& result, cv::Rect text_background = cv::Rect(result.boxes[i][0], result.boxes[i][1] - text_size.height, text_size.width, text_size.height); - cv::rectangle(*im, rect, rect_color, line_size); - cv::putText(*im, text, origin, font, font_size, cv::Scalar(255, 255, 255), - 1); + cv::rectangle(vis_im, rect, rect_color, line_size); + cv::putText(vis_im, text, origin, font, font_size, + cv::Scalar(255, 255, 255), 1); // vis landmarks (if have) if (vis_landmarks) { cv::Scalar landmark_color = rect_color; @@ -69,13 +71,14 @@ void Visualize::VisFaceDetection(cv::Mat* im, const FaceDetectionResult& result, result.landmarks[i * result.landmarks_per_face + j][0]); landmark.y = static_cast( result.landmarks[i * result.landmarks_per_face + j][1]); - cv::circle(*im, landmark, line_size, landmark_color, -1); + cv::circle(vis_im, landmark, line_size, landmark_color, -1); } } } + return vis_im; } } // namespace vision } // namespace fastdeploy -#endif \ No newline at end of file +#endif diff --git a/csrcs/fastdeploy/vision/visualize/matting_alpha.cc b/csrcs/fastdeploy/vision/visualize/matting_alpha.cc index f1eaa0cfaee..1018018c629 100644 --- a/csrcs/fastdeploy/vision/visualize/matting_alpha.cc +++ b/csrcs/fastdeploy/vision/visualize/matting_alpha.cc @@ -65,12 +65,14 @@ static void RemoveSmallConnectedArea(cv::Mat* alpha_pred, } } -void Visualize::VisMattingAlpha(const cv::Mat& im, const MattingResult& result, - cv::Mat* vis_img, - bool remove_small_connected_area) { +cv::Mat Visualize::VisMattingAlpha(const cv::Mat& im, + const MattingResult& result, + bool remove_small_connected_area) { // 只可视化alpha,fgr(前景)本身就是一张图 不需要可视化 FDASSERT((!im.empty()), "im can't be empty!"); FDASSERT((im.channels() == 3), "Only support 3 channels mat!"); + + auto vis_img = im.clone(); int out_h = static_cast(result.shape[0]); int out_w = static_cast(result.shape[1]); int height = im.rows; @@ -87,18 +89,11 @@ void Visualize::VisMattingAlpha(const cv::Mat& im, const MattingResult& result, cv::resize(alpha, alpha, cv::Size(width, height)); } - int vis_h = (*vis_img).rows; - int vis_w = (*vis_img).cols; - - if ((vis_h != height) || (vis_w != width)) { - // faster than resize - (*vis_img) = cv::Mat::zeros(height, width, CV_8UC3); - } - if ((*vis_img).type() != CV_8UC3) { - (*vis_img).convertTo((*vis_img), CV_8UC3); + if ((vis_img).type() != CV_8UC3) { + (vis_img).convertTo((vis_img), CV_8UC3); } - uchar* vis_data = static_cast(vis_img->data); + uchar* vis_data = static_cast(vis_img.data); uchar* im_data = static_cast(im.data); float* alpha_data = reinterpret_cast(alpha.data); @@ -116,6 +111,7 @@ void Visualize::VisMattingAlpha(const cv::Mat& im, const MattingResult& result, (1.f - alpha_val) * 120.f); } } + return vis_img; } } // namespace vision diff --git a/csrcs/fastdeploy/vision/visualize/segmentation.cc b/csrcs/fastdeploy/vision/visualize/segmentation.cc index 1fba09a1311..7d379032814 100644 --- a/csrcs/fastdeploy/vision/visualize/segmentation.cc +++ b/csrcs/fastdeploy/vision/visualize/segmentation.cc @@ -21,24 +21,24 @@ namespace fastdeploy { namespace vision { -void Visualize::VisSegmentation(const cv::Mat& im, - const SegmentationResult& result, - cv::Mat* vis_img, const int& num_classes) { - auto color_map = GetColorMap(num_classes); +cv::Mat Visualize::VisSegmentation(const cv::Mat& im, + const SegmentationResult& result) { + auto color_map = GetColorMap(); int64_t height = result.shape[0]; int64_t width = result.shape[1]; - *vis_img = cv::Mat::zeros(height, width, CV_8UC3); + auto vis_img = cv::Mat(height, width, CV_8UC3); int64_t index = 0; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { int category_id = result.label_map[index++]; - vis_img->at(i, j)[0] = color_map[3 * category_id + 0]; - vis_img->at(i, j)[1] = color_map[3 * category_id + 1]; - vis_img->at(i, j)[2] = color_map[3 * category_id + 2]; + vis_img.at(i, j)[0] = color_map[3 * category_id + 0]; + vis_img.at(i, j)[1] = color_map[3 * category_id + 1]; + vis_img.at(i, j)[2] = color_map[3 * category_id + 2]; } } - cv::addWeighted(im, .5, *vis_img, .5, 0, *vis_img); + cv::addWeighted(im, .5, vis_img, .5, 0, vis_img); + return vis_img; } } // namespace vision diff --git a/csrcs/fastdeploy/vision/visualize/visualize.h b/csrcs/fastdeploy/vision/visualize/visualize.h index 2ddb896937a..3ba4fe903ab 100644 --- a/csrcs/fastdeploy/vision/visualize/visualize.h +++ b/csrcs/fastdeploy/vision/visualize/visualize.h @@ -25,16 +25,15 @@ class FASTDEPLOY_DECL Visualize { static int num_classes_; static std::vector color_map_; static const std::vector& GetColorMap(int num_classes = 1000); - static void VisDetection(cv::Mat* im, const DetectionResult& result, - int line_size = 2, float font_size = 0.5f); - static void VisFaceDetection(cv::Mat* im, const FaceDetectionResult& result, - int line_size = 2, float font_size = 0.5f); - static void VisSegmentation(const cv::Mat& im, - const SegmentationResult& result, - cv::Mat* vis_img, const int& num_classes = 1000); - static void VisMattingAlpha(const cv::Mat& im, const MattingResult& result, - cv::Mat* vis_img, - bool remove_small_connected_area = false); + static cv::Mat VisDetection(const cv::Mat& im, const DetectionResult& result, + int line_size = 2, float font_size = 0.5f); + static cv::Mat VisFaceDetection(const cv::Mat& im, + const FaceDetectionResult& result, + int line_size = 2, float font_size = 0.5f); + static cv::Mat VisSegmentation(const cv::Mat& im, + const SegmentationResult& result); + static cv::Mat VisMattingAlpha(const cv::Mat& im, const MattingResult& result, + bool remove_small_connected_area = false); }; } // namespace vision diff --git a/csrcs/fastdeploy/vision/visualize/visualize_pybind.cc b/csrcs/fastdeploy/vision/visualize/visualize_pybind.cc index a50b9de8c1b..36010acf1f8 100644 --- a/csrcs/fastdeploy/vision/visualize/visualize_pybind.cc +++ b/csrcs/fastdeploy/vision/visualize/visualize_pybind.cc @@ -22,34 +22,41 @@ void BindVisualize(pybind11::module& m) { [](pybind11::array& im_data, vision::DetectionResult& result, int line_size, float font_size) { auto im = PyArrayToCvMat(im_data); - vision::Visualize::VisDetection(&im, result, line_size, - font_size); + auto vis_im = vision::Visualize::VisDetection( + im, result, line_size, font_size); + FDTensor out; + vision::Mat(vis_im).ShareWithTensor(&out); + return TensorToPyArray(out); }) .def_static( "vis_face_detection", [](pybind11::array& im_data, vision::FaceDetectionResult& result, int line_size, float font_size) { auto im = PyArrayToCvMat(im_data); - vision::Visualize::VisFaceDetection(&im, result, line_size, - font_size); + auto vis_im = vision::Visualize::VisFaceDetection( + im, result, line_size, font_size); + FDTensor out; + vision::Mat(vis_im).ShareWithTensor(&out); + return TensorToPyArray(out); }) .def_static( "vis_segmentation", - [](pybind11::array& im_data, vision::SegmentationResult& result, - pybind11::array& vis_im_data, const int& num_classes) { + [](pybind11::array& im_data, vision::SegmentationResult& result) { cv::Mat im = PyArrayToCvMat(im_data); - cv::Mat vis_im = PyArrayToCvMat(vis_im_data); - vision::Visualize::VisSegmentation(im, result, &vis_im, - num_classes); + auto vis_im = vision::Visualize::VisSegmentation(im, result); + FDTensor out; + vision::Mat(vis_im).ShareWithTensor(&out); + return TensorToPyArray(out); }) - .def_static( - "vis_matting_alpha", - [](pybind11::array& im_data, vision::MattingResult& result, - pybind11::array& vis_im_data, bool remove_small_connected_area) { - cv::Mat im = PyArrayToCvMat(im_data); - cv::Mat vis_im = PyArrayToCvMat(vis_im_data); - vision::Visualize::VisMattingAlpha(im, result, &vis_im, - remove_small_connected_area); - }); + .def_static("vis_matting_alpha", + [](pybind11::array& im_data, vision::MattingResult& result, + bool remove_small_connected_area) { + cv::Mat im = PyArrayToCvMat(im_data); + auto vis_im = vision::Visualize::VisMattingAlpha( + im, result, remove_small_connected_area); + FDTensor out; + vision::Mat(vis_im).ShareWithTensor(&out); + return TensorToPyArray(out); + }); } } // namespace fastdeploy diff --git a/csrcs/fastdeploy/vision/wongkinyiu/wongkinyiu_pybind.cc b/csrcs/fastdeploy/vision/wongkinyiu/wongkinyiu_pybind.cc deleted file mode 100644 index c90ed3f112c..00000000000 --- a/csrcs/fastdeploy/vision/wongkinyiu/wongkinyiu_pybind.cc +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "fastdeploy/pybind/main.h" - -namespace fastdeploy { -void BindWongkinyiu(pybind11::module& m) { - auto wongkinyiu_module = - m.def_submodule("wongkinyiu", "https://github.com/WongKinYiu"); - pybind11::class_( - wongkinyiu_module, "YOLOv7") - .def(pybind11::init()) - .def("predict", - [](vision::wongkinyiu::YOLOv7& self, pybind11::array& data, - float conf_threshold, float nms_iou_threshold) { - auto mat = PyArrayToCvMat(data); - vision::DetectionResult res; - self.Predict(&mat, &res, conf_threshold, nms_iou_threshold); - return res; - }) - .def_readwrite("size", &vision::wongkinyiu::YOLOv7::size) - .def_readwrite("padding_value", - &vision::wongkinyiu::YOLOv7::padding_value) - .def_readwrite("is_mini_pad", &vision::wongkinyiu::YOLOv7::is_mini_pad) - .def_readwrite("is_no_pad", &vision::wongkinyiu::YOLOv7::is_no_pad) - .def_readwrite("is_scale_up", &vision::wongkinyiu::YOLOv7::is_scale_up) - .def_readwrite("stride", &vision::wongkinyiu::YOLOv7::stride) - .def_readwrite("max_wh", &vision::wongkinyiu::YOLOv7::max_wh); - - pybind11::class_( - wongkinyiu_module, "YOLOR") - .def(pybind11::init()) - .def("predict", - [](vision::wongkinyiu::YOLOR& self, pybind11::array& data, - float conf_threshold, float nms_iou_threshold) { - auto mat = PyArrayToCvMat(data); - vision::DetectionResult res; - self.Predict(&mat, &res, conf_threshold, nms_iou_threshold); - return res; - }) - .def_readwrite("size", &vision::wongkinyiu::YOLOR::size) - .def_readwrite("padding_value", &vision::wongkinyiu::YOLOR::padding_value) - .def_readwrite("is_mini_pad", &vision::wongkinyiu::YOLOR::is_mini_pad) - .def_readwrite("is_no_pad", &vision::wongkinyiu::YOLOR::is_no_pad) - .def_readwrite("is_scale_up", &vision::wongkinyiu::YOLOR::is_scale_up) - .def_readwrite("stride", &vision::wongkinyiu::YOLOR::stride) - .def_readwrite("max_wh", &vision::wongkinyiu::YOLOR::max_wh); - - pybind11::class_( - wongkinyiu_module, "ScaledYOLOv4") - .def(pybind11::init()) - .def("predict", - [](vision::wongkinyiu::ScaledYOLOv4& self, pybind11::array& data, - float conf_threshold, float nms_iou_threshold) { - auto mat = PyArrayToCvMat(data); - vision::DetectionResult res; - self.Predict(&mat, &res, conf_threshold, nms_iou_threshold); - return res; - }) - .def_readwrite("size", &vision::wongkinyiu::ScaledYOLOv4::size) - .def_readwrite("padding_value", &vision::wongkinyiu::ScaledYOLOv4::padding_value) - .def_readwrite("is_mini_pad", &vision::wongkinyiu::ScaledYOLOv4::is_mini_pad) - .def_readwrite("is_no_pad", &vision::wongkinyiu::ScaledYOLOv4::is_no_pad) - .def_readwrite("is_scale_up", &vision::wongkinyiu::ScaledYOLOv4::is_scale_up) - .def_readwrite("stride", &vision::wongkinyiu::ScaledYOLOv4::stride) - .def_readwrite("max_wh", &vision::wongkinyiu::ScaledYOLOv4::max_wh); -} -} // namespace fastdeploy diff --git a/fastdeploy/vision/__init__.py b/fastdeploy/vision/__init__.py index fcf75c293a1..53dd76953ad 100644 --- a/fastdeploy/vision/__init__.py +++ b/fastdeploy/vision/__init__.py @@ -20,8 +20,6 @@ from . import ultralytics from . import meituan from . import megvii -from . import visualize -from . import wongkinyiu from . import deepcam from . import rangilyu from . import linzaer @@ -29,3 +27,7 @@ from . import ppogg from . import deepinsight from . import zhkkke + +from . import detection + +from .visualize import * diff --git a/fastdeploy/vision/detection/__init__.py b/fastdeploy/vision/detection/__init__.py new file mode 100644 index 00000000000..9d412774a57 --- /dev/null +++ b/fastdeploy/vision/detection/__init__.py @@ -0,0 +1,18 @@ +# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import +from .yolov7 import YOLOv7 +from .yolor import YOLOR +from .scaled_yolov4 import ScaledYOLOv4 diff --git a/fastdeploy/vision/detection/scaled_yolov4.py b/fastdeploy/vision/detection/scaled_yolov4.py new file mode 100644 index 00000000000..f1be5ed9185 --- /dev/null +++ b/fastdeploy/vision/detection/scaled_yolov4.py @@ -0,0 +1,116 @@ +# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import +import logging +from ... import FastDeployModel, Frontend +from ... import c_lib_wrap as C + + +class ScaledYOLOv4(FastDeployModel): + def __init__(self, + model_file, + params_file="", + runtime_option=None, + model_format=Frontend.ONNX): + # 调用基函数进行backend_option的初始化 + # 初始化后的option保存在self._runtime_option + super(ScaledYOLOv4, self).__init__(runtime_option) + + self._model = C.vision.detection.ScaledYOLOv4( + model_file, params_file, self._runtime_option, model_format) + # 通过self.initialized判断整个模型的初始化是否成功 + assert self.initialized, "ScaledYOLOv4 initialize failed." + + def predict(self, input_image, conf_threshold=0.25, nms_iou_threshold=0.5): + return self._model.predict(input_image, conf_threshold, + nms_iou_threshold) + + # 一些跟ScaledYOLOv4模型有关的属性封装 + # 多数是预处理相关,可通过修改如model.size = [1280, 1280]改变预处理时resize的大小(前提是模型支持) + @property + def size(self): + return self._model.size + + @property + def padding_value(self): + return self._model.padding_value + + @property + def is_no_pad(self): + return self._model.is_no_pad + + @property + def is_mini_pad(self): + return self._model.is_mini_pad + + @property + def is_scale_up(self): + return self._model.is_scale_up + + @property + def stride(self): + return self._model.stride + + @property + def max_wh(self): + return self._model.max_wh + + @size.setter + def size(self, wh): + assert isinstance(wh, [list, tuple]),\ + "The value to set `size` must be type of tuple or list." + assert len(wh) == 2,\ + "The value to set `size` must contatins 2 elements means [width, height], but now it contains {} elements.".format( + len(wh)) + self._model.size = wh + + @padding_value.setter + def padding_value(self, value): + assert isinstance( + value, + list), "The value to set `padding_value` must be type of list." + self._model.padding_value = value + + @is_no_pad.setter + def is_no_pad(self, value): + assert isinstance( + value, bool), "The value to set `is_no_pad` must be type of bool." + self._model.is_no_pad = value + + @is_mini_pad.setter + def is_mini_pad(self, value): + assert isinstance( + value, + bool), "The value to set `is_mini_pad` must be type of bool." + self._model.is_mini_pad = value + + @is_scale_up.setter + def is_scale_up(self, value): + assert isinstance( + value, + bool), "The value to set `is_scale_up` must be type of bool." + self._model.is_scale_up = value + + @stride.setter + def stride(self, value): + assert isinstance( + value, int), "The value to set `stride` must be type of int." + self._model.stride = value + + @max_wh.setter + def max_wh(self, value): + assert isinstance( + value, float), "The value to set `max_wh` must be type of float." + self._model.max_wh = value diff --git a/fastdeploy/vision/detection/yolor.py b/fastdeploy/vision/detection/yolor.py new file mode 100644 index 00000000000..f2409f69c78 --- /dev/null +++ b/fastdeploy/vision/detection/yolor.py @@ -0,0 +1,116 @@ +# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import +import logging +from ... import FastDeployModel, Frontend +from ... import c_lib_wrap as C + + +class YOLOR(FastDeployModel): + def __init__(self, + model_file, + params_file="", + runtime_option=None, + model_format=Frontend.ONNX): + # 调用基函数进行backend_option的初始化 + # 初始化后的option保存在self._runtime_option + super(YOLOR, self).__init__(runtime_option) + + self._model = C.vision.detection.YOLOR( + model_file, params_file, self._runtime_option, model_format) + # 通过self.initialized判断整个模型的初始化是否成功 + assert self.initialized, "YOLOR initialize failed." + + def predict(self, input_image, conf_threshold=0.25, nms_iou_threshold=0.5): + return self._model.predict(input_image, conf_threshold, + nms_iou_threshold) + + # 一些跟YOLOR模型有关的属性封装 + # 多数是预处理相关,可通过修改如model.size = [1280, 1280]改变预处理时resize的大小(前提是模型支持) + @property + def size(self): + return self._model.size + + @property + def padding_value(self): + return self._model.padding_value + + @property + def is_no_pad(self): + return self._model.is_no_pad + + @property + def is_mini_pad(self): + return self._model.is_mini_pad + + @property + def is_scale_up(self): + return self._model.is_scale_up + + @property + def stride(self): + return self._model.stride + + @property + def max_wh(self): + return self._model.max_wh + + @size.setter + def size(self, wh): + assert isinstance(wh, [list, tuple]),\ + "The value to set `size` must be type of tuple or list." + assert len(wh) == 2,\ + "The value to set `size` must contatins 2 elements means [width, height], but now it contains {} elements.".format( + len(wh)) + self._model.size = wh + + @padding_value.setter + def padding_value(self, value): + assert isinstance( + value, + list), "The value to set `padding_value` must be type of list." + self._model.padding_value = value + + @is_no_pad.setter + def is_no_pad(self, value): + assert isinstance( + value, bool), "The value to set `is_no_pad` must be type of bool." + self._model.is_no_pad = value + + @is_mini_pad.setter + def is_mini_pad(self, value): + assert isinstance( + value, + bool), "The value to set `is_mini_pad` must be type of bool." + self._model.is_mini_pad = value + + @is_scale_up.setter + def is_scale_up(self, value): + assert isinstance( + value, + bool), "The value to set `is_scale_up` must be type of bool." + self._model.is_scale_up = value + + @stride.setter + def stride(self, value): + assert isinstance( + value, int), "The value to set `stride` must be type of int." + self._model.stride = value + + @max_wh.setter + def max_wh(self, value): + assert isinstance( + value, float), "The value to set `max_wh` must be type of float." + self._model.max_wh = value diff --git a/fastdeploy/vision/detection/yolov7.py b/fastdeploy/vision/detection/yolov7.py new file mode 100644 index 00000000000..a4fd0009c10 --- /dev/null +++ b/fastdeploy/vision/detection/yolov7.py @@ -0,0 +1,116 @@ +# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import +import logging +from ... import FastDeployModel, Frontend +from ... import c_lib_wrap as C + + +class YOLOv7(FastDeployModel): + def __init__(self, + model_file, + params_file="", + runtime_option=None, + model_format=Frontend.ONNX): + # 调用基函数进行backend_option的初始化 + # 初始化后的option保存在self._runtime_option + super(YOLOv7, self).__init__(runtime_option) + + self._model = C.vision.detection.YOLOv7( + model_file, params_file, self._runtime_option, model_format) + # 通过self.initialized判断整个模型的初始化是否成功 + assert self.initialized, "YOLOv7 initialize failed." + + def predict(self, input_image, conf_threshold=0.25, nms_iou_threshold=0.5): + return self._model.predict(input_image, conf_threshold, + nms_iou_threshold) + + # 一些跟YOLOv7模型有关的属性封装 + # 多数是预处理相关,可通过修改如model.size = [1280, 1280]改变预处理时resize的大小(前提是模型支持) + @property + def size(self): + return self._model.size + + @property + def padding_value(self): + return self._model.padding_value + + @property + def is_no_pad(self): + return self._model.is_no_pad + + @property + def is_mini_pad(self): + return self._model.is_mini_pad + + @property + def is_scale_up(self): + return self._model.is_scale_up + + @property + def stride(self): + return self._model.stride + + @property + def max_wh(self): + return self._model.max_wh + + @size.setter + def size(self, wh): + assert isinstance(wh, [list, tuple]),\ + "The value to set `size` must be type of tuple or list." + assert len(wh) == 2,\ + "The value to set `size` must contatins 2 elements means [width, height], but now it contains {} elements.".format( + len(wh)) + self._model.size = wh + + @padding_value.setter + def padding_value(self, value): + assert isinstance( + value, + list), "The value to set `padding_value` must be type of list." + self._model.padding_value = value + + @is_no_pad.setter + def is_no_pad(self, value): + assert isinstance( + value, bool), "The value to set `is_no_pad` must be type of bool." + self._model.is_no_pad = value + + @is_mini_pad.setter + def is_mini_pad(self, value): + assert isinstance( + value, + bool), "The value to set `is_mini_pad` must be type of bool." + self._model.is_mini_pad = value + + @is_scale_up.setter + def is_scale_up(self, value): + assert isinstance( + value, + bool), "The value to set `is_scale_up` must be type of bool." + self._model.is_scale_up = value + + @stride.setter + def stride(self, value): + assert isinstance( + value, int), "The value to set `stride` must be type of int." + self._model.stride = value + + @max_wh.setter + def max_wh(self, value): + assert isinstance( + value, float), "The value to set `max_wh` must be type of float." + self._model.max_wh = value diff --git a/fastdeploy/vision/visualize/__init__.py b/fastdeploy/vision/visualize/__init__.py index 65607308d26..9770dfd1ddf 100644 --- a/fastdeploy/vision/visualize/__init__.py +++ b/fastdeploy/vision/visualize/__init__.py @@ -17,23 +17,22 @@ from ... import c_lib_wrap as C -def vis_detection(im_data, det_result, line_size=1, font_size=0.5): - C.vision.Visualize.vis_detection(im_data, det_result, line_size, font_size) +def vis_detection(im_data, det_result, line_size=2, font_size=0.5): + return C.vision.Visualize.vis_detection(im_data, det_result, line_size, + font_size) -def vis_face_detection(im_data, face_det_result, line_size=1, font_size=0.5): - C.vision.Visualize.vis_face_detection(im_data, face_det_result, line_size, - font_size) +def vis_face_detection(im_data, face_det_result, line_size=2, font_size=0.5): + return C.vision.Visualize.vis_face_detection(im_data, face_det_result, + line_size, font_size) -def vis_segmentation(im_data, seg_result, vis_im_data, num_classes=1000): - C.vision.Visualize.vis_segmentation(im_data, seg_result, vis_im_data, - num_classes) +def vis_segmentation(im_data, seg_result): + return C.vision.Visualize.vis_segmentation(im_data, seg_result) def vis_matting_alpha(im_data, matting_result, - vis_im_data, remove_small_connected_area=False): - C.vision.Visualize.vis_matting_alpha(im_data, matting_result, vis_im_data, - remove_small_connected_area) + return C.vision.Visualize.vis_matting_alpha(im_data, matting_result, + remove_small_connected_area) diff --git a/model_zoo/vision/retinaface/retinaface.py b/model_zoo/vision/retinaface/retinaface.py index 4e5a123c2ea..0c5bd4e331e 100644 --- a/model_zoo/vision/retinaface/retinaface.py +++ b/model_zoo/vision/retinaface/retinaface.py @@ -16,8 +16,8 @@ result = model.predict(im, conf_threshold=0.7, nms_iou_threshold=0.3) # 可视化结果 -fd.vision.visualize.vis_face_detection(im, result) -cv2.imwrite("vis_result.jpg", im) +vis_im = fd.vision.visualize.vis_face_detection(im, result) +cv2.imwrite("vis_result.jpg", vis_im) # 输出预测结果 print(result) From e8003392117caa67114bdbd4244e252848e757f7 Mon Sep 17 00:00:00 2001 From: jiangjiajun Date: Tue, 9 Aug 2022 06:12:08 +0000 Subject: [PATCH 2/6] Add a sample document and example code --- docs/api/vision_results/README.md | 8 ++ .../vision_results/classification_result.md | 28 +++++ docs/api/vision_results/detection_result.md | 31 +++++ docs/quick_start/install.md | 21 ++++ docs/quick_start/requirements.md | 21 ++++ new_examples/vision/detection/README.md | 24 ++++ .../vision/detection/yolov7/README.md | 37 ++++++ .../detection/yolov7/cpp/.README.md.swp | Bin 0 -> 12288 bytes .../detection/yolov7/cpp/CMakeLists.txt | 14 +++ .../vision/detection/yolov7/cpp/README.md | 65 +++++++++++ .../vision/detection/yolov7/cpp/infer.cc | 106 ++++++++++++++++++ .../vision/detection/yolov7/python/README.md | 61 ++++++++++ .../vision/detection/yolov7/python/infer.py | 52 +++++++++ 13 files changed, 468 insertions(+) create mode 100644 docs/api/vision_results/README.md create mode 100644 docs/api/vision_results/classification_result.md create mode 100644 docs/api/vision_results/detection_result.md create mode 100644 docs/quick_start/install.md create mode 100644 docs/quick_start/requirements.md create mode 100644 new_examples/vision/detection/README.md create mode 100644 new_examples/vision/detection/yolov7/README.md create mode 100644 new_examples/vision/detection/yolov7/cpp/.README.md.swp create mode 100644 new_examples/vision/detection/yolov7/cpp/CMakeLists.txt create mode 100644 new_examples/vision/detection/yolov7/cpp/README.md create mode 100644 new_examples/vision/detection/yolov7/cpp/infer.cc create mode 100644 new_examples/vision/detection/yolov7/python/README.md create mode 100644 new_examples/vision/detection/yolov7/python/infer.py diff --git a/docs/api/vision_results/README.md b/docs/api/vision_results/README.md new file mode 100644 index 00000000000..0a05aeaf8cd --- /dev/null +++ b/docs/api/vision_results/README.md @@ -0,0 +1,8 @@ +# 视觉模型预测结果说明 + +FastDeploy根据视觉模型的任务类型,定义了不同的结构体(`csrcs/fastdeploy/vision/common/result.h`)来表达模型预测结果,具体如下表所示 + +| 结构体 | 文档 | 说明 | 相应模型 | +| :----- | :--- | :---- | :------- | +| ClassificationResult | [C++/Python文档](./classificiation_result.md) | 图像分类返回结果 | ResNet50、MobileNetV3等 | +| DetectionResult | [C++/Python文档](./detection_result.md) | 目标检测返回结果 | PPYOLOE、YOLOv7系列模型等 | diff --git a/docs/api/vision_results/classification_result.md b/docs/api/vision_results/classification_result.md new file mode 100644 index 00000000000..113db39608a --- /dev/null +++ b/docs/api/vision_results/classification_result.md @@ -0,0 +1,28 @@ +# ClassifyResult 图像分类结果 + +ClassifyResult代码定义在`csrcs/fastdeploy/vision/common/result.h`中,用于表明图像的分类结果和置信度。 + +## C++ 结构体 + +`fastdeploy::vision::ClassifyResult` + +``` +struct ClassifyResult { + std::vector label_ids; + std::vector scores; + void Clear(); + std::string Str(); +}; +``` + +- **label_ids**: 成员变量,表示单张图片的分类结果,其个数根据在使用分类模型时传入的topk决定,例如可以返回top 5的分类结果 +- **scores**: 成员变量,表示单张图片在相应分类结果上的置信度,其个数根据在使用分类模型时传入的topk决定,例如可以返回top 5的分类置信度 +- **Clear()**: 成员函数,用于清除结构体中存储的结果 +- **Str()**: 成员函数,将结构体中的信息以字符串形式输出(用于Debug) + +## Python结构体 + +`fastdeploy.vision.ClassifyResult` + +- **label_ids**(list of int): 成员变量,表示单张图片的分类结果,其个数根据在使用分类模型时传入的topk决定,例如可以返回top 5的分类结果 +- **scores**(list of float): 成员变量,表示单张图片在相应分类结果上的置信度,其个数根据在使用分类模型时传入的topk决定,例如可以返回top 5的分类置信度 diff --git a/docs/api/vision_results/detection_result.md b/docs/api/vision_results/detection_result.md new file mode 100644 index 00000000000..e44a27b34c3 --- /dev/null +++ b/docs/api/vision_results/detection_result.md @@ -0,0 +1,31 @@ +# DetectionResult 目标检测结果 + +DetectionResult代码定义在`csrcs/fastdeploy/vision/common/result.h`中,用于表明图像检测出来的目标框、目标类别和目标置信度。 + +## C++ 结构体 + +`fastdeploy::vision::DetectionResult` + +``` +struct DetectionResult { + std::vector> boxes; + std::vector scores; + std::vector label_ids; + void Clear(); + std::string Str(); +}; +``` + +- **boxes**: 成员变量,表示单张图片检测出来的所有目标框坐标,`boxes.size()`表示框的个数,每个框以4个float数值依次表示xmin, ymin, xmax, ymax, 即左上角和右下角坐标 +- **scores**: 成员变量,表示单张图片检测出来的所有目标置信度,其元素个数与`boxes.size()`一致 +- **label_ids**: 成员变量,表示单张图片检测出来的所有目标类别,其元素个数与`boxes.size()`一致 +- **Clear()**: 成员函数,用于清除结构体中存储的结果 +- **Str()**: 成员函数,将结构体中的信息以字符串形式输出(用于Debug) + +## Python结构体 + +`fastdeploy.vision.DetectionResult` + +- **boxes**(list of list(float)): 成员变量,表示单张图片检测出来的所有目标框坐标。boxes是一个list,其每个元素为一个长度为4的list, 表示为一个框,每个框以4个float数值依次表示xmin, ymin, xmax, ymax, 即左上角和右下角坐标 +- **scores**(list of float): 成员变量,表示单张图片检测出来的所有目标置信度 +- **label_ids(list of int): 成员变量,表示单张图片检测出来的所有目标类别 diff --git a/docs/quick_start/install.md b/docs/quick_start/install.md new file mode 100644 index 00000000000..65414606f85 --- /dev/null +++ b/docs/quick_start/install.md @@ -0,0 +1,21 @@ +# FastDeploy安装 + +## Python安装 + +首先安装FastDeploy的SDK管理工具 + +``` +pip install fastdeploy-python +``` + +然后通过命令行管理工具,根据自己的硬件安装相应SDK + +### 安装CPU Python SDK +``` +fastdeploy --install cpu +``` + +### 安装GPU Python SDK +``` +fastdeploy --install gpu +``` diff --git a/docs/quick_start/requirements.md b/docs/quick_start/requirements.md new file mode 100644 index 00000000000..462f9a5c96d --- /dev/null +++ b/docs/quick_start/requirements.md @@ -0,0 +1,21 @@ +# FastDeploy环境要求 + +## 系统平台 + +- Linux x64/aarch64 +- Windows 10 x64 +- Mac OSX (x86 10.0以上/ arm64 12.0以上) + +## 硬件 + +- Intel CPU +- Nvidia GPU + +## 软件 + +- cmake >= 3.12 +- gcc/g++ >= 8.2 +- python >= 3.6 +- Visual Studio 2019 (Windows平台) +- cuda >= 11.0 +- cudnn >= 8.0 diff --git a/new_examples/vision/detection/README.md b/new_examples/vision/detection/README.md new file mode 100644 index 00000000000..56235f101b4 --- /dev/null +++ b/new_examples/vision/detection/README.md @@ -0,0 +1,24 @@ +# 视觉模型部署 + +本目录下提供了各类视觉模型的部署,主要涵盖以下任务类型 + +| 任务类型 | 说明 | 预测结果结构体 | +| :------- | :----| :------------- | +| Detection | 目标检测,输入图像,检测图像中物体位置,并返回检测框坐标及类别和置信度 | DetectionResult | +| Segmentation | 语义分割,输入图像,给出图像中每个像素的分类及置信度 | SegmentationResult | +| Classification | 图像分类,输入图像,给出图像的分类结果和置信度 | ClassificationResult | + + +## FastDeploy API设计 + +视觉模型具有较有统一任务范式,在设计API时(包括C++/Python),FastDeploy将视觉模型的部署拆分为三个步骤 + +- 模型加载 +- 图像预处理 +- 模型推理 +- 推理结果后处理 + +FastDeploy针对飞桨的视觉套件,以及外部热门模型,提供端到端的部署服务,用户只需准备模型,按以下步骤即可完成整个模型的部署 + +- 加载模型 +- 调用`predict`接口 diff --git a/new_examples/vision/detection/yolov7/README.md b/new_examples/vision/detection/yolov7/README.md new file mode 100644 index 00000000000..e67a58f96ef --- /dev/null +++ b/new_examples/vision/detection/yolov7/README.md @@ -0,0 +1,37 @@ +# YOLOv7部署 + +## 版本依赖 + +- [YOLOv7 0.1](https://github.com/WongKinYiu/yolov7/releases/tag/v0.1) + + +## 导出ONNX模型 + +``` +#下载yolov7模型文件 +wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt + +# 导出onnx格式文件 (Tips: 对应 YOLOv7 release v0.1 代码) +python models/export.py --grid --dynamic --weights PATH/TO/yolov7.pt + +# 如果您的代码版本中有支持NMS的ONNX文件导出,请使用如下命令导出ONNX文件(请暂时不要使用 "--end2end",我们后续将支持带有NMS的ONNX模型的部署) +python export.py --grid --dynamic --weights PATH/TO/yolov7.pt + +# 移动onnx文件到demo目录 +cp PATH/TO/yolov7.onnx PATH/TO/model_zoo/vision/yolov7/ +``` + +## 预训练模型集合 + +为了方便开发者的测试,下面提供了YOLOv7导出的各系列模型,开发者可直接下载使用。 + +| 模型 | 大小 | 精度 | +| :--- | :--- | :--- | +| [YOLOv7](https://bj.bcebos.com/paddlehub/fastdeploy/yolov7.onnx) | 141MB | 51.4% | +| [YOLOv7-x] | 10MB | 51.4% | + + +## 模型部署 + +- [Python部署](python) +- [C++部署](cpp) diff --git a/new_examples/vision/detection/yolov7/cpp/.README.md.swp b/new_examples/vision/detection/yolov7/cpp/.README.md.swp new file mode 100644 index 0000000000000000000000000000000000000000..3344a53ce3c476546c56d7a2f913fa0716397697 GIT binary patch literal 12288 zcmeI2&2JmW8OFy2g0zX#2F)plq!YU^BwAe3-!0L&RV_O)DocWF=L3d8QRGTmi`-?G zONnyfMzti%wmvA;u_M@SD#f&=#I~5kj-rU7{3oP`UW&E|&~SE1AV87km_z%_4r!4} zgIs!PFbh58&euCLzxSOTEjXS2mkuAH`;^@T*OP?&`-{&zH*Y#mK6{Q3(=aTa{Y_Wr zse#kp#6ZH^9Z!0D4ZS<2>D{{&o~?9-v`A738!;I1b%hdEM2$y{0iXXT{-62|A>Aa> zy4oL9Q=wQqswRAWT0%2)UqrRku*L5NjHuDK&lirzeeDMun~oe*Vv%nel`NM8B!OB2 zPmoVbAZUg&R+6Y^K+PteEE4QMa)=f?<{hNhr1P(So@=t=04?-BAB=qfY= zjX@WnHfa94gy_&7Xg9PA`VsUj^cv>=Tj&fFfnJ0-k4=!gBmqf45|9MGtpvREL~;IH zasGn6Fvs2;E4?*re=z3cF4=EhKUt?JzUse-5l;9*ajkqpEoiEVWYkLd>Itm04Ol&f zUK(0(^6$GX<8FpqCTtgSFg#JQS`VW8>3-^=9#29$t$I9leK_xCm_O5WYkz%!mIkjD zAB@^}Gc0}9zWi?C!J_^8M;IT=3_DZjP~gFZ+@D2Jac;rMteoiAB34fW?NPPv9_wTSE#+p4iy1b!T3B8=v3Iw> zf#TyNjtOnuN}cy|_W3C`J;Ty7rO|ZNsP$MPs2Rzi)q^qg7}1Ew6QC_ej&2wrs+4BV z!}!DLh7G-|wv~Nj5ZLXxb54G-@L-Ou+~QNuWi~VvzKf$)=`{ugmjai|vKgDkFHVk0-%wKX=x{bJu+u!qy7 z>w}>D;>!n+^UD|7?b%hfnkM@J$Kw&6_ITW6bwJvaH(B}yT2z~KuH8pI&lz2*=JDy^ zSy=1n=)mX3t1D`REczAC(dVgO*;D_u4Qf5+*50|N0o7k^ZEo$`7YMYOYD5cLb>Y51 z;7G{wP%ZWq*d8kU$J_DIA-cBGazi$nM~w3o!Pi5~h^WzEmtn?W%2Rj9G;~YVBUlsy zihu?jB5=X8wWZ~i3QE_6nba*UrUs2TKSBy4?INSqO~VqKnsjbw?a2i!rqaj+Vx-z$ zi=nGXvx$49(NX911<{eHR~}?MWQMQ;y0j=?y9vwWM%|@qUl}drmWm%{+1fBSlD||v z?8<20SVGzQ@?M=qtT=D4u)A+KcWxDy-$&mpovkHO-B;OWRbPZug%A<05w+OG*Tq8?(ew10mG6u%7s|Y468JDN~L=;qcOmUx8;t2tfuxQ0G+i11N z5FV9+K!6*n-_Wea729a7tI)cpyso}sLvx_U3FB?F$^mG!Wy{yqTc2SASwDju2#9mW zfj~dbEca^qfR+pd1hhJLt~@+N8qYxfFf6C&f$iJHg2amum?%C??^4ZRM2#6#oK;o~ zT~ARzzji36;@yPALcJ>Wo+4rQMp3A@=}_a*V@(I!np@=R{#q4Fg%+Z|+wP!!FCl=Z2AWKg>lUerG z5GLW}vVGj|6IQgm(S=88-Aino4ezjyY(^(AVF@|wPWoz%j;!35x2K2t@?RAZ`c zxjj)z2oRMgabW_6@tk+&7O|~@E9P3>j_=+qy_3Pab%iBL?DSM&?OKN$LrOT@!5-W$ z4gOKU6yXQU@QCk)w&R>z4qL%R+`~`eUxZ_-o-oYzV_4*9D8|5dy$tsqU$~??a?9mz GA^8tx)1(go literal 0 HcmV?d00001 diff --git a/new_examples/vision/detection/yolov7/cpp/CMakeLists.txt b/new_examples/vision/detection/yolov7/cpp/CMakeLists.txt new file mode 100644 index 00000000000..fea1a2888b9 --- /dev/null +++ b/new_examples/vision/detection/yolov7/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +PROJECT(infer_demo C CXX) +CMAKE_MINIMUM_REQUIRED (VERSION 3.12) + +# 指定下载解压后的fastdeploy库路径 +option(FASTDEPLOY_INSTALL_DIR "Path of downloaded fastdeploy sdk.") + +include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake) + +# 添加FastDeploy依赖头文件 +include_directories(${FASTDEPLOY_INCS}) + +add_executable(infer_demo ${PROJECT_SOURCE_DIR}/infer.cc) +# 添加FastDeploy库依赖 +target_link_libraries(infer_demo ${FASTDEPLOY_LIBS}) diff --git a/new_examples/vision/detection/yolov7/cpp/README.md b/new_examples/vision/detection/yolov7/cpp/README.md new file mode 100644 index 00000000000..29eea1c1d2d --- /dev/null +++ b/new_examples/vision/detection/yolov7/cpp/README.md @@ -0,0 +1,65 @@ +# YOLOv7 C++部署示例 + +本目录下提供`infer.cc`快速完成YOLOv7在CPU/GPU,以及GPU上通过TensorRT加速部署的示例。 + +在部署前,需确认以下两个步骤 + +- 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../docs/quick_start/requirements.md) +- 2. 根据开发环境,下载预编译部署库,参考[FastDeploy预编译库](../../docs/compile/prebuild_libraries.md) + +以Linux上CPU推理为例,在本目录执行如下命令即可完成编译测试 + +``` +mkdir build +cd build +wget https://xxx.tgz +tar xvf fastdeploy-linux-x64-0.2.0.tgz +cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-0.2.0 +make -j + +./infer_demo yolov7.onnx 000001.jpg +``` + +## YOLOv7 C++接口 + +### YOLOv7类 +``` +fastdeploy::vision::wongkinyiu::YOLOv7( + const string& model_file, + const string& params_file = "", + const RuntimeOption& runtime_option = RuntimeOption(), + const Frontend& model_format = Frontend::ONNX) +``` +YOLOv7模型加载和初始化,其中model_file为导出的ONNX模型格式。 + +**参数** + +> * **model_file**(str): 模型文件路径 +> * **params_file**(str): 参数文件路径,当模型格式为ONNX时,此参数传入空字符串即可 +> * **runtime_option**(RuntimeOption): 后端推理配置,默认为None,即采用默认配置 +> * **model_format**(Frontend): 模型格式,默认为ONNX格式 + +#### Predict函数 +> ``` +> YOLOv7::Predict(cv::Mat* im, DetectionResult* result, +> float conf_threshold = 0.25, +> float nms_iou_threshold = 0.5) +> ``` +> 模型预测接口,输入图像直接输出检测结果。 +> +> **参数** +> +> > * **im**: 输入图像,注意需为HWC,BGR格式 +> > * **result**: 检测结果,包括检测框,各个框的置信度, DetectionResult说明参考[视觉模型预测结果](../../../../../docs/api/vision_results/) +> > * **conf_threshold**: 检测框置信度过滤阈值 +> > * **nms_iou_threshold**: NMS处理过程中iou阈值 + + +### 类成员变量 + +> > * **size**(vector): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] + + +- [模型介绍](../../) +- [Python部署](../python) +- [视觉模型预测结果](../../../../../docs/api/vision_results/) diff --git a/new_examples/vision/detection/yolov7/cpp/infer.cc b/new_examples/vision/detection/yolov7/cpp/infer.cc new file mode 100644 index 00000000000..d5d4ad98123 --- /dev/null +++ b/new_examples/vision/detection/yolov7/cpp/infer.cc @@ -0,0 +1,106 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "fastdeploy/vision.h" + +void CpuInfer(const std::string& model_file, const std::string& image_file) { + auto model = fastdeploy::vision::detection::YOLOv7(model_file); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::DetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + + auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +void GpuInfer() { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + auto model = fastdeploy::vision::detection::YOLOv7(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::DetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + + auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +void TrtInfer() { + auto option = fastdeploy::RuntimeOption(); + option.UseGpu(); + option.UseTrtBackend(); + option.SetTrtInputShape("image", {1, 3, 320, 320}, {1, 3, 640, 640}, + {1, 3, 1280, 1280}); + auto model = fastdeploy::vision::detection::YOLOv7(model_file, "", option); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + + auto im = cv::imread(image_file); + auto im_bak = im.clone(); + + fastdeploy::vision::DetectionResult res; + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + + auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); + cv::imwrite("vis_result.jpg", vis_im); + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; +} + +int main(int argc, char* argv[]) { + if (argc < 4) { + std::cout << "Usage: infer_demo path/to/model path/to/image run_option, " + "e.g ./infer_model ./yolov7.onnx ./test.jpeg 0" + << std::endl; + std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " + "with gpu; 2: run with gpu and use tensorrt backend." + << std::endl; + return -1; + } + + if (std::atoi(argv[3]) == 0) { + CpuInfer(); + } else if (std::atoi(argv[3]) == 1) { + GpuInfer(); + } else if (std::atoi(argv[3]) == 2) { + TrtInfer(); + } + return 0; +} diff --git a/new_examples/vision/detection/yolov7/python/README.md b/new_examples/vision/detection/yolov7/python/README.md new file mode 100644 index 00000000000..5fb3c83f24b --- /dev/null +++ b/new_examples/vision/detection/yolov7/python/README.md @@ -0,0 +1,61 @@ +# YOLOv7 Python部署示例 + +如未安装FastDeploy Python环境,请参考下面文档安装。 + +- [FastDeploy Python安装](../../../../../docs/quick_start/install.md) + +本目录下提供`infer.py`快速完成YOLOv7在CPU/GPU,以及GPU上通过TensorRT加速部署的示例。执行如下脚本即可完成 + +``` +#下载yolov7模型文件和测试图片 +wget https://bj.bcebos.com/paddlehub/fastdeploy/yolov7.onnx +wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000087038.jpg + +python infer.py --model yolov7.onnx --image 000000087038.jpg --device cpu +``` + +运行完成可视化结果如下图所示 + + +## YOLOv7 Python接口 + +``` +fastdeploy.vision.detection.YOLOv7(model_file, params_file=None, runtime_option=None, model_format=Frontend.ONNX) +``` + +YOLOv7模型加载和初始化,其中model_file为导出的ONNX模型格式 + +**参数** + +> * **model_file**(str): 模型文件路径 +> * **params_file**(str): 参数文件路径,当模型格式为ONNX格式时,此参数无需设定 +> * **runtime_option**(RuntimeOption): 后端推理配置,默认为None,即采用默认配置 +> * **model_format**(Frontend): 模型格式,默认为ONNX + +### predict函数 + +> ``` +> YOLOv7.predict(image_data, conf_threshold=0.25, nms_iou_threshold=0.5) +> ``` +> 模型预测结口,输入图像直接输出检测结果。 +> +> **参数** +> +> > * **image_data**(np.ndarray): 输入数据,注意需为HWC,BGR格式 +> > * **conf_threshold**(float): 检测框置信度过滤阈值 +> > * **nms_iou_threshold**(float): NMS处理过程中iou阈值 + +> **返回** +> +> > 返回`fastdeploy.vision.DetectionResult`结构体,结构体说明参考文档[视觉模型预测结果](../../../../../docs/api/vision_results/) + +### 类成员属性 + +> > * **size**(list | tuple): 通过此参数修改预处理过程中resize的大小,包含两个整型元素,表示[width, height], 默认值为[640, 640] + + +## 其它文档 + +- [YOLOv7 模型介绍](..) +- [YOLOv7 C++部署](../cpp) +- [模型预测结果说明](../../../../../docs/api/vision_results/) diff --git a/new_examples/vision/detection/yolov7/python/infer.py b/new_examples/vision/detection/yolov7/python/infer.py new file mode 100644 index 00000000000..acc8c3a5094 --- /dev/null +++ b/new_examples/vision/detection/yolov7/python/infer.py @@ -0,0 +1,52 @@ +import fastdeploy as fd +import cv2 + + +def parse_arguments(): + import argparse + import ast + parser = argparse.ArgumentParser() + parser.add_argument( + "--model", required=True, help="Path of yolov7 onnx model.") + parser.add_argument( + "--image", required=True, help="Path of test image file.") + parser.add_argument( + "--device", + type=str, + default='cpu', + help="Type of inference device, support 'cpu' or 'gpu'.") + parser.add_argument( + "--use_trt", + type=ast.literal_eval, + default=False, + help="Wether to use tensorrt.") + return parser.parse_args() + + +def build_option(args): + option = fd.RuntimeOption() + + if args.device.lower() == "gpu": + option.use_gpu() + + if args.use_trt: + option.use_trt_backend() + option.set_trt_input_shape("image", [1, 3, 320, 320], + [1, 3, 640, 640], [1, 3, 1280, 1280]) + return option + + +args = parse_arguments() + +# 配置runtime,加载模型 +runtime_option = build_option(args) +model = fd.vision.detection.YOLOv7(args.model, runtime_option=runtime_option) + +# 预测图片检测结果 +im = cv2.imread(args.image) +result = model.predict(im) + +# 预测结果可视化 +vis_im = fd.vision.vis_detection(im, result) +cv2.imwrite("visualized_result.jpg", vis_im) +print("Visualized result save in ./visualized_result.jpg") From 3319602156881a788ab7787442e96161853993bb Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 9 Aug 2022 14:14:17 +0800 Subject: [PATCH 3/6] Delete .README.md.swp --- .../vision/detection/yolov7/cpp/.README.md.swp | Bin 12288 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 new_examples/vision/detection/yolov7/cpp/.README.md.swp diff --git a/new_examples/vision/detection/yolov7/cpp/.README.md.swp b/new_examples/vision/detection/yolov7/cpp/.README.md.swp deleted file mode 100644 index 3344a53ce3c476546c56d7a2f913fa0716397697..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2&2JmW8OFy2g0zX#2F)plq!YU^BwAe3-!0L&RV_O)DocWF=L3d8QRGTmi`-?G zONnyfMzti%wmvA;u_M@SD#f&=#I~5kj-rU7{3oP`UW&E|&~SE1AV87km_z%_4r!4} zgIs!PFbh58&euCLzxSOTEjXS2mkuAH`;^@T*OP?&`-{&zH*Y#mK6{Q3(=aTa{Y_Wr zse#kp#6ZH^9Z!0D4ZS<2>D{{&o~?9-v`A738!;I1b%hdEM2$y{0iXXT{-62|A>Aa> zy4oL9Q=wQqswRAWT0%2)UqrRku*L5NjHuDK&lirzeeDMun~oe*Vv%nel`NM8B!OB2 zPmoVbAZUg&R+6Y^K+PteEE4QMa)=f?<{hNhr1P(So@=t=04?-BAB=qfY= zjX@WnHfa94gy_&7Xg9PA`VsUj^cv>=Tj&fFfnJ0-k4=!gBmqf45|9MGtpvREL~;IH zasGn6Fvs2;E4?*re=z3cF4=EhKUt?JzUse-5l;9*ajkqpEoiEVWYkLd>Itm04Ol&f zUK(0(^6$GX<8FpqCTtgSFg#JQS`VW8>3-^=9#29$t$I9leK_xCm_O5WYkz%!mIkjD zAB@^}Gc0}9zWi?C!J_^8M;IT=3_DZjP~gFZ+@D2Jac;rMteoiAB34fW?NPPv9_wTSE#+p4iy1b!T3B8=v3Iw> zf#TyNjtOnuN}cy|_W3C`J;Ty7rO|ZNsP$MPs2Rzi)q^qg7}1Ew6QC_ej&2wrs+4BV z!}!DLh7G-|wv~Nj5ZLXxb54G-@L-Ou+~QNuWi~VvzKf$)=`{ugmjai|vKgDkFHVk0-%wKX=x{bJu+u!qy7 z>w}>D;>!n+^UD|7?b%hfnkM@J$Kw&6_ITW6bwJvaH(B}yT2z~KuH8pI&lz2*=JDy^ zSy=1n=)mX3t1D`REczAC(dVgO*;D_u4Qf5+*50|N0o7k^ZEo$`7YMYOYD5cLb>Y51 z;7G{wP%ZWq*d8kU$J_DIA-cBGazi$nM~w3o!Pi5~h^WzEmtn?W%2Rj9G;~YVBUlsy zihu?jB5=X8wWZ~i3QE_6nba*UrUs2TKSBy4?INSqO~VqKnsjbw?a2i!rqaj+Vx-z$ zi=nGXvx$49(NX911<{eHR~}?MWQMQ;y0j=?y9vwWM%|@qUl}drmWm%{+1fBSlD||v z?8<20SVGzQ@?M=qtT=D4u)A+KcWxDy-$&mpovkHO-B;OWRbPZug%A<05w+OG*Tq8?(ew10mG6u%7s|Y468JDN~L=;qcOmUx8;t2tfuxQ0G+i11N z5FV9+K!6*n-_Wea729a7tI)cpyso}sLvx_U3FB?F$^mG!Wy{yqTc2SASwDju2#9mW zfj~dbEca^qfR+pd1hhJLt~@+N8qYxfFf6C&f$iJHg2amum?%C??^4ZRM2#6#oK;o~ zT~ARzzji36;@yPALcJ>Wo+4rQMp3A@=}_a*V@(I!np@=R{#q4Fg%+Z|+wP!!FCl=Z2AWKg>lUerG z5GLW}vVGj|6IQgm(S=88-Aino4ezjyY(^(AVF@|wPWoz%j;!35x2K2t@?RAZ`c zxjj)z2oRMgabW_6@tk+&7O|~@E9P3>j_=+qy_3Pab%iBL?DSM&?OKN$LrOT@!5-W$ z4gOKU6yXQU@QCk)w&R>z4qL%R+`~`eUxZ_-o-oYzV_4*9D8|5dy$tsqU$~??a?9mz GA^8tx)1(go From 318c14e5eb6dd344212df7940e7bfb7a7b1fb497 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 9 Aug 2022 14:16:08 +0800 Subject: [PATCH 4/6] Update README.md --- new_examples/vision/detection/yolov7/python/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/new_examples/vision/detection/yolov7/python/README.md b/new_examples/vision/detection/yolov7/python/README.md index 5fb3c83f24b..3b84121d615 100644 --- a/new_examples/vision/detection/yolov7/python/README.md +++ b/new_examples/vision/detection/yolov7/python/README.md @@ -57,5 +57,5 @@ YOLOv7模型加载和初始化,其中model_file为导出的ONNX模型格式 ## 其它文档 - [YOLOv7 模型介绍](..) -- [YOLOv7 C++部署](../cpp) +- [YOLOv7 C++部署(../cpp) - [模型预测结果说明](../../../../../docs/api/vision_results/) From e3593c50158716b23dfc979fb2a8129c9412d398 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 9 Aug 2022 14:16:25 +0800 Subject: [PATCH 5/6] Update README.md --- new_examples/vision/detection/yolov7/python/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/new_examples/vision/detection/yolov7/python/README.md b/new_examples/vision/detection/yolov7/python/README.md index 3b84121d615..ea941929a15 100644 --- a/new_examples/vision/detection/yolov7/python/README.md +++ b/new_examples/vision/detection/yolov7/python/README.md @@ -57,5 +57,5 @@ YOLOv7模型加载和初始化,其中model_file为导出的ONNX模型格式 ## 其它文档 - [YOLOv7 模型介绍](..) -- [YOLOv7 C++部署(../cpp) +- [YOLOv7 C++部署](../cpp) - [模型预测结果说明](../../../../../docs/api/vision_results/) From 86d1511e053c6b9ff4bd7a0cf694c3c40ddfcde5 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 9 Aug 2022 14:17:56 +0800 Subject: [PATCH 6/6] Update README.md --- new_examples/vision/detection/yolov7/python/README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/new_examples/vision/detection/yolov7/python/README.md b/new_examples/vision/detection/yolov7/python/README.md index ea941929a15..6f9e4536642 100644 --- a/new_examples/vision/detection/yolov7/python/README.md +++ b/new_examples/vision/detection/yolov7/python/README.md @@ -1,8 +1,9 @@ # YOLOv7 Python部署示例 -如未安装FastDeploy Python环境,请参考下面文档安装。 +在部署前,需确认以下两个步骤 -- [FastDeploy Python安装](../../../../../docs/quick_start/install.md) +- 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../docs/quick_start/requirements.md) +- 2. FastDeploy Python安装,参考[FastDeploy Python安装](../../../../../docs/quick_start/install.md) 本目录下提供`infer.py`快速完成YOLOv7在CPU/GPU,以及GPU上通过TensorRT加速部署的示例。执行如下脚本即可完成