class

MaxUnpool2d

extends_MaxUnpoolNd
MaxUnpool2d(kernel_size: int | tuple[int, ...], stride: int | tuple[int, ...] | None = None, padding: int | tuple[int, ...] = 0)
source

Computes a partial inverse of MaxPool2d.

Reconstructs a sparse approximation of the original 2-D feature map by scattering each pooled value back to the spatial position where the maximum was found, with all other positions set to zero.

For a pooled value at output cell (h,w)(h, w) with flat spatial index idx[n,c,h,w]\text{idx}[n,c,h,w]:

xunpool[n,c,i,j]={xpool[n,c,h,w]if (i,j) maps to idx[n,c,h,w]0otherwisex_{\text{unpool}}[n,c,i,j] = \begin{cases} x_{\text{pool}}[n,c,h,w] & \text{if } (i,j) \text{ maps to } \text{idx}[n,c,h,w] \\ 0 & \text{otherwise} \end{cases}

Parameters

kernel_sizeint or tuple[int, int]
Must match the kernel_size of the paired MaxPool2d.
strideint or tuple[int, int] or None= None
Must match the stride of the paired pool. Defaults to kernel_size.
paddingint or tuple[int, int]= 0
Must match the padding of the paired pool. Default: 0.

Attributes

kernel_sizeint or tuple[int, int]
strideint or tuple[int, int]
paddingint or tuple[int, int]

Notes

  • Input x: (N, C, H_pooled, W_pooled)
  • Input indices: (N, C, H_pooled, W_pooled)
  • Output: (N, C, H_original, W_original)
  • Originally introduced as part of the SegNet encoder-decoder architecture, where max-pooling indices are passed across the skip connection to guide unpooling in the decoder.
  • The reconstructed tensor is not a true inverse — non-maximum values from the original pooling input are permanently lost.
  • An optional output_size parameter resolves ambiguity when the original input size cannot be uniquely recovered from stride and kernel_size alone.

Examples

Symmetric encoder-decoder pair:
>>> import lucid
>>> import lucid.nn as nn
>>> unpool = nn.MaxUnpool2d(kernel_size=2, stride=2)
>>> x = lucid.ones((1, 64, 14, 14))
>>> indices = lucid.zeros((1, 64, 14, 14), dtype="int32")
>>> y = unpool(x, indices)
>>> y.shape
(1, 64, 28, 28)
With explicit output_size to handle odd input dimensions:
>>> unpool = nn.MaxUnpool2d(kernel_size=2, stride=2)
>>> x = lucid.ones((1, 16, 7, 7))
>>> indices = lucid.zeros((1, 16, 7, 7), dtype="int32")
>>> y = unpool(x, indices, output_size=(1, 16, 15, 15))
>>> y.shape
(1, 16, 15, 15)

Methods (1)

fn

forward

Tensor
forward(x: Tensor, indices: Tensor, output_size: tuple[int, ...] | None = None)
source

Apply the pooling operation to the input tensor.

Parameters

inputTensor
Input tensor of shape (N,C,)(N, C, *) where * are the spatial dimensions appropriate for this pooling layer.

Returns

Tensor

Pooled output tensor.