mindspore.ops.interpolate

View Source On Gitee
mindspore.ops.interpolate(input, size=None, scale_factor=None, mode='nearest', align_corners=None, recompute_scale_factor=None)[source]

Samples the input Tensor to the given size or scale_factor by using one of the interpolate algorithms.

Parameters
  • input (Tensor) – Tensor to be resized. Input tensor must be a 3-D, 4-D, or 5-D tensor with shape \((N, C, [optional D], [optional H], W)\) , with data type of float.

  • size (Union[int, tuple[int], list[int]], optional) – The target size. If size is a tuple or list, its length should be the same as the number of dimensions in input after removing the first two dimensions N, C. One and only one of size and scale_factor can be set to None. Default: None .

  • scale_factor (Union[float, tuple[float], list[float]], optional) – The scale factor of new size of the tensor. If scale_factor is a tuple or list, its length should be the same as the number of dimensions in input after removing the first two dimensions N, C. One and only one of size and scale_factor can be set to None. Default: None .

  • mode (str) – The sampling algorithm. One of ‘nearest’, ‘linear’ (3D only), ‘bilinear’ (4D only), ‘trilinear’ (5D only), ‘bicubic’ (4D only), ‘area’, ‘nearest-exact’(matches Scikit-Image and PIL nearest neighbours interpolation algorithms and fixes knows issues with nearest, 3D and 4D). Default: "nearest" .

  • align_corners (bool) –

    Whether to use corner alignment for coordinate mapping. Assuming a transformation is applied to the input Tensor along the x-axis, the specific calculation formula is as follows:

    ori_i = new_length != 1 ? new_i * (ori_length - 1) / (new_length - 1) : 0   # 'align_corners' = True
    
    ori_i = new_length > 1 ? (new_i + 0.5) * ori_length / new_length - 0.5 : 0  # 'align_corners' = False
    

    Among them, \(ori\_length\) and \(new\_length\) represent the length of the Tensor before and after transformation along the x-axis respectively; \(new\_i\) represents the coordinate of the i-th element along the x-axis after transformation; \(ori\_i\) represents the corresponding coordinate of the original data along the x-axis.

    This is only valid for 'linear', 'bilinear', or 'bicubic' modes. Default: False .

  • recompute_scale_factor (bool, optional) – Recalculate scale_factor. If True, the parameter size will be calculated using the value of the scale_factor, and finally scaled using the value of size. If False, the value of size or scale_factor will be used for direct interpolation. Default: None .

Note

The ‘nearest-exact’ mode is the same as the nearest-neighbor interpolation algorithm used in scikit-image and PIL. The ‘nearest’ mode produces the same results as the INTER_NEAREST interpolation algorithm used in OpenCV.

Args Support List and Supported Platforms:

mode

input.dim

align_corners

scale_factor

device

nearest

3

-

×

Ascend,GPU,CPU

4

-

×

Ascend,GPU,CPU

5

-

Ascend,GPU,CPU

linear

3

×

Ascend,GPU,CPU

bilinear

4

×

Ascend,GPU,CPU

bicubic

4

×

Ascend,GPU,CPU

area

3

-

Ascend,GPU,CPU

4

-

Ascend,GPU,CPU

5

-

Ascend,GPU,CPU

nearest-exact

3

-

×

Ascend,CPU

4

-

×

Ascend,CPU

trilinear

5

Ascend,GPU,CPU

  • - indicates that there is no such parameter.

  • × indicates that this parameter is not currently supported.

  • indicates that this parameter is supported.

Returns

Tensor, resized, whose dimensions and dtype are the same as input.

Raises
  • TypeErrorinput is not a Tensor.

  • ValueError – Both size and scale_factor are not empty.

  • ValueError – Both size and scale_factor are empty.

  • ValueError – When size is a tuple or list, its length is not equal to input.ndim - 2.

  • ValueError – When scale_factor is a tuple or list, its length is not equal to input.ndim - 2.

  • ValueErrormode is not in the list of supported modes.

  • ValueErrorinput.ndim is not in the list of supported dimensions for the corresponding mode.

  • ValueErrorsize is not empty, recompute_scale_factor is not empty.

  • ValueErrorscale_factor is not in the corresponding list of supported values.

  • ValueErroralign_corners is not in the corresponding list of supported values.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> from mindspore import Tensor, ops
>>> input = Tensor([[[1, 2, 3], [4, 5, 6]]], mindspore.float32)
>>> output = ops.interpolate(input, size=(6,), mode='nearest')
>>> print(output)
    [[[1. 1. 2. 2. 3. 3.]
      [4. 4. 5. 5. 6. 6.]]]