tile_merger
sleap_nn.inference.tile_merger
¶
Torch-native tiled inference: importance windows + accumulate/normalize canvas.
This module provides the two primitives needed to run a fixed-input-size model over an image that is larger than the model's receptive field by splitting it into overlapping tiles and stitching the per-tile predictions back into a single full-frame output.
- :func:
build_importance_window— a separable per-axis weighting window for one tile that de-emphasizes the tile borders (where predictions are least reliable). This is a pure-torch port of MONAI's gaussian importance map, plus a"pyramid"(triangular) window ported from pytorch-toolbelt and a"constant"(uniform) fallback. - :class:
TileMerger— a per-frame accumulate-and-normalize canvas (a torch-only port of pytorch-toolbelt'sTileMerger). Each tile is added into a weighted accumulatorACCand its window into a weight counterCNT; the final merge is the elementwiseACC / CNT, i.e. a per-pixel weighted average of every tile that covered it. With sum-of-weights normalization a uniform field stitches back to itself everywhere it is covered, independent of the window.
All coordinates and windows are expressed in output-stride pixels (the
resolution of the model's output canvas), not input pixels. No dependencies
beyond torch.
Classes:
| Name | Description |
|---|---|
TileMerger |
Per-frame accumulate-and-normalize canvas at output-stride resolution. |
Functions:
| Name | Description |
|---|---|
build_importance_window |
Build a separable per-axis importance window for one tile. |
TileMerger
¶
Per-frame accumulate-and-normalize canvas at output-stride resolution.
Each integrated tile is added into a weighted accumulator ACC and its
window into a weight counter CNT. The final merge divides ACC by
CNT elementwise, yielding a per-pixel weighted average over every tile
that covered that pixel.
Attributes:
| Name | Type | Description |
|---|---|---|
w |
The importance window, shape |
|
acc |
Weighted accumulator, shape |
|
cnt |
Weight counter, shape |
All coordinates (y0, x0) and the window are in output-stride pixels.
Accumulation is done in dtype (float32 by default) even when tiles are
passed in at lower precision (e.g. fp16).
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the accumulator canvas. |
integrate |
Accumulate one tile at output-stride origin |
merge |
Normalize the accumulator by the weight counter. |
Source code in sleap_nn/inference/tile_merger.py
__init__(out_hw, channels, window, device='cpu', dtype=torch.float32)
¶
Initialize the accumulator canvas.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
out_hw
|
Tuple[int, int]
|
|
required |
channels
|
int
|
Number of output channels to accumulate. |
required |
window
|
Tensor
|
Importance window of shape |
required |
device
|
Union[str, device]
|
Device to hold the accumulators on. |
'cpu'
|
dtype
|
dtype
|
Accumulation dtype (float32 recommended). |
float32
|
Source code in sleap_nn/inference/tile_merger.py
integrate(tile, y0, x0)
¶
Accumulate one tile at output-stride origin (y0, x0).
The tile is moved to the accumulator device/dtype before accumulation, so fp16 tiles are accumulated in float32. If the tile is partial (clipped by the canvas edge), the window is cropped to match.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tile
|
Tensor
|
Tile of shape |
required |
y0
|
int
|
Top row of the tile within the canvas, in output-stride pixels. |
required |
x0
|
int
|
Left column of the tile within the canvas, in output-stride pixels. |
required |
Source code in sleap_nn/inference/tile_merger.py
merge(eps=None)
¶
Normalize the accumulator by the weight counter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
eps
|
Optional[float]
|
If given, clamp the counter to at least |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
The merged output of shape |
Source code in sleap_nn/inference/tile_merger.py
build_importance_window(tile_hw, mode='gaussian', sigma_scale=0.125, device='cpu', dtype=torch.float32)
¶
Build a separable per-axis importance window for one tile.
The window weights each pixel of a tile by how far it is from the tile border, so that overlapping tiles contribute most where they are most reliable (near their center) and least at their edges. It is built once per tile size and reused across every tile/frame.
The window is not sum-normalized — normalization happens at merge time
via the ACC/CNT accumulators in :class:TileMerger.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tile_hw
|
Tuple[int, int]
|
|
required |
mode
|
str
|
One of
|
'gaussian'
|
sigma_scale
|
float
|
Gaussian std as a fraction of each axis length. Only used
for |
0.125
|
device
|
Union[str, device]
|
Device to build the window on. |
'cpu'
|
dtype
|
dtype
|
Output dtype of the returned window. |
float32
|
Returns:
| Type | Description |
|---|---|
Tensor
|
A |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |