Skip to content

Commit 6408af2

Browse files
[Add Model]Add RKPicodet (#495)
* 11-02/14:35 * 新增输入数据format错误判断 * 优化推理过程,减少内存分配次数 * 支持多输入rknn模型 * rknn模型输出shape为三维时,输出将被强制对齐为4纬。现在将直接抹除rknn补充的shape,方便部分对输出shape进行判断的模型进行正确的后处理。 * 11-03/17:25 * 支持导出多输入RKNN模型 * 更新各种文档 * ppseg改用Fastdeploy中的模型进行转换 * 11-03/17:25 * 新增开源头 * 11-03/21:48 * 删除无用debug代码,补充注释 * 11-04/01:00 * 新增rkpicodet代码 * 11-04/13:13 * 提交编译缺少的文件 * 11-04/14:03 * 更新安装文档 * 11-04/14:21 * 更新picodet_s配置文件 * 11-04/14:21 * 更新picodet自适应输出结果 * 11-04/14:21 * 更新文档 * * 更新配置文件 * * 修正配置文件 * * 添加缺失的python文件 * * 修正文档 * * 修正代码格式问题0 * * 按照要求修改 * * 按照要求修改 * * 按照要求修改 * * 按照要求修改 * * 按照要求修改 * test
1 parent 295af8f commit 6408af2

File tree

19 files changed

+694
-3
lines changed

19 files changed

+694
-3
lines changed

docs/cn/faq/rknpu2/export.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ model_path: ./portrait_pp_humansegv2_lite_256x144_pretrained.onnx
2222
output_folder: ./
2323
target_platform: RK3588
2424
normalize:
25-
mean: [0.5,0.5,0.5]
26-
std: [0.5,0.5,0.5]
25+
mean: [[0.5,0.5,0.5]]
26+
std: [[0.5,0.5,0.5]]
2727
outputs: None
2828
```
2929
@@ -45,4 +45,4 @@ python tools/export.py --config_path=./config.yaml
4545

4646
## 模型导出要注意的事项
4747

48-
* 请不要导出带softmax和argmax的模型,这两个算子存在bug,请在外部进行运算
48+
* 请不要导出带softmax和argmax的模型,这两个算子存在bug,请在外部进行运算
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# PaddleDetection RKNPU2部署示例
2+
3+
## 支持模型列表
4+
5+
目前FastDeploy支持如下模型的部署
6+
- [PicoDet系列模型](https://github.com/PaddlePaddle/PaddleDetection/tree/release/2.4/configs/picodet)
7+
8+
## 准备PaddleDetection部署模型以及转换模型
9+
RKNPU部署模型前需要将Paddle模型转换成RKNN模型,具体步骤如下:
10+
* Paddle动态图模型转换为ONNX模型,请参考[PaddleDetection导出模型](https://github.com/PaddlePaddle/PaddleDetection/blob/release/2.4/deploy/EXPORT_MODEL.md)
11+
,注意在转换时请设置**export.nms=True**.
12+
* ONNX模型转换RKNN模型的过程,请参考[转换文档](../../../../../docs/cn/faq/rknpu2/export.md)进行转换。
13+
14+
15+
## 模型转换example
16+
下面以Picodet-npu为例子,教大家如何转换PaddleDetection模型到RKNN模型。
17+
```bash
18+
## 下载Paddle静态图模型并解压
19+
wget https://bj.bcebos.com/fastdeploy/models/rknn2/picodet_s_416_coco_npu.zip
20+
unzip -qo picodet_s_416_coco_npu.zip
21+
22+
# 静态图转ONNX模型,注意,这里的save_file请和压缩包名对齐
23+
paddle2onnx --model_dir picodet_s_416_coco_npu \
24+
--model_filename model.pdmodel \
25+
--params_filename model.pdiparams \
26+
--save_file picodet_s_416_coco_npu/picodet_s_416_coco_npu.onnx \
27+
--enable_dev_version True
28+
29+
python -m paddle2onnx.optimize --input_model picodet_s_416_coco_npu/picodet_s_416_coco_npu.onnx \
30+
--output_model picodet_s_416_coco_npu/picodet_s_416_coco_npu.onnx \
31+
--input_shape_dict "{'image':[1,3,416,416]}"
32+
# ONNX模型转RKNN模型
33+
# 转换模型,模型将生成在picodet_s_320_coco_lcnet_non_postprocess目录下
34+
python tools/rknpu2/export.py --config_path tools/rknpu2/config/RK3588/picodet_s_416_coco_npu.yaml
35+
```
36+
37+
- [Python部署](./python)
38+
- [视觉模型预测结果](../../../../../docs/api/vision_results/)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
CMAKE_MINIMUM_REQUIRED(VERSION 3.10)
2+
project(rknpu2_test)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
# 指定下载解压后的fastdeploy库路径
7+
set(FASTDEPLOY_INSTALL_DIR "thirdpartys/fastdeploy-0.0.3")
8+
9+
include(${FASTDEPLOY_INSTALL_DIR}/FastDeployConfig.cmake)
10+
include_directories(${FastDeploy_INCLUDE_DIRS})
11+
12+
add_executable(infer_picodet infer_picodet.cc)
13+
target_link_libraries(infer_picodet ${FastDeploy_LIBS})
14+
15+
16+
17+
set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/build/install)
18+
19+
install(TARGETS infer_picodet DESTINATION ./)
20+
21+
install(DIRECTORY model DESTINATION ./)
22+
install(DIRECTORY images DESTINATION ./)
23+
24+
file(GLOB FASTDEPLOY_LIBS ${FASTDEPLOY_INSTALL_DIR}/lib/*)
25+
message("${FASTDEPLOY_LIBS}")
26+
install(PROGRAMS ${FASTDEPLOY_LIBS} DESTINATION lib)
27+
28+
file(GLOB ONNXRUNTIME_LIBS ${FASTDEPLOY_INSTALL_DIR}/third_libs/install/onnxruntime/lib/*)
29+
install(PROGRAMS ${ONNXRUNTIME_LIBS} DESTINATION lib)
30+
31+
install(DIRECTORY ${FASTDEPLOY_INSTALL_DIR}/third_libs/install/opencv/lib DESTINATION ./)
32+
33+
file(GLOB PADDLETOONNX_LIBS ${FASTDEPLOY_INSTALL_DIR}/third_libs/install/paddle2onnx/lib/*)
34+
install(PROGRAMS ${PADDLETOONNX_LIBS} DESTINATION lib)
35+
36+
file(GLOB RKNPU2_LIBS ${FASTDEPLOY_INSTALL_DIR}/third_libs/install/rknpu2_runtime/RK3588/lib/*)
37+
install(PROGRAMS ${RKNPU2_LIBS} DESTINATION lib)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# PaddleDetection C++部署示例
2+
3+
本目录下提供`infer_xxxxx.cc`快速完成PPDetection模型在Rockchip板子上上通过二代NPU加速部署的示例。
4+
5+
在部署前,需确认以下两个步骤:
6+
7+
1. 软硬件环境满足要求
8+
2. 根据开发环境,下载预编译部署库或者从头编译FastDeploy仓库
9+
10+
以上步骤请参考[RK2代NPU部署库编译](../../../../../../docs/cn/build_and_install/rknpu2.md)实现
11+
12+
## 生成基本目录文件
13+
14+
该例程由以下几个部分组成
15+
```text
16+
.
17+
├── CMakeLists.txt
18+
├── build # 编译文件夹
19+
├── image # 存放图片的文件夹
20+
├── infer_cpu_npu.cc
21+
├── infer_cpu_npu.h
22+
├── main.cc
23+
├── model # 存放模型文件的文件夹
24+
└── thirdpartys # 存放sdk的文件夹
25+
```
26+
27+
首先需要先生成目录结构
28+
```bash
29+
mkdir build
30+
mkdir images
31+
mkdir model
32+
mkdir thirdpartys
33+
```
34+
35+
## 编译
36+
37+
### 编译并拷贝SDK到thirdpartys文件夹
38+
39+
请参考[RK2代NPU部署库编译](../../../../../../docs/cn/build_and_install/rknpu2.md)仓库编译SDK,编译完成后,将在build目录下生成
40+
fastdeploy-0.0.3目录,请移动它至thirdpartys目录下.
41+
42+
### 拷贝模型文件,以及配置文件至model文件夹
43+
在Paddle动态图模型 -> Paddle静态图模型 -> ONNX模型的过程中,将生成ONNX文件以及对应的yaml配置文件,请将配置文件存放到model文件夹内。
44+
转换为RKNN后的模型文件也需要拷贝至model。
45+
46+
### 准备测试图片至image文件夹
47+
```bash
48+
wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg
49+
cp 000000014439.jpg ./images
50+
```
51+
52+
### 编译example
53+
54+
```bash
55+
cd build
56+
cmake ..
57+
make -j8
58+
make install
59+
```
60+
61+
## 运行例程
62+
63+
```bash
64+
cd ./build/install
65+
./rknpu_test
66+
```
67+
68+
69+
- [模型介绍](../../)
70+
- [Python部署](../python)
71+
- [视觉模型预测结果](../../../../../../docs/api/vision_results/)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#include <iostream>
15+
#include <string>
16+
#include "fastdeploy/vision.h"
17+
18+
void InferPicodet(const std::string& device = "cpu");
19+
20+
int main() {
21+
InferPicodet("npu");
22+
return 0;
23+
}
24+
25+
fastdeploy::RuntimeOption GetOption(const std::string& device) {
26+
auto option = fastdeploy::RuntimeOption();
27+
if (device == "npu") {
28+
option.UseRKNPU2();
29+
} else {
30+
option.UseCpu();
31+
}
32+
return option;
33+
}
34+
35+
fastdeploy::ModelFormat GetFormat(const std::string& device) {
36+
auto format = fastdeploy::ModelFormat::ONNX;
37+
if (device == "npu") {
38+
format = fastdeploy::ModelFormat::RKNN;
39+
} else {
40+
format = fastdeploy::ModelFormat::ONNX;
41+
}
42+
return format;
43+
}
44+
45+
std::string GetModelPath(std::string& model_path, const std::string& device) {
46+
if (device == "npu") {
47+
model_path += "rknn";
48+
} else {
49+
model_path += "onnx";
50+
}
51+
return model_path;
52+
}
53+
54+
void InferPicodet(const std::string &device) {
55+
std::string model_file = "./model/picodet_s_416_coco_npu/picodet_s_416_coco_npu_rk3588.";
56+
std::string params_file;
57+
std::string config_file = "./model/picodet_s_416_coco_npu/infer_cfg.yml";
58+
59+
fastdeploy::RuntimeOption option = GetOption(device);
60+
fastdeploy::ModelFormat format = GetFormat(device);
61+
model_file = GetModelPath(model_file, device);
62+
auto model = fastdeploy::vision::detection::RKPicoDet(
63+
model_file, params_file, config_file,option,format);
64+
65+
if (!model.Initialized()) {
66+
std::cerr << "Failed to initialize." << std::endl;
67+
return;
68+
}
69+
auto image_file = "./images/000000014439.jpg";
70+
auto im = cv::imread(image_file);
71+
72+
fastdeploy::vision::DetectionResult res;
73+
clock_t start = clock();
74+
if (!model.Predict(&im, &res)) {
75+
std::cerr << "Failed to predict." << std::endl;
76+
return;
77+
}
78+
clock_t end = clock();
79+
auto dur = static_cast<double>(end - start);
80+
printf("picodet_npu use time:%f\n", (dur / CLOCKS_PER_SEC));
81+
82+
std::cout << res.Str() << std::endl;
83+
auto vis_im = fastdeploy::vision::VisDetection(im, res,0.5);
84+
cv::imwrite("picodet_npu_result.jpg", vis_im);
85+
std::cout << "Visualized result saved in ./picodet_npu_result.jpg" << std::endl;
86+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# PaddleDetection Python部署示例
2+
3+
在部署前,需确认以下两个步骤
4+
5+
- 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../../docs/cn/build_and_install/rknpu2.md)
6+
7+
本目录下提供`infer.py`快速完成Picodet在RKNPU上部署的示例。执行如下脚本即可完成
8+
9+
```bash
10+
# 下载部署示例代码
11+
git clone https://github.com/PaddlePaddle/FastDeploy.git
12+
cd FastDeploy/examples/vision/detection/paddledetection/rknpu2/python
13+
14+
# 下载图片
15+
wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg
16+
17+
# copy model
18+
cp -r ./picodet_s_416_coco_npu /path/to/FastDeploy/examples/vision/detection/rknpu2detection/paddledetection/python
19+
20+
# 推理
21+
python3 infer.py --model_file ./picodet_s_416_coco_npu/picodet_s_416_coco_npu_3588.rknn \
22+
--config_file ./picodet_s_416_coco_npu/infer_cfg.yml \
23+
--image 000000014439.jpg
24+
```
25+
26+
27+
## 注意事项
28+
RKNPU上对模型的输入要求是使用NHWC格式,且图片归一化操作会在转RKNN模型时,内嵌到模型中,因此我们在使用FastDeploy部署时,
29+
需要先调用DisableNormalizePermute(C++)或`disable_normalize_permute(Python),在预处理阶段禁用归一化以及数据格式的转换。
30+
## 其它文档
31+
32+
- [PaddleDetection 模型介绍](..)
33+
- [PaddleDetection C++部署](../cpp)
34+
- [模型预测结果说明](../../../../../../docs/api/vision_results/)
35+
- [转换PaddleDetection RKNN模型文档](../README.md)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
import fastdeploy as fd
15+
import cv2
16+
import os
17+
18+
19+
def parse_arguments():
20+
import argparse
21+
import ast
22+
parser = argparse.ArgumentParser()
23+
parser.add_argument(
24+
"--model_file", required=True, help="Path of rknn model.")
25+
parser.add_argument("--config_file", required=True, help="Path of config.")
26+
parser.add_argument(
27+
"--image", type=str, required=True, help="Path of test image file.")
28+
return parser.parse_args()
29+
30+
31+
def build_option(args):
32+
option = fd.RuntimeOption()
33+
option.use_rknpu2()
34+
return option
35+
36+
37+
args = parse_arguments()
38+
39+
# 配置runtime,加载模型
40+
runtime_option = build_option(args)
41+
model_file = args.model_file
42+
params_file = ""
43+
config_file = args.config_file
44+
model = fd.vision.detection.RKPicoDet(
45+
model_file,
46+
params_file,
47+
config_file,
48+
runtime_option=runtime_option,
49+
model_format=fd.ModelFormat.RKNN)
50+
51+
# 预测图片分割结果
52+
im = cv2.imread(args.image)
53+
result = model.predict(im.copy())
54+
print(result)
55+
56+
# 可视化结果
57+
vis_im = fd.vision.vis_detection(im, result, score_threshold=0.5)
58+
cv2.imwrite("visualized_result.jpg", vis_im)
59+
print("Visualized result save in ./visualized_result.jpg")

fastdeploy/vision.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "fastdeploy/vision/detection/contrib/yolov7end2end_trt.h"
3030
#include "fastdeploy/vision/detection/contrib/yolox.h"
3131
#include "fastdeploy/vision/detection/ppdet/model.h"
32+
#include "fastdeploy/vision/detection/contrib/rknpu2/model.h"
3233
#include "fastdeploy/vision/facedet/contrib/retinaface.h"
3334
#include "fastdeploy/vision/facedet/contrib/scrfd.h"
3435
#include "fastdeploy/vision/facedet/contrib/ultraface.h"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
#include "fastdeploy/vision/detection/contrib/rknpu2/rkpicodet.h"

0 commit comments

Comments
 (0)