Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
X2Paddle
提交
9450efde
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看板
提交
9450efde
编写于
7月 18, 2019
作者:
J
jiangjiajun
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
code without test
上级
3cf5a607
变更
6
显示空白变更内容
内联
并排
Showing
6 changed file
with
231 addition
and
7 deletion
+231
-7
tool/tf_export_model.py
tool/tf_export_model.py
+35
-0
x2paddle/convert.py
x2paddle/convert.py
+1
-1
x2paddle/core/fluid_code.py
x2paddle/core/fluid_code.py
+2
-0
x2paddle/core/util.py
x2paddle/core/util.py
+25
-0
x2paddle/emitter/tf_emitter.py
x2paddle/emitter/tf_emitter.py
+146
-6
x2paddle/parser/tf_parser.py
x2paddle/parser/tf_parser.py
+22
-0
未找到文件。
tool/tf_export_model.py
浏览文件 @
9450efde
...
...
@@ -11,3 +11,38 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from
tensorflow.python.framework
import
graph_util
import
tensorflow
as
tf
def
freeze_model
(
sess
,
output_tensor_names
,
freeze_model_path
):
out_graph
=
graph_util
.
convert_variables_to_constants
(
sess
,
sess
.
graph
.
as_graph_def
(),
output_tensor_names
)
with
tf
.
gfile
.
GFile
(
freeze_model_path
,
'wb'
)
as
f
:
f
.
write
(
out_graph
.
SerializeToString
())
print
(
"freeze model saved in {}"
.
format
(
freeze_model_path
))
import
tensorflow.contrib.slim
as
slim
from
tensorflow.contrib.slim.nets
import
vgg
import
numpy
with
tf
.
Session
()
as
sess
:
inputs
=
tf
.
placeholder
(
dtype
=
tf
.
float32
,
shape
=
[
None
,
224
,
224
,
3
],
name
=
"inputs"
)
logits
,
endpoint
=
vgg
.
vgg_16
(
inputs
,
num_classes
=
1000
,
is_training
=
False
)
load_model
=
slim
.
assign_from_checkpoint_fn
(
"vgg_16.ckpt"
,
slim
.
get_model_variables
(
"vgg_16"
))
load_model
(
sess
)
numpy
.
random
.
seed
(
13
)
data
=
numpy
.
random
.
rand
(
5
,
224
,
224
,
3
)
input_tensor
=
sess
.
graph
.
get_tensor_by_name
(
"inputs:0"
)
output_tensor
=
sess
.
graph
.
get_tensor_by_name
(
"vgg_16/fc8/squeezed:0"
)
result
=
sess
.
run
([
output_tensor
],
{
input_tensor
:
data
})
numpy
.
save
(
"tensorflow.npy"
,
numpy
.
array
(
result
))
freeze_model
(
sess
,
[
"vgg_16/fc8/squeezed"
],
"vgg16.pb"
)
x2paddle/convert.py
浏览文件 @
9450efde
...
...
@@ -15,7 +15,7 @@ from x2paddle.parser.tf_parser import TFParser
from
x2paddle.optimizer.tf_optimizer
import
TFGraphOptimizer
from
x2paddle.emitter.tf_emitter
import
TFEmitter
parser
=
TFParser
(
'/ssd
3/dltpsz/frozen_darknet_yolov3_model
.pb'
,
parser
=
TFParser
(
'/ssd
2/Jason/github/X2Paddle/tool/vgg16
.pb'
,
in_nodes
=
[
'inputs'
],
out_nodes
=
[
'output_boxes'
],
in_shapes
=
[[
-
1
,
416
,
416
,
3
]])
...
...
x2paddle/core/fluid_code.py
浏览文件 @
9450efde
...
...
@@ -59,6 +59,8 @@ class Layer(object):
"[{}]"
.
format
(
self
.
inputs
.
index
)
+
", "
)
else
:
layer_code
+=
(
self
.
inputs
.
layer_name
+
", "
)
elif
isinstance
(
self
.
inputs
,
str
):
layer_code
+=
(
self
.
inputs
+
", "
)
else
:
raise
Exception
(
"Unknown type of inputs."
)
...
...
x2paddle/core/util.py
0 → 100644
浏览文件 @
9450efde
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
def
string
(
param
):
return
"
\'
{}
\'
"
.
format
(
param
)
def
get_same_padding
(
input_size
,
kernel_size
,
stride
):
new_size
=
int
(
math
.
ceil
(
input_size
*
1.0
/
stride
))
pad_size
=
(
new_size
-
1
)
*
stride
+
filter_size
-
in_size
pad0
=
int
(
pad_size
/
2
)
pad1
=
pad_size
-
pad0
return
[
pad0
,
pad1
]
x2paddle/emitter/tf_emitter.py
浏览文件 @
9450efde
...
...
@@ -23,6 +23,9 @@ class TFEmitter(Emitter):
super
(
TFEmitter
,
self
).
__init__
()
self
.
parser
=
parser
self
.
graph
=
parser
.
tf_graph
# attr_node is used to record nodes that
# only for define attribute of op
self
.
attr_node
=
list
()
self
.
weights
=
dict
()
def
run
(
self
):
...
...
@@ -54,6 +57,9 @@ class TFEmitter(Emitter):
param_attr
=
attr
)
def
Const
(
self
,
node
):
## TODO
return
shape
=
node
.
out_shapes
[
0
]
dtype
=
node
.
dtype
value
=
node
.
value
...
...
@@ -75,8 +81,9 @@ class TFEmitter(Emitter):
param_attr
=
attr
)
def
Transpose
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
])
perm
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
])
return
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
perm
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
],
copy
=
True
)
perm
.
fluid_code
.
clear
()
perm
=
perm
.
value
.
tolist
()
...
...
@@ -87,13 +94,146 @@ class TFEmitter(Emitter):
param_attr
=
attr
)
def
RealDiv
(
self
,
node
):
x
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
])
y
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
])
return
x
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
y
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
],
copy
=
True
)
inputs
=
{
'x'
:
x
,
'y'
:
y
}
node
.
fluid_code
.
add_layer
(
"elementwise_div"
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
None
)
def
Fc
(
self
,
node
):
self
.
weight
[
'asdf'
]
=
np
.
tranpose
(
node
.
kerneln
[
1
,
0
])
def
Relu
(
self
,
node
):
return
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
):
return
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
squeeze_dims
=
node
.
get_attr
(
'squeeze_dims'
)
print
(
squeeze_dims
)
attr
=
{
'squeeze_dims'
:
squeeze_dims
}
node
.
fluid_code
.
add_layer
(
"squeeze"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
BiasAdd
(
self
,
node
):
return
x
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
y
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
],
copy
=
True
)
inputs
=
{
'x'
:
x
,
'y'
:
y
}
node
.
fluid_code
.
add_layer
(
"elementwise_add"
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
None
)
def
Identity
(
self
,
node
):
return
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
):
return
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
in_shape
=
input
.
out_shapes
[
0
]
k_size
=
node
.
get_attr
(
"ksize"
)
strides
=
node
.
get_attr
(
"strides"
)
data_format
=
node
.
get_attr
(
"data_format"
).
decode
()
pad_mode
=
node
.
get_attr
(
"padding"
).
decode
()
if
data_format
==
"NHWC"
:
attr
=
{
"perm"
:
[
0
,
3
,
1
,
2
]}
node
.
fluid_code
.
add_layer
(
"transpose"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
in_shape
=
[
in_shape
[
i
]
for
i
in
[
0
,
3
,
1
,
2
]]
k_size
=
[
k_size
[
i
]
for
i
in
[
0
,
3
,
1
,
2
]]
strides
=
[
strides
[
i
]
for
i
in
[
0
,
3
,
1
,
2
]]
if
pad_mode
==
"SAME"
:
pad_h
=
get_same_padding
(
in_shape
[
2
],
k_size
[
2
],
strides
[
2
])
pad_w
=
get_same_padding
(
in_shape
[
3
],
k_size
[
3
],
strides
[
3
])
pad_h
=
pad_h
[
0
]
+
pad_h
[
1
]
pad_w
=
pad_w
[
0
]
+
pad_w
[
1
]
attr
=
{
"paddings"
:
[
0
,
pad_h
,
0
,
pad_w
],
"pad_value"
:
-
10000.0
}
node
.
fluid_code
.
add_layer
(
"pad2d"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
attr
=
{
"pool_size"
:
k_size
[
1
:
3
],
"pool_type"
:
string
(
"max"
),
"pool_stride"
:
strides
[
1
:
3
]
}
node
.
fluid_code
.
add_layer
(
"pool2d"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
if
data_format
==
"NHWC"
:
attr
=
{
"perm"
:
[
0
,
2
,
3
,
1
]}
node
.
fluid_code
.
add_layer
(
"transpose"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
# def Conv2D(self, node):
# input = self.graph.get_node(node.layer.input[0], copy=True)
# in_shape = input.out_shapes[0]
# k_size = node.get_attr("ksize")
# strides = node.get_attr("strides")
# dilations = node.get_attr("dilations")
# data_format = node.get_attr("data_format").decode()
# pad_mode = node.get_attr("padding").decode()
#
# if data_format == "NHWC":
# attr = {"perm": [0, 3, 1, 2]}
# node.fluid_code.add_layer("transpose",
# inputs=input,
# output=node,
# param_attr=attr)
# in_shape = [in_shape[i] for i in [0, 3, 1, 2]]
# k_size = [k_size[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]]
#
# if pad_mode == "SAME":
# pad_h = get_same_padding(in_shape[2], k_size[2], strides[2])
# pad_w = get_same_padding(in_shape[3], k_size[3], strides[3])
# pad_h = pad_h[0] + pad_h[1]
# pad_w = pad_w[0] + pad_w[1]
# attr = {"paddings": pad_h+pad_w, "pad_value": 0.0}
# node.fluid_code.add_layer("pad2d",
# inputs=input,
# output=node,
# param_attr=attr)
# attr = {
# "pool_stride": strides[1:3],
# "bias_attr": False,
# "param_attr":,
# "num_filters":,
# "filter_size":,
# "stride":,
# "dilation":
# }
# node.fluid_code.add_layer("conv2d",
# inputs=input,
# output=node,
# param_attr=attr)
#
# if data_format == "NHWC":
# attr = {"perm": [0, 2, 3, 1]}
# node.fluid_code.add_layer("transpose",
# inputs=input,
# output=node,
# param_attr=attr)
x2paddle/parser/tf_parser.py
浏览文件 @
9450efde
...
...
@@ -16,6 +16,7 @@ from x2paddle.core.graph import GraphNode, Graph
from
x2paddle.core.fluid_code
import
FluidCode
from
tensorflow.python.framework
import
tensor_util
from
tensorflow.python.platform
import
gfile
from
tensorflow.core.framework
import
attr_value_pb2
import
tensorflow
as
tf
import
copy
...
...
@@ -56,6 +57,27 @@ class TFGraphNode(GraphNode):
field
=
getattr
(
attr
,
attr
.
WhichOneof
(
'value'
))
return
tensor_util
.
MakeNdarray
(
field
)
def
get_attr
(
self
,
name
):
if
name
not
in
self
.
layer
.
attr
:
return
None
attr
=
self
.
layer
.
attr
[
name
]
field
=
attr
.
WhichOneof
(
'value'
)
value
=
getattr
(
attr
,
field
)
if
field
else
None
if
isinstance
(
value
,
attr_value_pb2
.
AttrValue
.
ListValue
):
result
=
list
(
value
.
ListFields
()[
0
][
1
])
for
i
in
range
(
len
(
result
)):
if
isinstance
(
result
[
i
],
int
):
result
[
i
]
=
int
(
result
[
i
])
try
:
if
isinstance
(
result
[
i
],
long
):
result
[
i
]
=
int
(
result
[
i
])
except
:
pass
return
result
else
:
return
value
class
TFGraph
(
Graph
):
def
__init__
(
self
,
model
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录