pooling.py 85.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#   Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# TODO: define pooling functions
16 17
from ...fluid.layers import utils, LayerHelper
from ...tensor.manipulation import unsqueeze, squeeze
18
from ...fluid.data_feeder import check_type, check_variable_and_dtype
19
from paddle import _C_ops, _legacy_C_ops
Z
zhiboniu 已提交
20
from paddle import in_dynamic_mode
F
From00 已提交
21 22
from paddle.fluid.framework import _in_legacy_dygraph
from paddle.fluid.framework import in_dygraph_mode
23

24 25
__all__ = []

26

27 28 29 30 31
def _is_list_or_tuple(input):
    return isinstance(input, (list, tuple))


def _check_input(x, dimension):
32
    if len(x.shape) != dimension:
33 34 35
        raise ValueError(
            "Excepted Input X is {}-D tensor, but received {}-D {}".format(
                dimension, len(x.shape), type(x)))
36 37


38
def _check_instance(x, x_name, types=(int, float)):
39 40

    if not isinstance(x, types):
41 42 43
        raise ValueError(
            "Excepted {} type for {} but received type: {}. ".format(
                types, x_name, type(x)))
44 45


D
Double_V 已提交
46
def _check_value_limitation(x, x_name, min_limit=1e-3):
47

D
Double_V 已提交
48 49 50
    def _check_value(x, x_name, min_limit=1e-3):
        if isinstance(x, int) and min_limit is not None and x < min_limit:
            raise ValueError(
51 52
                "Excepted the input {} to be greater than {} but received x: {}. "
                .format(x_name, min_limit, x))
D
Double_V 已提交
53 54 55 56 57

    for ele in x:
        _check_value(ele, x_name)


58 59 60
def _zero_padding_in_batch_and_channel(padding, channel_last):
    if channel_last:
        return list(padding[0]) == [0, 0] and list(padding[-1]) == [0, 0]
61
    else:
62
        return list(padding[0]) == [0, 0] and list(padding[1]) == [0, 0]
63 64


65 66 67 68
def _exclude_padding_in_batch_and_channel(padding, channel_last):
    padding_ = padding[1:-1] if channel_last else padding[2:]
    padding_ = [elem for pad_a_dim in padding_ for elem in pad_a_dim]
    return padding_
69 70


71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
def _channel_last(data_format, num_dims):
    if num_dims == 1:
        if data_format not in ['NCL', 'NLC']:
            raise ValueError(
                "Attr(data_format) should be 'NCL' or 'NLC'. Received "
                "Attr(data_format): %s" % str(data_format))
        else:
            return True if data_format == "NLC" else False
    if num_dims == 2:
        if data_format not in ['NCHW', 'NHWC']:
            raise ValueError(
                "Attr(data_format) should be 'NCHW' or 'NHWC'. Received "
                "Attr(data_format): %s" % str(data_format))
        else:
            return True if data_format == "NHWC" else False
    if num_dims == 3:
        if data_format not in ['NCDHW', 'NDHWC']:
            raise ValueError(
                "Attr(data_format) should be 'NCDHW' or 'NDHWC'. Received "
                "Attr(data_format): %s" % str(data_format))
        else:
            return True if data_format == "NDHWC" else False
93 94


95 96 97 98 99 100 101 102 103
def _update_padding_nd(padding, num_dims, channel_last=False, ceil_mode=False):
    if isinstance(padding, str):
        padding = padding.upper()
        if padding not in ["SAME", "VALID"]:
            raise ValueError(
                "Unknown padding: '{}'. It can only be 'SAME' or 'VALID'.".
                format(padding))
        if padding == "VALID":
            if ceil_mode != False:
104
                raise ValueError(
105 106 107 108 109 110 111 112 113 114 115 116 117 118
                    "When Attr(padding) is \"VALID\", Attr(ceil_mode) must be False. "
                    "Received ceil_mode: True.")

            padding_algorithm = "VALID"
            padding = [0] * num_dims
        else:
            padding_algorithm = "SAME"
            padding = [0] * num_dims
    elif _is_list_or_tuple(padding):
        # for padding like
        # [(pad_before, pad_after), (pad_before, pad_after), ...]
        # padding for batch_dim and channel_dim included
        if len(padding) == 2 + num_dims and _is_list_or_tuple(padding[0]):
            if not _zero_padding_in_batch_and_channel(padding, channel_last):
119
                raise ValueError(
120 121 122
                    "Non-zero padding({}) in the batch or channel dimensions "
                    "is not supported.".format(padding))
            padding_algorithm = "EXPLICIT"
123 124
            padding = _exclude_padding_in_batch_and_channel(
                padding, channel_last)
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
            if utils._is_symmetric_padding(padding, num_dims):
                padding = padding[0::2]
        # for padding like [pad_before, pad_after, pad_before, pad_after, ...]
        elif len(padding) == 2 * num_dims and isinstance(padding[0], int):
            padding_algorithm = "EXPLICIT"
            padding = utils.convert_to_list(padding, 2 * num_dims, 'padding')
            if utils._is_symmetric_padding(padding, num_dims):
                padding = padding[0::2]
        # for padding like [pad_d1, pad_d2, ...]
        elif len(padding) == num_dims and isinstance(padding[0], int):
            padding_algorithm = "EXPLICIT"
            padding = utils.convert_to_list(padding, num_dims, 'padding')
        else:
            raise ValueError("Invalid padding: {}".format(padding))
    # for integer padding
140
    else:
141 142 143 144
        padding_algorithm = "EXPLICIT"
        padding = utils.convert_to_list(padding, num_dims, 'padding')
    return padding, padding_algorithm

145

146 147 148 149 150 151 152 153
def _expand_low_nd_padding(padding):
    #1d to 2d fake input
    if len(padding) == 2:
        padding = [0] * 2 + padding
    elif len(padding) == 1:
        padding = [0] + padding
    else:
        raise ValueError(
154 155
            "The size of padding's dimmention should be 1 or 2. But got padding={}"
            .format(padding))
156 157 158 159 160 161 162
    return padding


def avg_pool1d(x,
               kernel_size,
               stride=None,
               padding=0,
163
               exclusive=True,
164 165
               ceil_mode=False,
               name=None):
D
Double_V 已提交
166
    """
167 168
    This API implements average pooling 1d operation,
    See more details in :ref:`api_nn_pooling_AvgPool1d` .
169 170 171 172

    Args:
        x (Tensor): The input tensor of pooling operator which is a 3-D tensor with
                          shape [N, C, L]. where `N` is batch size, `C` is the number of channels,
173
                          `L` is the length of the feature. The data type is float32 or float64.
174
        kernel_size (int|list|tuple): The pool kernel size. If pool kernel size is a tuple or list,
175
            it must contain an integer.
176
        stride (int|list|tuple): The pool stride size. If pool stride size is a tuple or list,
177 178 179 180 181 182 183 184
            it must contain an integer.
        padding (string|int|list|tuple): The padding size. Padding could be in one of the following forms.
            1. A string in ['valid', 'same'].
            2. An int, which means the feature map is zero padded by size of `padding` on every sides.
            3. A list[int] or tuple(int) whose length is 1, which means the feature map is zero padded by the size of `padding[0]` on every sides.
            4. A list[int] or tuple(int) whose length is 2. It has the form [pad_before, pad_after].
            5. A list or tuple of pairs of integers. It has the form [[pad_before, pad_after], [pad_before, pad_after], ...]. Note that, the batch dimension and channel dimension should be [0,0] or (0,0).
            The default value is 0.
185
        exclusive (bool): Whether to exclude padding points in average pooling
186
                          mode, default is `True`.
187
        ceil_mode (bool): ${ceil_mode_comment}Whether to use the ceil function to calculate output height and width.
188
            If it is set to False, the floor function will be used. The default value is False.
189 190 191 192 193 194 195 196
        name(str, optional): For detailed information, please refer
                             to :ref:`api_guide_Name`. Usually name is no need to set and
                             None by default.
    Returns:
        Tensor: The output tensor of pooling result. The data type is same as input tensor.

    Examples:
        .. code-block:: python
C
Chen Long 已提交
197 198
          
            import paddle
199
            import paddle.nn as nn
C
Chen Long 已提交
200

201 202 203 204
            data = paddle.uniform([1, 3, 32], paddle.float32)
            AvgPool1D = nn.AvgPool1D(kernel_size=2, stride=2, padding=0)
            pool_out = AvgPool1D(data)
            # pool_out shape: [1, 3, 16]
205 206 207
    """
    """NCL to NCHW"""
    data_format = "NCHW"
Z
zhiboniu 已提交
208
    if not in_dynamic_mode():
209
        check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'avg_pool1d')
210
    _check_input(x, 3)
211
    x = unsqueeze(x, [2])
212
    kernel_size = utils.convert_to_list(kernel_size, 1, 'kernel_size')
213 214 215 216 217 218 219
    kernel_size = [1] + kernel_size
    if stride is None:
        stride = kernel_size
    else:
        stride = utils.convert_to_list(stride, 1, 'pool_stride')
        stride = [1] + stride

D
Double_V 已提交
220 221 222
    _check_value_limitation(kernel_size, "kernel_size", min_limit=1e-3)
    _check_value_limitation(stride, "stride", min_limit=1e-3)

223
    channel_last = _channel_last("NCL", 1)
224 225 226 227
    padding, padding_algorithm = _update_padding_nd(padding,
                                                    1,
                                                    channel_last=channel_last,
                                                    ceil_mode=ceil_mode)
228

229 230
    # use 2d to implenment 1d should expand padding in advance.
    padding = _expand_low_nd_padding(padding)
231

Z
zhiboniu 已提交
232
    if in_dynamic_mode():
233 234 235 236 237 238 239
        output = _legacy_C_ops.pool2d(x, 'pooling_type', 'avg', 'ksize',
                                      kernel_size, 'global_pooling', False,
                                      'strides', stride, 'paddings', padding,
                                      'padding_algorithm', padding_algorithm,
                                      'use_cudnn', True, 'ceil_mode', ceil_mode,
                                      'use_mkldnn', False, 'exclusive',
                                      exclusive, 'data_format', data_format)
240 241 242 243
        return squeeze(output, [2])

    op_type = 'pool2d'
    helper = LayerHelper(op_type, **locals())
244
    dtype = helper.input_dtype(input_param_name='x')
245 246
    pool_out = helper.create_variable_for_type_inference(dtype)

247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
    helper.append_op(type=op_type,
                     inputs={"X": x},
                     outputs={"Out": pool_out},
                     attrs={
                         "pooling_type": 'avg',
                         "ksize": kernel_size,
                         "global_pooling": False,
                         "strides": stride,
                         "paddings": padding,
                         "padding_algorithm": padding_algorithm,
                         "use_cudnn": True,
                         "ceil_mode": ceil_mode,
                         "use_mkldnn": False,
                         "exclusive": exclusive,
                         "data_format": data_format,
                     })
263 264 265 266

    return squeeze(pool_out, [2])


267
def avg_pool2d(x,
268 269 270 271
               kernel_size,
               stride=None,
               padding=0,
               ceil_mode=False,
272
               exclusive=True,
273 274
               divisor_override=None,
               data_format="NCHW",
275 276
               name=None):
    """
277 278
    This API implements average pooling 2d operation.
    See more details in :ref:`api_nn_pooling_AvgPool2d` .
D
Double_V 已提交
279

280
    Args:
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
        x (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"` or
                          `"NHWC"`, 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 if float32 or float64.
        kernel_size (int|list|tuple): The pool kernel size. If it is a tuple or list,
            it must contain two integers, (kernel_size_Height, kernel_size_Width).
            Otherwise, the pool kernel size will be a square of an int.
        stride (int|list|tuple): The stride size. If it is a tuple or list,
            it must contain two integers, (stride_Height, stride_Width).
            Otherwise, the stride size will be a square of an int.

        padding (string|int|list|tuple): The padding size. Padding could be in one of the following forms.
            1. A string in ['valid', 'same'].
            2. An int, which means the feature map is zero padded by size of `padding` on every sides.
            3. A list[int] or tuple(int) whose length is 2, [pad_height, pad_weight] whose value means the padding size of each dimension.
            4. A list[int] or tuple(int) whose length is 4. [pad_height_top, pad_height_bottom, pad_width_left, pad_width_right] whose value means the padding size of each side.
            5. A list or tuple of pairs of integers. It has the form [[pad_before, pad_after], [pad_before, pad_after], ...]. Note that, the batch dimension and channel dimension should be [0,0] or (0,0).
            The default value is 0.
        ceil_mode (bool): when True, will use `ceil` instead of `floor` to compute the output shape
301
        exclusive (bool): Whether to exclude padding points in average pooling
302 303 304 305 306
                          mode, default is `true`.
        divisor_override (float): if specified, it will be used as divisor, otherwise kernel_size will be used. Default None.
        data_format (string): The data format of the input and output data. An optional string from: `"NCHW"`, `"NHWC"`.
                        The default is `"NCHW"`. When it is `"NCHW"`, the data is stored in the order of:
                        `[batch_size, input_channels, input_height, input_width]`.
307 308 309
        name(str, optional): For detailed information, please refer
                             to :ref:`api_guide_Name`. Usually name is no need to set and
                             None by default.
C
Chen Long 已提交
310
    
311 312
    Returns:
        Tensor: The output tensor of pooling result. The data type is same as input tensor.
C
Chen Long 已提交
313
    
314 315
    Examples:
        .. code-block:: python
C
Chen Long 已提交
316 317 318 319 320
          
            import paddle
            import paddle.nn.functional as F
            
            # avg pool2d
321
            x = paddle.uniform([1, 3, 32, 32], paddle.float32)
C
Chen Long 已提交
322 323 324 325
            out = F.avg_pool2d(x,
                            kernel_size=2,
                            stride=2, padding=0)
            # out.shape [1, 3, 16, 16]
326
    """
327
    kernel_size = utils.convert_to_list(kernel_size, 2, 'pool_size')
328 329 330
    if stride is None:
        stride = kernel_size
    else:
331
        stride = utils.convert_to_list(stride, 2, 'pool_stride')
332

D
Double_V 已提交
333 334 335
    _check_value_limitation(kernel_size, "kernel_size", min_limit=1e-3)
    _check_value_limitation(stride, "stride", min_limit=1e-3)

336
    channel_last = _channel_last(data_format, 2)
337 338 339 340
    padding, padding_algorithm = _update_padding_nd(padding,
                                                    2,
                                                    channel_last,
                                                    ceil_mode=ceil_mode)
341

F
From00 已提交
342 343
    if in_dygraph_mode() or _in_legacy_dygraph():
        if in_dygraph_mode():
344 345 346
            output = _C_ops.pool2d(x, kernel_size, stride, padding, ceil_mode,
                                   exclusive, data_format, 'avg', False, False,
                                   padding_algorithm)
F
From00 已提交
347
        else:
348 349 350 351 352 353
            output = _legacy_C_ops.pool2d(
                x, 'pooling_type', 'avg', 'ksize', kernel_size,
                'global_pooling', False, 'padding_algorithm', padding_algorithm,
                'strides', stride, 'paddings', padding, 'use_cudnn', True,
                'ceil_mode', ceil_mode, 'use_mkldnn', False, 'exclusive',
                exclusive, 'data_format', data_format)
354 355 356 357 358
        if divisor_override is None:
            return output
        else:
            _check_instance(divisor_override, "divisor_override")
            return output * (kernel_size[0] * kernel_size[1]) / divisor_override
359

360
    op_type = 'pool2d'
361
    helper = LayerHelper(op_type, **locals())
362
    check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'avg_pool2d')
363
    dtype = helper.input_dtype(input_param_name='x')
364 365
    pool_out = helper.create_variable_for_type_inference(dtype)

366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
    helper.append_op(type=op_type,
                     inputs={"X": x},
                     outputs={"Out": pool_out},
                     attrs={
                         "pooling_type": "avg",
                         "ksize": kernel_size,
                         "global_pooling": False,
                         "strides": stride,
                         "paddings": padding,
                         "padding_algorithm": padding_algorithm,
                         "use_cudnn": True,
                         "ceil_mode": ceil_mode,
                         "use_mkldnn": False,
                         "exclusive": exclusive,
                         "data_format": data_format,
                     })
382

383 384 385 386 387
    if divisor_override is None:
        return pool_out
    else:
        _check_instance(divisor_override, "divisor_override")
        return pool_out * (kernel_size[0] * kernel_size[1]) / divisor_override
388 389


390 391 392 393 394
def avg_pool3d(x,
               kernel_size,
               stride=None,
               padding=0,
               ceil_mode=False,
395
               exclusive=True,
396 397 398
               divisor_override=None,
               data_format="NCDHW",
               name=None):
399
    """
400 401
    This API implements average pooling 3d operation.
    See more details in :ref:`api_nn_pooling_AvgPool3d` .
402 403

    Args:
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
        x (Tensor): The input tensor of pooling operator, which is a 5-D tensor with
                          shape [N, C, D, H, W], where `N` represents the batch size, `C` represents
                          the number of channels, `D`, `H` and `W` represent the depth, height and width of the feature respectively.
        kernel_size (int|list|tuple): The pool kernel size. If pool kernel size
            is a tuple or list, it must contain three integers,
            (kernel_size_Depth, kernel_size_Height, kernel_size_Width).
            Otherwise, the pool kernel size will be the cube of an int.
        stride (int|list|tuple): The pool stride size. If pool stride size is a tuple or list,
            it must contain three integers, [stride_Depth, stride_Height, stride_Width).
            Otherwise, the pool stride size will be a cube of an int.
        padding (string|int|list|tuple): The padding size. Padding could be in one of the following forms.
            1. A string in ['valid', 'same'].
            2. An int, which means the feature map is zero padded by size of `padding` on every sides.
            3. A list[int] or tuple(int) whose length is 3, [pad_depth, pad_height, pad_weight] whose value means the padding size of each dimension.
            4. A list[int] or tuple(int) whose length is 6. [pad_depth_front, pad_depth_back, pad_height_top, pad_height_bottom, pad_width_left, pad_width_right] whose value means the padding size of each side.
            5. A list or tuple of pairs of integers. It has the form [[pad_before, pad_after], [pad_before, pad_after], ...]. Note that, the batch dimension and channel dimension should be [0,0] or (0,0).
            The default value is 0.
        ceil_mode (bool): ${ceil_mode_comment}
422
        exclusive (bool): Whether to exclude padding points in average pooling
423 424 425 426 427
                          mode, default is True.
        divisor_override (int|float) if specified, it will be used as divisor, otherwise kernel_size will be used. Default None.
        data_format (string): The data format of the input and output data. An optional string from: `"NCDHW"`, `"NDHWC"`.
                        The default is `"NCDHW"`. When it is `"NCDHW"`, the data is stored in the order of:
                        `[batch_size, input_channels, input_depth, input_height, input_width]`.
428
        name(str, optional): For detailed information, please refer
429 430
                             to :ref:`api_guide_Name`. Usually name is no need to set and
                             None by default.
C
Chen Long 已提交
431
    
432
    Returns:
433
        Tensor: The output tensor of pooling result. The data type is same as input tensor.
434

435 436
    Examples:
        .. code-block:: python
C
Chen Long 已提交
437
          
438
          import paddle
C
Chen Long 已提交
439

440
          x = paddle.uniform([1, 3, 32, 32, 32], paddle.float32)
441 442 443 444 445 446 447
          # avg pool3d
          out = paddle.nn.functional.avg_pool3d(
                                            x,
                                            kernel_size = 2,
                                            stride = 2,
                                            padding=0)
          # out.shape: [1, 3, 16, 16, 16]
448
    """
449 450 451 452 453
    kernel_size = utils.convert_to_list(kernel_size, 3, 'pool_size')
    if stride is None:
        stride = kernel_size
    else:
        stride = utils.convert_to_list(stride, 3, 'pool_stride')
454

455
    channel_last = _channel_last(data_format, 3)
456 457 458 459
    padding, padding_algorithm = _update_padding_nd(padding,
                                                    3,
                                                    channel_last=channel_last,
                                                    ceil_mode=ceil_mode)
460

D
Double_V 已提交
461 462 463
    _check_value_limitation(kernel_size, "kernel_size", min_limit=1e-3)
    _check_value_limitation(stride, "stride", min_limit=1e-3)

F
From00 已提交
464 465
    if in_dygraph_mode() or _in_legacy_dygraph():
        if in_dygraph_mode():
466 467 468
            output = _C_ops.pool3d(x, kernel_size, stride, padding, ceil_mode,
                                   exclusive, data_format, 'avg', False, False,
                                   padding_algorithm)
F
From00 已提交
469
        if _in_legacy_dygraph():
470 471 472 473 474 475
            output = _legacy_C_ops.pool3d(
                x, 'pooling_type', 'avg', 'ksize', kernel_size, 'strides',
                stride, 'paddings', padding, 'global_pooling', False,
                'padding_algorithm', padding_algorithm, 'use_cudnn', True,
                'ceil_mode', ceil_mode, 'use_mkldnn', False, 'exclusive',
                exclusive, 'data_format', data_format)
476 477 478 479 480 481
        if divisor_override is None:
            return output
        else:
            _check_instance(divisor_override, "divisor_override")
            return output * (kernel_size[0] * kernel_size[1] *
                             kernel_size[2]) / divisor_override
482

483 484
    op_type = "pool3d"
    helper = LayerHelper(op_type, **locals())
485
    check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'max_pool3d')
486
    dtype = helper.input_dtype(input_param_name='x')
487 488
    pool_out = helper.create_variable_for_type_inference(dtype)
    outputs = {"Out": pool_out}
489

490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
    helper.append_op(type=op_type,
                     inputs={"X": x},
                     outputs=outputs,
                     attrs={
                         "pooling_type": 'avg',
                         "ksize": kernel_size,
                         "global_pooling": False,
                         "strides": stride,
                         "paddings": padding,
                         "padding_algorithm": padding_algorithm,
                         "use_cudnn": True,
                         "ceil_mode": ceil_mode,
                         "use_mkldnn": False,
                         "exclusive": exclusive,
                         "data_format": data_format,
                     })
506

507 508 509 510 511 512
    if divisor_override is None:
        return pool_out
    else:
        _check_instance(divisor_override, "divisor_override")
        return pool_out * (kernel_size[0] * kernel_size[1] *
                           kernel_size[2]) / divisor_override
513 514


515
def max_pool1d(x,
516 517 518
               kernel_size,
               stride=None,
               padding=0,
519
               return_mask=False,
520 521 522
               ceil_mode=False,
               name=None):
    """
523 524
    This API implements max pooling 1d opereation.
    See more details in :ref:`api_nn_pooling_MaxPool1d` .
525 526

    Args:
527 528 529
        x (Tensor): The input tensor of pooling operator which is a 3-D tensor with
                          shape [N, C, L], where `N` is batch size, `C` is the number of channels,
                          `L` is the length of the feature. The data type if float32 or float64.
530
        kernel_size (int|list|tuple): The pool kernel size. If pool kernel size is a tuple or list,
531
            it must contain an integer.
532
        stride (int|list|tuple): The pool stride size. If pool stride size is a tuple or list,
533 534 535 536 537 538 539 540
            it must contain an integer.
        padding (string|int|list|tuple): The padding size. Padding could be in one of the following forms.
            1. A string in ['valid', 'same'].
            2. An integer, which means the feature map is zero padded by size of `padding` on every sides.
            3. A list[int] or tuple(int) whose length is 1, which means the feature map is zero padded by the size of `padding[0]` on every sides.
            4. A list[int] or tuple(int) whose length is 2. It has the form [pad_before, pad_after].
            5. A list or tuple of pairs of integers. It has the form [[pad_before, pad_after], [pad_before, pad_after], ...]. Note that, the batch dimension and channel dimension should be [0,0] or (0,0).
            The default value is 0.
541
        return_mask (bool): Whether return the max indices along with the outputs. default is `False`.
542 543
        ceil_mode (bool): Whether to use the ceil function to calculate output height and width. False is the default.
            If it is set to False, the floor function will be used. Default False.
544 545 546 547 548
        name(str, optional): For detailed information, please refer
                             to :ref:`api_guide_Name`. Usually name is no need to set and
                             None by default.
    Returns:
        Tensor: The output tensor of pooling result. The data type is same as input tensor.
549

550 551 552
    Raises:
        ValueError: If `padding` is a string, but not "SAME" or "VALID".
        ValueError: If `padding` is "VALID", but `ceil_mode` is True.
553
        ShapeError: If the input is not a 3-D tensor.
554
        ShapeError: If the output's shape calculated is not greater than 0.
555

556 557
    Examples:
        .. code-block:: python
558

559 560
          import paddle
          import paddle.nn.functional as F
C
Chen Long 已提交
561

562
          data = paddle.uniform([1, 3, 32], paddle.float32)
563 564
          pool_out = F.max_pool1d(data, kernel_size=2, stride=2, padding=0)
          # pool_out shape: [1, 3, 16]
565
          pool_out, indices = F.max_pool1d(data, kernel_size=2, stride=2, padding=0, return_mask=True)
566
          # pool_out shape: [1, 3, 16],  indices shape: [1, 3, 16]
567
    """
568 569
    """NCL to NCHW"""
    data_format = "NCHW"
Z
zhiboniu 已提交
570
    if not in_dynamic_mode():
571
        check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'max_pool1d')
572 573 574
    _check_input(x, 3)
    x = unsqueeze(x, [2])
    kernel_size = [1] + utils.convert_to_list(kernel_size, 1, 'pool_size')
575 576 577
    if stride is None:
        stride = kernel_size
    else:
578
        stride = [1] + utils.convert_to_list(stride, 1, 'pool_stride')
579

580 581 582
    padding, padding_algorithm = _update_padding_nd(padding,
                                                    1,
                                                    ceil_mode=ceil_mode)
583

584 585
    # use 2d to implenment 1d should expand padding in advance.
    padding = _expand_low_nd_padding(padding)
586

F
From00 已提交
587 588
    if in_dygraph_mode():
        if return_mask:
589 590
            pool_out = _C_ops.max_pool2d_with_index(x, kernel_size, stride,
                                                    padding, False, False)
F
From00 已提交
591
            return (squeeze(pool_out[0], [2]),
592 593
                    squeeze(pool_out[1], [2])) if return_mask else squeeze(
                        pool_out[0], [2])
F
From00 已提交
594
        else:
595 596 597
            pool_out = _C_ops.pool2d(x, kernel_size, stride, padding, ceil_mode,
                                     True, data_format, 'max', False, False,
                                     padding_algorithm)
F
From00 已提交
598 599 600
            return squeeze(pool_out, [2])

    if _in_legacy_dygraph():
601
        if return_mask:
602
            pool_out = _legacy_C_ops.max_pool2d_with_index(
D
Double_V 已提交
603 604 605 606 607
                x, 'ksize', kernel_size, 'global_pooling', False, 'strides',
                stride, 'paddings', padding, 'padding_algorithm',
                padding_algorithm, 'use_cudnn', True, 'ceil_mode', ceil_mode,
                'use_mkldnn', False, 'exclusive', True, 'data_format',
                data_format)
608
            return (squeeze(pool_out[0], [2]),
609 610
                    squeeze(pool_out[1], [2])) if return_mask else squeeze(
                        pool_out[0], [2])
D
Double_V 已提交
611
        else:
612 613 614 615 616 617
            pool_out = _legacy_C_ops.pool2d(
                x, 'pooling_type', 'max', 'ksize', kernel_size,
                'global_pooling', False, 'padding_algorithm', padding_algorithm,
                'strides', stride, 'paddings', padding, 'use_cudnn', True,
                'ceil_mode', ceil_mode, 'use_mkldnn', False, 'exclusive', True,
                'data_format', data_format)
D
Double_V 已提交
618 619
            return squeeze(pool_out, [2])

620
    op_type = 'max_pool2d_with_index' if return_mask else "pool2d"
621
    helper = LayerHelper(op_type, **locals())
622
    dtype = helper.input_dtype(input_param_name='x')
623
    pool_out = helper.create_variable_for_type_inference(dtype)
624
    mask = helper.create_variable_for_type_inference('int32')
625 626
    outputs = {"Out": pool_out, "Mask": mask}

627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
    helper.append_op(type=op_type,
                     inputs={"X": x},
                     outputs=outputs,
                     attrs={
                         "pooling_type": 'max',
                         "ksize": kernel_size,
                         "global_pooling": False,
                         "strides": stride,
                         "paddings": padding,
                         "padding_algorithm": padding_algorithm,
                         "use_cudnn": True,
                         "ceil_mode": ceil_mode,
                         "use_mkldnn": False,
                         "exclusive": True,
                         "data_format": data_format,
                     })
643

644
    return (squeeze(pool_out, [2]),
645
            squeeze(mask, [2])) if return_mask else squeeze(pool_out, [2])
646 647


648 649 650 651
def _unpool_output_size(x, kernel_size, stride, padding, output_size):
    input_size = x.shape
    default_size = []
    for d in range(len(kernel_size)):
652 653
        default_size.append((input_size[-len(kernel_size) + d] - 1) *
                            stride[d] + kernel_size[d] - 2 * padding[d])
654 655 656 657 658 659 660 661 662
    if output_size is None:
        ret = default_size
    else:
        if len(output_size) == len(kernel_size) + 2:
            output_size = output_size[2:]
        if len(output_size) != len(kernel_size):
            raise ValueError(
                "output_size should be a sequence containing "
                "{} or {} elements, but it has a length of '{}'".format(
663 664
                    len(kernel_size),
                    len(kernel_size) + 2, len(output_size)))
665 666 667 668 669
        for d in range(len(kernel_size)):
            min_size = default_size[d] - stride[d]
            max_size = default_size[d] + stride[d]
            if not (min_size < output_size[d] < max_size):
                raise ValueError(
670 671
                    'invalid output_size "{}" (dim {} must be between {} and {})'
                    .format(output_size, d, min_size, max_size))
672 673 674 675 676

        ret = output_size
    return ret


677 678 679 680 681 682 683 684
def max_unpool1d(x,
                 indices,
                 kernel_size,
                 stride=None,
                 padding=0,
                 data_format="NCL",
                 output_size=None,
                 name=None):
