class
Upsample
extends
ModuleUpsample(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)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= NoneOutput 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= NoneMultiplier 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).'nearest-exact'— nearest with half-pixel centres.'area'— adaptive average pooling to the output size.
align_cornersbool or None= NoneWhen
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. Only accepted by the
interpolating modes ('linear', 'bilinear', 'bicubic',
'trilinear').recompute_scale_factorbool or None= NoneWhen
True, sample positions step by in / out of the
resolved output size instead of 1 / scale_factor; the two
differ when in * scale_factor is not a whole number. Not
allowed with size.Attributes
sizeint or tuple[int, ...] or NoneStored output size.
scale_factorfloat or tuple[float, ...] or NoneStored scale factor.
modestrStored interpolation mode.
align_cornersbool or NoneStored corner-alignment flag.
recompute_scale_factorbool or NoneStored flag for how a scale factor maps sample positions.
Notes
- 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)Used by 1
Constructors
1dunder
__init__
→None__init__(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)Initialise the Upsample module. See the class docstring for parameter semantics.