Skip to content

Commit

Permalink
[doc] fixing broken links and updating paddle2onnx usage descriptions (
Browse files Browse the repository at this point in the history
…#12749)

* [docs]:添加了对于Paddle2ONNX模型转化中选项--input_shape_dict的解释说明与解决方法

* Update readme_ch.md

[docs]:使用onnxsim修改input_shape可以兼容paddlepaddle现版本

* Update readme_ch.md

修改了不恰当的相关描述

* Rename inference_ch.md to inference.md

修改了中文版模型导出说明inference_ch.md命名为inference.md

* Update readme_ch.md

修改不恰当表述

* Update readme_ch.md

修改所有python3.7为python3

* update readme

---------

Co-authored-by: Wang Xin <xinwang614@gmail.com>
  • Loading branch information
Mrxuba and GreatV authored Jun 9, 2024
1 parent 86a447d commit 335c5c2
Show file tree
Hide file tree
Showing 3 changed files with 448 additions and 35 deletions.
36 changes: 20 additions & 16 deletions deploy/paddle2onnx/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ Need to prepare PaddleOCR, Paddle2ONNX model conversion environment, and ONNXRun

### PaddleOCR

Clone the PaddleOCR repository, use the release/2.6 branch, and install it.
Clone the PaddleOCR repository, use the main branch, and install it.

```
git clone -b release/2.6 https://github.com/PaddlePaddle/PaddleOCR.git
cd PaddleOCR && python3.7 setup.py install
git clone -b main https://github.com/PaddlePaddle/PaddleOCR.git
cd PaddleOCR && python3 pip install -e .
```

### Paddle2ONNX
Expand All @@ -23,13 +23,12 @@ For more details, please refer to [Paddle2ONNX](https://github.com/PaddlePaddle/

- install Paddle2ONNX
```
python3.7 -m pip install paddle2onnx
python3 -m pip install paddle2onnx
```

- install ONNXRuntime
```
# It is recommended to install version 1.9.0, and the version number can be changed according to the environment
python3.7 -m pip install onnxruntime==1.9.0
python3 -m pip install onnxruntime
```

## 2. Model conversion
Expand Down Expand Up @@ -62,32 +61,37 @@ paddle2onnx --model_dir ./inference/en_PP-OCRv3_det_infer \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--save_file ./inference/det_onnx/model.onnx \
--opset_version 10 \
--input_shape_dict="{'x':[-1,3,-1,-1]}" \
--opset_version 11 \
--enable_onnx_checker True
paddle2onnx --model_dir ./inference/en_PP-OCRv3_rec_infer \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--save_file ./inference/rec_onnx/model.onnx \
--opset_version 10 \
--input_shape_dict="{'x':[-1,3,-1,-1]}" \
--opset_version 11 \
--enable_onnx_checker True
paddle2onnx --model_dir ./inference/ch_ppocr_mobile_v2.0_cls_infer \
--model_filename ch_ppocr_mobile_v2.0_cls_infer/inference.pdmodel \
--params_filename ch_ppocr_mobile_v2.0_cls_infer/inference.pdiparams \
--save_file ./inferencecls_onnx/model.onnx \
--opset_version 10 \
--input_shape_dict="{'x':[-1,3,-1,-1]}" \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--save_file ./inference/cls_onnx/model.onnx \
--opset_version 11 \
--enable_onnx_checker True
```
After execution, the ONNX model will be saved in `./inference/det_onnx/`, `./inference/rec_onnx/`, `./inference/cls_onnx/` paths respectively

* Note: For the OCR model, the conversion process must be in the form of dynamic shape, that is, add the option --input_shape_dict="{'x': [-1, 3, -1, -1]}", otherwise the prediction result may be the same as Predicting directly with Paddle is slightly different.
* Note: For the OCR model, the conversion process must be in the form of dynamic shape, otherwise the prediction result may be the same as Predicting directly with Paddle is slightly different.
In addition, the following models do not currently support conversion to ONNX models:
NRTR, SAR, RARE, SRN

* Note: The current Paddle2ONNX version (v1.2.3) now supports dynamic shapes by default, i.e., float32[p2o.DynamicDimension.0,3,p2o.DynamicDimension.1,p2o.DynamicDimension.2]. The `--input_shape_dict` option has been deprecated. If you need to adjust the shape, you can use the following command to adjust the input shape of the Paddle model.

```
python3 -m paddle2onnx.optimize --input_model inference/det_onnx/model.onnx \
--output_model inference/det_onnx/model.onnx \
--input_shape_dict "{'x': [-1,3,-1,-1]}"
```

## 3. prediction

Take the English OCR model as an example, use **ONNXRuntime** to predict and execute the following commands:
Expand Down
42 changes: 23 additions & 19 deletions deploy/paddle2onnx/readme_ch.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,26 @@

### PaddleOCR

克隆PaddleOCR的仓库,使用release/2.4分支,并进行安装,由于PaddleOCR仓库比较大,git clone速度比较慢,所以本教程已下载
克隆PaddleOCR的仓库,使用 main 分支,并进行安装,由于 PaddleOCR 仓库比较大,git clone 速度比较慢,所以本教程已下载

```
git clone -b release/2.4 https://github.com/PaddlePaddle/PaddleOCR.git
cd PaddleOCR && python3.7 setup.py install
git clone -b main https://github.com/PaddlePaddle/PaddleOCR.git
cd PaddleOCR && python3 -m pip install -e .
```

### Paddle2ONNX

Paddle2ONNX 支持将 PaddlePaddle 模型格式转化到 ONNX 模型格式,算子目前稳定支持导出 ONNX Opset 9~11,部分Paddle算子支持更低的ONNX Opset转换。
Paddle2ONNX 支持将 PaddlePaddle 模型格式转化到 ONNX 模型格式,算子目前稳定支持导出 ONNX Opset 9~18,部分Paddle算子支持更低的ONNX Opset转换。
更多细节可参考 [Paddle2ONNX](https://github.com/PaddlePaddle/Paddle2ONNX/blob/develop/README_zh.md)

- 安装 Paddle2ONNX
```
python3.7 -m pip install paddle2onnx
python3 -m pip install paddle2onnx
```

- 安装 ONNXRuntime
```
# 建议安装 1.9.0 版本,可根据环境更换版本号
python3.7 -m pip install onnxruntime==1.9.0
python3 -m pip install onnxruntime
```

## 2. 模型转换
Expand Down Expand Up @@ -61,39 +60,44 @@ paddle2onnx --model_dir ./inference/ch_PP-OCRv3_det_infer \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--save_file ./inference/det_onnx/model.onnx \
--opset_version 10 \
--input_shape_dict="{'x':[-1,3,-1,-1]}" \
--opset_version 11 \
--enable_onnx_checker True
paddle2onnx --model_dir ./inference/ch_PP-OCRv3_rec_infer \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--save_file ./inference/rec_onnx/model.onnx \
--opset_version 10 \
--input_shape_dict="{'x':[-1,3,-1,-1]}" \
--opset_version 11 \
--enable_onnx_checker True
paddle2onnx --model_dir ./inference/ch_ppocr_mobile_v2.0_cls_infer \
--model_filename ch_ppocr_mobile_v2.0_cls_infer/inference.pdmodel \
--params_filename ch_ppocr_mobile_v2.0_cls_infer/inference.pdiparams \
--save_file ./inferencecls_onnx/model.onnx \
--opset_version 10 \
--input_shape_dict="{'x':[-1,3,-1,-1]}" \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--save_file ./inference/cls_onnx/model.onnx \
--opset_version 11 \
--enable_onnx_checker True
```

执行完毕后,ONNX 模型会被分别保存在 `./inference/det_onnx/``./inference/rec_onnx/``./inference/cls_onnx/`路径下

* 注意:对于OCR模型,转化过程中必须采用动态shape的形式,即加入选项--input_shape_dict="{'x': [-1, 3, -1, -1]}",否则预测结果可能与直接使用Paddle预测有细微不同。
* 注意:对于OCR模型,转化过程中必须采用动态shape的形式,否则预测结果可能与直接使用Paddle预测有细微不同。
另外,以下几个模型暂不支持转换为 ONNX 模型:
NRTR、SAR、RARE、SRN

* 注意:[当前Paddle2ONNX版本(v1.2.3)](https://github.com/PaddlePaddle/Paddle2ONNX/releases/tag/v1.2.3)现已默认支持动态shape,即 `float32[p2o.DynamicDimension.0,3,p2o.DynamicDimension.1,p2o.DynamicDimension.2]`,选项 `--input_shape_dict` 已废弃。如果有shape调整需求可使用如下命令进行Paddle模型输入shape调整。

```
python3 -m paddle2onnx.optimize --input_model inference/det_onnx/model.onnx \
--output_model inference/det_onnx/model.onnx \
--input_shape_dict "{'x': [-1,3,-1,-1]}"
```

## 3. 推理预测

以中文OCR模型为例,使用 ONNXRuntime 预测可执行如下命令:

```
python3.7 tools/infer/predict_system.py --use_gpu=False --use_onnx=True \
python3 tools/infer/predict_system.py --use_gpu=False --use_onnx=True \
--det_model_dir=./inference/det_onnx/model.onnx \
--rec_model_dir=./inference/rec_onnx/model.onnx \
--cls_model_dir=./inference/cls_onnx/model.onnx \
Expand All @@ -103,7 +107,7 @@ python3.7 tools/infer/predict_system.py --use_gpu=False --use_onnx=True \
以中文OCR模型为例,使用 Paddle Inference 预测可执行如下命令:

```
python3.7 tools/infer/predict_system.py --use_gpu=False \
python3 tools/infer/predict_system.py --use_gpu=False \
--cls_model_dir=./inference/ch_ppocr_mobile_v2.0_cls_infer \
--rec_model_dir=./inference/ch_PP-OCRv3_rec_infer \
--det_model_dir=./inference/ch_PP-OCRv3_det_infer \
Expand Down
Loading

0 comments on commit 335c5c2

Please sign in to comment.