mindspore.ops.ResizeNearestNeighborV2

class mindspore.ops.ResizeNearestNeighborV2(align_corners=False, half_pixel_centers=False, data_format='NHWC')[source]

Resizes the input tensor to specific size by using the nearest neighbor algorithm.

The nearest neighbor algorithm selects the value of the nearest point and does not consider the values of neighboring points at all, yielding a piecewise-constant interpolant.

Parameters
  • align_corners (bool, optional) – If true, the centers of the 4 corner pixels of the input and output tensors are aligned, preserving the values at the corner pixels. Defaults: False.

  • half_pixel_centers (bool, optional) – Whether half pixel center. If set to True, align_corners should be False. Default: False.

  • data_format (str, optional) – An optional string that describes the format of the input x. Default: NHWC.

Inputs:
  • x (Tensor) - 4-D with shape \((batch, height, width, channels)\) or \((batch, channels, height, width)\) depending on the attr ‘data_format’. Support type [int8, uint8, int16, uint16, int32, int64, float16, float32, float64].

  • size (Tensor) - The new size for the images. A 1-D int32 Tensor of 2 elements: [new_height, new_width].

Outputs:
  • y (Tensor) - The resized images. A 4-D with shape \((batch, new\_height, new\_width, channels)\) or \((batch, channels, new\_height, new\_width)\) depending on the attr data_format. It has the same dtype as x.

Raises
  • TypeError – If x or size is not a Tensor.

  • TypeError – If the data type of x is not in supported list.

  • TypeError – If the data type of size is not int32.

  • TypeError – If align_corners or half_pixel_centers is not bool.

  • TypeError – If data_format is not string.

  • ValueError – If data_format not in [NHWC, NCHW].

  • ValueError – If any value of size is non positive.

  • ValueError – If the dimension of x is not 4.

  • ValueError – If the dimension of size is not 1.

  • ValueError – If the elements number of size is not 2.

  • ValueError – If attr half_pixel_centers and align_corners are True at the same time.

Supported Platforms:

CPU

Examples

>>> input_tensor = Tensor(np.ones((1, 4, 4, 1)), mstype.float32)
>>> size = Tensor([2, 2], mstype.int32)
>>> resize = ops.ResizeNearestNeighborV2()
>>> output = resize(input_tensor, size)
>>> print(output)
[[[[1.]
   [1.]]
  [[1.]
   [1.]]]]
>>> print(output.shape)
(1, 2, 2, 1)