Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update Quantized PaddleOCRv3 For RKNN #2171

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions examples/vision/ocr/PP-OCR/rockchip/cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
同时, 在RKNPU2上部署PP-OCR系列模型时,我们需要把Paddle的推理模型转为RKNN模型.
由于rknn_toolkit2工具暂不支持直接从Paddle直接转换为RKNN模型,因此我们需要先将Paddle推理模型转为ONNX模型, 最后转为RKNN模型, 示例如下.

### 2.1 部署非量化模型

```bash
# 下载PP-OCRv3文字检测模型
wget https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_det_infer.tar
Expand Down Expand Up @@ -54,11 +56,54 @@ python -m paddle2onnx.optimize --input_model ch_PP-OCRv3_rec_infer/ch_PP-OCRv3_r
--input_shape_dict "{'x':[1,3,48,320]}"

# 在rockchip/rknpu2_tools/目录下, 我们为用户提供了转换ONNX模型到RKNN模型的工具
python rockchip/rknpu2_tools/export.py --config_path tools/rknpu2/config/ppocrv3_det.yaml \
python rockchip/rknpu2_tools/export.py --config_path rockchip/rknpu2_tools/config/ppocrv3_det_unquantized.yaml \
--target_platform rk3588
python rockchip/rknpu2_tools/export.py --config_path rockchip/rknpu2_tools/config/ppocrv3_rec_unquantized.yaml \
--target_platform rk3588
python rockchip/rknpu2_tools/export.py --config_path tools/rknpu2/config/ppocrv3_rec.yaml \
python rockchip/rknpu2_tools/export.py --config_path rockchip/rknpu2_tools/config/ppocrv3_cls_unquantized.yaml \
--target_platform rk3588
```

### 2.2 部署量化模型

`PaddleOCRv3-CLS`模型静态量化后精度较低,因此建议使用量化模型时保证输入图片方向,不开启方向分类检测器。

> 注意事项:
>
> 以下命令均在`/path/to/fastdeploy/examples/vision/ocr/PP-OCR`目录下执行

```bash
# 下载PP-OCRv3文字检测模型
wget https://bj.bcebos.com/v1/paddle-slim-models/act/OCR/PPOCRV3_det_QAT.tar
tar -xvf PPOCRV3_det_QAT.tar
# 下载PP-OCRv3文字识别模型
wget https://bj.bcebos.com/v1/paddle-slim-models/act/OCR/PPOCRV3_rec_QAT.tar
tar -xvf PPOCRV3_rec_QAT.tar

# 请用户自行安装最新发布版本的paddle2onnx, 转换模型到ONNX格式的模型
paddle2onnx --model_dir PPOCRV3_det_QAT \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--save_file PPOCRV3_det_QAT/PPOCRV3_det_QAT.onnx \
--enable_dev_version True
paddle2onnx --model_dir PPOCRV3_rec_QAT \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--save_file PPOCRV3_rec_QAT/PPOCRV3_rec_QAT.onnx \
--enable_dev_version True

# 固定模型的输入shape
python -m paddle2onnx.optimize --input_model PPOCRV3_det_QAT/PPOCRV3_det_QAT.onnx \
--output_model PPOCRV3_det_QAT/PPOCRV3_det_QAT.onnx \
--input_shape_dict "{'x':[1,3,960,960]}"
python -m paddle2onnx.optimize --input_model PPOCRV3_rec_QAT/PPOCRV3_rec_QAT.onnx \
--output_model PPOCRV3_rec_QAT/PPOCRV3_rec_QAT.onnx \
--input_shape_dict "{'x':[1,3,48,320]}"

# 在rockchip/rknpu2_tools/目录下, 我们为用户提供了转换ONNX模型到RKNN模型的工具
python rockchip/rknpu2_tools/export.py --config_path rockchip/rknpu2_tools/config/ppocrv3_det_quantized.yaml \
--target_platform rk3588
python rockchip/rknpu2_tools/export.py --config_path tools/rknpu2/config/ppocrv3_cls.yaml \
python rockchip/rknpu2_tools/export.py --config_path rockchip/rknpu2_tools/config/ppocrv3_rec_quantized.yaml \
--target_platform rk3588
```

Expand Down
6 changes: 3 additions & 3 deletions examples/vision/ocr/PP-OCR/rockchip/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ python -m paddle2onnx.optimize --input_model ch_PP-OCRv3_rec_infer/ch_PP-OCRv3_r
--input_shape_dict "{'x':[1,3,48,320]}"

