mindspore.ops.GridSampler2D

class mindspore.ops.GridSampler2D(interpolation_mode='bilinear', padding_mode='zeros', align_corners=False)[source]

This operation samples 2d input_x by using interpolation based on flow field grid, which is usually gennerated by mindspore.ops.affine_grid().

Warning

This is an experimental API that is subject to change or deletion.

Parameters
  • interpolation_mode (str, optional) – An optional string specifying the interpolation method. The optional values are “bilinear” or “nearest”. Default: “bilinear”.

  • padding_mode (str, optional) –

    An optional string specifying the pad method. The optional values are “zeros”, “border” or “reflection”. Default: “zeros”. When the sampling grid is outside input’s bounds, effects of various padding modes are as follows:

    • ”zeros”: Pads the input tensor with zeros.

    • ”border”: Pads the input tensor with the values of the pixels on the border of the tensor.

    • ”reflection”: Pads the input tensor by reflecting the values of the pixels at the boundary of the tensor.

  • align_corners (bool, optional) – An optional bool. When set to True, the centers of the corner pixels of the input and output tensors are aligned. When set to False, it is not aligned. Default: False.

Inputs:
  • input_x (Tensor) - A 4-D tensor with dtype of float16 or float32 and shape of \((N, C, H_{in}, W_{in})\).

  • grid (Tensor) - A 4-D tensor whose dtype is the same as input_x and whose shape is \((N, H_{out}, W_{out}, 2)\). Used to specify the sampling pixel locations normalized by the input spatial dimensions.

Outputs:

A 4-D Tensor whose dtype is the same as input_x and whose shape is \((N, C, H_{out}, W_{out})\).

Raises
  • TypeError – If input_x or grid is not a Tensor.

  • TypeError – If the dtypes of input_x and grid are inconsistent.

  • TypeError – If the dtype of input_x or grid is not a valid type.

  • TypeError – If align_corners is not a boolean value.

  • ValueError – If the rank of input_x or grid is not equal to 4.

  • ValueError – If the first dimension of input_x is not equal to that of grid.

  • ValueError – If the forth dimension of grid is not equal to 2.

  • ValueError – If interpolation_mode is not “bilinear”, “nearest” or a string value.

  • ValueError – If padding_mode is not “zeros”, “border”, “reflection” or a string value.

Supported Platforms:

Ascend GPU CPU

Examples

>>> gridsampler = ops.GridSampler2D(interpolation_mode='bilinear', padding_mode='zeros', align_corners=True)
>>> input_x = Tensor(np.arange(16).reshape((2, 2, 2, 2)).astype(np.float32))
>>> grid = Tensor(np.arange(-9, 9, 0.5).reshape((2, 3, 3, 2)).astype(np.float32))
>>> output = gridsampler(input_x, grid)
>>> print(output)
[[[[ 0.     0.     0.   ]
   [ 0.     0.     0.   ]
   [ 0.     0.     0.5  ]]
  [[ 0.     0.     0.   ]
   [ 0.     0.     0.   ]
   [ 0.     1.5    4.5  ]]]
 [[[10.     8.25   1.375]
   [ 0.     0.     0.   ]
   [ 0.     0.     0.   ]]
  [[14.    11.25   1.875]
   [ 0.     0.     0.   ]
   [ 0.     0.     0.   ]]]]