转换 TensorFlow 模型需装 tensorflow==1.14,与新版 TF 不兼容;转换 ONNX/PyTorch 需分别安装 onnx>=1.6.0、torch>=1.5.0;转 Paddle-Lite 格式需额外安装 paddlelite>=2.9.0 并加 --to_lite 参数。
X2Paddle
PaddlePaddleApache-2.0长期未更新把 TensorFlow、PyTorch、Caffe、ONNX 等模型一键转成飞桨格式,省去跨框架迁移时重写网络和权重的麻烦。
Deep learning model converter for PaddlePaddle. (『飞桨』深度学习模型转换工具)
pip install x2paddleREADME 未说明显存或 GPU 型号要求;转换产物可用 PaddleInference/PaddleLite 部署到 CPU/GPU/Arm 等设备。
把 Caffe、TensorFlow、ONNX、PyTorch 模型转成飞桨格式的官方转换工具
把 TensorFlow、PyTorch、Caffe、ONNX 等模型一键转成飞桨格式,省去跨框架迁移时重写网络和权重的麻烦。
核心能力
- 一条命令或一个 API 完成推理模型转换
- 支持 Caffe/TensorFlow/ONNX/PyTorch 四大框架
- 可将 PyTorch 训练项目代码一键迁移为飞桨代码
- 转换结果可用 PaddleInference/PaddleLite 部署到 CPU/GPU/Arm
- 提供 PyTorch 与飞桨的 API 对比文档降低迁移成本
容易踩的坑
- v1.6.0X2Paddle v1.6.0最新正式版2024-12-13 · 2024-12-13 02:47 UTC
Release Note 1.6.0
Adapt
X2Paddleto PaddlePaddle 3.0, and the test cases oftest_benchmarkcan be verified under:- python == 3.9
- paddlepaddle == 3.0.0beta1
- tensorflow == 2.16.1
- onnx == 1.17.0
- torch == 2.4.1
What's Changed
- [Bug] Fixed onnx
Padop with length-1. by @megemini in #1058 - [Bug] Fixed version check for PaddlePaddle. by @megemini in #1064
- [Bug] Fixed pytorch op mapper
aten_linear. by @megemini in #1079 - [Bug] Fixed onnx unknow dimension. by @megemini in #1082
- [Bug] Fixed the usage of
easyocr.Readerfor1.7.2. by @megemini in #1084 - [Bug] Fixed tensorflow manually updates shapes. by @megemini in #1089
- Update
Dockerfile. by @megemini in #1061 @luotao1 in #1105 - Update
LOD_TENSORtoDENSE_TENSOR. by @co63oc in #1095 - Update
x2paddleusage ofpaddle.fluidfor PaddlePaddle 3.0 by @megemini in #1087 - Update
x2paddleusage ofpaddle.jit.to_staticwithASTmode for PaddlePaddle 3.0. by @megemini in #1093 - Update
test_benchmarkcompatiblity for PaddlePaddle 3.0 by @megemini- Remove
paddle.fluidand update the usage ofpaddle.static.load_inference_modelin #1058 #1065 #1067 #1068 #1069 #1070 #1071 #1073 #1075 #1076 #1078 #1079 #1080 #1082 #1083 #1084 #1089 #1091 - Update onnx
one_hottype promotion compatiblity for PaddlePaddle 3.0. by @megemini in #1082 - Update onnx
nms3api compatiblity for PaddlePaddle 3.0. by @megemini in #1082 - Update onnx
roi_alignapi compatiblity for PaddlePaddle 3.0. by @megemini in #1082
- Remove
- Update
docsfor PaddlePaddle 3.0 by @megemini in #1088 #1098 #1101 #1103 - Add new models in
test_benchmarkby @megemini- pytorch
GPT2#1085 - pytorch
MT5_small#1086 - pytorch
YOLOv5#1099
- pytorch
- Add pytorch
aten::padand fix relatedtest_benchmarktest cases. by @megemini in #1073 - Add pytorch
aten::listand fix relatedtest_benchmarktest cases. by @megemini in #1075 - Add pytorch
aten::aten_linalg_vector_normand fix relatedtest_benchmarktest cases. by @megemini in #1076 - Add pytorch
aten::scaled_dot_product_attentionand fix relatedtest_benchmarktest cases. by @megemini in #1078 - Add pytorch
aten::unsafe_chunkand fix relatedtest_benchmarktest cases. by @megemini in #1080 - Add
black.listintest_benchmarkto control CI. by @megemini in #1061 - Remove Python 2.0 support for
raw_input. by @megemini in #1083
- v1.5.0X2Paddle v1.5.02024-08-07 · 2024-08-07 10:22 UTC
查看更新说明
What's Changed
- [Bug]Fixed onnx op_mapper for PNC model. by @wjj19950828 in #962
- [Bug]Fixed some typo. by @co63oc in #976 #977 #978
- [Bug]Fixed pytorch aten::squeeze for CHAN-DST model. by @wjj19950828 in #815
- Add onnx cast op in the LN pattern. by @wjj19950828 in #822
- Add pytorch aten::_convolution_mode. by @wjj19950828 @luotao1 in #1041
- Add pytorch aten::amax and aten::topk by @Ken-reddevil in #1019
- update CI Checks by @luotao (Release 1.5.0)
- Add 100+ test_benchmark demos (Caffe/TensorFlow/ONNX/Pytorch). in #1014 #1020 #1023 #1025 #1026 #1027 #1028 #1029 #1030 #1031 #1032 #1033 #1034
- Update test_autoscan unit-tests (Pytorch/ONNX). in #1035 #1036
- update CodeStyle checks. in #1046 #1047
- update Dockerfile. in #1022
New Contributors
- @co63oc made their first contribution in #976
- @luotao1 made their first contribution in #1014
- @Ken-reddevil made their first contribution in #1019
- v1.4.1X2Paddle v1.4.12023-03-31 · 2023-03-31 09:32 UTC
查看更新说明
What's Changed
- [Bug]Fixed ONNXDecoder by @wjj19950828 in https://github.com/PaddlePaddle/X2Paddle/pull/939
- Add ONNX2Paddle for visualdl by @rainyfly in https://github.com/PaddlePaddle/X2Paddle/pull/954
- [Bug]Support YOLOv8-cls by @wjj19950828 in https://github.com/PaddlePaddle/X2Paddle/pull/952
New Contributors
- @rainyfly made their first contribution in https://github.com/PaddlePaddle/X2Paddle/pull/954
- v1.4.0X2Paddle v1.4.02023-01-18 · 2023-01-18 03:59 UTC
查看更新说明
What's Changed
- Add input_shape_dict and rm raw_input by @wjj19950828 in https://github.com/PaddlePaddle/X2Paddle/pull/899
- Fixed ToPILImage && rm SymbolicShapeInference by @wjj19950828 in https://github.com/PaddlePaddle/X2Paddle/pull/903
- Support GFPGAN by @wjj19950828 in https://github.com/PaddlePaddle/X2Paddle/pull/906
- Add FastDeploy YOLOv5/v6/v7 demo in readme by @wjj19950828 in https://github.com/PaddlePaddle/X2Paddle/pull/911
- Updated torch.nn.utils.clip_grad_value_.md by @PommesPeter in https://github.com/PaddlePaddle/X2Paddle/pull/918
- [Bug] Fixed ONNX Gemm bug by @wjj19950828 in https://github.com/PaddlePaddle/X2Paddle/pull/917
- [Bug] Fixed typo error by @wjj19950828 in https://github.com/PaddlePaddle/X2Paddle/pull/920
- Support fmod=1 in Mod OP by @wjj19950828 in https://github.com/PaddlePaddle/X2Paddle/pull/921
- Fixed NonZero bug by @wjj19950828 in https://github.com/PaddlePaddle/X2Paddle/pull/922
- add optim by @AndPuQing in https://github.com/PaddlePaddle/X2Paddle/pull/927
- Support YOLOv8 for dynamic shape by @wjj19950828 in https://github.com/PaddlePaddle/X2Paddle/pull/931
New Contributors
- @PommesPeter made their first contribution in https://github.com/PaddlePaddle/X2Paddle/pull/918
- @AndPuQing made their first contribution in https://github.com/PaddlePaddle/X2Paddle/pull/927
- v1.3.9X2Paddle v1.3.92022-09-19 · 2022-09-19 03:00 UTC
查看更新说明
What's Changed
- Support 7-15 ONNX opset version by @wjj19950828 in https://github.com/PaddlePaddle/X2Paddle/pull/838
- Modify opset7~15 inheritance relationship by @wjj19950828 in https://github.com/PaddlePaddle/X2Paddle/pull/843
- Add autoscan test by @wjj19950828 in https://github.com/PaddlePaddle/X2Paddle/pull/836
- Add autoscan test for Elementwise ops by @wjj19950828 in https://github.com/PaddlePaddle/X2Paddle/pull/854
- Add_test_abs by @qqj1130247885 in https://github.com/PaddlePaddle/X2Paddle/pull/859
- Add logical and compare ops by @wjj19950828 in https://github.com/PaddlePaddle/X2Paddle/pull/855
- Fixed torch pad by @wjj19950828 in https://github.com/PaddlePaddle/X2Paddle/pull/863
- Solve the overload problem by @wjj19950828 in https://github.com/PaddlePaddle/X2Paddle/pull/865
- Add AveragePool tests by @wjj19950828 in https://github.com/PaddlePaddle/X2Paddle/pull/872
- Add Reduce_ops and HardSigmoid by @qqj1130247885 in https://github.com/PaddlePaddle/X2Paddle/pull/867
- Add isinf by @qqj1130247885 in https://github.com/PaddlePaddle/X2Paddle/pull/868
- Add IsNaN by @qqj1130247885 in https://github.com/PaddlePaddle/X2Paddle/pull/869
- Add Sum by @qqj1130247885 in https://github.com/PaddlePaddle/X2Paddle/pull/873
- Fixed Readme by @wjj19950828 in https://github.com/PaddlePaddle/X2Paddle/pull/879
- Fixed reduce tests bug by @wjj19950828 in https://github.com/PaddlePaddle/X2Paddle/pull/880
- Set enable_code_optim default to False by @wjj19950828 in https://github.com/PaddlePaddle/X2Paddle/pull/889
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
X2Paddle
简介
X2Paddle是飞桨生态下的模型转换工具,致力于帮助其它深度学习框架用户快速迁移至飞桨框架。目前支持推理模型的框架转换与PyTorch训练代码迁移,我们还提供了详细的不同框架间API对比文档,降低开发者将模型迁移到飞桨的时间成本。
特性
-
支持主流深度学习框架
-
支持的模型丰富
- 在主流的CV和NLP模型上支持大部分模型转换,目前X2Paddle支持130+ PyTorch OP,90+ ONNX OP,90+ TensorFlow OP 以及 30+ Caffe OP,详见 支持列表
-
简洁易用
- 一条命令行或者一个API即可完成模型转换
能力
-
预测模型转换
- 支持Caffe/TensorFlow/ONNX/PyTorch的模型一键转为飞桨的预测模型,并使用PaddleInference/PaddleLite进行CPU/GPU/Arm等设备的部署
-
PyTorch训练项目转换
- 支持PyTorch项目Python代码(包括训练、预测)一键转为基于飞桨框架的项目代码,帮助开发者快速迁移项目,并可享受AIStudio平台对于飞桨框架提供的海量免费计算资源【新功能,试一下!】
-
API对应文档
- 详细的API文档对比分析,帮助开发者快速从PyTorch框架的使用迁移至飞桨框架的使用,大大降低学习成本 【新内容,了解一下!】
安装
环境依赖
- python >= 3.8
- paddlepaddle >= 2.2.2 (官方验证到
3.0.0beta1) - tensorflow == 1.14 (如需转换TensorFlow模型。其中
test_benchmark模型已在2.16.1测试通过) - onnx >= 1.6.0 (如需转换ONNX模型,其中
test_benchmark模型已在1.17.0测试通过) - torch >= 1.5.0 (如需转换PyTorch模型,其中
test_benchmark模型已在2.4.1测试通过) - paddlelite >= 2.9.0 (如需一键转换成Paddle-Lite支持格式,推荐最新版本)
说明:以上测试通过的版本,不代表 X2Paddle 支持相应版本中的所有算子,仅表示可在此环境中使用。具体支持的算子请详见 支持列表
pip安装(推荐)
如需使用稳定版本,可通过pip方式安装X2Paddle:
pip install x2paddle
源码安装
如需体验最新功能,可使用源码安装方式:
git clone https://github.com/PaddlePaddle/X2Paddle.git
cd X2Paddle
git checkout develop
python setup.py install
快速开始
功能一:推理模型转换
PyTorch模型转换
from x2paddle.convert import pytorch2paddle
pytorch2paddle(module=torch_module,
save_dir="./pd_model",
jit_type="trace",
input_examples=[torch_input])
# module (torch.nn.Module): PyTorch的Module。
# save_dir (str): 转换后模型的保存路径。
# jit_type (str): 转换方式。默认为"trace"。
# input_examples (list[torch.tensor]): torch.nn.Module的输入示例,list的长度必须与输入的长度一致。默认为None。
script模式以及更多细节可参考PyTorch模型转换文档。
TensorFlow模型转换
x2paddle --framework=tensorflow --model=tf_model.pb --save_dir=pd_model
ONNX模型转换
x2paddle --framework=onnx --model=onnx_model.onnx --save_dir=pd_model
Caffe模型转换
x2paddle --framework=caffe --prototxt=deploy.prototxt --weight=deploy.caffemodel --save_dir=pd_model
转换参数说明
| 参数 | 作用 |
|---|---|
| --framework | 源模型类型 (tensorflow、caffe、onnx) |
| --prototxt | 当framework为caffe时,该参数指定caffe模型的proto文件路径 |
| --weight | 当framework为caffe时,该参数指定caffe模型的参数文件路径 |
| --save_dir | 指定转换后的模型保存目录路径 |
| --model | 当framework为tensorflow/onnx时,该参数指定tensorflow的pb模型文件或onnx模型路径 |
| --input_shape_dict | [可选] For ONNX, 定义ONNX模型输入大小 |
| --caffe_proto | [可选] 由caffe.proto编译成caffe_pb2.py文件的存放路径,当存在自定义Layer时使用,默认为None |
| --define_input_shape | [可选] For TensorFlow, 当指定该参数时,强制用户输入每个Placeholder的shape,见文档Q2 |
| --enable_code_optim | [可选] For PyTorch, 是否对生成代码进行优化,默认为False |
| --to_lite | [可选] 是否使用opt工具转成Paddle-Lite支持格式,默认为False |
| --lite_valid_places | [可选] 指定转换类型,可以同时指定多个backend(以逗号分隔),opt将会自动选择最佳方式,默认为arm |
| --lite_model_type | [可选] 指定模型转化类型,目前支持两种类型:protobuf和naive_buffer,默认为naive_buffer |
| --disable_feedback | [可选] 是否关闭X2Paddle使用反馈;X2Paddle默认会统计用户在进行模型转换时的成功率,以及转换框架来源等信息,以便于帮忙X2Paddle根据用户需求进行迭代,不会上传用户的模型文件。如若不想参与反馈,可指定此参数为False即可 |
X2Paddle API
目前X2Paddle提供API方式转换模型,可参考X2PaddleAPI
一键转换Paddle-Lite支持格式
可参考使用X2paddle导出Paddle-Lite支持格式
功能二:PyTorch模型训练迁移
项目转换包括3个步骤
- 项目代码预处理
- 代码/预训练模型一键转换
- 转换后代码后处理
使用VisualDL进行模型转换
飞桨可视化工具VisualDL已经将模型转换工具部署在官网提供服务,可以点击服务链接进行在线的ONNX2Paddle模型转换。

使用教程
:hugs:贡献代码:hugs:
我们非常欢迎您为X2Paddle贡献代码或者提供使用建议:
- 如果您可以修复某个issue或者增加一个新功能,欢迎给我们提交Pull Requests
- 可以使用开发镜像:paddlepaddle/x2paddle:latest-dev-cuda11.8-cudnn8.6-trt8.5-gcc82
- 如果有PyTorch训练项目转换需求欢迎随时提issue