adaptive_pool2d

paddle.fluid.layers.nn. adaptive_pool2d ( input, pool_size, pool_type='max', require_index=False, name=None ) [source]

Warning: API “paddle.fluid.layers.nn.adaptive_pool2d” is deprecated since 2.0.0, and will be removed in future versions.

This operation calculates the output based on the input, pool_size, pool_type parameters. Input(X) and output(Out) are in NCHW format, where N is batch size, C is the number of channels, H is the height of the feature, and W is the width of the feature. Parameters(pool_size) should contain two elements which represent height and width, respectively. Also the H and W dimensions of output(Out) is same as Parameter(pool_size). The output tensor shape will be [N, C, pool_size[0], pool_size[1]]

For average adaptive pool2d:

\[ \begin{align}\begin{aligned}hstart &= floor(i * H_{in} / H_{out})\\hend &= ceil((i + 1) * H_{in} / H_{out})\\wstart &= floor(j * W_{in} / W_{out})\\wend &= ceil((j + 1) * W_{in} / W_{out})\\\begin{split}Output(i ,j) &= \\frac{sum(Input[hstart:hend, wstart:wend])}{(hend - hstart) * (wend - wstart)}\end{split}\end{aligned}\end{align} \]
Args:
input (Tensor): The input tensor of pooling operator, which is a 4-D tensor

with shape [N, C, H, W]. The format of input tensor is NCHW, where N is batch size, C is the number of channels, H is the height of the feature, and W is the width of the feature. The data type is float32 or float64.

pool_size (int|list|tuple): The pool kernel size. If pool kernel size is a tuple or list,

it must contain two integers, (pool_size_Height, pool_size_Width).

System Message: WARNING/2 (/usr/local/lib/python3.8/site-packages/paddle/fluid/layers/nn.py:docstring of paddle.fluid.layers.nn.adaptive_pool2d, line 33)

Definition list ends without a blank line; unexpected unindent.

pool_type: (string), pooling type, can be “max” for max-pooling and “avg” for average-pooling require_index (bool): If true, the index of max pooling point will be returned along

System Message: ERROR/3 (/usr/local/lib/python3.8/site-packages/paddle/fluid/layers/nn.py:docstring of paddle.fluid.layers.nn.adaptive_pool2d, line 35)

Unexpected indentation.

with outputs. It cannot be set in average pooling type. Default False.

System Message: WARNING/2 (/usr/local/lib/python3.8/site-packages/paddle/fluid/layers/nn.py:docstring of paddle.fluid.layers.nn.adaptive_pool2d, line 36)

Block quote ends without a blank line; unexpected unindent.

name(str, optional): For detailed information, please refer

to Name. Usually name is no need to set and None by default.

Returns:
Tensor: The output tensor of adaptive pooling result. The data type is same

as input tensor.

Raises:

ValueError: ‘pool_type’ is not ‘max’ nor ‘avg’. ValueError: invalid setting ‘require_index’ true when ‘pool_type’ is ‘avg’. ValueError: ‘pool_size’ should be a list or tuple with length as 2.

Examples:
# average adaptive pool2d
# suppose input data in shape of [N, C, H, W], `pool_size` is [m, n],
# output shape is [N, C, m, n], adaptive pool divide H and W dimensions
# of input data into m * n grids averagely and performs poolings in each
# grid to get output.
# adaptive average pool performs calculations as follow:
#
#     for i in range(m):
#         for j in range(n):
#             hstart = floor(i * H / m)
#             hend = ceil((i + 1) * H / m)
#             wstart = floor(i * W / n)
#             wend = ceil((i + 1) * W / n)
#             output[:, :, i, j] = avg(input[:, :, hstart: hend, wstart: wend])
#
import paddle
paddle.enable_static()
data = paddle.rand(shape=[1,3,32,32])
pool_out = paddle.fluid.layers.adaptive_pool2d(
                  input=data,
                  pool_size=[3, 3],
                  pool_type='avg')

# max adaptive pool2d
# suppose input data in shape of [N, C, H, W], `pool_size` is [m, n],
# output shape is [N, C, m, n], adaptive pool divide H and W dimensions
# of input data into m * n grids averagely and performs poolings in each
# grid to get output.
# adaptive average pool performs calculations as follow:
#
#     for i in range(m):
#         for j in range(n):
#             hstart = floor(i * H / m)
#             hend = ceil((i + 1) * H / m)
#             wstart = floor(i * W / n)
#             wend = ceil((i + 1) * W / n)
#             output[:, :, i, j] = max(input[:, :, hstart: hend, wstart: wend])
#
import paddle
data = paddle.rand(shape=[1,3,32,32])
pool_out = paddle.fluid.layers.adaptive_pool2d(
                  input=data,
                  pool_size=[3, 3],
                  pool_type='max')