From dd5759bd990834d2217ba634c9a96382b952e204 Mon Sep 17 00:00:00 2001 From: Zheng-Bicheng <58363586+Zheng-Bicheng@users.noreply.github.com> Date: Thu, 29 Dec 2022 21:14:39 +0800 Subject: [PATCH] [Model] Update PPSeg Preprocess (#1007) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 更新PPSeg pybind and python * 更新PPSeg pybind and python --- .../paddleclas/rknpu2/cpp/README.md | 6 ++--- .../paddleclas/rknpu2/python/README.md | 6 ++--- .../paddleseg/rknpu2/cpp/infer.cc | 3 ++- .../paddleseg/rknpu2/python/infer.py | 3 ++- .../vision/segmentation/ppseg/ppseg_pybind.cc | 9 ++++--- .../vision/segmentation/ppseg/preprocessor.cc | 26 ++++++++++++------- .../vision/segmentation/ppseg/preprocessor.h | 14 ++++++---- .../vision/segmentation/ppseg/__init__.py | 13 +++++++--- 8 files changed, 51 insertions(+), 29 deletions(-) diff --git a/examples/vision/classification/paddleclas/rknpu2/cpp/README.md b/examples/vision/classification/paddleclas/rknpu2/cpp/README.md index 1e1883486..c21d1d77b 100644 --- a/examples/vision/classification/paddleclas/rknpu2/cpp/README.md +++ b/examples/vision/classification/paddleclas/rknpu2/cpp/README.md @@ -64,8 +64,8 @@ cd ./build/install ## 运行结果展示 ClassifyResult( -label_ids: 153, -scores: 0.684570, +label_ids: 153, +scores: 0.684570, ) ## 注意事项 @@ -75,4 +75,4 @@ DisablePermute(C++)或`disable_permute(Python),在预处理阶段禁用数据 ## 其它文档 - [ResNet50_vd Python 部署](../python) - [模型预测结果说明](../../../../../../docs/api/vision_results/) -- [转换ResNet50_vd RKNN模型文档](../README.md) \ No newline at end of file +- [转换ResNet50_vd RKNN模型文档](../README.md) diff --git a/examples/vision/classification/paddleclas/rknpu2/python/README.md b/examples/vision/classification/paddleclas/rknpu2/python/README.md index b85bb81f7..f1f0994d8 100644 --- a/examples/vision/classification/paddleclas/rknpu2/python/README.md +++ b/examples/vision/classification/paddleclas/rknpu2/python/README.md @@ -19,8 +19,8 @@ python3 infer.py --model_file ./ResNet50_vd_infer/ResNet50_vd_infer_rk3588.rknn # 运行完成后返回结果如下所示 ClassifyResult( -label_ids: 153, -scores: 0.684570, +label_ids: 153, +scores: 0.684570, ) ``` @@ -32,4 +32,4 @@ DisablePermute(C++)或`disable_permute(Python),在预处理阶段禁用数据 ## 其它文档 - [ResNet50_vd C++部署](../cpp) - [模型预测结果说明](../../../../../../docs/api/vision_results/) -- [转换ResNet50_vd RKNN模型文档](../README.md) \ No newline at end of file +- [转换ResNet50_vd RKNN模型文档](../README.md) diff --git a/examples/vision/segmentation/paddleseg/rknpu2/cpp/infer.cc b/examples/vision/segmentation/paddleseg/rknpu2/cpp/infer.cc index 834b2ccb3..f80d3fc8f 100644 --- a/examples/vision/segmentation/paddleseg/rknpu2/cpp/infer.cc +++ b/examples/vision/segmentation/paddleseg/rknpu2/cpp/infer.cc @@ -62,7 +62,8 @@ void RKNPU2Infer(const std::string& model_dir, const std::string& image_file) { std::cerr << "Failed to initialize." << std::endl; return; } - model.GetPreprocessor().DisableNormalizeAndPermute(); + model.GetPreprocessor().DisablePermute(); + model.GetPreprocessor().DisableNormalize(); fastdeploy::TimeCounter tc; tc.Start(); diff --git a/examples/vision/segmentation/paddleseg/rknpu2/python/infer.py b/examples/vision/segmentation/paddleseg/rknpu2/python/infer.py index 4168d591d..193a6dfb9 100644 --- a/examples/vision/segmentation/paddleseg/rknpu2/python/infer.py +++ b/examples/vision/segmentation/paddleseg/rknpu2/python/infer.py @@ -49,7 +49,8 @@ model = fd.vision.segmentation.PaddleSegModel( runtime_option=runtime_option, model_format=fd.ModelFormat.RKNN) -model.preprocessor.disable_normalize_and_permute() +model.preprocessor.disable_normalize() +model.preprocessor.disable_permute() # 预测图片分割结果 im = cv2.imread(args.image) diff --git a/fastdeploy/vision/segmentation/ppseg/ppseg_pybind.cc b/fastdeploy/vision/segmentation/ppseg/ppseg_pybind.cc index e687d3cc4..78c7c9ccc 100644 --- a/fastdeploy/vision/segmentation/ppseg/ppseg_pybind.cc +++ b/fastdeploy/vision/segmentation/ppseg/ppseg_pybind.cc @@ -36,9 +36,12 @@ void BindPPSeg(pybind11::module& m) { } return make_pair(outputs, imgs_info);; }) - .def("disable_normalize_and_permute", - &vision::segmentation::PaddleSegPreprocessor::DisableNormalizeAndPermute) - + .def("disable_normalize", [](vision::segmentation::PaddleSegPreprocessor& self) { + self.DisableNormalize(); + }) + .def("disable_permute", [](vision::segmentation::PaddleSegPreprocessor& self) { + self.DisablePermute(); + }) .def_property("is_vertical_screen", &vision::segmentation::PaddleSegPreprocessor::GetIsVerticalScreen, &vision::segmentation::PaddleSegPreprocessor::SetIsVerticalScreen); diff --git a/fastdeploy/vision/segmentation/ppseg/preprocessor.cc b/fastdeploy/vision/segmentation/ppseg/preprocessor.cc index 027309aad..92b037895 100644 --- a/fastdeploy/vision/segmentation/ppseg/preprocessor.cc +++ b/fastdeploy/vision/segmentation/ppseg/preprocessor.cc @@ -43,7 +43,7 @@ bool PaddleSegPreprocessor::BuildPreprocessPipelineFromConfig() { FDASSERT(op.IsMap(), "Require the transform information in yaml be Map type."); if (op["type"].as() == "Normalize") { - if (!disable_normalize_and_permute_) { + if (!disable_normalize_) { std::vector mean = {0.5, 0.5, 0.5}; std::vector std = {0.5, 0.5, 0.5}; if (op["mean"]) { @@ -55,7 +55,7 @@ bool PaddleSegPreprocessor::BuildPreprocessPipelineFromConfig() { processors_.push_back(std::make_shared(mean, std)); } } else if (op["type"].as() == "Resize") { - is_contain_resize_op = true; + is_contain_resize_op_ = true; const auto& target_size = op["target_size"]; int resize_width = target_size[0].as(); int resize_height = target_size[1].as(); @@ -73,13 +73,13 @@ bool PaddleSegPreprocessor::BuildPreprocessPipelineFromConfig() { auto input_shape = cfg["Deploy"]["input_shape"]; int input_height = input_shape[2].as(); int input_width = input_shape[3].as(); - if (input_height != -1 && input_width != -1 && !is_contain_resize_op) { - is_contain_resize_op = true; + if (input_height != -1 && input_width != -1 && !is_contain_resize_op_) { + is_contain_resize_op_ = true; processors_.insert(processors_.begin(), std::make_shared(input_width, input_height)); } } - if (!disable_normalize_and_permute_) { + if (!disable_permute_) { processors_.push_back(std::make_shared()); } @@ -121,7 +121,7 @@ bool PaddleSegPreprocessor::Run(std::vector* images, std::vectorsize(); // Batch preprocess : resize all images to the largest image shape in batch - if (!is_contain_resize_op && img_num > 1) { + if (!is_contain_resize_op_ && img_num > 1) { int max_width = 0; int max_height = 0; for (size_t i = 0; i < img_num; ++i) { @@ -156,14 +156,20 @@ bool PaddleSegPreprocessor::Run(std::vector* images, std::vectordisable_normalize_ = true; + // the DisableNormalize function will be invalid if the configuration file is loaded during preprocessing + if (!BuildPreprocessPipelineFromConfig()) { + FDERROR << "Failed to build preprocess pipeline from configuration file." << std::endl; + } +} +void PaddleSegPreprocessor::DisablePermute() { + this->disable_permute_ = true; + // the DisablePermute function will be invalid if the configuration file is loaded during preprocessing if (!BuildPreprocessPipelineFromConfig()) { FDERROR << "Failed to build preprocess pipeline from configuration file." << std::endl; } } - } // namespace segmentation } // namespace vision } // namespace fastdeploy diff --git a/fastdeploy/vision/segmentation/ppseg/preprocessor.h b/fastdeploy/vision/segmentation/ppseg/preprocessor.h index faa7fb8de..6452e8e0e 100644 --- a/fastdeploy/vision/segmentation/ppseg/preprocessor.h +++ b/fastdeploy/vision/segmentation/ppseg/preprocessor.h @@ -49,8 +49,10 @@ class FASTDEPLOY_DECL PaddleSegPreprocessor { is_vertical_screen_ = value; } - // This function will disable normalize and hwc2chw in preprocessing step. - void DisableNormalizeAndPermute(); + /// This function will disable normalize in preprocessing step. + void DisableNormalize(); + /// This function will disable hwc2chw in preprocessing step. + void DisablePermute(); private: virtual bool BuildPreprocessPipelineFromConfig(); @@ -61,10 +63,12 @@ class FASTDEPLOY_DECL PaddleSegPreprocessor { */ bool is_vertical_screen_ = false; - // for recording the switch of normalize and hwc2chw - bool disable_normalize_and_permute_ = false; + // for recording the switch of hwc2chw + bool disable_permute_ = false; + // for recording the switch of normalize + bool disable_normalize_ = false; - bool is_contain_resize_op = false; + bool is_contain_resize_op_ = false; bool initialized_ = false; }; diff --git a/python/fastdeploy/vision/segmentation/ppseg/__init__.py b/python/fastdeploy/vision/segmentation/ppseg/__init__.py index 455785686..f0106a39a 100644 --- a/python/fastdeploy/vision/segmentation/ppseg/__init__.py +++ b/python/fastdeploy/vision/segmentation/ppseg/__init__.py @@ -104,10 +104,17 @@ class PaddleSegPreprocessor: """ return self._preprocessor.run(input_ims) - def disable_normalize_and_permute(self): - """To disable normalize and hwc2chw in preprocessing step. + def disable_normalize(self): """ - return self._preprocessor.disable_normalize_and_permute() + This function will disable normalize in preprocessing step. + """ + self._preprocessor.disable_normalize() + + def disable_permute(self): + """ + This function will disable hwc2chw in preprocessing step. + """ + self._preprocessor.disable_permute() @property def is_vertical_screen(self):