drtk.grid_scatter#

grid_scatter

Scatter an image through a normalized sampling grid.

grid_scatter_ref

Pure PyTorch reference implementation used by tests.

drtk.grid_scatter(input, grid, output_height, output_width, mode='bilinear', padding_mode='border', align_corners=None)[source]#

Scatter an image through a normalized sampling grid.

grid_scatter is the splatting counterpart of torch.nn.functional.grid_sample(). In grid_sample, each output pixel reads from a source location in the input. In grid_scatter, each input pixel writes its value to a destination location described by grid. The operation is useful for tasks such as projecting camera-view values into a UV texture atlas or accumulating visibility weights in texture space.

The forward pass of grid_scatter corresponds to the input-gradient accumulation performed by grid_sample. The backward pass correspondingly samples gradients back from the output grid, while also producing gradients with respect to grid.

Parameters:
  • input – Source values with shape (N, C, H, W).

  • grid – Destination coordinates with shape (N, H, W, 2). Coordinates use the same normalized [-1, 1] convention, padding modes, and align_corners semantics as PyTorch grid_sample.

  • output_height – Height of the scattered output image.

  • output_width – Width of the scattered output image.

  • mode – Interpolation kernel. Supported values are "bilinear" and "bicubic".

  • padding_mode – Handling for samples outside the output image. Supported values are "zeros", "border", and "reflection".

  • align_corners – Same meaning as in grid_sample. None defaults to False.

Returns:

Tensor with shape (N, C, output_height, output_width) containing the accumulated scattered values.

Note

Multiple input pixels can scatter to the same output pixel; their weighted contributions are accumulated. This operation currently requires the accelerator backend.

drtk.grid_scatter_ref(input, grid, output_height, output_width, mode='bilinear', padding_mode='border', align_corners=None)[source]#

Pure PyTorch reference implementation used by tests.

This helper is intentionally not part of the documented public API. See drtk.grid_scatter() for the supported implementation.