Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
1eac2763
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
1eac2763
编写于
12月 17, 2017
作者:
S
sweetsky0901
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add spp avg
上级
ea093283
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
51 addition
and
11 deletion
+51
-11
paddle/operators/spp_op.cc
paddle/operators/spp_op.cc
+5
-0
paddle/operators/spp_op.h
paddle/operators/spp_op.h
+27
-7
python/paddle/v2/fluid/tests/test_spp_op.py
python/paddle/v2/fluid/tests/test_spp_op.py
+19
-4
未找到文件。
paddle/operators/spp_op.cc
浏览文件 @
1eac2763
...
...
@@ -30,6 +30,11 @@ class SppOpMaker : public framework::OpProtoAndCheckerMaker {
"N * M."
"M = C * H * W"
);
AddAttr
<
int
>
(
"pyramid_height"
,
"(int), multi level pooling"
);
AddAttr
<
std
::
string
>
(
"pooling_type"
,
"(string), pooling type, can be
\"
max
\"
for max-pooling "
"and
\"
avg
\"
for average-pooling."
)
.
InEnum
({
"max"
,
"avg"
});
AddComment
(
R"DOC(
"With spatial pyramid pooling, the input image can
be of any sizes. This not only allows arbitrary aspect
...
...
paddle/operators/spp_op.h
浏览文件 @
1eac2763
...
...
@@ -27,6 +27,8 @@ class SppKernel : public framework::OpKernel<T> {
const
framework
::
Tensor
*
in_x
=
context
.
Input
<
framework
::
Tensor
>
(
"X"
);
auto
*
out
=
context
.
Output
<
framework
::
Tensor
>
(
"Out"
);
int
pyramid_height
=
context
.
template
Attr
<
int
>(
"pyramid_height"
);
std
::
string
pooling_type
=
context
.
template
Attr
<
std
::
string
>(
"pooling_type"
);
out
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
out_stride
=
framework
::
stride
(
out
->
dims
());
int
input_h
=
in_x
->
dims
()[
2
];
...
...
@@ -48,10 +50,17 @@ class SppKernel : public framework::OpKernel<T> {
framework
::
DDim
output_shape
(
framework
::
make_ddim
(
output_shape_vec
));
out_level
.
mutable_data
<
T
>
(
output_shape
,
context
.
GetPlace
());
// pooling
math
::
Pool2dFunctor
<
DeviceContext
,
math
::
MaxPool
<
T
>
,
T
>
pool_forward
;
math
::
MaxPool
<
T
>
max_process
;
pool_forward
(
context
.
template
device_context
<
DeviceContext
>(),
*
in_x
,
kernel_size
,
strides
,
paddings
,
max_process
,
&
out_level
);
if
(
pooling_type
==
"max"
)
{
math
::
Pool2dFunctor
<
DeviceContext
,
math
::
MaxPool
<
T
>
,
T
>
pool_forward
;
math
::
MaxPool
<
T
>
max_process
;
pool_forward
(
context
.
template
device_context
<
DeviceContext
>(),
*
in_x
,
kernel_size
,
strides
,
paddings
,
max_process
,
&
out_level
);
}
else
if
(
pooling_type
==
"avg"
)
{
math
::
Pool2dFunctor
<
DeviceContext
,
math
::
AvgPool
<
T
>
,
T
>
pool_forward
;
math
::
AvgPool
<
T
>
avg_process
;
pool_forward
(
context
.
template
device_context
<
DeviceContext
>(),
*
in_x
,
kernel_size
,
strides
,
paddings
,
avg_process
,
&
out_level
);
}
// flatten pooling output shape
int
output_flatten_w
=
in_x
->
dims
()[
1
]
*
bins
*
bins
;
std
::
vector
<
int64_t
>
output_flatten_shape_vec
(
...
...
@@ -79,6 +88,8 @@ class SppGradKernel : public framework::OpKernel<T> {
framework
::
Tensor
*
in_x_grad
=
context
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"X"
));
int
pyramid_height
=
context
.
template
Attr
<
int
>(
"pyramid_height"
);
std
::
string
pooling_type
=
context
.
template
Attr
<
std
::
string
>(
"pooling_type"
);
auto
&
device_ctx
=
context
.
template
device_context
<
DeviceContext
>();
math
::
SetConstant
<
DeviceContext
,
T
>
zero
;
in_x_grad
->
mutable_data
<
T
>
(
context
.
GetPlace
());
...
...
@@ -130,10 +141,19 @@ class SppGradKernel : public framework::OpKernel<T> {
outgrad_level
.
ShareDataWith
(
outgrad_level
);
outgrad_level
.
Resize
(
out_shape
);
// pooling backward
math
::
MaxPool2dGradFunctor
<
DeviceContext
,
T
>
pool2d_backward
;
pool2d_backward
(
context
.
template
device_context
<
DeviceContext
>(),
*
in_x
,
if
(
pooling_type
==
"max"
)
{
math
::
MaxPool2dGradFunctor
<
DeviceContext
,
T
>
pool2d_backward
;
pool2d_backward
(
context
.
template
device_context
<
DeviceContext
>(),
*
in_x
,
*&
out_level
,
*&
outgrad_level
,
kernel_size
,
strides
,
paddings
,
in_x_grad
);
}
else
if
(
pooling_type
==
"avg"
)
{
math
::
Pool2dGradFunctor
<
DeviceContext
,
math
::
AvgPoolGrad
<
T
>
,
T
>
pool_backward
;
math
::
AvgPoolGrad
<
T
>
avg_process
;
pool_backward
(
context
.
template
device_context
<
DeviceContext
>(),
*
in_x
,
*&
out_level
,
*&
outgrad_level
,
kernel_size
,
strides
,
paddings
,
in_x_grad
);
paddings
,
avg_process
,
in_x_grad
);
}
}
}
};
...
...
python/paddle/v2/fluid/tests/test_spp_op.py
浏览文件 @
1eac2763
...
...
@@ -2,6 +2,7 @@ import unittest
import
numpy
as
np
from
op_test
import
OpTest
from
test_pool2d_op
import
max_pool2D_forward_naive
from
test_pool2d_op
import
avg_pool2D_forward_naive
class
TestSppOp
(
OpTest
):
...
...
@@ -24,8 +25,8 @@ class TestSppOp(OpTest):
bins
.
astype
(
"double"
)).
astype
(
"int32"
)
padding
[
1
]
=
(
(
kernel_size
[
1
]
*
bins
-
wsize
+
1
)
/
2
).
astype
(
"int32"
)
out_level
=
max_
pool2D_forward_naive
(
input
,
kernel_size
,
kernel_size
,
padding
)
out_level
=
self
.
pool2D_forward_naive
(
input
,
kernel_size
,
kernel_size
,
padding
)
out_level_flatten
.
append
(
out_level
.
reshape
(
nsize
,
bins
*
bins
*
csize
))
if
i
==
0
:
...
...
@@ -34,7 +35,10 @@ class TestSppOp(OpTest):
output
=
np
.
concatenate
((
output
,
out_level_flatten
[
i
]),
1
)
# output = np.concatenate(out_level_flatten.tolist(), 0);
self
.
inputs
=
{
'X'
:
input
.
astype
(
'float32'
),
}
self
.
attrs
=
{
'pyramid_height'
:
self
.
pyramid_height
}
self
.
attrs
=
{
'pyramid_height'
:
self
.
pyramid_height
,
'pooling_type'
:
self
.
pool_type
}
self
.
outputs
=
{
'Out'
:
output
.
astype
(
'float32'
)}
...
...
@@ -42,11 +46,22 @@ class TestSppOp(OpTest):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
'Out'
,
max_relative_error
=
0.05
)
if
self
.
pool_type
!=
"avg"
:
self
.
check_grad
([
'X'
],
'Out'
,
max_relative_error
=
0.05
)
def
init_test_case
(
self
):
self
.
shape
=
[
3
,
2
,
4
,
4
]
self
.
pyramid_height
=
3
self
.
pool2D_forward_naive
=
max_pool2D_forward_naive
self
.
pool_type
=
"max"
class
TestCase2
(
TestSppOp
):
def
init_test_case
(
self
):
self
.
shape
=
[
3
,
2
,
4
,
4
]
self
.
pyramid_height
=
3
self
.
pool2D_forward_naive
=
avg_pool2D_forward_naive
self
.
pool_type
=
"avg"
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录