class
PixelShuffle
extends
ModulePixelShuffle(upscale_factor: int)Rearrange channel-stacked sub-pixel planes into a high-resolution output.
PixelShuffle implements the sub-pixel convolution (ESPCN) operation:
given an input with channels and spatial size
, it reshapes and transposes to produce an output with
channels and spatial size .
where index the sub-pixel offset and indexes the output channel.
Parameters
upscale_factorintSpatial upscaling factor . The number of input channels
must be divisible by .
Attributes
upscale_factorintStored value of the
upscale_factor constructor argument.Notes
- Input: .
- Output: .
PixelShuffleis the core operation in the Efficient Sub-Pixel CNN (ESPCN) super-resolution architecture (Shi et al., 2016). The idea is to learn the upsampling entirely through convolution on the low-resolution grid, then rearrange the feature channels into a single high-resolution plane, which is cheaper than computing features at high resolution.- Implemented as reshape → permute → reshape on the C++ engine; no intermediate copies beyond those required by the permute.
PixelUnshuffleis the exact inverse.
Examples
**2× super-resolution decoder block:**
>>> import lucid
>>> import lucid.nn as nn
>>>
>>> r = 2
>>> # Conv outputs C*r^2 channels at LR spatial size, PixelShuffle expands
>>> block = nn.Sequential(
... nn.Conv2d(64, 3 * r * r, kernel_size=3, padding=1),
... nn.PixelShuffle(upscale_factor=r),
... )
>>> x = lucid.zeros(1, 64, 56, 56)
>>> block(x).shape
(1, 3, 112, 112)
**Verify channel / spatial trade-off:**
>>> ps = nn.PixelShuffle(upscale_factor=3)
>>> x = lucid.zeros(2, 36, 10, 10) # 36 = 4 * 3^2
>>> ps(x).shape
(2, 4, 30, 30)Methods (3)
dunder
__init__
→None__init__(upscale_factor: int)Initialise the PixelShuffle module. See the class docstring for parameter semantics.
fn
forward
→Tensorforward(x: Tensor)Upsample the input tensor.
Parameters
inputTensorInput tensor of shape .
Returns
TensorUpsampled output tensor.
fn
extra_repr
→strextra_repr()Return a string representation of the layer's configuration.