Shortcuts

torch.nn.functional.affine_grid

torch.nn.functional.affine_grid(theta, size, align_corners=None)[source]

Generates a 2D or 3D flow field (sampling grid), given a batch of affine matrices theta.

Note

This function is often used in conjunction with grid_sample() to build Spatial Transformer Networks .

Parameters
  • theta (Tensor) – input batch of affine matrices with shape (N×2×3N \times 2 \times 3) for 2D or (N×3×4N \times 3 \times 4) for 3D

  • size (torch.Size) – the target output image size. (N×C×H×WN \times C \times H \times W for 2D or N×C×D×H×WN \times C \times D \times H \times W for 3D) Example: torch.Size((32, 3, 24, 24))

  • align_corners (bool, optional) – if True, consider -1 and 1 to refer to the centers of the corner pixels rather than the image corners. Refer to grid_sample() for a more complete description. A grid generated by affine_grid() should be passed to grid_sample() with the same setting for this option. Default: False

Returns

output Tensor of size (N×H×W×2N \times H \times W \times 2)

Return type

output (Tensor)

Warning

When align_corners = True, the grid positions depend on the pixel size relative to the input image size, and so the locations sampled by grid_sample() will differ for the same input given at different resolutions (that is, after being upsampled or downsampled). The default behavior up to version 1.2.0 was align_corners = True. Since then, the default behavior has been changed to align_corners = False, in order to bring it in line with the default for interpolate().

Warning

When align_corners = True, 2D affine transforms on 1D data and 3D affine transforms on 2D data (that is, when one of the spatial dimensions has unit size) are ill-defined, and not an intended use case. This is not a problem when align_corners = False. Up to version 1.2.0, all grid points along a unit dimension were considered arbitrarily to be at -1. From version 1.3.0, under align_corners = True all grid points along a unit dimension are considered to be at 0 (the center of the input image).

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources