pooling.py 54.1 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.

from .. import functional as F
Z
zhiboniu 已提交
16
from .. import Layer
17

18 19
__all__ = []

20

Z
zhiboniu 已提交
21
class AvgPool1D(Layer):
W
Wei Shengyu 已提交
22
    r"""
23
    This operation applies a 1D average pooling over an input signal composed
24
    of several input planes, based on the input, output_size, return_mask parameters.
25 26 27 28 29
    Input(X) and output(Out) are in NCL format, where N is batch
    size, C is the number of channels, L is the length of the feature.
    The output tensor shape will be [N, C, output_size].

    The output value of the layer with input size (N, C, L),
W
Wei Shengyu 已提交
30
    output (N, C, :math:`L_{out}`) and kernel_size ksize can be precisely described as
31 32 33 34
    For average pool1d:

    ..  math::

W
Wei Shengyu 已提交
35
        Output(N_i, C_i, l) = \frac{Input[N_i, C_i, stride \times l:stride \times l+k]}{ksize}
36

W
Wei Shengyu 已提交
37 38
    Parameters:
        kernel_size(int|list|tuple): The pool kernel size. If pool kernel size is a tuple or list,
39
            it must contain an integer.
W
Wei Shengyu 已提交
40 41 42
        stride(int|list|tuple, optional): The pool stride size. If pool stride size is a tuple or list,
            it must contain an integer. Default None, then stride will be equal to the kernel_size.
        padding(str|int|list|tuple, optional): The padding size. Padding could be in one of the following forms.
43 44 45 46 47 48
            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.
W
Wei Shengyu 已提交
49 50 51 52 53
        exclusive(bool, optional): Whether to exclude padding points in average pooling mode, default is `True`.
        ceil_mode(bool, optional): ${ceil_mode_comment}Whether to use the ceil function to calculate output height
            and width. If it is set to False, the floor function will be used. The default value is False.
        name(str, optional): For eed to detailed information, please refer to :ref:`api_guide_Name`.
            Usually name is no nset and None by default.
54

55
    Shape:
W
Wei Shengyu 已提交
56 57 58 59
        - x(Tensor): The input tensor of avg pool1d operator, which is a 3-D tensor.
          The data type can be float32, float64.
        - output(Tensor): The output tensor of avg pool1d  operator, which is a 3-D tensor.
          The data type is same as input x.
60

61 62
    Returns:
        A callable object of AvgPool1D.
63

64 65 66
    Examples:

        .. code-block:: python
67

W
Wei Shengyu 已提交
68 69
            import paddle
            import paddle.nn as nn
70

71
            data = paddle.uniform([1, 3, 32], dtype="float32", min=-1, max=1)
W
Wei Shengyu 已提交
72 73 74
            AvgPool1D = nn.AvgPool1D(kernel_size=2, stride=2, padding=0)
            pool_out = AvgPool1D(data)
            # pool_out shape: [1, 3, 16]
75 76 77

    """

78 79 80 81 82 83 84 85 86
    def __init__(
        self,
        kernel_size,
        stride=None,
        padding=0,
        exclusive=True,
        ceil_mode=False,
        name=None,
    ):
C
cnn 已提交
87
        super(AvgPool1D, self).__init__()
88 89 90 91
        self.kernel_size = kernel_size
        self.stride = stride
        self.padding = padding
        self.ceil_mode = ceil_mode
92
        self.exclusive = exclusive
93 94 95
        self.name = name

    def forward(self, x):
96 97 98 99 100 101 102 103 104
        out = F.avg_pool1d(
            x,
            self.kernel_size,
            self.stride,
            self.padding,
            self.exclusive,
            self.ceil_mode,
            self.name,
        )
105 106
        return out

107 108
    def extra_repr(self):
        return 'kernel_size={kernel_size}, stride={stride}, padding={padding}'.format(
109 110
            **self.__dict__
        )
111

112

Z
zhiboniu 已提交
113
class AvgPool2D(Layer):
114
    r"""
115 116 117 118
    This operation applies 2D average pooling over input features based on the input,
    and kernel_size, stride, padding parameters. Input(X) and Output(Out) are
    in NCHW format, where N is batch size, C is the number of channels,
    H is the height of the feature, and W is the width of the feature.
119

120
    Example:
W
Wei Shengyu 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        Input:
            X shape: :math:`(N, C, :math:`H_{in}`, :math:`W_{in}`)`
        Attr:
            kernel_size: ksize

        Output:
            Out shape: :math:`(N, C, :math:`H_{out}`, :math:`W_{out}`)`

        ..  math::

            Output(N_i, C_j, h, w)  = \frac{\sum_{m=0}^{ksize[0]-1} \sum_{n=0}^{ksize[1]-1}
                Input(N_i, C_j, stride[0] \times h + m, stride[1] \times w + n)}{ksize[0] * ksize[1]}

    Parameters:
        kernel_size(int|list|tuple): The pool kernel size. If pool kernel size is a tuple or list,
136 137
            it must contain two integers, (pool_size_Height, pool_size_Width).
            Otherwise, the pool kernel size will be a square of an int.
W
Wei Shengyu 已提交
138
        stride(int|list|tuple, optional): The pool stride size. If pool stride size is a tuple or list,
139 140
            it must contain two integers, (pool_stride_Height, pool_stride_Width).
            Otherwise, the pool stride size will be a square of an int.
W
Wei Shengyu 已提交
141 142
            Default None, then stride will be equal to the kernel_size.
        padding(str|int|list|tuple, optional): The padding size. Padding could be in one of the following forms.
143 144 145 146 147 148
            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.
W
Wei Shengyu 已提交
149 150 151 152 153 154 155 156 157 158
        ceil_mode(bool, optional): When True, will use `ceil` instead of `floor` to compute the output shape.
        exclusive(bool, optional): Whether to exclude padding points in average pooling
            mode, default is `true`.
        divisor_override(float, optional): If specified, it will be used as divisor, otherwise kernel_size will be
            used. Default None.
        data_format(str, optional): The data format of the input and output data. An optional string from: `"NCHW"`,
            `"NDHW"`. 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.
159

160
    Shape:
W
Wei Shengyu 已提交
161 162 163 164
        - x(Tensor): The input tensor of avg pool2d operator, which is a 4-D tensor.
          The data type can be float32, float64.
        - output(Tensor): The output tensor of avg pool2d  operator, which is a 4-D tensor.
          The data type is same as input x.
165

W
Wei Shengyu 已提交
166 167
    Returns:
        A callable object of AvgPool2D.
168

169 170
    Examples:
        .. code-block:: python
171

W
Wei Shengyu 已提交
172 173
            import paddle
            import paddle.nn as nn
174

W
Wei Shengyu 已提交
175
            # max pool2d
176
            input = paddle.uniform([1, 3, 32, 32], dtype="float32", min=-1, max=1)
W
Wei Shengyu 已提交
177
            AvgPool2D = nn.AvgPool2D(kernel_size=2,
178
                                stride=2, padding=0)
W
Wei Shengyu 已提交
179 180
            output = AvgPool2D(input)
            # output.shape [1, 3, 16, 16]
181 182 183

    """