685
    r"""
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
    This API implements max unpooling 1d opereation.
    `max_unpool1d` accepts the output of `max_pool1d` as input, 
    including the indices of the maximum value and calculate the partial inverse. 
    All non-maximum values ​​are set to zero.

    - Input: :math:`(N, C, L_{in})`
    - Output: :math:`(N, C, L_{out})`, where
    
    .. math::
        L_{out} = (L_{in} - 1) * stride - 2 * padding + kernel\_size

    or as given by :attr:`output_size` in the call operator.


    Args:
        x (Tensor): The input tensor of unpooling operator which is a 3-D tensor with
                          shape [N, C, L]. The format of input tensor is `"NCL"`, 
                          where `N` is batch size, `C` is the number of channels, `L` is
                          the length of the feature. The data type is float32 or float64.
        indices (Tensor): The indices given out by maxpooling1d which is a 3-D tensor with
                          shape [N, C, L]. The format of input tensor is `"NCL"` , 
                          where `N` is batch size, `C` is the number of channels, `L` is
                          the length of the featuree. The data type is float32 or float64.
        kernel_size (int|list|tuple): The unpool kernel size. If unpool kernel size is a tuple or list,
            it must contain an integer.
        stride (int|list|tuple): The unpool stride size. If unpool stride size is a tuple or list,
            it must contain an integer.
        padding (int | tuple): Padding that was added to the input.
        output_size(list|tuple, optional): The target output size. If output_size is not specified, 
                           the actual output shape will be automatically calculated by (input_shape,
                           kernel_size, stride, padding).
        data_format (string): The data format of the input and output data.
                        The default is `"NCL"`. When it is `"NCL"`, the data is stored in the order of:
                        `[batch_size, input_channels, input_length]`.
        name(str, optional): For detailed information, please refer
                             to :ref:`api_guide_Name`. Usually name is no need to set and
                             None by default.

    Returns:
        Tensor: The output tensor of unpooling result. 

    Examples:
        .. code-block:: python
        
            import paddle
            import paddle.nn.functional as F

            data = paddle.rand(shape=[1, 3, 16])
            pool_out, indices = F.max_pool1d(data, kernel_size=2, stride=2, padding=0, return_mask=True)
            # pool_out shape: [1, 3, 8],  indices shape: [1, 3, 8]
            unpool_out = F.max_unpool1d(pool_out, indices, kernel_size=2, padding=0)
            # unpool_out shape: [1, 3, 16]

    """
    """NCL to NCHW"""
    if data_format not in ["NCL"]:
        raise ValueError("Attr(data_format) should be 'NCL'. Received "
                         "Attr(data_format): %s." % str(data_format))
    data_format = "NCHW"
    x = unsqueeze(x, [2])
    indices = unsqueeze(indices, [2])
    kernel_size = [1] + utils.convert_to_list(kernel_size, 1, 'pool_size')
    if stride is None:
        stride = kernel_size
    else:
        stride = [1] + utils.convert_to_list(stride, 1, 'pool_stride')
    padding, padding_algorithm = _update_padding_nd(padding, 1)
    # use 2d to implenment 1d should expand padding in advance.
    padding = _expand_low_nd_padding(padding)

    output_size = _unpool_output_size(x, kernel_size, stride, padding,
                                      output_size)

X
xiaoting 已提交
759
    if in_dygraph_mode():
760 761
        output = _C_ops.unpool(x, indices, kernel_size, stride, padding,
                               output_size, data_format)
X
xiaoting 已提交
762 763
        return squeeze(output, [2])
    elif in_dynamic_mode():
764 765 766 767
        output = _legacy_C_ops.unpool(x, indices, 'unpooling_type', 'max',
                                      'ksize', kernel_size, 'strides', stride,
                                      'paddings', padding, "output_size",
                                      output_size, "data_format", data_format)
768 769 770 771 772 773 774
        return squeeze(output, [2])

    op_type = "unpool"
    helper = LayerHelper(op_type, **locals())
    dtype = helper.input_dtype(input_param_name="x")
    unpool_out = helper.create_variable_for_type_inference(dtype)

775 776 777 778 779 780 781 782 783 784 785 786 787
    helper.append_op(type=op_type,
                     inputs={
                         "X": x,
                         "Indices": indices
                     },
                     outputs={"Out": unpool_out},
                     attrs={
                         "unpooling_type": "max",
                         "ksize": kernel_size,
                         "strides": stride,
                         "paddings": padding,
                         "output_size": output_size
                     })
788 789 790
    return squeeze(unpool_out, [2])


791 792 793 794 795 796 797 798
def max_unpool2d(x,
                 indices,
                 kernel_size,
                 stride=None,
                 padding=0,
                 data_format="NCHW",
                 output_size=None,
                 name=None):
799
    r"""
800
    This API implements max unpooling 2d opereation.
801
    See more details in :ref:`api_nn_pooling_MaxUnPool2D` .
802

803 804

    Args:
805 806 807
        x (Tensor): The input tensor of unpooling 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,
808 809
                          `H` is the height of the feature, and `W` is the width of the
                          feature. The data type if float32 or float64.
810 811 812 813 814 815 816 817 818 819 820 821 822
        indices (Tensor): The indices given out by maxpooling2d 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 if float32 or float64.
        kernel_size (int|list|tuple): The unpool kernel size. If unpool kernel size is a tuple or list,
            it must contain an integer.
        stride (int|list|tuple): The unpool stride size. If unpool stride size is a tuple or list,
            it must contain an integer.
        padding (int | tuple): Padding that was added to the input.
        output_size(list|tuple, optional): The target output size. If output_size is not specified, 
                           the actual output shape will be automatically calculated by (input_shape,
                           kernel_size, padding).
823 824 825
        name(str, optional): For detailed information, please refer
                             to :ref:`api_guide_Name`. Usually name is no need to set and
                             None by default.
826

827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849

        - Input: :math:`(N, C, H_{in}, W_{in})`
        - Output: :math:`(N, C, H_{out}, W_{out})`, where

          .. math::
            H_{out} = (H_{in} - 1) \times \text{stride[0]} - 2 \times \text{padding[0]} + \text{kernel\_size[0]}

          .. math::
            W_{out} = (W_{in} - 1) \times \text{stride[1]} - 2 \times \text{padding[1]} + \text{kernel\_size[1]}

          or as given by :attr:`output_size` in the call operator

        Returns:
            Tensor: The output tensor of unpooling result. 

        Raises:
            ValueError: If the input is not a 4-D tensor.
            ValueError: If indeces shape is not equal input shape.
            

        Examples:
            .. code-block:: python
          
C
Chen Long 已提交
850 851
            import paddle
            import paddle.nn.functional as F
852

853
            data = paddle.rand(shape=[1,1,6,6])
854 855 856 857 858 859 860 861 862
            pool_out, indices = F.max_pool2d(data, kernel_size=2, stride=2, padding=0, return_mask=True)
            # pool_out shape: [1, 1, 3, 3],  indices shape: [1, 1, 3, 3]
            unpool_out = F.max_unpool2d(pool_out, indices, kernel_size=2, padding=0)
            # unpool_out shape: [1, 1, 6, 6]

            # specify a different output size than input size 
            unpool_out = F.max_unpool2d(pool_out, indices, kernel_size=2, padding=0, output_size=[7,7])
            # unpool_out shape: [1, 1, 7, 7] 

863 864
    """
    kernel_size = utils.convert_to_list(kernel_size, 2, 'pool_size')
865 866 867 868 869 870 871 872 873 874 875 876 877
    if stride is None:
        stride = kernel_size
    else:
        stride = utils.convert_to_list(stride, 2, 'pool_stride')
    padding = utils.convert_to_list(padding, 2, 'padding')

    if data_format not in ["NCHW"]:
        raise ValueError("Attr(data_format) should be 'NCHW'. Received "
                         "Attr(data_format): %s." % str(data_format))

    output_size = _unpool_output_size(x, kernel_size, stride, padding,
                                      output_size)

X
xiaoting 已提交
878
    if in_dygraph_mode():
879 880
        output = _C_ops.unpool(x, indices, kernel_size, stride, padding,
                               output_size, data_format)
X
xiaoting 已提交
881 882

    elif in_dynamic_mode():
883 884 885 886
        output = _legacy_C_ops.unpool(x, indices, 'unpooling_type', 'max',
                                      'ksize', kernel_size, 'strides', stride,
                                      'paddings', padding, "output_size",
                                      output_size, "data_format", data_format)
887 888 889 890 891 892 893
        return output

    op_type = "unpool"
    helper = LayerHelper(op_type, **locals())
    dtype = helper.input_dtype(input_param_name="x")
    unpool_out = helper.create_variable_for_type_inference(dtype)

894 895 896 897 898 899 900 901 902 903 904 905 906
    helper.append_op(type=op_type,
                     inputs={
                         "X": x,
                         "Indices": indices
                     },
                     outputs={"Out": unpool_out},
                     attrs={
                         "unpooling_type": "max",
                         "ksize": kernel_size,
                         "strides": stride,
                         "paddings": padding,
                         "output_size": output_size
                     })
907 908 909
    return unpool_out


910 911 912 913 914 915 916 917
def max_unpool3d(x,
                 indices,
                 kernel_size,
                 stride=None,
                 padding=0,
                 data_format="NCDHW",
                 output_size=None,
                 name=None):
