Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
fcfce484
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
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看板
提交
fcfce484
编写于
10月 09, 2017
作者:
C
chengduoZH
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
follow coments
上级
14b2c98f
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
65 addition
and
29 deletion
+65
-29
paddle/operators/CMakeLists.txt
paddle/operators/CMakeLists.txt
+2
-1
paddle/operators/math/pooling.h
paddle/operators/math/pooling.h
+38
-4
paddle/operators/pool_with_index_op.cc
paddle/operators/pool_with_index_op.cc
+10
-10
paddle/operators/pool_with_index_op.cu
paddle/operators/pool_with_index_op.cu
+4
-4
python/paddle/v2/framework/tests/test_pool_max_op.py
python/paddle/v2/framework/tests/test_pool_max_op.py
+11
-10
未找到文件。
paddle/operators/CMakeLists.txt
浏览文件 @
fcfce484
...
@@ -75,10 +75,11 @@ function(op_library TARGET)
...
@@ -75,10 +75,11 @@ function(op_library TARGET)
file
(
APPEND
${
pybind_file
}
"USE_OP(reduce_sum);
\n
"
)
file
(
APPEND
${
pybind_file
}
"USE_OP(reduce_sum);
\n
"
)
endif
()
endif
()
# pool_with_index_op contains several operators
if
(
"
${
TARGET
}
"
STREQUAL
"pool_with_index_op"
)
if
(
"
${
TARGET
}
"
STREQUAL
"pool_with_index_op"
)
set
(
pybind_flag 1
)
set
(
pybind_flag 1
)
# It's enough to just adding one operator to pybind
# It's enough to just adding one operator to pybind
file
(
APPEND
${
pybind_file
}
"USE_OP(max
Pool2dWithI
ndex);
\n
"
)
file
(
APPEND
${
pybind_file
}
"USE_OP(max
_pool2d_with_i
ndex);
\n
"
)
endif
()
endif
()
# pybind USE_NO_KERNEL_OP
# pybind USE_NO_KERNEL_OP
...
...
paddle/operators/math/pooling.h
浏览文件 @
fcfce484
...
@@ -21,15 +21,26 @@ limitations under the License. */
...
@@ -21,15 +21,26 @@ limitations under the License. */
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
namespace
math
{
namespace
math
{
//////////////////////
#define FLT_MAX __FLT_MAX__ //
#define FLT_MAX \
__FLT_MAX__ // It might need to be placed in another file, but I'm still
// wondering where to put it
/*
* \brief Extracting simple operations from pooling.
* Both MaxPool and AvgPool need initial, compute and finalize operation.
* MaxPool initializes temp variable to the negative maximum to find the
* maximum value in the pooling field.
* AvgPool initializes temp variable to the zero to accumulate all values
* in pool pooling, and takes the average.
* MaxPoolGrad and AvgPoolGrad are gradient operations respectively.
*/
template
<
class
T
>
template
<
class
T
>
class
MaxPool
{
class
MaxPool
{
public:
public:
DEVICE
inline
T
initial
()
{
return
static_cast
<
T
>
(
-
FLT_MAX
);
}
DEVICE
inline
T
initial
()
{
return
static_cast
<
T
>
(
-
FLT_MAX
);
}
DEVICE
inline
void
compute
(
T
&
y
,
const
T
&
x
)
{
y
=
y
>
x
?
y
:
x
;
}
DEVICE
inline
void
compute
(
T
&
y
,
const
T
&
x
)
{
y
=
y
>
x
?
y
:
x
;
}
DEVICE
inline
void
finalize
(
T
&
y
,
const
T
&
poo
_size
)
{}
DEVICE
inline
void
finalize
(
T
&
y
,
const
T
&
poo
l_field
)
{}
};
};
template
<
class
T
>
template
<
class
T
>
...
@@ -37,8 +48,9 @@ class AvgPool {
...
@@ -37,8 +48,9 @@ class AvgPool {
public:
public:
DEVICE
inline
T
initial
()
{
return
static_cast
<
T
>
(
0
);
}
DEVICE
inline
T
initial
()
{
return
static_cast
<
T
>
(
0
);
}
DEVICE
inline
void
compute
(
T
&
y
,
const
T
&
x
)
{
y
+=
x
;
}
DEVICE
inline
void
compute
(
T
&
y
,
const
T
&
x
)
{
y
+=
x
;
}
DEVICE
inline
void
finalize
(
T
&
y
,
const
T
&
poo
_size
)
{
y
/=
poo_size
;
}
DEVICE
inline
void
finalize
(
T
&
y
,
const
T
&
poo
l_field
)
{
y
/=
pool_field
;
}
};
};
template
<
class
T
>
template
<
class
T
>
class
MaxPoolGrad
{
class
MaxPoolGrad
{
public:
public:
...
@@ -57,6 +69,20 @@ class AvgPoolGrad {
...
@@ -57,6 +69,20 @@ class AvgPoolGrad {
}
}
};
};
/*
* \brief Getting pooling results, and calculating gradient.
*
* In pool2d, all tensors are in NCHW format. In pool3d, all tensors are in
* NCDHW format.
*
* In max pooling, it is possible that the pooling region has multiple maximum
* elements.
* In this case, we should compute the gradient of the first maximum element.
* This is different from average pooling. So we rewrite the max_pool_grad:
* MaxPool2dGradFunctor, MaxPool3dGradFunctor.
*
*/
template
<
typename
Place
,
typename
PoolProcess
,
typename
T
>
template
<
typename
Place
,
typename
PoolProcess
,
typename
T
>
class
Pool2dFunctor
{
class
Pool2dFunctor
{
public:
public:
...
@@ -117,6 +143,14 @@ class MaxPool3dGradFunctor {
...
@@ -117,6 +143,14 @@ class MaxPool3dGradFunctor {
std
::
vector
<
int
>&
strides
,
std
::
vector
<
int
>&
paddings
);
std
::
vector
<
int
>&
strides
,
std
::
vector
<
int
>&
paddings
);
};
};
/*
* \brief Getting max pooling results and corresponding max index, and
* calculating gradient.
* In sub-sampling-pooling, it is necessary to know max element index.
* In pool2d, all tensors are in NCHW format. In pool3d, all tensors are in
* NCDHW format.
*
*/
template
<
typename
Place
,
typename
T
>
template
<
typename
Place
,
typename
T
>
class
MaxPool2dWithIndexFunctor
{
class
MaxPool2dWithIndexFunctor
{
public:
public:
...
...
paddle/operators/pool_with_index_op.cc
浏览文件 @
fcfce484
...
@@ -17,7 +17,7 @@ limitations under the License. */
...
@@ -17,7 +17,7 @@ limitations under the License. */
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
int
OutputSizeMaxPool
(
int
input_size
,
int
filter_size
,
int
padding
,
in
line
in
t
OutputSizeMaxPool
(
int
input_size
,
int
filter_size
,
int
padding
,
int
stride
)
{
int
stride
)
{
int
output_size
=
(
input_size
-
filter_size
+
2
*
padding
)
/
stride
+
1
;
int
output_size
=
(
input_size
-
filter_size
+
2
*
padding
)
/
stride
+
1
;
return
output_size
;
return
output_size
;
...
@@ -194,24 +194,24 @@ the input and ksize, strides, paddings parameters.
...
@@ -194,24 +194,24 @@ the input and ksize, strides, paddings parameters.
namespace
ops
=
paddle
::
operators
;
namespace
ops
=
paddle
::
operators
;
REGISTER_OP
(
max
Pool2dWithI
ndex
,
ops
::
MaxPoolWithIndexOp
,
REGISTER_OP
(
max
_pool2d_with_i
ndex
,
ops
::
MaxPoolWithIndexOp
,
ops
::
MaxPool2dWithIndexOpMaker
,
max
Pool2dWithI
ndex_grad
,
ops
::
MaxPool2dWithIndexOpMaker
,
max
_pool2d_with_i
ndex_grad
,
ops
::
MaxPoolWithIndexOpGrad
);
ops
::
MaxPoolWithIndexOpGrad
);
REGISTER_OP_CPU_KERNEL
(
REGISTER_OP_CPU_KERNEL
(
max
Pool2dWithI
ndex
,
max
_pool2d_with_i
ndex
,
ops
::
MaxPoolWithIndexKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
ops
::
MaxPoolWithIndexKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
REGISTER_OP_CPU_KERNEL
(
REGISTER_OP_CPU_KERNEL
(
max
Pool2dWithI
ndex_grad
,
max
_pool2d_with_i
ndex_grad
,
ops
::
MaxPoolWithIndexGradKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
)
ops
::
MaxPoolWithIndexGradKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
)
REGISTER_OP
(
max
Pool3dWithI
ndex
,
ops
::
MaxPoolWithIndexOp
,
REGISTER_OP
(
max
_pool3d_with_i
ndex
,
ops
::
MaxPoolWithIndexOp
,
ops
::
MaxPool3dWithIndexOpMaker
,
max
Pool3dWithI
ndex_grad
,
ops
::
MaxPool3dWithIndexOpMaker
,
max
_pool3d_with_i
ndex_grad
,
ops
::
MaxPoolWithIndexOpGrad
);
ops
::
MaxPoolWithIndexOpGrad
);
REGISTER_OP_CPU_KERNEL
(
REGISTER_OP_CPU_KERNEL
(
max
Pool3dWithI
ndex
,
max
_pool3d_with_i
ndex
,
ops
::
MaxPoolWithIndexKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
ops
::
MaxPoolWithIndexKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
REGISTER_OP_CPU_KERNEL
(
REGISTER_OP_CPU_KERNEL
(
max
Pool3dWithI
ndex_grad
,
max
_pool3d_with_i
ndex_grad
,
ops
::
MaxPoolWithIndexGradKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
)
ops
::
MaxPoolWithIndexGradKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
)
paddle/operators/pool_with_index_op.cu
浏览文件 @
fcfce484
...
@@ -17,15 +17,15 @@ limitations under the License. */
...
@@ -17,15 +17,15 @@ limitations under the License. */
namespace
ops
=
paddle
::
operators
;
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_GPU_KERNEL
(
REGISTER_OP_GPU_KERNEL
(
max
Pool2dWithI
ndex
,
max
_pool2d_with_i
ndex
,
ops
::
MaxPoolWithIndexKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
ops
::
MaxPoolWithIndexKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
REGISTER_OP_GPU_KERNEL
(
REGISTER_OP_GPU_KERNEL
(
max
Pool2dWithI
ndex_grad
,
max
_pool2d_with_i
ndex_grad
,
ops
::
MaxPoolWithIndexGradKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
)
ops
::
MaxPoolWithIndexGradKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
)
REGISTER_OP_GPU_KERNEL
(
REGISTER_OP_GPU_KERNEL
(
max
Pool3dWithI
ndex
,
max
_pool3d_with_i
ndex
,
ops
::
MaxPoolWithIndexKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
ops
::
MaxPoolWithIndexKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
REGISTER_OP_GPU_KERNEL
(
REGISTER_OP_GPU_KERNEL
(
max
Pool3dWithI
ndex_grad
,
max
_pool3d_with_i
ndex_grad
,
ops
::
MaxPoolWithIndexGradKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
)
ops
::
MaxPoolWithIndexGradKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
)
python/paddle/v2/framework/tests/test_pool_max_op.py
浏览文件 @
fcfce484
...
@@ -100,7 +100,8 @@ class TestMaxPoolWithIndex_Op(OpTest):
...
@@ -100,7 +100,8 @@ class TestMaxPoolWithIndex_Op(OpTest):
def
initTestCase
(
self
):
def
initTestCase
(
self
):
self
.
global_pool
=
True
self
.
global_pool
=
True
self
.
op_type
=
"maxPool3dWithIndex"
self
.
index
=
"max_pool3d_with_index"
self
.
op_type
=
"%s"
%
self
.
index
self
.
pool_forward_naive
=
max_pool3D_forward_naive
self
.
pool_forward_naive
=
max_pool3D_forward_naive
self
.
shape
=
[
2
,
3
,
5
,
5
,
5
]
self
.
shape
=
[
2
,
3
,
5
,
5
,
5
]
self
.
ksize
=
[
3
,
3
,
3
]
self
.
ksize
=
[
3
,
3
,
3
]
...
@@ -111,7 +112,7 @@ class TestMaxPoolWithIndex_Op(OpTest):
...
@@ -111,7 +112,7 @@ class TestMaxPoolWithIndex_Op(OpTest):
class
TestCase1
(
TestMaxPoolWithIndex_Op
):
class
TestCase1
(
TestMaxPoolWithIndex_Op
):
def
initTestCase
(
self
):
def
initTestCase
(
self
):
self
.
global_pool
=
True
self
.
global_pool
=
True
self
.
op_type
=
"max
Pool3dWithI
ndex"
self
.
op_type
=
"max
_pool3d_with_i
ndex"
self
.
pool_forward_naive
=
max_pool3D_forward_naive
self
.
pool_forward_naive
=
max_pool3D_forward_naive
self
.
shape
=
[
2
,
3
,
5
,
5
,
5
]
self
.
shape
=
[
2
,
3
,
5
,
5
,
5
]
self
.
ksize
=
[
3
,
3
,
3
]
self
.
ksize
=
[
3
,
3
,
3
]
...
@@ -122,7 +123,7 @@ class TestCase1(TestMaxPoolWithIndex_Op):
...
@@ -122,7 +123,7 @@ class TestCase1(TestMaxPoolWithIndex_Op):
class
TestCase2
(
TestMaxPoolWithIndex_Op
):
class
TestCase2
(
TestMaxPoolWithIndex_Op
):
def
initTestCase
(
self
):
def
initTestCase
(
self
):
self
.
global_pool
=
False
self
.
global_pool
=
False
self
.
op_type
=
"max
Pool3dWithI
ndex"
self
.
op_type
=
"max
_pool3d_with_i
ndex"
self
.
pool_forward_naive
=
max_pool3D_forward_naive
self
.
pool_forward_naive
=
max_pool3D_forward_naive
self
.
shape
=
[
2
,
3
,
7
,
7
,
7
]
self
.
shape
=
[
2
,
3
,
7
,
7
,
7
]
self
.
ksize
=
[
3
,
3
,
3
]
self
.
ksize
=
[
3
,
3
,
3
]
...
@@ -133,7 +134,7 @@ class TestCase2(TestMaxPoolWithIndex_Op):
...
@@ -133,7 +134,7 @@ class TestCase2(TestMaxPoolWithIndex_Op):
class
TestCase3
(
TestMaxPoolWithIndex_Op
):
class
TestCase3
(
TestMaxPoolWithIndex_Op
):
def
initTestCase
(
self
):
def
initTestCase
(
self
):
self
.
global_pool
=
False
self
.
global_pool
=
False
self
.
op_type
=
"max
Pool3dWithI
ndex"
self
.
op_type
=
"max
_pool3d_with_i
ndex"
self
.
pool_forward_naive
=
max_pool3D_forward_naive
self
.
pool_forward_naive
=
max_pool3D_forward_naive
self
.
shape
=
[
2
,
3
,
7
,
7
,
7
]
self
.
shape
=
[
2
,
3
,
7
,
7
,
7
]
self
.
ksize
=
[
3
,
3
,
3
]
self
.
ksize
=
[
3
,
3
,
3
]
...
@@ -144,7 +145,7 @@ class TestCase3(TestMaxPoolWithIndex_Op):
...
@@ -144,7 +145,7 @@ class TestCase3(TestMaxPoolWithIndex_Op):
class
TestCase4
(
TestMaxPoolWithIndex_Op
):
class
TestCase4
(
TestMaxPoolWithIndex_Op
):
def
initTestCase
(
self
):
def
initTestCase
(
self
):
self
.
global_pool
=
True
self
.
global_pool
=
True
self
.
op_type
=
"max
Pool3dWithI
ndex"
self
.
op_type
=
"max
_pool3d_with_i
ndex"
self
.
pool_forward_naive
=
max_pool3D_forward_naive
self
.
pool_forward_naive
=
max_pool3D_forward_naive
self
.
shape
=
[
2
,
3
,
5
,
5
,
5
]
self
.
shape
=
[
2
,
3
,
5
,
5
,
5
]
self
.
ksize
=
[
3
,
3
,
3
]
self
.
ksize
=
[
3
,
3
,
3
]
...
@@ -155,7 +156,7 @@ class TestCase4(TestMaxPoolWithIndex_Op):
...
@@ -155,7 +156,7 @@ class TestCase4(TestMaxPoolWithIndex_Op):
class
TestCase5
(
TestMaxPoolWithIndex_Op
):
class
TestCase5
(
TestMaxPoolWithIndex_Op
):
def
initTestCase
(
self
):
def
initTestCase
(
self
):
self
.
global_pool
=
True
self
.
global_pool
=
True
self
.
op_type
=
"max
Pool3dWithI
ndex"
self
.
op_type
=
"max
_pool3d_with_i
ndex"
self
.
pool_forward_naive
=
max_pool3D_forward_naive
self
.
pool_forward_naive
=
max_pool3D_forward_naive
self
.
shape
=
[
2
,
3
,
5
,
5
,
5
]
self
.
shape
=
[
2
,
3
,
5
,
5
,
5
]
self
.
ksize
=
[
3
,
3
,
3
]
self
.
ksize
=
[
3
,
3
,
3
]
...
@@ -166,7 +167,7 @@ class TestCase5(TestMaxPoolWithIndex_Op):
...
@@ -166,7 +167,7 @@ class TestCase5(TestMaxPoolWithIndex_Op):
class
TestCase6
(
TestMaxPoolWithIndex_Op
):
class
TestCase6
(
TestMaxPoolWithIndex_Op
):
def
initTestCase
(
self
):
def
initTestCase
(
self
):
self
.
global_pool
=
False
self
.
global_pool
=
False
self
.
op_type
=
"max
Pool2dWithI
ndex"
self
.
op_type
=
"max
_pool2d_with_i
ndex"
self
.
pool_forward_naive
=
max_pool2D_forward_naive
self
.
pool_forward_naive
=
max_pool2D_forward_naive
self
.
shape
=
[
2
,
3
,
7
,
7
]
self
.
shape
=
[
2
,
3
,
7
,
7
]
self
.
ksize
=
[
3
,
3
]
self
.
ksize
=
[
3
,
3
]
...
@@ -177,7 +178,7 @@ class TestCase6(TestMaxPoolWithIndex_Op):
...
@@ -177,7 +178,7 @@ class TestCase6(TestMaxPoolWithIndex_Op):
class
TestCase7
(
TestMaxPoolWithIndex_Op
):
class
TestCase7
(
TestMaxPoolWithIndex_Op
):
def
initTestCase
(
self
):
def
initTestCase
(
self
):
self
.
global_pool
=
False
self
.
global_pool
=
False
self
.
op_type
=
"max
Pool2dWithI
ndex"
self
.
op_type
=
"max
_pool2d_with_i
ndex"
self
.
pool_forward_naive
=
max_pool2D_forward_naive
self
.
pool_forward_naive
=
max_pool2D_forward_naive
self
.
shape
=
[
2
,
3
,
7
,
7
]
self
.
shape
=
[
2
,
3
,
7
,
7
]
self
.
ksize
=
[
3
,
3
]
self
.
ksize
=
[
3
,
3
]
...
@@ -188,7 +189,7 @@ class TestCase7(TestMaxPoolWithIndex_Op):
...
@@ -188,7 +189,7 @@ class TestCase7(TestMaxPoolWithIndex_Op):
class
TestCase8
(
TestMaxPoolWithIndex_Op
):
class
TestCase8
(
TestMaxPoolWithIndex_Op
):
def
initTestCase
(
self
):
def
initTestCase
(
self
):
self
.
global_pool
=
True
self
.
global_pool
=
True
self
.
op_type
=
"max
Pool2dWithI
ndex"
self
.
op_type
=
"max
_pool2d_with_i
ndex"
self
.
pool_forward_naive
=
max_pool2D_forward_naive
self
.
pool_forward_naive
=
max_pool2D_forward_naive
self
.
shape
=
[
2
,
3
,
5
,
5
]
self
.
shape
=
[
2
,
3
,
5
,
5
]
self
.
ksize
=
[
3
,
3
]
self
.
ksize
=
[
3
,
3
]
...
@@ -199,7 +200,7 @@ class TestCase8(TestMaxPoolWithIndex_Op):
...
@@ -199,7 +200,7 @@ class TestCase8(TestMaxPoolWithIndex_Op):
class
TestCase9
(
TestMaxPoolWithIndex_Op
):
class
TestCase9
(
TestMaxPoolWithIndex_Op
):
def
initTestCase
(
self
):
def
initTestCase
(
self
):
self
.
global_pool
=
True
self
.
global_pool
=
True
self
.
op_type
=
"max
Pool2dWithI
ndex"
self
.
op_type
=
"max
_pool2d_with_i
ndex"
self
.
pool_forward_naive
=
max_pool2D_forward_naive
self
.
pool_forward_naive
=
max_pool2D_forward_naive
self
.
shape
=
[
2
,
3
,
5
,
5
]
self
.
shape
=
[
2
,
3
,
5
,
5
]
self
.
ksize
=
[
3
,
3
]
self
.
ksize
=
[
3
,
3
]
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录