184 185 186 187 188 189 190 191 192 193 194
    def __init__(
        self,
        kernel_size,
        stride=None,
        padding=0,
        ceil_mode=False,
        exclusive=True,
        divisor_override=None,
        data_format="NCHW",
        name=None,
    ):
C
cnn 已提交
195
        super(AvgPool2D, self).__init__()
196
        self.ksize = kernel_size
197 198 199
        self.stride = stride
        self.padding = padding
        self.ceil_mode = ceil_mode
200
        self.exclusive = exclusive
201 202
        self.divisor = divisor_override
        self.data_format = data_format
203 204
        self.name = name

205
    def forward(self, x):
206 207 208 209 210 211 212 213 214 215 216
        return F.avg_pool2d(
            x,
            kernel_size=self.ksize,
            stride=self.stride,
            padding=self.padding,
            ceil_mode=self.ceil_mode,
            exclusive=self.exclusive,
            divisor_override=self.divisor,
            data_format=self.data_format,
            name=self.name,
        )
217

218 219
    def extra_repr(self):
        return 'kernel_size={ksize}, stride={stride}, padding={padding}'.format(
220 221
            **self.__dict__
        )
222

223

Z
zhiboniu 已提交
224
class AvgPool3D(Layer):
225
    """
226 227 228 229
    This operation applies 3D max pooling over input features based on the input,
    and kernel_size, stride, padding parameters. Input(X) and Output(Out) are
    in NCDHW format, where N is batch size, C is the number of channels,
    H is the height of the feature,  D is the depth of the feature, and W is the width of the feature.
230

W
Wei Shengyu 已提交
231 232
    Parameters:
        kernel_size(int|list|tuple): The pool kernel size. If pool kernel size
233 234 235
            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.
W
Wei Shengyu 已提交
236
        stride(int|list|tuple, optional): The pool stride size. If pool stride size is a tuple or list,
237 238
            it must contain three integers, [stride_Depth, stride_Height, stride_Width).
            Otherwise, the pool stride size will be a cube of an int.
W
Wei Shengyu 已提交
239 240
            Default None, then stride will be equal to the kernel_size.
        padding(str|int|list|tuple, optional): The padding size. Padding could be in one of the following forms.
241 242 243 244 245 246
            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.
W
Wei Shengyu 已提交
247 248 249 250 251 252 253
        ceil_mode(bool, optional): ${ceil_mode_comment}
        exclusive(bool, optional): Whether to exclude padding points in average pooling mode, default is True.
        divisor_override(int|float, optional): if specified, it will be used as divisor, otherwise kernel_size will
            be used. Default None.
        data_format(str, optional): 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]`.
254
        name(str, optional): For detailed information, please refer
W
Wei Shengyu 已提交
255 256
             to :ref:`api_guide_Name`. Usually name is no need to set and
             None by default.
257

W
Wei Shengyu 已提交
258 259
    Returns:
        A callable object of AvgPool3D.
260 261

    Shape:
W
Wei Shengyu 已提交
262 263 264 265
        - x(Tensor): The input tensor of avg pool3d operator, which is a 5-D tensor.
          The data type can be float32, float64.
        - output(Tensor): The output tensor of avg pool3d  operator, which is a 5-D tensor.
          The data type is same as input x.
266 267
    Examples:
        .. code-block:: python
268

W
Wei Shengyu 已提交
269 270
            import paddle
            import paddle.nn as nn
271

W
Wei Shengyu 已提交
272
            # avg pool3d
273
            input = paddle.uniform([1, 2, 3, 32, 32], dtype="float32", min=-1, max=1)
W
Wei Shengyu 已提交
274
            AvgPool3D = nn.AvgPool3D(kernel_size=2,
275
                                   stride=2, padding=0)
W
Wei Shengyu 已提交
276 277
            output = AvgPool3D(input)
            # output.shape [1, 2, 3, 16, 16]
278

279 280
    """

281 282 283 284 285 286 287 288 289 290 291
    def __init__(
        self,
        kernel_size,
        stride=None,
        padding=0,
        ceil_mode=False,
        exclusive=True,
        divisor_override=None,
        data_format="NCDHW",
        name=None,
    ):
C
cnn 已提交
292
        super(AvgPool3D, self).__init__()
293 294 295 296
        self.ksize = kernel_size
        self.stride = stride
        self.padding = padding
        self.ceil_mode = ceil_mode
297
        self.exclusive = exclusive
298 299
        self.divisor = divisor_override
        self.data_format = data_format
300 301
        self.name = name

302
    def forward(self, x):
303 304 305 306 307 308 309 310 311 312 313
        return F.avg_pool3d(
            x,
            kernel_size=self.ksize,
            stride=self.stride,
            padding=self.padding,
            ceil_mode=self.ceil_mode,
            exclusive=self.exclusive,
            divisor_override=self.divisor,
            data_format=self.data_format,
            name=self.name,
        )
