Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
X2Paddle
提交
ed0708e8
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看板
提交
ed0708e8
编写于
12月 14, 2020
作者:
S
SunAhong1993
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix the caffe
上级
fb71e9dd
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
115 addition
and
69 deletion
+115
-69
x2paddle/core/program.py
x2paddle/core/program.py
+8
-1
x2paddle/op_mapper/static/caffe2paddle/caffe_op_mapper.py
x2paddle/op_mapper/static/caffe2paddle/caffe_op_mapper.py
+13
-13
x2paddle/optimizer/fusion/static/bn_scale_fuser.py
x2paddle/optimizer/fusion/static/bn_scale_fuser.py
+94
-55
未找到文件。
x2paddle/core/program.py
浏览文件 @
ed0708e8
...
...
@@ -309,12 +309,19 @@ class PaddleGraph(object):
if
not
osp
.
exists
(
code_dir
):
os
.
makedirs
(
code_dir
)
f
=
open
(
osp
.
join
(
code_dir
,
'x2paddle_model.py'
),
'w'
)
if
self
.
source_type
==
"caffe"
:
custom_import
=
"from x2paddle.op_mapper.static.caffe2paddle "
+
\
"import caffe_custom_layer as x2paddle_nn"
else
:
custom_import
=
""
write_code
(
f
,
[
"from paddle.fluid.initializer import Constant"
,
"from paddle.fluid.param_attr import ParamAttr"
,
"import paddle.fluid as fluid"
,
custom_import
,
"import paddle"
,
"import math"
,
""
,
],
...
...
@@ -347,7 +354,7 @@ class PaddleGraph(object):
line
+=
"{}, "
.
format
(
output
)
line
=
line
.
strip
(
", "
)
if
layer
.
kernel
.
startswith
(
"custom_layer"
):
line
+=
"
= {}("
.
format
(
layer
.
kernel
.
split
(
":"
)[
-
1
].
lower
()
+
"_layer"
)
line
+=
"
= x2paddle_nn.{}("
.
format
(
layer
.
kernel
.
split
(
":"
)[
-
1
]
)
else
:
line
+=
" = {}("
.
format
(
layer
.
kernel
)
for
k
,
v
in
layer
.
inputs
.
items
():
...
...
x2paddle/op_mapper/static/caffe2paddle/caffe_op_mapper.py
浏览文件 @
ed0708e8
...
...
@@ -212,7 +212,7 @@ class CaffeOpMapper(OpMapper):
kernel_weight_name
=
node
.
name
+
'_weights'
self
.
params
[
kernel_weight_name
]
=
data
[
0
]
self
.
paddle_graph
.
add_layer
(
kernel
=
"paddle.static.
nn.
create_parameter"
,
kernel
=
"paddle.static.create_parameter"
,
inputs
=
{},
outputs
=
[
kernel_weight_name
],
shape
=
self
.
params
[
kernel_weight_name
].
shape
,
...
...
@@ -222,7 +222,7 @@ class CaffeOpMapper(OpMapper):
kernel_bias_name
=
node
.
name
+
'_bias'
self
.
params
[
kernel_bias_name
]
=
data
[
1
]
self
.
paddle_graph
.
add_layer
(
kernel
=
"paddle.static.
nn.
create_parameter"
,
kernel
=
"paddle.static.create_parameter"
,
inputs
=
{},
outputs
=
[
kernel_bias_name
],
shape
=
self
.
params
[
kernel_bias_name
].
shape
,
...
...
@@ -268,7 +268,7 @@ class CaffeOpMapper(OpMapper):
kernel_weight_name
=
node
.
name
+
'_weights'
self
.
params
[
kernel_weight_name
]
=
data
[
0
]
self
.
paddle_graph
.
add_layer
(
kernel
=
"paddle.static.
nn.
create_parameter"
,
kernel
=
"paddle.static.create_parameter"
,
inputs
=
{},
outputs
=
[
kernel_weight_name
],
shape
=
self
.
params
[
kernel_weight_name
].
shape
,
...
...
@@ -278,7 +278,7 @@ class CaffeOpMapper(OpMapper):
kernel_bias_name
=
node
.
name
+
'_bias'
self
.
params
[
kernel_bias_name
]
=
data
[
1
]
self
.
paddle_graph
.
add_layer
(
kernel
=
"paddle.static.
nn.
create_parameter"
,
kernel
=
"paddle.static.create_parameter"
,
inputs
=
{},
outputs
=
[
kernel_bias_name
],
shape
=
self
.
params
[
kernel_bias_name
].
shape
,
...
...
@@ -330,7 +330,7 @@ class CaffeOpMapper(OpMapper):
kernel_weight_name
=
node
.
name
+
'_weights'
self
.
params
[
kernel_weight_name
]
=
data
[
0
]
self
.
paddle_graph
.
add_layer
(
kernel
=
"paddle.static.
nn.
create_parameter"
,
kernel
=
"paddle.static.create_parameter"
,
inputs
=
{},
outputs
=
[
kernel_weight_name
],
shape
=
self
.
params
[
kernel_weight_name
].
shape
,
...
...
@@ -340,7 +340,7 @@ class CaffeOpMapper(OpMapper):
kernel_bias_name
=
node
.
name
+
'_bias'
self
.
params
[
kernel_bias_name
]
=
data
[
1
]
self
.
paddle_graph
.
add_layer
(
kernel
=
"paddle.static.
nn.
create_parameter"
,
kernel
=
"paddle.static.create_parameter"
,
inputs
=
{},
outputs
=
[
kernel_bias_name
],
shape
=
self
.
params
[
kernel_bias_name
].
shape
,
...
...
@@ -467,7 +467,7 @@ class CaffeOpMapper(OpMapper):
kernel_weight_name
=
node
.
name
+
'_weights'
self
.
params
[
kernel_weight_name
]
=
data
[
0
]
self
.
paddle_graph
.
add_layer
(
kernel
=
"paddle.static.
nn.
create_parameter"
,
kernel
=
"paddle.static.create_parameter"
,
inputs
=
{},
outputs
=
[
kernel_weight_name
],
shape
=
self
.
params
[
kernel_weight_name
].
shape
,
...
...
@@ -477,7 +477,7 @@ class CaffeOpMapper(OpMapper):
kernel_bias_name
=
node
.
name
+
'_bias'
self
.
params
[
kernel_bias_name
]
=
data
[
1
]
self
.
paddle_graph
.
add_layer
(
kernel
=
"paddle.static.
nn.
create_parameter"
,
kernel
=
"paddle.static.create_parameter"
,
inputs
=
{},
outputs
=
[
kernel_bias_name
],
shape
=
self
.
params
[
kernel_bias_name
].
shape
,
...
...
@@ -614,7 +614,7 @@ class CaffeOpMapper(OpMapper):
kernel_weight_name
=
node
.
name
+
'_weights'
self
.
params
[
kernel_weight_name
]
=
np
.
squeeze
(
data
[
0
])
self
.
paddle_graph
.
add_layer
(
kernel
=
"paddle.static.
nn.
create_parameter"
,
kernel
=
"paddle.static.create_parameter"
,
inputs
=
{},
outputs
=
[
kernel_weight_name
],
shape
=
[
num_parameters
],
...
...
@@ -727,7 +727,7 @@ class CaffeOpMapper(OpMapper):
mean_name
=
node
.
name
+
'_mean'
self
.
params
[
mean_name
]
=
mean
self
.
paddle_graph
.
add_layer
(
kernel
=
"paddle.static.
nn.
create_parameter"
,
kernel
=
"paddle.static.create_parameter"
,
inputs
=
{},
outputs
=
[
mean_name
],
shape
=
self
.
params
[
mean_name
].
shape
,
...
...
@@ -736,7 +736,7 @@ class CaffeOpMapper(OpMapper):
variance_name
=
node
.
name
+
'_variance'
self
.
params
[
variance_name
]
=
variance
self
.
paddle_graph
.
add_layer
(
kernel
=
"paddle.static.
nn.
create_parameter"
,
kernel
=
"paddle.static.create_parameter"
,
inputs
=
{},
outputs
=
[
variance_name
],
shape
=
self
.
params
[
variance_name
].
shape
,
...
...
@@ -790,7 +790,7 @@ class CaffeOpMapper(OpMapper):
axis
=
1
)
else
:
self
.
paddle_graph
.
add_layer
(
"paddle.static.
nn.
create_parameter"
,
"paddle.static.create_parameter"
,
inputs
=
{},
outputs
=
[
node
.
name
+
"_cparam1"
],
shape
=
self
.
params
[
node
.
name
+
"_cparam1"
].
shape
,
...
...
@@ -807,7 +807,7 @@ class CaffeOpMapper(OpMapper):
outputs
=
[
node
.
name
+
"_mul"
],
axis
=
axis
)
self
.
paddle_graph
.
add_layer
(
"paddle.static.
nn.
create_parameter"
,
"paddle.static.create_parameter"
,
inputs
=
{},
outputs
=
[
node
.
name
+
"_cparam2"
],
shape
=
self
.
params
[
node
.
name
+
"_cparam2"
].
shape
,
...
...
x2paddle/optimizer/fusion/static/bn_scale_fuser.py
浏览文件 @
ed0708e8
...
...
@@ -20,87 +20,126 @@ from x2paddle.core.util import *
class
Static_BNScaleFuser
(
FuseBase
):
def
__init__
(
self
):
super
(
Static_BNScaleFuser
,
self
).
__init__
(
graph_type
=
"dygraph"
)
super
(
Static_BNScaleFuser
,
self
).
__init__
(
graph_type
=
"static"
)
patterns
=
list
()
def
build_pattern
(
self
):
""" 描述需要替换的batchnorm2d图结构。
batchnorm2d层模式python实现代码示例:
conv5_bn = fluid.layers.batch_norm(input=conv5, is_test=True, param_attr=None, bias_attr=None, moving_mean_name='conv5_bn_mean', moving_variance_name='conv5_bn_variance', epsilon=9.999999747378752e-06, name='conv5_bn')
conv5_scale_scale = fluid.ParamAttr(name='conv5_scale_scale')
conv5_scale_cparam1 = fluid.layers.create_parameter(attr=conv5_scale_scale, dtype=conv5_bn.dtype, shape=[256], name='conv5_scale_cparam1', is_bias=True, default_initializer=Constant(value=1.0))
conv5_scale_mul = fluid.layers.elementwise_mul(x=conv5_bn, y=conv5_scale_cparam1, axis=1)
conv5_scale_offset = fluid.ParamAttr(name='conv5_scale_offset')
conv5_scale_cparam2 = fluid.layers.create_parameter(attr=conv5_scale_offset, dtype=conv5_bn.dtype, shape=[256], name='conv5_scale_cparam2', is_bias=True, default_initializer=Constant(value=1.0))
conv5_scale = fluid.layers.elementwise_add(x=conv5_scale_mul, y=conv5_scale_cparam2, axis=1)
模式一:
conv1_bn = paddle.nn.functional.batch_norm(x=conv1, weight=conv1_bn_weight, bias=conv1_bn_bias, running_mean=conv1_bn_mean, running_var=conv1_bn_variance, epsilon=9.999999747378752e-06, momentum=0.9990000128746033)
conv1_scale_cparam1 = paddle.static.create_parameter(shape=(32,), dtype='float32', name='conv1_scale_cparam1')
conv1_scale_mul = paddle.multiply(x=conv1_bn, y=conv1_scale_cparam1, axis=1)
conv1_scale_cparam2 = paddle.static.create_parameter(shape=(32,), dtype='float32', name='conv1_scale_cparam2')
conv1_scale_cparam2 = paddle.reshape(x=conv1_scale_cparam2, shape=[32, 1, 1])
conv1_scale = paddle.add(x=conv1_scale_mul, y=conv1_scale_cparam2)
模式二:
conv1_bn = paddle.nn.functional.batch_norm(x=conv1, weight=conv1_bn_weight, bias=conv1_bn_bias, running_mean=conv1_bn_mean, running_var=conv1_bn_variance, epsilon=9.999999747378752e-06, momentum=0.9990000128746033)
conv1_scale_cparam1 = paddle.static.create_parameter(shape=(32,), dtype='float32', name='conv1_scale_cparam1')
conv1_scale_mul = paddle.multiply(x=conv1_bn, y=conv1_scale_cparam1, axis=1)
conv1_scale_cparam2 = paddle.static.create_parameter(shape=(32,), dtype='float32', name='conv1_scale_cparam2')
conv1_scale = paddle.add(x=conv1_scale_mul, y=conv1_scale_cparam2)
"""
def
gen_name
(
id
):
return
"x"
+
str
(
id
)
self
.
pattern
.
add_layer
(
"fluid.layers.batch_norm"
,
inputs
=
{
"input"
:
"bn-input-0"
},
pattern
=
PaddleGraph
(
graph_type
=
"dygraph"
)
pattern
.
add_layer
(
"paddle.nn.functional.batch_norm"
,
inputs
=
{
"input"
:
"bn-input-0"
,
"weight"
:
"bn-input-1"
,
"bias"
:
"bn-input-2"
,
"running_mean"
:
"bn-input-3"
,
"running_var"
:
"bn-input-4"
,},
outputs
=
[
gen_name
(
0
)])
self
.
pattern
.
add_layer
(
"
fluid.ParamAtt
r"
,
pattern
.
add_layer
(
"
paddle.static.create_paramete
r"
,
inputs
=
{},
outputs
=
[
gen_name
(
1
)])
self
.
pattern
.
add_layer
(
"fluid.layers.create_parameter"
,
inputs
=
{
"attr"
:
gen_name
(
1
)},
outputs
=
[
gen_name
(
2
)])
inputs_dict
=
{}
inputs_dict
[
'x'
]
=
gen_name
(
0
)
inputs_dict
[
'y'
]
=
gen_name
(
2
)
self
.
pattern
.
add_layer
(
"
fluid.layers.elementwise_mul
"
,
inputs_dict
[
'y'
]
=
gen_name
(
1
)
pattern
.
add_layer
(
"
paddle.multiply
"
,
inputs
=
inputs_dict
,
outputs
=
[
gen_name
(
3
)])
self
.
pattern
.
add_layer
(
"
fluid.ParamAtt
r"
,
outputs
=
[
gen_name
(
2
)])
pattern
.
add_layer
(
"
paddle.static.create_paramete
r"
,
inputs
=
{},
outputs
=
[
gen_name
(
3
)])
pattern
.
add_layer
(
"paddle.reshape"
,
inputs
=
{
"x"
:
gen_name
(
3
)},
outputs
=
[
gen_name
(
4
)])
self
.
pattern
.
add_layer
(
"fluid.layers.create_parameter"
,
inputs
=
{
"attr"
:
gen_name
(
4
)},
inputs_dict
=
{}
inputs_dict
[
'x'
]
=
gen_name
(
2
)
inputs_dict
[
'y'
]
=
gen_name
(
4
)
pattern
.
add_layer
(
"paddle.add"
,
inputs
=
inputs_dict
,
outputs
=
[
gen_name
(
5
)])
pattern
.
build
(
inputs
=
{
"input-0"
:
"bn-input-0"
,
"input-1"
:
"bn-input-1"
,
"input-2"
:
"bn-input-2"
,
"input-3"
:
"bn-input-3"
,
"input-4"
:
"bn-input-4"
})
self
.
patterns
.
append
(
pattern
)
pattern
=
PaddleGraph
(
graph_type
=
"dygraph"
)
pattern
.
add_layer
(
"paddle.nn.functional.batch_norm"
,
inputs
=
{
"input"
:
"bn-input-0"
,
"weight"
:
"bn-input-1"
,
"bias"
:
"bn-input-2"
,
"running_mean"
:
"bn-input-3"
,
"running_var"
:
"bn-input-4"
,},
outputs
=
[
gen_name
(
0
)])
pattern
.
add_layer
(
"paddle.static.create_parameter"
,
inputs
=
{},
outputs
=
[
gen_name
(
1
)])
inputs_dict
=
{}
inputs_dict
[
'x'
]
=
gen_name
(
3
)
inputs_dict
[
'y'
]
=
gen_name
(
5
)
self
.
pattern
.
add_layer
(
"
fluid.layers.elementwise_add
"
,
inputs_dict
[
'x'
]
=
gen_name
(
0
)
inputs_dict
[
'y'
]
=
gen_name
(
1
)
pattern
.
add_layer
(
"
paddle.multiply
"
,
inputs
=
inputs_dict
,
outputs
=
[
gen_name
(
6
)])
self
.
pattern
.
build
(
inputs
=
{
"input-0"
:
"bn-input-0"
})
outputs
=
[
gen_name
(
2
)])
pattern
.
add_layer
(
"paddle.static.create_parameter"
,
inputs
=
{},
outputs
=
[
gen_name
(
3
)])
inputs_dict
=
{}
inputs_dict
[
'x'
]
=
gen_name
(
2
)
inputs_dict
[
'y'
]
=
gen_name
(
3
)
pattern
.
add_layer
(
"paddle.add"
,
inputs
=
inputs_dict
,
outputs
=
[
gen_name
(
4
)])
pattern
.
build
(
inputs
=
{
"input-0"
:
"bn-input-0"
,
"input-1"
:
"bn-input-1"
,
"input-2"
:
"bn-input-2"
,
"input-3"
:
"bn-input-3"
,
"input-4"
:
"bn-input-4"
})
self
.
patterns
.
append
(
pattern
)
def
insert_new_layer
(
self
,
graph
,
parameters
,
matches
):
new_layer
=
self
.
gen_new_layer
(
parameters
,
matches
)
new_layer_id
=
list
(
matches
.
keys
())[
0
]
new_layer_id
=
list
(
matches
.
keys
())[
-
1
]
graph
.
layers
[
new_layer_id
]
=
new_layer
matches
.
pop
(
list
(
matches
.
keys
())[
1
])
matches
.
pop
(
list
(
matches
.
keys
())[
2
])
matches
.
pop
(
new_layer_id
)
def
gen_new_layer
(
self
,
parameters
,
matches
):
layers_id
=
list
(
matches
.
keys
())
layer
=
matches
[
layers_id
[
0
]]
layer_inputs
=
layer
.
inputs
layer_name
=
layer
.
outputs
[
0
]
layer_attrs
=
layer
.
attrs
layer_attrs
[
"param_attr"
]
=
string
(
"{}_scale"
.
format
(
layer_name
))
layer_attrs
[
"bias_attr"
]
=
string
(
"{}_offset"
.
format
(
layer_name
))
layer
=
matches
[
layers_id
[
-
1
]]
layer_outputs
=
layer
.
outputs
bn_layer
=
matches
[
layers_id
[
0
]]
layer
=
matches
[
layers_id
[
1
]]
layer_name
=
layer
.
outputs
[
0
]
scale_numpy
=
parameters
.
pop
(
layer_name
)
parameters
[
layer_attrs
[
"param_attr"
][
1
:
-
1
]]
=
scale_numpy
layer
=
matches
[
layers_id
[
4
]]
layer_name
=
layer
.
outputs
[
0
]
scale_numpy
=
parameters
.
pop
(
layer_name
)
parameters
[
layer_attrs
[
"bias_attr"
][
1
:
-
1
]]
=
scale_numpy
new_layer
=
PaddleLayer
(
layers_id
[
0
],
"fluid.layers.batch_norm"
,
inputs
=
layer_inputs
,
outputs
=
layer_outputs
,
**
layer_attrs
)
return
new_layer
\ No newline at end of file
bn_layer
.
inputs
[
"weight"
]
=
layer
.
outputs
[
0
]
layer
=
matches
[
layers_id
[
3
]]
bn_layer
.
inputs
[
"bias"
]
=
layer
.
outputs
[
0
]
bn_layer
.
id
=
layers_id
[
-
1
]
layer
=
matches
[
layers_id
[
-
1
]]
bn_layer
.
outputs
=
layer
.
outputs
return
bn_layer
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录