EXPORT_ONNX_MODEL_en.md 4.4 KB
Newer Older
qq_30618961's avatar
qq_30618961 已提交
1 2 3 4 5 6
# PaddleDetection Model Export as ONNX Format Tutorial

PaddleDetection Model support is saved in ONNX format and the list of current test support is as follows
| Model  | OP Version | NOTE |
| :---- | :----- | :--- |
| YOLOv3 |  11   |  Only batch=1 inferring is supported. Model export needs fixed shape |
W
wangguanzhong 已提交
7 8 9 10 11
| PP-YOLO | 11 | Only batch=1 inferring is supported. A MatrixNMS will be converted to an NMS with slightly different precision; Model export needs fixed shape |
| PP-YOLOv2 | 11 | Only batch=1 inferring is supported. MatrixNMS will be converted to NMS with slightly different precision; Model export needs fixed shape |
| PP-YOLO Tiny | 11 | Only batch=1 inferring is supported. Model export needs fixed shape |
| PP-YOLOE | 11 | Only batch=1 inferring is supported. Model export needs fixed shape |
| PP-PicoDet | 11 | Only batch=1 inferring is supported. Model export needs fixed shape |
qq_30618961's avatar
qq_30618961 已提交
12 13 14 15
| FCOS | 11 |Only batch=1 inferring is supported |
| PAFNet | 11 |- |
| TTFNet | 11 |-|
| SSD | 11 |Only batch=1 inferring is supported |
W
wangguanzhong 已提交
16 17 18 19 20 21
| PP-TinyPose | 11 | - |
| Faster RCNN | 16 | Only batch=1 inferring is supported, require paddle2onnx>=0.9.7|
| Mask RCNN | 16 | Only batch=1 inferring is supported, require paddle2onnx>=0.9.7|
| Cascade RCNN | 16 | Only batch=1 inferring is supported, require paddle2onnx>=0.9.7|
| Cascade Mask RCNN | 16 | Only batch=1 inferring is supported, require paddle2onnx>=0.9.7|

qq_30618961's avatar
qq_30618961 已提交
22 23 24 25 26 27

The function of saving ONNX is provided by [Paddle2ONNX](https://github.com/PaddlePaddle/Paddle2ONNX). If there is feedback on related problems during conversion, Communicate with engineers in Paddle2ONNX's Github project via [ISSUE](https://github.com/PaddlePaddle/Paddle2ONNX/issues).

## Export Tutorial

### Step 1. Export the Paddle deployment model
W
wangguanzhong 已提交
28 29 30
Export procedure reference document[Tutorial on PaddleDetection deployment model export](./EXPORT_MODEL_en.md), for example:

- Models except RCNN series, take YOLOv3 as example
qq_30618961's avatar
qq_30618961 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
```
cd PaddleDetection
python tools/export_model.py -c configs/yolov3/yolov3_darknet53_270e_coco.yml \
                             -o weights=https://paddledet.bj.bcebos.com/models/yolov3_darknet53_270e_coco.pdparams \
                             TestReader.inputs_def.image_shape=[3,608,608] \
                             --output_dir inference_model
```
The derived models were saved in `inference_model/yolov3_darknet53_270e_coco/`, with the structure as follows
```
yolov3_darknet
  ├── infer_cfg.yml          # Model configuration file information
  ├── model.pdiparams        # Static diagram model parameters
  ├── model.pdiparams.info   # Parameter Information is not required
  └── model.pdmodel          # Static diagram model file
```
> check`TestReader.inputs_def.image_shape`, For YOLO series models, specify this parameter when exporting; otherwise, the conversion fails

W
wangguanzhong 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
- RCNN series models, take Faster RCNN as example

The conditional block needs to be removed in RCNN series when export ONNX model. Add `export_onnx=True` in command line
```
cd PaddleDetection
python tools/export_model.py -c configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.yml \
                             -o weights=https://paddledet.bj.bcebos.com/models/faster_rcnn_r50_fpn_1x_coco.pdparams \
                             export_onnx=True \
                             --output_dir inference_model
```
The derived models were saved in `inference_model/faster_rcnn_r50_fpn_1x_coco/`, with the structure as follows
```
faster_rcnn_r50_fpn_1x_coco
  ├── infer_cfg.yml          # Model configuration file information
  ├── model.pdiparams        # Static diagram model parameters
  ├── model.pdiparams.info   # Parameter Information is not required
  └── model.pdmodel          # Static diagram model file
```


qq_30618961's avatar
qq_30618961 已提交
68
### Step 2. Convert the deployment model to ONNX format
W
wangguanzhong 已提交
69
Install Paddle2ONNX (version 0.9.7 or higher)
qq_30618961's avatar
qq_30618961 已提交
70 71 72 73 74
```
pip install paddle2onnx
```
Use the following command to convert
```
W
wangguanzhong 已提交
75
# YOLOv3
qq_30618961's avatar
qq_30618961 已提交
76 77 78 79 80
paddle2onnx --model_dir inference_model/yolov3_darknet53_270e_coco \
            --model_filename model.pdmodel \
            --params_filename model.pdiparams \
            --opset_version 11 \
            --save_file yolov3.onnx
W
wangguanzhong 已提交
81 82 83 84 85 86 87

# Faster RCNN
paddle2onnx --model_dir inference_model/faster_rcnn_r50_fpn_1x_coco \
            --model_filename model.pdmodel \
            --params_filename model.pdiparams \
            --opset_version 16 \
            --save_file faster_rcnn.onnx
qq_30618961's avatar
qq_30618961 已提交
88
```
W
wangguanzhong 已提交
89
The transformed model is under the current path`yolov3.onnx` and `faster_rcnn.onnx`