Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
6c6474cb
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
6c6474cb
编写于
10月 10, 2017
作者:
C
chengduoZH
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
follow coments
上级
fcfce484
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
54 addition
and
41 deletion
+54
-41
paddle/operators/CMakeLists.txt
paddle/operators/CMakeLists.txt
+8
-7
paddle/operators/math/pooling.h
paddle/operators/math/pooling.h
+12
-11
paddle/operators/pool_with_index_op.cc
paddle/operators/pool_with_index_op.cc
+34
-23
未找到文件。
paddle/operators/CMakeLists.txt
浏览文件 @
6c6474cb
...
@@ -55,12 +55,20 @@ function(op_library TARGET)
...
@@ -55,12 +55,20 @@ function(op_library TARGET)
set
(
pybind_flag 1
)
set
(
pybind_flag 1
)
endif
()
endif
()
# pool_op contains several operators
if
(
"
${
TARGET
}
"
STREQUAL
"pool_op"
)
if
(
"
${
TARGET
}
"
STREQUAL
"pool_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(pool2d);
\n
"
)
file
(
APPEND
${
pybind_file
}
"USE_OP(pool2d);
\n
"
)
endif
()
endif
()
# pool_with_index_op contains several operators
if
(
"
${
TARGET
}
"
STREQUAL
"pool_with_index_op"
)
set
(
pybind_flag 1
)
# It's enough to just adding one operator to pybind
file
(
APPEND
${
pybind_file
}
"USE_OP(max_pool2d_with_index);
\n
"
)
endif
()
# activation_op contains several operators
# activation_op contains several operators
if
(
"
${
TARGET
}
"
STREQUAL
"activation_op"
)
if
(
"
${
TARGET
}
"
STREQUAL
"activation_op"
)
set
(
pybind_flag 1
)
set
(
pybind_flag 1
)
...
@@ -75,13 +83,6 @@ function(op_library TARGET)
...
@@ -75,13 +83,6 @@ 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"
)
set
(
pybind_flag 1
)
# It's enough to just adding one operator to pybind
file
(
APPEND
${
pybind_file
}
"USE_OP(max_pool2d_with_index);
\n
"
)
endif
()
# pybind USE_NO_KERNEL_OP
# pybind USE_NO_KERNEL_OP
file
(
READ
${
TARGET
}
.cc TARGET_CONTENT
)
file
(
READ
${
TARGET
}
.cc TARGET_CONTENT
)
string
(
REGEX MATCH
"OperatorWithKernel"
regex_result
"
${
TARGET_CONTENT
}
"
)
string
(
REGEX MATCH
"OperatorWithKernel"
regex_result
"
${
TARGET_CONTENT
}
"
)
...
...
paddle/operators/math/pooling.h
浏览文件 @
6c6474cb
...
@@ -24,15 +24,16 @@ namespace math {
...
@@ -24,15 +24,16 @@ namespace math {
#define FLT_MAX \
#define FLT_MAX \
__FLT_MAX__ // It might need to be placed in another file, but I'm still
__FLT_MAX__ // It might need to be placed in another file, but I'm still
// wondering where to put it
// wondering where to put it
.
/*
/*
* \brief Extracting simple operations from pooling.
* \brief Extracting simple operations from pooling.
* Both MaxPool and AvgPool need initial, compute and finalize operation.
* Both MaxPool and AvgPool need "initial", "compute" and "finalize"
* operation.
* MaxPool initializes temp variable to the negative maximum to find the
* MaxPool initializes temp variable to the negative maximum to find the
* maximum value in the pooling field.
* maximum value in the pooling field.
* AvgPool initializes temp variable to the zero to accumulate all values
* AvgPool initializes temp variable to the zero to accumulate all values
* in pool pooling, and takes the average.
* in pool pooling, and
finally
takes the average.
* MaxPoolGrad and AvgPoolGrad are gradient operations respectively.
* MaxPoolGrad and AvgPoolGrad are gradient operations respectively.
*/
*/
template
<
class
T
>
template
<
class
T
>
...
@@ -72,17 +73,17 @@ class AvgPoolGrad {
...
@@ -72,17 +73,17 @@ class AvgPoolGrad {
/*
/*
* \brief Getting pooling results, and calculating gradient.
* \brief Getting pooling results, and calculating gradient.
*
*
* In pool2d, all tensors are in NCHW format. In pool3d, all tensors are in
* In pool2d, all tensors are in NCHW format. Where N is batch size, C is the
* NCDHW format.
* number of channels, H and W is the height and width of feature.
* In pool3d, all tensors are in NCDHW format. Where N is batch size, C is the
* number of channels, D, H and W is the depth, height and width of feature.
*
*
* In max pooling, it is possible that the pooling region has multiple maximum
* In max pooling, it is possible that the pooling region has multiple maximum
* elements.
* elements.
In this case, we should compute the gradient of the first maximum
*
In this case, we should compute the gradient of the first maximum
element.
* element.
* This is different from average pooling. So we rewrite the max_pool_grad:
* This is different from average pooling. So we rewrite the max_pool_grad:
* MaxPool2dGradFunctor, MaxPool3dGradFunctor.
* MaxPool2dGradFunctor, MaxPool3dGradFunctor.
*
*/
*/
template
<
typename
Place
,
typename
PoolProcess
,
typename
T
>
template
<
typename
Place
,
typename
PoolProcess
,
typename
T
>
class
Pool2dFunctor
{
class
Pool2dFunctor
{
public:
public:
...
@@ -146,10 +147,9 @@ class MaxPool3dGradFunctor {
...
@@ -146,10 +147,9 @@ class MaxPool3dGradFunctor {
/*
/*
* \brief Getting max pooling results and corresponding max index, and
* \brief Getting max pooling results and corresponding max index, and
* calculating gradient.
* calculating gradient.
* In
sub
-sampling-pooling, it is necessary to know max element index.
* In
up
-sampling-pooling, it is necessary to know max element index.
* In pool2d, all tensors are in NCHW format. In pool3d, all tensors are in
* In pool2d, all tensors are in NCHW format. In pool3d, all tensors are in
* NCDHW format.
* NCDHW format.
*
*/
*/
template
<
typename
Place
,
typename
T
>
template
<
typename
Place
,
typename
T
>
class
MaxPool2dWithIndexFunctor
{
class
MaxPool2dWithIndexFunctor
{
...
@@ -188,6 +188,7 @@ class MaxPool3dWithIndexGradFunctor {
...
@@ -188,6 +188,7 @@ class MaxPool3dWithIndexGradFunctor {
const
framework
::
Tensor
&
mask
,
std
::
vector
<
int
>&
ksize
,
const
framework
::
Tensor
&
mask
,
std
::
vector
<
int
>&
ksize
,
std
::
vector
<
int
>&
strides
,
std
::
vector
<
int
>&
paddings
);
std
::
vector
<
int
>&
strides
,
std
::
vector
<
int
>&
paddings
);
};
};
}
// namespace math
}
// namespace math
}
// namespace operators
}
// namespace operators
}
// namespace paddle
}
// namespace paddle
paddle/operators/pool_with_index_op.cc
浏览文件 @
6c6474cb
...
@@ -34,7 +34,7 @@ class MaxPoolWithIndexOp : public framework::OperatorWithKernel {
...
@@ -34,7 +34,7 @@ class MaxPoolWithIndexOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
"Out(Output) of Pooling should not be null."
);
"Out(Output) of Pooling should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Mask"
),
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Mask"
),
"
Out
(Output) of Pooling should not be null."
);
"
Mask
(Output) of Pooling should not be null."
);
auto
in_x_dims
=
ctx
->
GetInputDim
(
"X"
);
auto
in_x_dims
=
ctx
->
GetInputDim
(
"X"
);
...
@@ -52,13 +52,11 @@ class MaxPoolWithIndexOp : public framework::OperatorWithKernel {
...
@@ -52,13 +52,11 @@ class MaxPoolWithIndexOp : public framework::OperatorWithKernel {
}
}
PADDLE_ENFORCE
(
in_x_dims
.
size
()
-
ksize
.
size
()
==
2U
,
PADDLE_ENFORCE
(
in_x_dims
.
size
()
-
ksize
.
size
()
==
2U
,
"Pooling intput size and pooling size should be consistent"
);
"Intput size and pooling size should be consistent."
);
PADDLE_ENFORCE
(
ksize
.
size
()
==
2
||
ksize
.
size
()
==
3
,
"Pooling size size should be 2 elements. or 3 elements."
);
PADDLE_ENFORCE_EQ
(
ksize
.
size
(),
strides
.
size
(),
PADDLE_ENFORCE_EQ
(
ksize
.
size
(),
strides
.
size
(),
"
s
trides size and pooling size should be the same."
);
"
S
trides size and pooling size should be the same."
);
PADDLE_ENFORCE_EQ
(
ksize
.
size
(),
paddings
.
size
(),
PADDLE_ENFORCE_EQ
(
ksize
.
size
(),
paddings
.
size
(),
"
p
addings size and pooling size should be the same."
);
"
P
addings size and pooling size should be the same."
);
std
::
vector
<
int64_t
>
output_shape
({
in_x_dims
[
0
],
in_x_dims
[
1
]});
std
::
vector
<
int64_t
>
output_shape
({
in_x_dims
[
0
],
in_x_dims
[
1
]});
for
(
size_t
i
=
0
;
i
<
ksize
.
size
();
++
i
)
{
for
(
size_t
i
=
0
;
i
<
ksize
.
size
();
++
i
)
{
...
@@ -76,11 +74,9 @@ class MaxPoolWithIndexOpGrad : public framework::OperatorWithKernel {
...
@@ -76,11 +74,9 @@ class MaxPoolWithIndexOpGrad : public framework::OperatorWithKernel {
protected:
protected:
void
InferShape
(
framework
::
InferShapeContextBase
*
ctx
)
const
override
{
void
InferShape
(
framework
::
InferShapeContextBase
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"Input(X) must not be null."
);
"X(Input) of Pooling should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
framework
::
GradVarName
(
"X"
)),
PADDLE_ENFORCE
(
"Input(X@GRAD) should not be null."
);
ctx
->
HasOutput
(
framework
::
GradVarName
(
"X"
)),
"X@GRAD(Input@GRAD) of MaxPoolWithIndexOpGrad should not be null."
);
ctx
->
SetOutputDim
(
framework
::
GradVarName
(
"X"
),
ctx
->
GetInputDim
(
"X"
));
ctx
->
SetOutputDim
(
framework
::
GradVarName
(
"X"
),
ctx
->
GetInputDim
(
"X"
));
}
}
};
};
...
@@ -110,9 +106,10 @@ class MaxPool2dWithIndexOpMaker : public framework::OpProtoAndCheckerMaker {
...
@@ -110,9 +106,10 @@ class MaxPool2dWithIndexOpMaker : public framework::OpProtoAndCheckerMaker {
AddAttr
<
std
::
vector
<
int
>>
(
AddAttr
<
std
::
vector
<
int
>>
(
"ksize"
,
"ksize"
,
"
P
ooling size(height, width) of pooling operator."
"
The p
ooling size(height, width) of pooling operator."
"If globalPooling = true, ksize is ignored and need not be "
"If globalPooling = true, ksize is ignored and need not be "
"specified."
);
// TODO(Add checker)
"specified."
);
// TODO(Chengduo): Add checker. (Currently,
// TypedAttrChecker don't support vector type.)
AddAttr
<
bool
>
(
AddAttr
<
bool
>
(
"globalPooling"
,
"globalPooling"
,
"Whether to use the globalPooling."
"Whether to use the globalPooling."
...
@@ -123,15 +120,21 @@ class MaxPool2dWithIndexOpMaker : public framework::OpProtoAndCheckerMaker {
...
@@ -123,15 +120,21 @@ class MaxPool2dWithIndexOpMaker : public framework::OpProtoAndCheckerMaker {
AddAttr
<
std
::
vector
<
int
>>
(
"strides"
,
AddAttr
<
std
::
vector
<
int
>>
(
"strides"
,
"Strides(height, width) of pooling operator."
"Strides(height, width) of pooling operator."
"Default {1,1}."
)
"Default {1,1}."
)
.
SetDefault
({
1
,
1
});
// TODO(Add checker)
.
SetDefault
({
1
,
1
});
// TODO(Chengduo): Add checker. (Currently,
// TypedAttrChecker don't support vector type.)
AddAttr
<
std
::
vector
<
int
>>
(
"paddings"
,
AddAttr
<
std
::
vector
<
int
>>
(
"paddings"
,
"Paddings(height, width) of pooling operator."
"Paddings(height, width) of pooling operator."
"Default {0,0}."
)
"Default {0,0}."
)
.
SetDefault
({
0
,
0
});
// TODO(Add checker)
.
SetDefault
({
0
,
0
});
// TODO(Chengduo): Add checker. (Currently,
// TypedAttrChecker don't support vector type.)
AddComment
(
R"DOC(
AddComment
(
R"DOC(
The maxPooling2d with index operation calculates the output and the mask based on
The maxPooling2d with index operation calculates the output and the mask
the input and ksize, strides, paddings parameters.
based on the input and ksize, strides, paddings parameters. Input(X) and
output(Out, Mask) are in NCHW format. Where N is batch size, C is the
number of channels, H and W is the height and width of feature.
Parameters(ksize, strides, paddings) are two elements.
These two elements represent height and width, respectively.
)DOC"
);
)DOC"
);
}
}
};
};
...
@@ -162,9 +165,10 @@ class MaxPool3dWithIndexOpMaker : public framework::OpProtoAndCheckerMaker {
...
@@ -162,9 +165,10 @@ class MaxPool3dWithIndexOpMaker : public framework::OpProtoAndCheckerMaker {
AddAttr
<
std
::
vector
<
int
>>
(
AddAttr
<
std
::
vector
<
int
>>
(
"ksize"
,
"ksize"
,
"
P
ooling size(depth, height, width) of pooling operator."
"
The p
ooling size(depth, height, width) of pooling operator."
"If globalPooling = true, ksize is ignored and need not be "
"If globalPooling = true, ksize is ignored and need not be "
"specified."
);
// TODO(Add checker)
"specified."
);
// TODO(Chengduo): Add checker. (Currently,
// TypedAttrChecker don't support vector type.)
AddAttr
<
bool
>
(
AddAttr
<
bool
>
(
"globalPooling"
,
"globalPooling"
,
"Whether to use the globalPooling."
"Whether to use the globalPooling."
...
@@ -176,19 +180,26 @@ class MaxPool3dWithIndexOpMaker : public framework::OpProtoAndCheckerMaker {
...
@@ -176,19 +180,26 @@ class MaxPool3dWithIndexOpMaker : public framework::OpProtoAndCheckerMaker {
"strides"
,
"strides"
,
"Strides(depth, height, width) of pooling operator."
"Strides(depth, height, width) of pooling operator."
"Default {1,1,1}."
)
"Default {1,1,1}."
)
.
SetDefault
({
1
,
1
,
1
});
// TODO(Add checker)
.
SetDefault
({
1
,
1
,
1
});
// TODO(Chengduo): Add checker. (Currently,
// TypedAttrChecker don't support vector type.)
AddAttr
<
std
::
vector
<
int
>>
(
AddAttr
<
std
::
vector
<
int
>>
(
"paddings"
,
"paddings"
,
"Paddings(depth, height, width) of pooling operator."
"Paddings(depth, height, width) of pooling operator."
"Default {0,0,0}."
)
"Default {0,0,0}."
)
.
SetDefault
({
0
,
0
,
0
});
// TODO(Add checker)
.
SetDefault
({
0
,
0
,
0
});
// TODO(Chengduo): Add checker. (Currently,
// TypedAttrChecker don't support vector type.)
AddComment
(
R"DOC(
AddComment
(
R"DOC(
The maxpooling3d with index operation calculates the output and the mask based on
The maxpooling3d with index operation calculates the output and the mask
the input and ksize, strides, paddings parameters.
based on the input and ksize, strides, paddings parameters.
Input(X) and output(Out, Mask) are in NCDHW format. Where N is batch
size, C is the number of channels, D, H and W is the depth, height and
width of feature. Parameters(ksize, strides, paddings) are three elements.
These three elements represent depth, height and width, respectively.
)DOC"
);
)DOC"
);
}
}
};
};
}
// namespace operators
}
// namespace operators
}
// namespace paddle
}
// namespace paddle
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录