314

315 316
    def extra_repr(self):
        return 'kernel_size={ksize}, stride={stride}, padding={padding}'.format(
317 318
            **self.__dict__
        )
319

320

Z
zhiboniu 已提交
321
class MaxPool1D(Layer):
322
    """
W
Wei Shengyu 已提交
323 324 325 326 327
    This operation applies 1D max pooling over input signal
    composed of several input planes based on the input,
    and kernel_size, stride, padding parameters. Input(X) and Output(Out) are
    in NCL format, where N is batch size, C is the number of channels,
    L is the length of the feature.
328

329 330 331
    The output value of the layer with input size (N, C, L),
    output (N, C, L_{out}) and kernel_size k can be precisely described as
    For average pool1d:
332 333 334

    ..  math::

W
Wei Shengyu 已提交
335
        Output(N_i, C_i, l) =  max(Input[N_i, C_i, stride \times l:stride \times l+k])
336

W
Wei Shengyu 已提交
337 338
    Parameters:
        kernel_size(int|list|tuple): The pool kernel size. If pool kernel size is a tuple or list,
339
            it must contain an integer.
W
Wei Shengyu 已提交
340 341 342
        stride(int|list|tuple, optional): The pool stride size. If pool stride size is a tuple or list,
            it must contain an integer. Default None, then stride will be equal to the kernel_size.
        padding(str|int|list|tuple, optional): The padding size. Padding could be in one of the following forms.
343 344 345
            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.
W
Wei Shengyu 已提交
346 347
            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).
348
            The default value is 0.
W
Wei Shengyu 已提交
349 350 351 352 353
        return_mask(bool, optional): Whether return the max indices along with the outputs. default is `False`.
        ceil_mode(bool, optional): 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.
        name(str, optional): For detailed information, please refer to :ref:`api_guide_Name`.
            Usually name is no need to set and None by default.
354
    Returns:
W
Wei Shengyu 已提交
355
        A callable object of MaxPool1D.
356

357
    Shape:
W
Wei Shengyu 已提交
358 359 360 361
        - x(Tensor): The input tensor of max pool1d operator, which is a 3-D tensor.
          The data type can be float32, float64.
        - output(Tensor): The output tensor of max pool1d  operator, which is a 3-D tensor.
          The data type is same as input x.
362 363

    Examples:
364

365 366
        .. code-block:: python

W
Wei Shengyu 已提交
367 368
            import paddle
            import paddle.nn as nn
369

370
            data = paddle.uniform([1, 3, 32], dtype="float32", min=-1, max=1)
W
Wei Shengyu 已提交
371 372 373
            MaxPool1D = nn.MaxPool1D(kernel_size=2, stride=2, padding=0)
            pool_out = MaxPool1D(data)
            # pool_out shape: [1, 3, 16]
374

W
Wei Shengyu 已提交
375 376 377
            MaxPool1D = nn.MaxPool1D(kernel_size=2, stride=2, padding=0, return_mask=True)
            pool_out, indices = MaxPool1D(data)
            # pool_out shape: [1, 3, 16], indices shape: [1, 3, 16]
378 379 380

    """

381 382 383 384 385 386 387 388 389
    def __init__(
        self,
        kernel_size,
        stride=None,
        padding=0,
        return_mask=False,
        ceil_mode=False,
        name=None,
    ):
C
cnn 已提交
390
        super(MaxPool1D, self).__init__()
391 392 393 394
        self.kernel_size = kernel_size
        self.stride = stride
        self.padding = padding
        self.ceil_mode = ceil_mode
395
        self.return_mask = return_mask
396 397 398
        self.name = name

    def forward(self, input):
399 400 401 402 403 404 405 406 407
        out = F.max_pool1d(
            input,
            self.kernel_size,
            self.stride,
            self.padding,
            self.return_mask,
            self.ceil_mode,
            self.name,
        )
408
        return out
409

410 411
    def extra_repr(self):
        return 'kernel_size={kernel_size}, stride={stride}, padding={padding}'.format(
412 413
            **self.__dict__
        )
414

415

Z
zhiboniu 已提交
416
class MaxPool2D(Layer):
417
    r"""
418
    This operation applies 2D max pooling over input feature based on the input,
419 420 421 422 423
    and kernel_size, stride, padding parameters. Input(X) and Output(Out) are
    in NCHW format, where N is batch size, C is the number of channels,
    H is the height of the feature, and W is the width of the feature.

    Example:
W
Wei Shengyu 已提交
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
        - Input:
            X shape: :math:`(N, C, H_{in}, W_{in})`
        - Attr:
            kernel_size: ksize

        - Output:
            Out shape: :math:`(N, C, H_{out}, W_{out})`

        ..  math::

            Output(N_i, C_j, h, w) = \max_{m=0, \ldots, ksize[0] -1} \max_{n=0, \ldots, ksize[1]-1}
                Input(N_i, C_j, stride[0] \times h + m, stride[1] \times w + n)

    Parameters:
        kernel_size(int|list|tuple): The pool kernel size. If pool kernel size is a tuple or list,
439 440
            it must contain two integers, (pool_size_Height, pool_size_Width).
            Otherwise, the pool kernel size will be a square of an int.
W
Wei Shengyu 已提交
441
        stride(int|list|tuple, optional): The pool stride size. If pool stride size is a tuple or list,
442
            it must contain two integers, (pool_stride_Height, pool_stride_Width).
443
            Otherwise, the pool stride size will be a square of an int.
W
Wei Shengyu 已提交
444 445
            Default None, then stride will be equal to the kernel_size.
        padding(str|int|list|tuple, optional): The padding size. Padding could be in one of the following forms.
446 447 448
            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.
W
Wei Shengyu 已提交
449
            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.
450 451
            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.
W
Wei Shengyu 已提交
452 453 454 455 456 457 458
        ceil_mode(bool, optional): when True, will use `ceil` instead of `floor` to compute the output shape
        return_mask(bool, optional): Whether to return the max indices along with the outputs.
        data_format(str, optional): The data format of the input and output data. An optional string from: `"NCHW"`, `"NDHW"`.
            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.
459

W
Wei Shengyu 已提交
460 461
    Returns:
        A callable object of MaxPool2D.
462 463

    Shape:
W
Wei Shengyu 已提交
464 465 466 467
        - x(Tensor): The input tensor of max pool2d operator, which is a 4-D tensor.
          The data type can be float32, float64.
        - output(Tensor): The output tensor of max pool2d  operator, which is a 4-D tensor.
          The data type is same as input x.
468

469 470
    Examples:
        .. code-block:: python
471

W
Wei Shengyu 已提交
472 473
            import paddle
            import paddle.nn as nn
474

W
Wei Shengyu 已提交
475
            # max pool2d
476
            input = paddle.uniform([1, 3, 32, 32], dtype="float32", min=-1, max=1)
W
Wei Shengyu 已提交
477
            MaxPool2D = nn.MaxPool2D(kernel_size=2,
478
                                   stride=2, padding=0)
W
Wei Shengyu 已提交
479 480
            output = MaxPool2D(input)
            # output.shape [1, 3, 16, 16]
481

W
Wei Shengyu 已提交
482 483 484 485
            # for return_mask=True
            MaxPool2D = nn.MaxPool2D(kernel_size=2, stride=2, padding=0, return_mask=True)
            output, max_indices = MaxPool2D(input)
            # output.shape [1, 3, 16, 16], max_indices.shape [1, 3, 16, 16],
486 487
    """

