megengine.functional.nn.remap

remap(inp, map_xy, border_mode='replicate', scalar=0.0, interp_mode='linear')[source]

Applies remap transformation to batched 2D images. Remap is an operation that relocates pixels in a image to another location in a new image.

The input images are transformed to the output images by the tensor map_xy. The output’s H and W are same as map_xy’s H and W.

Parameters
  • inp (Tensor) – input image, its shape represents [b, c, in_h, in_w].

  • map_xy (Tensor) – transformation matrix, its shape shoule be [b, o_h, o_w, 2]. The shape of output is determined by o_h and o_w. For each element in output, its value is determined by inp and map_xy. map_xy[..., 0] and map_xy[..., 1] are the positions of the current element in inp, respectively. Therefore, their ranges are [0, in_w - 1] and [0, in_h - 1].

  • border_mode (str) – pixel extrapolation method. Default: “replicate”. Currently also support “constant”, “reflect”, “reflect_101”, “wrap”. “replicate”: repeatedly fills the edge pixel values of the duplicate image, expanding the new boundary pixel values with the edge pixel values. “constant”: fills the edges of the image with a fixed numeric value.

  • scalar (float) – value used in case of a constant border. Default: 0

  • interp_mode (str) – interpolation methods. Default: “linear”. Currently also support “nearest” mode.

Return type

Tensor

Returns

output tensor. [b, c, o_h, o_w]

Examples

>>> import numpy as np
>>> inp_shape = (1, 1, 4, 4)
>>> inp = Tensor(np.arange(16, dtype=np.float32).reshape(inp_shape))
>>> map_xy_shape = (1, 2, 2, 2)
>>> map_xy = Tensor(np.array([[[1., 0.],[0., 1.]],
...                     [[0., 1.],[0., 1.]]],
...                     dtype=np.float32).reshape(map_xy_shape))
>>> out = F.vision.remap(inp, map_xy)
>>> out.numpy()
array([[[[1., 4.],
         [4., 4.]]]], dtype=float32)