adaptive_pool2d

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

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:

hstart=floor(iHin/Hout)hend=ceil((i+1)Hin/Hout)wstart=floor(jWin/Wout)wend=ceil((j+1)Win/Wout)Output(i,j)=fracsum(Input[hstart:hend,wstart:wend])(hendhstart)(wendwstart)
Parameters
  • 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).

  • 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 with outputs. It cannot be set in average pooling type. Default False.

  • name (str, optional) – For detailed information, please refer to Name. Usually name is no need to set and None by default.

Returns

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

as input tensor.

Return type

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')