488 489 490 491 492 493 494 495 496 497
    def __init__(
        self,
        kernel_size,
        stride=None,
        padding=0,
        return_mask=False,
        ceil_mode=False,
        data_format="NCHW",
        name=None,
    ):
C
cnn 已提交
498
        super(MaxPool2D, self).__init__()
499 500 501
        self.ksize = kernel_size
        self.stride = stride
        self.padding = padding
502
        self.return_mask = return_mask
503 504 505 506 507
        self.ceil_mode = ceil_mode
        self.data_format = data_format
        self.name = name

    def forward(self, x):
508 509 510 511 512 513 514 515 516 517
        return F.max_pool2d(
            x,
            kernel_size=self.ksize,
            stride=self.stride,
            padding=self.padding,
            return_mask=self.return_mask,
            ceil_mode=self.ceil_mode,
            data_format=self.data_format,
            name=self.name,
        )
518

519 520
    def extra_repr(self):
        return 'kernel_size={ksize}, stride={stride}, padding={padding}'.format(
521 522
            **self.__dict__
        )
523

524

Z
zhiboniu 已提交
525
class MaxPool3D(Layer):
526
    """
527
    This operation applies 3D max pooling over input features based on the input,
528
    and kernel_size, stride, padding parameters. Input(X) and Output(Out) are
529 530
    in NCDHW format, where N is batch size, C is the number of channels,
    H is the height of the feature,  D is the depth of the feature, and W is the width of the feature.
531

W
Wei Shengyu 已提交
532 533
    Parameters:
        kernel_size(int|list|tuple): The pool kernel size. If the kernel size
534
            is a tuple or list, it must contain three integers,
535
            (kernel_size_Depth, kernel_size_Height, kernel_size_Width).
536
            Otherwise, the pool kernel size will be the cube of an int.
W
Wei Shengyu 已提交
537
        stride(int|list|tuple, optional): The pool stride size. If pool stride size is a tuple or list,
538 539
            it must contain three integers, [stride_Depth, stride_Height, stride_Width).
            Otherwise, the pool stride size will be a cube of an int.
W
Wei Shengyu 已提交
540 541
            Default None, then stride will be equal to the kernel_size.
        padding(str|int|list|tuple, optional): The padding size. Padding could be in one of the following forms.
542 543 544
            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.
W
Wei Shengyu 已提交
545
            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.
546 547
            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.
W
Wei Shengyu 已提交
548 549 550 551 552 553 554
        ceil_mode(bool, optional): ${ceil_mode_comment}
        return_mask(bool, optional): Whether to return the max indices along with the outputs.
        data_format(str, optional): 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.
555 556


W
Wei Shengyu 已提交
557 558
    Returns:
        A callable object of MaxPool3D.
559 560

    Shape:
W
Wei Shengyu 已提交
561 562 563 564
        - x(Tensor): The input tensor of max pool3d operator, which is a 5-D tensor.
          The data type can be float32, float64.
        - output(Tensor): The output tensor of max pool3d  operator, which is a 5-D tensor.
          The data type is same as input x.
565

566 567
    Examples:
        .. code-block:: python
568

W
Wei Shengyu 已提交
569 570
            import paddle
            import paddle.nn as nn
571

W
Wei Shengyu 已提交
572
            # max pool3d
573
            input = paddle.uniform([1, 2, 3, 32, 32], dtype="float32", min=-1, max=1)
W
Wei Shengyu 已提交
574
            MaxPool3D = nn.MaxPool3D(kernel_size=2,
575
                                   stride=2, padding=0)
W
Wei Shengyu 已提交
576 577
            output = MaxPool3D(input)
            # output.shape [1, 2, 3, 16, 16]
578

W
Wei Shengyu 已提交
579 580 581 582
            # for return_mask=True
            MaxPool3D = nn.MaxPool3D(kernel_size=2, stride=2, padding=0, return_mask=True)
            output, max_indices = MaxPool3D(input)
            # output.shape [1, 2, 3, 16, 16], max_indices.shape [1, 2, 3, 16, 16],
583 584
    """

585 586 587 588 589 590 591 592 593 594
    def __init__(
        self,
        kernel_size,
        stride=None,
        padding=0,
        return_mask=False,
        ceil_mode=False,
        data_format="NCDHW",
        name=None,
    ):
C
cnn 已提交
595
        super(MaxPool3D, self).__init__()
