Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
487a13bb
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
487a13bb
编写于
10月 12, 2017
作者:
C
chengduo
提交者:
GitHub
10月 12, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #4742 from chengduoZH/refine_conv2_naive_func
refine conv2d naive function
上级
d3b8bffa
db4de4ff
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
70 addition
and
68 deletion
+70
-68
python/paddle/v2/framework/tests/test_conv2d_op.py
python/paddle/v2/framework/tests/test_conv2d_op.py
+70
-68
未找到文件。
python/paddle/v2/framework/tests/test_conv2d_op.py
浏览文件 @
487a13bb
...
...
@@ -3,71 +3,56 @@ import numpy as np
from
op_test
import
OpTest
def
conv2d_forward_naive
(
input
,
filter
,
group
,
conv_param
):
in_n
,
in_c
,
in_h
,
in_w
=
input
.
shape
out_c
,
f_c
,
f_h
,
f_w
=
filter
.
shape
assert
f_c
*
group
==
in_c
assert
np
.
mod
(
out_c
,
group
)
==
0
sub_out_c
=
out_c
/
group
stride
,
pad
=
conv_param
[
'stride'
],
conv_param
[
'pad'
]
out_h
=
1
+
(
in_h
+
2
*
pad
[
0
]
-
f_h
)
/
stride
[
0
]
out_w
=
1
+
(
in_w
+
2
*
pad
[
1
]
-
f_w
)
/
stride
[
1
]
out
=
np
.
zeros
((
in_n
,
out_c
,
out_h
,
out_w
))
input_pad
=
np
.
pad
(
input
,
((
0
,
),
(
0
,
),
(
pad
[
0
],
),
(
pad
[
1
],
)),
mode
=
'constant'
,
constant_values
=
0
)
for
i
in
range
(
out_h
):
for
j
in
range
(
out_w
):
for
g
in
range
(
group
):
input_pad_masked
=
\
input_pad
[:,
g
*
f_c
:(
g
+
1
)
*
f_c
,
i
*
stride
[
0
]:
i
*
stride
[
0
]
+
f_h
,
j
*
stride
[
1
]:
j
*
stride
[
1
]
+
f_w
]
f_sub
=
filter
[
g
*
sub_out_c
:(
g
+
1
)
*
sub_out_c
,
:,
:,
:]
for
k
in
range
(
sub_out_c
):
out
[:,
g
*
sub_out_c
+
k
,
i
,
j
]
=
\
np
.
sum
(
input_pad_masked
*
f_sub
[
k
,
:,
:,
:],
axis
=
(
1
,
2
,
3
))
return
out
class
TestConv2dOp
(
OpTest
):
def
setUp
(
self
):
self
.
init_groups
()
self
.
init_optype
()
batch_size
=
2
input_channels
=
3
input_height
=
5
input_width
=
5
output_channels
=
6
filter_height
=
3
filter_width
=
3
stride
=
1
padding
=
0
output_height
=
(
input_height
-
filter_height
+
2
*
padding
)
/
stride
+
1
output_width
=
(
input_width
-
filter_width
+
2
*
padding
)
/
stride
+
1
input
=
np
.
random
.
random
((
batch_size
,
input_channels
,
input_height
,
input_width
)).
astype
(
"float32"
)
filter
=
np
.
random
.
random
(
(
output_channels
,
input_channels
/
self
.
groups
,
filter_height
,
filter_width
)).
astype
(
"float32"
)
output
=
np
.
ndarray
(
(
batch_size
,
output_channels
,
output_height
,
output_width
))
self
.
init_op_type
()
self
.
init_group
()
self
.
init_test_case
()
conv2d_param
=
{
'stride'
:
self
.
stride
,
'pad'
:
self
.
pad
}
input
=
np
.
random
.
random
(
self
.
input_size
).
astype
(
"float32"
)
filter
=
np
.
random
.
random
(
self
.
filter_size
).
astype
(
"float32"
)
output
=
conv2d_forward_naive
(
input
,
filter
,
self
.
groups
,
conv2d_param
)
self
.
inputs
=
{
'Input'
:
input
,
'Filter'
:
filter
}
self
.
attrs
=
{
'strides'
:
[
1
,
1
]
,
'paddings'
:
[
0
,
0
]
,
'
dilations'
:
[
1
,
1
]
,
'
groups'
:
self
.
group
s
'strides'
:
self
.
stride
,
'paddings'
:
self
.
pad
,
'
groups'
:
self
.
groups
,
'
dilations'
:
self
.
dilation
s
}
output_group_channels
=
output_channels
/
self
.
groups
input_group_channels
=
input_channels
/
self
.
groups
for
batchid
in
xrange
(
batch_size
):
for
group
in
xrange
(
self
.
groups
):
for
outchannelid
in
range
(
group
*
output_group_channels
,
(
group
+
1
)
*
output_group_channels
):
for
rowid
in
xrange
(
output_height
):
for
colid
in
xrange
(
output_width
):
start_h
=
(
rowid
*
stride
)
-
padding
start_w
=
(
colid
*
stride
)
-
padding
output_value
=
0.0
for
inchannelid
in
range
(
group
*
input_group_channels
,
(
group
+
1
)
*
input_group_channels
):
for
frowid
in
xrange
(
filter_height
):
for
fcolid
in
xrange
(
filter_width
):
input_value
=
0.0
inrowid
=
start_h
+
frowid
incolid
=
start_w
+
fcolid
if
((
inrowid
>=
0
and
inrowid
<
input_height
)
and
(
incolid
>=
0
and
incolid
<
input_width
)):
input_value
=
input
[
batchid
][
inchannelid
][
inrowid
][
incolid
]
filter_value
=
filter
[
outchannelid
][
inchannelid
%
input_group_channels
][
frowid
][
fcolid
]
output_value
+=
input_value
*
filter_value
output
[
batchid
][
outchannelid
][
rowid
][
colid
]
=
output_value
self
.
outputs
=
{
'Output'
:
output
}
def
test_check_output
(
self
):
...
...
@@ -91,30 +76,47 @@ class TestConv2dOp(OpTest):
max_relative_error
=
0.05
,
no_grad_set
=
set
([
'Input'
]))
def
init_groups
(
self
):
def
init_test_case
(
self
):
# self.groups = 1
# self.op_type = "conv2d"
self
.
pad
=
[
0
,
0
]
self
.
stride
=
[
1
,
1
]
self
.
dilations
=
[
1
,
1
]
self
.
input_size
=
[
2
,
3
,
5
,
5
]
# NCHW
assert
np
.
mod
(
self
.
input_size
[
1
],
self
.
groups
)
==
0
f_c
=
self
.
input_size
[
1
]
/
self
.
groups
self
.
filter_size
=
[
6
,
f_c
,
3
,
3
]
def
init_group
(
self
):
self
.
groups
=
1
def
init_optype
(
self
):
def
init_op
_
type
(
self
):
self
.
op_type
=
"conv2d"
class
TestWithGroup
(
TestConv2dOp
):
def
init_group
s
(
self
):
def
init_group
(
self
):
self
.
groups
=
3
def
init_op_type
(
self
):
self
.
op_type
=
"conv2d"
class
TestCudnn2d
(
TestConv2dOp
):
def
init_optype
(
self
):
self
.
op_type
=
"conv_cudnn"
class
TestCudnn
(
TestConv2dOp
):
def
init_group
(
self
):
self
.
groups
=
1
class
TestCudnn2dWithGroup
(
TestConv2dOp
):
def
init_optype
(
self
):
def
init_op_type
(
self
):
self
.
op_type
=
"conv_cudnn"
def
init_groups
(
self
):
class
TestCudnnWithGroup
(
TestConv2dOp
):
def
init_group
(
self
):
self
.
groups
=
3
def
init_op_type
(
self
):
self
.
op_type
=
"conv_cudnn"
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录