1 Star 0 Fork 5

xyqgcs/Paddle2ONNX

forked from PaddlePaddle/Paddle2ONNX 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

Paddle2ONNX

简体中文 | English

简介

paddle2onnx支持将PaddlePaddle模型格式转化到ONNX模型格式。

  • 模型格式,支持Paddle静态图和动态图模型转为ONNX,可转换由save_inference_model导出的静态图模型,使用方法请参考IPthon示例。动态图转换目前处于实验状态,将伴随Paddle 2.0正式版发布后,提供详细使用教程。
  • 算子支持,目前稳定支持导出ONNX Opset 9~11,部分Paddle算子支持更低的ONNX Opset转换,详情可参考算子列表
  • 模型类型,官方测试可转换的模型请参考模型库

AIStudio入门教程

环境依赖

用户环境配置

 python >= 2.7  
 静态图: paddlepaddle >= 1.8.0
 动态图: paddlepaddle >= 2.0.0
 onnx == 1.7.0 | 可选

安装

安装方式1

 pip install paddle2onnx

安装方式2

 git clone https://github.com/PaddlePaddle/paddle2onnx.git
 python setup.py install

使用方式

静态图模型导出

命令行

Paddle模型的参数保存为多个文件(not combined):

paddle2onnx --model_dir paddle_model  --save_file onnx_file --opset_version 10 --enable_onnx_checker True

Paddle模型的参数保存在一个单独的二进制文件中(combined):

paddle2onnx --model_dir paddle_model  --model_filename model_filename --params_filename params_filename --save_file onnx_file --opset_version 10 --enable_onnx_checker True

参数选项

参数 参数说明
--model_dir 配置包含Paddle模型的路径, 由paddle.fluid.io.save_inference_model保存得到
--model_filename [可选] 配置位于--model_dir下存储网络结构的文件名称。当且仅当所有模型参数被保存在一个单独的二进制文件中,它才需要被指定。默认为None
--params_filename [可选] 配置位于--model_dir下存储模型参数的文件名称。当且仅当所有模型参数被保存在一个单独的二进制文件中,它才需要被指定。默认为None
--save_file 指定转换后的模型保存目录路径
--opset_version [可选] 配置转换为ONNX的OpSet版本,目前比较稳定地支持9、10、11三个版本,默认为9
--enable_onnx_checker [可选] 配置是否检查导出为ONNX模型的正确性, 建议打开此开关。若指定为True,需要安装 onnx>=1.7.0, 默认为False
--version [可选] 查看paddle2onnx版本
  • PaddlePaddle模型的两种存储形式:
    • 参数被保存在一个单独的二进制文件中(combined),需要在指定--model_dir的前提下,指定--model_filename, --params_filename, 分别表示--model_dir目录下的网络文件名称和参数文件名称。
    • 参数被保存为多个文件(not combined),只需要指定--model_dir,该目录下面需要包含了'__model__',以及多个参数文件。

IPython教程

动态图模型导出

import paddle
from paddle import nn
from paddle.static import InputSpec
import paddle2onnx as p2o

class LinearNet(nn.Layer):
    def __init__(self):
        super(LinearNet, self).__init__()
        self._linear = nn.Linear(784, 10)

    def forward(self, x):
        return self._linear(x)

layer = LinearNet()

# configure model inputs
x_spec = InputSpec([None, 784], 'float32', 'x')

# convert model to inference mode
layer.eval()

save_path = 'onnx.save/linear_net'
p2o.dygraph2onnx(layer, save_path + '.onnx', input_spec=[x_spec])

# when you paddlepaddle>2.0.0, you can try:
# paddle.onnx.export(layer, save_path, input_spec=[x_spec])

IPython教程

相关文档

License

Provided under the Apache-2.0 license.

from __future__ import absolute_import from __future__ import print_function from __future__ import unicode_literals import argparse import io, re import sys, os import subprocess import platform COPYRIGHT = ''' Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ''' LANG_COMMENT_MARK = None NEW_LINE_MARK = None COPYRIGHT_HEADER = None if platform.system() == "Windows": NEW_LINE_MARK = "\r\n" else: NEW_LINE_MARK = '\n' COPYRIGHT_HEADER = COPYRIGHT.split(NEW_LINE_MARK)[1] p = re.search('(\d{4})', COPYRIGHT_HEADER).group(0) process = subprocess.Popen(["date", "+%Y"], stdout=subprocess.PIPE) date, err = process.communicate() date = date.decode("utf-8").rstrip("\n") COPYRIGHT_HEADER = COPYRIGHT_HEADER.replace(p, date) def generate_copyright(template, lang='C'): if lang == 'Python': LANG_COMMENT_MARK = '#' else: LANG_COMMENT_MARK = "//" lines = template.split(NEW_LINE_MARK) BLANK = " " ans = LANG_COMMENT_MARK + BLANK + COPYRIGHT_HEADER + NEW_LINE_MARK for lino, line in enumerate(lines): if lino == 0 or lino == 1 or lino == len(lines) - 1: continue if len(line) == 0: BLANK = "" else: BLANK = " " ans += LANG_COMMENT_MARK + BLANK + line + NEW_LINE_MARK return ans + "\n" def lang_type(filename): if filename.endswith(".py"): return "Python" elif filename.endswith(".h"): return "C" elif filename.endswith(".c"): return "C" elif filename.endswith(".hpp"): return "C" elif filename.endswith(".cc"): return "C" elif filename.endswith(".cpp"): return "C" elif filename.endswith(".cu"): return "C" elif filename.endswith(".cuh"): return "C" elif filename.endswith(".go"): return "C" elif filename.endswith(".proto"): return "C" else: print("Unsupported filetype %s", filename) exit(0) PYTHON_ENCODE = re.compile("^[ \t\v]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)") def main(argv=None): parser = argparse.ArgumentParser( description='Checker for copyright declaration.') parser.add_argument('filenames', nargs='*', help='Filenames to check') args = parser.parse_args(argv) retv = 0 for filename in args.filenames: fd = io.open(filename, encoding="utf-8") first_line = fd.readline() second_line = fd.readline() if "COPYRIGHT (C)" in first_line.upper(): continue if first_line.startswith("#!") or PYTHON_ENCODE.match( second_line) != None or PYTHON_ENCODE.match(first_line) != None: continue original_contents = io.open(filename, encoding="utf-8").read() new_contents = generate_copyright( COPYRIGHT, lang_type(filename)) + original_contents print('Auto Insert Copyright Header {}'.format(filename)) retv = 1 with io.open(filename, 'w') as output_file: output_file.write(new_contents) return retv if __name__ == '__main__': exit(main())

简介

paddle2onnx支持将PaddlePaddle模型格式转化到ONNX模型格式 展开 收起
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xyqgcs/Paddle2ONNX.git
git@gitee.com:xyqgcs/Paddle2ONNX.git
xyqgcs
Paddle2ONNX
Paddle2ONNX
develop

搜索帮助

Cb406eda 1850385 E526c682 1850385