596 597 598
        self.ksize = kernel_size
        self.stride = stride
        self.padding = padding
599
        self.return_mask = return_mask
600 601 602 603 604
        self.ceil_mode = ceil_mode
        self.data_format = data_format
        self.name = name

    def forward(self, x):
605 606 607 608 609 610 611 612 613 614
        return F.max_pool3d(
            x,
            kernel_size=self.ksize,
            stride=self.stride,
            padding=self.padding,
            return_mask=self.return_mask,
            ceil_mode=self.ceil_mode,
            data_format=self.data_format,
            name=self.name,
        )
615

616 617
    def extra_repr(self):
        return 'kernel_size={ksize}, stride={stride}, padding={padding}'.format(
618 619
            **self.__dict__
        )
620

621

Z
zhiboniu 已提交
622
class AdaptiveAvgPool1D(Layer):
623
    r"""
624

625 626 627 628 629
    A 1D adaptive average pooling over an input signal composed
    of several input planes, based on :attr:`output_size`.
    Input and output are in NCL format, where N is batch
    size, C is the number of channels and L is the length of the feature.
    The shape of output will be :math:`[N, C, output\_size]`.
630

631
    The formulation for average adaptive pool1d is
632 633 634

    ..  math::

635
        lstart &= \lfloor i * L_{in} / L_{out}\rfloor,
636

637
        lend &= \lceil(i + 1) * L_{in} / L_{out}\rceil,
638

639
        Output(i) &= \frac{\sum Input[lstart:lend]}{lend - lstart}.
640

W
Wei Shengyu 已提交
641
    Parameters:
642 643
        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.
644

645
    Returns:
646
        A callable object for computing 1D adaptive average pooling.
647

648 649
    Examples:
        .. code-block:: python
650

W
Wei Shengyu 已提交
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
            # 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])/(lend - lstart)
            #
            import paddle
            import paddle.nn as nn

666
            data = paddle.uniform([1, 3, 32], dtype="float32", min=-1, max=1)
W
Wei Shengyu 已提交
667 668 669
            AdaptiveAvgPool1D = nn.AdaptiveAvgPool1D(output_size=16)
            pool_out = AdaptiveAvgPool1D(data)
            # pool_out shape: [1, 3, 16]
670 671
    """

672
    def __init__(self, output_size, name=None):
C
cnn 已提交
673
        super(AdaptiveAvgPool1D, self).__init__()
674
        self.output_size = output_size
675 676
        self.name = name

677 678 679
    def forward(self, input):
        return F.adaptive_avg_pool1d(input, self.output_size, self.name)

680 681 682
    def extra_repr(self):
        return 'output_size={}'.format(self.output_size)

683

Z
zhiboniu 已提交
684
class AdaptiveAvgPool2D(Layer):
685
    r"""
686 687 688 689 690 691 692 693

    This operation 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::

W
Wei Shengyu 已提交
694
        hstart &= floor(i * H_{in} / H_{out})
695

W
Wei Shengyu 已提交
696
        hend &= ceil((i + 1) * H_{in} / H_{out})
697

W
Wei Shengyu 已提交
698
        wstart &= floor(j * W_{in} / W_{out})
699

W
Wei Shengyu 已提交
700
        wend &= ceil((j + 1) * W_{in} / W_{out})
701

W
Wei Shengyu 已提交
702
        Output(i ,j) &= \frac{\sum Input[hstart:hend, wstart:wend]}{(hend - hstart) * (wend - wstart)}
703 704 705


    Parameters:
W
Wei Shengyu 已提交
706
        output_size(int|list|tuple): The pool kernel size. If pool kernel size is a tuple or list,
707 708
            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.
W
Wei Shengyu 已提交
709
        data_format(str, optional): The data format of the input and output data. An optional string
710 711
            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].
W
Wei Shengyu 已提交
712 713
        name(str, optional): For detailed information, please refer to :ref:`api_guide_Name`.
            Usually name is no need to set and None by default.
714 715

    Shape:
W
Wei Shengyu 已提交
716 717 718 719
        - x(Tensor): The input tensor of adaptive avg pool2d operator, which is a 4-D tensor.
          The data type can be float32, float64.
        - output(Tensor): The output tensor of adaptive avg pool2d operator, which is a 4-D tensor.
          The data type is same as input x.
720 721

    Returns:
C
cnn 已提交
722
        A callable object of AdaptiveAvgPool2D.
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742

    Examples:
        .. code-block:: python

            # 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
743

744 745
            x = paddle.rand([2, 3, 32, 32])

C
cnn 已提交
746
            adaptive_avg_pool = paddle.nn.AdaptiveAvgPool2D(output_size=3)
747 748 749 750 751
            pool_out = adaptive_avg_pool(x = x)
            # pool_out.shape is [2, 3, 3, 3]
    """

    def __init__(self, output_size, data_format="NCHW", name=None):
C
cnn 已提交
752
        super(AdaptiveAvgPool2D, self).__init__()
753 754 755 756 757
        self._output_size = output_size
        self._data_format = data_format
        self._name = name

    def forward(self, x):
758 759 760 761 762 763
        return F.adaptive_avg_pool2d(
            x,
            output_size=self._output_size,
            data_format=self._data_format,
            name=self._name,
        )
764

765 766 767
    def extra_repr(self):
        return 'output_size={}'.format(self._output_size)

768

