mindspore.mint.nn.functional.interpolate

View Source On Gitee
mindspore.mint.nn.functional.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.

Note

  • In 'linear' mode, the scenarios, where scale_factor is not None and align_corners is False, is not supported.

  • In 'nearest' mode, there may exist precision problem in the scenarios, where input is 3-D/4-D Tensor and the image is scaled by scale_factor.

  • mode and scale_factor should be constants.

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), and 'bicubic' (4D only). 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 .

Args Support List and Supported Platforms:

mode

input.dim

align_corners

scale_factor

device

nearest

3

-

Ascend

4

-

Ascend

5

-

Ascend

linear

3

Ascend

bilinear

4

×

Ascend

bicubic

4

×

Ascend

trilinear

5

Ascend

  • - indicates that there is no such parameter.

  • × indicates that this parameter is not currently supported.

  • indicates that this parameter is supported.

Returns

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

Shape:
  • Input: \((N, C, W_{in})\), \((N, C, H_{in}, W_{in})\) or \((N, C, D_{in}, H_{in}, W_{in})\)

  • Output: \((N, C, W_{out})\), \((N, C, H_{out}, W_{out})\) or \((N, C, D_{out}, H_{out}, W_{out})\), where

\[D_{out} = \left\lfloor D_{in} \times \text{scale\_factor} \right\rfloor\]
\[H_{out} = \left\lfloor H_{in} \times \text{scale\_factor} \right\rfloor\]
\[W_{out} = \left\lfloor W_{in} \times \text{scale\_factor} \right\rfloor\]
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

Examples

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