From 56bdbe5b0df177df64adbe9e77bf72d4d63024aa Mon Sep 17 00:00:00 2001 From: jiangjiajun Date: Wed, 3 Aug 2022 08:15:23 +0000 Subject: [PATCH] add more ppdet models --- .../vision/common/processors/pad_to_size.cc | 141 ++++++++++++++++++ .../vision/common/processors/pad_to_size.h | 46 ++++++ .../vision/common/processors/stride_pad.cc | 124 +++++++++++++++ .../vision/common/processors/stride_pad.h | 44 ++++++ .../vision/common/processors/transform.h | 2 + .../vision/ppdet/build_preprocess.cc | 78 ++++++++++ csrcs/fastdeploy/vision/ppdet/centernet.cc | 25 ---- csrcs/fastdeploy/vision/ppdet/centernet.h | 19 --- csrcs/fastdeploy/vision/ppdet/model.h | 16 ++ csrcs/fastdeploy/vision/ppdet/picodet.cc | 14 ++ csrcs/fastdeploy/vision/ppdet/picodet.h | 14 ++ csrcs/fastdeploy/vision/ppdet/ppdet_pybind.cc | 16 ++ csrcs/fastdeploy/vision/ppdet/ppyolo.cc | 18 ++- csrcs/fastdeploy/vision/ppdet/ppyoloe.cc | 34 +++-- csrcs/fastdeploy/vision/ppdet/ppyoloe.h | 19 +++ csrcs/fastdeploy/vision/ppdet/rcnn.cc | 89 +++++++++++ csrcs/fastdeploy/vision/ppdet/rcnn.h | 39 +++++ csrcs/fastdeploy/vision/ppdet/yolov3.cc | 18 ++- csrcs/fastdeploy/vision/ppdet/yolov3.h | 14 ++ csrcs/fastdeploy/vision/ppdet/yolox.cc | 74 +++++++++ csrcs/fastdeploy/vision/ppdet/yolox.h | 35 +++++ csrcs/fastdeploy/vision/ppogg/yolov5lite.cc | 1 - fastdeploy/vision/ppdet/__init__.py | 66 +++++++- 23 files changed, 883 insertions(+), 63 deletions(-) create mode 100644 csrcs/fastdeploy/vision/common/processors/pad_to_size.cc create mode 100644 csrcs/fastdeploy/vision/common/processors/pad_to_size.h create mode 100644 csrcs/fastdeploy/vision/common/processors/stride_pad.cc create mode 100644 csrcs/fastdeploy/vision/common/processors/stride_pad.h create mode 100644 csrcs/fastdeploy/vision/ppdet/build_preprocess.cc delete mode 100644 csrcs/fastdeploy/vision/ppdet/centernet.cc delete mode 100644 csrcs/fastdeploy/vision/ppdet/centernet.h create mode 100644 csrcs/fastdeploy/vision/ppdet/rcnn.cc create mode 100644 csrcs/fastdeploy/vision/ppdet/rcnn.h create mode 100644 csrcs/fastdeploy/vision/ppdet/yolox.cc create mode 100644 csrcs/fastdeploy/vision/ppdet/yolox.h diff --git a/csrcs/fastdeploy/vision/common/processors/pad_to_size.cc b/csrcs/fastdeploy/vision/common/processors/pad_to_size.cc new file mode 100644 index 0000000000..d4cbacd879 --- /dev/null +++ b/csrcs/fastdeploy/vision/common/processors/pad_to_size.cc @@ -0,0 +1,141 @@ +// 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/common/processors/pad_to_size.h" + +namespace fastdeploy { +namespace vision { + +bool PadToSize::CpuRun(Mat* mat) { + if (mat->layout != Layout::HWC) { + FDERROR << "PadToSize: The input data must be Layout::HWC format!" + << std::endl; + return false; + } + if (mat->Channels() > 4) { + FDERROR << "PadToSize: Only support channels <= 4." << std::endl; + return false; + } + if (mat->Channels() != value_.size()) { + FDERROR + << "PadToSize: Require input channels equals to size of padding value, " + "but now channels = " + << mat->Channels() << ", the size of padding values = " << value_.size() + << "." << std::endl; + return false; + } + int origin_w = mat->Width(); + int origin_h = mat->Height(); + if (origin_w > width_) { + FDERROR << "PadToSize: the input width:" << origin_w + << " is greater than the target width: " << width_ << "." + << std::endl; + return false; + } + if (origin_h > height_) { + FDERROR << "PadToSize: the input height:" << origin_h + << " is greater than the target height: " << height_ << "." + << std::endl; + return false; + } + if (origin_w == width_ && origin_h == height_) { + return true; + } + + cv::Mat* im = mat->GetCpuMat(); + cv::Scalar value; + if (value_.size() == 1) { + value = cv::Scalar(value_[0]); + } else if (value_.size() == 2) { + value = cv::Scalar(value_[0], value_[1]); + } else if (value_.size() == 3) { + value = cv::Scalar(value_[0], value_[1], value_[2]); + } else { + value = cv::Scalar(value_[0], value_[1], value_[2], value_[3]); + } + // top, bottom, left, right + cv::copyMakeBorder(*im, *im, 0, height_ - origin_h, 0, width_ - origin_w, + cv::BORDER_CONSTANT, value); + mat->SetHeight(height_); + mat->SetWidth(width_); + return true; +} + +#ifdef ENABLE_OPENCV_CUDA +bool PadToSize::GpuRun(Mat* mat) { + if (mat->layout != Layout::HWC) { + FDERROR << "PadToSize: The input data must be Layout::HWC format!" + << std::endl; + return false; + } + if (mat->Channels() > 4) { + FDERROR << "PadToSize: Only support channels <= 4." << std::endl; + return false; + } + if (mat->Channels() != value_.size()) { + FDERROR + << "PadToSize: Require input channels equals to size of padding value, " + "but now channels = " + << mat->Channels() << ", the size of padding values = " << value_.size() + << "." << std::endl; + return false; + } + + int origin_w = mat->Width(); + int origin_h = mat->Height(); + if (origin_w > width_) { + FDERROR << "PadToSize: the input width:" << origin_w + << " is greater than the target width: " << width_ << "." + << std::endl; + return false; + } + if (origin_h > height_) { + FDERROR << "PadToSize: the input height:" << origin_h + << " is greater than the target height: " << height_ << "." + << std::endl; + return false; + } + if (origin_w == width_ && origin_h == height_) { + return true; + } + + cv::cuda::GpuMat* im = mat->GetGpuMat(); + cv::Scalar value; + if (value_.size() == 1) { + value = cv::Scalar(value_[0]); + } else if (value_.size() == 2) { + value = cv::Scalar(value_[0], value_[1]); + } else if (value_.size() == 3) { + value = cv::Scalar(value_[0], value_[1], value_[2]); + } else { + value = cv::Scalar(value_[0], value_[1], value_[2], value_[3]); + } + + // top, bottom, left, right + cv::cuda::copyMakeBorder(*im, *im, 0, height_ - origin_h, 0, + width_ - origin_w, cv::BORDER_CONSTANT, value); + mat->SetHeight(height_); + mat->SetWidth(width_); + return true; +} +#endif + +bool PadToSize::Run(Mat* mat, int width, int height, + const std::vector& value, ProcLib lib) { + auto p = PadToSize(width, height, value); + return p(mat, lib); +} + +} // namespace vision +} // namespace fastdeploy diff --git a/csrcs/fastdeploy/vision/common/processors/pad_to_size.h b/csrcs/fastdeploy/vision/common/processors/pad_to_size.h new file mode 100644 index 0000000000..ece0158f7b --- /dev/null +++ b/csrcs/fastdeploy/vision/common/processors/pad_to_size.h @@ -0,0 +1,46 @@ +// 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. + +#pragma once + +#include "fastdeploy/vision/common/processors/base.h" + +namespace fastdeploy { +namespace vision { + +class PadToSize : public Processor { + public: + // only support pad with left-top padding mode + PadToSize(int width, int height, const std::vector& value) { + width_ = width; + height_ = height; + value_ = value; + } + bool CpuRun(Mat* mat); +#ifdef ENABLE_OPENCV_CUDA + bool GpuRun(Mat* mat); +#endif + std::string Name() { return "PadToSize"; } + + static bool Run(Mat* mat, int width, int height, + const std::vector& value, + ProcLib lib = ProcLib::OPENCV_CPU); + + private: + int width_; + int height_; + std::vector value_; +}; +} // namespace vision +} // namespace fastdeploy diff --git a/csrcs/fastdeploy/vision/common/processors/stride_pad.cc b/csrcs/fastdeploy/vision/common/processors/stride_pad.cc new file mode 100644 index 0000000000..8597c83758 --- /dev/null +++ b/csrcs/fastdeploy/vision/common/processors/stride_pad.cc @@ -0,0 +1,124 @@ +// 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/common/processors/stride_pad.h" + +namespace fastdeploy { +namespace vision { + +bool StridePad::CpuRun(Mat* mat) { + if (mat->layout != Layout::HWC) { + FDERROR << "StridePad: The input data must be Layout::HWC format!" + << std::endl; + return false; + } + if (mat->Channels() > 4) { + FDERROR << "StridePad: Only support channels <= 4." << std::endl; + return false; + } + if (mat->Channels() != value_.size()) { + FDERROR + << "StridePad: Require input channels equals to size of padding value, " + "but now channels = " + << mat->Channels() << ", the size of padding values = " << value_.size() + << "." << std::endl; + return false; + } + int origin_w = mat->Width(); + int origin_h = mat->Height(); + + int pad_h = (mat->Height() / stride_) * stride_ + + (mat->Height() % stride_ != 0) * stride_ - mat->Height(); + int pad_w = (mat->Width() / stride_) * stride_ + + (mat->Width() % stride_ != 0) * stride_ - mat->Width(); + if (pad_h == 0 && pad_w == 0) { + return true; + } + cv::Mat* im = mat->GetCpuMat(); + cv::Scalar value; + if (value_.size() == 1) { + value = cv::Scalar(value_[0]); + } else if (value_.size() == 2) { + value = cv::Scalar(value_[0], value_[1]); + } else if (value_.size() == 3) { + value = cv::Scalar(value_[0], value_[1], value_[2]); + } else { + value = cv::Scalar(value_[0], value_[1], value_[2], value_[3]); + } + // top, bottom, left, right + cv::copyMakeBorder(*im, *im, 0, pad_h, 0, pad_w, cv::BORDER_CONSTANT, value); + mat->SetHeight(origin_h + pad_h); + mat->SetWidth(origin_w + pad_w); + return true; +} + +#ifdef ENABLE_OPENCV_CUDA +bool StridePad::GpuRun(Mat* mat) { + if (mat->layout != Layout::HWC) { + FDERROR << "StridePad: The input data must be Layout::HWC format!" + << std::endl; + return false; + } + if (mat->Channels() > 4) { + FDERROR << "StridePad: Only support channels <= 4." << std::endl; + return false; + } + if (mat->Channels() != value_.size()) { + FDERROR + << "StridePad: Require input channels equals to size of padding value, " + "but now channels = " + << mat->Channels() << ", the size of padding values = " << value_.size() + << "." << std::endl; + return false; + } + + int origin_w = mat->Width(); + int origin_h = mat->Height(); + int pad_h = (mat->Height() / stride_) * stride_ + + (mat->Height() % stride_ != 0) * stride_; + int pad_w = (mat->Width() / stride_) * stride_ + + (mat->Width() % stride_ != 0) * stride_; + if (pad_h == 0 && pad_w == 0) { + return true; + } + + cv::cuda::GpuMat* im = mat->GetGpuMat(); + cv::Scalar value; + if (value_.size() == 1) { + value = cv::Scalar(value_[0]); + } else if (value_.size() == 2) { + value = cv::Scalar(value_[0], value_[1]); + } else if (value_.size() == 3) { + value = cv::Scalar(value_[0], value_[1], value_[2]); + } else { + value = cv::Scalar(value_[0], value_[1], value_[2], value_[3]); + } + + // top, bottom, left, right + cv::cuda::copyMakeBorder(*im, *im, 0, pad_h, 0, pad_w, cv::BORDER_CONSTANT, + value); + mat->SetHeight(origin_h + pad_h); + mat->SetWidth(origin_w + pad_w); + return true; +} +#endif + +bool StridePad::Run(Mat* mat, int stride, const std::vector& value, + ProcLib lib) { + auto p = StridePad(stride, value); + return p(mat, lib); +} + +} // namespace vision +} // namespace fastdeploy diff --git a/csrcs/fastdeploy/vision/common/processors/stride_pad.h b/csrcs/fastdeploy/vision/common/processors/stride_pad.h new file mode 100644 index 0000000000..c002ca697b --- /dev/null +++ b/csrcs/fastdeploy/vision/common/processors/stride_pad.h @@ -0,0 +1,44 @@ +// 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. + +#pragma once + +#include "fastdeploy/vision/common/processors/base.h" + +namespace fastdeploy { +namespace vision { + +class StridePad : public Processor { + public: + // only support pad with left-top padding mode + StridePad(int stride, const std::vector& value) { + stride_ = stride; + value_ = value; + } + bool CpuRun(Mat* mat); +#ifdef ENABLE_OPENCV_CUDA + bool GpuRun(Mat* mat); +#endif + std::string Name() { return "StridePad"; } + + static bool Run(Mat* mat, int stride, + const std::vector& value = std::vector(), + ProcLib lib = ProcLib::OPENCV_CPU); + + private: + int stride_ = 32; + std::vector value_; +}; +} // namespace vision +} // namespace fastdeploy diff --git a/csrcs/fastdeploy/vision/common/processors/transform.h b/csrcs/fastdeploy/vision/common/processors/transform.h index 08073b4e42..fed3d0c9a2 100644 --- a/csrcs/fastdeploy/vision/common/processors/transform.h +++ b/csrcs/fastdeploy/vision/common/processors/transform.h @@ -21,5 +21,7 @@ #include "fastdeploy/vision/common/processors/hwc2chw.h" #include "fastdeploy/vision/common/processors/normalize.h" #include "fastdeploy/vision/common/processors/pad.h" +#include "fastdeploy/vision/common/processors/pad_to_size.h" #include "fastdeploy/vision/common/processors/resize.h" #include "fastdeploy/vision/common/processors/resize_by_short.h" +#include "fastdeploy/vision/common/processors/stride_pad.h" diff --git a/csrcs/fastdeploy/vision/ppdet/build_preprocess.cc b/csrcs/fastdeploy/vision/ppdet/build_preprocess.cc new file mode 100644 index 0000000000..ee3b6a16a6 --- /dev/null +++ b/csrcs/fastdeploy/vision/ppdet/build_preprocess.cc @@ -0,0 +1,78 @@ +// 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/ppdet/ppyoloe.h" + +bool BuildPreprocessPipelineFromConfig( + std::vector>* processors, + const std::string& config_file) { + processors->clear(); + YAML::Node cfg; + try { + cfg = YAML::LoadFile(config_file); + } catch (YAML::BadFile& e) { + FDERROR << "Failed to load yaml file " << config_file_ + << ", maybe you should check this file." << std::endl; + return false; + } + + processors->push_back(std::make_shared()); + + for (const auto& op : cfg["Preprocess"]) { + std::string op_name = op["type"].as(); + if (op_name == "NormalizeImage") { + auto mean = op["mean"].as>(); + auto std = op["std"].as>(); + bool is_scale = op["is_scale"].as(); + processors->push_back(std::make_shared(mean, std, is_scale)); + } else if (op_name == "Resize") { + bool keep_ratio = op["keep_ratio"].as(); + auto target_size = op["target_size"].as>(); + int interp = op["interp"].as(); + FDASSERT(target_size.size(), + "Require size of target_size be 2, but now it's " + + std::to_string(target_size.size()) + "."); + if (!keep_ratio) { + int width = target_size[1]; + int height = target_size[0]; + processors->push_back( + std::make_shared(width, height, -1.0, -1.0, interp, false)); + } else { + int min_target_size = std::min(target_size[0], target_size[1]); + int max_target_size = std::max(target_size[0], target_size[1]); + processors->push_back(std::make_shared( + min_target_size, interp, true, max_target_size)); + } + } else if (op_name == "Permute") { + // Do nothing, do permute as the last operation + continue; + } else if (op_name == "Pad") { + auto size = op["size"].as>(); + auto value = op["fill_value"].as>(); + processors->push_back(std::make_shared("float")); + processors->push_back( + std::make_shared(size[1], size[0], value)); + } else if (op_name == "PadStride") { + auto stride = op["stride"].as(); + processors->push_back( + std::make_shared(stride, std::vector(3, 0))); + } else { + FDERROR << "Unexcepted preprocess operator: " << op_name << "." + << std::endl; + return false; + } + } + processors->push_back(std::make_shared()); + return true; +} diff --git a/csrcs/fastdeploy/vision/ppdet/centernet.cc b/csrcs/fastdeploy/vision/ppdet/centernet.cc deleted file mode 100644 index 259ecf620d..0000000000 --- a/csrcs/fastdeploy/vision/ppdet/centernet.cc +++ /dev/null @@ -1,25 +0,0 @@ -#include "fastdeploy/vision/ppdet/centernet.h" - -namespace fastdeploy { -namespace vision { -namespace ppdet { - -CenterNet::CenterNet(const std::string& model_file, - const std::string& params_file, - const std::string& config_file, - const RuntimeOption& custom_option, - const Frontend& model_format) { - config_file_ = config_file; - valid_cpu_backends = {Backend::PDINFER}; - valid_gpu_backends = {Backend::PDINFER}; - has_nms_ = true; - runtime_option = custom_option; - runtime_option.model_format = model_format; - runtime_option.model_file = model_file; - runtime_option.params_file = params_file; - initialized = Initialize(); -} - -} // namespace ppdet -} // namespace vision -} // namespace fastdeploy diff --git a/csrcs/fastdeploy/vision/ppdet/centernet.h b/csrcs/fastdeploy/vision/ppdet/centernet.h deleted file mode 100644 index a6ae756cd1..0000000000 --- a/csrcs/fastdeploy/vision/ppdet/centernet.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once -#include "fastdeploy/vision/ppdet/ppyolo.h" - -namespace fastdeploy { -namespace vision { -namespace ppdet { - -class FASTDEPLOY_DECL CenterNet : public PPYOLO { - public: - CenterNet(const std::string& model_file, const std::string& params_file, - const std::string& config_file, - const RuntimeOption& custom_option = RuntimeOption(), - const Frontend& model_format = Frontend::PADDLE); - - virtual std::string ModelName() const { return "PaddleDetection/CenterNet"; } -}; -} // namespace ppdet -} // namespace vision -} // namespace fastdeploy diff --git a/csrcs/fastdeploy/vision/ppdet/model.h b/csrcs/fastdeploy/vision/ppdet/model.h index 89c59fd1a9..81bad81c4c 100644 --- a/csrcs/fastdeploy/vision/ppdet/model.h +++ b/csrcs/fastdeploy/vision/ppdet/model.h @@ -1,6 +1,22 @@ +// 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. + #pragma once #include "fastdeploy/vision/ppdet/centernet.h" #include "fastdeploy/vision/ppdet/picodet.h" #include "fastdeploy/vision/ppdet/ppyolo.h" #include "fastdeploy/vision/ppdet/ppyoloe.h" +#include "fastdeploy/vision/ppdet/rcnn.h" #include "fastdeploy/vision/ppdet/yolov3.h" +#include "fastdeploy/vision/ppdet/yolox.h" diff --git a/csrcs/fastdeploy/vision/ppdet/picodet.cc b/csrcs/fastdeploy/vision/ppdet/picodet.cc index f8070a3d24..5f912b8cf4 100644 --- a/csrcs/fastdeploy/vision/ppdet/picodet.cc +++ b/csrcs/fastdeploy/vision/ppdet/picodet.cc @@ -1,3 +1,17 @@ +// 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/ppdet/picodet.h" #include "yaml-cpp/yaml.h" diff --git a/csrcs/fastdeploy/vision/ppdet/picodet.h b/csrcs/fastdeploy/vision/ppdet/picodet.h index 9024269261..7b45b9baf1 100644 --- a/csrcs/fastdeploy/vision/ppdet/picodet.h +++ b/csrcs/fastdeploy/vision/ppdet/picodet.h @@ -1,3 +1,17 @@ +// 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. + #pragma once #include "fastdeploy/vision/ppdet/ppyoloe.h" diff --git a/csrcs/fastdeploy/vision/ppdet/ppdet_pybind.cc b/csrcs/fastdeploy/vision/ppdet/ppdet_pybind.cc index bd1fc4621f..9134b59cd1 100644 --- a/csrcs/fastdeploy/vision/ppdet/ppdet_pybind.cc +++ b/csrcs/fastdeploy/vision/ppdet/ppdet_pybind.cc @@ -27,5 +27,21 @@ void BindPPDet(pybind11::module& m) { self.Predict(&mat, &res); return res; }); + pybind11::class_(ppdet_module, + "PPYOLO") + .def(pybind11::init()); + pybind11::class_(ppdet_module, + "PicoDet") + .def(pybind11::init()); + pybind11::class_(ppdet_module, + "YOLOX") + .def(pybind11::init()); + pybind11::class_( + ppdet_module, "FasterRCNN") + .def(pybind11::init()); } } // namespace fastdeploy diff --git a/csrcs/fastdeploy/vision/ppdet/ppyolo.cc b/csrcs/fastdeploy/vision/ppdet/ppyolo.cc index 2926b54a09..307f8b4f07 100644 --- a/csrcs/fastdeploy/vision/ppdet/ppyolo.cc +++ b/csrcs/fastdeploy/vision/ppdet/ppyolo.cc @@ -1,3 +1,17 @@ +// 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/ppdet/ppyolo.h" namespace fastdeploy { @@ -48,20 +62,16 @@ bool PPYOLO::Preprocess(Mat* mat, std::vector* outputs) { outputs->resize(3); (*outputs)[0].Allocate({1, 2}, FDDataType::FP32, "im_shape"); (*outputs)[2].Allocate({1, 2}, FDDataType::FP32, "scale_factor"); - std::cout << "111111111" << std::endl; float* ptr0 = static_cast((*outputs)[0].MutableData()); ptr0[0] = mat->Height(); ptr0[1] = mat->Width(); - std::cout << "090909" << std::endl; float* ptr2 = static_cast((*outputs)[2].MutableData()); ptr2[0] = mat->Height() * 1.0 / origin_h; ptr2[1] = mat->Width() * 1.0 / origin_w; - std::cout << "88888" << std::endl; (*outputs)[1].name = "image"; mat->ShareWithTensor(&((*outputs)[1])); // reshape to [1, c, h, w] (*outputs)[1].shape.insert((*outputs)[1].shape.begin(), 1); - std::cout << "??????" << std::endl; return true; } diff --git a/csrcs/fastdeploy/vision/ppdet/ppyoloe.cc b/csrcs/fastdeploy/vision/ppdet/ppyoloe.cc index 99dae0fc6b..3aea781b4d 100644 --- a/csrcs/fastdeploy/vision/ppdet/ppyoloe.cc +++ b/csrcs/fastdeploy/vision/ppdet/ppyoloe.cc @@ -101,21 +101,38 @@ bool PPYOLOE::BuildPreprocessPipelineFromConfig() { FDASSERT(target_size.size(), "Require size of target_size be 2, but now it's " + std::to_string(target_size.size()) + "."); - FDASSERT(!keep_ratio, - "Only support keep_ratio is false while deploy " - "PaddleDetection model."); - int width = target_size[1]; - int height = target_size[0]; - processors_.push_back( - std::make_shared(width, height, -1.0, -1.0, interp, false)); + if (!keep_ratio) { + int width = target_size[1]; + int height = target_size[0]; + processors_.push_back( + std::make_shared(width, height, -1.0, -1.0, interp, false)); + } else { + int min_target_size = std::min(target_size[0], target_size[1]); + int max_target_size = std::max(target_size[0], target_size[1]); + processors_.push_back(std::make_shared( + min_target_size, interp, true, max_target_size)); + } } else if (op_name == "Permute") { - processors_.push_back(std::make_shared()); + // Do nothing, do permute as the last operation + continue; + // processors_.push_back(std::make_shared()); + } else if (op_name == "Pad") { + auto size = op["size"].as>(); + auto value = op["fill_value"].as>(); + processors_.push_back(std::make_shared("float")); + processors_.push_back( + std::make_shared(size[1], size[0], value)); + } else if (op_name == "PadStride") { + auto stride = op["stride"].as(); + processors_.push_back( + std::make_shared(stride, std::vector(3, 0))); } else { FDERROR << "Unexcepted preprocess operator: " << op_name << "." << std::endl; return false; } } + processors_.push_back(std::make_shared()); return true; } @@ -224,7 +241,6 @@ bool PPYOLOE::Predict(cv::Mat* im, DetectionResult* result) { processed_data[0].PrintInfo("Before infer"); float* tmp = static_cast(processed_data[1].Data()); - std::cout << "==== " << tmp[0] << " " << tmp[1] << std::endl; std::vector infer_result; if (!Infer(processed_data, &infer_result)) { FDERROR << "Failed to inference while using model:" << ModelName() << "." diff --git a/csrcs/fastdeploy/vision/ppdet/ppyoloe.h b/csrcs/fastdeploy/vision/ppdet/ppyoloe.h index 84fe0781ba..6c79755d14 100644 --- a/csrcs/fastdeploy/vision/ppdet/ppyoloe.h +++ b/csrcs/fastdeploy/vision/ppdet/ppyoloe.h @@ -1,3 +1,17 @@ +// 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. + #pragma once #include "fastdeploy/fastdeploy_model.h" #include "fastdeploy/vision/common/processors/transform.h" @@ -47,6 +61,11 @@ class FASTDEPLOY_DECL PPYOLOE : public FastDeployModel { bool normalized = true; bool has_nms_ = false; }; + +// Read configuration and build pipeline to process input image +bool BuildPreprocessPipelineFromConfig( + std::vector>* processors, + const std::string& config_file); } // namespace ppdet } // namespace vision } // namespace fastdeploy diff --git a/csrcs/fastdeploy/vision/ppdet/rcnn.cc b/csrcs/fastdeploy/vision/ppdet/rcnn.cc new file mode 100644 index 0000000000..25b822e246 --- /dev/null +++ b/csrcs/fastdeploy/vision/ppdet/rcnn.cc @@ -0,0 +1,89 @@ +// 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/ppdet/rcnn.h" + +namespace fastdeploy { +namespace vision { +namespace ppdet { + +FasterRCNN::FasterRCNN(const std::string& model_file, + const std::string& params_file, + const std::string& config_file, + const RuntimeOption& custom_option, + const Frontend& model_format) { + config_file_ = config_file; + valid_cpu_backends = {Backend::PDINFER}; + valid_gpu_backends = {Backend::PDINFER}; + has_nms_ = true; + runtime_option = custom_option; + runtime_option.model_format = model_format; + runtime_option.model_file = model_file; + runtime_option.params_file = params_file; + initialized = Initialize(); +} + +bool FasterRCNN::Initialize() { + if (!BuildPreprocessPipelineFromConfig()) { + FDERROR << "Failed to build preprocess pipeline from configuration file." + << std::endl; + return false; + } + if (!InitRuntime()) { + FDERROR << "Failed to initialize fastdeploy backend." << std::endl; + return false; + } + return true; +} + +bool FasterRCNN::Preprocess(Mat* mat, std::vector* outputs) { + int origin_w = mat->Width(); + int origin_h = mat->Height(); + mat->PrintInfo("Origin"); + float scale[2] = {1.0, 1.0}; + for (size_t i = 0; i < processors_.size(); ++i) { + if (!(*(processors_[i].get()))(mat)) { + FDERROR << "Failed to process image data in " << processors_[i]->Name() + << "." << std::endl; + return false; + } + if (processors_[i]->Name().find("Resize") != std::string::npos) { + scale[0] = mat->Height() * 1.0 / origin_h; + scale[1] = mat->Width() * 1.0 / origin_w; + } + mat->PrintInfo(processors_[i]->Name()); + } + + outputs->resize(3); + (*outputs)[0].Allocate({1, 2}, FDDataType::FP32, "im_shape"); + (*outputs)[2].Allocate({1, 2}, FDDataType::FP32, "scale_factor"); + float* ptr0 = static_cast((*outputs)[0].MutableData()); + ptr0[0] = mat->Height(); + ptr0[1] = mat->Width(); + float* ptr2 = static_cast((*outputs)[2].MutableData()); + ptr2[0] = scale[0]; + ptr2[1] = scale[1]; + (*outputs)[1].name = "image"; + mat->ShareWithTensor(&((*outputs)[1])); + // reshape to [1, c, h, w] + (*outputs)[1].shape.insert((*outputs)[1].shape.begin(), 1); + (*outputs)[0].PrintInfo("im_shape"); + (*outputs)[1].PrintInfo("image"); + (*outputs)[2].PrintInfo("scale_factor"); + return true; +} + +} // namespace ppdet +} // namespace vision +} // namespace fastdeploy diff --git a/csrcs/fastdeploy/vision/ppdet/rcnn.h b/csrcs/fastdeploy/vision/ppdet/rcnn.h new file mode 100644 index 0000000000..2a9255a549 --- /dev/null +++ b/csrcs/fastdeploy/vision/ppdet/rcnn.h @@ -0,0 +1,39 @@ +// 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. + +#pragma once +#include "fastdeploy/vision/ppdet/ppyoloe.h" + +namespace fastdeploy { +namespace vision { +namespace ppdet { + +class FASTDEPLOY_DECL FasterRCNN : public PPYOLOE { + public: + FasterRCNN(const std::string& model_file, const std::string& params_file, + const std::string& config_file, + const RuntimeOption& custom_option = RuntimeOption(), + const Frontend& model_format = Frontend::PADDLE); + + virtual std::string ModelName() const { return "PaddleDetection/FasterRCNN"; } + + virtual bool Preprocess(Mat* mat, std::vector* outputs); + virtual bool Initialize(); + + protected: + FasterRCNN() {} +}; +} // namespace ppdet +} // namespace vision +} // namespace fastdeploy diff --git a/csrcs/fastdeploy/vision/ppdet/yolov3.cc b/csrcs/fastdeploy/vision/ppdet/yolov3.cc index 9d68381947..608105b040 100644 --- a/csrcs/fastdeploy/vision/ppdet/yolov3.cc +++ b/csrcs/fastdeploy/vision/ppdet/yolov3.cc @@ -1,3 +1,17 @@ +// 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/ppdet/yolov3.h" namespace fastdeploy { @@ -34,20 +48,16 @@ bool YOLOv3::Preprocess(Mat* mat, std::vector* outputs) { outputs->resize(3); (*outputs)[0].Allocate({1, 2}, FDDataType::FP32, "im_shape"); (*outputs)[2].Allocate({1, 2}, FDDataType::FP32, "scale_factor"); - std::cout << "111111111" << std::endl; float* ptr0 = static_cast((*outputs)[0].MutableData()); ptr0[0] = mat->Height(); ptr0[1] = mat->Width(); - std::cout << "090909" << std::endl; float* ptr2 = static_cast((*outputs)[2].MutableData()); ptr2[0] = mat->Height() * 1.0 / origin_h; ptr2[1] = mat->Width() * 1.0 / origin_w; - std::cout << "88888" << std::endl; (*outputs)[1].name = "image"; mat->ShareWithTensor(&((*outputs)[1])); // reshape to [1, c, h, w] (*outputs)[1].shape.insert((*outputs)[1].shape.begin(), 1); - std::cout << "??????" << std::endl; return true; } diff --git a/csrcs/fastdeploy/vision/ppdet/yolov3.h b/csrcs/fastdeploy/vision/ppdet/yolov3.h index 9919c02335..27b1352c9c 100644 --- a/csrcs/fastdeploy/vision/ppdet/yolov3.h +++ b/csrcs/fastdeploy/vision/ppdet/yolov3.h @@ -1,3 +1,17 @@ +// 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. + #pragma once #include "fastdeploy/vision/ppdet/ppyoloe.h" diff --git a/csrcs/fastdeploy/vision/ppdet/yolox.cc b/csrcs/fastdeploy/vision/ppdet/yolox.cc new file mode 100644 index 0000000000..90168d2989 --- /dev/null +++ b/csrcs/fastdeploy/vision/ppdet/yolox.cc @@ -0,0 +1,74 @@ +// 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/ppdet/yolox.h" + +namespace fastdeploy { +namespace vision { +namespace ppdet { + +YOLOX::YOLOX(const std::string& model_file, const std::string& params_file, + const std::string& config_file, const RuntimeOption& custom_option, + const Frontend& model_format) { + config_file_ = config_file; + valid_cpu_backends = {Backend::PDINFER, Backend::ORT}; + valid_gpu_backends = {Backend::PDINFER, Backend::ORT}; + runtime_option = custom_option; + runtime_option.model_format = model_format; + runtime_option.model_file = model_file; + runtime_option.params_file = params_file; + background_label = -1; + keep_top_k = 1000; + nms_eta = 1; + nms_threshold = 0.65; + nms_top_k = 10000; + normalized = true; + score_threshold = 0.001; + initialized = Initialize(); +} + +bool YOLOX::Preprocess(Mat* mat, std::vector* outputs) { + int origin_w = mat->Width(); + int origin_h = mat->Height(); + float scale[2] = {1.0, 1.0}; + mat->PrintInfo("Origin"); + for (size_t i = 0; i < processors_.size(); ++i) { + if (!(*(processors_[i].get()))(mat)) { + FDERROR << "Failed to process image data in " << processors_[i]->Name() + << "." << std::endl; + return false; + } + mat->PrintInfo(processors_[i]->Name()); + if (processors_[i]->Name().find("Resize") != std::string::npos) { + scale[0] = mat->Height() * 1.0 / origin_h; + scale[1] = mat->Width() * 1.0 / origin_w; + } + } + + outputs->resize(2); + (*outputs)[0].name = InputInfoOfRuntime(0).name; + mat->ShareWithTensor(&((*outputs)[0])); + + // reshape to [1, c, h, w] + (*outputs)[0].shape.insert((*outputs)[0].shape.begin(), 1); + + (*outputs)[1].Allocate({1, 2}, FDDataType::FP32, InputInfoOfRuntime(1).name); + float* ptr = static_cast((*outputs)[1].MutableData()); + ptr[0] = scale[0]; + ptr[1] = scale[1]; + return true; +} +} // namespace ppdet +} // namespace vision +} // namespace fastdeploy diff --git a/csrcs/fastdeploy/vision/ppdet/yolox.h b/csrcs/fastdeploy/vision/ppdet/yolox.h new file mode 100644 index 0000000000..e689674a4e --- /dev/null +++ b/csrcs/fastdeploy/vision/ppdet/yolox.h @@ -0,0 +1,35 @@ +// 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. + +#pragma once +#include "fastdeploy/vision/ppdet/ppyoloe.h" + +namespace fastdeploy { +namespace vision { +namespace ppdet { + +class FASTDEPLOY_DECL YOLOX : public PPYOLOE { + public: + YOLOX(const std::string& model_file, const std::string& params_file, + const std::string& config_file, + const RuntimeOption& custom_option = RuntimeOption(), + const Frontend& model_format = Frontend::PADDLE); + + virtual bool Preprocess(Mat* mat, std::vector* outputs); + + virtual std::string ModelName() const { return "PaddleDetection/YOLOX"; } +}; +} // namespace ppdet +} // namespace vision +} // namespace fastdeploy diff --git a/csrcs/fastdeploy/vision/ppogg/yolov5lite.cc b/csrcs/fastdeploy/vision/ppogg/yolov5lite.cc index 320867f581..c58f5f2cc9 100644 --- a/csrcs/fastdeploy/vision/ppogg/yolov5lite.cc +++ b/csrcs/fastdeploy/vision/ppogg/yolov5lite.cc @@ -328,7 +328,6 @@ bool YOLOv5Lite::Predict(cv::Mat* im, DetectionResult* result, #ifdef FASTDEPLOY_DEBUG TIMERECORD_START(0) #endif - std::cout << nms_iou_threshold << nms_iou_threshold << std::endl; Mat mat(*im); std::vector input_tensors(1); diff --git a/fastdeploy/vision/ppdet/__init__.py b/fastdeploy/vision/ppdet/__init__.py index 661ef0e1fc..bc17578530 100644 --- a/fastdeploy/vision/ppdet/__init__.py +++ b/fastdeploy/vision/ppdet/__init__.py @@ -27,7 +27,7 @@ class PPYOLOE(FastDeployModel): model_format=Frontend.PADDLE): super(PPYOLOE, self).__init__(runtime_option) - assert model_format == Frontend.PADDLE, "PPYOLOE only support model format of Frontend.Paddle now." + assert model_format == Frontend.PADDLE, "PPYOLOE model only support model format of Frontend.Paddle now." self._model = C.vision.ppdet.PPYOLOE(model_file, params_file, config_file, self._runtime_option, model_format) @@ -36,3 +36,67 @@ class PPYOLOE(FastDeployModel): def predict(self, input_image): assert input_image is not None, "The input image data is None." return self._model.predict(input_image) + + +class PPYOLO(PPYOLOE): + def __init__(self, + model_file, + params_file, + config_file, + runtime_option=None, + model_format=Frontend.PADDLE): + super(PPYOLO, self).__init__(runtime_option) + + assert model_format == Frontend.PADDLE, "PPYOLO model only support model format of Frontend.Paddle now." + self._model = C.vision.ppdet.PPYOLO(model_file, params_file, + config_file, self._runtime_option, + model_format) + assert self.initialized, "PPYOLO model initialize failed." + + +class YOLOX(PPYOLOE): + def __init__(self, + model_file, + params_file, + config_file, + runtime_option=None, + model_format=Frontend.PADDLE): + super(YOLOX, self).__init__(runtime_option) + + assert model_format == Frontend.PADDLE, "YOLOX model only support model format of Frontend.Paddle now." + self._model = C.vision.ppdet.YOLOX(model_file, params_file, + config_file, self._runtime_option, + model_format) + assert self.initialized, "YOLOX model initialize failed." + + +class PicoDet(PPYOLOE): + def __init__(self, + model_file, + params_file, + config_file, + runtime_option=None, + model_format=Frontend.PADDLE): + super(PicoDet, self).__init__(runtime_option) + + assert model_format == Frontend.PADDLE, "PicoDet model only support model format of Frontend.Paddle now." + self._model = C.vision.ppdet.PicoDet(model_file, params_file, + config_file, self._runtime_option, + model_format) + assert self.initialized, "PicoDet model initialize failed." + + +class FasterRCNN(PPYOLOE): + def __init__(self, + model_file, + params_file, + config_file, + runtime_option=None, + model_format=Frontend.PADDLE): + super(FasterRCNN, self).__init__(runtime_option) + + assert model_format == Frontend.PADDLE, "FasterRCNN model only support model format of Frontend.Paddle now." + self._model = C.vision.ppdet.FasterRCNN( + model_file, params_file, config_file, self._runtime_option, + model_format) + assert self.initialized, "FasterRCNN model initialize failed."