Z
zhiboniu 已提交
769
class AdaptiveAvgPool3D(Layer):
770
    r"""
771 772 773 774 775 776 777 778

    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::

W
Wei Shengyu 已提交
779
        dstart &= floor(i * D_{in} / D_{out})
780

W
Wei Shengyu 已提交
781
        dend &= ceil((i + 1) * D_{in} / D_{out})
782

W
Wei Shengyu 已提交
783
        hstart &= floor(j * H_{in} / H_{out})
784

W
Wei Shengyu 已提交
785
        hend &= ceil((j + 1) * H_{in} / H_{out})
786

W
Wei Shengyu 已提交
787
        wstart &= floor(k * W_{in} / W_{out})
788

W
Wei Shengyu 已提交
789
        wend &= ceil((k + 1) * W_{in} / W_{out})
790

W
Wei Shengyu 已提交
791 792
        Output(i ,j, k) &= \frac{\sum Input[dstart:dend, hstart:hend, wstart:wend]}
            {(dend - dstart) * (hend - hstart) * (wend - wstart)}
793 794 795


    Parameters:
W
Wei Shengyu 已提交
796
        output_size(int|list|tuple): The pool kernel size. If pool kernel size is a tuple or list,
797 798
            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.
W
Wei Shengyu 已提交
799
        data_format(str, optional): The data format of the input and output data. An optional string
800 801
            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].
W
Wei Shengyu 已提交
802 803
        name(str, optional): For detailed information, please refer to :ref:`api_guide_Name`.
            Usually name is no need to set and None by default.
804
    Shape:
W
Wei Shengyu 已提交
805 806 807 808
        - x(Tensor): The input tensor of adaptive avg pool3d operator, which is a 5-D tensor.
          The data type can be float32, float64\.
        - output(Tensor): The output tensor of adaptive avg pool3d operator, which is a 5-D tensor.
          The data type is same as input x.
809 810

    Returns:
C
cnn 已提交
811
        A callable object of AdaptiveAvgPool3D.
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834

    Examples:
        .. code-block:: python

            # 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
835

836 837
            x = paddle.rand([2, 3, 8, 32, 32])

C
cnn 已提交
838
            adaptive_avg_pool = paddle.nn.AdaptiveAvgPool3D(output_size=3)
839 840 841 842 843
            pool_out = adaptive_avg_pool(x = x)
            # pool_out = [2, 3, 3, 3, 3]
    """

    def __init__(self, output_size, data_format="NCDHW", name=None):
C
cnn 已提交
844
        super(AdaptiveAvgPool3D, self).__init__()
845 846 847 848 849
        self._output_size = output_size
        self._data_format = data_format
        self._name = name

    def forward(self, x):
850 851 852 853 854 855
        return F.adaptive_avg_pool3d(
            x,
            output_size=self._output_size,
            data_format=self._data_format,
            name=self._name,
        )
856

857 858 859
    def extra_repr(self):
        return 'output_size={}'.format(self._output_size)

860

Z
zhiboniu 已提交
861
class AdaptiveMaxPool1D(Layer):
862 863 864
    """

    This operation applies a 1D adaptive max pooling over an input signal composed
865
    of several input planes, based on the input, output_size, return_mask parameters.
866 867 868 869 870 871 872 873
    Input(X) and output(Out) are in NCL format, where N is batch
    size, C is the number of channels, L is the length of the feature.
    The output tensor shape will be [N, C, output_size].

    For max adaptive pool1d:

    ..  math::

W
Wei Shengyu 已提交
874
        lstart &= floor(i * L_{in} / L_{out})
875

W
Wei Shengyu 已提交
876
        lend &= ceil((i + 1) * L_{in} / L_{out})
877

W
Wei Shengyu 已提交
878
        Output(i) &= max(Input[lstart:lend])
879

W
Wei Shengyu 已提交
880 881 882 883
    Parameters:
        output_size(int|list|tuple): The pool kernel size. If pool kernel size is a tuple or list,
            it must contain one int.
        return_mask(bool, optional): If true, the index of max pooling point will be returned along
884
            with outputs. It cannot be set in average pooling type. Default False.
W
Wei Shengyu 已提交
885 886
        name(str, optional): For detailed information, please refer to :ref:`api_guide_Name`.
            Usually name is no need to set and None by default.
887
    Returns:
W
Wei Shengyu 已提交
888
        A callable object of AdaptiveMaxPool1D.
889 890

    Shape:
W
Wei Shengyu 已提交
891 892 893 894
        - x(Tensor): The input tensor of adaptive max pool1d operator, which is a 3-D tensor.
          The data type can be float32, float64.
        - output(Tensor): The output tensor of adaptive max pool1d operator, which is a 3-D tensor.
          The data type is same as input x.
895 896 897 898

    Examples:
        .. code-block:: python

W
Wei Shengyu 已提交
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
            # 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 as nn

914
            data = paddle.uniform([1, 3, 32], dtype="float32", min=-1, max=1)
W
Wei Shengyu 已提交
915 916 917 918 919 920 921 922
            AdaptiveMaxPool1D = nn.AdaptiveMaxPool1D(output_size=16)
            pool_out = AdaptiveMaxPool1D(data)
            # pool_out shape: [1, 3, 16]

            # for return_mask = true
            AdaptiveMaxPool1D = nn.AdaptiveMaxPool1D(output_size=16, return_mask=True)
            pool_out, indices = AdaptiveMaxPool1D(data)
            # pool_out shape: [1, 3, 16], indices shape: [1, 3, 16]
923 924 925

    """

926
    def __init__(self, output_size, return_mask=False, name=None):
C
cnn 已提交
927
        super(AdaptiveMaxPool1D, self).__init__()
928
        self.output_size = output_size
929
        self.return_mask = return_mask
930 931 932
        self.name = name

    def forward(self, input):
933 934 935
        return F.adaptive_max_pool1d(
            input, self.output_size, self.return_mask, self.name
        )
936

937
    def extra_repr(self):
938 939 940
        return 'output_size={}, return_mask={}'.format(
            self.output_size, self.return_mask
        )
941

942

Z
zhiboniu 已提交
943
class AdaptiveMaxPool2D(Layer):
944 945
    """
    This operation applies 2D adaptive max pooling on input tensor. The h and w dimensions
W
Wei Shengyu 已提交
946 947
    of the output tensor are determined by the parameter output_size. The difference between adaptive pooling and
    pooling is adaptive one focus on the output size.
948

949
    For adaptive max pool2d:
950

951
    ..  math::
952

W
Wei Shengyu 已提交
953
        hstart &= floor(i * H_{in} / H_{out})
954

W
Wei Shengyu 已提交
955
        hend &= ceil((i + 1) * H_{in} / H_{out})
956

W
Wei Shengyu 已提交
957
        wstart &= floor(j * W_{in} / W_{out})
958

W
Wei Shengyu 已提交
959
        wend &= ceil((j + 1) * W_{in} / W_{out})
960

W
Wei Shengyu 已提交
961
        Output(i ,j) &= max(Input[hstart:hend, wstart:wend])
962

963
    Parameters:
W
Wei Shengyu 已提交
964 965 966 967 968 969 970
        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.
        return_mask(bool, optional): If true, the index of max pooling point will be returned along with outputs.
            It cannot be set in average pooling type. Default False.
        name(str, optional): For detailed information, please refer to :ref:`api_guide_Name`.
            Usually name is no need to set and None by default.
971
    Shape:
W
Wei Shengyu 已提交
972 973 974 975
        - x(Tensor): The input tensor of adaptive max pool2d operator, which is a 4-D tensor.
          The data type can be float32, float64.
        - output(Tensor): The output tensor of adaptive max pool2d operator, which is a 4-D tensor.
          The data type is same as input x.
D
Double_V 已提交
976

977
    Returns:
C
cnn 已提交
978
        A callable object of AdaptiveMaxPool2D.
979 980
    Examples:
        .. code-block:: python
981

982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
            # adaptive max 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 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
998

999 1000
            x = paddle.rand([2, 3, 32, 32])

1001
            adaptive_max_pool = paddle.nn.AdaptiveMaxPool2D(output_size=3, return_mask=True)
1002 1003 1004
            pool_out, indices = adaptive_max_pool(x = x)
    """

1005
    def __init__(self, output_size, return_mask=False, name=None):
C
cnn 已提交
1006
        super(AdaptiveMaxPool2D, self).__init__()
1007
        self._output_size = output_size
1008
        self._return_mask = return_mask
1009 1010 1011
        self._name = name

    def forward(self, x):
1012 1013 1014 1015 1016 1017
        return F.adaptive_max_pool2d(
            x,
            output_size=self._output_size,
            return_mask=self._return_mask,
            name=self._name,
        )
1018

1019
    def extra_repr(self):
1020 1021 1022
        return 'output_size={}, return_mask={}'.format(
            self._output_size, self._return_mask
        )
1023

1024

Z
zhiboniu 已提交
1025
class AdaptiveMaxPool3D(Layer):
1026
    """
W
Wei Shengyu 已提交
1027 1028 1029
    This operation applies 3D adaptive max pooling on input tensor. The h and w dimensions of the output tensor are
    determined by the parameter output_size. The difference between adaptive pooling and pooling is adaptive one focus
    on the output size.
1030

1031
    For adaptive max pool3d:
1032

1033
    ..  math::
1034

W
Wei Shengyu 已提交
1035
        dstart &= floor(i * D_{in} / D_{out})
1036

W
Wei Shengyu 已提交
1037
        dend &= ceil((i + 1) * D_{in} / D_{out})
1038

W
Wei Shengyu 已提交
1039
        hstart &= floor(j * H_{in} / H_{out})
1040

W
Wei Shengyu 已提交
1041
        hend &= ceil((j + 1) * H_{in} / H_{out})
1042

W
Wei Shengyu 已提交
1043
        wstart &= floor(k * W_{in} / W_{out})
1044

W
Wei Shengyu 已提交
1045
        wend &= ceil((k + 1) * W_{in} / W_{out})
1046

W
Wei Shengyu 已提交
1047
        Output(i ,j, k) &= max(Input[dstart:dend, hstart:hend, wstart:wend])
1048

1049
    Parameters:
W
Wei Shengyu 已提交
1050 1051 1052 1053 1054 1055 1056
        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.
        return_mask(bool, optional): If true, the index of max pooling point will be returned along with outputs.
            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.
1057
    Shape:
W
Wei Shengyu 已提交
1058 1059 1060 1061 1062
        - x(Tensor): The input tensor of adaptive max pool3d operator, which is a 5-D tensor.
          The data type can be float32, float64.
        - output(Tensor): The output tensor of adaptive max pool3d operator, which is a 5-D tensor.
          The data type is same as input x.

1063
    Returns:
C
cnn 已提交
1064
        A callable object of AdaptiveMaxPool3D.
1065 1066
    Examples:
        .. code-block:: python
1067

1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
            # adaptive max 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 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(j * H / m)
            #                 hend = ceil((j + 1) * H / m)
            #                 wstart = floor(k * W / n)
            #                 wend = ceil((k + 1) * W / n)
            #                 output[:, :, i, j, k] =
            #                     max(input[:, :, dstart:dend, hstart: hend, wstart: wend])
            import paddle
1087

1088
            x = paddle.rand([2, 3, 8, 32, 32])
C
cnn 已提交
1089
            pool = paddle.nn.AdaptiveMaxPool3D(output_size=4)
1090 1091
            out = pool(x)
            # out shape: [2, 3, 4, 4, 4]
1092
            pool = paddle.nn.AdaptiveMaxPool3D(output_size=3, return_mask=True)
1093
            out, indices = pool(x)
1094
            # out shape: [2, 3, 4, 4, 4], indices shape: [2, 3, 4, 4, 4]
D
Double_V 已提交
1095

1096 1097
    """

1098
    def __init__(self, output_size, return_mask=False, name=None):
C
cnn 已提交
1099
        super(AdaptiveMaxPool3D, self).__init__()
1100
        self._output_size = output_size
1101
        self._return_mask = return_mask
1102 1103 1104
        self._name = name

    def forward(self, x):
1105 1106 1107 1108 1109 1110
        return F.adaptive_max_pool3d(
            x,
            output_size=self._output_size,
            return_mask=self._return_mask,
            name=self._name,
        )
1111 1112

    def extra_repr(self):
