pool2d¶
- paddle.fluid.layers.nn. pool2d ( input, pool_size=- 1, pool_type='max', pool_stride=1, pool_padding=0, global_pooling=False, use_cudnn=True, ceil_mode=False, name=None, exclusive=True, data_format='NCHW' ) [source]
-
This operation calculates the pooling output based on the input, pooling_type and pool_size, pool_stride, pool_padding parameters. Input(X) and Output(Out) are in NCHW or NHWC format, where N is batch size, C is the number of channels, H is the height of the feature, and W is the width of the feature. Parameters(pool_size, pool_stride, pool_padding) hold two integer elements. These two elements represent height and width, respectively. The input(X) size and output(Out) size may be different.
Example:
Input:
X shape: $(N, C, H_{in}, W_{in})$
Output:
Out shape: $(N, C, H_{out}, W_{out})$
For pool_padding = “SAME”: $$ H_{out} = \frac{(H_{in} + strides[0] - 1)}{strides[0]} $$ $$ W_{out} = \frac{(W_{in} + strides[1] - 1)}{strides[1]} $$
For pool_padding = “VALID”: $$ H_{out} = \frac{(H_{in} - ksize[0] + strides[0])}{strides[0]} $$ $$ W_{out} = \frac{(W_{in} - ksize[1] + strides[1])}{strides[1]} $$
For ceil_mode = false: $$ H_{out} = \frac{(H_{in} - ksize[0] + pad_height_top + pad_height_bottom}{strides[0]} + 1 $$ $$ W_{out} = \frac{(W_{in} - ksize[1] + pad_width_left + pad_width_right}{strides[1]} + 1 $$
For ceil_mode = true: $$ H_{out} = \frac{(H_{in} - ksize[0] + pad_height_top + pad_height_bottom + strides[0] - 1)}{strides[0]} + 1 $$ $$ W_{out} = \frac{(W_{in} - ksize[1] + pad_width_left + pad_width_right + strides[1] - 1)}{strides[1]} + 1 $$
For exclusive = false: $$ hstart = i * strides[0] - pad_height_top $$ $$ hend = hstart + ksize[0] $$ $$ wstart = j * strides[1] - pad_width_left $$ $$ wend = wstart + ksize[1] $$ $$ Output(i ,j) = \frac{sum(Input[hstart:hend, wstart:wend])}{ksize[0] * ksize[1]} $$
For exclusive = true: $$ hstart = max(0, i * strides[0] - pad_height_top) $$ $$ hend = min(H, hstart + ksize[0]) $$ $$ wstart = max(0, j * strides[1] - pad_width_left) $$ $$ wend = min(W, wstart + ksize[1]) $$ $$ Output(i ,j) = \frac{sum(Input[hstart:hend, wstart:wend])}{(hend - hstart) * (wend - wstart)} $$
- Parameters
-
input (Variable) – The input tensor of pooling operator which is a 4-D tensor with shape [N, C, H, W]. The format of input tensor is “NCHW” or “NHWC”, where N is batch size, C is the number of channels, H is the height of the feature, and W is the width of the feature. The data type if float32 or float64.
pool_size (int|list|tuple) – The pool kernel size. If pool kernel size is a tuple or list, it must contain two integers, (pool_size_Height, pool_size_Width). Otherwise, the pool kernel size will be a square of an int.
pool_type – (string), pooling type, can be “max” for max-pooling and “avg” for average-pooling
pool_stride (int|list|tuple) – The pool stride size. If pool stride size is a tuple or list, it must contain two integers, (pool_stride_Height, pool_stride_Width). Otherwise, the pool stride size will be a square of an int.
pool_padding (string|int|list|tuple) – The pool padding. If pool_padding is a string, either ‘VALID’ or ‘SAME’ which is the padding algorithm. If pool padding size is a tuple or list, it could be in three forms: [pad_height, pad_width] or [pad_height_top, pad_height_bottom, pad_width_left, pad_width_right], and when data_format is “NCHW”, pool_padding can be in the form [[0,0], [0,0], [pad_height_top, pad_height_bottom], [pad_width_left, pad_width_right]]. when data_format is “NHWC”, pool_padding can be in the form [[0,0], [pad_height_top, pad_height_bottom], [pad_width_left, pad_width_right], [0,0]]. Otherwise, the pool padding size will be a square of an int.
global_pooling (bool) – (bool) Whether to use the global pooling. If global_pooling = true, kernel size and paddings will be ignored. Default False
use_cudnn (bool) – (bool) Only used in cudnn kernel, need install cudnn. Default False
ceil_mode (bool) – (bool) Whether to use the ceil function to calculate output height and width. False is the default. If it is set to False, the floor function will be used. Default False
name (str, optional) – For detailed information, please refer to Name. Usually name is no need to set and None by default.
exclusive (bool) – Whether to exclude padding points in average pooling mode, default is true.
data_format (string) – The data format of the input and output data. An optional string from: “NCHW”, “NHWC”. The default is “NCHW”. When it is “NCHW”, the data is stored in the order of: [batch_size, input_channels, input_height, input_width].
- Returns
-
The output tensor of pooling result. The data type is same as input tensor.
- Return type
-
Variable
- Raises
-
ValueError – If pool_type is not “max” nor “avg”.
ValueError – If global_pooling is False and pool_size is -1.
TypeError – If use_cudnn is not a bool value.
ValueError – If data_format is not “NCHW” or “NHWC”.
ValueError – If pool_padding is a string, but not “SAME” or “VALID”.
ValueError – If pool_padding is “VALID”, but ceil_mode is True.
ValueError – If pool_padding is a list or tuple, but the elements in the batch or channel dimensions are non-zero.
ShapeError – If the input is not a 4-D or 5-D Tensor.
ShapeError – If the dimension of input minus the size of pool_stride is not 2.
ShapeError – If the size of pool_size and pool_stride is not equal.
ShapeError – If the output’s shape calculated is not greater than 0.
Examples
import paddle.fluid as fluid import paddle paddle.enable_static() data = fluid.data(name='data', shape=[None, 3, 32, 32], dtype='float32') # max pool2d pool2d = fluid.layers.pool2d( input = data, pool_size = 2, pool_type = "max", pool_stride = 1, global_pooling=False) # average pool2d pool2d = fluid.layers.pool2d( input = data, pool_size = 2, pool_type = "avg", pool_stride = 1, global_pooling=False) # global average pool2d pool2d = fluid.layers.pool2d( input = data, pool_size = 2, pool_type = "avg", pool_stride = 1, global_pooling=True) # Attr(pool_padding) is a list with 4 elements, Attr(data_format) is "NCHW". out_1 = fluid.layers.pool2d( input = data, pool_size = 3, pool_type = "avg", pool_stride = 1, pool_padding = [1, 2, 1, 0], data_format = "NCHW") # Attr(pool_padding) is a string, Attr(data_format) is "NCHW". out_2 = fluid.layers.pool2d( input = data, pool_size = 3, pool_type = "avg", pool_stride = 1, pool_padding = "VALID", data_format = "NCHW")