# 在rockchip/rknpu2_tools/目录下, 我们为用户提供了转换ONNX模型到RKNN模型的工具
python rockchip/rknpu2_tools/export.py --config_path tools/rknpu2/config/ppocrv3_det.yaml \
python rockchip/rknpu2_tools/export.py --config_path tools/rknpu2/config/ppocrv3_det_unquantized.yaml \
--target_platform rk3588
python rockchip/rknpu2_tools/export.py --config_path tools/rknpu2/config/ppocrv3_rec.yaml \
python rockchip/rknpu2_tools/export.py --config_path tools/rknpu2/config/ppocrv3_rec_unquantized.yaml \
--target_platform rk3588
python rockchip/rknpu2_tools/export.py --config_path tools/rknpu2/config/ppocrv3_cls.yaml \
python rockchip/rknpu2_tools/export.py --config_path tools/rknpu2/config/ppocrv3_cls_unquantized.yaml \
--target_platform rk3588
```

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
mean:
-
- 123.675
- 116.28
- 103.53
std:
-
- 58.395
- 57.12
- 57.375
model_path: "./PPOCRV3_det_QAT/PPOCRV3_det_QAT.onnx"
outputs_nodes:
do_quantization: False
dataset:
output_folder: "./PPOCRV3_det_QAT"
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
mean:
-
- 127.5
- 127.5
- 127.5
std:
-
- 127.5
- 127.5
- 127.5
model_path: "./PPOCRV3_rec_QAT/PPOCRV3_rec_QAT.onnx"
outputs_nodes:
do_quantization: False
dataset:
output_folder: "./PPOCRV3_rec_QAT"
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ paddle2onnx --model_dir Portrait_PP_HumanSegV2_Lite_256x144_infer \
cp -r ./Portrait_PP_HumanSegV2_Lite_256x144_infer /path/to/Fastdeploy
# 转换模型,模型将生成在Portrait_PP_HumanSegV2_Lite_256x144_infer目录下
python tools/rknpu2/export.py \
--config_path tools/rknpu2/config/Portrait_PP_HumanSegV2_Lite_256x144_infer.yaml \
--config_path tools/rknpu2/config/Portrait_PP_HumanSegV2_Lite_256x144_infer_unquantized.yaml \
--target_platform rk3588
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ paddle2onnx --model_dir Portrait_PP_HumanSegV2_Lite_256x144_infer \
cp -r ./Portrait_PP_HumanSegV2_Lite_256x144_infer /path/to/Fastdeploy
# Convert model, the model will be generated in the Portrait_PP_HumanSegV2_Lite_256x144_infer directory.
python tools/rknpu2/export.py \
--config_path tools/rknpu2/config/Portrait_PP_HumanSegV2_Lite_256x144_infer.yaml \
--config_path tools/rknpu2/config/Portrait_PP_HumanSegV2_Lite_256x144_infer_unquantized.yaml \
--target_platform rk3588
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ std:
- 127.5
model_path: ./Portrait_PP_HumanSegV2_Lite_256x144_infer/Portrait_PP_HumanSegV2_Lite_256x144_infer.onnx
outputs_nodes:
do_quantization: True
do_quantization: False
dataset: "./Portrait_PP_HumanSegV2_Lite_256x144_infer/dataset.txt"
output_folder: "./Portrait_PP_HumanSegV2_Lite_256x144_infer"
15 changes: 15 additions & 0 deletions tools/rknpu2/config/ppocrv3_det_quantized.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
mean:
-
- 123.675
- 116.28
- 103.53
std:
-
- 58.395
- 57.12
- 57.375
model_path: "./PPOCRV3_det_QAT/PPOCRV3_det_QAT.onnx"
outputs_nodes:
do_quantization: False
dataset:
output_folder: "./PPOCRV3_det_QAT"
15 changes: 15 additions & 0 deletions tools/rknpu2/config/ppocrv3_rec_quantized.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
mean:
-
- 127.5
- 127.5
- 127.5
std:
-
- 127.5
- 127.5
- 127.5
model_path: "./PPOCRV3_rec_QAT/PPOCRV3_rec_QAT.onnx"
outputs_nodes:
do_quantization: False
dataset:
output_folder: "./PPOCRV3_rec_QAT"