From 0401580264fd015e60cc0024694871406e3599f2 Mon Sep 17 00:00:00 2001 From: Zheng-Bicheng <58363586+Zheng-Bicheng@users.noreply.github.com> Date: Thu, 29 Dec 2022 14:13:55 +0800 Subject: [PATCH] [Bug Fix] Fixed Ppclas Bugs (#1009) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 更新ppclas * 更新ppclas * 更新ppclas * 更新ppclas --- docs/cn/faq/rknpu2/rknpu2.md | 2 +- .../paddleclas/rknpu2/README.md | 58 ++++++++++++++----- .../classification/ppcls/preprocessor.cc | 8 +-- .../classification/ppcls/preprocessor.h | 4 +- .../rknpu2/config/ResNet50_vd_infer_rknn.yaml | 23 +++++--- 5 files changed, 63 insertions(+), 32 deletions(-) diff --git a/docs/cn/faq/rknpu2/rknpu2.md b/docs/cn/faq/rknpu2/rknpu2.md index e0384e0513..fcd3e7c35c 100644 --- a/docs/cn/faq/rknpu2/rknpu2.md +++ b/docs/cn/faq/rknpu2/rknpu2.md @@ -23,4 +23,4 @@ ONNX模型不能直接调用RK芯片中的NPU进行运算,需要把ONNX模型 | Segmentation | [PP-HumanSegV2Lite](../../../../examples/vision/segmentation/paddleseg/rknpu2/README.md) | portrait(int8) | 133/43 | | Segmentation | [PP-HumanSegV2Lite](../../../../examples/vision/segmentation/paddleseg/rknpu2/README.md) | human(int8) | 133/43 | | Face Detection | [SCRFD](../../../../examples/vision/facedet/scrfd/rknpu2/README.md) | SCRFD-2.5G-kps-640(int8) | 108/42 | -| Classification | [ResNet](../../../../examples/vision/classification/paddleclas/rknpu2/README.md) | ResNet50_vd | -/92 | +| Classification | [ResNet](../../../../examples/vision/classification/paddleclas/rknpu2/README.md) | ResNet50_vd | -/33 | diff --git a/examples/vision/classification/paddleclas/rknpu2/README.md b/examples/vision/classification/paddleclas/rknpu2/README.md index bd4305dc0a..d61e6c5706 100644 --- a/examples/vision/classification/paddleclas/rknpu2/README.md +++ b/examples/vision/classification/paddleclas/rknpu2/README.md @@ -3,6 +3,7 @@ ## 转换模型 下面以 ResNet50_vd为例子,教大家如何转换分类模型到RKNN模型。 +### 导出ONNX模型 ```bash # 安装 paddle2onnx pip install paddle2onnx @@ -17,34 +18,59 @@ paddle2onnx --model_dir ResNet50_vd_infer \ --params_filename inference.pdiparams \ --save_file ResNet50_vd_infer/ResNet50_vd_infer.onnx \ --enable_dev_version True \ - --opset_version 12 \ + --opset_version 10 \ --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]}" -``` +``` - ### 编写模型导出配置文件 -以转化RK3588的RKNN模型为例子,我们需要编辑tools/rknpu2/config/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参数,例如: +如果你需要在NPU上执行normalize操作,请根据你的模型配置normalize参数,例如: ```yaml -model_path: ./ResNet50_vd_infer.onnx -output_folder: ./ -target_platform: RK3588 -normalize: - mean: [[0.485,0.456,0.406]] - std: [[0.229,0.224,0.225]] -outputs: [] -outputs_nodes: [] +model_path: ./ResNet50_vd_infer/ResNet50_vd_infer.onnx +output_folder: ./ResNet50_vd_infer +mean: + - + - 123.675 + - 116.28 + - 103.53 +std: + - + - 58.395 + - 57.12 + - 57.375 +outputs_nodes: do_quantization: False -dataset: +dataset: "./ResNet50_vd_infer/dataset.txt" ``` +**在CPU上做normalize**可以参考以下yaml: +```yaml +model_path: ./ResNet50_vd_infer/ResNet50_vd_infer.onnx +output_folder: ./ResNet50_vd_infer +mean: + - + - 0 + - 0 + - 0 +std: + - + - 1 + - 1 + - 1 +outputs_nodes: +do_quantization: False +dataset: "./ResNet50_vd_infer/dataset.txt" +``` +这里我们选择在NPU上执行normalize操作. -# ONNX模型转RKNN模型 + +### ONNX模型转RKNN模型 ```shell python tools/rknpu2/export.py \ --config_path tools/rknpu2/config/ResNet50_vd_infer_rknn.yaml \ @@ -54,4 +80,4 @@ python tools/rknpu2/export.py \ ## 其他链接 - [Cpp部署](./cpp) - [Python部署](./python) -- [视觉模型预测结果](../../../../../docs/api/vision_results/) \ No newline at end of file +- [视觉模型预测结果](../../../../../docs/api/vision_results/) diff --git a/fastdeploy/vision/classification/ppcls/preprocessor.cc b/fastdeploy/vision/classification/ppcls/preprocessor.cc index ef8d8f20ef..aa4314cf87 100644 --- a/fastdeploy/vision/classification/ppcls/preprocessor.cc +++ b/fastdeploy/vision/classification/ppcls/preprocessor.cc @@ -57,7 +57,7 @@ bool PaddleClasPreprocessor::BuildPreprocessPipelineFromConfig() { int height = op.begin()->second["size"].as(); processors_.push_back(std::make_shared(width, height)); } else if (op_name == "NormalizeImage") { - if (!disable_normalize) { + if (!disable_normalize_) { auto mean = op.begin()->second["mean"].as>(); auto std = op.begin()->second["std"].as>(); auto scale = op.begin()->second["scale"].as(); @@ -67,7 +67,7 @@ bool PaddleClasPreprocessor::BuildPreprocessPipelineFromConfig() { processors_.push_back(std::make_shared(mean, std)); } } else if (op_name == "ToCHWImage") { - if (!disable_permute) { + if (!disable_permute_) { processors_.push_back(std::make_shared()); } } else { @@ -83,14 +83,14 @@ bool PaddleClasPreprocessor::BuildPreprocessPipelineFromConfig() { } void PaddleClasPreprocessor::DisableNormalize() { - this->disable_normalize = true; + this->disable_normalize_ = true; // the DisableNormalize function will be invalid if the configuration file is loaded during preprocessing if (!BuildPreprocessPipelineFromConfig()) { FDERROR << "Failed to build preprocess pipeline from configuration file." << std::endl; } } void PaddleClasPreprocessor::DisablePermute() { - this->disable_permute = true; + this->disable_permute_ = true; // the DisablePermute function will be invalid if the configuration file is loaded during preprocessing if (!BuildPreprocessPipelineFromConfig()) { FDERROR << "Failed to build preprocess pipeline from configuration file." << std::endl; diff --git a/fastdeploy/vision/classification/ppcls/preprocessor.h b/fastdeploy/vision/classification/ppcls/preprocessor.h index 2162ac0951..06b586d636 100644 --- a/fastdeploy/vision/classification/ppcls/preprocessor.h +++ b/fastdeploy/vision/classification/ppcls/preprocessor.h @@ -59,9 +59,9 @@ class FASTDEPLOY_DECL PaddleClasPreprocessor { // GPU device id int device_id_ = -1; // for recording the switch of hwc2chw - bool disable_permute = false; + bool disable_permute_ = false; // for recording the switch of normalize - bool disable_normalize = false; + bool disable_normalize_ = false; // read config file std::string config_file_; }; diff --git a/tools/rknpu2/config/ResNet50_vd_infer_rknn.yaml b/tools/rknpu2/config/ResNet50_vd_infer_rknn.yaml index d47075090e..72c27a18a7 100644 --- a/tools/rknpu2/config/ResNet50_vd_infer_rknn.yaml +++ b/tools/rknpu2/config/ResNet50_vd_infer_rknn.yaml @@ -1,10 +1,15 @@ -model_path: ./ResNet50_vd_infer.onnx -output_folder: ./ -target_platform: RK3588 -normalize: - mean: [[0, 0, 0]] - std: [[1, 1, 1]] -outputs: [] -outputs_nodes: [] +model_path: ./ResNet50_vd_infer/ResNet50_vd_infer.onnx +output_folder: ./ResNet50_vd_infer +mean: + - + - 123.675 + - 116.28 + - 103.53 +std: + - + - 58.395 + - 57.12 + - 57.375 +outputs_nodes: do_quantization: False -dataset: \ No newline at end of file +dataset: "./ResNet50_vd_infer/dataset.txt"