From f441ffe56bd291edde6780b2a177c6ccdc349197 Mon Sep 17 00:00:00 2001 From: Zheng-Bicheng <58363586+Zheng-Bicheng@users.noreply.github.com> Date: Mon, 16 Jan 2023 22:33:02 +0800 Subject: [PATCH] [Model] Add YOLOV8 For RKNPU2 (#1153) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 更新ppdet * 更新ppdet * 更新ppdet * 更新ppdet * 更新ppdet * 新增ppdet_decode * 更新多batch支持 * 更新多batch支持 * 更新多batch支持 * 更新注释内容 * 尝试解决pybind问题 * 尝试解决pybind的问题 * 尝试解决pybind的问题 * 重构代码 * 重构代码 * 重构代码 * 按照要求修改 * 更新Picodet文档 * 更新Picodet文档,更新yolov8文档 * 修改picodet 以及 yolov8 example * 更新Picodet模型转换脚本 * 更新example代码 * 更新yolov8量化代码 * 修复部分bug 加入pybind * 修复pybind * 修复pybind错误的问题 * 更新说明文档 * 更新说明文档 --- .../paddledetection/rknpu2/README_CN.md | 114 ++---------------- .../paddledetection/rknpu2/cpp/CMakeLists.txt | 41 ++----- .../paddledetection/rknpu2/cpp/README_CN.md | 59 +++------ .../rknpu2/cpp/infer_picodet.cc | 27 +++-- .../rknpu2/cpp/infer_yolov8.cc | 95 +++++++++++++++ .../paddledetection/rknpu2/picodet.md | 68 +++++++++++ .../paddledetection/rknpu2/yolov8.md | 50 ++++++++ fastdeploy/vision/detection/ppdet/model.h | 1 + .../vision/detection/ppdet/preprocessor.cc | 2 - ...picodet_s_416_coco_lcnet_unquantized.yaml} | 4 +- tools/rknpu2/config/yolov8_n_quantized.yaml | 9 ++ tools/rknpu2/config/yolov8_n_unquantized.yaml | 9 ++ 12 files changed, 290 insertions(+), 189 deletions(-) create mode 100644 examples/vision/detection/paddledetection/rknpu2/cpp/infer_yolov8.cc create mode 100644 examples/vision/detection/paddledetection/rknpu2/picodet.md create mode 100644 examples/vision/detection/paddledetection/rknpu2/yolov8.md mode change 100755 => 100644 fastdeploy/vision/detection/ppdet/model.h rename tools/rknpu2/config/{picodet_s_416_coco_lcnet.yaml => picodet_s_416_coco_lcnet_unquantized.yaml} (72%) create mode 100644 tools/rknpu2/config/yolov8_n_quantized.yaml create mode 100644 tools/rknpu2/config/yolov8_n_unquantized.yaml diff --git a/examples/vision/detection/paddledetection/rknpu2/README_CN.md b/examples/vision/detection/paddledetection/rknpu2/README_CN.md index aa390351a7..8fb3765baf 100644 --- a/examples/vision/detection/paddledetection/rknpu2/README_CN.md +++ b/examples/vision/detection/paddledetection/rknpu2/README_CN.md @@ -1,121 +1,31 @@ [English](README.md) | 简体中文 + # PaddleDetection RKNPU2部署示例 ## 支持模型列表 -目前FastDeploy支持如下模型的部署 -- [PicoDet系列模型](https://github.com/PaddlePaddle/PaddleDetection/tree/release/2.4/configs/picodet) +目前FastDeploy使用RKNPU2支持如下PaddleDetection模型的部署: + +- Picodet +- PPYOLOE +- YOLOV8 ## 准备PaddleDetection部署模型以及转换模型 + RKNPU部署模型前需要将Paddle模型转换成RKNN模型,具体步骤如下: + * Paddle动态图模型转换为ONNX模型,请参考[PaddleDetection导出模型](https://github.com/PaddlePaddle/PaddleDetection/blob/release/2.4/deploy/EXPORT_MODEL.md) - ,注意在转换时请设置**export.nms=True**. +,注意在转换时请设置**export.nms=True**. * ONNX模型转换RKNN模型的过程,请参考[转换文档](../../../../../docs/cn/faq/rknpu2/export.md)进行转换。 - ## 模型转换example -以下步骤均在Ubuntu电脑上完成,请参考配置文档完成转换模型环境配置。下面以Picodet-s为例子,教大家如何转换PaddleDetection模型到RKNN模型。 -### 导出ONNX模型 -```bash -# 下载Paddle静态图模型并解压 -wget https://paddledet.bj.bcebos.com/deploy/Inference/picodet_s_416_coco_lcnet.tar -tar xvf picodet_s_416_coco_lcnet.tar +- [Picodet RKNPU2模型转换文档](./picodet.md) +- [YOLOv8 RKNPU2模型转换文档](./yolov8.md) -# 静态图转ONNX模型,注意,这里的save_file请和压缩包名对齐 -paddle2onnx --model_dir picodet_s_416_coco_lcnet \ - --model_filename model.pdmodel \ - --params_filename model.pdiparams \ - --save_file picodet_s_416_coco_lcnet/picodet_s_416_coco_lcnet.onnx \ - --enable_dev_version True - -# 固定shape -python -m paddle2onnx.optimize --input_model picodet_s_416_coco_lcnet/picodet_s_416_coco_lcnet.onnx \ - --output_model picodet_s_416_coco_lcnet/picodet_s_416_coco_lcnet.onnx \ - --input_shape_dict "{'image':[1,3,416,416]}" -``` - -### 编写模型导出配置文件 -以转化RK3568的RKNN模型为例子,我们需要编辑tools/rknpu2/config/RK3568/picodet_s_416_coco_lcnet.yaml,来转换ONNX模型到RKNN模型。 - -**修改normalize参数** - -如果你需要在NPU上执行normalize操作,请根据你的模型配置normalize参数,例如: -```yaml -model_path: ./picodet_s_416_coco_lcnet/picodet_s_416_coco_lcnet.onnx -output_folder: ./picodet_s_416_coco_lcnet -target_platform: RK3568 -normalize: - mean: [[0.485,0.456,0.406]] - std: [[0.229,0.224,0.225]] -outputs: ['tmp_17','p2o.Concat.9'] -``` - -**修改outputs参数** -由于Paddle2ONNX版本的不同,转换模型的输出节点名称也有所不同,请使用[Netron](https://netron.app),并找到以下蓝色方框标记的NonMaxSuppression节点,红色方框的节点名称即为目标名称。 - -例如,使用Netron可视化后,得到以下图片: -![](https://user-images.githubusercontent.com/58363586/202728663-4af0b843-d012-4aeb-8a66-626b7b87ca69.png) - -找到蓝色方框标记的NonMaxSuppression节点,可以看到红色方框标记的两个节点名称为tmp_17和p2o.Concat.9,因此需要修改outputs参数,修改后如下: -```yaml -model_path: ./picodet_s_416_coco_lcnet/picodet_s_416_coco_lcnet.onnx -output_folder: ./picodet_s_416_coco_lcnet -target_platform: RK3568 -normalize: None -outputs: ['tmp_17','p2o.Concat.9'] -``` - -### 转换模型 -```bash - -# ONNX模型转RKNN模型 -# 转换模型,模型将生成在picodet_s_320_coco_lcnet_non_postprocess目录下 -python tools/rknpu2/export.py --config_path tools/rknpu2/config/picodet_s_416_coco_lcnet.yaml \ - --target_platform rk3588 -``` - -### 修改模型运行时的配置文件 - -配置文件中,我们只需要修改**Preprocess**下的**Normalize**和**Permute**. - -**删除Permute** - -RKNPU只支持NHWC的输入格式,因此需要删除Permute操作.删除后,配置文件Precess部分后如下: -```yaml -Preprocess: -- interp: 2 - keep_ratio: false - target_size: - - 416 - - 416 - type: Resize -- is_scale: true - mean: - - 0.485 - - 0.456 - - 0.406 - std: - - 0.229 - - 0.224 - - 0.225 - type: NormalizeImage -``` - -**根据模型转换文件决定是否删除Normalize** - -RKNPU支持使用NPU进行Normalize操作,如果你在导出模型时配置了Normalize参数,请删除**Normalize**.删除后配置文件Precess部分如下: -```yaml -Preprocess: -- interp: 2 - keep_ratio: false - target_size: - - 416 - - 416 - type: Resize -``` ## 其他链接 + - [Cpp部署](./cpp) - [Python部署](./python) - [视觉模型预测结果](../../../../../docs/api/vision_results/) diff --git a/examples/vision/detection/paddledetection/rknpu2/cpp/CMakeLists.txt b/examples/vision/detection/paddledetection/rknpu2/cpp/CMakeLists.txt index 1962477733..41db4b1366 100644 --- a/examples/vision/detection/paddledetection/rknpu2/cpp/CMakeLists.txt +++ b/examples/vision/detection/paddledetection/rknpu2/cpp/CMakeLists.txt @@ -1,37 +1,16 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 3.10) -project(rknpu2_test) - -set(CMAKE_CXX_STANDARD 14) +PROJECT(infer_demo C CXX) +CMAKE_MINIMUM_REQUIRED (VERSION 3.10) # 指定下载解压后的fastdeploy库路径 -set(FASTDEPLOY_INSTALL_DIR "thirdpartys/fastdeploy-0.0.3") +option(FASTDEPLOY_INSTALL_DIR "Path of downloaded fastdeploy sdk.") -include(${FASTDEPLOY_INSTALL_DIR}/FastDeployConfig.cmake) -include_directories(${FastDeploy_INCLUDE_DIRS}) +include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake) -add_executable(infer_picodet infer_picodet.cc) -target_link_libraries(infer_picodet ${FastDeploy_LIBS}) +# 添加FastDeploy依赖头文件 +include_directories(${FASTDEPLOY_INCS}) +add_executable(infer_picodet_demo ${PROJECT_SOURCE_DIR}/infer_picodet_demo.cc) +target_link_libraries(infer_picodet_demo ${FASTDEPLOY_LIBS}) - -set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/build/install) - -install(TARGETS infer_picodet DESTINATION ./) - -install(DIRECTORY model DESTINATION ./) -install(DIRECTORY images DESTINATION ./) - -file(GLOB FASTDEPLOY_LIBS ${FASTDEPLOY_INSTALL_DIR}/lib/*) -message("${FASTDEPLOY_LIBS}") -install(PROGRAMS ${FASTDEPLOY_LIBS} DESTINATION lib) - -file(GLOB ONNXRUNTIME_LIBS ${FASTDEPLOY_INSTALL_DIR}/third_libs/install/onnxruntime/lib/*) -install(PROGRAMS ${ONNXRUNTIME_LIBS} DESTINATION lib) - -install(DIRECTORY ${FASTDEPLOY_INSTALL_DIR}/third_libs/install/opencv/lib DESTINATION ./) - -file(GLOB PADDLETOONNX_LIBS ${FASTDEPLOY_INSTALL_DIR}/third_libs/install/paddle2onnx/lib/*) -install(PROGRAMS ${PADDLETOONNX_LIBS} DESTINATION lib) - -file(GLOB RKNPU2_LIBS ${FASTDEPLOY_INSTALL_DIR}/third_libs/install/rknpu2_runtime/${RKNN2_TARGET_SOC}/lib/*) -install(PROGRAMS ${RKNPU2_LIBS} DESTINATION lib) +add_executable(infer_yolov8_demo ${PROJECT_SOURCE_DIR}/infer_yolov8_demo.cc) +target_link_libraries(infer_yolov8_demo ${FASTDEPLOY_LIBS}) diff --git a/examples/vision/detection/paddledetection/rknpu2/cpp/README_CN.md b/examples/vision/detection/paddledetection/rknpu2/cpp/README_CN.md index d58a092aed..18ddfa77a5 100644 --- a/examples/vision/detection/paddledetection/rknpu2/cpp/README_CN.md +++ b/examples/vision/detection/paddledetection/rknpu2/cpp/README_CN.md @@ -1,4 +1,5 @@ [English](README.md) | 简体中文 + # PaddleDetection C++部署示例 本目录下提供`infer_picodet.cc`快速完成PPDetection模型在Rockchip板子上上通过二代NPU加速部署的示例。 @@ -10,50 +11,24 @@ 以上步骤请参考[RK2代NPU部署库编译](../../../../../../docs/cn/build_and_install/rknpu2.md)实现 -## 生成基本目录文件 - -该例程由以下几个部分组成 -```text -. -├── CMakeLists.txt -├── build # 编译文件夹 -├── image # 存放图片的文件夹 -├── infer_picodet.cc -├── model # 存放模型文件的文件夹 -└── thirdpartys # 存放sdk的文件夹 -``` - -首先需要先生成目录结构 ```bash +以picodet为例进行推理部署 + mkdir build -mkdir images -mkdir model -mkdir thirdpartys -``` - -## 编译 - -### 编译并拷贝SDK到thirdpartys文件夹 - -请参考[RK2代NPU部署库编译](../../../../../../docs/cn/build_and_install/rknpu2.md)仓库编译SDK,编译完成后,将在build目录下生成fastdeploy-0.0.3目录,请移动它至thirdpartys目录下. - -### 拷贝模型文件,以及配置文件至model文件夹 -在Paddle动态图模型 -> Paddle静态图模型 -> ONNX模型的过程中,将生成ONNX文件以及对应的yaml配置文件,请将配置文件存放到model文件夹内。 -转换为RKNN后的模型文件也需要拷贝至model。 - -### 准备测试图片至image文件夹 -```bash -wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg -cp 000000014439.jpg ./images -``` - -### 编译example - -```bash cd build -cmake .. -make -j8 -make install +# 下载预编译库,详情见文档导航处 +wget https://bj.bcebos.com/fastdeploy/release/cpp/fastdeploy-linux-x64-x.x.x.tgz +tar xvf fastdeploy-linux-x64-x.x.x.tgz +cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-x.x.x +make -j + +# 下载PPYOLOE模型文件和测试图片 +wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg + +# CPU推理 +./infer_picodet_demo ./picodet_s_416_coco_lcnet 000000014439.jpg 0 +# RKNPU2推理 +./infer_picodet_demo ./picodet_s_416_coco_lcnet 000000014439.jpg 1 ``` ## 运行例程 @@ -63,7 +38,9 @@ cd ./build/install ./infer_picodet model/picodet_s_416_coco_lcnet images/000000014439.jpg ``` +## 文档导航 - [模型介绍](../../) - [Python部署](../python) - [视觉模型预测结果](../../../../../../docs/api/vision_results/) +- [RKNPU2 预编译库](../../../../../../docs/cn/faq/rknpu2/rknpu2.md) diff --git a/examples/vision/detection/paddledetection/rknpu2/cpp/infer_picodet.cc b/examples/vision/detection/paddledetection/rknpu2/cpp/infer_picodet.cc index 6d474fe7ad..61e6aeb264 100644 --- a/examples/vision/detection/paddledetection/rknpu2/cpp/infer_picodet.cc +++ b/examples/vision/detection/paddledetection/rknpu2/cpp/infer_picodet.cc @@ -13,8 +13,8 @@ // limitations under the License. #include #include + #include "fastdeploy/vision.h" -#include void ONNXInfer(const std::string& model_dir, const std::string& image_file) { std::string model_file = model_dir + "/picodet_s_416_coco_lcnet.onnx"; @@ -25,7 +25,7 @@ void ONNXInfer(const std::string& model_dir, const std::string& image_file) { auto format = fastdeploy::ModelFormat::ONNX; auto model = fastdeploy::vision::detection::PicoDet( - model_file, params_file, config_file,option,format); + model_file, params_file, config_file, option, format); fastdeploy::TimeCounter tc; tc.Start(); @@ -35,14 +35,12 @@ void ONNXInfer(const std::string& model_dir, const std::string& image_file) { std::cerr << "Failed to predict." << std::endl; return; } - auto vis_im = fastdeploy::vision::VisDetection(im, res,0.5); + auto vis_im = fastdeploy::vision::VisDetection(im, res, 0.5); tc.End(); tc.PrintInfo("PPDet in ONNX"); cv::imwrite("infer_onnx.jpg", vis_im); - std::cout - << "Visualized result saved in ./infer_onnx.jpg" - << std::endl; + std::cout << "Visualized result saved in ./infer_onnx.jpg" << std::endl; } void RKNPU2Infer(const std::string& model_dir, const std::string& image_file) { @@ -56,8 +54,10 @@ void RKNPU2Infer(const std::string& model_dir, const std::string& image_file) { auto format = fastdeploy::ModelFormat::RKNN; auto model = fastdeploy::vision::detection::PicoDet( - model_file, params_file, config_file,option,format); + model_file, params_file, config_file, option, format); + model.GetPreprocessor().DisablePermute(); + model.GetPreprocessor().DisableNormalize(); model.GetPostprocessor().ApplyDecodeAndNMS(); auto im = cv::imread(image_file); @@ -73,21 +73,24 @@ void RKNPU2Infer(const std::string& model_dir, const std::string& image_file) { tc.PrintInfo("PPDet in RKNPU2"); std::cout << res.Str() << std::endl; - auto vis_im = fastdeploy::vision::VisDetection(im, res,0.5); + auto vis_im = fastdeploy::vision::VisDetection(im, res, 0.5); cv::imwrite("infer_rknpu2.jpg", vis_im); std::cout << "Visualized result saved in ./infer_rknpu2.jpg" << std::endl; } int main(int argc, char* argv[]) { - if (argc < 3) { + if (argc < 4) { std::cout << "Usage: infer_demo path/to/model_dir path/to/image run_option, " "e.g ./infer_model ./picodet_model_dir ./test.jpeg" << std::endl; return -1; } - RKNPU2Infer(argv[1], argv[2]); -//ONNXInfer(argv[1], argv[2]); + + if (std::atoi(argv[3]) == 0) { + ONNXInfer(argv[1], argv[2]); + } else if (std::atoi(argv[3]) == 1) { + RKNPU2Infer(argv[1], argv[2]); + } return 0; } - diff --git a/examples/vision/detection/paddledetection/rknpu2/cpp/infer_yolov8.cc b/examples/vision/detection/paddledetection/rknpu2/cpp/infer_yolov8.cc new file mode 100644 index 0000000000..cf8dcd3b6a --- /dev/null +++ b/examples/vision/detection/paddledetection/rknpu2/cpp/infer_yolov8.cc @@ -0,0 +1,95 @@ +// 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 ONNXInfer(const std::string& model_dir, const std::string& image_file) { + std::string model_file = model_dir + "/yolov8_n_500e_coco.onnx"; + std::string params_file; + std::string config_file = model_dir + "/infer_cfg.yml"; + auto option = fastdeploy::RuntimeOption(); + option.UseCpu(); + auto format = fastdeploy::ModelFormat::ONNX; + + auto model = fastdeploy::vision::detection::PaddleYOLOv8( + model_file, params_file, config_file, option, format); + + fastdeploy::TimeCounter tc; + tc.Start(); + auto im = cv::imread(image_file); + fastdeploy::vision::DetectionResult res; + if (!model.Predict(im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + auto vis_im = fastdeploy::vision::VisDetection(im, res, 0.5); + tc.End(); + tc.PrintInfo("PPDet in ONNX"); + + std::cout << res.Str() << std::endl; + cv::imwrite("infer_onnx.jpg", vis_im); + std::cout << "Visualized result saved in ./infer_onnx.jpg" << std::endl; +} + +void RKNPU2Infer(const std::string& model_dir, const std::string& image_file) { + auto model_file = model_dir + "/yolov8_n_500e_coco_rk3588_unquantized.rknn"; + auto params_file = ""; + auto config_file = model_dir + "/infer_cfg.yml"; + + auto option = fastdeploy::RuntimeOption(); + option.UseRKNPU2(); + + auto format = fastdeploy::ModelFormat::RKNN; + + auto model = fastdeploy::vision::detection::PaddleYOLOv8( + model_file, params_file, config_file, option, format); + + model.GetPreprocessor().DisablePermute(); + model.GetPreprocessor().DisableNormalize(); + model.GetPostprocessor().ApplyDecodeAndNMS(); + + auto im = cv::imread(image_file); + + fastdeploy::vision::DetectionResult res; + fastdeploy::TimeCounter tc; + tc.Start(); + if (!model.Predict(&im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + tc.End(); + tc.PrintInfo("PPDet in RKNPU2"); + + std::cout << res.Str() << std::endl; + auto vis_im = fastdeploy::vision::VisDetection(im, res, 0.5); + cv::imwrite("infer_rknpu2.jpg", vis_im); + std::cout << "Visualized result saved in ./infer_rknpu2.jpg" << std::endl; +} + +int main(int argc, char* argv[]) { + if (argc < 4) { + std::cout + << "Usage: infer_demo path/to/model_dir path/to/image run_option, " + "e.g ./infer_model ./picodet_model_dir ./test.jpeg" + << std::endl; + return -1; + } + + if (std::atoi(argv[3]) == 0) { + ONNXInfer(argv[1], argv[2]); + } else if (std::atoi(argv[3]) == 1) { + RKNPU2Infer(argv[1], argv[2]); + } + return 0; +} diff --git a/examples/vision/detection/paddledetection/rknpu2/picodet.md b/examples/vision/detection/paddledetection/rknpu2/picodet.md new file mode 100644 index 0000000000..de01926480 --- /dev/null +++ b/examples/vision/detection/paddledetection/rknpu2/picodet.md @@ -0,0 +1,68 @@ +# Picodet RKNPU2模型转换文档 + +以下步骤均在Ubuntu电脑上完成,请参考配置文档完成转换模型环境配置。下面以Picodet-s为例子,教大家如何转换PaddleDetection模型到RKNN模型。 + + +### 导出ONNX模型 + +```bash +# 下载Paddle静态图模型并解压 +wget https://paddledet.bj.bcebos.com/deploy/Inference/picodet_s_416_coco_lcnet.tar +tar xvf picodet_s_416_coco_lcnet.tar + +# 静态图转ONNX模型,注意,这里的save_file请和压缩包名对齐 +paddle2onnx --model_dir picodet_s_416_coco_lcnet \ + --model_filename model.pdmodel \ + --params_filename model.pdiparams \ + --save_file picodet_s_416_coco_lcnet/picodet_s_416_coco_lcnet.onnx \ + --enable_dev_version True + +# 固定shape +python -m paddle2onnx.optimize --input_model picodet_s_416_coco_lcnet/picodet_s_416_coco_lcnet.onnx \ + --output_model picodet_s_416_coco_lcnet/picodet_s_416_coco_lcnet.onnx \ + --input_shape_dict "{'image':[1,3,416,416]}" +``` + +### 编写模型导出配置文件 + +以转化RK3568的RKNN模型为例子,我们需要编辑tools/rknpu2/config/picodet_s_416_coco_lcnet_unquantized.yaml,来转换ONNX模型到RKNN模型。 + +**修改normalize参数** + +如果你需要在NPU上执行normalize操作,请根据你的模型配置normalize参数,例如: + +```yaml +mean: + - + - 127.5 + - 127.5 + - 127.5 +std: + - + - 127.5 + - 127.5 + - 127.5 +``` + +**修改outputs参数** +由于Paddle2ONNX版本的不同,转换模型的输出节点名称也有所不同,请使用[Netron](https://netron.app)对模型进行可视化,并找到以下蓝色方框标记的NonMaxSuppression节点,红色方框的节点名称即为目标名称。 + +例如,使用Netron可视化后,得到以下图片: + +![](https://user-images.githubusercontent.com/58363586/212599781-e1952da7-6eae-4951-8ca7-bab7e6940692.png) + +找到蓝色方框标记的NonMaxSuppression节点,可以看到红色方框标记的两个节点名称为p2o.Div.79和p2o.Concat.9,因此需要修改outputs参数,修改后如下: + +```yaml +outputs_nodes: [ 'p2o.Div.79','p2o.Concat.9' ] +``` + +### 转换模型 + +```bash + +# ONNX模型转RKNN模型 +# 转换模型,模型将生成在picodet_s_320_coco_lcnet_non_postprocess目录下 +python tools/rknpu2/export.py --config_path tools/rknpu2/config/picodet_s_416_coco_lcnet_unquantized.yaml \ + --target_platform rk3588 +``` diff --git a/examples/vision/detection/paddledetection/rknpu2/yolov8.md b/examples/vision/detection/paddledetection/rknpu2/yolov8.md new file mode 100644 index 0000000000..432fe02bb4 --- /dev/null +++ b/examples/vision/detection/paddledetection/rknpu2/yolov8.md @@ -0,0 +1,50 @@ +# YOLOv8 RKNPU2模型转换文档 + +以下步骤均在Ubuntu电脑上完成,请参考配置文档完成转换模型环境配置。下面以yolov8为例子,教大家如何转换PaddleDetection模型到RKNN模型。 + + +### 导出ONNX模型 + +```bash +# 下载Paddle静态图模型并解压 + +# 静态图转ONNX模型,注意,这里的save_file请和压缩包名对齐 +paddle2onnx --model_dir yolov8_n_500e_coco \ + --model_filename model.pdmodel \ + --params_filename model.pdiparams \ + --save_file yolov8_n_500e_coco/yolov8_n_500e_coco.onnx \ + --enable_dev_version True + +# 固定shape +python -m paddle2onnx.optimize --input_model yolov8_n_500e_coco/yolov8_n_500e_coco.onnx \ + --output_model yolov8_n_500e_coco/yolov8_n_500e_coco.onnx \ + --input_shape_dict "{'image':[1,3,640,640],'scale_factor':[1,2]}" +``` + +### 编写模型导出配置文件 +**修改outputs参数** +由于Paddle2ONNX版本的不同,转换模型的输出节点名称也有所不同,请使用[Netron](https://netron.app)对模型进行可视化,并找到以下蓝色方框标记的NonMaxSuppression节点,红色方框的节点名称即为目标名称。 + +例如,使用Netron可视化后,得到以下图片: + +![](https://user-images.githubusercontent.com/58363586/212599658-8a2c4b79-f59a-40b5-ade7-f77c6fcfdf2a.png) + +找到蓝色方框标记的NonMaxSuppression节点,可以看到红色方框标记的两个节点名称为p2o.Div.1和p2o.Concat.9,因此需要修改outputs参数,修改后如下: + +```yaml +outputs_nodes: [ 'p2o.Div.1','p2o.Concat.49' ] +``` + +### 转换模型 + +```bash + +# ONNX模型转RKNN模型 +# 转换非全量化模型,模型将生成在yolov8_n目录下 +python tools/rknpu2/export.py --config_path tools/rknpu2/config/yolov8_n_unquantized.yaml \ + --target_platform rk3588 + +# 转换全量化模型,模型将生成在yolov8_n目录下 +python tools/rknpu2/export.py --config_path tools/rknpu2/config/yolov8_n_quantized.yaml \ + --target_platform rk3588 +``` diff --git a/fastdeploy/vision/detection/ppdet/model.h b/fastdeploy/vision/detection/ppdet/model.h old mode 100755 new mode 100644 index 05d646ab6d..0e16906423 --- a/fastdeploy/vision/detection/ppdet/model.h +++ b/fastdeploy/vision/detection/ppdet/model.h @@ -258,6 +258,7 @@ class FASTDEPLOY_DECL PaddleYOLOv8 : public PPDetBase { Backend::LITE}; valid_gpu_backends = {Backend::ORT, Backend::PDINFER, Backend::TRT}; valid_kunlunxin_backends = {Backend::LITE}; + valid_rknpu_backends = {Backend::RKNPU2}; valid_ascend_backends = {Backend::LITE}; initialized = Initialize(); } diff --git a/fastdeploy/vision/detection/ppdet/preprocessor.cc b/fastdeploy/vision/detection/ppdet/preprocessor.cc index 2d409bd4fd..e3a30c011a 100644 --- a/fastdeploy/vision/detection/ppdet/preprocessor.cc +++ b/fastdeploy/vision/detection/ppdet/preprocessor.cc @@ -90,11 +90,9 @@ bool PaddleDetPreprocessor::BuildPreprocessPipelineFromConfig() { // Do nothing, do permute as the last operation has_permute = true; 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") { diff --git a/tools/rknpu2/config/picodet_s_416_coco_lcnet.yaml b/tools/rknpu2/config/picodet_s_416_coco_lcnet_unquantized.yaml similarity index 72% rename from tools/rknpu2/config/picodet_s_416_coco_lcnet.yaml rename to tools/rknpu2/config/picodet_s_416_coco_lcnet_unquantized.yaml index 2cbc7068d1..418b996074 100644 --- a/tools/rknpu2/config/picodet_s_416_coco_lcnet.yaml +++ b/tools/rknpu2/config/picodet_s_416_coco_lcnet_unquantized.yaml @@ -10,6 +10,8 @@ std: - 127.5 model_path: ./picodet_s_416_coco_lcnet/picodet_s_416_coco_lcnet.onnx outputs_nodes: + - 'p2o.Div.79' + - 'p2o.Concat.9' do_quantization: False dataset: -output_folder: "./Portrait_PP_HumanSegV2_Lite_256x144_infer" +output_folder: "./picodet_s_416_coco_lcnet" diff --git a/tools/rknpu2/config/yolov8_n_quantized.yaml b/tools/rknpu2/config/yolov8_n_quantized.yaml new file mode 100644 index 0000000000..c0811864f0 --- /dev/null +++ b/tools/rknpu2/config/yolov8_n_quantized.yaml @@ -0,0 +1,9 @@ +mean: +std: +model_path: ./yolov8_n_500e_coco/yolov8_n_500e_coco.onnx +outputs_nodes: + - 'p2o.Mul.119' + - 'p2o.Concat.49' +do_quantization: True +dataset: "./yolov8_n_500e_coco/dataset.txt" +output_folder: "./yolov8_n_500e_coco" diff --git a/tools/rknpu2/config/yolov8_n_unquantized.yaml b/tools/rknpu2/config/yolov8_n_unquantized.yaml new file mode 100644 index 0000000000..c1a777e1d7 --- /dev/null +++ b/tools/rknpu2/config/yolov8_n_unquantized.yaml @@ -0,0 +1,9 @@ +mean: +std: +model_path: ./yolov8_n_500e_coco/yolov8_n_500e_coco.onnx +outputs_nodes: + - 'p2o.Div.1' + - 'p2o.Concat.49' +do_quantization: False +dataset: "./dataset.txt" +output_folder: "./yolov8_n_500e_coco"