torch.nn.functional.grid_sample — PyTorch 2.7 documentation (original) (raw)

torch.nn.functional.grid_sample(input, grid, mode='bilinear', padding_mode='zeros', align_corners=None)[source][source]

Compute grid sample.

Given an input and a flow-field grid, computes theoutput using input values and pixel locations from grid.

Currently, only spatial (4-D) and volumetric (5-D) input are supported.

In the spatial (4-D) case, for input with shape(N,C,Hin,Win)(N, C, H_\text{in}, W_\text{in}) and grid with shape(N,Hout,Wout,2)(N, H_\text{out}, W_\text{out}, 2), the output will have shape(N,C,Hout,Wout)(N, C, H_\text{out}, W_\text{out}).

For each output location output[n, :, h, w], the size-2 vectorgrid[n, h, w] specifies input pixel locations x and y, which are used to interpolate the output value output[n, :, h, w]. In the case of 5D inputs, grid[n, d, h, w] specifies thex, y, z pixel locations for interpolatingoutput[n, :, d, h, w]. mode argument specifies nearest orbilinear interpolation method to sample the input pixels.

grid specifies the sampling pixel locations normalized by theinput spatial dimensions. Therefore, it should have most values in the range of [-1, 1]. For example, values x = -1, y = -1 is the left-top pixel of input, and values x = 1, y = 1 is the right-bottom pixel of input.

If grid has values outside the range of [-1, 1], the corresponding outputs are handled as defined by padding_mode. Options are

Note

When using the CUDA backend, this operation may induce nondeterministic behaviour in its backward pass that is not easily switched off. Please see the notes on Reproducibility for background.

Note

NaN values in grid would be interpreted as -1.

Parameters

Returns

output Tensor

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 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().

Note

mode='bicubic' is implemented using the cubic convolution algorithm with α=−0.75\alpha=-0.75. The constant α\alpha might be different from packages to packages. For example, PIL and OpenCV use -0.5 and -0.75 respectively. This algorithm may “overshoot” the range of values it’s interpolating. For example, it may produce negative values or values greater than 255 when interpolating input in [0, 255]. Clamp the results with torch.clamp() to ensure they are within the valid range.