vit_neck
sleap_nn.architectures.vit_neck
¶
Feature-pyramid neck that lets a plain (isotropic) ViT feed spatial heads.
A plain ViT -- DINOv2/v3, CLIP, SigLIP, MAE, ... -- emits every layer at a single
stride (its patch size). The sleap-nn decoder needs a multi-scale pyramid to
upsample through, so this neck turns one or more single-stride token maps into
four levels at strides s/4, s/2, s, 2s (s = the token stride), in the
style of ViTDet's Simple Feature Pyramid (Li et al. 2022) and DPT's reassemble
stage (Ranftl et al. 2021):
"dpt"-- one tapped transformer layer per level, shallowest to the finest level (the default: intermediate layers keep more spatial detail)."sfp"-- every level is built from the last tapped layer alone (ViTDet).
The pyramid then feeds the existing
:class:~sleap_nn.architectures.encoder_decoder.Decoder exactly like the stage
outputs of a hierarchical backbone, so heads need no changes.
Classes:
| Name | Description |
|---|---|
DetailStem |
Light convolutional stream giving a plain ViT high-resolution skips. |
LayerNorm2d |
LayerNorm over the channels of an NCHW map (torchvision's, as ConvNeXt uses). |
ViTPyramidNeck |
Build a feature pyramid from single-stride ViT feature maps. |
DetailStem
¶
Bases: Module
Light convolutional stream giving a plain ViT high-resolution skips.
A plain ViT's finest real features sit at its token stride (16), so the pyramid levels the neck synthesizes below it carry no image detail of their own. This stem (in the spirit of ViTMatte's detail-capture stream, Yao et al. 2024) reads the image directly and emits features at strides 2, 4 and 8, which are concatenated with the neck levels at the same stride (or added as new, finer levels) before the decoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels
|
int
|
Input image channels. |
3
|
channels
|
List[int]
|
Output channels at strides 2, 4, 8 (in that order). |
(32, 64, 128)
|
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the stem. |
forward |
Return features at strides 2, 4, 8 (finest first). |
Source code in sleap_nn/architectures/vit_neck.py
__init__(in_channels=3, channels=(32, 64, 128))
¶
Initialize the stem.
Source code in sleap_nn/architectures/vit_neck.py
forward(x)
¶
Return features at strides 2, 4, 8 (finest first).
LayerNorm2d
¶
Bases: LayerNorm2d
LayerNorm over the channels of an NCHW map (torchvision's, as ConvNeXt uses).
Checkpoints saved when this was a wrapper around an inner nn.LayerNorm
store its parameters as <prefix>norm.weight / norm.bias; they are
renamed on load so those models keep loading.
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the norm. |
Source code in sleap_nn/architectures/vit_neck.py
ViTPyramidNeck
¶
Bases: Module
Build a feature pyramid from single-stride ViT feature maps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels
|
int
|
Channel count of the ViT feature maps (its hidden size). |
required |
out_channels
|
List[int]
|
Channels of each output level, finest first (one per scale). |
required |
num_inputs
|
int
|
Number of tapped layers fed to |
4
|
mode
|
str
|
|
'dpt'
|
scales
|
Sequence[float]
|
The levels to build, as scale factors of the token grid, finest
first -- a suffix of |
PYRAMID_SCALES
|
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the neck. |
forward |
Map tapped token maps to the pyramid, finest level first. |
Source code in sleap_nn/architectures/vit_neck.py
__init__(in_channels, out_channels, num_inputs=4, mode='dpt', scales=PYRAMID_SCALES)
¶
Initialize the neck.
Source code in sleap_nn/architectures/vit_neck.py
forward(feats)
¶
Map tapped token maps to the pyramid, finest level first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feats
|
List[Tensor]
|
Tapped ViT feature maps |
required |
Returns:
| Type | Description |
|---|---|
List[Tensor]
|
One map per level, at relative scales |