Skip to content
fn

interpolate

→Tensor
interpolate(input: Tensor, size: int | tuple[int, ...] | None = None, scale_factor: float | tuple[float, ...] | None = None, mode: str = 'nearest', align_corners: bool | None = None, recompute_scale_factor: bool | None = None, antialias: bool = False)

Resample an N-D tensor to a target spatial size or scale factor.

Supports the standard family of image / volume resampling kernels. The leading two axes (N batch and C channels) are preserved; only the trailing spatial axes are resampled.

Parameters

inputTensor
Input of shape (N, C, *spatial). "nearest", "nearest-exact" and "area" take 1, 2 or 3 spatial axes; "linear" takes 1, "bilinear" / "bicubic" / "lanczos" 2 and "trilinear" 3.
sizeint or tuple of int= None
Target spatial size. Mutually exclusive with scale_factor.
scale_factorfloat or tuple of float= None
Multiplicative factor applied to each spatial axis. Output size is floor(in_size * scale_factor).
modestr= 'nearest'
One of "nearest", "nearest-exact", "linear", "bilinear", "bicubic", "lanczos", "trilinear", "area". Default "nearest".
align_cornersbool= None
Only accepted by "linear" / "bilinear" / "bicubic" / "trilinear" (and False for "lanczos"). If True, the corner pixels of the input and output are aligned and their values are preserved. If False (default), pixels are treated as 1×1 squares and the sampling grid is centred.
recompute_scale_factorbool= None
If True, the sampling step is in_size / out_size from the resolved output size rather than 1 / scale_factor. Not allowed together with size.
antialiasbool= False
For "bilinear", "bicubic" and "lanczos" (required for "lanczos"): when downsampling, widen the filter over every input sample an output sample covers, so detail finer than the output grid is averaged rather than aliased. Default False.

Returns

Tensor

Resampled tensor of shape (N, C, *out_spatial).

Raises

ValueError
For an unknown mode, a mode given the wrong number of spatial axes, both or neither of size and scale_factor, align_corners or antialias with a mode that has no use for it, or a non-positive size.
NotImplementedError
For a bool or integer input in any mode; cast a label mask to float32 and back.

Notes

Output sample dd reads the input around

xsrc={d⋅Sin−1Sout−1align_corners(d+0.5)⋅r−0.5otherwisex_{\mathrm{src}} = \begin{cases} d \cdot \frac{S_{\mathrm{in}} - 1}{S_{\mathrm{out}} - 1} & \text{align\_corners} \\ (d + 0.5) \cdot r - 0.5 & \text{otherwise} \end{cases}

where r=1/scale_factorr = 1 / \text{scale\_factor} when a scale factor is given and recompute_scale_factor is not True, and r=Sin/Soutr = S_{\mathrm{in}} / S_{\mathrm{out}} otherwise. The two differ whenever Sin⋅scale_factorS_{\mathrm{in}} \cdot \text{scale\_factor} is not a whole number. "nearest" copies sample ⌊d⋅r⌋\lfloor d \cdot r \rfloor and "nearest-exact" sample ⌊(d+0.5)⋅r⌋\lfloor (d + 0.5) \cdot r \rfloor; "area" is adaptive average pooling to the output size.

Examples

>>> import lucid
>>> from lucid.nn.functional import interpolate
>>> x = lucid.randn(1, 3, 32, 32)
>>> y = interpolate(x, scale_factor=2.0, mode="bilinear", align_corners=False)
>>> y.shape
(1, 3, 64, 64)
>>> interpolate(x, scale_factor=0.25, mode="bicubic", antialias=True).shape
(1, 3, 8, 8)

Used by 3