MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / interpolate

Function interpolate

imperative/python/megengine/functional/vision.py:467–644  ·  view source on GitHub ↗

r"""Down/up samples the input tensor to either the given size or with the given scale_factor. ``size`` can not coexist with ``scale_factor``. Args: inp: input tensor. size: the size of the output tensor. Default: None scale_factor: scaling factor of the output tensor. De

(
    inp: Tensor,
    size: Optional[Union[int, Tuple[int, int]]] = None,
    scale_factor: Optional[Union[float, Tuple[float, float]]] = None,
    mode: str = "bilinear",
    align_corners: Optional[bool] = None,
)

Source from the content-addressed store, hash-verified

465
466
467def interpolate(
468 inp: Tensor,
469 size: Optional[Union[int, Tuple[int, int]]] = None,
470 scale_factor: Optional[Union[float, Tuple[float, float]]] = None,
471 mode: str = "bilinear",
472 align_corners: Optional[bool] = None,
473) -> Tensor:
474 r"""Down/up samples the input tensor to either the given size or with the given scale_factor. ``size`` can not coexist with ``scale_factor``.
475
476 Args:
477 inp: input tensor.
478 size: the size of the output tensor. Default: None
479 scale_factor: scaling factor of the output tensor. Default: None
480 mode: interpolation methods, acceptable values are:
481 "bilinear", "linear", "trilinear", "bicubic" and "nearest". Default: "bilinear"
482 "trilinear" is valid only when inp is a 5D-tensor
483 align_corners: This only has an effect when ``mode``
484 is "bilinear" or "linear". Geometrically, we consider the pixels of the input
485 and output as squares rather than points. If set to ``True``, the input
486 and output tensors are aligned by the center points of their corner
487 pixels, preserving the values at the corner pixels. If set to ``False``,
488 the input and output tensors are aligned by the corner points of their
489 corner pixels, and the interpolation uses edge value padding for
490 out-of-boundary values, making this operation *independent* of input size
491
492 Returns:
493 output tensor
494
495 Examples:
496 >>> import numpy as np
497 >>> x = Tensor(np.arange(1, 5, dtype=np.float32).reshape(1, 1, 2, 2))
498 >>> out = F.vision.interpolate(x, [4, 4], align_corners=False)
499 >>> out.numpy()
500 array([[[[1. , 1.25, 1.75, 2. ],
501 [1.5 , 1.75, 2.25, 2.5 ],
502 [2.5 , 2.75, 3.25, 3.5 ],
503 [3. , 3.25, 3.75, 4. ]]]], dtype=float32)
504 >>> out2 = F.vision.interpolate(x, scale_factor=2.)
505 >>> np.testing.assert_allclose(out.numpy(), out2.numpy())
506 """
507 mode = mode.lower()
508 if mode not in ["bilinear", "linear", "trilinear", "bicubic", "nearest"]:
509 raise ValueError("unsupported interpolate mode: {}".format(mode))
510 if mode not in ["bilinear", "linear", "trilinear"]:
511 if align_corners is not None:
512 raise ValueError(
513 "align_corners option can only be set in the bilinear/linear interpolating mode"
514 )
515 else:
516 if align_corners is None:
517 align_corners = False
518
519 if mode == "linear":
520 inp = expand_dims(inp, 3)
521
522 if mode == "trilinear":
523 assert (
524 inp.ndim == 5

Callers

nothing calls this directly

Calls 12

get_dsizeFunction · 0.85
astensor1dFunction · 0.85
warp_perspectiveFunction · 0.85
expand_dimsFunction · 0.70
concatFunction · 0.70
broadcast_toFunction · 0.70
reshapeFunction · 0.70
applyFunction · 0.50
TensorClass · 0.50
formatMethod · 0.45
astypeMethod · 0.45
reshapeMethod · 0.45

Tested by

no test coverage detected