Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
X2Paddle
提交
01ac9740
X
X2Paddle
项目概览
PaddlePaddle
/
X2Paddle
大约 2 年 前同步成功
通知
329
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看板
提交
01ac9740
编写于
11月 06, 2020
作者:
S
SunAhong1993
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add tf dygraph
上级
0a209d49
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
129 addition
and
113 deletion
+129
-113
x2paddle/convert.py
x2paddle/convert.py
+11
-7
x2paddle/core/program.py
x2paddle/core/program.py
+9
-3
x2paddle/decoder/tf_decoder.py
x2paddle/decoder/tf_decoder.py
+1
-0
x2paddle/op_mapper/dygraph/prim2code.py
x2paddle/op_mapper/dygraph/prim2code.py
+5
-1
x2paddle/op_mapper/static/tf2paddle/tf_op_mapper.py
x2paddle/op_mapper/static/tf2paddle/tf_op_mapper.py
+103
-102
x2paddle/optimizer/fusion/static/__pycache__/__init__.cpython-37.pyc
...timizer/fusion/static/__pycache__/__init__.cpython-37.pyc
+0
-0
x2paddle/optimizer/fusion/static/__pycache__/bn_scale_fuse_pass.cpython-37.pyc
...sion/static/__pycache__/bn_scale_fuse_pass.cpython-37.pyc
+0
-0
x2paddle/optimizer/fusion/static/__pycache__/bn_scale_fuser.cpython-37.pyc
...r/fusion/static/__pycache__/bn_scale_fuser.cpython-37.pyc
+0
-0
未找到文件。
x2paddle/convert.py
浏览文件 @
01ac9740
...
@@ -103,6 +103,7 @@ def tf2paddle(model_path,
...
@@ -103,6 +103,7 @@ def tf2paddle(model_path,
save_dir
,
save_dir
,
without_data_format_optimization
=
False
,
without_data_format_optimization
=
False
,
define_input_shape
=
False
,
define_input_shape
=
False
,
paddle_type
=
"dygraph"
,
params_merge
=
False
):
params_merge
=
False
):
# check tensorflow installation and version
# check tensorflow installation and version
try
:
try
:
...
@@ -120,25 +121,28 @@ def tf2paddle(model_path,
...
@@ -120,25 +121,28 @@ def tf2paddle(model_path,
"[ERROR] Tensorflow is not installed, use
\"
pip install tensorflow
\"
."
"[ERROR] Tensorflow is not installed, use
\"
pip install tensorflow
\"
."
)
)
return
return
from
x2paddle.decoder.tf_decoder
import
TFDecoder
from
x2paddle.decoder.tf_decoder
import
TFDecoder
from
x2paddle.op_mapper.tf_op_mapper
import
TFOpMapper
if
paddle_type
==
"dygraph"
:
from
x2paddle.op_mapper.dygraph.tf2paddle.tf_op_mapper
import
TFOpMapper
else
:
from
x2paddle.op_mapper.static.tf2paddle.tf_op_mapper
import
TFOpMapper
from
x2paddle.optimizer.tensorflow.bias
import
BiasOpt
from
x2paddle.optimizer.tensorflow.bias
import
BiasOpt
from
x2paddle.optimizer.tensorflow.transpose
import
TransposeOpt
from
x2paddle.optimizer.tensorflow.transpose
import
TransposeOpt
from
x2paddle.optimizer.tensorflow.batch_norm
import
BatchNormOpt
from
x2paddle.optimizer.tensorflow.batch_norm
import
BatchNormOpt
print
(
"Now translating model from tensorflow to paddle."
)
print
(
"Now translating model from tensorflow to paddle."
)
model
=
TFDecoder
(
model_path
,
define_input_shape
=
define_input_shape
)
model
=
TFDecoder
(
model_path
,
define_input_shape
=
define_input_shape
)
mapper
=
TFOpMapper
(
model
)
mapper
=
TFOpMapper
(
model
)
program
.
build
()
mapper
.
paddle_graph
.
build
()
bias_opt
=
BiasOpt
()
bias_opt
=
BiasOpt
()
transpose_opt
=
TransposeOpt
()
transpose_opt
=
TransposeOpt
()
batch_norm_opt
=
BatchNormOpt
()
batch_norm_opt
=
BatchNormOpt
()
bias_opt
.
run
(
program
)
bias_opt
.
run
(
program
)
batch_norm_opt
.
run
(
program
)
batch_norm_opt
.
run
(
program
)
transpose_opt
.
run
(
program
)
transpose_opt
.
run
(
program
)
program
.
gen_model
(
save_dir
)
mapper
.
paddle_graph
.
gen_model
(
save_dir
)
def
caffe2paddle
(
proto
,
weight
,
save_dir
,
caffe_proto
,
def
caffe2paddle
(
proto
,
weight
,
save_dir
,
caffe_proto
,
...
@@ -293,7 +297,7 @@ def main():
...
@@ -293,7 +297,7 @@ def main():
if
args
.
params_merge
:
if
args
.
params_merge
:
params_merge
=
True
params_merge
=
True
tf2paddle
(
args
.
model
,
args
.
save_dir
,
without_data_format_optimization
,
tf2paddle
(
args
.
model
,
args
.
save_dir
,
without_data_format_optimization
,
define_input_shape
,
params_merge
)
define_input_shape
,
args
.
paddle_type
,
params_merge
)
elif
args
.
framework
==
"caffe"
:
elif
args
.
framework
==
"caffe"
:
assert
args
.
prototxt
is
not
None
and
args
.
weight
is
not
None
,
"--prototxt and --weight should be defined while translating caffe model"
assert
args
.
prototxt
is
not
None
and
args
.
weight
is
not
None
,
"--prototxt and --weight should be defined while translating caffe model"
...
...
x2paddle/core/program.py
浏览文件 @
01ac9740
...
@@ -76,6 +76,7 @@ class PaddleGraph(object):
...
@@ -76,6 +76,7 @@ class PaddleGraph(object):
self
.
custom_code
=
None
self
.
custom_code
=
None
self
.
inputs_info
=
None
self
.
inputs_info
=
None
def
set_name
(
self
,
name
):
def
set_name
(
self
,
name
):
self
.
name
=
name
.
replace
(
"-"
,
"_"
).
replace
(
"/"
,
"_"
)
self
.
name
=
name
.
replace
(
"-"
,
"_"
).
replace
(
"/"
,
"_"
)
...
@@ -285,8 +286,8 @@ class PaddleGraph(object):
...
@@ -285,8 +286,8 @@ class PaddleGraph(object):
for
input_name
in
self
.
inputs
:
for
input_name
in
self
.
inputs
:
input_shapes
.
append
(
self
.
inputs_info
[
input_name
][
0
])
input_shapes
.
append
(
self
.
inputs_info
[
input_name
][
0
])
input_types
.
append
(
self
.
inputs_info
[
input_name
][
1
])
input_types
.
append
(
self
.
inputs_info
[
input_name
][
1
])
# 如果input_files非空,则导出推理模型;其值类似[[None, 3, 224, 224]]
# 如果input_files非空,则导出推理模型;其值类似[[None, 3, 224, 224]]
self
.
dygraph2static
(
save_dir
,
input_shapes
,
input_types
)
self
.
dygraph2static
(
save_dir
,
input_shapes
,
input_types
)
def
gen_static_code
(
self
,
code_dir
):
def
gen_static_code
(
self
,
code_dir
):
def
write_code
(
f
,
code_list
,
indent
=
0
):
def
write_code
(
f
,
code_list
,
indent
=
0
):
...
@@ -446,6 +447,8 @@ class PaddleGraph(object):
...
@@ -446,6 +447,8 @@ class PaddleGraph(object):
if
self
.
source_type
==
"caffe"
:
if
self
.
source_type
==
"caffe"
:
custom_import
=
"from x2paddle.op_mapper.dygraph.caffe2paddle "
+
\
custom_import
=
"from x2paddle.op_mapper.dygraph.caffe2paddle "
+
\
"import caffe_custom_layer as x2paddle_nn"
"import caffe_custom_layer as x2paddle_nn"
else
:
custom_import
=
""
self
.
head
=
gen_codes
(
self
.
head
=
gen_codes
(
[
[
"from paddle.fluid.initializer import Constant"
,
"from paddle.fluid.initializer import Constant"
,
...
@@ -618,7 +621,10 @@ class PaddleGraph(object):
...
@@ -618,7 +621,10 @@ class PaddleGraph(object):
paddle
.
disable_static
()
paddle
.
disable_static
()
restore
,
_
=
fluid
.
load_dygraph
(
osp
.
join
(
save_dir
,
"model"
))
restore
,
_
=
fluid
.
load_dygraph
(
osp
.
join
(
save_dir
,
"model"
))
model
=
getattr
(
x2paddle_code
,
self
.
name
)()
model
=
getattr
(
x2paddle_code
,
self
.
name
)()
model
.
set_dict
(
restore
)
if
self
.
source_type
==
"tf"
:
model
.
set_dict
(
restore
,
use_structured_name
=
False
)
else
:
model
.
set_dict
(
restore
)
model
.
eval
()
model
.
eval
()
static_model
=
paddle
.
jit
.
to_static
(
model
,
input_spec
=
sepc_list
)
static_model
=
paddle
.
jit
.
to_static
(
model
,
input_spec
=
sepc_list
)
paddle
.
jit
.
save
(
static_model
,
osp
.
join
(
save_dir
,
"inference_model/model"
))
paddle
.
jit
.
save
(
static_model
,
osp
.
join
(
save_dir
,
"inference_model/model"
))
\ No newline at end of file
x2paddle/decoder/tf_decoder.py
浏览文件 @
01ac9740
...
@@ -132,6 +132,7 @@ class TFGraph(Graph):
...
@@ -132,6 +132,7 @@ class TFGraph(Graph):
self
.
identity_map
=
dict
()
self
.
identity_map
=
dict
()
self
.
multi_out_ops
=
[
'Split'
,
'SplitV'
,
'IteratorV2'
]
self
.
multi_out_ops
=
[
'Split'
,
'SplitV'
,
'IteratorV2'
]
self
.
tf_data_format
=
data_format
self
.
tf_data_format
=
data_format
self
.
graph_name
=
"TFModel"
def
build
(
self
):
def
build
(
self
):
for
layer
in
self
.
model
.
node
:
for
layer
in
self
.
model
.
node
:
...
...
x2paddle/op_mapper/dygraph/prim2code.py
浏览文件 @
01ac9740
...
@@ -277,7 +277,11 @@ def prim_list(layer, indent=1, init_func=[], forward_func=[], layer_id=None, dif
...
@@ -277,7 +277,11 @@ def prim_list(layer, indent=1, init_func=[], forward_func=[], layer_id=None, dif
inputs_str
=
', '
.
join
(
inputs_list
)
inputs_str
=
', '
.
join
(
inputs_list
)
line
=
"{} = [{}]"
.
format
(
layer
.
outputs
[
0
],
inputs_str
)
line
=
"{} = [{}]"
.
format
(
layer
.
outputs
[
0
],
inputs_str
)
forward_func
.
extend
(
gen_codes
([
line
],
indent
=
indent
))
forward_func
.
extend
(
gen_codes
([
line
],
indent
=
indent
))
def
prim_list_mul
(
layer
,
indent
=
1
,
init_func
=
[],
forward_func
=
[],
layer_id
=
None
,
different_attrs
=
None
):
line
=
"{} = [a*b for a,b in zip({}, {})]"
.
format
(
layer
.
outputs
[
0
],
get_value
(
layer
,
"list0"
,
different_attrs
),
get_value
(
layer
,
"list1"
,
different_attrs
))
def
prim_list_unpack
(
layer
,
indent
=
1
,
init_func
=
[],
forward_func
=
[],
layer_id
=
None
,
different_attrs
=
None
):
def
prim_list_unpack
(
layer
,
indent
=
1
,
init_func
=
[],
forward_func
=
[],
layer_id
=
None
,
different_attrs
=
None
):
line
=
"{} = {}"
.
format
(
", "
.
join
(
layer
.
outputs
),
get_value
(
layer
,
"input"
,
different_attrs
))
line
=
"{} = {}"
.
format
(
", "
.
join
(
layer
.
outputs
),
get_value
(
layer
,
"input"
,
different_attrs
))
...
...
x2paddle/op_mapper/static/tf2paddle/tf_op_mapper.py
浏览文件 @
01ac9740
...
@@ -13,6 +13,7 @@
...
@@ -13,6 +13,7 @@
# limitations under the License.
# limitations under the License.
from
x2paddle.decoder.tf_decoder
import
TFGraph
from
x2paddle.decoder.tf_decoder
import
TFGraph
from
x2paddle.core.program
import
PaddleGraph
from
x2paddle.core.op_mapper
import
OpMapper
from
x2paddle.core.op_mapper
import
OpMapper
from
x2paddle.core.util
import
*
from
x2paddle.core.util
import
*
from
x2paddle
import
program
from
x2paddle
import
program
...
@@ -82,10 +83,8 @@ class TFOpMapper(OpMapper):
...
@@ -82,10 +83,8 @@ class TFOpMapper(OpMapper):
super
(
TFOpMapper
,
self
).
__init__
()
super
(
TFOpMapper
,
self
).
__init__
()
self
.
decoder
=
decoder
self
.
decoder
=
decoder
self
.
graph
=
decoder
.
tf_graph
self
.
graph
=
decoder
.
tf_graph
self
.
weights
=
dict
()
self
.
params
=
dict
()
self
.
omit_nodes
=
list
()
self
.
paddle_graph
=
PaddleGraph
(
parent_layer
=
None
,
graph_type
=
"static"
,
source_type
=
"tf"
)
self
.
used_custom_layers
=
dict
()
program
.
clear
()
not_placeholder
=
list
()
not_placeholder
=
list
()
for
name
in
self
.
graph
.
input_nodes
:
for
name
in
self
.
graph
.
input_nodes
:
...
@@ -99,8 +98,8 @@ class TFOpMapper(OpMapper):
...
@@ -99,8 +98,8 @@ class TFOpMapper(OpMapper):
idx
=
self
.
graph
.
input_nodes
.
index
(
name
)
idx
=
self
.
graph
.
input_nodes
.
index
(
name
)
del
self
.
graph
.
input_nodes
[
idx
]
del
self
.
graph
.
input_nodes
[
idx
]
program
.
inputs
=
self
.
graph
.
input_nodes
self
.
paddle_graph
.
inputs
=
self
.
graph
.
input_nodes
program
.
outputs
=
self
.
graph
.
output_nodes
self
.
paddle_graph
.
outputs
=
self
.
graph
.
output_nodes
unsupported_ops
=
set
()
unsupported_ops
=
set
()
sys
.
stderr
.
write
(
"Total nodes: {}
\n
"
.
format
(
len
(
self
.
graph
.
topo_sort
)))
sys
.
stderr
.
write
(
"Total nodes: {}
\n
"
.
format
(
len
(
self
.
graph
.
topo_sort
)))
...
@@ -134,6 +133,8 @@ class TFOpMapper(OpMapper):
...
@@ -134,6 +133,8 @@ class TFOpMapper(OpMapper):
print
(
"========== {} ============"
.
format
(
op
))
print
(
"========== {} ============"
.
format
(
op
))
sys
.
exit
(
-
1
)
sys
.
exit
(
-
1
)
sys
.
stderr
.
write
(
"
\n
Done!
\n
"
)
sys
.
stderr
.
write
(
"
\n
Done!
\n
"
)
self
.
paddle_graph
.
set_name
(
self
.
graph
.
graph_name
)
self
.
paddle_graph
.
set_parameters
(
self
.
params
)
def
directly_map
(
self
,
node
):
def
directly_map
(
self
,
node
):
assert
node
.
layer_type
in
self
.
directly_map_ops
assert
node
.
layer_type
in
self
.
directly_map_ops
...
@@ -146,7 +147,7 @@ class TFOpMapper(OpMapper):
...
@@ -146,7 +147,7 @@ class TFOpMapper(OpMapper):
tf_param
=
node
.
get_attr
(
tf_param_name
)
tf_param
=
node
.
get_attr
(
tf_param_name
)
attr
[
pd_param_name
]
=
tf_param
attr
[
pd_param_name
]
=
tf_param
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.{}"
.
format
(
op_info
[
0
]),
kernel
=
"fluid.layers.{}"
.
format
(
op_info
[
0
]),
inputs
=
{
"x"
:
input
.
name
},
inputs
=
{
"x"
:
input
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -159,18 +160,18 @@ class TFOpMapper(OpMapper):
...
@@ -159,18 +160,18 @@ class TFOpMapper(OpMapper):
y
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
])
y
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
])
x_shape
=
x
.
out_shapes
[
0
]
x_shape
=
x
.
out_shapes
[
0
]
y_shape
=
y
.
out_shapes
[
0
]
y_shape
=
y
.
out_shapes
[
0
]
layer_id
=
program
.
add_layer
(
layer_id
=
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.{}"
.
format
(
op_type
),
kernel
=
"fluid.layers.{}"
.
format
(
op_type
),
inputs
=
{
"x"
:
x
.
name
,
inputs
=
{
"x"
:
x
.
name
,
"y"
:
y
.
name
},
"y"
:
y
.
name
},
outputs
=
[
node
.
name
])
outputs
=
[
node
.
name
])
program
.
layers
[
layer_id
].
input_shapes
=
{
"x"
:
x_shape
,
"y"
:
y_shape
}
self
.
paddle_graph
.
layers
[
layer_id
].
input_shapes
=
{
"x"
:
x_shape
,
"y"
:
y_shape
}
def
NotEqual
(
self
,
node
):
def
NotEqual
(
self
,
node
):
x
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
])
x
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
])
y
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
])
y
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
])
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.not_equal"
,
kernel
=
"fluid.layers.not_equal"
,
inputs
=
{
"x"
:
x
.
name
,
inputs
=
{
"x"
:
x
.
name
,
"y"
:
y
.
name
},
"y"
:
y
.
name
},
...
@@ -181,7 +182,7 @@ class TFOpMapper(OpMapper):
...
@@ -181,7 +182,7 @@ class TFOpMapper(OpMapper):
assert
len
(
shape
)
!=
0
,
"Unknown shape of input nodes[{}]."
.
format
(
assert
len
(
shape
)
!=
0
,
"Unknown shape of input nodes[{}]."
.
format
(
node
.
layer_name
)
node
.
layer_name
)
dtype
=
node
.
dtype
dtype
=
node
.
dtype
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.data"
,
kernel
=
"fluid.data"
,
inputs
=
{},
inputs
=
{},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -199,7 +200,7 @@ class TFOpMapper(OpMapper):
...
@@ -199,7 +200,7 @@ class TFOpMapper(OpMapper):
shape
=
[
1
]
shape
=
[
1
]
if
value
==
float
(
'inf'
):
if
value
==
float
(
'inf'
):
value
=
"float('inf')"
value
=
"float('inf')"
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.fill_constant"
,
kernel
=
"fluid.layers.fill_constant"
,
inputs
=
{},
inputs
=
{},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -208,8 +209,8 @@ class TFOpMapper(OpMapper):
...
@@ -208,8 +209,8 @@ class TFOpMapper(OpMapper):
value
=
value
)
value
=
value
)
return
return
program
.
parameter
s
[
node
.
name
]
=
node
.
value
self
.
param
s
[
node
.
name
]
=
node
.
value
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.create_parameter"
,
kernel
=
"fluid.layers.create_parameter"
,
inputs
=
{},
inputs
=
{},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -224,7 +225,7 @@ class TFOpMapper(OpMapper):
...
@@ -224,7 +225,7 @@ class TFOpMapper(OpMapper):
assert
perm
.
layer_type
==
"Const"
,
"Perm of transpose OP should be Const"
assert
perm
.
layer_type
==
"Const"
,
"Perm of transpose OP should be Const"
perm
=
perm
.
value
.
tolist
()
perm
=
perm
.
value
.
tolist
()
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.transpose"
,
kernel
=
"fluid.layers.transpose"
,
inputs
=
{
"x"
:
input
.
name
},
inputs
=
{
"x"
:
input
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -243,7 +244,7 @@ class TFOpMapper(OpMapper):
...
@@ -243,7 +244,7 @@ class TFOpMapper(OpMapper):
attr
[
"dtype"
]
=
string
(
input_value
.
dtype
)
attr
[
"dtype"
]
=
string
(
input_value
.
dtype
)
attr
[
"value"
]
=
input_value
.
value
attr
[
"value"
]
=
input_value
.
value
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
"fluid.layers.fill_constant"
,
"fluid.layers.fill_constant"
,
inputs
=
inputs
,
inputs
=
inputs
,
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -262,7 +263,7 @@ class TFOpMapper(OpMapper):
...
@@ -262,7 +263,7 @@ class TFOpMapper(OpMapper):
input_name
=
input
.
name
input_name
=
input
.
name
if
data_format
==
"NHWC"
:
if
data_format
==
"NHWC"
:
transpose_name
=
gen_name
(
"depth_to_space"
,
"transpose"
)
transpose_name
=
gen_name
(
"depth_to_space"
,
"transpose"
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.transpose"
,
kernel
=
"fluid.layers.transpose"
,
inputs
=
{
"x"
:
input
.
name
},
inputs
=
{
"x"
:
input
.
name
},
outputs
=
[
transpose_name
],
outputs
=
[
transpose_name
],
...
@@ -271,34 +272,34 @@ class TFOpMapper(OpMapper):
...
@@ -271,34 +272,34 @@ class TFOpMapper(OpMapper):
shape
=
[
0
,
block_size
*
block_size
,
-
1
,
h
,
w
]
shape
=
[
0
,
block_size
*
block_size
,
-
1
,
h
,
w
]
reshape_name
=
gen_name
(
"depth_to_space"
,
"reshape"
)
reshape_name
=
gen_name
(
"depth_to_space"
,
"reshape"
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.reshape"
,
kernel
=
"fluid.layers.reshape"
,
inputs
=
{
"x"
:
input_name
},
inputs
=
{
"x"
:
input_name
},
outputs
=
[
reshape_name
],
outputs
=
[
reshape_name
],
shape
=
shape
)
shape
=
shape
)
transpose_name
=
gen_name
(
"depth_to_space"
,
"transpose"
)
transpose_name
=
gen_name
(
"depth_to_space"
,
"transpose"
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.transpose"
,
kernel
=
"fluid.layers.transpose"
,
inputs
=
{
"x"
:
reshape_name
},
inputs
=
{
"x"
:
reshape_name
},
outputs
=
[
transpose_name
],
outputs
=
[
transpose_name
],
perm
=
[
0
,
2
,
1
,
3
,
4
])
perm
=
[
0
,
2
,
1
,
3
,
4
])
reshape_name
=
gen_name
(
"depth_to_space"
,
"reshape"
)
reshape_name
=
gen_name
(
"depth_to_space"
,
"reshape"
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.reshape"
,
kernel
=
"fluid.layers.reshape"
,
inputs
=
{
"x"
:
transpose_name
},
inputs
=
{
"x"
:
transpose_name
},
outputs
=
[
reshape_name
],
outputs
=
[
reshape_name
],
shape
=
[
0
,
c
,
h
,
w
])
shape
=
[
0
,
c
,
h
,
w
])
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.pixel_shuffle"
,
kernel
=
"fluid.layers.pixel_shuffle"
,
inputs
=
{
"x"
:
reshape_name
},
inputs
=
{
"x"
:
reshape_name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
upscale_factor
=
block_size
)
upscale_factor
=
block_size
)
if
data_format
==
"NHWC"
:
if
data_format
==
"NHWC"
:
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.transpose"
,
kernel
=
"fluid.layers.transpose"
,
inputs
=
{
"x"
:
node
.
name
},
inputs
=
{
"x"
:
node
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -315,7 +316,7 @@ class TFOpMapper(OpMapper):
...
@@ -315,7 +316,7 @@ class TFOpMapper(OpMapper):
input_name
=
input
.
name
input_name
=
input
.
name
if
data_format
==
"NHWC"
:
if
data_format
==
"NHWC"
:
transpose_name
=
gen_name
(
"max_pool"
,
"transpose"
)
transpose_name
=
gen_name
(
"max_pool"
,
"transpose"
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.transpose"
,
kernel
=
"fluid.layers.transpose"
,
inputs
=
{
"x"
:
input
.
name
},
inputs
=
{
"x"
:
input
.
name
},
outputs
=
[
transpose_name
],
outputs
=
[
transpose_name
],
...
@@ -324,7 +325,7 @@ class TFOpMapper(OpMapper):
...
@@ -324,7 +325,7 @@ class TFOpMapper(OpMapper):
k_size
=
[
k_size
[
i
]
for
i
in
[
0
,
3
,
1
,
2
]]
k_size
=
[
k_size
[
i
]
for
i
in
[
0
,
3
,
1
,
2
]]
input_name
=
transpose_name
input_name
=
transpose_name
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.pool2d"
,
kernel
=
"fluid.layers.pool2d"
,
inputs
=
{
"input"
:
input_name
},
inputs
=
{
"input"
:
input_name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -334,7 +335,7 @@ class TFOpMapper(OpMapper):
...
@@ -334,7 +335,7 @@ class TFOpMapper(OpMapper):
pool_padding
=
string
(
pad_mode
))
pool_padding
=
string
(
pad_mode
))
if
data_format
==
"NHWC"
:
if
data_format
==
"NHWC"
:
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.transpose"
,
kernel
=
"fluid.layers.transpose"
,
inputs
=
{
"x"
:
node
.
name
},
inputs
=
{
"x"
:
node
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -364,7 +365,7 @@ class TFOpMapper(OpMapper):
...
@@ -364,7 +365,7 @@ class TFOpMapper(OpMapper):
kernel
.
name
)
kernel
.
name
)
else
:
else
:
kernel_weight_name
=
kernel
.
name
.
replace
(
'/'
,
'_'
)
kernel_weight_name
=
kernel
.
name
.
replace
(
'/'
,
'_'
)
program
.
parameter
s
[
kernel_weight_name
]
=
numpy
.
transpose
(
kernel_value
,
self
.
param
s
[
kernel_weight_name
]
=
numpy
.
transpose
(
kernel_value
,
(
3
,
2
,
0
,
1
))
(
3
,
2
,
0
,
1
))
input_name
=
input
.
name
input_name
=
input
.
name
...
@@ -372,7 +373,7 @@ class TFOpMapper(OpMapper):
...
@@ -372,7 +373,7 @@ class TFOpMapper(OpMapper):
strides
=
[
strides
[
i
]
for
i
in
[
0
,
3
,
1
,
2
]]
strides
=
[
strides
[
i
]
for
i
in
[
0
,
3
,
1
,
2
]]
dilations
=
[
dilations
[
i
]
for
i
in
[
0
,
3
,
1
,
2
]]
dilations
=
[
dilations
[
i
]
for
i
in
[
0
,
3
,
1
,
2
]]
transpose_name
=
gen_name
(
"conv2d"
,
"transpose"
)
transpose_name
=
gen_name
(
"conv2d"
,
"transpose"
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.transpose"
,
kernel
=
"fluid.layers.transpose"
,
inputs
=
{
"x"
:
input
.
name
},
inputs
=
{
"x"
:
input
.
name
},
outputs
=
[
transpose_name
],
outputs
=
[
transpose_name
],
...
@@ -383,13 +384,13 @@ class TFOpMapper(OpMapper):
...
@@ -383,13 +384,13 @@ class TFOpMapper(OpMapper):
attr
=
{
"shape"
:
[
0
,
k_size
[
2
],
0
,
0
]}
attr
=
{
"shape"
:
[
0
,
k_size
[
2
],
0
,
0
]}
node
.
fluid_code
.
add_layer
(
node
.
fluid_code
.
add_layer
(
"reshape"
,
inputs
=
input
,
output
=
input
,
param_attr
=
attr
)
"reshape"
,
inputs
=
input
,
output
=
input
,
param_attr
=
attr
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.reshape"
,
kernel
=
"fluid.layers.reshape"
,
inputs
=
{
"x"
:
input_name
},
inputs
=
{
"x"
:
input_name
},
outputs
=
[
input_name
],
outputs
=
[
input_name
],
shape
=
[
0
,
k_size
[
2
],
0
,
0
])
shape
=
[
0
,
k_size
[
2
],
0
,
0
])
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.conv2d"
,
kernel
=
"fluid.layers.conv2d"
,
inputs
=
{
"input"
:
input_name
},
inputs
=
{
"input"
:
input_name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -402,7 +403,7 @@ class TFOpMapper(OpMapper):
...
@@ -402,7 +403,7 @@ class TFOpMapper(OpMapper):
padding
=
string
(
pad_mode
))
padding
=
string
(
pad_mode
))
if
data_format
==
"NHWC"
:
if
data_format
==
"NHWC"
:
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.transpose"
,
kernel
=
"fluid.layers.transpose"
,
inputs
=
{
"x"
:
node
.
name
},
inputs
=
{
"x"
:
node
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -411,7 +412,7 @@ class TFOpMapper(OpMapper):
...
@@ -411,7 +412,7 @@ class TFOpMapper(OpMapper):
def
BiasAdd
(
self
,
node
):
def
BiasAdd
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
])
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
])
bias
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
])
bias
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
])
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.elementwise_add"
,
kernel
=
"fluid.layers.elementwise_add"
,
inputs
=
{
"x"
:
input
.
name
,
inputs
=
{
"x"
:
input
.
name
,
"y"
:
bias
.
name
},
"y"
:
bias
.
name
},
...
@@ -433,14 +434,14 @@ class TFOpMapper(OpMapper):
...
@@ -433,14 +434,14 @@ class TFOpMapper(OpMapper):
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"
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.transpose"
,
kernel
=
"fluid.layers.transpose"
,
inputs
=
{
"x"
:
input
.
name
},
inputs
=
{
"x"
:
input
.
name
},
outputs
=
[
transpose_name
],
outputs
=
[
transpose_name
],
perm
=
[
0
,
3
,
1
,
2
])
perm
=
[
0
,
3
,
1
,
2
])
input_name
=
transpose_name
input_name
=
transpose_name
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.batch_norm"
,
kernel
=
"fluid.layers.batch_norm"
,
inputs
=
{
"input"
:
input_name
},
inputs
=
{
"input"
:
input_name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -452,7 +453,7 @@ class TFOpMapper(OpMapper):
...
@@ -452,7 +453,7 @@ class TFOpMapper(OpMapper):
is_test
=
True
)
is_test
=
True
)
if
data_format
==
"NHWC"
:
if
data_format
==
"NHWC"
:
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.transpose"
,
kernel
=
"fluid.layers.transpose"
,
inputs
=
{
"x"
:
node
.
name
},
inputs
=
{
"x"
:
node
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -465,7 +466,7 @@ class TFOpMapper(OpMapper):
...
@@ -465,7 +466,7 @@ class TFOpMapper(OpMapper):
dims
=
reduce_idx
.
value
.
tolist
()
dims
=
reduce_idx
.
value
.
tolist
()
keep_dims
=
node
.
get_attr
(
"keep_dims"
)
keep_dims
=
node
.
get_attr
(
"keep_dims"
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.reduce_mean"
,
kernel
=
"fluid.layers.reduce_mean"
,
inputs
=
{
"input"
:
input
.
name
},
inputs
=
{
"input"
:
input
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -479,7 +480,7 @@ class TFOpMapper(OpMapper):
...
@@ -479,7 +480,7 @@ class TFOpMapper(OpMapper):
input_name
=
input
.
name
input_name
=
input
.
name
if
input
.
dtype
==
'bool'
:
if
input
.
dtype
==
'bool'
:
cast_name
=
gen_name
(
'reshape'
,
'cast'
)
cast_name
=
gen_name
(
'reshape'
,
'cast'
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.cast"
,
kernel
=
"fluid.layers.cast"
,
inputs
=
{
"x"
:
input_name
},
inputs
=
{
"x"
:
input_name
},
outputs
=
[
cast_name
],
outputs
=
[
cast_name
],
...
@@ -488,13 +489,13 @@ class TFOpMapper(OpMapper):
...
@@ -488,13 +489,13 @@ class TFOpMapper(OpMapper):
if
param
.
layer_type
==
"Const"
:
if
param
.
layer_type
==
"Const"
:
shape
=
param
.
value
.
tolist
()
shape
=
param
.
value
.
tolist
()
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.reshape"
,
kernel
=
"fluid.layers.reshape"
,
inputs
=
{
"x"
:
input_name
},
inputs
=
{
"x"
:
input_name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
shape
=
shape
)
shape
=
shape
)
else
:
else
:
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.reshape"
,
kernel
=
"fluid.layers.reshape"
,
inputs
=
{
"x"
:
input_name
,
inputs
=
{
"x"
:
input_name
,
"shape"
:
param
.
name
},
"shape"
:
param
.
name
},
...
@@ -503,14 +504,14 @@ class TFOpMapper(OpMapper):
...
@@ -503,14 +504,14 @@ class TFOpMapper(OpMapper):
out_shape
=
numpy
.
array
(
node
.
out_shapes
[
0
])
out_shape
=
numpy
.
array
(
node
.
out_shapes
[
0
])
if
(
out_shape
>
0
).
any
():
if
(
out_shape
>
0
).
any
():
out_shape
[
out_shape
<
0
]
=
0
out_shape
[
out_shape
<
0
]
=
0
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.reshape"
,
kernel
=
"fluid.layers.reshape"
,
inputs
=
{
"x"
:
node
.
name
},
inputs
=
{
"x"
:
node
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
shape
=
out_shape
.
tolist
())
shape
=
out_shape
.
tolist
())
if
input
.
dtype
==
'bool'
:
if
input
.
dtype
==
'bool'
:
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.cast"
,
kernel
=
"fluid.layers.cast"
,
inputs
=
{
"x"
:
node
.
name
},
inputs
=
{
"x"
:
node
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -526,24 +527,24 @@ class TFOpMapper(OpMapper):
...
@@ -526,24 +527,24 @@ class TFOpMapper(OpMapper):
if
paddings
[
0
]
+
paddings
[
1
]
+
paddings
[
6
]
+
paddings
[
7
]
==
0
:
if
paddings
[
0
]
+
paddings
[
1
]
+
paddings
[
6
]
+
paddings
[
7
]
==
0
:
new_padding
=
paddings
[
2
:
6
]
new_padding
=
paddings
[
2
:
6
]
transpose_name
=
gen_name
(
"pad"
,
"transpose"
)
transpose_name
=
gen_name
(
"pad"
,
"transpose"
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.transpose"
,
kernel
=
"fluid.layers.transpose"
,
inputs
=
{
"x"
:
input
.
name
},
inputs
=
{
"x"
:
input
.
name
},
outputs
=
[
transpose_name
],
outputs
=
[
transpose_name
],
perm
=
[
0
,
3
,
1
,
2
])
perm
=
[
0
,
3
,
1
,
2
])
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.pad2d"
,
kernel
=
"fluid.layers.pad2d"
,
inputs
=
{
"input"
:
transpose_name
},
inputs
=
{
"input"
:
transpose_name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
paddings
=
new_padding
)
paddings
=
new_padding
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.transpose"
,
kernel
=
"fluid.layers.transpose"
,
inputs
=
{
"x"
:
node
.
name
},
inputs
=
{
"x"
:
node
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
perm
=
[
0
,
2
,
3
,
1
])
perm
=
[
0
,
2
,
3
,
1
])
return
return
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.pad"
,
kernel
=
"fluid.layers.pad"
,
inputs
=
{
"input"
:
input
.
name
},
inputs
=
{
"input"
:
input
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -552,7 +553,7 @@ class TFOpMapper(OpMapper):
...
@@ -552,7 +553,7 @@ class TFOpMapper(OpMapper):
def
Squeeze
(
self
,
node
):
def
Squeeze
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
])
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
])
squeeze_dims
=
node
.
get_attr
(
'squeeze_dims'
)
squeeze_dims
=
node
.
get_attr
(
'squeeze_dims'
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.squeeze"
,
kernel
=
"fluid.layers.squeeze"
,
inputs
=
{
"input"
:
input
.
name
},
inputs
=
{
"input"
:
input
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -561,7 +562,7 @@ class TFOpMapper(OpMapper):
...
@@ -561,7 +562,7 @@ class TFOpMapper(OpMapper):
def
Softmax
(
self
,
node
):
def
Softmax
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
])
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
])
axis
=
node
.
get_attr
(
"axis"
)
axis
=
node
.
get_attr
(
"axis"
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.softmax"
,
kernel
=
"fluid.layers.softmax"
,
inputs
=
{
"input"
:
input
.
name
},
inputs
=
{
"input"
:
input
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -572,13 +573,13 @@ class TFOpMapper(OpMapper):
...
@@ -572,13 +573,13 @@ class TFOpMapper(OpMapper):
input_name
=
input
.
name
input_name
=
input
.
name
if
input
.
dtype
==
'bool'
:
if
input
.
dtype
==
'bool'
:
cast_name
=
gen_name
(
'shape'
,
'cast'
)
cast_name
=
gen_name
(
'shape'
,
'cast'
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.cast"
,
kernel
=
"fluid.layers.cast"
,
inputs
=
{
"x"
:
input
.
name
},
inputs
=
{
"x"
:
input
.
name
},
outputs
=
[
cast_name
],
outputs
=
[
cast_name
],
dtype
=
"'int32'"
)
dtype
=
"'int32'"
)
input_name
=
cast_name
input_name
=
cast_name
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.shape"
,
kernel
=
"fluid.layers.shape"
,
inputs
=
{
"input"
:
input_name
},
inputs
=
{
"input"
:
input_name
},
outputs
=
[
node
.
name
])
outputs
=
[
node
.
name
])
...
@@ -588,7 +589,7 @@ class TFOpMapper(OpMapper):
...
@@ -588,7 +589,7 @@ class TFOpMapper(OpMapper):
axis
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
])
axis
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
])
assert
axis
.
layer_type
==
"Const"
,
"ArgMax only support Const parameter"
assert
axis
.
layer_type
==
"Const"
,
"ArgMax only support Const parameter"
axis
=
axis
.
value
axis
=
axis
.
value
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.argmax"
,
kernel
=
"fluid.layers.argmax"
,
inputs
=
{
"x"
:
input
.
name
},
inputs
=
{
"x"
:
input
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -603,7 +604,7 @@ class TFOpMapper(OpMapper):
...
@@ -603,7 +604,7 @@ class TFOpMapper(OpMapper):
transpose_a
=
node
.
get_attr
(
'adj_x'
)
transpose_a
=
node
.
get_attr
(
'adj_x'
)
if
transpose_b
is
None
:
if
transpose_b
is
None
:
transpose_b
=
node
.
get_attr
(
'adj_y'
)
transpose_b
=
node
.
get_attr
(
'adj_y'
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.matmul"
,
kernel
=
"fluid.layers.matmul"
,
inputs
=
{
"x"
:
x
.
name
,
inputs
=
{
"x"
:
x
.
name
,
"y"
:
y
.
name
},
"y"
:
y
.
name
},
...
@@ -629,7 +630,7 @@ class TFOpMapper(OpMapper):
...
@@ -629,7 +630,7 @@ class TFOpMapper(OpMapper):
data_format
=
node
.
get_attr
(
"data_format"
).
decode
()
data_format
=
node
.
get_attr
(
"data_format"
).
decode
()
pad_mode
=
node
.
get_attr
(
"padding"
).
decode
()
pad_mode
=
node
.
get_attr
(
"padding"
).
decode
()
program
.
parameter
s
[
kernel
.
layer_name
.
replace
(
self
.
param
s
[
kernel
.
layer_name
.
replace
(
'/'
,
'_'
)]
=
numpy
.
transpose
(
kernel
.
value
,
(
2
,
3
,
0
,
1
))
'/'
,
'_'
)]
=
numpy
.
transpose
(
kernel
.
value
,
(
2
,
3
,
0
,
1
))
input_name
=
input
.
name
input_name
=
input
.
name
...
@@ -638,14 +639,14 @@ class TFOpMapper(OpMapper):
...
@@ -638,14 +639,14 @@ class TFOpMapper(OpMapper):
strides
=
[
strides
[
i
]
for
i
in
[
0
,
3
,
1
,
2
]]
strides
=
[
strides
[
i
]
for
i
in
[
0
,
3
,
1
,
2
]]
dilations
=
[
dilations
[
i
]
for
i
in
[
0
,
3
,
1
,
2
]]
dilations
=
[
dilations
[
i
]
for
i
in
[
0
,
3
,
1
,
2
]]
transpose_name
=
gen_name
(
'depthwise_conv2d'
,
'transpose'
)
transpose_name
=
gen_name
(
'depthwise_conv2d'
,
'transpose'
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.transpose"
,
kernel
=
"fluid.layers.transpose"
,
inputs
=
{
"x"
:
input
.
name
},
inputs
=
{
"x"
:
input
.
name
},
outputs
=
[
transpose_name
],
outputs
=
[
transpose_name
],
perm
=
[
0
,
3
,
1
,
2
])
perm
=
[
0
,
3
,
1
,
2
])
input_name
=
transpose_name
input_name
=
transpose_name
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.conv2d"
,
kernel
=
"fluid.layers.conv2d"
,
inputs
=
{
"input"
:
input_name
},
inputs
=
{
"input"
:
input_name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -659,7 +660,7 @@ class TFOpMapper(OpMapper):
...
@@ -659,7 +660,7 @@ class TFOpMapper(OpMapper):
bias_attr
=
False
)
bias_attr
=
False
)
if
data_format
==
"NHWC"
:
if
data_format
==
"NHWC"
:
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.transpose"
,
kernel
=
"fluid.layers.transpose"
,
inputs
=
{
"x"
:
node
.
name
},
inputs
=
{
"x"
:
node
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -676,7 +677,7 @@ class TFOpMapper(OpMapper):
...
@@ -676,7 +677,7 @@ class TFOpMapper(OpMapper):
input_name
=
input
.
name
input_name
=
input
.
name
if
data_format
==
"NHWC"
:
if
data_format
==
"NHWC"
:
transpose_name
=
gen_name
(
"avg_pool"
,
"transpose"
)
transpose_name
=
gen_name
(
"avg_pool"
,
"transpose"
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.transpose"
,
kernel
=
"fluid.layers.transpose"
,
inputs
=
{
"x"
:
input
.
name
},
inputs
=
{
"x"
:
input
.
name
},
outputs
=
[
transpose_name
],
outputs
=
[
transpose_name
],
...
@@ -685,7 +686,7 @@ class TFOpMapper(OpMapper):
...
@@ -685,7 +686,7 @@ class TFOpMapper(OpMapper):
k_size
=
[
k_size
[
i
]
for
i
in
[
0
,
3
,
1
,
2
]]
k_size
=
[
k_size
[
i
]
for
i
in
[
0
,
3
,
1
,
2
]]
input_name
=
transpose_name
input_name
=
transpose_name
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.pool2d"
,
kernel
=
"fluid.layers.pool2d"
,
inputs
=
{
"input"
:
input_name
},
inputs
=
{
"input"
:
input_name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -695,7 +696,7 @@ class TFOpMapper(OpMapper):
...
@@ -695,7 +696,7 @@ class TFOpMapper(OpMapper):
pool_padding
=
string
(
pad_mode
))
pool_padding
=
string
(
pad_mode
))
if
data_format
==
"NHWC"
:
if
data_format
==
"NHWC"
:
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.transpose"
,
kernel
=
"fluid.layers.transpose"
,
inputs
=
{
"x"
:
node
.
name
},
inputs
=
{
"x"
:
node
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -705,13 +706,13 @@ class TFOpMapper(OpMapper):
...
@@ -705,13 +706,13 @@ class TFOpMapper(OpMapper):
inputs
=
[
self
.
graph
.
get_node
(
name
)
for
name
in
node
.
layer
.
input
]
inputs
=
[
self
.
graph
.
get_node
(
name
)
for
name
in
node
.
layer
.
input
]
input_names
=
[
i
.
name
for
i
in
inputs
]
input_names
=
[
i
.
name
for
i
in
inputs
]
axis
=
node
.
get_attr
(
"axis"
)
axis
=
node
.
get_attr
(
"axis"
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.stack"
,
kernel
=
"fluid.layers.stack"
,
inputs
=
{
"x"
:
input_names
},
inputs
=
{
"x"
:
input_names
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
axis
=
axis
)
axis
=
axis
)
if
len
(
node
.
out_shapes
[
0
])
==
1
:
if
len
(
node
.
out_shapes
[
0
])
==
1
:
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.reshape"
,
kernel
=
"fluid.layers.reshape"
,
inputs
=
{
"x"
:
node
.
name
},
inputs
=
{
"x"
:
node
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -725,7 +726,7 @@ class TFOpMapper(OpMapper):
...
@@ -725,7 +726,7 @@ class TFOpMapper(OpMapper):
input_name
=
input
.
name
input_name
=
input
.
name
if
len
(
shape
)
==
1
:
if
len
(
shape
)
==
1
:
if
shape
[
0
]
>
0
and
num
==
shape
[
0
]:
if
shape
[
0
]
>
0
and
num
==
shape
[
0
]:
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.unsqueeze"
,
kernel
=
"fluid.layers.unsqueeze"
,
inputs
=
{
"input"
:
input
.
name
},
inputs
=
{
"input"
:
input
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -734,7 +735,7 @@ class TFOpMapper(OpMapper):
...
@@ -734,7 +735,7 @@ class TFOpMapper(OpMapper):
axis
=
1
axis
=
1
else
:
else
:
raise
Exception
(
"Unexpected situation happend in Unpack OP"
)
raise
Exception
(
"Unexpected situation happend in Unpack OP"
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.unstack"
,
kernel
=
"fluid.layers.unstack"
,
inputs
=
{
"x"
:
input_name
},
inputs
=
{
"x"
:
input_name
},
outputs
=
[
"{}_p{}"
.
format
(
node
.
layer_name
,
i
)
for
i
in
range
(
num
)],
outputs
=
[
"{}_p{}"
.
format
(
node
.
layer_name
,
i
)
for
i
in
range
(
num
)],
...
@@ -753,19 +754,19 @@ class TFOpMapper(OpMapper):
...
@@ -753,19 +754,19 @@ class TFOpMapper(OpMapper):
for
i
,
ipt
in
enumerate
(
inputs
):
for
i
,
ipt
in
enumerate
(
inputs
):
if
ipt
.
dtype
==
'bool'
:
if
ipt
.
dtype
==
'bool'
:
cast_name
=
gen_name
(
'concat'
,
'cast'
)
cast_name
=
gen_name
(
'concat'
,
'cast'
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.cast"
,
kernel
=
"fluid.layers.cast"
,
inputs
=
{
"x"
:
ipt
.
name
},
inputs
=
{
"x"
:
ipt
.
name
},
outputs
=
[
cast_name
],
outputs
=
[
cast_name
],
dtype
=
"'int32'"
)
dtype
=
"'int32'"
)
input_names
[
i
]
=
cast_name
input_names
[
i
]
=
cast_name
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.concat"
,
kernel
=
"fluid.layers.concat"
,
inputs
=
{
"input"
:
input_names
},
inputs
=
{
"input"
:
input_names
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
axis
=
axis
)
axis
=
axis
)
if
node
.
dtype
==
'bool'
:
if
node
.
dtype
==
'bool'
:
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.cast"
,
kernel
=
"fluid.layers.cast"
,
inputs
=
{
"x"
:
node
.
name
},
inputs
=
{
"x"
:
node
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -838,7 +839,7 @@ class TFOpMapper(OpMapper):
...
@@ -838,7 +839,7 @@ class TFOpMapper(OpMapper):
else
:
else
:
new_end
.
append
(
end
[
i
])
new_end
.
append
(
end
[
i
])
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.slice"
,
kernel
=
"fluid.layers.slice"
,
inputs
=
{
"input"
:
input
.
name
},
inputs
=
{
"input"
:
input
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -846,7 +847,7 @@ class TFOpMapper(OpMapper):
...
@@ -846,7 +847,7 @@ class TFOpMapper(OpMapper):
starts
=
new_begin
,
starts
=
new_begin
,
ends
=
new_end
)
ends
=
new_end
)
if
len
(
new_axes
)
>
0
:
if
len
(
new_axes
)
>
0
:
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.unsqueeze"
,
kernel
=
"fluid.layers.unsqueeze"
,
inputs
=
{
"input"
:
node
.
name
},
inputs
=
{
"input"
:
node
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -855,7 +856,7 @@ class TFOpMapper(OpMapper):
...
@@ -855,7 +856,7 @@ class TFOpMapper(OpMapper):
if
len
(
input
.
out_shapes
[
0
])
+
len
(
new_axes
)
<=
1
:
if
len
(
input
.
out_shapes
[
0
])
+
len
(
new_axes
)
<=
1
:
pass
pass
else
:
else
:
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.squeeze"
,
kernel
=
"fluid.layers.squeeze"
,
inputs
=
{
"input"
:
node
.
name
},
inputs
=
{
"input"
:
node
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -868,7 +869,7 @@ class TFOpMapper(OpMapper):
...
@@ -868,7 +869,7 @@ class TFOpMapper(OpMapper):
num_split
=
node
.
get_attr
(
'num_split'
)
num_split
=
node
.
get_attr
(
'num_split'
)
dim
=
dim
.
value
dim
=
dim
.
value
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.split"
,
kernel
=
"fluid.layers.split"
,
inputs
=
{
"input"
:
input
.
name
},
inputs
=
{
"input"
:
input
.
name
},
outputs
=
[
outputs
=
[
...
@@ -890,7 +891,7 @@ class TFOpMapper(OpMapper):
...
@@ -890,7 +891,7 @@ class TFOpMapper(OpMapper):
else
:
else
:
# shape = begin.out_shapes[0]
# shape = begin.out_shapes[0]
# reshape_name = gen_name("slice", "reshape")
# reshape_name = gen_name("slice", "reshape")
#
program
.add_layer(
#
self.paddle_graph
.add_layer(
# kernel="fluid.layers.reshape",
# kernel="fluid.layers.reshape",
# inputs={"x": begin.name},
# inputs={"x": begin.name},
# outputs=[reshape_name],
# outputs=[reshape_name],
...
@@ -904,13 +905,13 @@ class TFOpMapper(OpMapper):
...
@@ -904,13 +905,13 @@ class TFOpMapper(OpMapper):
else
:
else
:
shape
=
size
.
out_shapes
[
0
]
shape
=
size
.
out_shapes
[
0
]
reshape_name
=
gen_name
(
"slice"
,
"reshape"
)
reshape_name
=
gen_name
(
"slice"
,
"reshape"
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.reshape"
,
kernel
=
"fluid.layers.reshape"
,
inputs
=
{
"x"
:
size
.
name
},
inputs
=
{
"x"
:
size
.
name
},
outputs
=
[
reshape_name
],
outputs
=
[
reshape_name
],
shape
=
shape
)
shape
=
shape
)
inputs
[
'shape'
]
=
reshape_name
inputs
[
'shape'
]
=
reshape_name
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.crop_tensor"
,
kernel
=
"fluid.layers.crop_tensor"
,
inputs
=
inputs
,
inputs
=
inputs
,
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -929,7 +930,7 @@ class TFOpMapper(OpMapper):
...
@@ -929,7 +930,7 @@ class TFOpMapper(OpMapper):
else
:
else
:
shape
=
resize_shape
.
out_shapes
[
0
]
shape
=
resize_shape
.
out_shapes
[
0
]
reshape_name
=
gen_name
(
"resize_nearest"
,
"reshape"
)
reshape_name
=
gen_name
(
"resize_nearest"
,
"reshape"
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.reshape"
,
kernel
=
"fluid.layers.reshape"
,
inputs
=
{
"x"
:
resize_shape
.
name
},
inputs
=
{
"x"
:
resize_shape
.
name
},
outputs
=
[
reshape_name
],
outputs
=
[
reshape_name
],
...
@@ -938,21 +939,21 @@ class TFOpMapper(OpMapper):
...
@@ -938,21 +939,21 @@ class TFOpMapper(OpMapper):
if
data_format
==
"NHWC"
:
if
data_format
==
"NHWC"
:
transpose_name
=
gen_name
(
"resize_nearest"
,
"reshape"
)
transpose_name
=
gen_name
(
"resize_nearest"
,
"reshape"
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.transpose"
,
kernel
=
"fluid.layers.transpose"
,
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
[
"input"
]
=
transpose_name
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.resize_nearest"
,
kernel
=
"fluid.layers.resize_nearest"
,
inputs
=
inputs
,
inputs
=
inputs
,
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
**
attrs
)
**
attrs
)
if
data_format
==
"NHWC"
:
if
data_format
==
"NHWC"
:
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.transpose"
,
kernel
=
"fluid.layers.transpose"
,
inputs
=
{
"x"
:
node
.
name
},
inputs
=
{
"x"
:
node
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -971,7 +972,7 @@ class TFOpMapper(OpMapper):
...
@@ -971,7 +972,7 @@ class TFOpMapper(OpMapper):
else
:
else
:
shape
=
resize_shape
.
out_shapes
[
0
]
shape
=
resize_shape
.
out_shapes
[
0
]
reshape_name
=
gen_name
(
"resize_bilinear"
,
"reshape"
)
reshape_name
=
gen_name
(
"resize_bilinear"
,
"reshape"
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.reshape"
,
kernel
=
"fluid.layers.reshape"
,
inputs
=
{
"x"
:
resize_shape
.
name
},
inputs
=
{
"x"
:
resize_shape
.
name
},
outputs
=
[
reshape_name
],
outputs
=
[
reshape_name
],
...
@@ -980,21 +981,21 @@ class TFOpMapper(OpMapper):
...
@@ -980,21 +981,21 @@ class TFOpMapper(OpMapper):
if
data_format
==
"NHWC"
:
if
data_format
==
"NHWC"
:
transpose_name
=
gen_name
(
"resize_bilinear"
,
"reshape"
)
transpose_name
=
gen_name
(
"resize_bilinear"
,
"reshape"
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.transpose"
,
kernel
=
"fluid.layers.transpose"
,
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
[
"input"
]
=
transpose_name
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.resize_bilinear"
,
kernel
=
"fluid.layers.resize_bilinear"
,
inputs
=
inputs
,
inputs
=
inputs
,
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
**
attrs
)
**
attrs
)
if
data_format
==
"NHWC"
:
if
data_format
==
"NHWC"
:
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.transpose"
,
kernel
=
"fluid.layers.transpose"
,
inputs
=
{
"x"
:
node
.
name
},
inputs
=
{
"x"
:
node
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -1003,7 +1004,7 @@ class TFOpMapper(OpMapper):
...
@@ -1003,7 +1004,7 @@ class TFOpMapper(OpMapper):
def
Cast
(
self
,
node
):
def
Cast
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
])
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
])
dtype
=
node
.
dtype
dtype
=
node
.
dtype
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.cast"
,
kernel
=
"fluid.layers.cast"
,
inputs
=
{
"x"
:
input
.
name
},
inputs
=
{
"x"
:
input
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -1016,7 +1017,7 @@ class TFOpMapper(OpMapper):
...
@@ -1016,7 +1017,7 @@ class TFOpMapper(OpMapper):
keep_dims
=
node
.
get_attr
(
"keep_dims"
)
keep_dims
=
node
.
get_attr
(
"keep_dims"
)
dim
=
reduce_idx
.
value
.
tolist
()
dim
=
reduce_idx
.
value
.
tolist
()
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.reduce_sum"
,
kernel
=
"fluid.layers.reduce_sum"
,
inputs
=
{
"input"
:
input
.
name
},
inputs
=
{
"input"
:
input
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -1029,7 +1030,7 @@ class TFOpMapper(OpMapper):
...
@@ -1029,7 +1030,7 @@ class TFOpMapper(OpMapper):
assert
reduce_idx
.
layer_type
==
"Const"
,
"Only support Const parameter[reduce_idx]"
assert
reduce_idx
.
layer_type
==
"Const"
,
"Only support Const parameter[reduce_idx]"
keep_dims
=
node
.
get_attr
(
"keep_dims"
)
keep_dims
=
node
.
get_attr
(
"keep_dims"
)
dim
=
reduce_idx
.
value
.
tolist
()
dim
=
reduce_idx
.
value
.
tolist
()
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.reduce_max"
,
kernel
=
"fluid.layers.reduce_max"
,
inputs
=
{
"input"
:
input
.
name
},
inputs
=
{
"input"
:
input
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -1040,7 +1041,7 @@ class TFOpMapper(OpMapper):
...
@@ -1040,7 +1041,7 @@ class TFOpMapper(OpMapper):
shape
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
])
shape
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
])
if
shape
.
layer_type
==
"Const"
:
if
shape
.
layer_type
==
"Const"
:
shape
=
shape
.
value
.
tolist
()
shape
=
shape
.
value
.
tolist
()
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.uniform_random"
,
kernel
=
"fluid.layers.uniform_random"
,
inputs
=
{},
inputs
=
{},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -1048,7 +1049,7 @@ class TFOpMapper(OpMapper):
...
@@ -1048,7 +1049,7 @@ class TFOpMapper(OpMapper):
min
=
0.0
,
min
=
0.0
,
max
=
0.9999
)
max
=
0.9999
)
else
:
else
:
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.uniform_random"
,
kernel
=
"fluid.layers.uniform_random"
,
inputs
=
{
'shape'
:
shape
.
name
},
inputs
=
{
'shape'
:
shape
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -1080,7 +1081,7 @@ class TFOpMapper(OpMapper):
...
@@ -1080,7 +1081,7 @@ class TFOpMapper(OpMapper):
dilations
=
node
.
get_attr
(
"dilations"
)
dilations
=
node
.
get_attr
(
"dilations"
)
data_format
=
node
.
get_attr
(
"data_format"
).
decode
()
data_format
=
node
.
get_attr
(
"data_format"
).
decode
()
program
.
parameter
s
[
kernel
.
layer_name
.
replace
(
self
.
param
s
[
kernel
.
layer_name
.
replace
(
'/'
,
'_'
)]
=
numpy
.
transpose
(
kernel
.
value
,
(
3
,
2
,
0
,
1
))
'/'
,
'_'
)]
=
numpy
.
transpose
(
kernel
.
value
,
(
3
,
2
,
0
,
1
))
input_name
=
input
.
name
input_name
=
input
.
name
...
@@ -1089,14 +1090,14 @@ class TFOpMapper(OpMapper):
...
@@ -1089,14 +1090,14 @@ class TFOpMapper(OpMapper):
strides
=
[
strides
[
i
]
for
i
in
[
0
,
3
,
1
,
2
]]
strides
=
[
strides
[
i
]
for
i
in
[
0
,
3
,
1
,
2
]]
dilations
=
[
dilations
[
i
]
for
i
in
[
0
,
3
,
1
,
2
]]
dilations
=
[
dilations
[
i
]
for
i
in
[
0
,
3
,
1
,
2
]]
transpose_name
=
gen_name
(
"conv2dbackpropinput"
,
"transpose"
)
transpose_name
=
gen_name
(
"conv2dbackpropinput"
,
"transpose"
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.transpose"
,
kernel
=
"fluid.layers.transpose"
,
inputs
=
{
"x"
:
input
.
name
},
inputs
=
{
"x"
:
input
.
name
},
outputs
=
[
transpose_name
],
outputs
=
[
transpose_name
],
perm
=
[
0
,
3
,
1
,
2
])
perm
=
[
0
,
3
,
1
,
2
])
input_name
=
transpose_name
input_name
=
transpose_name
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.conv2d_transpose"
,
kernel
=
"fluid.layers.conv2d_transpose"
,
inputs
=
{
"input"
:
input_name
},
inputs
=
{
"input"
:
input_name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -1110,7 +1111,7 @@ class TFOpMapper(OpMapper):
...
@@ -1110,7 +1111,7 @@ class TFOpMapper(OpMapper):
output_size
=
out_shape
[
1
:
3
])
output_size
=
out_shape
[
1
:
3
])
if
data_format
==
"NHWC"
:
if
data_format
==
"NHWC"
:
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.transpose"
,
kernel
=
"fluid.layers.transpose"
,
inputs
=
{
"x"
:
node
.
name
},
inputs
=
{
"x"
:
node
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -1127,7 +1128,7 @@ class TFOpMapper(OpMapper):
...
@@ -1127,7 +1128,7 @@ class TFOpMapper(OpMapper):
else
:
else
:
inputs
[
"expand_times"
]
=
expand_times
.
name
inputs
[
"expand_times"
]
=
expand_times
.
name
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.expand"
,
kernel
=
"fluid.layers.expand"
,
inputs
=
inputs
,
inputs
=
inputs
,
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -1162,7 +1163,7 @@ class TFOpMapper(OpMapper):
...
@@ -1162,7 +1163,7 @@ class TFOpMapper(OpMapper):
node
.
set_dtype
(
dtype
)
node
.
set_dtype
(
dtype
)
attr
[
"dtype"
]
=
string
(
node
.
dtype
)
attr
[
"dtype"
]
=
string
(
node
.
dtype
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.range"
,
kernel
=
"fluid.layers.range"
,
inputs
=
inputs
,
inputs
=
inputs
,
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -1174,16 +1175,16 @@ class TFOpMapper(OpMapper):
...
@@ -1174,16 +1175,16 @@ class TFOpMapper(OpMapper):
inputs
=
{
"x"
:
x
.
name
,
"y"
:
y
.
name
}
inputs
=
{
"x"
:
x
.
name
,
"y"
:
y
.
name
}
x_shape
=
x
.
out_shapes
[
0
]
x_shape
=
x
.
out_shapes
[
0
]
y_shape
=
y
.
out_shapes
[
0
]
y_shape
=
y
.
out_shapes
[
0
]
layer_id
=
program
.
add_layer
(
layer_id
=
self
.
paddle_graph
.
add_layer
(
"fluid.layers.elementwise_sub"
,
inputs
=
inputs
,
outputs
=
[
node
.
name
])
"fluid.layers.elementwise_sub"
,
inputs
=
inputs
,
outputs
=
[
node
.
name
])
program
.
layers
[
layer_id
].
input_shapes
=
{
"x"
:
x_shape
,
"y"
:
y_shape
}
self
.
paddle_graph
.
layers
[
layer_id
].
input_shapes
=
{
"x"
:
x_shape
,
"y"
:
y_shape
}
inputs
=
{
"x"
:
node
.
name
,
"y"
:
node
.
name
}
inputs
=
{
"x"
:
node
.
name
,
"y"
:
node
.
name
}
x_shape
=
node
.
out_shapes
[
0
]
x_shape
=
node
.
out_shapes
[
0
]
y_shape
=
node
.
out_shapes
[
0
]
y_shape
=
node
.
out_shapes
[
0
]
layer_id
=
program
.
add_layer
(
layer_id
=
self
.
paddle_graph
.
add_layer
(
"fluid.layers.elementwise_mul"
,
inputs
=
inputs
,
outputs
=
[
node
.
name
])
"fluid.layers.elementwise_mul"
,
inputs
=
inputs
,
outputs
=
[
node
.
name
])
program
.
layers
[
layer_id
].
input_shapes
=
{
"x"
:
x_shape
,
"y"
:
y_shape
}
self
.
paddle_graph
.
layers
[
layer_id
].
input_shapes
=
{
"x"
:
x_shape
,
"y"
:
y_shape
}
def
OneHot
(
self
,
node
):
def
OneHot
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
])
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
])
...
@@ -1202,7 +1203,7 @@ class TFOpMapper(OpMapper):
...
@@ -1202,7 +1203,7 @@ class TFOpMapper(OpMapper):
assert
math
.
fabs
(
off_value
-
assert
math
.
fabs
(
off_value
-
0.0
)
<
1e-06
,
"off_value should be 0 in OneHot"
0.0
)
<
1e-06
,
"off_value should be 0 in OneHot"
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
"fluid.one_hot"
,
"fluid.one_hot"
,
inputs
=
{
"input"
:
input
.
name
},
inputs
=
{
"input"
:
input
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -1217,7 +1218,7 @@ class TFOpMapper(OpMapper):
...
@@ -1217,7 +1218,7 @@ class TFOpMapper(OpMapper):
attr
[
"factor"
]
=
factor
.
value
.
tolist
()
attr
[
"factor"
]
=
factor
.
value
.
tolist
()
else
:
else
:
inputs
[
"factor"
]
=
factor
.
name
inputs
[
"factor"
]
=
factor
.
name
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
"fluid.layers.pow"
,
inputs
=
inputs
,
outputs
=
[
node
.
name
],
**
attr
)
"fluid.layers.pow"
,
inputs
=
inputs
,
outputs
=
[
node
.
name
],
**
attr
)
def
All
(
self
,
node
):
def
All
(
self
,
node
):
...
@@ -1231,12 +1232,12 @@ class TFOpMapper(OpMapper):
...
@@ -1231,12 +1232,12 @@ class TFOpMapper(OpMapper):
input_name
=
input
.
name
input_name
=
input
.
name
if
input
.
dtype
!=
"bool"
:
if
input
.
dtype
!=
"bool"
:
input_name
=
gen_name
(
"all"
,
"cast"
)
input_name
=
gen_name
(
"all"
,
"cast"
)
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
"fluid.layers.cast"
,
"fluid.layers.cast"
,
inputs
=
{
"x"
:
input
.
name
},
inputs
=
{
"x"
:
input
.
name
},
outputs
=
[
input_name
],
outputs
=
[
input_name
],
dtype
=
string
(
"bool"
))
dtype
=
string
(
"bool"
))
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
"fluid.layers.reduce_all"
,
"fluid.layers.reduce_all"
,
inputs
=
{
"input"
:
input_name
},
inputs
=
{
"input"
:
input_name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -1255,20 +1256,20 @@ class TFOpMapper(OpMapper):
...
@@ -1255,20 +1256,20 @@ class TFOpMapper(OpMapper):
if
len
(
index
.
out_shapes
[
0
])
!=
1
:
if
len
(
index
.
out_shapes
[
0
])
!=
1
:
reshape_name
=
gen_name
(
"gather"
,
"reshape"
)
reshape_name
=
gen_name
(
"gather"
,
"reshape"
)
index_name
=
reshape_name
index_name
=
reshape_name
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
"fluid.layers.reshape"
,
"fluid.layers.reshape"
,
inputs
=
{
"x"
:
index
.
name
},
inputs
=
{
"x"
:
index
.
name
},
outputs
=
[
reshape_name
],
outputs
=
[
reshape_name
],
shape
=
[
-
1
])
shape
=
[
-
1
])
inputs
=
{
'input'
:
embeddings
.
name
,
'index'
:
index_name
}
inputs
=
{
'input'
:
embeddings
.
name
,
'index'
:
index_name
}
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
"fluid.layers.gather"
,
"fluid.layers.gather"
,
inputs
=
inputs
,
inputs
=
inputs
,
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
overwrite
=
False
)
overwrite
=
False
)
if
len
(
index
.
out_shapes
[
0
])
!=
1
:
if
len
(
index
.
out_shapes
[
0
])
!=
1
:
out_shape
=
node
.
out_shapes
[
0
]
out_shape
=
node
.
out_shapes
[
0
]
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.reshape"
,
kernel
=
"fluid.layers.reshape"
,
inputs
=
{
"x"
:
node
.
name
},
inputs
=
{
"x"
:
node
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
@@ -1286,7 +1287,7 @@ class TFOpMapper(OpMapper):
...
@@ -1286,7 +1287,7 @@ class TFOpMapper(OpMapper):
attr
[
'axes'
]
=
dim
attr
[
'axes'
]
=
dim
else
:
else
:
inputs
[
'axes'
]
=
y
.
name
inputs
[
'axes'
]
=
y
.
name
program
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
"fluid.layers.unsqueeze"
,
"fluid.layers.unsqueeze"
,
inputs
=
inputs
,
inputs
=
inputs
,
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
...
...
x2paddle/optimizer/fusion/static/__pycache__/__init__.cpython-37.pyc
浏览文件 @
01ac9740
无法预览此类型文件
x2paddle/optimizer/fusion/static/__pycache__/bn_scale_fuse_pass.cpython-37.pyc
浏览文件 @
01ac9740
无法预览此类型文件
x2paddle/optimizer/fusion/static/__pycache__/bn_scale_fuser.cpython-37.pyc
浏览文件 @
01ac9740
无法预览此类型文件
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录