Skip to main content

Export PaddlePaddle to ONNX

Project description

Paddle2ONNX

简体中文 | English

1 简介

Paddle2ONNX 支持将 PaddlePaddle 模型格式转化到 ONNX 模型格式。通过 ONNX 可以完成将 Paddle 模型到多种推理引擎的部署,包括 TensorRT/OpenVINO/MNN/TNN/NCNN,以及其它对 ONNX 开源格式进行支持的推理引擎或硬件。

2 环境依赖

  • PaddlePaddle == 2.6.0
  • onnxruntime >= 1.10.0

3 安装

pip install paddle2onnx

开发用户,请按照Github 源码安装方式编译Paddle2ONNX。

4 使用

4.1 获取PaddlePaddle部署模型

Paddle2ONNX 在导出模型时,需要传入部署模型格式,包括两个文件

  • model_name.pdmodel: 表示模型结构
  • model_name.pdiparams: 表示模型参数 [注意] 这里需要注意,两个文件其中参数文件后辍为 .pdiparams,如你的参数文件后辍是 .pdparams ,那说明你的参数是训练过程中保存的,当前还不是部署模型格式。 部署模型的导出可以参照各个模型套件的导出模型文档。

4.2 调整Paddle模型

如果对Paddle模型的输入输出需要做调整,可以前往Paddle 相关工具查看教程。

4.3 命令行转换

使用如下命令将Paddle模型转换为ONNX模型

paddle2onnx --model_dir saved_inference_model \
            --model_filename model.pdmodel \
            --params_filename model.pdiparams \
            --save_file model.onnx

可调整的转换参数如下表:

参数 参数说明
--model_dir 配置包含 Paddle 模型的目录路径
--model_filename [可选] 配置位于 --model_dir 下存储网络结构的文件名
--params_filename [可选] 配置位于 --model_dir 下存储模型参数的文件名称
--save_file 指定转换后的模型保存目录路径
--opset_version [可选] 配置转换为 ONNX 的 OpSet 版本,目前支持 7~16 等多个版本,默认为 9
--enable_onnx_checker [可选] 配置是否检查导出为 ONNX 模型的正确性, 建议打开此开关, 默认为 False
--enable_auto_update_opset [可选] 是否开启 opset version 自动升级功能,当低版本 opset 无法转换时,自动选择更高版本的 opset进行转换, 默认为 True
--deploy_backend [可选] 量化模型部署的推理引擎,支持 onnxruntime、tensorrt 或 others,当选择 others 时,所有的量化信息存储于 max_range.txt 文件中,默认为 onnxruntime
--save_calibration_file [可选] TensorRT 8.X版本部署量化模型需要读取的 cache 文件的保存路径,默认为 calibration.cache
--version [可选] 查看 paddle2onnx 版本
--external_filename [可选] 当导出的 ONNX 模型大于 2G 时,需要设置 external data 的存储路径,推荐设置为:external_data
--export_fp16_model [可选] 是否将导出的 ONNX 的模型转换为 FP16 格式,并用 ONNXRuntime-GPU 加速推理,默认为 False
--custom_ops [可选] 将 Paddle OP 导出为 ONNX 的 Custom OP,例如:--custom_ops '{"paddle_op":"onnx_op"},默认为 {}

4.4 裁剪ONNX

如果你需要调整 ONNX 模型,请参考如下工具:ONNX 相关工具

4.5 优化ONNX

如你对导出的 ONNX 模型有优化的需求,推荐使用 onnx-simplifier,也可使用如下命令对模型进行优化

python -m paddle2onnx.optimize --input_model model.onnx --output_model new_model.onnx

5 License

Provided under the Apache-2.0 license.

6 捐赠

  • 感谢 PaddlePaddle 团队提供服务器支持 Paddle2ONNX 的 CI 建设。
  • 感谢社区用户 chenwhqlluotao1goocodyjeff41404jzhang553ZhengBicheng 于2024年03月28日向 Paddle2ONNX PMC 捐赠共 10000 元人名币用于 Paddle2ONNX 的发展。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

paddle2onnx-1.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.4 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

paddle2onnx-1.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.4 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

paddle2onnx-1.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.4 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

paddle2onnx-1.2.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.4 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

paddle2onnx-1.2.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.4 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

Details for the file paddle2onnx-1.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for paddle2onnx-1.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b5273df8080912da0b5e29e57043475b165979f10e7d2fb699fd401b8c4e9022
MD5 7932267dd96d3fb18d1d4fcf50fc9c02
BLAKE2b-256 d454a0efca142039a95e1974cc4c06c4c44df880a8d78010c11d2e6b41d35553

See more details on using hashes here.

File details

Details for the file paddle2onnx-1.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for paddle2onnx-1.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8a7baec15f5ad0d60a2add9ac420da3edeaa3096931b361ed3806e88761a1bd1
MD5 41dcd5a2ec5b07022a9727a3f497ef72
BLAKE2b-256 9678bb8b1d98bb2f1af67d52e544fb4cfdfa4875e8ef4c953870bf4b90860a21

See more details on using hashes here.

File details

Details for the file paddle2onnx-1.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for paddle2onnx-1.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 20a107a8316dfa389416812c5249f9ee92b934fed9ed222ac94281ad022efdb1
MD5 392cbb0b015b13f5c0795bb74508c117
BLAKE2b-256 2d330365dbf61b261c3be9f7315d2ff62656bee2076559b6458ce9a97fbd285a

See more details on using hashes here.

File details

Details for the file paddle2onnx-1.2.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for paddle2onnx-1.2.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8997b64e12e4c114d455edd45e6298fe67d206de27650829760b21ed8762a12a
MD5 50293410f379b3dee57d8e7ba45d69c4
BLAKE2b-256 c84bbd17bbd51fa2a82e1befa9974473558a7c30fb7fcc40628cec5e1d3c5e55

See more details on using hashes here.

File details

Details for the file paddle2onnx-1.2.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for paddle2onnx-1.2.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4360568a3702b6c397ecebf6088143ccdfd468e2e3336c429230c5254b0c47f3
MD5 24af35fd90b64d7d66d4b6a0ffbdb279
BLAKE2b-256 435a59f580e4bc78fe33e9fbe60c7ca4a78e8b9de0ddb20542e8edf653fb34fb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page