Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
X2Paddle
提交
b589d4be
X
X2Paddle
项目概览
PaddlePaddle
/
X2Paddle
大约 1 年 前同步成功
通知
328
Star
698
Fork
167
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
26
列表
看板
标记
里程碑
合并请求
4
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
X
X2Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
26
Issue
26
列表
看板
标记
里程碑
合并请求
4
合并请求
4
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
b589d4be
编写于
7月 07, 2020
作者:
C
Channingss
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support set onnx op_set
上级
0489f6d5
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
30 addition
and
52 deletion
+30
-52
x2paddle/convert.py
x2paddle/convert.py
+9
-3
x2paddle/op_mapper/paddle_op_mapper.py
x2paddle/op_mapper/paddle_op_mapper.py
+21
-49
未找到文件。
x2paddle/convert.py
浏览文件 @
b589d4be
...
...
@@ -75,6 +75,12 @@ def arg_parser():
action
=
"store_true"
,
default
=
False
,
help
=
"define input shape for tf model"
)
parser
.
add_argument
(
"--onnx_opset"
,
"-oo"
,
type
=
int
,
default
=
10
,
help
=
"when paddle2onnx set onnx opset version to export"
)
parser
.
add_argument
(
"--params_merge"
,
"-pm"
,
...
...
@@ -186,12 +192,12 @@ def onnx2paddle(model_path, save_dir, params_merge=False):
print
(
"Paddle model and code generated."
)
def
paddle2onnx
(
model_path
,
save_dir
):
def
paddle2onnx
(
model_path
,
save_dir
,
opset
):
from
x2paddle.decoder.paddle_decoder
import
PaddleDecoder
from
x2paddle.op_mapper.paddle_op_mapper
import
PaddleOpMapper
model
=
PaddleDecoder
(
model_path
,
'__model__'
,
'__params__'
)
mapper
=
PaddleOpMapper
()
mapper
.
convert
(
model
.
program
,
save_dir
)
mapper
.
convert
(
model
.
program
,
save_dir
,
opset
)
def
main
():
...
...
@@ -258,7 +264,7 @@ def main():
elif
args
.
framework
==
"paddle2onnx"
:
assert
args
.
model
is
not
None
,
"--model should be defined while translating paddle model to onnx"
paddle2onnx
(
args
.
model
,
args
.
save_dir
)
paddle2onnx
(
args
.
model
,
args
.
save_dir
,
args
.
onnx_opset
)
else
:
raise
Exception
(
...
...
x2paddle/op_mapper/paddle_op_mapper.py
浏览文件 @
b589d4be
...
...
@@ -37,7 +37,7 @@ class PaddleOpMapper(object):
self
.
name_counter
=
dict
()
def
convert
(
self
,
program
,
save_dir
):
def
convert
(
self
,
program
,
save_dir
,
opset
=
10
):
weight_nodes
=
self
.
convert_weights
(
program
)
op_nodes
=
list
()
input_nodes
=
list
()
...
...
@@ -81,7 +81,9 @@ class PaddleOpMapper(object):
initializer
=
[],
inputs
=
input_nodes
,
outputs
=
output_nodes
)
model
=
helper
.
make_model
(
graph
,
producer_name
=
'X2Paddle'
)
opset_imports
=
[
helper
.
make_opsetid
(
""
,
opset
)]
model
=
helper
.
make_model
(
graph
,
producer_name
=
'X2Paddle'
,
opset_imports
=
opset_imports
)
onnx
.
checker
.
check_model
(
model
)
if
not
os
.
path
.
isdir
(
save_dir
):
...
...
@@ -553,13 +555,6 @@ class PaddleOpMapper(object):
def
bilinear_interp
(
self
,
op
,
block
):
input_names
=
op
.
input_names
input_shape
=
block
.
vars
[
op
.
input
(
'X'
)[
0
]].
shape
batch_size
=
input_shape
[
0
]
channels
=
input_shape
[
1
]
height
=
input_shape
[
2
]
width
=
input_shape
[
3
]
coordinate_transformation_mode
=
'half_pixel'
if
op
.
attr
(
'align_corners'
):
coordinate_transformation_mode
=
'align_corners'
if
(
'OutSize'
in
input_names
and
len
(
op
.
input
(
'OutSize'
))
>
0
)
or
(
'SizeTensor'
in
input_names
and
len
(
op
.
input
(
'SizeTensor'
))
>
0
):
...
...
@@ -602,50 +597,31 @@ class PaddleOpMapper(object):
outputs
=
[
cast_shape_name
],
to
=
onnx_pb
.
TensorProto
.
INT64
)
node_list
.
extend
([
concat_shape_node
,
cast_shape_node
])
shape_name
3
=
self
.
get_name
(
op
.
type
,
"shape.concat"
)
shape_node
3
=
helper
.
make_node
(
shape_name
2
=
self
.
get_name
(
op
.
type
,
"shape.concat"
)
shape_node
2
=
helper
.
make_node
(
'Concat'
,
inputs
=
[
shape_name1
,
cast_shape_name
],
outputs
=
[
shape_name
3
],
outputs
=
[
shape_name
2
],
axis
=
0
)
node_list
.
append
(
shape_node3
)
name_h_w
=
op
.
output
(
'Out'
)[
0
]
+
"@h_w"
node_h_w
=
helper
.
make_node
(
'Constant'
,
inputs
=
[],
outputs
=
[
name_h_w
],
value
=
helper
.
make_tensor
(
name
=
name_h_w
,
data_type
=
onnx_pb
.
TensorProto
.
INT64
,
dims
=
[
2
],
vals
=
[
height
,
width
],
raw
=
False
))
node_list
.
append
(
node_h_w
)
shape_name4
=
op
.
output
(
'Out'
)[
0
]
+
"@shape.concat4"
shape_node4
=
helper
.
make_node
(
'Concat'
,
inputs
=
[
shape_name1
,
name_h_w
],
outputs
=
[
shape_name4
],
axis
=
0
)
node_list
.
append
(
shape_node4
)
cast_shape_name3
=
self
.
get_name
(
op
.
type
,
"shape.cast"
)
cast_shape_node3
=
helper
.
make_node
(
node_list
.
append
(
shape_node2
)
cast_shape_name2
=
self
.
get_name
(
op
.
type
,
"shape.cast"
)
cast_shape_node2
=
helper
.
make_node
(
'Cast'
,
inputs
=
[
shape_name
3
],
outputs
=
[
cast_shape_name
3
],
inputs
=
[
shape_name
2
],
outputs
=
[
cast_shape_name
2
],
to
=
onnx_pb
.
TensorProto
.
FLOAT
)
node_list
.
append
(
cast_shape_node
3
)
cast_shape_name
4
=
self
.
get_name
(
op
.
type
,
"shape.cast"
)
cast_shape_node
4
=
helper
.
make_node
(
node_list
.
append
(
cast_shape_node
2
)
cast_shape_name
0
=
self
.
get_name
(
op
.
type
,
"shape.cast"
)
cast_shape_node
0
=
helper
.
make_node
(
'Cast'
,
inputs
=
[
shape_name
4
],
outputs
=
[
cast_shape_name
4
],
inputs
=
[
shape_name
0
],
outputs
=
[
cast_shape_name
0
],
to
=
onnx_pb
.
TensorProto
.
FLOAT
)
node_list
.
append
(
cast_shape_node
4
)
node_list
.
append
(
cast_shape_node
0
)
outputs_h_w_scales
=
op
.
output
(
'Out'
)[
0
]
+
"@out_hw_scales"
node_h_w_scales
=
helper
.
make_node
(
'Div'
,
inputs
=
[
cast_shape_name
3
,
cast_shape_name4
],
inputs
=
[
cast_shape_name
2
,
cast_shape_name0
],
outputs
=
[
outputs_h_w_scales
])
node_list
.
append
(
node_h_w_scales
)
result_node
=
helper
.
make_node
(
...
...
@@ -660,8 +636,7 @@ class PaddleOpMapper(object):
'Resize'
,
inputs
=
[
op
.
input
(
'X'
)[
0
],
op
.
input
(
'Scale'
)[
0
]],
outputs
=
op
.
output
(
'Out'
),
mode
=
'linear'
,
coordinate_transformation_mode
=
coordinate_transformation_mode
)
mode
=
'linear'
)
else
:
out_shape
=
[
op
.
attr
(
'out_h'
),
op
.
attr
(
'out_w'
)]
scale
=
op
.
attr
(
'scale'
)
...
...
@@ -674,7 +649,7 @@ class PaddleOpMapper(object):
'Resize'
,
inputs
=
[
op
.
input
(
'X'
)[
0
],
scale_name
],
outputs
=
op
.
output
(
'Out'
),
mode
=
'
nearest
'
)
mode
=
'
linear
'
)
return
[
scale_node
,
node
]
else
:
raise
Exception
(
"Unexpected situation happend"
)
...
...
@@ -682,9 +657,6 @@ class PaddleOpMapper(object):
def
nearest_interp
(
self
,
op
,
block
):
input_names
=
op
.
input_names
coordinate_transformation_mode
=
'half_pixel'
if
op
.
attr
(
'align_corners'
):
coordinate_transformation_mode
=
'align_corners'
if
'OutSize'
in
input_names
and
len
(
op
.
input
(
'OutSize'
))
>
0
:
node
=
helper
.
make_node
(
'Resize'
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录