918
    r"""
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
    This API implements max unpooling 3d opereation.
    `max_unpool3d` accepts the output of `max_pool3d` as input, 
    including the indices of the maximum value and calculate the partial inverse. 
    All non-maximum values ​​are set to zero.

    - Input: :math:`(N, C, D_{in}, H_{in}, W_{in})`
    - Output: :math:`(N, C, D_{out}, H_{out}, W_{out})`, where
    
    .. math::
        D_{out} = (D_{in} - 1) * stride[0] - 2 * padding[0] + kernel\_size[0]

    .. math::
        H_{out} = (H_{in} - 1) * stride[1] - 2 * padding[1] + kernel\_size[1]

    .. math::
        W_{out} = (W_{in} - 1) * stride[2] - 2 * padding[2] + kernel\_size[2]

    or as given by :attr:`output_size` in the call operator


    Args:
        x (Tensor): The input tensor of unpooling operator which is a 5-D tensor with
                          shape [N, C, D, H, W]. The format of input tensor is `"NCDHW"`, 
                          where `N` is batch size, `C` is the number of channels, `D` is
                          the depth of the feature, `H` is the height of the feature, 
                          and `W` is the width of the feature. The data type is float32 or float64.
        indices (Tensor): The indices given out by maxpooling3d which is a 5-D tensor with
                          shape [N, C, D, H, W]. The format of input tensor is `"NCDHW"` , 
                          where `N` is batch size, `C` is the number of channels, `D` is
                          the depth of the feature, `H` is the height of the feature, 
                          and `W` is the width of the feature. The data type is float32 or float64.
        kernel_size (int|list|tuple): The unpool kernel size. If unpool kernel size is a tuple or list,
            it must contain an integer.
        stride (int|list|tuple): The unpool stride size. If unpool stride size is a tuple or list,
            it must contain an integer.
        padding (int | tuple): Padding that was added to the input.
        output_size(list|tuple, optional): The target output size. If output_size is not specified, 
                           the actual output shape will be automatically calculated by (input_shape,
                           kernel_size, stride, padding).
        data_format (string): The data format of the input and output data.
                        The default is `"NCDHW"`. When it is `"NCDHW"`, the data is stored in the order of:
                        `[batch_size, input_channels, input_depth, input_height, input_width]`.
        name(str, optional): For detailed information, please refer
                             to :ref:`api_guide_Name`. Usually name is no need to set and
                             None by default.

    Returns:
        Tensor: The output tensor of unpooling result. 

    Examples:
        .. code-block:: python
        
            import paddle
            import paddle.nn.functional as F

            data = paddle.rand(shape=[1, 1, 4, 4, 6])
            pool_out, indices = F.max_pool3d(data, kernel_size=2, stride=2, padding=0, return_mask=True)
            # pool_out shape: [1, 1, 2, 2, 3],  indices shape: [1, 1, 2, 2, 3]
            unpool_out = F.max_unpool3d(pool_out, indices, kernel_size=2, padding=0)
            # unpool_out shape: [1, 1, 4, 4, 6]

    """
    kernel_size = utils.convert_to_list(kernel_size, 3, 'pool_size')
    if stride is None:
        stride = kernel_size
    else:
        stride = utils.convert_to_list(stride, 3, 'pool_stride')
    padding = utils.convert_to_list(padding, 3, 'padding')

    if data_format not in ["NCDHW"]:
        raise ValueError("Attr(data_format) should be 'NCDHW'. Received "
                         "Attr(data_format): %s." % str(data_format))

    output_size = _unpool_output_size(x, kernel_size, stride, padding,
                                      output_size)

X
xiaoting 已提交
995
    if in_dygraph_mode():
996 997
        output = _C_ops.unpool3d(x, indices, kernel_size, stride, padding,
                                 output_size, data_format)
X
xiaoting 已提交
998
    elif in_dynamic_mode():
999 1000 1001 1002
        output = _legacy_C_ops.unpool3d(x, indices, 'unpooling_type', 'max',
                                        'ksize', kernel_size, 'strides', stride,
                                        'paddings', padding, "output_size",
                                        output_size, "data_format", data_format)
1003 1004 1005 1006 1007 1008 1009
        return output

    op_type = "unpool3d"
    helper = LayerHelper(op_type, **locals())
    dtype = helper.input_dtype(input_param_name="x")
    unpool_out = helper.create_variable_for_type_inference(dtype)

1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
    helper.append_op(type=op_type,
                     inputs={
                         "X": x,
                         "Indices": indices
                     },
                     outputs={"Out": unpool_out},
                     attrs={
                         "unpooling_type": "max",
                         "ksize": kernel_size,
                         "strides": stride,
                         "paddings": padding,
                         "output_size": output_size
                     })
1023 1024 1025
    return unpool_out


1026 1027 1028 1029 1030 1031 1032 1033
def max_pool2d(x,
               kernel_size,
               stride=None,
               padding=0,
               return_mask=False,
               ceil_mode=False,
               data_format="NCHW",
               name=None):
W
Wei Shengyu 已提交
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
    """
    This API implements max pooling 2d operation.
    See more details in :ref:`api_nn_pooling_MaxPool2d` .
    Args:
        x (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"` or
                          `"NHWC"`, 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 if float32 or float64.
        kernel_size (int|list|tuple): The pool kernel size. If pool kernel size is a tuple or list,
            it must contain two integers, (kernel_size_Height, kernel_size_Width).
            Otherwise, the pool kernel size will be a square of an int.
        stride (int|list|tuple): The pool stride size. If pool stride size is a tuple or list,
            it must contain two integers, (stride_Height, stride_Width).
            Otherwise, the pool stride size will be a square of an int.
        padding (string|int|list|tuple): The padding size. Padding could be in one of the following forms.
            1. A string in ['valid', 'same'].
            2. An int, which means the feature map is zero padded by size of `padding` on every sides.
            3. A list[int] or tuple(int) whose length is 2, [pad_height, pad_weight] whose value means the padding size of each dimension.
            4. A list[int] or tuple(int) whose length is 4. [pad_height_top, pad_height_bottom, pad_width_left, pad_width_right] whose value means the padding size of each side.
            5. A list or tuple of pairs of integers. It has the form [[pad_before, pad_after], [pad_before, pad_after], ...]. Note that, the batch dimension and channel dimension should be [0,0] or (0,0).
            The default value is 0.
        ceil_mode (bool): when True, will use `ceil` instead of `floor` to compute the output shape
        return_mask (bool): Whether to return the max indices along with the outputs. Default False, only support `"NCHW"` data format
        data_format (string): The data format of the input and output data. An optional string from: `"NCHW"`, `"NHWC"`.
                        The default is `"NCHW"`. When it is `"NCHW"`, the data is stored in the order of:
                        `[batch_size, input_channels, input_height, input_width]`.
        name(str, optional): For detailed information, please refer
                             to :ref:`api_guide_Name`. Usually name is no need to set and
                             None by default.
    Returns:
        Tensor: The output tensor of pooling result. The data type is same as input tensor.

   Raises:
        ValueError: If `padding` is a string, but not "SAME" or "VALID".
        ValueError: If `padding` is "VALID", but `ceil_mode` is True.
        ShapeError: If the output's shape calculated is not greater than 0.

    Examples:
        .. code-block:: python
            import paddle
            import paddle.nn.functional as F
            import numpy as np

            # max pool2d
            x = paddle.to_tensor(np.random.uniform(-1, 1, [1, 3, 32, 32]).astype(np.float32))
            out = F.max_pool2d(x,
                                  kernel_size=2,
                                  stride=2, padding=0)
            # output.shape [1, 3, 16, 16]
            # for return_mask=True
            out, max_indices = F.max_pool2d(x,
                                               kernel_size=2,
                                               stride=2,
                                               padding=0,
                                               return_mask=True)
            # out.shape [1, 3, 16, 16], max_indices.shape [1, 3, 16, 16],
    """
1092
    kernel_size = utils.convert_to_list(kernel_size, 2, 'pool_size')
1093 1094 1095 1096 1097 1098 1099 1100 1101
    if stride is None:
        stride = kernel_size
    else:
        stride = utils.convert_to_list(stride, 2, 'pool_stride')

    if data_format not in ["NCHW", "NHWC"]:
        raise ValueError(
            "Attr(data_format) should be 'NCHW' or 'NHWC'. Received "
            "Attr(data_format): %s." % str(data_format))
1102 1103 1104

    channel_last = True if data_format == "NHWC" else False

1105 1106 1107 1108
    padding, padding_algorithm = _update_padding_nd(padding,
                                                    num_dims=2,
                                                    channel_last=channel_last,
                                                    ceil_mode=ceil_mode)
1109

1110
    if data_format == "NHWC" and return_mask:
D
Double_V 已提交
1111
        raise ValueError(
1112
            "When setting return_mask to true, data_format must be set to NCHW in API:max_pool2d"
D
Double_V 已提交
1113 1114
        )

F
From00 已提交
1115 1116
    if in_dygraph_mode():
        if return_mask:
1117 1118
            output = _C_ops.max_pool2d_with_index(x, kernel_size, stride,
                                                  padding, False, False)
F
From00 已提交
1119 1120
            return output if return_mask else output[0]
        else:
1121 1122 1123
            return _C_ops.pool2d(x, kernel_size, stride, padding, ceil_mode,
                                 True, data_format, 'max', False, False,
                                 padding_algorithm)
F
From00 已提交
1124 1125

    if _in_legacy_dygraph():
1126
        if return_mask:
1127
            output = _legacy_C_ops.max_pool2d_with_index(
D
Double_V 已提交
1128 1129 1130 1131 1132
                x, 'ksize', kernel_size, 'global_pooling', False, 'strides',
                stride, 'paddings', padding, 'padding_algorithm',
                padding_algorithm, 'use_cudnn', True, 'ceil_mode', ceil_mode,
                'use_mkldnn', False, 'exclusive', True, 'data_format',
                data_format)
1133
            return output if return_mask else output[0]
D
Double_V 已提交
1134
        else:
1135 1136 1137 1138 1139 1140
            output = _legacy_C_ops.pool2d(
                x, 'pooling_type', 'max', 'ksize', kernel_size,
                'global_pooling', False, 'padding_algorithm', padding_algorithm,
                'strides', stride, 'paddings', padding, 'use_cudnn', True,
                'ceil_mode', ceil_mode, 'use_mkldnn', False, 'exclusive', True,
                'data_format', data_format)
D
Double_V 已提交
1141
            return output
1142

1143
    op_type = 'max_pool2d_with_index' if return_mask else "pool2d"
1144
    helper = LayerHelper(op_type, **locals())
1145 1146
    check_variable_and_dtype(x, 'x', ['float16', 'float32', 'float64'],
                             'max_pool2d')
1147
    dtype = helper.input_dtype(input_param_name='x')
1148
    pool_out = helper.create_variable_for_type_inference(dtype)
1149
    mask = helper.create_variable_for_type_inference("int32")
1150
    outputs = {"Out": pool_out, "Mask": mask}
1151

1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
    helper.append_op(type=op_type,
                     inputs={"X": x},
                     outputs=outputs,
                     attrs={
                         "pooling_type": 'max',
                         "ksize": kernel_size,
                         "global_pooling": False,
                         "strides": stride,
                         "paddings": padding,
                         "padding_algorithm": padding_algorithm,
                         "use_cudnn": True,
                         "ceil_mode": ceil_mode,
                         "use_mkldnn": False,
                         "exclusive": True,
                         "data_format": data_format,
                     })
1168

1169
    return (pool_out, mask) if return_mask else pool_out
1170 1171 1172 1173 1174 1175


