From 109d1046ae846440777960105bb3cde4ac11fc4b Mon Sep 17 00:00:00 2001 From: Zheng-Bicheng <58363586+Zheng-Bicheng@users.noreply.github.com> Date: Tue, 4 Apr 2023 20:33:06 +0800 Subject: [PATCH] [Model] add function for setting anchor rknpu2 (#1728) * add function for setting anchor rknpu2 add more demo for rknpu2 fixed md error * Update config.h --------- Co-authored-by: DefTruth <31974251+DefTruth@users.noreply.github.com> --- .../detection/rkyolo/cpp/CMakeLists.txt | 13 +- .../vision/detection/rkyolo/cpp/README.md | 16 +- .../vision/detection/rkyolo/cpp/README_CN.md | 13 +- .../{infer_rkyolo.cc => infer_rkyolov5.cc} | 7 +- .../detection/rkyolo/cpp/infer_rkyolov7.cc | 54 ++++++ .../detection/rkyolo/cpp/infer_rkyolox.cc | 54 ++++++ .../vision/detection/rkyolo/python/README.md | 25 ++- .../detection/rkyolo/python/README_CN.md | 25 ++- .../python/{infer.py => infer_rkyolov5.py} | 0 .../detection/rkyolo/python/infer_rkyolov7.py | 53 ++++++ .../detection/rkyolo/python/infer_rkyolox.py | 53 ++++++ .../vision/detection/contrib/rknpu2/model.h | 9 +- .../detection/contrib/rknpu2/postprocessor.h | 20 +- .../detection/contrib/rknpu2/rkyolo_pybind.cc | 178 ++++++++++++------ .../detection/contrib/rkyolo/rkyolov5.py | 129 ++++++++++++- 15 files changed, 552 insertions(+), 97 deletions(-) rename examples/vision/detection/rkyolo/cpp/{infer_rkyolo.cc => infer_rkyolov5.cc} (91%) create mode 100644 examples/vision/detection/rkyolo/cpp/infer_rkyolov7.cc create mode 100644 examples/vision/detection/rkyolo/cpp/infer_rkyolox.cc rename examples/vision/detection/rkyolo/python/{infer.py => infer_rkyolov5.py} (100%) create mode 100644 examples/vision/detection/rkyolo/python/infer_rkyolov7.py create mode 100644 examples/vision/detection/rkyolo/python/infer_rkyolox.py mode change 100755 => 100644 fastdeploy/vision/detection/contrib/rknpu2/rkyolo_pybind.cc diff --git a/examples/vision/detection/rkyolo/cpp/CMakeLists.txt b/examples/vision/detection/rkyolo/cpp/CMakeLists.txt index c29281d847..54c18f9259 100644 --- a/examples/vision/detection/rkyolo/cpp/CMakeLists.txt +++ b/examples/vision/detection/rkyolo/cpp/CMakeLists.txt @@ -3,10 +3,15 @@ project(rknpu2_test) set(CMAKE_CXX_STANDARD 14) -# 指定下载解压后的fastdeploy库路径 option(FASTDEPLOY_INSTALL_DIR "Path of downloaded fastdeploy sdk.") include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake) -# 添加FastDeploy依赖头文件 include_directories(${FASTDEPLOY_INCS}) -add_executable(infer_rkyolo infer_rkyolo.cc) -target_link_libraries(infer_rkyolo ${FASTDEPLOY_LIBS}) + +add_executable(infer_rkyolov5 infer_rkyolov5.cc) +target_link_libraries(infer_rkyolov5 ${FASTDEPLOY_LIBS}) + +add_executable(infer_rkyolov7 infer_rkyolov7.cc) +target_link_libraries(infer_rkyolov7 ${FASTDEPLOY_LIBS}) + +add_executable(infer_rkyolox infer_rkyolox.cc) +target_link_libraries(infer_rkyolox ${FASTDEPLOY_LIBS}) diff --git a/examples/vision/detection/rkyolo/cpp/README.md b/examples/vision/detection/rkyolo/cpp/README.md index da54395b2d..da9695de09 100644 --- a/examples/vision/detection/rkyolo/cpp/README.md +++ b/examples/vision/detection/rkyolo/cpp/README.md @@ -15,7 +15,21 @@ wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/0000000 cd build cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-x.x.x make -j8 -./infer_rkyolo /path/to/model 000000014439.jpg + +# infer yolov5 +wget https://bj.bcebos.com/paddlehub/fastdeploy/rknpu2/yolov5-s-relu.zip +unzip yolov5-s-relu.zip +./infer_rkyolov5 yolov5-s-relu/yolov5s_relu_tk2_RK3588_i8.rknn 000000014439.jpg + +# infer yolov7 +wget https://bj.bcebos.com/paddlehub/fastdeploy/rknpu2/yolov7-tiny.zip +unzip yolov7-tiny.zip +./infer_rkyolov7 yolov7-tiny/yolov7-tiny_tk2_RK3588_i8.rknn 000000014439.jpg + +# infer yolox +wget https://bj.bcebos.com/paddlehub/fastdeploy/rknpu2/yolox-s.zip +unzip yolox-s.zip +./infer_rkyolox yolox-s/yoloxs_tk2_RK3588_i8.rknn 000000014439.jpg ``` ## common problem diff --git a/examples/vision/detection/rkyolo/cpp/README_CN.md b/examples/vision/detection/rkyolo/cpp/README_CN.md index 9b3c077b64..d76608b62e 100644 --- a/examples/vision/detection/rkyolo/cpp/README_CN.md +++ b/examples/vision/detection/rkyolo/cpp/README_CN.md @@ -15,7 +15,18 @@ wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/0000000 cd build cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-x.x.x make -j8 -./infer_rkyolo /path/to/model 000000014439.jpg + +wget https://bj.bcebos.com/paddlehub/fastdeploy/rknpu2/yolov5-s-relu.zip +unzip yolov5-s-relu.zip +./infer_rkyolov5 yolov5-s-relu/yolov5s_relu_tk2_RK3588_i8.rknn 000000014439.jpg + +wget https://bj.bcebos.com/paddlehub/fastdeploy/rknpu2/yolov7-tiny.zip +unzip yolov7-tiny.zip +./infer_rkyolov7 yolov7-tiny/yolov7-tiny_tk2_RK3588_i8.rknn 000000014439.jpg + +wget https://bj.bcebos.com/paddlehub/fastdeploy/rknpu2/yolox-s.zip +unzip yolox-s.zip +./infer_rkyolox yolox-s/yoloxs_tk2_RK3588_i8.rknn 000000014439.jpg ``` ## 常见问题 diff --git a/examples/vision/detection/rkyolo/cpp/infer_rkyolo.cc b/examples/vision/detection/rkyolo/cpp/infer_rkyolov5.cc similarity index 91% rename from examples/vision/detection/rkyolo/cpp/infer_rkyolo.cc rename to examples/vision/detection/rkyolo/cpp/infer_rkyolov5.cc index 3433c1f41b..ec8e2e677b 100644 --- a/examples/vision/detection/rkyolo/cpp/infer_rkyolo.cc +++ b/examples/vision/detection/rkyolo/cpp/infer_rkyolov5.cc @@ -19,8 +19,8 @@ void RKNPU2Infer(const std::string& model_file, const std::string& image_file) { auto format = fastdeploy::ModelFormat::RKNN; - auto model = fastdeploy::vision::detection::RKYOLOV5( - model_file, option,format); + auto model = + fastdeploy::vision::detection::RKYOLOV5(model_file, option, format); auto im = cv::imread(image_file); @@ -31,7 +31,7 @@ void RKNPU2Infer(const std::string& model_file, 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("RKYOLOV5 in RKNN"); std::cout << res.Str() << std::endl; @@ -52,4 +52,3 @@ int main(int argc, char* argv[]) { return 0; } - diff --git a/examples/vision/detection/rkyolo/cpp/infer_rkyolov7.cc b/examples/vision/detection/rkyolo/cpp/infer_rkyolov7.cc new file mode 100644 index 0000000000..0de24bca90 --- /dev/null +++ b/examples/vision/detection/rkyolo/cpp/infer_rkyolov7.cc @@ -0,0 +1,54 @@ +// 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 RKNPU2Infer(const std::string& model_file, const std::string& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseRKNPU2(); + + auto format = fastdeploy::ModelFormat::RKNN; + + auto model = + fastdeploy::vision::detection::RKYOLOV7(model_file, option, format); + + 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; + } + auto vis_im = fastdeploy::vision::VisDetection(im, res, 0.5); + tc.End(); + tc.PrintInfo("RKYOLOV5 in RKNN"); + std::cout << res.Str() << std::endl; + 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 < 3) { + 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]); + + return 0; +} diff --git a/examples/vision/detection/rkyolo/cpp/infer_rkyolox.cc b/examples/vision/detection/rkyolo/cpp/infer_rkyolox.cc new file mode 100644 index 0000000000..fff74da3a5 --- /dev/null +++ b/examples/vision/detection/rkyolo/cpp/infer_rkyolox.cc @@ -0,0 +1,54 @@ +// 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 RKNPU2Infer(const std::string& model_file, const std::string& image_file) { + auto option = fastdeploy::RuntimeOption(); + option.UseRKNPU2(); + + auto format = fastdeploy::ModelFormat::RKNN; + + auto model = + fastdeploy::vision::detection::RKYOLOX(model_file, option, format); + + 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; + } + auto vis_im = fastdeploy::vision::VisDetection(im, res, 0.5); + tc.End(); + tc.PrintInfo("RKYOLOV5 in RKNN"); + std::cout << res.Str() << std::endl; + 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 < 3) { + 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]); + + return 0; +} diff --git a/examples/vision/detection/rkyolo/python/README.md b/examples/vision/detection/rkyolo/python/README.md index c79a1262bc..b46076254b 100644 --- a/examples/vision/detection/rkyolo/python/README.md +++ b/examples/vision/detection/rkyolo/python/README.md @@ -8,18 +8,29 @@ Two steps before deployment This directory provides examples that `infer.py` fast finishes the deployment of Picodet on RKNPU. The script is as follows ```bash -# Download the example code for deployment git clone https://github.com/PaddlePaddle/FastDeploy.git cd FastDeploy/examples/vision/detection/rkyolo/python -# Download images +# download picture wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg -# copy model -cp -r ./model /path/to/FastDeploy/examples/vision/detection/rkyolo/python - -# Inference -python3 infer.py --model_file /path/to/model --image /path/to/000000014439.jpg +# infer yolov5 +wget https://bj.bcebos.com/paddlehub/fastdeploy/rknpu2/yolov5-s-relu.zip +unzip yolov5-s-relu.zip +python3 infer_rkyolov5.py --model_file yolov5-s-relu/yolov5s_relu_tk2_RK3588_i8.rknn \ + --image 000000014439.jpg + +# infer yolov7 +wget https://bj.bcebos.com/paddlehub/fastdeploy/rknpu2/yolov7-tiny.zip +unzip yolov7-tiny.zip +python3 infer_rkyolov7.py --model_file yolov7-tiny/yolov7-tiny_tk2_RK3588_i8.rknn \ + --image 000000014439.jpg + +# infer yolox +wget https://bj.bcebos.com/paddlehub/fastdeploy/rknpu2/yolox-s.zip +unzip yolox-s.zip +python3 infer_rkyolox.py --model_file yolox-s/yoloxs_tk2_RK3588_i8.rknn \ + --image 000000014439.jpg ``` ## common problem diff --git a/examples/vision/detection/rkyolo/python/README_CN.md b/examples/vision/detection/rkyolo/python/README_CN.md index 006dcf8ca9..80feb76535 100644 --- a/examples/vision/detection/rkyolo/python/README_CN.md +++ b/examples/vision/detection/rkyolo/python/README_CN.md @@ -8,18 +8,29 @@ 本目录下提供`infer.py`快速完成Picodet在RKNPU上部署的示例。执行如下脚本即可完成 ```bash -# 下载部署示例代码 git clone https://github.com/PaddlePaddle/FastDeploy.git cd FastDeploy/examples/vision/detection/rkyolo/python -# 下载图片 +# download picture wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg -# copy model -cp -r ./model /path/to/FastDeploy/examples/vision/detection/rkyolo/python - -# 推理 -python3 infer.py --model_file /path/to/model --image /path/to/000000014439.jpg +# infer yolov5 +wget https://bj.bcebos.com/paddlehub/fastdeploy/rknpu2/yolov5-s-relu.zip +unzip yolov5-s-relu.zip +python3 infer_rkyolov5.py --model_file yolov5-s-relu/yolov5s_relu_tk2_RK3588_i8.rknn \ + --image 000000014439.jpg + +# infer yolov7 +wget https://bj.bcebos.com/paddlehub/fastdeploy/rknpu2/yolov7-tiny.zip +unzip yolov7-tiny.zip +python3 infer_rkyolov7.py --model_file yolov7-tiny/yolov7-tiny_tk2_RK3588_i8.rknn \ + --image 000000014439.jpg + +# infer yolox +wget https://bj.bcebos.com/paddlehub/fastdeploy/rknpu2/yolox-s.zip +unzip yolox-s.zip +python3 infer_rkyolox.py --model_file yolox-s/yoloxs_tk2_RK3588_i8.rknn \ + --image 000000014439.jpg ``` ## 常见问题 diff --git a/examples/vision/detection/rkyolo/python/infer.py b/examples/vision/detection/rkyolo/python/infer_rkyolov5.py similarity index 100% rename from examples/vision/detection/rkyolo/python/infer.py rename to examples/vision/detection/rkyolo/python/infer_rkyolov5.py diff --git a/examples/vision/detection/rkyolo/python/infer_rkyolov7.py b/examples/vision/detection/rkyolo/python/infer_rkyolov7.py new file mode 100644 index 0000000000..27f9a1a4b5 --- /dev/null +++ b/examples/vision/detection/rkyolo/python/infer_rkyolov7.py @@ -0,0 +1,53 @@ +# 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. +import fastdeploy as fd +import cv2 +import os + + +def parse_arguments(): + import argparse + import ast + parser = argparse.ArgumentParser() + parser.add_argument( + "--model_file", required=True, help="Path of rknn model.") + parser.add_argument( + "--image", type=str, required=True, help="Path of test image file.") + return parser.parse_args() + + +if __name__ == "__main__": + args = parse_arguments() + + model_file = args.model_file + params_file = "" + + # 配置runtime,加载模型 + runtime_option = fd.RuntimeOption() + runtime_option.use_rknpu2() + + model = fd.vision.detection.RKYOLOV7( + model_file, + runtime_option=runtime_option, + model_format=fd.ModelFormat.RKNN) + + # 预测图片分割结果 + im = cv2.imread(args.image) + result = model.predict(im) + print(result) + + # 可视化结果 + vis_im = fd.vision.vis_detection(im, result, score_threshold=0.5) + cv2.imwrite("visualized_result.jpg", vis_im) + print("Visualized result save in ./visualized_result.jpg") diff --git a/examples/vision/detection/rkyolo/python/infer_rkyolox.py b/examples/vision/detection/rkyolo/python/infer_rkyolox.py new file mode 100644 index 0000000000..04e5fff451 --- /dev/null +++ b/examples/vision/detection/rkyolo/python/infer_rkyolox.py @@ -0,0 +1,53 @@ +# 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. +import fastdeploy as fd +import cv2 +import os + + +def parse_arguments(): + import argparse + import ast + parser = argparse.ArgumentParser() + parser.add_argument( + "--model_file", required=True, help="Path of rknn model.") + parser.add_argument( + "--image", type=str, required=True, help="Path of test image file.") + return parser.parse_args() + + +if __name__ == "__main__": + args = parse_arguments() + + model_file = args.model_file + params_file = "" + + # 配置runtime,加载模型 + runtime_option = fd.RuntimeOption() + runtime_option.use_rknpu2() + + model = fd.vision.detection.RKYOLOX( + model_file, + runtime_option=runtime_option, + model_format=fd.ModelFormat.RKNN) + + # 预测图片分割结果 + im = cv2.imread(args.image) + result = model.predict(im) + print(result) + + # 可视化结果 + vis_im = fd.vision.vis_detection(im, result, score_threshold=0.5) + cv2.imwrite("visualized_result.jpg", vis_im) + print("Visualized result save in ./visualized_result.jpg") diff --git a/fastdeploy/vision/detection/contrib/rknpu2/model.h b/fastdeploy/vision/detection/contrib/rknpu2/model.h index 1b8a7ce8be..7acd0206e7 100644 --- a/fastdeploy/vision/detection/contrib/rknpu2/model.h +++ b/fastdeploy/vision/detection/contrib/rknpu2/model.h @@ -36,7 +36,8 @@ class FASTDEPLOY_DECL RKYOLOV5 : public RKYOLO { std::vector anchors = {10, 13, 16, 30, 33, 23, 30, 61, 62, 45, 59, 119, 116, 90, 156, 198, 373, 326}; int anchor_per_branch_ = 3; - GetPostprocessor().SetAnchor(anchors, anchor_per_branch_); + GetPostprocessor().SetAnchor(anchors); + GetPostprocessor().SetAnchorPerBranch(anchor_per_branch_); } virtual std::string ModelName() const { return "RKYOLOV5"; } @@ -60,7 +61,8 @@ class FASTDEPLOY_DECL RKYOLOV7 : public RKYOLO { std::vector anchors = {12, 16, 19, 36, 40, 28, 36, 75, 76, 55, 72, 146, 142, 110, 192, 243, 459, 401}; int anchor_per_branch_ = 3; - GetPostprocessor().SetAnchor(anchors, anchor_per_branch_); + GetPostprocessor().SetAnchor(anchors); + GetPostprocessor().SetAnchorPerBranch(anchor_per_branch_); } virtual std::string ModelName() const { return "RKYOLOV7"; } @@ -84,7 +86,8 @@ class FASTDEPLOY_DECL RKYOLOX : public RKYOLO { std::vector anchors = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; int anchor_per_branch_ = 1; - GetPostprocessor().SetAnchor(anchors, anchor_per_branch_); + GetPostprocessor().SetAnchor(anchors); + GetPostprocessor().SetAnchorPerBranch(anchor_per_branch_); } virtual std::string ModelName() const { return "RKYOLOV7"; } diff --git a/fastdeploy/vision/detection/contrib/rknpu2/postprocessor.h b/fastdeploy/vision/detection/contrib/rknpu2/postprocessor.h index 7178b13b4b..a0f9a54ecd 100755 --- a/fastdeploy/vision/detection/contrib/rknpu2/postprocessor.h +++ b/fastdeploy/vision/detection/contrib/rknpu2/postprocessor.h @@ -40,9 +40,7 @@ class FASTDEPLOY_DECL RKYOLOPostprocessor { std::vector* results); /// Set nms_threshold, default 0.45 - void SetNMSThreshold(float nms_threshold) { - nms_threshold_ = nms_threshold; - } + void SetNMSThreshold(float nms_threshold) { nms_threshold_ = nms_threshold; } /// Set conf_threshold, default 0.25 void SetConfThreshold(float conf_threshold) { @@ -50,10 +48,10 @@ class FASTDEPLOY_DECL RKYOLOPostprocessor { } /// Get conf_threshold, default 0.25 - float GetConfThreshold() const { return conf_threshold_; } + const float GetConfThreshold() { return conf_threshold_; } /// Get nms_threshold, default 0.45 - float GetNMSThreshold() const { return nms_threshold_; } + const float GetNMSThreshold() { return nms_threshold_; } /// Set height and weight void SetHeightAndWeight(int height, int width) { @@ -69,9 +67,13 @@ class FASTDEPLOY_DECL RKYOLOPostprocessor { /// Set scale void SetScale(const std::vector& scale) { scale_ = scale; } + /// Get Anchor + const std::vector& GetAnchor() { return anchors_; } + /// Set Anchor - void SetAnchor(const std::vector& anchors, int anchor_per_branch) { - anchors_ = anchors; + void SetAnchor(const std::vector& anchors) { anchors_ = anchors; } + + void SetAnchorPerBranch(int anchor_per_branch) { anchor_per_branch_ = anchor_per_branch; } @@ -81,9 +83,7 @@ class FASTDEPLOY_DECL RKYOLOPostprocessor { prob_box_size_ = obj_class_num_ + 5; } /// Get the number of class - int GetClassNum() { - return obj_class_num_; - } + int GetClassNum() { return obj_class_num_; } private: std::vector anchors_ = {10, 13, 16, 30, 33, 23, 30, 61, 62, diff --git a/fastdeploy/vision/detection/contrib/rknpu2/rkyolo_pybind.cc b/fastdeploy/vision/detection/contrib/rknpu2/rkyolo_pybind.cc old mode 100755 new mode 100644 index 5fe70b7fde..c92a1e0fbc --- a/fastdeploy/vision/detection/contrib/rknpu2/rkyolo_pybind.cc +++ b/fastdeploy/vision/detection/contrib/rknpu2/rkyolo_pybind.cc @@ -16,82 +16,148 @@ namespace fastdeploy { void BindRKYOLO(pybind11::module& m) { - pybind11::class_( - m, "RKYOLOPreprocessor") + pybind11::class_(m, + "RKYOLOPreprocessor") .def(pybind11::init<>()) - .def("run", [](vision::detection::RKYOLOPreprocessor& self, - std::vector& im_list) { - std::vector images; - for (size_t i = 0; i < im_list.size(); ++i) { - images.push_back(vision::WrapMat(PyArrayToCvMat(im_list[i]))); - } - std::vector outputs; - if (!self.Run(&images, &outputs)) { - throw std::runtime_error("Failed to preprocess the input data in PaddleClasPreprocessor."); - } - for (size_t i = 0; i < outputs.size(); ++i) { - outputs[i].StopSharing(); - } - return outputs; - }) + .def("run", + [](vision::detection::RKYOLOPreprocessor& self, + std::vector& im_list) { + std::vector images; + for (size_t i = 0; i < im_list.size(); ++i) { + images.push_back(vision::WrapMat(PyArrayToCvMat(im_list[i]))); + } + std::vector outputs; + if (!self.Run(&images, &outputs)) { + throw std::runtime_error( + "Failed to preprocess the input data in " + "PaddleClasPreprocessor."); + } + for (size_t i = 0; i < outputs.size(); ++i) { + outputs[i].StopSharing(); + } + return outputs; + }) .def_property("size", &vision::detection::RKYOLOPreprocessor::GetSize, &vision::detection::RKYOLOPreprocessor::SetSize) - .def_property("padding_value", &vision::detection::RKYOLOPreprocessor::GetPaddingValue, + .def_property("padding_value", + &vision::detection::RKYOLOPreprocessor::GetPaddingValue, &vision::detection::RKYOLOPreprocessor::SetPaddingValue) - .def_property("is_scale_up", &vision::detection::RKYOLOPreprocessor::GetScaleUp, + .def_property("is_scale_up", + &vision::detection::RKYOLOPreprocessor::GetScaleUp, &vision::detection::RKYOLOPreprocessor::SetScaleUp); pybind11::class_( m, "RKYOLOPostprocessor") .def(pybind11::init<>()) - .def("run", [](vision::detection::RKYOLOPostprocessor& self, - std::vector& inputs) { - std::vector results; - if (!self.Run(inputs, &results)) { - throw std::runtime_error("Failed to postprocess the runtime result in RKYOLOV5Postprocessor."); - } - return results; - }) - .def("run", [](vision::detection::RKYOLOPostprocessor& self, - std::vector& input_array) { - std::vector results; - std::vector inputs; - PyArrayToTensorList(input_array, &inputs, /*share_buffer=*/true); - if (!self.Run(inputs, &results)) { - throw std::runtime_error("Failed to postprocess the runtime result in RKYOLOV5Postprocessor."); - } - return results; - }) - .def_property("conf_threshold", &vision::detection::RKYOLOPostprocessor::GetConfThreshold, + .def("run", + [](vision::detection::RKYOLOPostprocessor& self, + std::vector& inputs) { + std::vector results; + if (!self.Run(inputs, &results)) { + throw std::runtime_error( + "Failed to postprocess the runtime result in " + "RKYOLOV5Postprocessor."); + } + return results; + }) + .def("run", + [](vision::detection::RKYOLOPostprocessor& self, + std::vector& input_array) { + std::vector results; + std::vector inputs; + PyArrayToTensorList(input_array, &inputs, /*share_buffer=*/true); + if (!self.Run(inputs, &results)) { + throw std::runtime_error( + "Failed to postprocess the runtime result in " + "RKYOLOV5Postprocessor."); + } + return results; + }) + .def("set_anchor", [](vision::detection::RKYOLOPostprocessor& self, + std::vector& data) { self.SetAnchor(data); }) + .def_property("conf_threshold", + &vision::detection::RKYOLOPostprocessor::GetConfThreshold, &vision::detection::RKYOLOPostprocessor::SetConfThreshold) - .def_property("nms_threshold", &vision::detection::RKYOLOPostprocessor::GetNMSThreshold, + .def_property("nms_threshold", + &vision::detection::RKYOLOPostprocessor::GetNMSThreshold, &vision::detection::RKYOLOPostprocessor::SetNMSThreshold) - .def_property("class_num", &vision::detection::RKYOLOPostprocessor::GetClassNum, + .def_property("class_num", + &vision::detection::RKYOLOPostprocessor::GetClassNum, &vision::detection::RKYOLOPostprocessor::SetClassNum); pybind11::class_(m, "RKYOLOV5") - .def(pybind11::init()) + .def(pybind11::init()) .def("predict", + [](vision::detection::RKYOLOV5& self, pybind11::array& data) { + auto mat = PyArrayToCvMat(data); + vision::DetectionResult res; + self.Predict(mat, &res); + return res; + }) + .def("batch_predict", [](vision::detection::RKYOLOV5& self, - pybind11::array& data) { + std::vector& data) { + std::vector images; + for (size_t i = 0; i < data.size(); ++i) { + images.push_back(PyArrayToCvMat(data[i])); + } + std::vector results; + self.BatchPredict(images, &results); + return results; + }) + .def_property_readonly("preprocessor", + &vision::detection::RKYOLOV5::GetPreprocessor) + .def_property_readonly("postprocessor", + &vision::detection::RKYOLOV5::GetPostprocessor); + + pybind11::class_(m, "RKYOLOX") + .def(pybind11::init()) + .def("predict", + [](vision::detection::RKYOLOX& self, pybind11::array& data) { + auto mat = PyArrayToCvMat(data); + vision::DetectionResult res; + self.Predict(mat, &res); + return res; + }) + .def("batch_predict", + [](vision::detection::RKYOLOX& self, + std::vector& data) { + std::vector images; + for (size_t i = 0; i < data.size(); ++i) { + images.push_back(PyArrayToCvMat(data[i])); + } + std::vector results; + self.BatchPredict(images, &results); + return results; + }) + .def_property_readonly("preprocessor", + &vision::detection::RKYOLOX::GetPreprocessor) + .def_property_readonly("postprocessor", + &vision::detection::RKYOLOX::GetPostprocessor); + + pybind11::class_(m, "RKYOLOV7") + .def(pybind11::init()) + .def("predict", + [](vision::detection::RKYOLOV7& self, pybind11::array& data) { auto mat = PyArrayToCvMat(data); vision::DetectionResult res; self.Predict(mat, &res); return res; }) - .def("batch_predict", [](vision::detection::RKYOLOV5& self, - std::vector& data) { - std::vector images; - for (size_t i = 0; i < data.size(); ++i) { - images.push_back(PyArrayToCvMat(data[i])); - } - std::vector results; - self.BatchPredict(images, &results); - return results; - }) - .def_property_readonly("preprocessor", &vision::detection::RKYOLOV5::GetPreprocessor) - .def_property_readonly("postprocessor", &vision::detection::RKYOLOV5::GetPostprocessor); + .def("batch_predict", + [](vision::detection::RKYOLOV7& self, + std::vector& data) { + std::vector images; + for (size_t i = 0; i < data.size(); ++i) { + images.push_back(PyArrayToCvMat(data[i])); + } + std::vector results; + self.BatchPredict(images, &results); + return results; + }) + .def_property_readonly("preprocessor", + &vision::detection::RKYOLOV7::GetPreprocessor) + .def_property_readonly("postprocessor", + &vision::detection::RKYOLOV7::GetPostprocessor); } } // namespace fastdeploy diff --git a/python/fastdeploy/vision/detection/contrib/rkyolo/rkyolov5.py b/python/fastdeploy/vision/detection/contrib/rkyolo/rkyolov5.py index 335826a990..a61cedd80f 100644 --- a/python/fastdeploy/vision/detection/contrib/rkyolo/rkyolov5.py +++ b/python/fastdeploy/vision/detection/contrib/rkyolo/rkyolov5.py @@ -93,6 +93,9 @@ def run(self, runtime_results): """ return self._postprocessor.run(runtime_results) + def set_anchor(self, anchor): + self._postprocessor.set_anchor(anchor) + @property def conf_threshold(self): """ @@ -135,16 +138,16 @@ def class_num(self, class_num): "The value to set `nms_threshold` must be type of float." self._postprocessor.class_num = class_num + class RKYOLOV5(FastDeployModel): def __init__(self, model_file, - params_file="", runtime_option=None, - model_format=ModelFormat.ONNX): + model_format=ModelFormat.RKNN): """Load a RKYOLOV5 model exported by RKYOLOV5. - :param model_file: (str)Path of model file, e.g ./yolov5.onnx - :param params_file: (str)Path of parameters file, e.g yolox/model.pdiparams, if the model_fomat is ModelFormat.ONNX, this param will be ignored, can be set as empty string + :param model_file: (str)Path of model file, e.g ./yolov5.rknn + :param params_file: (str)Path of parameters file, e.g , if the model_fomat is ModelFormat.ONNX, this param will be ignored, can be set as empty string :param runtime_option: (fastdeploy.RuntimeOption)RuntimeOption for inference this model, if it's None, will use the default backend on CPU :param model_format: (fastdeploy.ModelForamt)Model format of the loaded model """ @@ -194,3 +197,121 @@ def postprocessor(self): :return RKYOLOV5Postprocessor """ return self._model.postprocessor + + +class RKYOLOX(FastDeployModel): + def __init__(self, + model_file, + runtime_option=None, + model_format=ModelFormat.RKNN): + """Load a RKYOLOX model exported by RKYOLOX. + + :param model_file: (str)Path of model file, e.g ./yolox.rknn + :param runtime_option: (fastdeploy.RuntimeOption)RuntimeOption for inference this model, if it's None, will use the default backend on CPU + :param model_format: (fastdeploy.ModelForamt)Model format of the loaded model + """ + # 调用基函数进行backend_option的初始化 + # 初始化后的option保存在self._runtime_option + super(RKYOLOX, self).__init__(runtime_option) + + self._model = C.vision.detection.RKYOLOX( + model_file, self._runtime_option, model_format) + # 通过self.initialized判断整个模型的初始化是否成功 + assert self.initialized, "RKYOLOV5 initialize failed." + + def predict(self, input_image, conf_threshold=0.25, nms_iou_threshold=0.5): + """Detect an input image + + :param input_image: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format + :param conf_threshold: confidence threshold for postprocessing, default is 0.25 + :param nms_iou_threshold: iou threshold for NMS, default is 0.5 + :return: DetectionResult + """ + + self.postprocessor.conf_threshold = conf_threshold + self.postprocessor.nms_threshold = nms_iou_threshold + return self._model.predict(input_image) + + def batch_predict(self, images): + """Classify a batch of input image + + :param im: (list of numpy.ndarray) The input image list, each element is a 3-D array with layout HWC, BGR format + :return list of DetectionResult + """ + + return self._model.batch_predict(images) + + @property + def preprocessor(self): + """Get RKYOLOV5Preprocessor object of the loaded model + + :return RKYOLOV5Preprocessor + """ + return self._model.preprocessor + + @property + def postprocessor(self): + """Get RKYOLOV5Postprocessor object of the loaded model + + :return RKYOLOV5Postprocessor + """ + return self._model.postprocessor + + +class RKYOLOV7(FastDeployModel): + def __init__(self, + model_file, + runtime_option=None, + model_format=ModelFormat.RKNN): + """Load a RKYOLOX model exported by RKYOLOV7. + + :param model_file: (str)Path of model file, e.g ./yolov7.rknn + :param runtime_option: (fastdeploy.RuntimeOption)RuntimeOption for inference this model, if it's None, will use the default backend on CPU + :param model_format: (fastdeploy.ModelForamt)Model format of the loaded model + """ + # 调用基函数进行backend_option的初始化 + # 初始化后的option保存在self._runtime_option + super(RKYOLOV7, self).__init__(runtime_option) + + self._model = C.vision.detection.RKYOLOV7( + model_file, self._runtime_option, model_format) + # 通过self.initialized判断整个模型的初始化是否成功 + assert self.initialized, "RKYOLOV5 initialize failed." + + def predict(self, input_image, conf_threshold=0.25, nms_iou_threshold=0.5): + """Detect an input image + + :param input_image: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format + :param conf_threshold: confidence threshold for postprocessing, default is 0.25 + :param nms_iou_threshold: iou threshold for NMS, default is 0.5 + :return: DetectionResult + """ + + self.postprocessor.conf_threshold = conf_threshold + self.postprocessor.nms_threshold = nms_iou_threshold + return self._model.predict(input_image) + + def batch_predict(self, images): + """Classify a batch of input image + + :param im: (list of numpy.ndarray) The input image list, each element is a 3-D array with layout HWC, BGR format + :return list of DetectionResult + """ + + return self._model.batch_predict(images) + + @property + def preprocessor(self): + """Get RKYOLOV5Preprocessor object of the loaded model + + :return RKYOLOV5Preprocessor + """ + return self._model.preprocessor + + @property + def postprocessor(self): + """Get RKYOLOV5Postprocessor object of the loaded model + + :return RKYOLOV5Postprocessor + """ + return self._model.postprocessor