class

UpsamplingBilinear2d

extendsUpsample
UpsamplingBilinear2d(size: int | tuple[int, int] | None = None, scale_factor: float | tuple[float, float] | None = None)
source

Bilinear upsampling for 4-D tensors (N, C, H, W) with aligned corners.

A convenience wrapper around Upsample with mode='bilinear' and align_corners=True fixed. The align_corners=True default matches the reference framework's UpsamplingBilinear2d — note that plain Upsample(mode='bilinear') defaults to align_corners=False, which gives different output values near the image boundary.

Bilinear interpolation performs 2-D linear interpolation between the four nearest input neighbours:

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

where th,tw[0,1)t_h, t_w \in [0, 1) are the fractional offsets within the input grid cell (computed with corner-aligned coordinates when align_corners=True).

Parameters

sizeint or tuple[int, int] or None= None
Target output spatial size (H_out, W_out). Mutually exclusive with scale_factor.
scale_factorfloat or tuple[float, float] or None= None
Spatial scale multiplier (s_H, s_W). Mutually exclusive with size.

Notes

  • Input: (N,C,H,W)(N, C, H, W).
  • Output: (N,C,Hout,Wout)(N, C, H_{\text{out}}, W_{\text{out}}).
  • align_corners=True means that the corner pixels of the input and output grids coincide exactly; this is the legacy behaviour used in many pre-trained segmentation and super-resolution models.
  • When precise sub-pixel alignment matters (e.g. image restoration), prefer Upsample(mode='bilinear', align_corners=False) which avoids the half-pixel offset at image boundaries.

Examples

**4× bilinear upsampling with corner alignment:**
>>> import lucid
>>> import lucid.nn as nn
>>>
>>> up = nn.UpsamplingBilinear2d(scale_factor=4)
>>> x = lucid.zeros(1, 64, 8, 8)
>>> up(x).shape
(1, 64, 32, 32)
**Upsample to a fixed output size:**
>>> up = nn.UpsamplingBilinear2d(size=(512, 512))
>>> x = lucid.zeros(2, 3, 128, 128)
>>> up(x).shape
(2, 3, 512, 512)

Methods (1)

dunder

__init__

None
__init__(size: int | tuple[int, int] | None = None, scale_factor: float | tuple[float, float] | None = None)
source

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