torchsample.encoding.functional module#

torchsample.encoding.functional.gamma(coords, order=10)[source]#

Positional encoding via sin and cos.

From:

NeRF: Representing Scenes as Neural Radiance Fields for View Synthesis

Parameters
  • coords (torch.Tensor) – (..., dim) Coordinates to convert to positional encoding. In range [-1, 1].

  • order (int) – Number

Returns

(..., 2*dim*order)

Return type

torch.Tensor

torchsample.encoding.functional.identity(coords)[source]#

Return coords unmodified.

torchsample.encoding.functional.nearest_pixel(coords, size, align_corners=False)[source]#

Encode normalized coords and relative offset to nearest neighbor.

Note: offsets are multiplied by 2, so that their range is [-1, 1] instead of [-0.5, 0.5]

From:

High Quality Segmentation for Ultra High-resolution Images

Example

import torch
import torchsample as ts

target = torch.rand(1, 3, 480, 640)
featmap = torch.rand(1, 256, 15, 20)
coords = ts.coord.randint(1, 4096, (640, 480))
pos_enc = ts.encoding.nearest_pixel(coords, (20, 15))
Parameters
  • coords (torch.Tensor) – (..., dim) Coordinates to convert to positional encoding. In range [-1, 1].

  • size (tuple) – Size of field to generate pixel-center offsets for. i.e. (x, y, ...).

Returns

(..., 2*dim) Normalized coordinates and nearest-pixel relative offset.

Return type

torch.Tensor