fn
affine_grid
→Tensoraffine_grid(theta: Tensor, size: list[int] | tuple[int, ...], align_corners: bool | None = None)Generate a sampling grid from a batch of affine transform matrices.
Builds the flow field needed to apply an affine transform via
grid_sample. Together they form a Spatial Transformer
Network (STN, Jaderberg et al. 2015), where theta is typically
the output of a localisation network.
For each output pixel the corresponding source coordinate is computed as:
Parameters
thetaTensorBatch of affine matrices of shape
(N, 2, 3) (2-D) or
(N, 3, 4) (3-D). Each row encodes one element of the affine
transform applied to homogeneous output coordinates.sizesequence of intTarget output shape passed to
grid_sample afterwards —
(N, C, H, W) for 2-D, (N, C, D, H, W) for 3-D.align_cornersbool= NoneMust match the
align_corners argument later passed to
grid_sample. Controls whether -1/+1 refer to
the centre of the corner pixels (True) or to the outer edge
(False, default).Returns
TensorSampling grid of shape (N, H, W, 2) (or
(N, D, H, W, 3) for 3-D) in normalised
coordinates, ready to be fed to grid_sample.
Notes
affine_grid does not look at the source image — only at the
requested output size — so the resulting grid is reusable across any
input of matching spatial dims and any channel count.
Examples
>>> import lucid
>>> from lucid.nn.functional import affine_grid
>>> theta = lucid.tensor([[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]]) # identity
>>> g = affine_grid(theta, (1, 3, 4, 4), align_corners=False)
>>> g.shape
(1, 4, 4, 2)