Upsample
ModuleUpsample(size: int | tuple[int, ...] | None = None, scale_factor: float | tuple[float, ...] | None = None, mode: str = 'nearest', align_corners: bool | None = None)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'):
Each output element copies the value of the nearest input element.
Bilinear (mode='bilinear', 2-D only):
where are the two nearest row indices in the input and is the fractional offset between them (analogously for ).
Parameters
sizeint or tuple[int, ...] or None= Noneint 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> 1 upsample;
values < 1 downsample. Mutually exclusive with size.modestr= 'nearest''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= NoneTrue, 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 Nonescale_factorfloat or tuple[float, ...] or Nonemodestralign_cornersbool or NoneNotes
- Input: .
- Output: where each
is either given by
sizeor .
- The
align_cornersflag has a significant effect on output values for bilinear / bicubic / trilinear modes. WhenTrue, corner pixels map exactly onto each other; whenFalsethe 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 toUpsamplingNearest2d; formode='bilinear'withalign_corners=Trueit is equivalent toUpsamplingBilinear2d. - 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)
__init__
→None__init__(size: int | tuple[int, ...] | None = None, scale_factor: float | tuple[float, ...] | None = None, mode: str = 'nearest', align_corners: bool | None = None)Initialise the Upsample module. See the class docstring for parameter semantics.
forward
→Tensorforward(x: Tensor)Upsample the input tensor.
Parameters
inputTensorReturns
TensorUpsampled output tensor.
extra_repr
→strextra_repr()Return a string representation of the layer's configuration.