From ef7f7aaef982b16b42b71077063cf86511a65c93 Mon Sep 17 00:00:00 2001 From: pengwei08 Date: Sat, 24 Dec 2022 18:53:31 +0800 Subject: [PATCH 01/11] [RKNPU2]support rknpu2 ClasModel #957 --- fastdeploy/vision/classification/ppcls/model.cc | 5 +++++ python/fastdeploy/vision/classification/ppcls/__init__.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/fastdeploy/vision/classification/ppcls/model.cc b/fastdeploy/vision/classification/ppcls/model.cc index e1b8d52491f..d8221f4b94a 100755 --- a/fastdeploy/vision/classification/ppcls/model.cc +++ b/fastdeploy/vision/classification/ppcls/model.cc @@ -34,6 +34,11 @@ PaddleClasModel::PaddleClasModel(const std::string& model_file, } else if (model_format == ModelFormat::ONNX) { valid_cpu_backends = {Backend::ORT, Backend::OPENVINO}; valid_gpu_backends = {Backend::ORT, Backend::TRT}; + } else if (model_format == ModelFormat::RKNN) { + valid_cpu_backends = {Backend::OPENVINO, Backend::ORT, + Backend::PDINFER, Backend::LITE}; + valid_gpu_backends = {Backend::ORT, Backend::PDINFER, Backend::TRT}; + valid_rknpu_backends = {Backend::RKNPU2}; } runtime_option = custom_option; diff --git a/python/fastdeploy/vision/classification/ppcls/__init__.py b/python/fastdeploy/vision/classification/ppcls/__init__.py index 91fa66c4aef..9dc148f6ccf 100644 --- a/python/fastdeploy/vision/classification/ppcls/__init__.py +++ b/python/fastdeploy/vision/classification/ppcls/__init__.py @@ -79,7 +79,7 @@ def __init__(self, super(PaddleClasModel, self).__init__(runtime_option) - assert model_format == ModelFormat.PADDLE, "PaddleClasModel only support model format of ModelFormat.PADDLE now." + assert model_format in [ModelFormat.PADDLE, ModelFormat.RKNN], "PaddleClasModel only support model format of ModelFormat.PADDLE、RKNN now." self._model = C.vision.classification.PaddleClasModel( model_file, params_file, config_file, self._runtime_option, model_format) From b90a7e0fc903754bee7de5aed994e3a697f9ea64 Mon Sep 17 00:00:00 2001 From: pengwei08 Date: Mon, 26 Dec 2022 19:13:20 +0800 Subject: [PATCH 02/11] [RKNPU2]support rknpu2 ClasModel #957 --- .../classification/ppcls/ppcls_pybind.cc | 4 ++ .../classification/ppcls/preprocessor.cc | 37 ++++++++++++++----- .../classification/ppcls/preprocessor.h | 11 ++++++ 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/fastdeploy/vision/classification/ppcls/ppcls_pybind.cc b/fastdeploy/vision/classification/ppcls/ppcls_pybind.cc index 1873e73e5f6..e1873ecc8c0 100644 --- a/fastdeploy/vision/classification/ppcls/ppcls_pybind.cc +++ b/fastdeploy/vision/classification/ppcls/ppcls_pybind.cc @@ -36,6 +36,10 @@ void BindPaddleClas(pybind11::module& m) { }) .def("use_gpu", [](vision::classification::PaddleClasPreprocessor& self, int gpu_id = -1) { self.UseGpu(gpu_id); + }).def("DisableNormalize", [](vision::classification::PaddleClasPreprocessor& self) { + self.DisableNormalize(); + }).def("DisablePermute", [](vision::classification::PaddleClasPreprocessor& self) { + self.DisablePermute(); }); pybind11::class_( diff --git a/fastdeploy/vision/classification/ppcls/preprocessor.cc b/fastdeploy/vision/classification/ppcls/preprocessor.cc index bdc21ad1eee..0e42d74f4f3 100644 --- a/fastdeploy/vision/classification/ppcls/preprocessor.cc +++ b/fastdeploy/vision/classification/ppcls/preprocessor.cc @@ -23,7 +23,7 @@ namespace fastdeploy { namespace vision { namespace classification { -PaddleClasPreprocessor::PaddleClasPreprocessor(const std::string& config_file) { +PaddleClasPreprocessor::PaddleClasPreprocessor(const std::string& config_file) : config_file(config_file) { FDASSERT(BuildPreprocessPipelineFromConfig(config_file), "Failed to create PaddleClasPreprocessor."); initialized_ = true; @@ -57,15 +57,19 @@ bool PaddleClasPreprocessor::BuildPreprocessPipelineFromConfig( int height = op.begin()->second["size"].as(); processors_.push_back(std::make_shared(width, height)); } else if (op_name == "NormalizeImage") { - auto mean = op.begin()->second["mean"].as>(); - auto std = op.begin()->second["std"].as>(); - auto scale = op.begin()->second["scale"].as(); - FDASSERT((scale - 0.00392157) < 1e-06 && (scale - 0.00392157) > -1e-06, - "Only support scale in Normalize be 0.00392157, means the pixel " - "is in range of [0, 255]."); - processors_.push_back(std::make_shared(mean, std)); + if (!disable_normalize) { + auto mean = op.begin()->second["mean"].as>(); + auto std = op.begin()->second["std"].as>(); + auto scale = op.begin()->second["scale"].as(); + FDASSERT((scale - 0.00392157) < 1e-06 && (scale - 0.00392157) > -1e-06, + "Only support scale in Normalize be 0.00392157, means the pixel " + "is in range of [0, 255]."); + processors_.push_back(std::make_shared(mean, std)); + } } else if (op_name == "ToCHWImage") { - processors_.push_back(std::make_shared()); + if (!disable_permute) { + processors_.push_back(std::make_shared()); + } } else { FDERROR << "Unexcepted preprocess operator: " << op_name << "." << std::endl; @@ -78,6 +82,21 @@ bool PaddleClasPreprocessor::BuildPreprocessPipelineFromConfig( return true; } +void PaddleClasPreprocessor::DisableNormalize() { + this->disable_normalize = true; + // the DisableNormalize function will be invalid if the configuration file is loaded during preprocessing + if (!BuildPreprocessPipelineFromConfig(config_file)) { + FDERROR << "Failed to build preprocess pipeline from configuration file." << std::endl; + } +} +void PaddleClasPreprocessor::DisablePermute() { + this->disable_permute = true; + // the DisablePermute function will be invalid if the configuration file is loaded during preprocessing + if (!BuildPreprocessPipelineFromConfig(config_file)) { + FDERROR << "Failed to build preprocess pipeline from configuration file." << std::endl; + } +} + void PaddleClasPreprocessor::UseGpu(int gpu_id) { #ifdef WITH_GPU use_cuda_ = true; diff --git a/fastdeploy/vision/classification/ppcls/preprocessor.h b/fastdeploy/vision/classification/ppcls/preprocessor.h index 54c5e669d15..4d865d6e844 100644 --- a/fastdeploy/vision/classification/ppcls/preprocessor.h +++ b/fastdeploy/vision/classification/ppcls/preprocessor.h @@ -46,6 +46,11 @@ class FASTDEPLOY_DECL PaddleClasPreprocessor { bool WithGpu() { return use_cuda_; } + // This function will disable normalize in preprocessing step. + void DisableNormalize(); + // This function will disable hwc2chw in preprocessing step. + void DisablePermute(); + private: bool BuildPreprocessPipelineFromConfig(const std::string& config_file); std::vector> processors_; @@ -53,6 +58,12 @@ class FASTDEPLOY_DECL PaddleClasPreprocessor { bool use_cuda_ = false; // GPU device id int device_id_ = -1; + // for recording the switch of hwc2chw + bool disable_permute = false; + // for recording the switch of normalize + bool disable_normalize = false; + // read config file + std::string config_file; }; } // namespace classification From d75e9bbd57651f6f0e72acba2d0bc9ce5cb0d2d5 Mon Sep 17 00:00:00 2001 From: pengwei08 Date: Tue, 27 Dec 2022 14:04:27 +0800 Subject: [PATCH 03/11] [RKNPU2]support rknpu2 add Resnet50_vd example #957 --- .../paddleclas/rknpu2/python/README.md | 50 +++++++++++++++++++ .../paddleclas/rknpu2/python/infer.py | 50 +++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 examples/vision/classification/paddleclas/rknpu2/python/README.md create mode 100644 examples/vision/classification/paddleclas/rknpu2/python/infer.py diff --git a/examples/vision/classification/paddleclas/rknpu2/python/README.md b/examples/vision/classification/paddleclas/rknpu2/python/README.md new file mode 100644 index 00000000000..b1a14292c06 --- /dev/null +++ b/examples/vision/classification/paddleclas/rknpu2/python/README.md @@ -0,0 +1,50 @@ +# ResNet50_vd模型部署 + +## 转换模型 +下面以 ResNet50_vd为例子,教大家如何转换分类模型到RKNN模型。 + +```bash +# 安装 paddle2onnx +pip install paddle2onnx + +# 下载ResNet50_vd模型文件和测试图片 +wget https://bj.bcebos.com/paddlehub/fastdeploy/ResNet50_vd_infer.tgz +tar -xvf ResNet50_vd_infer.tgz +wget https://gitee.com/paddlepaddle/PaddleClas/raw/release/2.4/deploy/images/ImageNet/ILSVRC2012_val_00000010.jpeg + +# 静态图转ONNX模型,注意,这里的save_file请和压缩包名对齐 +paddle2onnx --model_dir ResNet50_vd_infer --model_filename inference.pdmodel --params_filename inference.pdiparams --save_file ResNet50_vd_infer/ResNet50_vd_infer.onnx --enable_dev_version True --opset_version 12 --enable_onnx_checker True + +# 固定shape,注意这里的inputs得对应netron.app展示的 inputs 的 name,有可能是image 或者 x +python -m paddle2onnx.optimize --input_model ResNet50_vd_infer/ResNet50_vd_infer.onnx --output_model ResNet50_vd_infer/ResNet50_vd_infer.onnx --input_shape_dict "{'inputs':[1,3,224,224]}" + +# 配置文件 ResNet50_vd_infer_rknn.yaml 如下: +model_path: ./ResNet50_vd_infer.onnx +output_folder: ./ +target_platform: RK3588 +normalize: + mean: [[0, 0, 0]] + std: [[1, 1, 1]] +outputs: [] +outputs_nodes: [] +do_quantization: False +dataset: + + +# ONNX模型转RKNN模型 +python tools/rknpu2/export.py \ + --config_path tools/rknpu2/config/ResNet50_vd_infer_rknn.yaml \ + --target_platform rk3588 +``` + +## 执行代码 + +```bash +python3 infer.py --model_file ./ResNet50_vd_infer/ResNet50_vd_infer_rk3588.rknn --config_file ResNet50_vd_infer/inference_cls.yaml --image ILSVRC2012_val_00000010.jpeg + +# 运行完成后返回结果如下所示 +ClassifyResult( +label_ids: 153, +scores: 0.684570, +) +``` \ No newline at end of file diff --git a/examples/vision/classification/paddleclas/rknpu2/python/infer.py b/examples/vision/classification/paddleclas/rknpu2/python/infer.py new file mode 100644 index 00000000000..8431a696c6a --- /dev/null +++ b/examples/vision/classification/paddleclas/rknpu2/python/infer.py @@ -0,0 +1,50 @@ +# 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("--config_file", required=True, help="Path of config.") + 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 = "" + config_file = args.config_file + # 配置runtime,加载模型 + runtime_option = fd.RuntimeOption() + runtime_option.use_rknpu2() + model = fd.vision.classification.ResNet50vd( + model_file, + params_file, + config_file, + runtime_option=runtime_option, + model_format=fd.ModelFormat.RKNN) + # 禁用通道转换 + model.preprocessor.DisablePermute() + im = cv2.imread(args.image) + result = model.predict(im, topk=1) + print(result) From 11ad0736c75595cdd64ddd87a3f6bfac93246e71 Mon Sep 17 00:00:00 2001 From: pengwei08 Date: Wed, 28 Dec 2022 13:36:28 +0800 Subject: [PATCH 04/11] [RKNPU2]support rknpu2 add Resnet50_vd example #957 --- .../paddleclas/rknpu2/cpp/CMakeLists.txt | 37 ++++++++++ .../paddleclas/rknpu2/cpp/README.md | 73 +++++++++++++++++++ .../paddleclas/rknpu2/cpp/infer.cc | 57 +++++++++++++++ .../paddleclas/rknpu2/python/infer.py | 2 +- .../vision/classification/ppcls/model.cc | 7 +- .../classification/ppcls/ppcls_pybind.cc | 6 +- .../classification/ppcls/preprocessor.cc | 16 ++-- .../classification/ppcls/preprocessor.h | 4 +- .../vision/classification/ppcls/__init__.py | 14 +++- 9 files changed, 197 insertions(+), 19 deletions(-) create mode 100644 examples/vision/classification/paddleclas/rknpu2/cpp/CMakeLists.txt create mode 100644 examples/vision/classification/paddleclas/rknpu2/cpp/README.md create mode 100755 examples/vision/classification/paddleclas/rknpu2/cpp/infer.cc diff --git a/examples/vision/classification/paddleclas/rknpu2/cpp/CMakeLists.txt b/examples/vision/classification/paddleclas/rknpu2/cpp/CMakeLists.txt new file mode 100644 index 00000000000..bb9b06ada58 --- /dev/null +++ b/examples/vision/classification/paddleclas/rknpu2/cpp/CMakeLists.txt @@ -0,0 +1,37 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.10) +project(rknpu_test) + +set(CMAKE_CXX_STANDARD 14) + +# 指定下载解压后的fastdeploy库路径 +set(FASTDEPLOY_INSTALL_DIR "thirdpartys/fastdeploy-0.0.3") + +include(${FASTDEPLOY_INSTALL_DIR}/FastDeployConfig.cmake) +include_directories(${FastDeploy_INCLUDE_DIRS}) +add_executable(rknpu_test infer.cc) +target_link_libraries(rknpu_test + ${FastDeploy_LIBS} + ) + + +set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/build/install) + +install(TARGETS rknpu_test 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/RK3588/lib/*) +install(PROGRAMS ${RKNPU2_LIBS} DESTINATION lib) \ No newline at end of file diff --git a/examples/vision/classification/paddleclas/rknpu2/cpp/README.md b/examples/vision/classification/paddleclas/rknpu2/cpp/README.md new file mode 100644 index 00000000000..8abdd6610c9 --- /dev/null +++ b/examples/vision/classification/paddleclas/rknpu2/cpp/README.md @@ -0,0 +1,73 @@ +# ResNet50_vd C++部署示例 + +本目录下用于展示 ResNet50_vd 模型在RKNPU2上的部署,以下的部署过程以 ResNet50_vd 为例子。 + +在部署前,需确认以下两个步骤: + +1. 软硬件环境满足要求 +2. 根据开发环境,下载预编译部署库或者从头编译FastDeploy仓库 + +以上步骤请参考[RK2代NPU部署库编译](../../../../../../docs/cn/build_and_install/rknpu2.md)实现 + +## 生成基本目录文件 + +该例程由以下几个部分组成 +```text +. +├── CMakeLists.txt +├── build # 编译文件夹 +├── image # 存放图片的文件夹 +├── infer.cc +├── model # 存放模型文件的文件夹 +└── thirdpartys # 存放sdk的文件夹 +``` + +首先需要先生成目录结构 +```bash +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,转换方案: ([ResNet50_vd RKNN模型](../python/README.md))。 + +### 准备测试图片至image文件夹 +```bash +wget https://gitee.com/paddlepaddle/PaddleClas/raw/release/2.4/deploy/images/ImageNet/ILSVRC2012_val_00000010.jpeg +``` + +### 编译example + +```bash +cd build +cmake .. +make -j8 +make install +``` + +## 运行例程 + +```bash +cd ./build/install +./rknpu_test ./model ./images/ILSVRC2012_val_00000010.jpeg +``` + +## 运行结果展示 +ClassifyResult( +label_ids: 153, +scores: 0.684570, +) + +## 注意事项 +RKNPU上对模型的输入要求是使用NHWC格式,且图片归一化操作会在转RKNN模型时,内嵌到模型中,因此我们在使用FastDeploy部署时, +DisablePermute(C++)或`disable_permute(Python),在预处理阶段禁用数据格式的转换。 diff --git a/examples/vision/classification/paddleclas/rknpu2/cpp/infer.cc b/examples/vision/classification/paddleclas/rknpu2/cpp/infer.cc new file mode 100755 index 00000000000..3d02ef9135d --- /dev/null +++ b/examples/vision/classification/paddleclas/rknpu2/cpp/infer.cc @@ -0,0 +1,57 @@ +// 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_dir, const std::string& image_file) { + auto model_file = model_dir + "/ResNet50_vd_infer_rk3588.rknn"; + auto params_file = ""; + auto config_file = model_dir + "/inference_cls.yaml"; + + auto option = fastdeploy::RuntimeOption(); + option.UseRKNPU2(); + + auto format = fastdeploy::ModelFormat::RKNN; + + auto model = fastdeploy::vision::classification::PaddleClasModel( + model_file, params_file, config_file,option,format); + if (!model.Initialized()) { + std::cerr << "Failed to initialize." << std::endl; + return; + } + model.GetPreprocessor().DisablePermute(); + + auto im = cv::imread(image_file); + + fastdeploy::vision::ClassifyResult res; + if (!model.Predict(im, &res)) { + std::cerr << "Failed to predict." << std::endl; + return; + } + + // print res + std::cout << res.Str() << 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/classification/paddleclas/rknpu2/python/infer.py b/examples/vision/classification/paddleclas/rknpu2/python/infer.py index 8431a696c6a..92dd92c2bda 100644 --- a/examples/vision/classification/paddleclas/rknpu2/python/infer.py +++ b/examples/vision/classification/paddleclas/rknpu2/python/infer.py @@ -44,7 +44,7 @@ def parse_arguments(): runtime_option=runtime_option, model_format=fd.ModelFormat.RKNN) # 禁用通道转换 - model.preprocessor.DisablePermute() + model.preprocessor.disable_permute() im = cv2.imread(args.image) result = model.predict(im, topk=1) print(result) diff --git a/fastdeploy/vision/classification/ppcls/model.cc b/fastdeploy/vision/classification/ppcls/model.cc index d8221f4b94a..8cc1555032b 100755 --- a/fastdeploy/vision/classification/ppcls/model.cc +++ b/fastdeploy/vision/classification/ppcls/model.cc @@ -34,10 +34,9 @@ PaddleClasModel::PaddleClasModel(const std::string& model_file, } else if (model_format == ModelFormat::ONNX) { valid_cpu_backends = {Backend::ORT, Backend::OPENVINO}; valid_gpu_backends = {Backend::ORT, Backend::TRT}; - } else if (model_format == ModelFormat::RKNN) { - valid_cpu_backends = {Backend::OPENVINO, Backend::ORT, - Backend::PDINFER, Backend::LITE}; - valid_gpu_backends = {Backend::ORT, Backend::PDINFER, Backend::TRT}; + } else { + valid_cpu_backends = {Backend::ORT, Backend::OPENVINO}; + valid_gpu_backends = {Backend::ORT, Backend::TRT}; valid_rknpu_backends = {Backend::RKNPU2}; } diff --git a/fastdeploy/vision/classification/ppcls/ppcls_pybind.cc b/fastdeploy/vision/classification/ppcls/ppcls_pybind.cc index e1873ecc8c0..b776d5c4524 100644 --- a/fastdeploy/vision/classification/ppcls/ppcls_pybind.cc +++ b/fastdeploy/vision/classification/ppcls/ppcls_pybind.cc @@ -36,9 +36,11 @@ void BindPaddleClas(pybind11::module& m) { }) .def("use_gpu", [](vision::classification::PaddleClasPreprocessor& self, int gpu_id = -1) { self.UseGpu(gpu_id); - }).def("DisableNormalize", [](vision::classification::PaddleClasPreprocessor& self) { + }) + .def("disable_normalize", [](vision::classification::PaddleClasPreprocessor& self) { self.DisableNormalize(); - }).def("DisablePermute", [](vision::classification::PaddleClasPreprocessor& self) { + }) + .def("disable_permute", [](vision::classification::PaddleClasPreprocessor& self) { self.DisablePermute(); }); diff --git a/fastdeploy/vision/classification/ppcls/preprocessor.cc b/fastdeploy/vision/classification/ppcls/preprocessor.cc index 0e42d74f4f3..ef8d8f20eff 100644 --- a/fastdeploy/vision/classification/ppcls/preprocessor.cc +++ b/fastdeploy/vision/classification/ppcls/preprocessor.cc @@ -23,20 +23,20 @@ namespace fastdeploy { namespace vision { namespace classification { -PaddleClasPreprocessor::PaddleClasPreprocessor(const std::string& config_file) : config_file(config_file) { - FDASSERT(BuildPreprocessPipelineFromConfig(config_file), +PaddleClasPreprocessor::PaddleClasPreprocessor(const std::string& config_file) { + this->config_file_ = config_file; + FDASSERT(BuildPreprocessPipelineFromConfig(), "Failed to create PaddleClasPreprocessor."); initialized_ = true; } -bool PaddleClasPreprocessor::BuildPreprocessPipelineFromConfig( - const std::string& config_file) { +bool PaddleClasPreprocessor::BuildPreprocessPipelineFromConfig() { processors_.clear(); YAML::Node cfg; try { - cfg = YAML::LoadFile(config_file); + cfg = YAML::LoadFile(config_file_); } catch (YAML::BadFile& e) { - FDERROR << "Failed to load yaml file " << config_file + FDERROR << "Failed to load yaml file " << config_file_ << ", maybe you should check this file." << std::endl; return false; } @@ -85,14 +85,14 @@ bool PaddleClasPreprocessor::BuildPreprocessPipelineFromConfig( void PaddleClasPreprocessor::DisableNormalize() { this->disable_normalize = true; // the DisableNormalize function will be invalid if the configuration file is loaded during preprocessing - if (!BuildPreprocessPipelineFromConfig(config_file)) { + if (!BuildPreprocessPipelineFromConfig()) { FDERROR << "Failed to build preprocess pipeline from configuration file." << std::endl; } } void PaddleClasPreprocessor::DisablePermute() { this->disable_permute = true; // the DisablePermute function will be invalid if the configuration file is loaded during preprocessing - if (!BuildPreprocessPipelineFromConfig(config_file)) { + if (!BuildPreprocessPipelineFromConfig()) { FDERROR << "Failed to build preprocess pipeline from configuration file." << std::endl; } } diff --git a/fastdeploy/vision/classification/ppcls/preprocessor.h b/fastdeploy/vision/classification/ppcls/preprocessor.h index 4d865d6e844..f61f0cee84e 100644 --- a/fastdeploy/vision/classification/ppcls/preprocessor.h +++ b/fastdeploy/vision/classification/ppcls/preprocessor.h @@ -52,7 +52,7 @@ class FASTDEPLOY_DECL PaddleClasPreprocessor { void DisablePermute(); private: - bool BuildPreprocessPipelineFromConfig(const std::string& config_file); + bool BuildPreprocessPipelineFromConfig(); std::vector> processors_; bool initialized_ = false; bool use_cuda_ = false; @@ -63,7 +63,7 @@ class FASTDEPLOY_DECL PaddleClasPreprocessor { // for recording the switch of normalize bool disable_normalize = false; // read config file - std::string config_file; + std::string config_file_; }; } // namespace classification diff --git a/python/fastdeploy/vision/classification/ppcls/__init__.py b/python/fastdeploy/vision/classification/ppcls/__init__.py index 9dc148f6ccf..b88c4361fbd 100644 --- a/python/fastdeploy/vision/classification/ppcls/__init__.py +++ b/python/fastdeploy/vision/classification/ppcls/__init__.py @@ -42,6 +42,18 @@ def use_gpu(self, gpu_id=-1): """ return self._preprocessor.use_gpu(gpu_id) + def disable_normalize(self): + """ + 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() + class PaddleClasPostprocessor: def __init__(self, topk=1): @@ -78,8 +90,6 @@ def __init__(self, """ super(PaddleClasModel, self).__init__(runtime_option) - - assert model_format in [ModelFormat.PADDLE, ModelFormat.RKNN], "PaddleClasModel only support model format of ModelFormat.PADDLE、RKNN now." self._model = C.vision.classification.PaddleClasModel( model_file, params_file, config_file, self._runtime_option, model_format) From d5bc9ade0f3aba2db760d53462f535bcd5244b5f Mon Sep 17 00:00:00 2001 From: pengwei08 Date: Wed, 28 Dec 2022 14:33:22 +0800 Subject: [PATCH 05/11] [RKNPU2]support rknpu2, improve doc #957 --- .../paddleclas/rknpu2/README.md | 38 ++++++++++++ .../paddleclas/rknpu2/cpp/CMakeLists.txt | 2 +- .../paddleclas/rknpu2/cpp/README.md | 15 +++-- .../paddleclas/rknpu2/cpp/infer.cc | 4 +- .../paddleclas/rknpu2/python/README.md | 59 +++++++------------ .../vision/classification/ppcls/model.cc | 3 - .../rknpu2/config/ResNet50_vd_infer_rknn.yaml | 10 ++++ 7 files changed, 83 insertions(+), 48 deletions(-) create mode 100644 examples/vision/classification/paddleclas/rknpu2/README.md create mode 100644 tools/rknpu2/config/ResNet50_vd_infer_rknn.yaml diff --git a/examples/vision/classification/paddleclas/rknpu2/README.md b/examples/vision/classification/paddleclas/rknpu2/README.md new file mode 100644 index 00000000000..b9e91c0b408 --- /dev/null +++ b/examples/vision/classification/paddleclas/rknpu2/README.md @@ -0,0 +1,38 @@ +# ResNet50_vd模型部署 + +## 转换模型 +下面以 ResNet50_vd为例子,教大家如何转换分类模型到RKNN模型。 + +```bash +# 安装 paddle2onnx +pip install paddle2onnx + +# 下载ResNet50_vd模型文件和测试图片 +wget https://bj.bcebos.com/paddlehub/fastdeploy/ResNet50_vd_infer.tgz +tar -xvf ResNet50_vd_infer.tgz +wget https://gitee.com/paddlepaddle/PaddleClas/raw/release/2.4/deploy/images/ImageNet/ILSVRC2012_val_00000010.jpeg + +# 静态图转ONNX模型,注意,这里的save_file请和压缩包名对齐 +paddle2onnx --model_dir ResNet50_vd_infer --model_filename inference.pdmodel --params_filename inference.pdiparams --save_file ResNet50_vd_infer/ResNet50_vd_infer.onnx --enable_dev_version True --opset_version 12 --enable_onnx_checker True + +# 固定shape,注意这里的inputs得对应netron.app展示的 inputs 的 name,有可能是image 或者 x +python -m paddle2onnx.optimize --input_model ResNet50_vd_infer/ResNet50_vd_infer.onnx --output_model ResNet50_vd_infer/ResNet50_vd_infer.onnx --input_shape_dict "{'inputs':[1,3,224,224]}" + +# 配置文件 ResNet50_vd_infer_rknn.yaml 如下: +model_path: ./ResNet50_vd_infer.onnx +output_folder: ./ +target_platform: RK3588 +normalize: + mean: [[0, 0, 0]] + std: [[1, 1, 1]] +outputs: [] +outputs_nodes: [] +do_quantization: False +dataset: + + +# ONNX模型转RKNN模型 +python tools/rknpu2/export.py \ + --config_path tools/rknpu2/config/ResNet50_vd_infer_rknn.yaml \ + --target_platform rk3588 +``` \ No newline at end of file diff --git a/examples/vision/classification/paddleclas/rknpu2/cpp/CMakeLists.txt b/examples/vision/classification/paddleclas/rknpu2/cpp/CMakeLists.txt index bb9b06ada58..6cfd9bf053b 100644 --- a/examples/vision/classification/paddleclas/rknpu2/cpp/CMakeLists.txt +++ b/examples/vision/classification/paddleclas/rknpu2/cpp/CMakeLists.txt @@ -18,7 +18,7 @@ set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/build/install) install(TARGETS rknpu_test DESTINATION ./) -install(DIRECTORY model DESTINATION ./) +install(DIRECTORY ppclas_model_dir DESTINATION ./) install(DIRECTORY images DESTINATION ./) file(GLOB FASTDEPLOY_LIBS ${FASTDEPLOY_INSTALL_DIR}/lib/*) diff --git a/examples/vision/classification/paddleclas/rknpu2/cpp/README.md b/examples/vision/classification/paddleclas/rknpu2/cpp/README.md index 8abdd6610c9..0d77107e8d1 100644 --- a/examples/vision/classification/paddleclas/rknpu2/cpp/README.md +++ b/examples/vision/classification/paddleclas/rknpu2/cpp/README.md @@ -16,9 +16,9 @@ . ├── CMakeLists.txt ├── build # 编译文件夹 -├── image # 存放图片的文件夹 +├── images # 存放图片的文件夹 ├── infer.cc -├── model # 存放模型文件的文件夹 +├── ppclas_model_dir # 存放模型文件的文件夹 └── thirdpartys # 存放sdk的文件夹 ``` @@ -26,7 +26,7 @@ ```bash mkdir build mkdir images -mkdir model +mkdir ppclas_model_dir mkdir thirdpartys ``` @@ -39,7 +39,7 @@ fastdeploy-0.0.3目录,请移动它至thirdpartys目录下. ### 拷贝模型文件,以及配置文件至model文件夹 在Paddle动态图模型 -> Paddle静态图模型 -> ONNX模型的过程中,将生成ONNX文件以及对应的yaml配置文件,请将配置文件存放到model文件夹内。 -转换为RKNN后的模型文件也需要拷贝至model,转换方案: ([ResNet50_vd RKNN模型](../python/README.md))。 +转换为RKNN后的模型文件也需要拷贝至model,转换方案: ([ResNet50_vd RKNN模型](../README.md))。 ### 准备测试图片至image文件夹 ```bash @@ -59,7 +59,7 @@ make install ```bash cd ./build/install -./rknpu_test ./model ./images/ILSVRC2012_val_00000010.jpeg +./rknpu_test ./ppclas_model_dir ./images/ILSVRC2012_val_00000010.jpeg ``` ## 运行结果展示 @@ -71,3 +71,8 @@ scores: 0.684570, ## 注意事项 RKNPU上对模型的输入要求是使用NHWC格式,且图片归一化操作会在转RKNN模型时,内嵌到模型中,因此我们在使用FastDeploy部署时, DisablePermute(C++)或`disable_permute(Python),在预处理阶段禁用数据格式的转换。 + +## 其它文档 +- [ResNet50_vd Python 部署](../python) +- [模型预测结果说明](../../../../../../docs/api/vision_results/) +- [转换ResNet50_vd RKNN模型文档](../README.md) \ No newline at end of file diff --git a/examples/vision/classification/paddleclas/rknpu2/cpp/infer.cc b/examples/vision/classification/paddleclas/rknpu2/cpp/infer.cc index 3d02ef9135d..6424e0046ec 100755 --- a/examples/vision/classification/paddleclas/rknpu2/cpp/infer.cc +++ b/examples/vision/classification/paddleclas/rknpu2/cpp/infer.cc @@ -47,8 +47,8 @@ void RKNPU2Infer(const std::string& model_dir, const std::string& image_file) { 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" + << "Usage: rknpu_test path/to/model_dir path/to/image run_option, " + "e.g ./rknpu_test ./ppclas_model_dir ./images/ILSVRC2012_val_00000010.jpeg << std::endl; return -1; } diff --git a/examples/vision/classification/paddleclas/rknpu2/python/README.md b/examples/vision/classification/paddleclas/rknpu2/python/README.md index b1a14292c06..0b3f02754ab 100644 --- a/examples/vision/classification/paddleclas/rknpu2/python/README.md +++ b/examples/vision/classification/paddleclas/rknpu2/python/README.md @@ -1,45 +1,20 @@ -# ResNet50_vd模型部署 +# ResNet50_vd Python部署示例 -## 转换模型 -下面以 ResNet50_vd为例子,教大家如何转换分类模型到RKNN模型。 +在部署前,需确认以下两个步骤 -```bash -# 安装 paddle2onnx -pip install paddle2onnx +- 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../../docs/cn/build_and_install/rknpu2.md) -# 下载ResNet50_vd模型文件和测试图片 -wget https://bj.bcebos.com/paddlehub/fastdeploy/ResNet50_vd_infer.tgz -tar -xvf ResNet50_vd_infer.tgz -wget https://gitee.com/paddlepaddle/PaddleClas/raw/release/2.4/deploy/images/ImageNet/ILSVRC2012_val_00000010.jpeg +本目录下提供`infer.py`快速完成 ResNet50_vd 在RKNPU上部署的示例。执行如下脚本即可完成 -# 静态图转ONNX模型,注意,这里的save_file请和压缩包名对齐 -paddle2onnx --model_dir ResNet50_vd_infer --model_filename inference.pdmodel --params_filename inference.pdiparams --save_file ResNet50_vd_infer/ResNet50_vd_infer.onnx --enable_dev_version True --opset_version 12 --enable_onnx_checker True - -# 固定shape,注意这里的inputs得对应netron.app展示的 inputs 的 name,有可能是image 或者 x -python -m paddle2onnx.optimize --input_model ResNet50_vd_infer/ResNet50_vd_infer.onnx --output_model ResNet50_vd_infer/ResNet50_vd_infer.onnx --input_shape_dict "{'inputs':[1,3,224,224]}" - -# 配置文件 ResNet50_vd_infer_rknn.yaml 如下: -model_path: ./ResNet50_vd_infer.onnx -output_folder: ./ -target_platform: RK3588 -normalize: - mean: [[0, 0, 0]] - std: [[1, 1, 1]] -outputs: [] -outputs_nodes: [] -do_quantization: False -dataset: - - -# ONNX模型转RKNN模型 -python tools/rknpu2/export.py \ - --config_path tools/rknpu2/config/ResNet50_vd_infer_rknn.yaml \ - --target_platform rk3588 -``` +```bash +# 下载部署示例代码 +git clone https://github.com/PaddlePaddle/FastDeploy.git +cd FastDeploy/examples/vision/classification/paddleclas/rknpu2/python -## 执行代码 +# 下载图片 +wget https://gitee.com/paddlepaddle/PaddleClas/raw/release/2.4/deploy/images/ImageNet/ILSVRC2012_val_00000010.jpeg -```bash +# 推理 python3 infer.py --model_file ./ResNet50_vd_infer/ResNet50_vd_infer_rk3588.rknn --config_file ResNet50_vd_infer/inference_cls.yaml --image ILSVRC2012_val_00000010.jpeg # 运行完成后返回结果如下所示 @@ -47,4 +22,14 @@ ClassifyResult( label_ids: 153, scores: 0.684570, ) -``` \ No newline at end of file +``` + + +## 注意事项 +RKNPU上对模型的输入要求是使用NHWC格式,且图片归一化操作会在转RKNN模型时,内嵌到模型中,因此我们在使用FastDeploy部署时, +DisablePermute(C++)或`disable_permute(Python),在预处理阶段禁用数据格式的转换。 + +## 其它文档 +- [ResNet50_vd C++部署](../cpp) +- [模型预测结果说明](../../../../../../docs/api/vision_results/) +- [转换ResNet50_vd RKNN模型文档](../README.md) \ No newline at end of file diff --git a/fastdeploy/vision/classification/ppcls/model.cc b/fastdeploy/vision/classification/ppcls/model.cc index 66897c3f160..a9b5b46f010 100755 --- a/fastdeploy/vision/classification/ppcls/model.cc +++ b/fastdeploy/vision/classification/ppcls/model.cc @@ -32,9 +32,6 @@ PaddleClasModel::PaddleClasModel(const std::string& model_file, valid_ascend_backends = {Backend::LITE}; valid_kunlunxin_backends = {Backend::LITE}; valid_ipu_backends = {Backend::PDINFER}; - } else if (model_format == ModelFormat::ONNX) { - valid_cpu_backends = {Backend::ORT, Backend::OPENVINO}; - valid_gpu_backends = {Backend::ORT, Backend::TRT}; } else { valid_cpu_backends = {Backend::ORT, Backend::OPENVINO}; valid_gpu_backends = {Backend::ORT, Backend::TRT}; diff --git a/tools/rknpu2/config/ResNet50_vd_infer_rknn.yaml b/tools/rknpu2/config/ResNet50_vd_infer_rknn.yaml new file mode 100644 index 00000000000..d47075090e9 --- /dev/null +++ b/tools/rknpu2/config/ResNet50_vd_infer_rknn.yaml @@ -0,0 +1,10 @@ +model_path: ./ResNet50_vd_infer.onnx +output_folder: ./ +target_platform: RK3588 +normalize: + mean: [[0, 0, 0]] + std: [[1, 1, 1]] +outputs: [] +outputs_nodes: [] +do_quantization: False +dataset: \ No newline at end of file From 3f764227a1df51c00cec4447800a391168f8ad49 Mon Sep 17 00:00:00 2001 From: pengwei08 Date: Wed, 28 Dec 2022 14:48:58 +0800 Subject: [PATCH 06/11] [RKNPU2]support rknpu2, improve doc #957 --- .../classification/paddleclas/rknpu2/README.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/examples/vision/classification/paddleclas/rknpu2/README.md b/examples/vision/classification/paddleclas/rknpu2/README.md index b9e91c0b408..9d568e73f01 100644 --- a/examples/vision/classification/paddleclas/rknpu2/README.md +++ b/examples/vision/classification/paddleclas/rknpu2/README.md @@ -13,10 +13,18 @@ tar -xvf ResNet50_vd_infer.tgz wget https://gitee.com/paddlepaddle/PaddleClas/raw/release/2.4/deploy/images/ImageNet/ILSVRC2012_val_00000010.jpeg # 静态图转ONNX模型,注意,这里的save_file请和压缩包名对齐 -paddle2onnx --model_dir ResNet50_vd_infer --model_filename inference.pdmodel --params_filename inference.pdiparams --save_file ResNet50_vd_infer/ResNet50_vd_infer.onnx --enable_dev_version True --opset_version 12 --enable_onnx_checker True +paddle2onnx --model_dir ResNet50_vd_infer \ + --model_filename inference.pdmodel \ + --params_filename inference.pdiparams \ + --save_file ResNet50_vd_infer/ResNet50_vd_infer.onnx \ + --enable_dev_version True \ + --opset_version 12 \ + --enable_onnx_checker True # 固定shape,注意这里的inputs得对应netron.app展示的 inputs 的 name,有可能是image 或者 x -python -m paddle2onnx.optimize --input_model ResNet50_vd_infer/ResNet50_vd_infer.onnx --output_model ResNet50_vd_infer/ResNet50_vd_infer.onnx --input_shape_dict "{'inputs':[1,3,224,224]}" +python -m paddle2onnx.optimize --input_model ResNet50_vd_infer/ResNet50_vd_infer.onnx \ + --output_model ResNet50_vd_infer/ResNet50_vd_infer.onnx \ + --input_shape_dict "{'inputs':[1,3,224,224]}" # 配置文件 ResNet50_vd_infer_rknn.yaml 如下: model_path: ./ResNet50_vd_infer.onnx From 434a279e8cc91c7f8b1630157584af3d8cca13dd Mon Sep 17 00:00:00 2001 From: pengwei08 Date: Wed, 28 Dec 2022 15:11:36 +0800 Subject: [PATCH 07/11] [RKNPU2]support rknpu2, improve doc #957 --- .../paddleclas/rknpu2/README.md | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/examples/vision/classification/paddleclas/rknpu2/README.md b/examples/vision/classification/paddleclas/rknpu2/README.md index 9d568e73f01..19bd6b362a9 100644 --- a/examples/vision/classification/paddleclas/rknpu2/README.md +++ b/examples/vision/classification/paddleclas/rknpu2/README.md @@ -10,7 +10,6 @@ pip install paddle2onnx # 下载ResNet50_vd模型文件和测试图片 wget https://bj.bcebos.com/paddlehub/fastdeploy/ResNet50_vd_infer.tgz tar -xvf ResNet50_vd_infer.tgz -wget https://gitee.com/paddlepaddle/PaddleClas/raw/release/2.4/deploy/images/ImageNet/ILSVRC2012_val_00000010.jpeg # 静态图转ONNX模型,注意,这里的save_file请和压缩包名对齐 paddle2onnx --model_dir ResNet50_vd_infer \ @@ -25,22 +24,34 @@ paddle2onnx --model_dir ResNet50_vd_infer \ python -m paddle2onnx.optimize --input_model ResNet50_vd_infer/ResNet50_vd_infer.onnx \ --output_model ResNet50_vd_infer/ResNet50_vd_infer.onnx \ --input_shape_dict "{'inputs':[1,3,224,224]}" +``` -# 配置文件 ResNet50_vd_infer_rknn.yaml 如下: + ### 编写模型导出配置文件 +以转化RK3588的RKNN模型为例子,我们需要编辑tools/rknpu2/config/RK3568/ResNet50_vd_infer_rknn.yaml,来转换ONNX模型到RKNN模型。 + +默认的 mean=0, std=1是在内存做normalize,如果你需要在NPU上执行normalize操作,请根据你的模型配置normalize参数,例如: +```yaml model_path: ./ResNet50_vd_infer.onnx output_folder: ./ target_platform: RK3588 normalize: - mean: [[0, 0, 0]] - std: [[1, 1, 1]] + mean: [[0.485,0.456,0.406]] + std: [[0.229,0.224,0.225]] outputs: [] outputs_nodes: [] do_quantization: False dataset: +``` # ONNX模型转RKNN模型 +```shell python tools/rknpu2/export.py \ --config_path tools/rknpu2/config/ResNet50_vd_infer_rknn.yaml \ --target_platform rk3588 -``` \ No newline at end of file +``` + +## 其他链接 +- [Cpp部署](./cpp) +- [Python部署](./python) +- [视觉模型预测结果](../../../../../docs/api/vision_results/) \ No newline at end of file From 9f4138002bcb81b802dd60ad425795b572088c9b Mon Sep 17 00:00:00 2001 From: pengwei08 Date: Wed, 28 Dec 2022 15:54:57 +0800 Subject: [PATCH 08/11] [RKNPU2]support rknpu2, improve doc #957 --- docs/cn/faq/rknpu2/rknpu2.md | 1 + .../vision/classification/paddleclas/rknpu2/README.md | 2 +- .../classification/paddleclas/rknpu2/cpp/README.md | 2 +- .../vision/classification/paddleclas/rknpu2/cpp/infer.cc | 9 +++++---- .../classification/paddleclas/rknpu2/python/README.md | 2 +- 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/docs/cn/faq/rknpu2/rknpu2.md b/docs/cn/faq/rknpu2/rknpu2.md index bb06508600c..91ca0a194be 100644 --- a/docs/cn/faq/rknpu2/rknpu2.md +++ b/docs/cn/faq/rknpu2/rknpu2.md @@ -23,6 +23,7 @@ ONNX模型不能直接调用RK芯片中的NPU进行运算,需要把ONNX模型 | Segmentation | PP-HumanSegV2Lite | portrait | 133/43 | | Segmentation | PP-HumanSegV2Lite | human | 133/43 | | Face Detection | SCRFD | SCRFD-2.5G-kps-640 | 108/42 | +| PPClas | ResNet50_vd | ResNet50_vd | -/92 | ## RKNPU2 Backend推理使用教程 diff --git a/examples/vision/classification/paddleclas/rknpu2/README.md b/examples/vision/classification/paddleclas/rknpu2/README.md index 19bd6b362a9..4eaf5e806be 100644 --- a/examples/vision/classification/paddleclas/rknpu2/README.md +++ b/examples/vision/classification/paddleclas/rknpu2/README.md @@ -1,4 +1,4 @@ -# ResNet50_vd模型部署 +# PPClas 模型部署 ## 转换模型 下面以 ResNet50_vd为例子,教大家如何转换分类模型到RKNN模型。 diff --git a/examples/vision/classification/paddleclas/rknpu2/cpp/README.md b/examples/vision/classification/paddleclas/rknpu2/cpp/README.md index 0d77107e8d1..d3129be6860 100644 --- a/examples/vision/classification/paddleclas/rknpu2/cpp/README.md +++ b/examples/vision/classification/paddleclas/rknpu2/cpp/README.md @@ -1,4 +1,4 @@ -# ResNet50_vd C++部署示例 +# PPClas C++部署示例 本目录下用于展示 ResNet50_vd 模型在RKNPU2上的部署,以下的部署过程以 ResNet50_vd 为例子。 diff --git a/examples/vision/classification/paddleclas/rknpu2/cpp/infer.cc b/examples/vision/classification/paddleclas/rknpu2/cpp/infer.cc index 6424e0046ec..fdc84dcd58c 100755 --- a/examples/vision/classification/paddleclas/rknpu2/cpp/infer.cc +++ b/examples/vision/classification/paddleclas/rknpu2/cpp/infer.cc @@ -31,24 +31,25 @@ void RKNPU2Infer(const std::string& model_dir, const std::string& image_file) { return; } model.GetPreprocessor().DisablePermute(); - + fastdeploy::TimeCounter tc; + tc.Start(); auto im = cv::imread(image_file); - fastdeploy::vision::ClassifyResult res; if (!model.Predict(im, &res)) { std::cerr << "Failed to predict." << std::endl; return; } - // print res std::cout << res.Str() << std::endl; + tc.End(); + tc.PrintInfo("PPClas in RKNPU2"); } int main(int argc, char* argv[]) { if (argc < 3) { std::cout << "Usage: rknpu_test path/to/model_dir path/to/image run_option, " - "e.g ./rknpu_test ./ppclas_model_dir ./images/ILSVRC2012_val_00000010.jpeg + "e.g ./rknpu_test ./ppclas_model_dir ./images/ILSVRC2012_val_00000010.jpeg" << std::endl; return -1; } diff --git a/examples/vision/classification/paddleclas/rknpu2/python/README.md b/examples/vision/classification/paddleclas/rknpu2/python/README.md index 0b3f02754ab..e9f4e313ee6 100644 --- a/examples/vision/classification/paddleclas/rknpu2/python/README.md +++ b/examples/vision/classification/paddleclas/rknpu2/python/README.md @@ -1,4 +1,4 @@ -# ResNet50_vd Python部署示例 +# PPClas Python部署示例 在部署前,需确认以下两个步骤 From 1215489e4bc4df88eb1dfd9b7e40a395c574446a Mon Sep 17 00:00:00 2001 From: pengwei08 Date: Wed, 28 Dec 2022 16:12:35 +0800 Subject: [PATCH 09/11] [RKNPU2]support rknpu2, improve doc #957 --- docs/cn/faq/rknpu2/rknpu2.md | 2 +- examples/vision/classification/paddleclas/rknpu2/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/cn/faq/rknpu2/rknpu2.md b/docs/cn/faq/rknpu2/rknpu2.md index 91ca0a194be..d9e811f8de1 100644 --- a/docs/cn/faq/rknpu2/rknpu2.md +++ b/docs/cn/faq/rknpu2/rknpu2.md @@ -23,7 +23,7 @@ ONNX模型不能直接调用RK芯片中的NPU进行运算,需要把ONNX模型 | Segmentation | PP-HumanSegV2Lite | portrait | 133/43 | | Segmentation | PP-HumanSegV2Lite | human | 133/43 | | Face Detection | SCRFD | SCRFD-2.5G-kps-640 | 108/42 | -| PPClas | ResNet50_vd | ResNet50_vd | -/92 | +| Classification | ResNet | ResNet50_vd | -/92 | ## RKNPU2 Backend推理使用教程 diff --git a/examples/vision/classification/paddleclas/rknpu2/README.md b/examples/vision/classification/paddleclas/rknpu2/README.md index 4eaf5e806be..5f3707af570 100644 --- a/examples/vision/classification/paddleclas/rknpu2/README.md +++ b/examples/vision/classification/paddleclas/rknpu2/README.md @@ -27,7 +27,7 @@ python -m paddle2onnx.optimize --input_model ResNet50_vd_infer/ResNet50_vd_infer ``` ### 编写模型导出配置文件 -以转化RK3588的RKNN模型为例子,我们需要编辑tools/rknpu2/config/RK3568/ResNet50_vd_infer_rknn.yaml,来转换ONNX模型到RKNN模型。 +以转化RK3588的RKNN模型为例子,我们需要编辑tools/rknpu2/config/ResNet50_vd_infer_rknn.yaml,来转换ONNX模型到RKNN模型。 默认的 mean=0, std=1是在内存做normalize,如果你需要在NPU上执行normalize操作,请根据你的模型配置normalize参数,例如: ```yaml From 03e289ace30eb29102393d27a1869fec6f6ff015 Mon Sep 17 00:00:00 2001 From: pengwei08 Date: Wed, 28 Dec 2022 16:30:28 +0800 Subject: [PATCH 10/11] [RKNPU2]support rknpu2, improve doc #957 --- .../vision/classification/paddleclas/rknpu2/cpp/README.md | 2 +- .../vision/classification/paddleclas/rknpu2/python/README.md | 2 +- fastdeploy/vision/classification/ppcls/preprocessor.h | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/vision/classification/paddleclas/rknpu2/cpp/README.md b/examples/vision/classification/paddleclas/rknpu2/cpp/README.md index d3129be6860..6898d06c79b 100644 --- a/examples/vision/classification/paddleclas/rknpu2/cpp/README.md +++ b/examples/vision/classification/paddleclas/rknpu2/cpp/README.md @@ -1,4 +1,4 @@ -# PPClas C++部署示例 +# PaddleClas 模型RKNPU2部署 本目录下用于展示 ResNet50_vd 模型在RKNPU2上的部署,以下的部署过程以 ResNet50_vd 为例子。 diff --git a/examples/vision/classification/paddleclas/rknpu2/python/README.md b/examples/vision/classification/paddleclas/rknpu2/python/README.md index e9f4e313ee6..b85bb81f70a 100644 --- a/examples/vision/classification/paddleclas/rknpu2/python/README.md +++ b/examples/vision/classification/paddleclas/rknpu2/python/README.md @@ -1,4 +1,4 @@ -# PPClas Python部署示例 +# PaddleClas Python部署示例 在部署前,需确认以下两个步骤 diff --git a/fastdeploy/vision/classification/ppcls/preprocessor.h b/fastdeploy/vision/classification/ppcls/preprocessor.h index f61f0cee84e..2162ac0951c 100644 --- a/fastdeploy/vision/classification/ppcls/preprocessor.h +++ b/fastdeploy/vision/classification/ppcls/preprocessor.h @@ -46,9 +46,9 @@ class FASTDEPLOY_DECL PaddleClasPreprocessor { bool WithGpu() { return use_cuda_; } - // This function will disable normalize in preprocessing step. + /// This function will disable normalize in preprocessing step. void DisableNormalize(); - // This function will disable hwc2chw in preprocessing step. + /// This function will disable hwc2chw in preprocessing step. void DisablePermute(); private: From da3596fdcf8db3cc41d4b0c369e869d14bb2af12 Mon Sep 17 00:00:00 2001 From: pengwei08 Date: Wed, 28 Dec 2022 16:32:13 +0800 Subject: [PATCH 11/11] [RKNPU2]support rknpu2, improve doc #957 --- examples/vision/classification/paddleclas/rknpu2/README.md | 2 +- examples/vision/classification/paddleclas/rknpu2/cpp/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/vision/classification/paddleclas/rknpu2/README.md b/examples/vision/classification/paddleclas/rknpu2/README.md index 5f3707af570..bd4305dc0a5 100644 --- a/examples/vision/classification/paddleclas/rknpu2/README.md +++ b/examples/vision/classification/paddleclas/rknpu2/README.md @@ -1,4 +1,4 @@ -# PPClas 模型部署 +# PaddleClas 模型RKNPU2部署 ## 转换模型 下面以 ResNet50_vd为例子,教大家如何转换分类模型到RKNN模型。 diff --git a/examples/vision/classification/paddleclas/rknpu2/cpp/README.md b/examples/vision/classification/paddleclas/rknpu2/cpp/README.md index 6898d06c79b..1e1883486d1 100644 --- a/examples/vision/classification/paddleclas/rknpu2/cpp/README.md +++ b/examples/vision/classification/paddleclas/rknpu2/cpp/README.md @@ -1,4 +1,4 @@ -# PaddleClas 模型RKNPU2部署 +# PaddleClas C++部署示例 本目录下用于展示 ResNet50_vd 模型在RKNPU2上的部署,以下的部署过程以 ResNet50_vd 为例子。