1113 1114 1115
        return 'output_size={}, return_mask={}'.format(
            self._output_size, self._return_mask
        )
1116 1117


1118
class MaxUnPool1D(Layer):
1119
    r"""
1120 1121
    This API implements max unpooling 1d opereation.

1122 1123
    `max_unpool1d` accepts the output of `max_pool1d` as input,
    including the indices of the maximum value and calculate the partial inverse.
1124 1125 1126 1127
    All non-maximum values ​​are set to zero.

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

1129 1130 1131 1132
    .. math::
        L_{out} = (L_{in} - 1) * stride - 2 * padding + kernel\_size

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

1134 1135 1136 1137 1138 1139
    Parameters:
        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.
1140
        output_size(list|tuple, optional): The target output size. If output_size is not specified,
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
                           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:
        A callable object of MaxUnPool1D.

    Examples:
        .. code-block:: python
1156

1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
            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]
            Unpool1D = paddle.nn.MaxUnPool1D(kernel_size=2, padding=0)
            unpool_out = Unpool1D(pool_out, indices)
            # unpool_out shape: [1, 3, 16]

    """

1169 1170 1171 1172 1173 1174 1175 1176 1177
    def __init__(
        self,
        kernel_size,
        stride=None,
        padding=0,
        data_format="NCL",
        output_size=None,
        name=None,
    ):
1178 1179 1180 1181 1182 1183 1184 1185 1186
        super(MaxUnPool1D, self).__init__()
        self.ksize = kernel_size
        self.stride = stride
        self.padding = padding
        self.data_format = data_format
        self.output_size = output_size
        self.name = name

    def forward(self, x, indices):
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
        return F.max_unpool1d(
            x,
            indices,
            kernel_size=self.ksize,
            stride=self.stride,
            padding=self.padding,
            data_format=self.data_format,
            output_size=self.output_size,
            name=self.name,
        )
1197 1198 1199 1200 1201

    def extra_repr(self):
        return 'output_size={}'.format(self.output_size)


1202
class MaxUnPool2D(Layer):
1203
    r"""
1204 1205
    This API implements max unpooling 2d opereation.

1206 1207 1208
    'max_unpool2d' accepts the output of 'max_unpool2d' as input
    Including the indices of the maximum value and calculating the partial inverse
    All non-maximum values ​​are set to zero.
1209

1210 1211 1212 1213 1214 1215 1216 1217

    Parameters:
        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.
        kernel_size (int|tuple): Size of the max unpooling window.
        padding (int | tuple): Padding that was added to the input.
1218
        output_size(list|tuple, optional): The target output size. If output_size is not specified,
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
                           the actual output shape will be automatically calculated by (input_shape,
                           kernel_size, padding).
        name(str, optional): For detailed information, please refer
                             to :ref:`api_guide_Name`. Usually name is no need to set and
                             None by default.


        - 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:
        A callable object of MaxUnPool2D.

1240

1241 1242 1243

    Examples:
        .. code-block:: python
1244

1245 1246 1247
        import paddle
        import paddle.nn.functional as F

X
xiaoting 已提交
1248
        data = paddle.rand(shape=[1,1,6,6])
1249 1250 1251
        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]
        Unpool2D = paddle.nn.MaxUnPool2D(kernel_size=2, padding=0)
X
xiaoting 已提交
1252
        unpool_out = Unpool2D(pool_out, indices)
1253 1254 1255 1256
        # unpool_out shape: [1, 1, 6, 6]

    """

1257 1258 1259 1260 1261 1262 1263 1264 1265
    def __init__(
        self,
        kernel_size,
        stride=None,
        padding=0,
        data_format="NCHW",
        output_size=None,
        name=None,
    ):
1266 1267 1268 1269 1270 1271 1272 1273 1274
        super(MaxUnPool2D, self).__init__()
        self.ksize = kernel_size
        self.stride = stride
        self.padding = padding
        self.data_format = data_format
        self.output_size = output_size
        self.name = name

    def forward(self, x, indices):
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
        return F.max_unpool2d(
            x,
            indices,
            kernel_size=self.ksize,
            stride=self.stride,
            padding=self.padding,
            data_format=self.data_format,
            output_size=self.output_size,
            name=self.name,
        )
1285 1286 1287

    def extra_repr(self):
        return 'output_size={}'.format(self.output_size)
1288 1289 1290


class MaxUnPool3D(Layer):
1291
    r"""
1292 1293
    This API implements max unpooling 3d opereation.

1294 1295
    `max_unpool3d` accepts the output of `max_pool3d` as input,
    including the indices of the maximum value and calculate the partial inverse.
1296 1297 1298 1299
    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
1300

1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
    .. 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

1312

1313 1314 1315 1316 1317 1318
    Parameters:
        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.
1319
        output_size(list|tuple, optional): The target output size. If output_size is not specified,
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
                           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:
        A callable object of MaxUnPool3D.

    Examples:
        .. code-block:: python
1335

1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
            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]
            Unpool3D = paddle.nn.MaxUnPool3D(kernel_size=2, padding=0)
            unpool_out = Unpool3D(pool_out, indices)
            # unpool_out shape: [1, 1, 4, 4, 6]

    """

1348 1349 1350 1351 1352 1353 1354 1355 1356
    def __init__(
        self,
        kernel_size,
        stride=None,
        padding=0,
        data_format="NCDHW",
        output_size=None,
        name=None,
    ):
1357 1358 1359 1360 1361 1362 1363 1364 1365
        super(MaxUnPool3D, self).__init__()
        self.ksize = kernel_size
        self.stride = stride
        self.padding = padding
        self.data_format = data_format
        self.output_size = output_size
        self.name = name

    def forward(self, x, indices):
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
        return F.max_unpool3d(
            x,
            indices,
            kernel_size=self.ksize,
            stride=self.stride,
            padding=self.padding,
            data_format=self.data_format,
            output_size=self.output_size,
            name=self.name,
        )
1376 1377 1378

    def extra_repr(self):
        return 'output_size={}'.format(self.output_size)