Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
X2Paddle
提交
3b8afdb9
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看板
提交
3b8afdb9
编写于
11月 19, 2020
作者:
S
SunAhong1993
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add tf optimizer
上级
c236c3c2
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
28 addition
and
16 deletion
+28
-16
tools/README.md
tools/README.md
+1
-0
x2paddle/convert.py
x2paddle/convert.py
+3
-0
x2paddle/op_mapper/tf_op_mapper.py
x2paddle/op_mapper/tf_op_mapper.py
+20
-10
x2paddle/optimizer/tensorflow/batch_norm.py
x2paddle/optimizer/tensorflow/batch_norm.py
+4
-6
未找到文件。
tools/README.md
浏览文件 @
3b8afdb9
...
@@ -4,6 +4,7 @@
...
@@ -4,6 +4,7 @@
```
```
python tools/check_for_lite.py paddle_model/inference_model/__model__
python tools/check_for_lite.py paddle_model/inference_model/__model__
```
```
> 附:check_for_lite工具并不能完全判断模型是否被支持,PaddleLite详细支持的算子请参考[PaddleLite支持算子集](https://github.com/PaddlePaddle/Paddle-Lite/blob/develop/docs/introduction/support_operation_list.md)
### 二、模型参数合并
### 二、模型参数合并
...
...
x2paddle/convert.py
浏览文件 @
3b8afdb9
...
@@ -117,6 +117,7 @@ def tf2paddle(model_path,
...
@@ -117,6 +117,7 @@ def tf2paddle(model_path,
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
from
x2paddle.optimizer.tensorflow.prelu
import
PReLUOpt
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
)
...
@@ -125,8 +126,10 @@ def tf2paddle(model_path,
...
@@ -125,8 +126,10 @@ def tf2paddle(model_path,
bias_opt
=
BiasOpt
()
bias_opt
=
BiasOpt
()
transpose_opt
=
TransposeOpt
()
transpose_opt
=
TransposeOpt
()
batch_norm_opt
=
BatchNormOpt
()
batch_norm_opt
=
BatchNormOpt
()
prelu_opt
=
PReLUOpt
()
bias_opt
.
run
(
program
)
bias_opt
.
run
(
program
)
batch_norm_opt
.
run
(
program
)
batch_norm_opt
.
run
(
program
)
prelu_opt
.
run
(
program
)
transpose_opt
.
run
(
program
)
transpose_opt
.
run
(
program
)
program
.
gen_model
(
save_dir
)
program
.
gen_model
(
save_dir
)
...
...
x2paddle/op_mapper/tf_op_mapper.py
浏览文件 @
3b8afdb9
...
@@ -882,7 +882,7 @@ class TFOpMapper(OpMapper):
...
@@ -882,7 +882,7 @@ class TFOpMapper(OpMapper):
begin
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
])
begin
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
])
size
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
2
])
size
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
2
])
inputs
=
{
"
x
"
:
input
.
name
}
inputs
=
{
"
input
"
:
input
.
name
}
attrs
=
{}
attrs
=
{}
if
begin
.
layer_type
==
"Const"
:
if
begin
.
layer_type
==
"Const"
:
begin
=
begin
.
value
.
tolist
()
begin
=
begin
.
value
.
tolist
()
...
@@ -901,20 +901,30 @@ class TFOpMapper(OpMapper):
...
@@ -901,20 +901,30 @@ class TFOpMapper(OpMapper):
if
size
.
layer_type
==
"Const"
:
if
size
.
layer_type
==
"Const"
:
size
=
size
.
value
.
tolist
()
size
=
size
.
value
.
tolist
()
attrs
[
'shape'
]
=
size
attrs
[
'shape'
]
=
size
shape
=
size
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
(
# program.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
# inputs.pop('shape')
program
.
add_layer
(
program
.
add_layer
(
kernel
=
"fluid.layers.
crop_tensor
"
,
kernel
=
"fluid.layers.
slice
"
,
inputs
=
inputs
,
inputs
=
inputs
,
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
**
attrs
)
axes
=
list
(
range
(
len
(
attrs
[
'offsets'
]))),
starts
=
attrs
[
'offsets'
],
ends
=
[
attrs
[
'offsets'
][
i
]
+
shape
[
i
]
for
i
in
range
(
len
(
shape
))])
# program.add_layer(
# kernel="fluid.layers.crop_tensor",
# inputs=inputs,
# outputs=[node.name],
# **attrs)
def
ResizeNearestNeighbor
(
self
,
node
):
def
ResizeNearestNeighbor
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
])
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
])
...
...
x2paddle/optimizer/tensorflow/batch_norm.py
浏览文件 @
3b8afdb9
...
@@ -20,10 +20,12 @@ class BatchNormOpt:
...
@@ -20,10 +20,12 @@ class BatchNormOpt:
input_ids0
=
graph
.
edges_in
[
layer_id
]
input_ids0
=
graph
.
edges_in
[
layer_id
]
mul_layer0
=
graph
.
layers
[
input_ids0
[
0
]]
mul_layer0
=
graph
.
layers
[
input_ids0
[
0
]]
sub_layer0
=
graph
.
layers
[
input_ids0
[
1
]]
sub_layer0
=
graph
.
layers
[
input_ids0
[
1
]]
if
mul_layer0
.
kernel
!=
"fluid.layers.elementwise_mul"
:
if
mul_layer0
.
kernel
!=
"fluid.layers.elementwise_mul"
:
continue
continue
if
sub_layer0
.
kernel
!=
"fluid.layers.elementwise_sub"
:
if
sub_layer0
.
kernel
!=
"fluid.layers.elementwise_sub"
:
continue
continue
axis
=
mul_layer0
.
attrs
.
get
(
'axis'
,
-
1
)
axis
=
mul_layer0
.
attrs
.
get
(
'axis'
,
-
1
)
if
axis
!=
-
1
and
axis
!=
3
:
if
axis
!=
-
1
and
axis
!=
3
:
continue
continue
...
@@ -116,7 +118,7 @@ class BatchNormOpt:
...
@@ -116,7 +118,7 @@ class BatchNormOpt:
other
=
graph
.
layers
[
input_ids6
[
1
]]
other
=
graph
.
layers
[
input_ids6
[
1
]]
if
variance
.
kernel
!=
"fluid.layers.create_parameter"
:
if
variance
.
kernel
!=
"fluid.layers.create_parameter"
:
continue
continue
if
other
.
kernel
!=
"fluid.layers.
create_parameter
"
:
if
other
.
kernel
!=
"fluid.layers.
fill_constant
"
:
continue
continue
if
len
(
graph
.
edges_out
.
get
(
input_ids6
[
0
],
[]))
!=
1
:
if
len
(
graph
.
edges_out
.
get
(
input_ids6
[
0
],
[]))
!=
1
:
continue
continue
...
@@ -127,10 +129,6 @@ class BatchNormOpt:
...
@@ -127,10 +129,6 @@ class BatchNormOpt:
variance_shape
=
graph
.
parameters
[
variance
.
outputs
[
0
]].
shape
variance_shape
=
graph
.
parameters
[
variance
.
outputs
[
0
]].
shape
if
variance_shape
!=
beta_shape
:
if
variance_shape
!=
beta_shape
:
continue
continue
if
other
.
outputs
[
0
]
not
in
graph
.
parameters
:
continue
if
graph
.
parameters
[
other
.
outputs
[
0
]].
size
!=
1
:
continue
ids
=
set
([
ids
=
set
([
layer_id
,
mul_layer0
.
id
,
sub_layer0
.
id
,
mul_layer1
.
id
,
beta
.
id
,
layer_id
,
mul_layer0
.
id
,
sub_layer0
.
id
,
mul_layer1
.
id
,
beta
.
id
,
...
@@ -163,7 +161,7 @@ class BatchNormOpt:
...
@@ -163,7 +161,7 @@ class BatchNormOpt:
kernel
=
"fluid.layers.batch_norm"
,
kernel
=
"fluid.layers.batch_norm"
,
inputs
=
{
"input"
:
"transpose_for_bn"
},
inputs
=
{
"input"
:
"transpose_for_bn"
},
outputs
=
layer
.
outputs
,
outputs
=
layer
.
outputs
,
epsilon
=
graph
.
parameters
[
other
.
outputs
[
0
]
],
epsilon
=
other
.
attrs
[
"value"
],
param_attr
=
"'{}'"
.
format
(
gamma
.
outputs
[
0
]),
param_attr
=
"'{}'"
.
format
(
gamma
.
outputs
[
0
]),
bias_attr
=
"'{}'"
.
format
(
beta
.
outputs
[
0
]),
bias_attr
=
"'{}'"
.
format
(
beta
.
outputs
[
0
]),
moving_mean_name
=
"'{}'"
.
format
(
mean
.
outputs
[
0
]),
moving_mean_name
=
"'{}'"
.
format
(
mean
.
outputs
[
0
]),
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录