Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
X2Paddle
提交
7e68465e
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看板
未验证
提交
7e68465e
编写于
12月 14, 2020
作者:
S
SunAhong1993
提交者:
GitHub
12月 14, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update caffe_op_mapper.py
上级
8d952fd3
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
20 addition
and
21 deletion
+20
-21
x2paddle/op_mapper/static/caffe2paddle/caffe_op_mapper.py
x2paddle/op_mapper/static/caffe2paddle/caffe_op_mapper.py
+20
-21
未找到文件。
x2paddle/op_mapper/static/caffe2paddle/caffe_op_mapper.py
浏览文件 @
7e68465e
...
...
@@ -21,7 +21,7 @@ from x2paddle.core.util import *
from
x2paddle.core.program
import
PaddleGraph
def
adjust_parameters
(
node
):
def
_
adjust_parameters
(
node
):
data
=
node
.
data
# When using the protobuf-backend, each parameter initially has four dimensions.
# In certain cases (like FC layers), we want to eliminate the singleton dimensions.
...
...
@@ -57,8 +57,8 @@ def adjust_parameters(node):
shape_new
=
data
[
idx
].
shape
return
data
def
get_kernel_parameters
(
kind
,
params
):
assert
kind
in
[
'Convolution'
,
'Pooling'
,
'Deconvolution'
]
def
_
get_kernel_parameters
(
kind
,
params
):
assert
kind
in
[
"Convolution"
,
"Pooling"
,
"Deconvolution"
,
"ConvolutionDepthwise"
]
[
k_h
,
k_w
]
=
[
1
,
1
]
if
isinstance
(
params
.
kernel_size
,
numbers
.
Number
):
[
k_h
,
k_w
]
=
[
params
.
kernel_size
]
*
2
...
...
@@ -93,7 +93,8 @@ def get_kernel_parameters(kind, params):
dila_h
=
dila_w
=
1
group
=
1
c_o
=
1
if
kind
in
[
'Convolution'
,
'Deconvolution'
]:
if
kind
in
[
"Convolution"
,
"Deconvolution"
,
"ConvolutionDepthwise"
]:
if
kind
in
[
"Convolution"
,
"Deconvolution"
]:
c_o
=
params
.
num_output
dila_len
=
len
(
params
.
dilation
)
if
dila_len
==
2
:
...
...
@@ -193,7 +194,7 @@ class CaffeOpMapper(OpMapper):
def
Convolution
(
self
,
node
):
data
=
node
.
data
params
=
node
.
layer
.
convolution_param
channel
,
kernel
,
stride
,
pad
,
dilation
,
group
=
get_kernel_parameters
(
channel
,
kernel
,
stride
,
pad
,
dilation
,
group
=
_
get_kernel_parameters
(
node
.
layer_type
,
params
)
if
data
is
None
:
data
=
[]
...
...
@@ -207,7 +208,7 @@ class CaffeOpMapper(OpMapper):
'float32'
))
data
.
append
(
np
.
zeros
([
output_c
,
]).
astype
(
'float32'
))
else
:
data
=
adjust_parameters
(
node
)
data
=
_
adjust_parameters
(
node
)
kernel_weight_name
=
node
.
name
+
'_weights'
self
.
params
[
kernel_weight_name
]
=
data
[
0
]
self
.
paddle_graph
.
add_layer
(
...
...
@@ -249,7 +250,7 @@ class CaffeOpMapper(OpMapper):
def
Deconvolution
(
self
,
node
):
data
=
node
.
data
params
=
node
.
layer
.
convolution_param
channel
,
kernel
,
stride
,
pad
,
dilation
,
group
=
get_kernel_parameters
(
channel
,
kernel
,
stride
,
pad
,
dilation
,
group
=
_
get_kernel_parameters
(
node
.
layer_type
,
params
)
if
data
is
None
:
data
=
[]
...
...
@@ -263,7 +264,7 @@ class CaffeOpMapper(OpMapper):
'float32'
))
data
.
append
(
np
.
zeros
([
output_c
,
]).
astype
(
'float32'
))
else
:
data
=
adjust_parameters
(
node
)
data
=
_
adjust_parameters
(
node
)
kernel_weight_name
=
node
.
name
+
'_weights'
self
.
params
[
kernel_weight_name
]
=
data
[
0
]
self
.
paddle_graph
.
add_layer
(
...
...
@@ -307,9 +308,6 @@ class CaffeOpMapper(OpMapper):
self
.
ConvolutionDepthwise
(
node
)
def
ConvolutionDepthwise
(
self
,
node
):
conv2d_name
=
name_generator
(
"conv"
,
self
.
nn_name2id
)
output_name
=
node
.
layer_name
layer_outputs
=
[
conv2d_name
,
output_name
]
data
=
node
.
data
params
=
node
.
layer
.
convolution_param
out_channel
,
kernel
,
stride
,
pad
,
dilation
,
group
=
_get_kernel_parameters
(
...
...
@@ -372,7 +370,7 @@ class CaffeOpMapper(OpMapper):
ceil_mode
=
getattr
(
params
,
'ceil_mode'
,
True
)
global_pool
=
getattr
(
params
,
'global_pooling'
,
False
)
kernel_default
=
[
1
,
1
]
channel
,
kernel
,
stride
,
pad
,
dilation
,
group
=
get_kernel_parameters
(
channel
,
kernel
,
stride
,
pad
,
dilation
,
group
=
_
get_kernel_parameters
(
node
.
layer_type
,
params
)
assert
len
(
node
.
inputs
)
==
1
,
'The count of Pooling node
\'
s input is not 1.'
...
...
@@ -390,7 +388,7 @@ class CaffeOpMapper(OpMapper):
self
.
paddle_graph
.
add_layer
(
"paddle.nn.functional.adaptive_avg_pool2d"
,
inputs
=
{
"x"
:
input
.
name
},
outputs
=
layer_outputs
,
outputs
=
[
node
.
name
]
,
output_size
=
kernel
)
else
:
if
params
.
pool
==
0
:
...
...
@@ -406,7 +404,7 @@ class CaffeOpMapper(OpMapper):
# TODO(syf): The op has diff.
self
.
paddle_graph
.
add_layer
(
kernel
=
"fluid.layers.pool2d"
,
inputs
=
{
"input"
:
x
.
name
},
inputs
=
{
"input"
:
input
.
name
},
outputs
=
[
node
.
name
],
pool_size
=
kernel
,
pool_type
=
string
(
"avg"
),
...
...
@@ -456,7 +454,7 @@ class CaffeOpMapper(OpMapper):
data
.
append
(
np
.
zeros
([
output_c
]).
astype
(
'float32'
).
astype
(
'float32'
))
else
:
data
=
adjust_parameters
(
node
)
data
=
_
adjust_parameters
(
node
)
# Reshape the parameters to Paddle's ordering
transpose_order
=
(
1
,
0
)
w
=
data
[
0
]
...
...
@@ -605,20 +603,21 @@ class CaffeOpMapper(OpMapper):
input
=
self
.
graph
.
get_input_node
(
node
,
idx
=
0
,
copy
=
True
)
params
=
node
.
layer
.
prelu_param
mode_bool
=
params
.
channel_shared
output_shape
=
node
.
out_shapes
[
0
]
if
mode_bool
:
mode
=
'all'
num_parameters
=
1
else
:
mode
=
'channel'
num_parameters
=
output_shape
[
1
]
data
=
node
.
data
assert
data
is
not
None
,
'The parameter of {} (type is {}) is not set. You need to use python package of caffe to set the default value.'
.
format
(
node
.
name
,
node
.
layer_type
)
kernel_weight_name
=
node
.
name
+
'_weights'
self
.
params
[
kernel_weight_name
]
=
data
[
0
]
self
.
params
[
kernel_weight_name
]
=
np
.
squeeze
(
data
[
0
])
self
.
paddle_graph
.
add_layer
(
kernel
=
"paddle.static.nn.create_parameter"
,
inputs
=
{},
outputs
=
[
kernel_weight_name
],
shape
=
self
.
params
[
kernel_weight_name
].
shape
,
shape
=
[
num_parameters
]
,
dtype
=
string
(
str
(
self
.
params
[
kernel_weight_name
].
dtype
)),
name
=
string
(
kernel_weight_name
))
self
.
paddle_graph
.
add_layer
(
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录