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 \times 2 \times 3$) for 2D or ($N \times 3 \times 4$) for 3D
size (torch.Size) – the target output image size. ($N \times C \times H \times W$ for 2D or $N \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
and1
to refer to the centers of the corner pixels rather than the image corners. Refer togrid_sample()
for a more complete description. A grid generated byaffine_grid()
should be passed togrid_sample()
with the same setting for this option. Default:False
- Returns
output Tensor of size ($N \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 bygrid_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 wasalign_corners = True
. Since then, the default behavior has been changed toalign_corners = False
, in order to bring it in line with the default forinterpolate()
.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 whenalign_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, underalign_corners = True
all grid points along a unit dimension are considered to be at0
(the center of the input image).