pooling.py 48.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#   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 ...fluid.dygraph import layers
from ...fluid.layer_helper import LayerHelper
from .. import functional as F


C
cnn 已提交
20
class AvgPool1D(layers.Layer):
W
Wei Shengyu 已提交
21
    r"""
22
    This operation applies a 1D average pooling over an input signal composed
23
    of several input planes, based on the input, output_size, return_mask parameters.
24 25 26 27 28
    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 已提交
29
    output (N, C, :math:`L_{out}`) and kernel_size ksize can be precisely described as
30 31 32 33
    For average pool1d:

    ..  math::

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

W
Wei Shengyu 已提交
36 37
    Parameters:
        kernel_size(int|list|tuple): The pool kernel size. If pool kernel size is a tuple or list,
38
            it must contain an integer.
W
Wei Shengyu 已提交
39 40 41
        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.
42 43 44 45 46 47
            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 已提交
48 49 50 51 52
        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.
53 54

    Returns:
W
Wei Shengyu 已提交
55
        A callable object of AvgPool1D.
56 57 58 59 60

    Raises:
        ValueError: If `padding` is a string, but not "SAME" or "VALID".
        ValueError: If `padding` is "VALID", but `ceil_mode` is True.
        ValueError: If `padding` is a list or tuple but its length greater than 1.
61
        ShapeError: If the input is not a 3-D tensor.
62 63
        ShapeError: If the output's shape calculated is not greater than 0.

64
    Shape:
W
Wei Shengyu 已提交
65 66 67 68
        - 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.
69

70 71 72
    Examples:

        .. code-block:: python
73

W
Wei Shengyu 已提交
74 75 76
            import paddle
            import paddle.nn as nn
            import numpy as np
77

W
Wei Shengyu 已提交
78 79 80 81
            data = paddle.to_tensor(np.random.uniform(-1, 1, [1, 3, 32]).astype(np.float32))
            AvgPool1D = nn.AvgPool1D(kernel_size=2, stride=2, padding=0)
            pool_out = AvgPool1D(data)
            # pool_out shape: [1, 3, 16]
82 83 84 85 86 87 88

    """

    def __init__(self,
                 kernel_size,
                 stride=None,
                 padding=0,
89
                 exclusive=True,
90 91
                 ceil_mode=False,
                 name=None):
C
cnn 已提交
92
        super(AvgPool1D, self).__init__()
93 94 95 96
        self.kernel_size = kernel_size
        self.stride = stride
        self.padding = padding
        self.ceil_mode = ceil_mode
97
        self.exclusive = exclusive
98 99 100 101
        self.name = name

    def forward(self, x):
        out = F.avg_pool1d(x, self.kernel_size, self.stride, self.padding,
102
                           self.exclusive, self.ceil_mode, self.name)
103 104
        return out

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

109

C
cnn 已提交
110
class AvgPool2D(layers.Layer):
111
    r"""
112 113 114 115
    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.
116

117
    Example:
W
Wei Shengyu 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
        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,
133 134
            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 已提交
135
        stride(int|list|tuple, optional): The pool stride size. If pool stride size is a tuple or list,
136 137
            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 已提交
138 139
            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.
140 141 142 143 144 145
            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 已提交
146 147 148 149 150 151 152 153 154 155
        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.
156

157
    Shape:
W
Wei Shengyu 已提交
158 159 160 161
        - 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.
162

W
Wei Shengyu 已提交
163 164
    Returns:
        A callable object of AvgPool2D.
165 166 167 168 169 170
    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
171

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

W
Wei Shengyu 已提交
176 177 178
            # max pool2d
            input = paddle.to_tensor(np.random.uniform(-1, 1, [1, 3, 32, 32]).astype(np.float32))
            AvgPool2D = nn.AvgPool2D(kernel_size=2,
179
                                stride=2, padding=0)
W
Wei Shengyu 已提交
180 181
            output = AvgPool2D(input)
            # output.shape [1, 3, 16, 16]
182 183 184 185 186 187 188 189

    """

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

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

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

220

