Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
X2Paddle
提交
d20d08b4
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看板
提交
d20d08b4
编写于
11月 08, 2020
作者:
S
SunAhong1993
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix the bug
上级
8aa1008b
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
88 addition
and
44 deletion
+88
-44
x2paddle/core/program.py
x2paddle/core/program.py
+4
-1
x2paddle/op_mapper/dygraph/tf2paddle/tf_op_mapper.py
x2paddle/op_mapper/dygraph/tf2paddle/tf_op_mapper.py
+84
-43
未找到文件。
x2paddle/core/program.py
浏览文件 @
d20d08b4
...
@@ -584,7 +584,10 @@ class PaddleGraph(object):
...
@@ -584,7 +584,10 @@ class PaddleGraph(object):
line
=
','
.
join
(
layer
.
outputs
)
line
=
','
.
join
(
layer
.
outputs
)
line
+=
" = {}("
.
format
(
layer
.
kernel
)
line
+=
" = {}("
.
format
(
layer
.
kernel
)
for
k
,
v
in
layer
.
inputs
.
items
():
for
k
,
v
in
layer
.
inputs
.
items
():
line
+=
"{}={}, "
.
format
(
k
,
v
)
if
isinstance
(
v
,
list
):
line
+=
"{}=[{}], "
.
format
(
k
,
", "
.
join
(
v
))
else
:
line
+=
"{}={}, "
.
format
(
k
,
v
)
for
k
,
v
in
layer
.
attrs
.
items
():
for
k
,
v
in
layer
.
attrs
.
items
():
line
+=
"{}={}, "
.
format
(
k
,
v
)
line
+=
"{}={}, "
.
format
(
k
,
v
)
line
=
line
.
strip
(
", "
)
line
=
line
.
strip
(
", "
)
...
...
x2paddle/op_mapper/dygraph/tf2paddle/tf_op_mapper.py
浏览文件 @
d20d08b4
...
@@ -224,12 +224,20 @@ class TFOpMapper(OpMapper):
...
@@ -224,12 +224,20 @@ class TFOpMapper(OpMapper):
return
return
self
.
params
[
node
.
name
]
=
node
.
value
self
.
params
[
node
.
name
]
=
node
.
value
if
dtype
!=
"float32"
:
self
.
params
[
node
.
name
]
=
node
.
value
.
astype
(
"float32"
)
self
.
paddle_graph
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
"self.create_parameter"
,
"self.create_parameter"
,
inputs
=
{},
inputs
=
{},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
shape
=
shape
,
shape
=
shape
,
attr
=
string
(
node
.
name
))
attr
=
string
(
node
.
name
))
if
dtype
!=
"float32"
:
self
.
paddle_graph
.
add_layer
(
kernel
=
"paddle.cast"
,
inputs
=
{
"x"
:
node
.
name
},
outputs
=
[
node
.
name
],
dtype
=
string
(
dtype
))
def
Transpose
(
self
,
node
):
def
Transpose
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
])
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
])
...
@@ -340,14 +348,14 @@ class TFOpMapper(OpMapper):
...
@@ -340,14 +348,14 @@ class TFOpMapper(OpMapper):
op_name
=
name_generator
(
"pool"
,
self
.
nn_name2id
)
op_name
=
name_generator
(
"pool"
,
self
.
nn_name2id
)
output_name
=
node
.
name
output_name
=
node
.
name
layer_outputs
=
[
op_name
,
output_name
]
layer_outputs
=
[
op_name
,
output_name
]
self
.
paddle_graph
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"paddle.nn.Pool2D"
,
kernel
=
"paddle.nn.
Max
Pool2D"
,
inputs
=
{
"input"
:
input_name
},
inputs
=
{
"input"
:
input_name
},
outputs
=
layer_outputs
,
outputs
=
layer_outputs
,
pool_size
=
k_size
[
2
:
4
],
kernel_size
=
k_size
[
2
:
4
],
pool_type
=
string
(
"max"
),
stride
=
strides
[
2
:
4
],
pool_stride
=
strides
[
2
:
4
],
padding
=
string
(
pad_mode
))
pool_padding
=
string
(
pad_mode
))
if
data_format
==
"NHWC"
:
if
data_format
==
"NHWC"
:
self
.
paddle_graph
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
...
@@ -438,10 +446,7 @@ class TFOpMapper(OpMapper):
...
@@ -438,10 +446,7 @@ class TFOpMapper(OpMapper):
output_name
=
node
.
name
output_name
=
node
.
name
layer_outputs
=
[
op_name
,
output_name
]
layer_outputs
=
[
op_name
,
output_name
]
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
])
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
])
if
data_format
==
"NHWC"
:
n
,
h
,
w
,
c
=
input
.
out_shapes
[
0
]
else
:
n
,
c
,
h
,
w
=
input
.
out_shapes
[
0
]
gamma
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
])
gamma
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
])
beta
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
2
])
beta
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
2
])
moving_mean
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
3
])
moving_mean
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
3
])
...
@@ -453,7 +458,7 @@ class TFOpMapper(OpMapper):
...
@@ -453,7 +458,7 @@ class TFOpMapper(OpMapper):
assert
moving_mean
.
layer_type
==
"Const"
assert
moving_mean
.
layer_type
==
"Const"
assert
moving_var
.
layer_type
==
"Const"
assert
moving_var
.
layer_type
==
"Const"
input_name
=
input
.
name
input_name
=
input
.
name
if
data_format
==
"NHWC"
:
if
data_format
==
"NHWC"
:
transpose_name
=
gen_name
(
"batch_norm"
,
"transpose"
)
transpose_name
=
gen_name
(
"batch_norm"
,
"transpose"
)
self
.
paddle_graph
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
...
@@ -462,17 +467,24 @@ class TFOpMapper(OpMapper):
...
@@ -462,17 +467,24 @@ class TFOpMapper(OpMapper):
outputs
=
[
transpose_name
],
outputs
=
[
transpose_name
],
perm
=
[
0
,
3
,
1
,
2
])
perm
=
[
0
,
3
,
1
,
2
])
input_name
=
transpose_name
input_name
=
transpose_name
n
,
h
,
w
,
c
=
input
.
out_shapes
[
0
]
else
:
n
,
c
,
h
,
w
=
input
.
out_shapes
[
0
]
self
.
params
[
"{}_{}"
.
format
(
node
.
name
,
gamma
.
name
)]
=
self
.
params
[
gamma
.
name
]
self
.
params
[
"{}_{}"
.
format
(
node
.
name
,
beta
.
name
)]
=
self
.
params
[
beta
.
name
]
self
.
params
[
"{}_{}"
.
format
(
node
.
name
,
moving_mean
.
name
)]
=
self
.
params
[
moving_mean
.
name
]
self
.
params
[
"{}_{}"
.
format
(
node
.
name
,
moving_var
.
name
)]
=
self
.
params
[
moving_var
.
name
]
self
.
paddle_graph
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"paddle.nn.BatchNorm"
,
kernel
=
"paddle.nn.BatchNorm"
,
inputs
=
{
"input"
:
input_name
},
inputs
=
{
"input"
:
input_name
},
outputs
=
layer_outputs
,
outputs
=
layer_outputs
,
num_
feature
s
=
c
,
num_
channel
s
=
c
,
epsilon
=
node
.
get_attr
(
"epsilon"
),
epsilon
=
node
.
get_attr
(
"epsilon"
),
weight_attr
=
string
(
gamma
.
name
),
param_attr
=
string
(
"{}_{}"
.
format
(
node
.
name
,
gamma
.
name
)
),
bias_attr
=
string
(
beta
.
name
),
bias_attr
=
string
(
"{}_{}"
.
format
(
node
.
name
,
beta
.
name
)
),
moving_mean_name
=
string
(
moving_mean
.
name
),
moving_mean_name
=
string
(
"{}_{}"
.
format
(
node
.
name
,
moving_mean
.
name
)
),
moving_variance_name
=
string
(
moving_var
.
name
),
moving_variance_name
=
string
(
"{}_{}"
.
format
(
node
.
name
,
moving_var
.
name
)
),
is_test
=
True
)
is_test
=
True
)
if
data_format
==
"NHWC"
:
if
data_format
==
"NHWC"
:
...
@@ -557,7 +569,7 @@ class TFOpMapper(OpMapper):
...
@@ -557,7 +569,7 @@ class TFOpMapper(OpMapper):
perm
=
[
0
,
3
,
1
,
2
])
perm
=
[
0
,
3
,
1
,
2
])
self
.
paddle_graph
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"paddle.nn.functional.pad"
,
kernel
=
"paddle.nn.functional.pad"
,
inputs
=
{
"
input
"
:
transpose_name
},
inputs
=
{
"
x
"
:
transpose_name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
pad
=
new_padding
)
pad
=
new_padding
)
self
.
paddle_graph
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
...
@@ -709,10 +721,19 @@ class TFOpMapper(OpMapper):
...
@@ -709,10 +721,19 @@ class TFOpMapper(OpMapper):
op_name
=
name_generator
(
"pool"
,
self
.
nn_name2id
)
op_name
=
name_generator
(
"pool"
,
self
.
nn_name2id
)
output_name
=
node
.
name
output_name
=
node
.
name
layer_outputs
=
[
op_name
,
output_name
]
layer_outputs
=
[
op_name
,
output_name
]
# self.paddle_graph.add_layer(
# kernel="paddle.nn.AvgPool2D",
# inputs={"input": input_name},
# outputs=layer_outputs,
# kernel_size=k_size[2:4],
# stride=strides[2:4],
# padding=string(pad_mode))
self
.
paddle_graph
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"
paddle.nn.Pool2D
"
,
kernel
=
"
fluid.layers.pool2d
"
,
inputs
=
{
"input"
:
input_name
},
inputs
=
{
"input"
:
input_name
},
outputs
=
layer_outputs
,
outputs
=
[
node
.
name
]
,
pool_size
=
k_size
[
2
:
4
],
pool_size
=
k_size
[
2
:
4
],
pool_type
=
string
(
"avg"
),
pool_type
=
string
(
"avg"
),
pool_stride
=
strides
[
2
:
4
],
pool_stride
=
strides
[
2
:
4
],
...
@@ -785,7 +806,7 @@ class TFOpMapper(OpMapper):
...
@@ -785,7 +806,7 @@ class TFOpMapper(OpMapper):
input_names
[
i
]
=
cast_name
input_names
[
i
]
=
cast_name
self
.
paddle_graph
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"paddle.concat"
,
kernel
=
"paddle.concat"
,
inputs
=
{
"
input
"
:
input_names
},
inputs
=
{
"
x
"
:
input_names
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
axis
=
axis
)
axis
=
axis
)
if
node
.
dtype
==
'bool'
:
if
node
.
dtype
==
'bool'
:
...
@@ -894,12 +915,12 @@ class TFOpMapper(OpMapper):
...
@@ -894,12 +915,12 @@ class TFOpMapper(OpMapper):
self
.
paddle_graph
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"paddle.split"
,
kernel
=
"paddle.split"
,
inputs
=
{
"
input
"
:
input
.
name
},
inputs
=
{
"
x
"
:
input
.
name
},
outputs
=
[
outputs
=
[
"{}_p{}"
.
format
(
node
.
layer_name
,
i
)
for
i
in
range
(
num_split
)
"{}_p{}"
.
format
(
node
.
layer_name
,
i
)
for
i
in
range
(
num_split
)
],
],
num_or_sections
=
num_split
,
num_or_sections
=
num_split
,
dim
=
dim
)
axis
=
dim
)
def
Slice
(
self
,
node
):
def
Slice
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
])
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
])
...
@@ -944,9 +965,10 @@ class TFOpMapper(OpMapper):
...
@@ -944,9 +965,10 @@ class TFOpMapper(OpMapper):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
])
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
])
resize_shape
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
])
resize_shape
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
])
data_format
=
"NHWC"
data_format
=
"NHWC"
inputs
=
{
"input"
:
input
.
name
}
inputs
=
{
"x"
:
input
.
name
}
attrs
=
{
"align_corner"
:
node
.
get_attr
(
"align_corners"
),
attrs
=
{
"align_corners"
:
node
.
get_attr
(
"align_corners"
),
"mode"
:
"nearest"
}
"mode"
:
string
(
"nearest"
),
"align_mode"
:
1
}
if
resize_shape
.
layer_type
==
"Const"
:
if
resize_shape
.
layer_type
==
"Const"
:
resize_shape
=
resize_shape
.
value
.
tolist
()
resize_shape
=
resize_shape
.
value
.
tolist
()
...
@@ -959,7 +981,7 @@ class TFOpMapper(OpMapper):
...
@@ -959,7 +981,7 @@ class TFOpMapper(OpMapper):
inputs
=
{
"x"
:
resize_shape
.
name
},
inputs
=
{
"x"
:
resize_shape
.
name
},
outputs
=
[
reshape_name
],
outputs
=
[
reshape_name
],
shape
=
shape
)
shape
=
shape
)
inputs
[
"
out_shap
e"
]
=
reshape_name
inputs
[
"
siz
e"
]
=
reshape_name
if
data_format
==
"NHWC"
:
if
data_format
==
"NHWC"
:
transpose_name
=
gen_name
(
"resize_nearest"
,
"reshape"
)
transpose_name
=
gen_name
(
"resize_nearest"
,
"reshape"
)
...
@@ -968,10 +990,10 @@ class TFOpMapper(OpMapper):
...
@@ -968,10 +990,10 @@ class TFOpMapper(OpMapper):
inputs
=
{
"x"
:
input
.
name
},
inputs
=
{
"x"
:
input
.
name
},
outputs
=
[
transpose_name
],
outputs
=
[
transpose_name
],
perm
=
[
0
,
3
,
1
,
2
])
perm
=
[
0
,
3
,
1
,
2
])
inputs
[
"
input
"
]
=
transpose_name
inputs
[
"
x
"
]
=
transpose_name
self
.
paddle_graph
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"paddle.nn.functio
an
l.interpolate"
,
kernel
=
"paddle.nn.functio
na
l.interpolate"
,
inputs
=
inputs
,
inputs
=
inputs
,
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
**
attrs
)
**
attrs
)
...
@@ -982,14 +1004,15 @@ class TFOpMapper(OpMapper):
...
@@ -982,14 +1004,15 @@ class TFOpMapper(OpMapper):
inputs
=
{
"x"
:
node
.
name
},
inputs
=
{
"x"
:
node
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
perm
=
[
0
,
2
,
3
,
1
])
perm
=
[
0
,
2
,
3
,
1
])
def
ResizeBilinear
(
self
,
node
):
def
ResizeBilinear
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
])
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
])
resize_shape
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
])
resize_shape
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
])
data_format
=
"NHWC"
data_format
=
"NHWC"
inputs
=
{
"
input
"
:
input
.
name
}
inputs
=
{
"
x
"
:
input
.
name
}
attrs
=
{
"align_corners"
:
node
.
get_attr
(
"align_corners"
),
attrs
=
{
"align_corners"
:
node
.
get_attr
(
"align_corners"
),
"mode"
:
"bilinear"
}
"mode"
:
string
(
"bilinear"
),
"align_mode"
:
1
}
if
resize_shape
.
layer_type
==
"Const"
:
if
resize_shape
.
layer_type
==
"Const"
:
resize_shape
=
resize_shape
.
value
.
tolist
()
resize_shape
=
resize_shape
.
value
.
tolist
()
...
@@ -1002,7 +1025,7 @@ class TFOpMapper(OpMapper):
...
@@ -1002,7 +1025,7 @@ class TFOpMapper(OpMapper):
inputs
=
{
"x"
:
resize_shape
.
name
},
inputs
=
{
"x"
:
resize_shape
.
name
},
outputs
=
[
reshape_name
],
outputs
=
[
reshape_name
],
shape
=
shape
)
shape
=
shape
)
inputs
[
"
out_shap
e"
]
=
reshape_name
inputs
[
"
siz
e"
]
=
reshape_name
if
data_format
==
"NHWC"
:
if
data_format
==
"NHWC"
:
transpose_name
=
gen_name
(
"resize_bilinear"
,
"reshape"
)
transpose_name
=
gen_name
(
"resize_bilinear"
,
"reshape"
)
...
@@ -1011,10 +1034,10 @@ class TFOpMapper(OpMapper):
...
@@ -1011,10 +1034,10 @@ class TFOpMapper(OpMapper):
inputs
=
{
"x"
:
input
.
name
},
inputs
=
{
"x"
:
input
.
name
},
outputs
=
[
transpose_name
],
outputs
=
[
transpose_name
],
perm
=
[
0
,
3
,
1
,
2
])
perm
=
[
0
,
3
,
1
,
2
])
inputs
[
"
input
"
]
=
transpose_name
inputs
[
"
x
"
]
=
transpose_name
self
.
paddle_graph
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"paddle.nn.functio
an
l.interpolate"
,
kernel
=
"paddle.nn.functio
na
l.interpolate"
,
inputs
=
inputs
,
inputs
=
inputs
,
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
**
attrs
)
**
attrs
)
...
@@ -1044,7 +1067,7 @@ class TFOpMapper(OpMapper):
...
@@ -1044,7 +1067,7 @@ class TFOpMapper(OpMapper):
self
.
paddle_graph
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"paddle.sum"
,
kernel
=
"paddle.sum"
,
inputs
=
{
"
input
"
:
input
.
name
},
inputs
=
{
"
x
"
:
input
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
axis
=
dim
,
axis
=
dim
,
keepdim
=
keep_dims
)
keepdim
=
keep_dims
)
...
@@ -1057,7 +1080,7 @@ class TFOpMapper(OpMapper):
...
@@ -1057,7 +1080,7 @@ class TFOpMapper(OpMapper):
dim
=
reduce_idx
.
value
.
tolist
()
dim
=
reduce_idx
.
value
.
tolist
()
self
.
paddle_graph
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"paddle.max"
,
kernel
=
"paddle.max"
,
inputs
=
{
"
input
"
:
input
.
name
},
inputs
=
{
"
x
"
:
input
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
axis
=
dim
,
axis
=
dim
,
keepdim
=
keep_dims
)
keepdim
=
keep_dims
)
...
@@ -1125,17 +1148,35 @@ class TFOpMapper(OpMapper):
...
@@ -1125,17 +1148,35 @@ class TFOpMapper(OpMapper):
perm
=
[
0
,
3
,
1
,
2
])
perm
=
[
0
,
3
,
1
,
2
])
input_name
=
transpose_name
input_name
=
transpose_name
# self.paddle_graph.add_layer(
# kernel="paddle.nn.Conv2DTranspose",
# inputs={"input": input_name},
# outputs=layer_outputs,
# weight_attr=string(kernel_name),
# bias_attr=False,
# in_channels=k_size[3],
# out_channels=k_size[2],
# kernel_size=k_size[0:2],
# stride=strides[2:4],
# dilation=dilations[2:4],
# padding=string(pad_mode))
self
.
paddle_graph
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"paddle.nn.Conv2DTranspose"
,
"self.create_parameter"
,
inputs
=
{
"input"
:
input_name
},
inputs
=
{},
outputs
=
layer_outputs
,
outputs
=
[
"{}_{}"
.
format
(
node
.
name
,
kernel_name
).
replace
(
"."
,
"_"
)],
bias_attr
=
False
,
shape
=
self
.
params
[
kernel_name
].
shape
,
in_channels
=
k_size
[
3
],
attr
=
string
(
kernel_name
))
out_channels
=
k_size
[
2
],
kernel_size
=
k_size
[
0
:
2
],
self
.
paddle_graph
.
add_layer
(
kernel
=
"paddle.nn.functional.conv2d_transpose"
,
inputs
=
{
"x"
:
input_name
,
"weight"
:
"{}_{}"
.
format
(
node
.
name
,
kernel_name
).
replace
(
"."
,
"_"
)},
outputs
=
[
node
.
name
],
bias
=
None
,
stride
=
strides
[
2
:
4
],
stride
=
strides
[
2
:
4
],
dilation
=
dilations
[
2
:
4
],
dilation
=
dilations
[
2
:
4
],
padding
=
string
(
pad_mode
))
padding
=
string
(
pad_mode
),
output_size
=
out_shape
[
1
:
3
])
if
data_format
==
"NHWC"
:
if
data_format
==
"NHWC"
:
self
.
paddle_graph
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录