megengine.functional.vision.interpolate

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

按照给定的 sizescale_factor 对输入张量进行向下/向上采样。size 不可以与 scale_factor 共存。

参数
  • inp – 输入张量。

  • size – 输出张量的形状。 默认:None

  • scale_factor – 输出张量的放缩参数。默认:None

  • mode – interpolation methods, acceptable values are: “bilinear”, “linear”. Default: “bilinear”

  • align_corners – 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

返回

输出张量。

例如:

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())

输出:

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