def max_pool3d(x,
               kernel_size,
               stride=None,
               padding=0,
1176
               return_mask=False,
1177 1178 1179 1180
               ceil_mode=False,
               data_format="NCDHW",
               name=None):
    """
1181 1182
    This API implements max pooling 2d operation.
    See more details in :ref:`api_nn_pooling_MaxPool3d` .
1183 1184
    Args:
        x (Tensor): The input tensor of pooling operator, which is a 5-D tensor with
D
Double_V 已提交
1185
                          shape [N, C, D, H, W]. The format of input tensor is `"NCDHW"` or `"NDHWC"`, where N represents batch size, C represents the number of channels, D, H and W represent the depth, height and width of the feature respectively.
1186
        kernel_size (int|list|tuple): The pool kernel size. If the kernel size
1187
            is a tuple or list, it must contain three integers,
1188
            (kernel_size_Depth, kernel_size_Height, kernel_size_Width).
1189
            Otherwise, the pool kernel size will be the cube of an int.
1190 1191
        stride (int|list|tuple): The pool stride size. If pool stride size is a tuple or list,
            it must contain three integers, [stride_Depth, stride_Height, stride_Width).
1192
            Otherwise, the pool stride size will be a cube of an int.
1193 1194 1195 1196 1197 1198 1199
        padding (string|int|list|tuple): The padding size. Padding could be in one of the following forms.
            1. A string in ['valid', 'same'].
            2. An int, which means the feature map is zero padded by size of `padding` on every sides.
            3. A list[int] or tuple(int) whose length is 3, [pad_depth, pad_height, pad_weight] whose value means the padding size of each dimension.
            4. A list[int] or tuple(int) whose length is 6. [pad_depth_front, pad_depth_back, pad_height_top, pad_height_bottom, pad_width_left, pad_width_right] whose value means the padding size of each side.
            5. A list or tuple of pairs of integers. It has the form [[pad_before, pad_after], [pad_before, pad_after], ...]. Note that, the batch dimension and channel dimension should be [0,0] or (0,0).
            The default value is 0.
1200
        ceil_mode (bool): ${ceil_mode_comment}
1201
        return_mask (bool): Whether to return the max indices along with the outputs. Default False. Only support "NDCHW" data_format.
1202 1203 1204 1205 1206 1207
        data_format (string): The data format of the input and output data. An optional string from: `"NCDHW"`, `"NDHWC"`.
                        The default is `"NCDHW"`. When it is `"NCDHW"`, the data is stored in the order of:
                        `[batch_size, input_channels, input_depth, input_height, input_width]`.
        name(str, optional): For detailed information, please refer
                             to :ref:`api_guide_Name`. Usually name is no need to set and
                             None by default.
C
Chen Long 已提交
1208
    
1209 1210
    Returns:
        Tensor: The output tensor of pooling result. The data type is same as input tensor.
C
Chen Long 已提交
1211
    
1212 1213 1214 1215
    Raises:
        ValueError: If `padding` is a string, but not "SAME" or "VALID".
        ValueError: If `padding` is "VALID", but `ceil_mode` is True.
        ShapeError: If the output's shape calculated is not greater than 0.
C
Chen Long 已提交
1216
    
1217 1218
    Examples:
        .. code-block:: python
1219

C
Chen Long 已提交
1220 1221
            import paddle
            import paddle.nn.functional as F
1222

C
Chen Long 已提交
1223
            # max pool3d
1224 1225
            x = paddle.uniform([1, 3, 32, 32, 32])
            output = F.max_pool3d(x,
C
Chen Long 已提交
1226 1227
                                  kernel_size=2,
                                  stride=2, padding=0)
1228
            # output.shape [1, 3, 16, 16, 16]
C
Chen Long 已提交
1229
            # for return_mask=True
1230
            x = paddle.uniform([1, 3, 32, 32, 32])
C
Chen Long 已提交
1231 1232 1233 1234 1235
            output, max_indices = paddle.nn.functional.max_pool3d(x,
                                          kernel_size = 2,
                                          stride = 2,
                                          padding=0,
                                          return_mask=True)
1236
            # output.shape [1, 3, 16, 16, 16], max_indices.shape [1, 3, 16, 16, 16]
1237 1238 1239 1240 1241 1242 1243
    """
    kernel_size = utils.convert_to_list(kernel_size, 3, 'pool_size')
    if stride is None:
        stride = kernel_size
    else:
        stride = utils.convert_to_list(stride, 3, 'pool_stride')

1244
    channel_last = _channel_last(data_format, 3)
1245

1246 1247 1248 1249
    padding, padding_algorithm = _update_padding_nd(padding,
                                                    3,
                                                    channel_last=channel_last,
                                                    ceil_mode=ceil_mode)
1250

1251
    if data_format == "NDHWC" and return_mask:
D
Double_V 已提交
1252
        raise ValueError(
1253
            "When setting return_mask to true, data_format must be set to NCDHW in API:max_pool3d"
D
Double_V 已提交
1254 1255
        )

F
From00 已提交
1256 1257
    if in_dygraph_mode():
        if return_mask:
1258 1259
            output = _C_ops.max_pool3d_with_index(x, kernel_size, stride,
                                                  padding, False, False)
F
From00 已提交
1260 1261
            return output if return_mask else output[0]
        else:
1262 1263 1264
            return _C_ops.pool3d(x, kernel_size, stride, padding, ceil_mode,
                                 True, data_format, 'max', False, False,
                                 padding_algorithm)
F
From00 已提交
1265 1266

    if _in_legacy_dygraph():
1267
        if return_mask:
1268
            output = _legacy_C_ops.max_pool3d_with_index(
D
Double_V 已提交
1269 1270 1271 1272 1273
                x, 'pooling_type', 'max', 'ksize', kernel_size, 'strides',
                stride, 'paddings', padding, 'global_pooling', False,
                'padding_algorithm', padding_algorithm, 'use_cudnn', True,
                'ceil_mode', ceil_mode, 'use_mkldnn', False, 'exclusive', True,
                'data_format', data_format)
1274
            return output if return_mask else output[0]
D
Double_V 已提交
1275
        else:
1276 1277 1278 1279 1280 1281
            output = _legacy_C_ops.pool3d(
                x, 'pooling_type', 'max', 'ksize', kernel_size,
                'global_pooling', False, 'padding_algorithm', padding_algorithm,
                'strides', stride, 'paddings', padding, 'use_cudnn', True,
                'ceil_mode', ceil_mode, 'use_mkldnn', False, 'exclusive', True,
                'data_format', data_format)
D
Double_V 已提交
1282
            return output
1283

1284
    op_type = "max_pool3d_with_index" if return_mask else "pool3d"
1285
    helper = LayerHelper(op_type, **locals())
1286
    check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'max_pool3d')
1287
    dtype = helper.input_dtype(input_param_name='x')
1288
    pool_out = helper.create_variable_for_type_inference(dtype)
1289
    mask = helper.create_variable_for_type_inference('int32')
1290 1291
    outputs = {"Out": pool_out, "Mask": mask}

1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
    helper.append_op(type=op_type,
                     inputs={"X": x},
                     outputs=outputs,
                     attrs={
                         "pooling_type": 'max',
                         "ksize": kernel_size,
                         "global_pooling": False,
                         "strides": stride,
                         "paddings": padding,
                         "padding_algorithm": padding_algorithm,
                         "use_cudnn": True,
                         "ceil_mode": ceil_mode,
                         "use_mkldnn": False,
                         "exclusive": False,
                         "data_format": data_format,
                     })
1308

1309
    return (pool_out, mask) if return_mask else pool_out
1310 1311


1312
def adaptive_avg_pool1d(x, output_size, name=None):
1313
    """
1314 1315 1316 1317
    Adaptive average pooling 1d operation on :attr:`x` according to :attr:`output_size`. 
    
    Notes:
        See more details in :ref:`api_nn_pooling_AdaptiveAvgPool1d` .
D
Double_V 已提交
1318

1319
    Args:
1320 1321 1322
        x (Tensor): The input Tensor of pooling, which is a 3-D tensor with shape :math:`[N, C, L]`, where :math:`N` is batch size, :math:`C` is the number of channels and :math:`L` is the length of the feature. The data type is float32 or float64.
        output_size (int): The target output size. Its data type must be int.
        name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
1323
    
1324
    Returns:
1325
        Tensor: The result of 1D adaptive average pooling. Its data type is same as input.
1326
    
1327 1328
    Examples:
        .. code-block:: python
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347

            # average adaptive pool1d
            # suppose input data in shape of [N, C, L], `output_size` is m or [m],
            # output shape is [N, C, m], adaptive pool divide L dimension
            # of input data into m grids averagely and performs poolings in each
            # grid to get output.
            # adaptive max pool performs calculations as follow:
            #
            #     for i in range(m):
            #         lstart = floor(i * L / m)
            #         lend = ceil((i + 1) * L / m)
            #         output[:, :, i] = sum(input[:, :, lstart: lend])/(lstart - lend)
            #
            import paddle
            import paddle.nn.functional as F

            data = paddle.uniform([1, 3, 32])
            pool_out = F.adaptive_avg_pool1d(data, output_size=16)
            # pool_out shape: [1, 3, 16])
1348 1349
    """
    pool_type = 'avg'
Z
zhiboniu 已提交
1350
    if not in_dynamic_mode():
1351 1352 1353
        check_variable_and_dtype(x, 'x', ['float16', 'float32', 'float64'],
                                 'adaptive_pool2d')
        check_type(output_size, 'pool_size', (int), 'adaptive_pool1d')
1354 1355
    _check_input(x, 3)
    pool_size = [1] + utils.convert_to_list(output_size, 1, 'pool_size')
1356

1357
    x = unsqueeze(x, [2])
Z
zhiboniu 已提交
1358
    if in_dynamic_mode():
1359 1360
        pool_out = _legacy_C_ops.pool2d(x, 'pooling_type', pool_type, 'ksize',
                                        pool_size, 'adaptive', True)
1361
        return squeeze(pool_out, [2])
1362

1363 1364
    l_type = "pool2d"

1365
    helper = LayerHelper(l_type, **locals())
1366
    dtype = helper.input_dtype(input_param_name='x')
1367 1368
    pool_out = helper.create_variable_for_type_inference(dtype)

1369
    outputs = {"Out": pool_out}
1370 1371 1372 1373 1374 1375 1376 1377
    helper.append_op(type=l_type,
                     inputs={"X": x},
                     outputs=outputs,
                     attrs={
                         "pooling_type": pool_type,
                         "ksize": pool_size,
                         "adaptive": True,
                     })
1378

1379
    return squeeze(pool_out, [2])
1380 1381


1382 1383
def adaptive_avg_pool2d(x, output_size, data_format='NCHW', name=None):
    """
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
    Applies 2D adaptive avg pooling on input tensor. The h and w dimensions
    of the output tensor are determined by the parameter output_size.
    
    For avg adaptive pool2d:
    ..  math::
        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})
        Output(i ,j) &= \frac{\sum Input[hstart:hend, wstart:wend]}{(hend - hstart) * (wend - wstart)}
1394 1395 1396

    Args:
        x (Tensor): The input tensor of adaptive avg pool2d operator, which is a 4-D tensor.
1397
                          The data type can be float32 or float64.
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
        output_size (int|list|tuple): The pool kernel size. If pool kernel size is a tuple or list,
            it must contain two element, (H, W). H and W can be either a int, or None which means
            the size will be the same as that of the input.
        data_format (str): The data format of the input and output data. An optional string
            from: "NCHW", "NHWC". The default is "NCHW". When it is "NCHW", the data is stored in
            the order of: [batch_size, input_channels, input_height, input_width].
        name(str, optional): For detailed information, please refer
                             to :ref:`api_guide_Name`. Usually name is no need to set and
                             None by default.
    Returns:
        Tensor: The output tensor of avg adaptive pool2d result. The data type is same as input tensor.
1409

1410 1411
    Examples:
        .. code-block:: python
B
Bai Yifan 已提交
1412

1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
            # adaptive avg pool2d
            # suppose input data in shape of [N, C, H, W], `output_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 avg 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
            import numpy as np
1430

1431 1432 1433
            input_data = np.random.rand(2, 3, 32, 32)
            x = paddle.to_tensor(input_data)
            # x.shape is [2, 3, 32, 32]
1434
            out = paddle.nn.functional.adaptive_avg_pool2d(
1435 1436
                            x = x,
                            output_size=[3, 3])
1437
            # out.shape is [2, 3, 3, 3]
1438
    """
Z
zhiboniu 已提交
1439
    if not in_dynamic_mode():
1440
        check_variable_and_dtype(x, 'x', ['float16', 'float32', 'float64'],
1441
                                 'adaptive_avg_pool2d')
1442
        check_type(data_format, 'data_format', str, 'adaptive_avg_pool2d')
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456

    if data_format not in ["NCHW", "NHWC"]:
        raise ValueError(
            "Attr(data_format) should be 'NCHW' or 'NHWC'. Received "
            "Attr(data_format): %s." % str(data_format))

    if data_format == "NCHW":
        in_h, in_w = x.shape[2:4]
    else:
        in_h, in_w = x.shape[1:3]

    if isinstance(output_size, int):
        output_size = utils.convert_to_list(output_size, 2, 'output_size')
    else:
1457
        output_size = list(output_size)
1458 1459 1460 1461 1462
        if output_size[0] == None:
            output_size[0] = in_h
        if output_size[1] == None:
            output_size[1] = in_w

F
From00 已提交
1463
    if in_dygraph_mode():
1464 1465 1466
        return _C_ops.pool2d_gpudnn_unused(x, output_size, [1, 1], [0, 0],
                                           False, True, data_format, 'avg',
                                           False, True, "EXPLICIT")
F
From00 已提交
1467 1468

    if _in_legacy_dygraph():
1469 1470 1471 1472
        return _legacy_C_ops.pool2d(x, 'pooling_type', 'avg', 'ksize',
                                    output_size, 'global_pooling', False,
                                    'adaptive', True, 'data_format',
                                    data_format)
1473 1474 1475 1476

    l_type = 'pool2d'

    helper = LayerHelper(l_type, **locals())
1477
    dtype = helper.input_dtype(input_param_name='x')
1478 1479 1480 1481
    pool_out = helper.create_variable_for_type_inference(dtype)

    outputs = {"Out": pool_out}

1482 1483 1484 1485 1486 1487 1488 1489 1490
    helper.append_op(type=l_type,
                     inputs={"X": x},
                     outputs=outputs,
                     attrs={
                         "pooling_type": "avg",
                         "ksize": output_size,
                         "adaptive": True,
                         "data_format": data_format,
                     })
1491 1492 1493 1494 1495 1496

    return pool_out


def adaptive_avg_pool3d(x, output_size, data_format='NCDHW', name=None):
    """
1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
    This operation applies 3D adaptive avg pooling on input tensor. The h and w dimensions
    of the output tensor are determined by the parameter output_size.
    
    For avg adaptive pool3d:
    ..  math::
        dstart &= floor(i * D_{in} / D_{out})
        dend &= ceil((i + 1) * D_{in} / D_{out})
        hstart &= floor(j * H_{in} / H_{out})
        hend &= ceil((j + 1) * H_{in} / H_{out})
        wstart &= floor(k * W_{in} / W_{out})
        wend &= ceil((k + 1) * W_{in} / W_{out})
        Output(i ,j, k) &= \frac{\sum Input[dstart:dend, hstart:hend, wstart:wend]}
            {(dend - dstart) * (hend - hstart) * (wend - wstart)}
1510 1511 1512

    Args:
        x (Tensor): The input tensor of adaptive avg pool3d operator, which is a 5-D tensor.
1513
                          The data type can be float32, float64.
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
        output_size (int|list|tuple): The pool kernel size. If pool kernel size is a tuple or list,
            it must contain three elements, (D, H, W). D, H and W can be either a int, or None which means
            the size will be the same as that of the input.
        data_format (str): The data format of the input and output data. An optional string
            from: "NCDHW", "NDHWC". The default is "NCDHW". When it is "NCDHW", the data is stored in
            the order of: [batch_size, input_channels, input_depth, input_height, input_width].
        name(str, optional): For detailed information, please refer
                             to :ref:`api_guide_Name`. Usually name is no need to set and
                             None by default.
    Returns:
        Tensor: The output tensor of avg adaptive pool3d result. The data type is same as input tensor.
1525

1526 1527
    Examples:
        .. code-block:: python
B
Bai Yifan 已提交
1528

1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
            # adaptive avg pool3d
            # suppose input data in shape of [N, C, D, H, W], `output_size` is [l, m, n],
            # output shape is [N, C, l, m, n], adaptive pool divide D, H and W dimensions
            # of input data into l * m * n grids averagely and performs poolings in each
            # grid to get output.
            # adaptive avg pool performs calculations as follow:
            #
            #     for i in range(l):
            #         for j in range(m):
            #             for k in range(n):
            #                 dstart = floor(i * D / l)
            #                 dend = ceil((i + 1) * D / l)
            #                 hstart = floor(j * H / m)
            #                 hend = ceil((j + 1) * H / m)
            #                 wstart = floor(k * W / n)
            #                 wend = ceil((k + 1) * W / n)
            #                 output[:, :, i, j, k] =
            #                     avg(input[:, :, dstart:dend, hstart: hend, wstart: wend])
            import paddle
1548 1549

            input_data = paddle.randn(shape=(2, 3, 8, 32, 32))
1550
            out = paddle.nn.functional.adaptive_avg_pool3d(
1551
                            x = input_data,
1552
                            output_size=[3, 3, 3])
1553
            # out.shape is [2, 3, 3, 3, 3]
1554
    """
Z
zhiboniu 已提交
1555
    if not in_dynamic_mode():
1556 1557
        check_variable_and_dtype(x, 'x', ['float32', 'float64'],
                                 'adaptive_avg_pool3d')
1558
        check_type(data_format, 'data_format', str, 'adaptive_avg_pool3d')
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572

    if data_format not in ["NCDHW", "NDHWC"]:
        raise ValueError(
            "Attr(data_format) should be 'NCDHW' or 'NDHWC'. Received "
            "Attr(data_format): %s." % str(data_format))

    if data_format == "NCDHW":
        in_l, in_h, in_w = x.shape[2:5]
    else:
        in_l, in_h, in_w = x.shape[1:4]

    if isinstance(output_size, int):
        output_size = utils.convert_to_list(output_size, 3, 'output_size')
    else:
1573
        output_size = list(output_size)
1574 1575 1576 1577 1578 1579 1580
        if output_size[0] == None:
            output_size[0] = in_l
        if output_size[1] == None:
            output_size[1] = in_h
        if output_size[2] == None:
            output_size[2] = in_w

Z
zhiboniu 已提交
1581
    if in_dynamic_mode():
1582 1583 1584 1585
        return _legacy_C_ops.pool3d(x, 'pooling_type', 'avg', 'ksize',
                                    output_size, 'global_pooling', False,
                                    'adaptive', True, 'data_format',
                                    data_format)
1586 1587 1588 1589

    l_type = 'pool3d'

    helper = LayerHelper(l_type, **locals())
1590
    dtype = helper.input_dtype(input_param_name='x')
1591 1592 1593
    pool_out = helper.create_variable_for_type_inference(dtype)
    outputs = {"Out": pool_out}

1594 1595 1596 1597 1598 1599 1600 1601 1602
    helper.append_op(type=l_type,
                     inputs={"X": x},
                     outputs=outputs,
                     attrs={
                         "pooling_type": "avg",
                         "ksize": output_size,
                         "adaptive": True,
                         "data_format": data_format,
                     })
1603 1604

    return pool_out
1605 1606


1607
def adaptive_max_pool1d(x, output_size, return_mask=False, name=None):
1608 1609 1610 1611 1612 1613 1614 1615 1616
    """
    This API implements adaptive max pooling 1d operation.
    See more details in :ref:`api_nn_pooling_AdaptiveMaxPool1d` .

    Args:
        x (Tensor): The input tensor of pooling operator, which is a 3-D tensor
                              with shape [N, C, L].  The format of input tensor is NCL,
                              where N is batch size, C is the number of channels, L is the
                              length of the feature. The data type is float32 or float64.
1617
        output_size (int): The pool kernel size. The value should be an integer.
1618
        return_mask (bool): If true, the index of max pooling point will be returned along
1619 1620 1621 1622 1623 1624 1625 1626
                with outputs. It cannot be set in average pooling type. Default False.
        name(str, optional): For detailed information, please refer
                                 to :ref:`api_guide_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:
1627
            ValueError: 'output_size' should be an integer.
1628 1629
    Examples:
        .. code-block:: python
1630

1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644
              # max adaptive pool1d
              # suppose input data in shape of [N, C, L], `output_size` is m or [m],
              # output shape is [N, C, m], adaptive pool divide L dimension
              # of input data into m grids averagely and performs poolings in each
              # grid to get output.
              # adaptive max pool performs calculations as follow:
              #
              #     for i in range(m):
              #         lstart = floor(i * L / m)
              #         lend = ceil((i + 1) * L / m)
              #         output[:, :, i] = max(input[:, :, lstart: lend])
              #
              import paddle
              import paddle.nn.functional as F
1645

1646
              data = paddle.uniform([1, 3, 32], paddle.float32)
1647 1648
              pool_out = F.adaptive_max_pool1d(data, output_size=16)
              # pool_out shape: [1, 3, 16])
1649
              pool_out, indices = F.adaptive_max_pool1d(data, output_size=16, return_mask=True)
1650 1651 1652
              # pool_out shape: [1, 3, 16] indices  shape: [1, 3, 16]
    """
    pool_type = 'max'
Z
zhiboniu 已提交
1653
    if not in_dynamic_mode():
1654 1655 1656 1657
        check_variable_and_dtype(x, 'x', ['float32', 'float64'],
                                 'adaptive_max_pool1d')
        check_type(output_size, 'pool_size', int, 'adaptive_max_pool1d')
        check_type(return_mask, 'return_mask', bool, 'adaptive_max_pool1d')
1658 1659 1660 1661 1662
    _check_input(x, 3)

    pool_size = [1] + utils.convert_to_list(output_size, 1, 'pool_size')

    x = unsqueeze(x, [2])
1663
    if in_dygraph_mode():
1664 1665
        pool_out = _C_ops.max_pool2d_with_index(x, pool_size, [1, 1], [0, 0],
                                                False, True)
1666 1667 1668
        return (squeeze(pool_out[0], [2]), squeeze(
            pool_out[1], [2])) if return_mask else squeeze(pool_out[0], [2])
    if _in_legacy_dygraph():
1669 1670 1671 1672
        pool_out = _legacy_C_ops.max_pool2d_with_index(x, 'pooling_type',
                                                       pool_type, 'ksize',
                                                       pool_size, 'adaptive',
                                                       True)
1673
        return (squeeze(pool_out[0], [2]), squeeze(
1674
            pool_out[1], [2])) if return_mask else squeeze(pool_out[0], [2])
1675

1676 1677
    l_type = 'max_pool2d_with_index'

1678
    helper = LayerHelper(l_type, **locals())
1679
    dtype = helper.input_dtype(input_param_name='x')
1680 1681
    pool_out = helper.create_variable_for_type_inference(dtype)

1682
    mask = helper.create_variable_for_type_inference('int32')
1683 1684
    outputs = {"Out": pool_out, "Mask": mask}

1685 1686 1687 1688 1689 1690 1691 1692
    helper.append_op(type=l_type,
                     inputs={"X": x},
                     outputs=outputs,
                     attrs={
                         "pooling_type": pool_type,
                         "ksize": pool_size,
                         "adaptive": True,
                     })
1693 1694

    return (squeeze(pool_out, [2]),
1695
            squeeze(mask, [2])) if return_mask else squeeze(pool_out, [2])
1696 1697


1698
def adaptive_max_pool2d(x, output_size, return_mask=False, name=None):
1699 1700 1701
    """
        This operation applies a 2D adaptive max pooling on input tensor.
        See more details in :ref:`api_nn_pooling_AdaptiveMaxPool2d` .
1702

1703 1704 1705
        Args:
            x (Tensor): The input tensor of adaptive max pool2d operator, which is a 4-D tensor. The data type can be float16, float32, float64, int32 or int64.
            output_size (int|list|tuple): The pool kernel size. If pool kernel size is a tuple or list, it must contain two elements, (H, W). H and W can be either a int, or None which means the size will be the same as that of the input.
1706
            return_mask (bool): If true, the index of max pooling point will be returned along with outputs. Default False.
1707
            name(str, optional): For detailed information, please refer to :ref:`api_guide_Name`. Usually name is no need to set and None by default.
1708

1709 1710
        Returns:
            Tensor: The output tensor of adaptive max pool2d result. The data type is same as input tensor.
1711

1712 1713
        Examples:
            .. code-block:: python
1714

1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
              # max adaptive pool2d
              # suppose input data in the shape of [N, C, H, W], `output_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 max 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
1731

1732
              input_data = paddle.randn(shape=(2, 3, 32, 32))
1733
              out = paddle.nn.functional.adaptive_max_pool2d(
1734
                            x = input_data,
1735 1736 1737
                            output_size=[3, 3])
              # out.shape is [2, 3, 3, 3]
    """
Z
zhiboniu 已提交
1738
    if not in_dynamic_mode():
1739 1740
        check_variable_and_dtype(x, 'x', ['float32', 'float64'],
                                 'adaptive_max_pool2d')
1741 1742
        check_type(return_mask, 'return_mask', bool, 'adaptive_max_pool2d')
        #check_type(output_size, 'pool_size', (int), 'adaptive_max_pool2d')
1743 1744 1745 1746 1747 1748
    _check_input(x, 4)

    in_h, in_w = x.shape[2:4]
    if isinstance(output_size, int):
        output_size = utils.convert_to_list(output_size, 2, 'output_size')
    else:
1749
        output_size = list(output_size)
1750 1751 1752 1753
        if output_size[0] == None:
            output_size[0] = in_h
        if output_size[1] == None:
            output_size[1] = in_w
1754
    if in_dygraph_mode():
1755 1756
        pool_out = _C_ops.max_pool2d_with_index(x, output_size, [1, 1], [0, 0],
                                                False, True)
1757 1758
        return pool_out if return_mask else pool_out[0]
    if _in_legacy_dygraph():
1759 1760 1761
        pool_out = _legacy_C_ops.max_pool2d_with_index(x, 'pooling_type', 'max',
                                                       'ksize', output_size,
                                                       'adaptive', True)
1762
        return pool_out if return_mask else pool_out[0]
1763 1764 1765 1766

    l_type = 'max_pool2d_with_index'

    helper = LayerHelper(l_type, **locals())
1767
    dtype = helper.input_dtype(input_param_name='x')
1768 1769
    pool_out = helper.create_variable_for_type_inference(dtype)

1770
    mask = helper.create_variable_for_type_inference('int32')
1771 1772
    outputs = {"Out": pool_out, "Mask": mask}

1773 1774 1775 1776 1777 1778 1779 1780
    helper.append_op(type=l_type,
                     inputs={"X": x},
                     outputs=outputs,
                     attrs={
                         "pooling_type": 'max',
                         "ksize": output_size,
                         "adaptive": True,
                     })
1781
    #return (pool_out, mask) if return_mask else pool_out
1782 1783 1784
    return pool_out


1785
def adaptive_max_pool3d(x, output_size, return_mask=False, name=None):
1786 1787 1788
    """
        This operation applies a 3D adaptive max pooling on input tensor.
        See more details in :ref:`api_nn_pooling_AdaptiveMaxPool3d` .
1789

1790 1791 1792
        Args:
            x (Tensor): The input tensor of adaptive max pool3d operator, which is a 5-D tensor. The data type can be float32, float64.
            output_size (int|list|tuple): The pool kernel size. If pool kernel size is a tuple or list, it must contain three elements, (D, H, W). D, H and W can be either a int, or None which means the size will be the same as that of the input.
1793
            return_mask (bool): If true, the index of max pooling point will be returned along with outputs. Default False.
1794
            name(str, optional): For detailed information, please refer to :ref:`api_guide_Name`. Usually name is no need to set and None by default.
1795

1796 1797
        Returns:
            Tensor: The output tensor of adaptive max pool3d result. The data type is same as input tensor.
1798

1799 1800
        Examples:
            .. code-block:: python
1801

1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820
              # adaptive max pool3d
              # suppose input data in the shape of [N, C, D, H, W], `output_size` is [l, m, n]
              # output shape is [N, C, l, m, n], adaptive pool divide D, H and W dimensions
              # of input data into m*n grids averagely and performs poolings in each
              # grid to get output.
              # adaptive max pool performs calculations as follow:
              #
              #     for i in range(l):
              #         for j in range(m):
              #             for k in range(n):
              #                 dstart = floor(i * D / l)
              #                 dend = ceil((i + 1) * D / l)
              #                 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, k] = max(input[:, :, dstart: dend, hstart: hend, wstart: wend])
              #
              import paddle
1821

1822
              input_data = paddle.randn(shape=(2, 3, 8, 32, 32))
1823
              out = paddle.nn.functional.adaptive_max_pool3d(
1824
                            x = input_data,
1825 1826 1827 1828
                            output_size=[3, 3, 3])
              # out.shape is [2, 3, 3, 3, 3]
    """

Z
zhiboniu 已提交
1829
    if not in_dynamic_mode():
1830 1831
        check_variable_and_dtype(x, 'x', ['float32', 'float64'],
                                 'adaptive_max_pool3d')
1832 1833
        check_type(return_mask, 'return_mask', bool, 'adaptive_max_pool3d')
        #check_type(output_size, 'pool_size', (int), 'adaptive_max_pool3d')
1834 1835 1836 1837 1838 1839
    _check_input(x, 5)

    in_l, in_h, in_w = x.shape[2:5]
    if isinstance(output_size, int):
        output_size = utils.convert_to_list(output_size, 3, 'output_size')
    else:
1840
        output_size = list(output_size)
1841 1842 1843 1844 1845 1846 1847
        if output_size[0] == None:
            output_size[0] = in_l
        if output_size[1] == None:
            output_size[1] = in_h
        if output_size[2] == None:
            output_size[2] = in_w

Z
zhiboniu 已提交
1848
    if in_dynamic_mode():
1849 1850 1851
        pool_out = _legacy_C_ops.max_pool3d_with_index(x, 'pooling_type', 'max',
                                                       'ksize', output_size,
                                                       'adaptive', True)
1852
        return pool_out if return_mask else pool_out[0]
1853 1854 1855 1856

    l_type = 'max_pool3d_with_index'

    helper = LayerHelper(l_type, **locals())
1857
    dtype = helper.input_dtype(input_param_name='x')
1858 1859
    pool_out = helper.create_variable_for_type_inference(dtype)

1860
    mask = helper.create_variable_for_type_inference('int32')
1861 1862
    outputs = {"Out": pool_out, "Mask": mask}

1863 1864 1865 1866 1867 1868 1869 1870
    helper.append_op(type=l_type,
                     inputs={"X": x},
                     outputs=outputs,
                     attrs={
                         "pooling_type": 'max',
                         "ksize": output_size,
                         "adaptive": True,
                     })
1871

1872
    return (pool_out, mask) if return_mask else pool_out