class

Upsample

extendsModule
Upsample(size: int | tuple[int, ...] | None = None, scale_factor: float | tuple[float, ...] | None = None, mode: str = 'nearest', align_corners: bool | None = None)
source

Upsample an input tensor to a given spatial size or scale factor.

Upsample resizes the spatial dimensions of an input tensor using one of several interpolation algorithms. The batch and channel dimensions are never modified; only the trailing spatial axes are scaled.

Exactly one of size or scale_factor must be provided.

Nearest-neighbour (mode='nearest'):

y[i]=x ⁣[iscale]y[i] = x\!\left[\left\lfloor \frac{i}{\text{scale}} \right\rfloor\right]

Each output element copies the value of the nearest input element.

Bilinear (mode='bilinear', 2-D only):

y[i,j]=(1th)(1tw)x[i0,j0]+th(1tw)x[i1,j0]+(1th)twx[i0,j1]+thtwx[i1,j1]y[i, j] = (1 - t_h)(1 - t_w)\, x[i_0, j_0] + t_h(1 - t_w)\, x[i_1, j_0] + (1 - t_h) t_w\, x[i_0, j_1] + t_h t_w\, x[i_1, j_1]

where i0,i1i_0, i_1 are the two nearest row indices in the input and tht_h is the fractional offset between them (analogously for j0,j1,twj_0, j_1, t_w).

Parameters

sizeint or tuple[int, ...] or None= None
Output spatial size. Pass a single int to set all spatial dimensions to the same value, or a tuple matching the number of spatial dimensions. Mutually exclusive with scale_factor.
scale_factorfloat or tuple[float, ...] or None= None
Multiplier for each spatial dimension. Values > 1 upsample; values < 1 downsample. Mutually exclusive with size.
modestr= 'nearest'
Interpolation algorithm. One of:
  • 'nearest' — no learnable parameters, fastest (default).
  • 'linear' — 1-D linear interpolation (3-D input).
  • 'bilinear' — 2-D bilinear interpolation (4-D input).
  • 'bicubic' — 2-D bicubic interpolation (4-D input).
  • 'trilinear' — 3-D trilinear interpolation (5-D input).
align_cornersbool or None= None
When True, the corner pixels of input and output are aligned, meaning the interpolation grid spans exactly from the first to the last pixel centre. When False (or None), the grid is scaled by the ratio of sizes, which may shift the grid by half a pixel relative to the corners. Ignored for 'nearest' mode.

Attributes

sizeint or tuple[int, ...] or None
Stored output size.
scale_factorfloat or tuple[float, ...] or None
Stored scale factor.
modestr
Stored interpolation mode.
align_cornersbool or None
Stored corner-alignment flag.

Notes

  • Input: (N,C,d1,d2,,dk)(N, C, d_1, d_2, \dots, d_k).
  • Output: (N,C,d1,d2,,dk)(N, C, d_1', d_2', \dots, d_k') where each did_i' is either given by size or di=discale_factord_i' = \lfloor d_i \cdot \text{scale\_factor} \rfloor.
  • The align_corners flag has a significant effect on output values for bilinear / bicubic / trilinear modes. When True, corner pixels map exactly onto each other; when False the mapping preserves the overall scale but shifts by 0.5 input pixels at the boundary.
  • For 4-D inputs with mode='nearest' this module is equivalent to UpsamplingNearest2d; for mode='bilinear' with align_corners=True it is equivalent to UpsamplingBilinear2d.
  • Delegates to nn.functional.interpolate.

Examples

**2× nearest-neighbour upsampling of a feature map:**
>>> import lucid
>>> import lucid.nn as nn
>>>
>>> up = nn.Upsample(scale_factor=2, mode="nearest")
>>> x = lucid.zeros(1, 16, 14, 14)
>>> up(x).shape
(1, 16, 28, 28)
**Fixed output size with bilinear interpolation:**
>>> up = nn.Upsample(size=(256, 256), mode="bilinear", align_corners=False)
>>> x = lucid.zeros(4, 3, 64, 64)
>>> up(x).shape
(4, 3, 256, 256)
**Trilinear upsampling for 3-D volumetric data:**
>>> up_3d = nn.Upsample(scale_factor=2, mode="trilinear", align_corners=True)
>>> x = lucid.zeros(2, 8, 16, 16, 16)
>>> up_3d(x).shape
(2, 8, 32, 32, 32)

Methods (3)

dunder

__init__

None
__init__(size: int | tuple[int, ...] | None = None, scale_factor: float | tuple[float, ...] | None = None, mode: str = 'nearest', align_corners: bool | None = None)
source

Initialise the Upsample module. See the class docstring for parameter semantics.

fn

forward

Tensor
forward(x: Tensor)
source

Upsample the input tensor.

Parameters

inputTensor
Input tensor of shape (N,C,)(N, C, *).

Returns

Tensor

Upsampled output tensor.

fn

extra_repr

str
extra_repr()
source

Return a string representation of the layer's configuration.