C
cnn 已提交
221
class AvgPool3D(layers.Layer):
222
    """
223 224 225 226
    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.
227

W
Wei Shengyu 已提交
228 229
    Parameters:
        kernel_size(int|list|tuple): The pool kernel size. If pool kernel size
230 231 232
            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 已提交
233
        stride(int|list|tuple, optional): The pool stride size. If pool stride size is a tuple or list,
234 235
            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 已提交
236 237
            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.
238 239 240 241 242 243
            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 已提交
244 245 246 247 248 249 250
        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]`.
251
        name(str, optional): For detailed information, please refer
W
Wei Shengyu 已提交
252 253
             to :ref:`api_guide_Name`. Usually name is no need to set and
             None by default.
254

W
Wei Shengyu 已提交
255 256
    Returns:
        A callable object of AvgPool3D.
257
    Raises:
258 259 260 261 262
        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.

    Shape:
W
Wei Shengyu 已提交
263 264 265 266
        - 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.
267 268
    Examples:
        .. code-block:: python
269

W
Wei Shengyu 已提交
270 271 272
            import paddle
            import paddle.nn as nn
            import numpy as np
273

W
Wei Shengyu 已提交
274 275 276
            # avg pool3d
            input = paddle.to_tensor(np.random.uniform(-1, 1, [1, 2, 3, 32, 32]).astype(np.float32))
            AvgPool3D = nn.AvgPool3D(kernel_size=2,
277
                                   stride=2, padding=0)
W
Wei Shengyu 已提交
278 279
            output = AvgPool3D(input)
            # output.shape [1, 2, 3, 16, 16]
280

281 282
    """

