megengine.functional.nn.interpolate

interpolate(inp, size=None, scale_factor=None, mode='bilinear', align_corners=None)[源代码]

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

参数
  • inp (Tensor) – input tensor.

  • size (Union[int, Tuple[int, int], None]) – 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

返回类型

Tensor

返回

output tensor.

实际案例

import numpy as np
from megengine import tensor
import megengine.functional as F

x = tensor(np.arange(1, 5, dtype=np.float32).reshape(1, 1, 2, 2))
out = F.vision.interpolate(x, [4, 4], align_corners=False)
print(out.numpy())
out2 = F.vision.interpolate(x, scale_factor=2.)
np.testing.assert_allclose(out.numpy(), out2.numpy())

Outputs:

[[[[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.  ]]]]