mindspore.nn.ReflectionPad2d

View Source On Gitee
class mindspore.nn.ReflectionPad2d(padding)[source]

Using a given padding to do reflection pad the given tensor. 2d means the dimension of padding is 2-dimension.

Parameters

padding (union[int, tuple]) – The padding size to pad the input tensor. If padding is an integer: all directions will be padded with the same size. If padding is a tuple: uses \((pad\_left, pad\_right, pad\_up, pad\_down)\) to pad.

Inputs:
  • x (Tensor) - 3D or 4D, shape: \((C, H_{in}, W_{in})\) or \((N, C, H_{in}, W_{in})\).

Outputs:

Tensor, after padding. Shape: \((C, H_{out}, W_{out})\) or \((N, C, H_{out}, W_{out})\), where \(H_{out} = H_{in} + pad\_up + pad\_down\), \(W_{out} = W_{in} + pad\_left + pad\_right\).

Raises
  • TypeError – If 'padding' is not a tuple or int.

  • TypeError – If there is an element in 'padding' that is not int.

  • ValueError – If the length of 'padding' is not divisible by 2.

  • ValueError – If there is an element in 'padding' that is negative.

  • ValueError – If the there is a dimension mismatch between the padding and the tensor.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import numpy as np
>>> import mindspore as ms
>>> x = ms.Tensor(np.array([[[0, 1, 2], [3, 4, 5], [6, 7, 8]]]).astype(np.float32))
>>> # x has shape (1, 3, 3)
>>> padding = (1, 1, 2, 0)
>>> pad2d = ms.nn.ReflectionPad2d(padding)
>>> # The first dimension of x remains the same.
>>> # The second dimension of x: H_out = H_in + pad_up + pad_down = 3 + 1 + 1 = 5
>>> # The third dimension of x: W_out = W_in + pad_left + pad_right = 3 + 2 + 0 = 5
>>> out = pad2d(x)
>>> # The shape of out is (1, 5, 5)
>>> print(out)
[[[7. 6. 7. 8. 7.]
  [4. 3. 4. 5. 4.]
  [1. 0. 1. 2. 1.]
  [4. 3. 4. 5. 4.]
  [7. 6. 7. 8. 7.]]]