283 284
    def __init__(self,
                 kernel_size,
W
Wei Shengyu 已提交
285
                 stride=None,
286 287
                 padding=0,
                 ceil_mode=False,
288
                 exclusive=True,
289 290 291
                 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 303 304 305 306 307 308
    def forward(self, x):
        return F.avg_pool3d(
            x,
            kernel_size=self.ksize,
            stride=self.stride,
            padding=self.padding,
            ceil_mode=self.ceil_mode,
309
            exclusive=self.exclusive,
310 311 312
            divisor_override=self.divisor,
            data_format=self.data_format,
            name=self.name)
313

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

318

C
cnn 已提交
319
class MaxPool1D(layers.Layer):
320
    """
W
Wei Shengyu 已提交
321 322 323 324 325
    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.
326

327 328 329
    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:
330 331 332

    ..  math::

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

W
Wei Shengyu 已提交
335 336
    Parameters:
        kernel_size(int|list|tuple): The pool kernel size. If pool kernel size is a tuple or list,
337
            it must contain an integer.
W
Wei Shengyu 已提交
338 339 340
        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.
341 342 343
            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 已提交
344 345
            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).
346
            The default value is 0.
W
Wei Shengyu 已提交
347 348 349 350 351
        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.
352
    Returns:
W
Wei Shengyu 已提交
353
        A callable object of MaxPool1D.
354 355

    Raises:
356 357 358 359 360 361 362 363
        ValueError: If `padding` is a string, but not "SAME" or "VALID".
        ValueError: If `padding` is "VALID", but `ceil_mode` is True.
        ValueError: If `padding` is a list or tuple but its length greater than 1.
        ShapeError: If the input is not a 3-D.
        ShapeError: If the output's shape calculated is not greater than 0.


    Shape:
W
Wei Shengyu 已提交
364 365 366 367
        - 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.
368 369

    Examples:
370

371 372
        .. code-block:: python

W
Wei Shengyu 已提交
373 374 375
            import paddle
            import paddle.nn as nn
            import numpy as np
376

W
Wei Shengyu 已提交
377 378 379 380
            data = paddle.to_tensor(np.random.uniform(-1, 1, [1, 3, 32]).astype(np.float32))
            MaxPool1D = nn.MaxPool1D(kernel_size=2, stride=2, padding=0)
            pool_out = MaxPool1D(data)
            # pool_out shape: [1, 3, 16]
381

W
Wei Shengyu 已提交
382 383 384
            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]
385 386 387

    """

388 389 390 391
    def __init__(self,
                 kernel_size,
                 stride=None,
                 padding=0,
392
                 return_mask=False,
393 394
                 ceil_mode=False,
                 name=None):
C
cnn 已提交
395
        super(MaxPool1D, self).__init__()
396 397 398 399
        self.kernel_size = kernel_size
        self.stride = stride
        self.padding = padding
        self.ceil_mode = ceil_mode
400
        self.return_mask = return_mask
401 402 403
        self.name = name

    def forward(self, input):
404
        out = F.max_pool1d(input, self.kernel_size, self.stride, self.padding,
405
                           self.return_mask, self.ceil_mode, self.name)
406
        return out
407

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

412

C
cnn 已提交
413
class MaxPool2D(layers.Layer):
414
    r"""
415
    This operation applies 2D max pooling over input feature based on the input,
416 417 418 419 420
    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 已提交
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
        - 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,
436 437
            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 已提交
438
        stride(int|list|tuple, optional): The pool stride size. If pool stride size is a tuple or list,
439
            it must contain two integers, (pool_stride_Height, pool_stride_Width).
440
            Otherwise, the pool stride size will be a square of an int.
W
Wei Shengyu 已提交
441 442
            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.
443 444 445
            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 已提交
446
            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.
447 448
            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 已提交
449 450 451 452 453 454 455
        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.
456

W
Wei Shengyu 已提交
457 458
    Returns:
        A callable object of MaxPool2D.
459 460 461 462
    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.
463 464

    Shape:
W
Wei Shengyu 已提交
465 466 467 468
        - 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.
469

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

W
Wei Shengyu 已提交
473 474 475
            import paddle
            import paddle.nn as nn
            import numpy as np
476

W
Wei Shengyu 已提交
477 478 479
            # max pool2d
            input = paddle.to_tensor(np.random.uniform(-1, 1, [1, 3, 32, 32]).astype(np.float32))
            MaxPool2D = nn.MaxPool2D(kernel_size=2,
480
                                   stride=2, padding=0)
W
Wei Shengyu 已提交
481 482
            output = MaxPool2D(input)
            # output.shape [1, 3, 16, 16]
483

W
Wei Shengyu 已提交
484 485 486 487
            # 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],
488 489 490 491 492 493
    """

    def __init__(self,
                 kernel_size,
                 stride=None,
                 padding=0,
494
                 return_mask=False,
495 496 497
                 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
        return F.max_pool2d(
509 510 511 512
            x,
            kernel_size=self.ksize,
            stride=self.stride,
            padding=self.padding,
513
            return_mask=self.return_mask,
D
Double_V 已提交
514
            ceil_mode=self.ceil_mode,
515 516 517
            data_format=self.data_format,
            name=self.name)

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

522

C
cnn 已提交
523
class MaxPool3D(layers.Layer):
524
    """
525
    This operation applies 3D max pooling over input features based on the input,
526
    and kernel_size, stride, padding parameters. Input(X) and Output(Out) are
527 528
    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.
529

W
Wei Shengyu 已提交
530 531
    Parameters:
        kernel_size(int|list|tuple): The pool kernel size. If the kernel size
532
            is a tuple or list, it must contain three integers,
533
            (kernel_size_Depth, kernel_size_Height, kernel_size_Width).
534
            Otherwise, the pool kernel size will be the cube of an int.
W
Wei Shengyu 已提交
535
        stride(int|list|tuple, optional): The pool stride size. If pool stride size is a tuple or list,
536 537
            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 已提交
538 539
            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.
540 541 542
            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 已提交
543
            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.
544 545
            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 已提交
546 547 548 549 550 551 552
        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.
553 554


W
Wei Shengyu 已提交
555 556
    Returns:
        A callable object of MaxPool3D.
557 558 559 560
    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.
561 562

    Shape:
W
Wei Shengyu 已提交
563 564 565 566
        - 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.
567

568 569
    Examples:
        .. code-block:: python
570

W
Wei Shengyu 已提交
571 572 573
            import paddle
            import paddle.nn as nn
            import numpy as np
574

W
Wei Shengyu 已提交
575 576 577
            # max pool3d
            input = paddle.to_tensor(np.random.uniform(-1, 1, [1, 2, 3, 32, 32]).astype(np.float32))
            MaxPool3D = nn.MaxPool3D(kernel_size=2,
578
                                   stride=2, padding=0)
W
Wei Shengyu 已提交
579 580
            output = MaxPool3D(input)
            # output.shape [1, 2, 3, 16, 16]
581

W
Wei Shengyu 已提交
582 583 584 585
            # 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],
586 587 588 589
    """

    def __init__(self,
                 kernel_size,
P
parap1uie-s 已提交
590 591
                 stride=None,
                 padding=0,
592
                 return_mask=False,
593 594 595
                 ceil_mode=False,
                 data_format="NCDHW",
                 name=None):
C
cnn 已提交
596
        super(MaxPool3D, self).__init__()
597 598 599
        self.ksize = kernel_size
        self.stride = stride
        self.padding = padding
600
        self.return_mask = return_mask
601 602 603 604 605 606 607 608 609 610
        self.ceil_mode = ceil_mode
        self.data_format = data_format
        self.name = name

    def forward(self, x):
        return F.max_pool3d(
            x,
            kernel_size=self.ksize,
            stride=self.stride,
            padding=self.padding,
611
            return_mask=self.return_mask,
D
Double_V 已提交
612
            ceil_mode=self.ceil_mode,
613 614 615
            data_format=self.data_format,
            name=self.name)

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

620

C
cnn 已提交
621
class AdaptiveAvgPool1D(layers.Layer):
622
    r"""
623 624

    This operation applies a 1D adaptive average pooling over an input signal composed
625
    of several input planes, based on the input, output_size, return_mask parameters.
626 627 628 629 630 631 632 633
    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 average adaptive pool1d:

    ..  math::

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

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

W
Wei Shengyu 已提交
638
        Output(i) &= \frac{ \sum Input[lstart:lend]}{lend - lstart}
639

W
Wei Shengyu 已提交
640 641 642 643
    Parameters:
        output_size(int): The target output size. It must be an integer.
        name(str, optional): For detailed information, please refer to :ref:`api_guide_Name`.
            Usually name is no need to set and None by default.
644

645
    Returns:
W
Wei Shengyu 已提交
646
        A callable object of AdaptiveAvgPool1D.
647

648
    Raises:
649
        ValueError: 'output_size' should be an integer.
650 651

    Shape:
W
Wei Shengyu 已提交
652 653 654 655
        - x(Tensor): 3-D tensor. The input tensor of adaptive avg pool1d operator, which is a 3-D tensor.
          The data type can be float32, float64.
        - output(Tensor): 3-D tensor. The output tensor of adaptive avg pool1d operator, which is a 3-D tensor.
          The data type is same as input x.
656

657 658
    Examples:
        .. code-block:: python
659

W
Wei Shengyu 已提交
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
            # 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
            import numpy as np

            data = paddle.to_tensor(np.random.uniform(-1, 1, [1, 3, 32]).astype(np.float32))
            AdaptiveAvgPool1D = nn.AdaptiveAvgPool1D(output_size=16)
            pool_out = AdaptiveAvgPool1D(data)
            # pool_out shape: [1, 3, 16]
680 681
    """

682
    def __init__(self, output_size, name=None):
C
cnn 已提交
683
        super(AdaptiveAvgPool1D, self).__init__()
684
        self.output_size = output_size
685 686
        self.name = name

687 688 689
    def forward(self, input):
        return F.adaptive_avg_pool1d(input, self.output_size, self.name)

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

693

C
cnn 已提交
694
class AdaptiveAvgPool2D(layers.Layer):
695
    r"""
696 697 698 699 700 701 702 703

    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 已提交
704
        hstart &= floor(i * H_{in} / H_{out})
705

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

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

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

W
Wei Shengyu 已提交
712
        Output(i ,j) &= \frac{\sum Input[hstart:hend, wstart:wend]}{(hend - hstart) * (wend - wstart)}
713 714 715


    Parameters:
W
Wei Shengyu 已提交
716
        output_size(int|list|tuple): The pool kernel size. If pool kernel size is a tuple or list,
717 718
            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 已提交
719
        data_format(str, optional): The data format of the input and output data. An optional string
720 721
            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 已提交
722 723
        name(str, optional): For detailed information, please refer to :ref:`api_guide_Name`.
            Usually name is no need to set and None by default.
724 725

    Shape:
W
Wei Shengyu 已提交
726 727 728 729
        - 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.
730 731

    Returns:
C
cnn 已提交
732
        A callable object of AdaptiveAvgPool2D.
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753

    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
            import numpy as np
754

755 756 757
            input_data = np.random.rand(2, 3, 32, 32)
            x = paddle.to_tensor(input_data)
            # x.shape is [2, 3, 32, 32]
C
cnn 已提交
758
            adaptive_avg_pool = paddle.nn.AdaptiveAvgPool2D(output_size=3)
759 760 761 762 763
            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 已提交
764
        super(AdaptiveAvgPool2D, self).__init__()
765 766 767 768 769 770 771 772 773 774 775
        self._output_size = output_size
        self._data_format = data_format
        self._name = name

    def forward(self, x):
        return F.adaptive_avg_pool2d(
            x,
            output_size=self._output_size,
            data_format=self._data_format,
            name=self._name)

776 777 778
    def extra_repr(self):
        return 'output_size={}'.format(self._output_size)

779

C
cnn 已提交
780
class AdaptiveAvgPool3D(layers.Layer):
781
    r"""
782 783 784 785 786 787 788 789

    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 已提交
790
        dstart &= floor(i * D_{in} / D_{out})
791

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

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

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

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

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

W
Wei Shengyu 已提交
802 803
        Output(i ,j, k) &= \frac{\sum Input[dstart:dend, hstart:hend, wstart:wend]}
            {(dend - dstart) * (hend - hstart) * (wend - wstart)}
804 805 806


    Parameters:
W
Wei Shengyu 已提交
807
        output_size(int|list|tuple): The pool kernel size. If pool kernel size is a tuple or list,
808 809
            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 已提交
810
        data_format(str, optional): The data format of the input and output data. An optional string
811 812
            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 已提交
813 814
        name(str, optional): For detailed information, please refer to :ref:`api_guide_Name`.
            Usually name is no need to set and None by default.
815
    Shape:
W
Wei Shengyu 已提交
816 817 818 819
        - 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.
820 821

    Returns:
C
cnn 已提交
822
        A callable object of AdaptiveAvgPool3D.
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846

    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
            import numpy as np
847

848 849 850
            input_data = np.random.rand(2, 3, 8, 32, 32)
            x = paddle.to_tensor(input_data)
            # x.shape is [2, 3, 8, 32, 32]
C
cnn 已提交
851
            adaptive_avg_pool = paddle.nn.AdaptiveAvgPool3D(output_size=3)
852 853 854 855 856
            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 已提交
857
        super(AdaptiveAvgPool3D, self).__init__()
858 859 860 861 862 863 864 865 866 867 868
        self._output_size = output_size
        self._data_format = data_format
        self._name = name

    def forward(self, x):
        return F.adaptive_avg_pool3d(
            x,
            output_size=self._output_size,
            data_format=self._data_format,
            name=self._name)

869 870 871
    def extra_repr(self):
        return 'output_size={}'.format(self._output_size)

872

C
cnn 已提交
873
class AdaptiveMaxPool1D(layers.Layer):
874 875 876
    """

    This operation applies a 1D adaptive max pooling over an input signal composed
877
    of several input planes, based on the input, output_size, return_mask parameters.
878 879 880 881 882 883 884 885
    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 已提交
886
        lstart &= floor(i * L_{in} / L_{out})
887

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

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

W
Wei Shengyu 已提交
892 893 894 895
    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
896
            with outputs. It cannot be set in average pooling type. Default False.
W
Wei Shengyu 已提交
897 898
        name(str, optional): For detailed information, please refer to :ref:`api_guide_Name`.
            Usually name is no need to set and None by default.
899
    Returns:
W
Wei Shengyu 已提交
900
        A callable object of AdaptiveMaxPool1D.
901 902 903 904 905

    Raises:
        ValueError: 'pool_size' should be a integer or list or tuple with length as 1.

    Shape:
W
Wei Shengyu 已提交
906 907 908 909
        - 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.
910 911 912 913

    Examples:
        .. code-block:: python

W
Wei Shengyu 已提交
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
            # 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
            import numpy as np

            data = paddle.to_tensor(np.random.uniform(-1, 1, [1, 3, 32]).astype(np.float32))
            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]
939 940 941

    """

942
    def __init__(self, output_size, return_mask=False, name=None):
C
cnn 已提交
943
        super(AdaptiveMaxPool1D, self).__init__()
944
        self.output_size = output_size
945
        self.return_mask = return_mask
946 947 948
        self.name = name

    def forward(self, input):
949 950
        return F.adaptive_max_pool1d(input, self.output_size, self.return_mask,
                                     self.name)
951

952 953 954 955
    def extra_repr(self):
        return 'output_size={}, return_mask={}'.format(self.output_size,
                                                       self.return_mask)

956

C
cnn 已提交
957
class AdaptiveMaxPool2D(layers.Layer):
958 959
    """
    This operation applies 2D adaptive max pooling on input tensor. The h and w dimensions
W
Wei Shengyu 已提交
960 961
    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.
962

963
    For adaptive max pool2d:
964

965
    ..  math::
966

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

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

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

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

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

977
    Parameters:
W
Wei Shengyu 已提交
978 979 980 981 982 983 984
        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.
985
    Shape:
W
Wei Shengyu 已提交
986 987 988 989
        - 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 已提交
990

991
    Returns:
C
cnn 已提交
992
        A callable object of AdaptiveMaxPool2D.
993 994
    Examples:
        .. code-block:: python
995

996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
            # 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
            import numpy as np
1013

1014 1015
            input_data = np.random.rand(2, 3, 32, 32)
            x = paddle.to_tensor(input_data)
1016
            adaptive_max_pool = paddle.nn.AdaptiveMaxPool2D(output_size=3, return_mask=True)
1017 1018 1019
            pool_out, indices = adaptive_max_pool(x = x)
    """

1020
    def __init__(self, output_size, return_mask=False, name=None):
C
cnn 已提交
1021
        super(AdaptiveMaxPool2D, self).__init__()
1022
        self._output_size = output_size
1023
        self._return_mask = return_mask
1024 1025 1026 1027 1028 1029
        self._name = name

    def forward(self, x):
        return F.adaptive_max_pool2d(
            x,
            output_size=self._output_size,
1030
            return_mask=self._return_mask,
1031 1032
            name=self._name)

1033 1034 1035 1036
    def extra_repr(self):
        return 'output_size={}, return_mask={}'.format(self._output_size,
                                                       self._return_mask)

1037

C
cnn 已提交
1038
class AdaptiveMaxPool3D(layers.Layer):
1039
    """
W
Wei Shengyu 已提交
1040 1041 1042
    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.
1043

1044
    For adaptive max pool3d:
1045

1046
    ..  math::
1047

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

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

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

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

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

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

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

1062
    Parameters:
W
Wei Shengyu 已提交
1063 1064 1065 1066 1067 1068 1069
        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.
1070
    Shape:
W
Wei Shengyu 已提交
1071 1072 1073 1074 1075
        - 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.

1076
    Returns:
C
cnn 已提交
1077
        A callable object of AdaptiveMaxPool3D.
1078 1079
    Examples:
        .. code-block:: python
1080

1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
            # 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
            import numpy as np
1101

1102 1103
            input_data = np.random.rand(2, 3, 8, 32, 32)
            x = paddle.to_tensor(input_data)
C
cnn 已提交
1104
            pool = paddle.nn.AdaptiveMaxPool3D(output_size=4)
1105 1106
            out = pool(x)
            # out shape: [2, 3, 4, 4, 4]
1107
            pool = paddle.nn.AdaptiveMaxPool3D(output_size=3, return_mask=True)
1108
            out, indices = pool(x)
1109
            # out shape: [2, 3, 4, 4, 4], indices shape: [2, 3, 4, 4, 4]
D
Double_V 已提交
1110

1111 1112
    """

1113
    def __init__(self, output_size, return_mask=False, name=None):
C
cnn 已提交
1114
        super(AdaptiveMaxPool3D, self).__init__()
1115
        self._output_size = output_size
1116
        self._return_mask = return_mask
1117 1118 1119 1120 1121 1122
        self._name = name

    def forward(self, x):
        return F.adaptive_max_pool3d(
            x,
            output_size=self._output_size,
1123
            return_mask=self._return_mask,
1124
            name=self._name)
1125 1126 1127 1128

    def extra_repr(self):
        return 'output_size={}, return_mask={}'.format(self._output_size,
                                                       self._return_mask)