|
|
|
@ -17,8 +17,15 @@ limitations under the License. */
|
|
|
|
|
namespace paddle {
|
|
|
|
|
namespace operators {
|
|
|
|
|
|
|
|
|
|
int PoolOutputSize(int input_size, int filter_size, int padding, int stride) {
|
|
|
|
|
int output_size = (input_size - filter_size + 2 * padding) / stride + 1;
|
|
|
|
|
int PoolOutputSize(int input_size, int filter_size, int padding, int stride,
|
|
|
|
|
bool ceil_mode) {
|
|
|
|
|
int output_size;
|
|
|
|
|
if (!ceil_mode) {
|
|
|
|
|
output_size = (input_size - filter_size + 2 * padding) / stride + 1;
|
|
|
|
|
} else {
|
|
|
|
|
output_size =
|
|
|
|
|
(input_size - filter_size + 2 * padding + stride - 1) / stride + 1;
|
|
|
|
|
}
|
|
|
|
|
PADDLE_ENFORCE(output_size > 0,
|
|
|
|
|
"Due to the settings of padding(%d), filter_size(%d) and "
|
|
|
|
|
"stride(%d), the output size is less than 0, please check "
|
|
|
|
@ -38,6 +45,7 @@ void PoolOp::InferShape(framework::InferShapeContext *ctx) const {
|
|
|
|
|
std::vector<int> ksize = ctx->Attrs().Get<std::vector<int>>("ksize");
|
|
|
|
|
std::vector<int> strides = ctx->Attrs().Get<std::vector<int>>("strides");
|
|
|
|
|
std::vector<int> paddings = ctx->Attrs().Get<std::vector<int>>("paddings");
|
|
|
|
|
bool ceil_mode = ctx->Attrs().Get<bool>("ceil_mode");
|
|
|
|
|
|
|
|
|
|
PADDLE_ENFORCE(in_x_dims.size() == 4 || in_x_dims.size() == 5,
|
|
|
|
|
"Pooling intput should be 4-D or 5-D tensor.");
|
|
|
|
@ -59,8 +67,8 @@ void PoolOp::InferShape(framework::InferShapeContext *ctx) const {
|
|
|
|
|
|
|
|
|
|
std::vector<int64_t> output_shape({in_x_dims[0], in_x_dims[1]});
|
|
|
|
|
for (size_t i = 0; i < ksize.size(); ++i) {
|
|
|
|
|
output_shape.push_back(
|
|
|
|
|
PoolOutputSize(in_x_dims[i + 2], ksize[i], paddings[i], strides[i]));
|
|
|
|
|
output_shape.push_back(PoolOutputSize(in_x_dims[i + 2], ksize[i],
|
|
|
|
|
paddings[i], strides[i], ceil_mode));
|
|
|
|
|
}
|
|
|
|
|
ctx->SetOutputDim("Out", framework::make_ddim(output_shape));
|
|
|
|
|
ctx->ShareLoD("X", "Out");
|
|
|
|
@ -167,6 +175,13 @@ Pool2dOpMaker::Pool2dOpMaker(OpProto *proto, OpAttrChecker *op_checker)
|
|
|
|
|
"use_cudnn",
|
|
|
|
|
"(bool, default false) Only used in cudnn kernel, need install cudnn")
|
|
|
|
|
.SetDefault(false);
|
|
|
|
|
AddAttr<bool>(
|
|
|
|
|
"ceil_mode",
|
|
|
|
|
"(bool, default false) Wether to use the ceil function to calculate "
|
|
|
|
|
"output height and width."
|
|
|
|
|
"True is the default. If it is set to False, the floor function will"
|
|
|
|
|
"be used")
|
|
|
|
|
.SetDefault(false);
|
|
|
|
|
AddAttr<std::string>(
|
|
|
|
|
"data_format",
|
|
|
|
|
"(string, default NCHW) Only used in "
|
|
|
|
@ -187,16 +202,21 @@ Parameters(ksize, strides, paddings) are two elements.
|
|
|
|
|
These two elements represent height and width, respectively.
|
|
|
|
|
The input(X) size and output(Out) size may be different.
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
Example:
|
|
|
|
|
Input:
|
|
|
|
|
X shape: $(N, C, H_{in}, W_{in})$
|
|
|
|
|
Output:
|
|
|
|
|
Out shape: $(N, C, H_{out}, W_{out})$
|
|
|
|
|
Where
|
|
|
|
|
$$
|
|
|
|
|
For ceil_mode = false:
|
|
|
|
|
$$
|
|
|
|
|
H_{out} = \frac{(H_{in} - ksize[0] + 2 * paddings[0])}{strides[0]} + 1 \\
|
|
|
|
|
W_{out} = \frac{(W_{in} - ksize[1] + 2 * paddings[1])}{strides[1]} + 1
|
|
|
|
|
$$
|
|
|
|
|
For ceil_mode = true:
|
|
|
|
|
$$
|
|
|
|
|
H_{out} = \frac{(H_{in} - ksize[0] + 2 * paddings[0] + strides[0] - 1)}{strides[0]} + 1 \\
|
|
|
|
|
W_{out} = \frac{(W_{in} - ksize[1] + 2 * paddings[1] + strides[1] - 1)}{strides[1]} + 1
|
|
|
|
|
$$
|
|
|
|
|
|
|
|
|
|
)DOC");
|
|
|
|
|
}
|
|
|
|
@ -251,6 +271,13 @@ Pool3dOpMaker::Pool3dOpMaker(OpProto *proto, OpAttrChecker *op_checker)
|
|
|
|
|
"use_cudnn",
|
|
|
|
|
"(bool, default false) Only used in cudnn kernel, need install cudnn")
|
|
|
|
|
.SetDefault(false);
|
|
|
|
|
AddAttr<bool>(
|
|
|
|
|
"ceil_mode",
|
|
|
|
|
"(bool, default false) Wether to use the ceil function to calculate "
|
|
|
|
|
"output height and width."
|
|
|
|
|
"True is the default. If it is set to False, the floor function will"
|
|
|
|
|
"be used")
|
|
|
|
|
.SetDefault(false);
|
|
|
|
|
AddAttr<std::string>(
|
|
|
|
|
"data_format",
|
|
|
|
|
"(string, default NCHW) Only used in "
|
|
|
|
@ -267,8 +294,8 @@ The pooling3d operation calculates the output based on
|
|
|
|
|
the input, pooling_type, ksize, strides, and paddings parameters.
|
|
|
|
|
Input(X) and output(Out) are in NCDHW format, where N is batch
|
|
|
|
|
size, C is the number of channels, and D, H and W are the depth, height and
|
|
|
|
|
width of the feature, respectively. Parameters(ksize, strides, paddings)
|
|
|
|
|
are three elements. These three elements represent depth, height and
|
|
|
|
|
width of the feature, respectively. Parameters(ksize, strides, paddings)
|
|
|
|
|
are three elements. These three elements represent depth, height and
|
|
|
|
|
width, respectively. The input(X) size and output(Out) size may be different.
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
@ -276,12 +303,18 @@ Example:
|
|
|
|
|
X shape: $(N, C, D_{in}, H_{in}, W_{in})$
|
|
|
|
|
Output:
|
|
|
|
|
Out shape: $(N, C, D_{out}, H_{out}, W_{out})$
|
|
|
|
|
Where
|
|
|
|
|
For ceil_mode = false:
|
|
|
|
|
$$
|
|
|
|
|
D_{out} = \frac{(D_{in} - ksize[0] + 2 * paddings[0])}{strides[0]} + 1 \\
|
|
|
|
|
H_{out} = \frac{(H_{in} - ksize[1] + 2 * paddings[1])}{strides[1]} + 1 \\
|
|
|
|
|
W_{out} = \frac{(W_{in} - ksize[2] + 2 * paddings[2])}{strides[2]} + 1
|
|
|
|
|
$$
|
|
|
|
|
For ceil_mode = true:
|
|
|
|
|
$$
|
|
|
|
|
D_{out} = \frac{(D_{in} - ksize[0] + 2 * paddings[0] + strides[0] -1)}{strides[0]} + 1 \\
|
|
|
|
|
H_{out} = \frac{(H_{in} - ksize[1] + 2 * paddings[1] + strides[1] -1)}{strides[1]} + 1 \\
|
|
|
|
|
W_{out} = \frac{(W_{in} - ksize[2] + 2 * paddings[2] + strides[2] -1)}{strides[2]} + 1
|
|
|
|
|
$$
|
|
|
|
|
|
|
|
|
|
)DOC");
|
|
|
|
|
}
|
|
|
|
|