Skip to content

Commit

Permalink
[Bug Fix] Fixed Ppclas Bugs (PaddlePaddle#1009)
Browse files Browse the repository at this point in the history
* 更新ppclas

* 更新ppclas

* 更新ppclas

* 更新ppclas
  • Loading branch information
Zheng-Bicheng authored Dec 29, 2022
1 parent ab09296 commit 0401580
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 32 deletions.
2 changes: 1 addition & 1 deletion docs/cn/faq/rknpu2/rknpu2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
58 changes: 42 additions & 16 deletions examples/vision/classification/paddleclas/rknpu2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 转换模型
下面以 ResNet50_vd为例子,教大家如何转换分类模型到RKNN模型。

### 导出ONNX模型
```bash
# 安装 paddle2onnx
pip install paddle2onnx
Expand All @@ -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 \
Expand All @@ -54,4 +80,4 @@ python tools/rknpu2/export.py \
## 其他链接
- [Cpp部署](./cpp)
- [Python部署](./python)
- [视觉模型预测结果](../../../../../docs/api/vision_results/)
- [视觉模型预测结果](../../../../../docs/api/vision_results/)
8 changes: 4 additions & 4 deletions fastdeploy/vision/classification/ppcls/preprocessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ bool PaddleClasPreprocessor::BuildPreprocessPipelineFromConfig() {
int height = op.begin()->second["size"].as<int>();
processors_.push_back(std::make_shared<CenterCrop>(width, height));
} else if (op_name == "NormalizeImage") {
if (!disable_normalize) {
if (!disable_normalize_) {
auto mean = op.begin()->second["mean"].as<std::vector<float>>();
auto std = op.begin()->second["std"].as<std::vector<float>>();
auto scale = op.begin()->second["scale"].as<float>();
Expand All @@ -67,7 +67,7 @@ bool PaddleClasPreprocessor::BuildPreprocessPipelineFromConfig() {
processors_.push_back(std::make_shared<Normalize>(mean, std));
}
} else if (op_name == "ToCHWImage") {
if (!disable_permute) {
if (!disable_permute_) {
processors_.push_back(std::make_shared<HWC2CHW>());
}
} else {
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions fastdeploy/vision/classification/ppcls/preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
};
Expand Down
23 changes: 14 additions & 9 deletions tools/rknpu2/config/ResNet50_vd_infer_rknn.yaml
Original file line number Diff line number Diff line change
@@ -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:
dataset: "./ResNet50_vd_infer/dataset.txt"

0 comments on commit 0401580

Please sign in to comment.