Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
X2Paddle
提交
d248ff64
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看板
提交
d248ff64
编写于
8月 01, 2019
作者:
J
jiangjiajun
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix code
上级
13e73084
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
82 addition
and
136 deletion
+82
-136
x2paddle/core/fluid_code.py
x2paddle/core/fluid_code.py
+28
-16
x2paddle/decoder/tf_decoder.py
x2paddle/decoder/tf_decoder.py
+3
-2
x2paddle/op_mapper/tf_op_mapper.py
x2paddle/op_mapper/tf_op_mapper.py
+51
-118
未找到文件。
x2paddle/core/fluid_code.py
浏览文件 @
d248ff64
...
@@ -13,6 +13,7 @@
...
@@ -13,6 +13,7 @@
# limitations under the License.
# limitations under the License.
from
x2paddle.core.graph
import
GraphNode
from
x2paddle.core.graph
import
GraphNode
import
collections
class
Layer
(
object
):
class
Layer
(
object
):
...
@@ -36,25 +37,34 @@ class Layer(object):
...
@@ -36,25 +37,34 @@ class Layer(object):
if
isinstance
(
self
.
inputs
,
list
):
if
isinstance
(
self
.
inputs
,
list
):
in_list
=
"["
in_list
=
"["
for
input
in
self
.
inputs
:
for
input
in
self
.
inputs
:
assert
isinstance
(
if
isinstance
(
input
,
GraphNode
):
input
,
GraphNode
),
"Type of input should be GraphNode"
if
hasattr
(
input
,
"index"
):
if
hasattr
(
input
,
"index"
):
in_list
+=
(
input
.
layer_name
+
"[{}]"
.
format
(
input
.
index
)
+
in_list
+=
(
input
.
layer_name
+
", "
)
"[{}]"
.
format
(
input
.
index
)
+
", "
)
else
:
else
:
in_list
+=
(
input
.
layer_name
+
", "
)
in_list
+=
(
input
.
layer_name
+
", "
)
elif
isinstance
(
input
,
str
):
in_list
+=
(
input
+
", "
)
else
:
raise
Exception
(
"Element of inputs should GraphNode or String"
)
in_list
=
in_list
.
strip
(
", "
)
+
"], "
in_list
=
in_list
.
strip
(
", "
)
+
"], "
layer_code
+=
in_list
layer_code
+=
in_list
elif
isinstance
(
self
.
inputs
,
dict
):
elif
isinstance
(
self
.
inputs
,
dict
):
for
key
,
input
in
self
.
inputs
.
items
():
inputs
=
collections
.
OrderedDict
(
self
.
inputs
)
assert
isinstance
(
for
key
,
input
in
inputs
.
items
():
input
,
GraphNode
),
"Type of input should be GraphNode"
if
isinstance
(
input
,
GraphNode
):
if
hasattr
(
input
,
"index"
):
if
hasattr
(
input
,
"index"
):
layer_code
=
layer_code
+
key
+
"={}, "
.
format
(
layer_code
=
layer_code
+
key
+
"={}, "
.
format
(
input
.
layer_name
+
"[{}]"
.
format
(
input
.
index
))
input
.
layer_name
+
"[{}]"
.
format
(
input
.
index
))
else
:
else
:
layer_code
=
layer_code
+
key
+
"={}, "
.
format
(
layer_code
=
layer_code
+
key
+
"={}, "
.
format
(
input
.
layer_name
)
input
.
layer_name
)
elif
isinstance
(
input
,
str
):
layer_code
=
layer_code
+
key
+
"={}, "
.
format
(
input
)
else
:
raise
Exception
(
"Element of inputs should GraphNode or String"
)
elif
isinstance
(
self
.
inputs
,
GraphNode
):
elif
isinstance
(
self
.
inputs
,
GraphNode
):
if
hasattr
(
self
.
inputs
,
"index"
):
if
hasattr
(
self
.
inputs
,
"index"
):
layer_code
+=
(
self
.
inputs
.
layer_name
+
layer_code
+=
(
self
.
inputs
.
layer_name
+
...
@@ -66,7 +76,8 @@ class Layer(object):
...
@@ -66,7 +76,8 @@ class Layer(object):
else
:
else
:
raise
Exception
(
"Unknown type of inputs."
)
raise
Exception
(
"Unknown type of inputs."
)
for
key
,
value
in
self
.
param_attr
.
items
():
param_attr
=
collections
.
OrderedDict
(
self
.
param_attr
)
for
key
,
value
in
param_attr
.
items
():
layer_code
=
layer_code
+
key
+
"={}, "
.
format
(
value
)
layer_code
=
layer_code
+
key
+
"={}, "
.
format
(
value
)
layer_code
=
layer_code
.
strip
(
", "
)
layer_code
=
layer_code
.
strip
(
", "
)
...
@@ -97,7 +108,8 @@ class Layer(object):
...
@@ -97,7 +108,8 @@ class Layer(object):
else
:
else
:
raise
Exception
(
"Unknown type of inputs."
)
raise
Exception
(
"Unknown type of inputs."
)
for
key
,
value
in
self
.
param_attr
.
items
():
param_attr
=
collections
.
OrderedDict
(
self
.
param_attr
)
for
key
,
value
in
param_attr
.
items
():
layer_code
=
layer_code
+
key
+
"={}, "
.
format
(
value
)
layer_code
=
layer_code
+
key
+
"={}, "
.
format
(
value
)
layer_code
=
layer_code
.
strip
(
", "
)
layer_code
=
layer_code
.
strip
(
", "
)
...
...
x2paddle/decoder/tf_decoder.py
浏览文件 @
d248ff64
...
@@ -176,8 +176,9 @@ class TFDecoder(object):
...
@@ -176,8 +176,9 @@ class TFDecoder(object):
self
.
sess
.
graph
.
as_default
()
self
.
sess
.
graph
.
as_default
()
tf
.
import_graph_def
(
graph_def
,
name
=
''
,
input_map
=
input_map
)
tf
.
import_graph_def
(
graph_def
,
name
=
''
,
input_map
=
input_map
)
for
node
in
graph_def
.
node
:
print
(
node
.
name
,
node
.
op
,
node
.
input
)
# for node in graph_def.node:
# print(node.name, node.op, node.input)
self
.
sess
.
run
(
tf
.
global_variables_initializer
())
self
.
sess
.
run
(
tf
.
global_variables_initializer
())
...
...
x2paddle/op_mapper/tf_op_mapper.py
浏览文件 @
d248ff64
...
@@ -19,6 +19,31 @@ import numpy
...
@@ -19,6 +19,31 @@ import numpy
class
TFOpMapper
(
OpMapper
):
class
TFOpMapper
(
OpMapper
):
directly_map_ops
=
{
'Relu'
:
[
'relu'
],
'Relu6'
:
[
'relu6'
],
'Shape'
:
[
'shape'
],
'Abs'
:
[
'abs'
],
'Sigmoid'
:
[
'sigmoid'
],
'Exp'
:
[
'exp'
],
'Rsqrt'
:
[
'rsqrt'
],
'Squeeze'
:
[
'squeeze'
,
{
'squeeze_dims'
:
'axes'
}],
'Softmax'
:
[
'softmax'
,
{
'axis'
:
'axis'
}],
}
elementwise_ops
=
{
'Add'
:
'elementwise_add'
,
'RealDiv'
:
'elementwise_div'
,
'BiasAdd'
:
'elementwise_add'
,
'Sub'
:
'elementwise_sub'
,
'Maximum'
:
'elementwise_max'
,
'Mul'
:
'elementwise_mul'
}
def
__init__
(
self
,
decoder
):
def
__init__
(
self
,
decoder
):
super
(
TFOpMapper
,
self
).
__init__
()
super
(
TFOpMapper
,
self
).
__init__
()
self
.
decoder
=
decoder
self
.
decoder
=
decoder
...
@@ -30,15 +55,20 @@ class TFOpMapper(OpMapper):
...
@@ -30,15 +55,20 @@ class TFOpMapper(OpMapper):
print
(
"Total nodes: {}"
.
format
(
len
(
self
.
graph
.
topo_sort
)))
print
(
"Total nodes: {}"
.
format
(
len
(
self
.
graph
.
topo_sort
)))
# check if ops in model are all supported
# check if ops in model are all supported
if
not
self
.
op_checker
():
# TODO
raise
Exception
(
"Model are not supported yet."
)
for
node_name
in
self
.
graph
.
topo_sort
:
for
node_name
in
self
.
graph
.
topo_sort
:
node
=
self
.
graph
.
get_node
(
node_name
)
node
=
self
.
graph
.
get_node
(
node_name
)
op
=
node
.
layer_type
op
=
node
.
layer_type
if
hasattr
(
self
,
op
):
if
op
in
self
.
directly_map_ops
:
self
.
directly_map
(
node
)
elif
op
in
self
.
elementwise_ops
:
self
.
elementwise_map
(
node
)
elif
hasattr
(
self
,
op
):
func
=
getattr
(
self
,
op
)
func
=
getattr
(
self
,
op
)
func
(
node
)
func
(
node
)
else
:
raise
Exception
(
"OP: [{}] not support yet"
.
format
(
op
))
for
i
in
range
(
len
(
self
.
graph
.
topo_sort
)):
for
i
in
range
(
len
(
self
.
graph
.
topo_sort
)):
node_name
=
self
.
graph
.
topo_sort
[
i
]
node_name
=
self
.
graph
.
topo_sort
[
i
]
...
@@ -47,7 +77,24 @@ class TFOpMapper(OpMapper):
...
@@ -47,7 +77,24 @@ class TFOpMapper(OpMapper):
node
=
self
.
graph
.
get_node
(
node_name
)
node
=
self
.
graph
.
get_node
(
node_name
)
self
.
net_code
+=
node
.
fluid_code
.
gen_codes
()
self
.
net_code
+=
node
.
fluid_code
.
gen_codes
()
def
elementwise_operator
(
self
,
node
,
op_type
):
def
directly_map
(
self
,
node
):
assert
node
.
layer_type
in
self
.
directly_map_ops
op_info
=
self
.
directly_map_ops
[
node
.
layer_type
]
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
attr
=
dict
()
for
param
in
op_info
[
1
:]:
tf_param_name
=
list
(
param
.
keys
())[
0
]
pd_param_name
=
list
(
param
.
values
())[
0
]
tf_param
=
node
.
get_attr
(
tf_param_name
)
attr
[
pd_param_name
]
=
tf_param
node
.
fluid_code
.
add_layer
(
op_info
[
0
],
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
elementwise_map
(
self
,
node
):
assert
node
.
layer_type
in
self
.
elementwise_ops
op_type
=
self
.
elementwise_ops
[
node
.
layer_type
]
x
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
x
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
y
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
],
copy
=
True
)
y
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
],
copy
=
True
)
x_shape
=
x
.
out_shapes
[
0
]
x_shape
=
x
.
out_shapes
[
0
]
...
@@ -161,41 +208,6 @@ class TFOpMapper(OpMapper):
...
@@ -161,41 +208,6 @@ class TFOpMapper(OpMapper):
output
=
node
,
output
=
node
,
param_attr
=
attr
)
param_attr
=
attr
)
def
RealDiv
(
self
,
node
):
self
.
elementwise_operator
(
node
,
"elementwise_div"
)
def
Relu
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
node
.
fluid_code
.
add_layer
(
"relu"
,
inputs
=
input
,
output
=
node
,
param_attr
=
None
)
def
Squeeze
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
squeeze_dims
=
node
.
get_attr
(
'squeeze_dims'
)
attr
=
{
'axes'
:
squeeze_dims
}
node
.
fluid_code
.
add_layer
(
"squeeze"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
BiasAdd
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
bias
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
],
copy
=
True
)
inputs
=
{
'x'
:
input
,
'y'
:
bias
}
node
.
fluid_code
.
add_layer
(
"elementwise_add"
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
None
)
def
Identity
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
node
.
fluid_code
.
add_layer
(
"assign"
,
inputs
=
input
,
output
=
node
,
param_attr
=
None
)
def
MaxPool
(
self
,
node
):
def
MaxPool
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
...
@@ -314,13 +326,6 @@ class TFOpMapper(OpMapper):
...
@@ -314,13 +326,6 @@ class TFOpMapper(OpMapper):
output
=
node
,
output
=
node
,
param_attr
=
attr
)
param_attr
=
attr
)
def
Relu6
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
node
.
fluid_code
.
add_layer
(
"relu6"
,
inputs
=
input
,
output
=
node
,
param_attr
=
None
)
def
FusedBatchNorm
(
self
,
node
):
def
FusedBatchNorm
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
gamma
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
],
copy
=
True
)
gamma
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
],
copy
=
True
)
...
@@ -433,13 +438,6 @@ class TFOpMapper(OpMapper):
...
@@ -433,13 +438,6 @@ class TFOpMapper(OpMapper):
output
=
node
,
output
=
node
,
param_attr
=
attr
)
param_attr
=
attr
)
def
Shape
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
node
.
fluid_code
.
add_layer
(
"shape"
,
inputs
=
input
,
output
=
node
,
param_attr
=
None
)
def
Reshape
(
self
,
node
):
def
Reshape
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
param
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
],
copy
=
True
)
param
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
],
copy
=
True
)
...
@@ -474,27 +472,6 @@ class TFOpMapper(OpMapper):
...
@@ -474,27 +472,6 @@ class TFOpMapper(OpMapper):
inputs
=
input
,
inputs
=
input
,
output
=
node
,
output
=
node
,
param_attr
=
attr
)
param_attr
=
attr
)
# temporary shape inference fix
# if param.layer_type == "Pack":
# shape_slices = list()
# for i in range(len(param.layer.input)):
# slice = self.graph.get_node(param.layer.input[i], copy=True)
# if slice.layer_type == "Const":
# shape_slices.append(slice.value.tolist())
# else:
# shape_slices.append(0)
# if shape_slices.count(-1) == 0:
# shape_slices[shape_slices.index(0)] = -1
# attr = {"shape": shape_slices}
# node.fluid_code.add_layer("reshape",
# inputs=node,
# output=node,
# param_attr=attr)
def
Add
(
self
,
node
):
self
.
elementwise_operator
(
node
,
"elementwise_add"
)
def
AvgPool
(
self
,
node
):
def
AvgPool
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
...
@@ -542,23 +519,6 @@ class TFOpMapper(OpMapper):
...
@@ -542,23 +519,6 @@ class TFOpMapper(OpMapper):
output
=
node
,
output
=
node
,
param_attr
=
attr
)
param_attr
=
attr
)
def
Softmax
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
node
.
fluid_code
.
add_layer
(
"softmax"
,
inputs
=
input
,
output
=
node
,
param_attr
=
None
)
def
Sigmoid
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
node
.
fluid_code
.
add_layer
(
"sigmoid"
,
inputs
=
input
,
output
=
node
,
param_attr
=
None
)
def
Maximum
(
self
,
node
):
self
.
elementwise_operator
(
node
,
"elementwise_max"
)
def
SplitV
(
self
,
node
):
def
SplitV
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
num_sections
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
],
copy
=
True
)
num_sections
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
],
copy
=
True
)
...
@@ -576,13 +536,6 @@ class TFOpMapper(OpMapper):
...
@@ -576,13 +536,6 @@ class TFOpMapper(OpMapper):
output
=
node
,
output
=
node
,
param_attr
=
attr
)
param_attr
=
attr
)
def
Exp
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
node
.
fluid_code
.
add_layer
(
"exp"
,
inputs
=
input
,
output
=
node
,
param_attr
=
None
)
def
ConcatV2
(
self
,
node
):
def
ConcatV2
(
self
,
node
):
inputs
=
[
inputs
=
[
self
.
graph
.
get_node
(
name
,
copy
=
True
)
self
.
graph
.
get_node
(
name
,
copy
=
True
)
...
@@ -649,19 +602,6 @@ class TFOpMapper(OpMapper):
...
@@ -649,19 +602,6 @@ class TFOpMapper(OpMapper):
output
=
node
,
output
=
node
,
param_attr
=
None
)
param_attr
=
None
)
def
Mul
(
self
,
node
):
self
.
elementwise_operator
(
node
,
"elementwise_mul"
)
def
Sub
(
self
,
node
):
self
.
elementwise_operator
(
node
,
"elementwise_sub"
)
def
Rsqrt
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
node
.
fluid_code
.
add_layer
(
"rsqrt"
,
inputs
=
input
,
output
=
node
,
param_attr
=
None
)
def
swish_f32
(
self
,
node
):
def
swish_f32
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
node
.
fluid_code
.
add_layer
(
"sigmoid"
,
node
.
fluid_code
.
add_layer
(
"sigmoid"
,
...
@@ -765,13 +705,6 @@ class TFOpMapper(OpMapper):
...
@@ -765,13 +705,6 @@ class TFOpMapper(OpMapper):
output
=
node
,
output
=
node
,
param_attr
=
attr
)
param_attr
=
attr
)
def
Abs
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
node
.
fluid_code
.
add_layer
(
"abs"
,
inputs
=
input
,
output
=
node
,
param_attr
=
None
)
def
Conv2DBackpropInput
(
self
,
node
):
def
Conv2DBackpropInput
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
kernel
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
],
copy
=
True
)
kernel
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
],
copy
=
True
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录