pad2d

paddle.fluid.layers.nn. pad2d ( input, paddings=[0, 0, 0, 0], mode='constant', pad_value=0.0, data_format='NCHW', name=None ) [source]

Pad 2-d images according to ‘paddings’ and ‘mode’. If mode is ‘reflect’, paddings[0] and paddings[1] must be no greater than height-1. And the width dimension has the same condition.

Parameters
  • input (Tensor) – The input image with [N, C, H, W] format or [N, H, W, C] format, which is a 4-D Tensor with data type float32.

  • paddings (Tensor | List[int32]) – The padding size. If padding is a List, it must contain four integers, (padding_top, padding_bottom, padding_left, padding_right). Otherwise, it is a 1-D Tensor with shape [4]. Data type is int32. Default is [0, 0, 0, 0].

  • mode (str) – Three modes: ‘constant’ (default), ‘reflect’, ‘edge’ . When in ‘constant’ mode, this op uses a constant value to pad the input tensor. When in ‘reflect’ mode, uses reflection of the input boundaries to pad the input tensor. When in ‘edge’ mode, uses input boundaries to pad the input tensor. Default is ‘constant’

  • pad_value (float32) – The value to fill the padded areas in ‘constant’ mode . Default is 0.0

  • data_format (str) – An string from: “NHWC”, “NCHW”. Specify the data format of the input data. Default is “NCHW”

  • name (str, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name .

Returns: Tensor, a 4-D Tensor padded according to paddings and mode and data type is same as input.

Examples

Input = [[[[1., 2., 3.],
           [4., 5., 6.]]]]

Case 0:
    paddings = [0, 1, 2, 3],
    mode = 'constant'
    pad_value = 0
    Out = [[[[0., 0., 1., 2., 3., 0., 0., 0.],
             [0., 0., 4., 5., 6., 0., 0., 0.],
             [0., 0., 0., 0., 0., 0., 0., 0.]]]]

Case 1:
    paddings = [0, 1, 2, 1],
    mode = 'reflect'
    Out = [[[[3., 2., 1., 2., 3., 2.],
             [6., 5., 4., 5., 6., 5.],
             [3., 2., 1., 2., 3., 2.]]]]

Case 2:
    paddings = [0, 1, 2, 1],
    mode = 'edge'
    Out = [[[[1., 1., 1., 2., 3., 3.],
             [4., 4., 4., 5., 6., 6.],
             [4., 4., 4., 5., 6., 6.]]]]
Code Examples:
import numpy as np
import paddle
import paddle.nn.functional as F

# example 1
x_shape = (1, 1, 3, 4)
x = np.arange(np.prod(x_shape), dtype=np.float32).reshape(x_shape) + 1
tensor_x = paddle.to_tensor(x)
y = paddle.fluid.layers.pad2d(tensor_x, paddings=[1, 2, 2, 1], pad_value=1, mode='constant')
print(y.numpy())
# [[[[ 1.  1.  1.  1.  1.  1.  1.]
#    [ 1.  1.  1.  2.  3.  4.  1.]
#    [ 1.  1.  5.  6.  7.  8.  1.]
#    [ 1.  1.  9. 10. 11. 12.  1.]
#    [ 1.  1.  1.  1.  1.  1.  1.]
#    [ 1.  1.  1.  1.  1.  1.  1.]]]]

# example 2
x_shape = (1, 1, 2, 3)
x = np.arange(np.prod(x_shape), dtype=np.float32).reshape(x_shape) + 1
tensor_x = paddle.to_tensor(x)
y = paddle.fluid.layers.pad2d(tensor_x, paddings=[1, 1, 1, 1], mode='reflect')
print(y.numpy())
# [[[[5. 4. 5. 6. 5.]
#    [2. 1. 2. 3. 2.]
#    [5. 4. 5. 6. 5.]
#    [2. 1. 2. 3. 2.]]]]