megengine.functional.nn.interpolate#

interpolate(inp, size=None, scale_factor=None, mode='bilinear', align_corners=None)[source]#

Down/up samples the input tensor to either the given size or with the given scale_factor. size can not coexist with scale_factor.

Parameters:
  • inp (Tensor) – input tensor.

  • size (Union[int, Tuple[int, int], None]) – the size of the output tensor. Default: None

  • scale_factor (Union[float, Tuple[float, float], None]) – scaling factor of the output tensor. Default: None

  • mode (str) – interpolation methods, acceptable values are: “bilinear”, “linear”, “bicubic” and “nearest”. Default: “bilinear”

  • align_corners (Optional[bool]) – This only has an effect when mode is “bilinear” or “linear”. Geometrically, we consider the pixels of the input and output as squares rather than points. If set to True, the input and output tensors are aligned by the center points of their corner pixels, preserving the values at the corner pixels. If set to False, the input and output tensors are aligned by the corner points of their corner pixels, and the interpolation uses edge value padding for out-of-boundary values, making this operation independent of input size

Return type:

Tensor

Returns:

output tensor

Examples

>>> import numpy as np
>>> x = Tensor(np.arange(1, 5, dtype=np.float32).reshape(1, 1, 2, 2))
>>> out = F.vision.interpolate(x, [4, 4], align_corners=False)
>>> out.numpy()
array([[[[1.  , 1.25, 1.75, 2.  ],
         [1.5 , 1.75, 2.25, 2.5 ],
         [2.5 , 2.75, 3.25, 3.5 ],
         [3.  , 3.25, 3.75, 4.  ]]]], dtype=float32)
>>> out2 = F.vision.interpolate(x, scale_factor=2.)
>>> np.testing.assert_allclose(out.numpy(), out2.numpy())