heads
sleap_nn.architectures.heads
¶
Model head definitions for defining model output types.
Classes:
| Name | Description |
|---|---|
CenterOffsetHead |
Head for predicting per-pixel offset vectors to instance centers. |
CenteredInstanceConfmapsHead |
Head for specifying centered instance confidence maps. |
CentroidConfmapsHead |
Head for specifying instance centroid confidence maps. |
ClassMapsHead |
Head for specifying class identity maps. |
ClassVectorsHead |
Head for specifying classification heads. |
EmbeddingHead |
Head for crop -> embedding-vector (re-ID) models. |
GeM |
Generalized-mean pooling: |
Head |
Base class for model output heads. |
InstanceCenterHead |
Head for predicting instance center heatmaps. |
L2Norm |
L2-normalize along |
MultiInstanceConfmapsHead |
Head for specifying multi-instance confidence maps. |
OffsetRefinementHead |
Head for specifying offset refinement maps. |
PartAffinityFieldsHead |
Head for specifying multi-instance part affinity fields. |
SegmentationHead |
Head for predicting binary foreground segmentation masks. |
SingleInstanceConfmapsHead |
Head for specifying single instance confidence maps. |
CenterOffsetHead
¶
Bases: Head
Head for predicting per-pixel offset vectors to instance centers.
Outputs a 2-channel map where each pixel's value is (dx, dy) pointing from the pixel to its instance's center. Only meaningful on foreground pixels.
Attributes:
| Name | Type | Description |
|---|---|---|
output_stride |
Stride of the output head tensor. |
|
loss_weight |
Weight of the loss term for this head during optimization. |
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the object with the specified attributes. |
Source code in sleap_nn/architectures/heads.py
CenteredInstanceConfmapsHead
¶
Bases: Head
Head for specifying centered instance confidence maps.
Attributes:
| Name | Type | Description |
|---|---|---|
part_names |
List of strings specifying the part names associated with channels. |
|
anchor_part |
Name of the part to use as an anchor node. If not specified, the bounding box centroid will be used. |
|
centroid_method |
How the centroid is derived from the instance's points --
|
|
centroid_fallback |
Reduce method used when |
|
sigma |
Spread of the confidence maps. |
|
output_stride |
Stride of the output head tensor. The input tensor is expected to be at the same stride. |
|
loss_weight |
Weight of the loss term for this head during optimization. |
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the object with the specified attributes. |
from_config |
Create this head from a set of configurations. |
Source code in sleap_nn/architectures/heads.py
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | |
channels
property
¶
Return the number of channels in the tensor output by this head.
__init__(part_names, anchor_part=None, centroid_method=None, centroid_fallback=None, sigma=5.0, output_stride=1, loss_weight=1.0)
¶
Initialize the object with the specified attributes.
Source code in sleap_nn/architectures/heads.py
from_config(config, part_names=None)
classmethod
¶
Create this head from a set of configurations.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
A |
|
part_names |
Text name of the body parts (nodes) that the head will be
configured to produce. The number of parts determines the number of
channels in the output. This must be provided if the |
Returns:
| Type | Description |
|---|---|
CenteredInstanceConfmapsHead
|
The instantiated head with the specified configuration options. |
Source code in sleap_nn/architectures/heads.py
CentroidConfmapsHead
¶
Bases: Head
Head for specifying instance centroid confidence maps.
Attributes:
| Name | Type | Description |
|---|---|---|
anchor_part |
Name of the part to use as an anchor node. If not specified, the bounding box centroid will be used. |
|
centroid_source |
Data-pipeline setting for which centroid the model is
trained to predict ( |
|
centroid_method |
How the centroid is derived from the instance's points --
|
|
centroid_fallback |
Reduce method used when |
|
sigma |
Spread of the confidence maps. |
|
output_stride |
Stride of the output head tensor. The input tensor is expected to be at the same stride. |
|
loss_weight |
Weight of the loss term for this head during optimization. |
|
use_sigmoid_activation |
If |
|
focal_loss_alpha |
Training-loss metadata only (does not affect the head
tensor); kept so the head is constructible from
|
|
focal_loss_beta |
Same as |
|
focal_loss_pos_threshold |
Same as |
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the object with the specified attributes. |
from_config |
Create this head from a set of configurations. |
Source code in sleap_nn/architectures/heads.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | |
activation
property
¶
Return the activation function of the head output layer.
channels
property
¶
Return the number of channels in the tensor output by this head.
__init__(anchor_part=None, centroid_source=None, centroid_method=None, centroid_fallback=None, sigma=5.0, output_stride=1, loss_weight=1.0, use_sigmoid_activation=False, focal_loss_alpha=0.0, focal_loss_beta=4.0, focal_loss_pos_threshold=0.5)
¶
Initialize the object with the specified attributes.
Source code in sleap_nn/architectures/heads.py
from_config(config)
classmethod
¶
Create this head from a set of configurations.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
A |
Returns:
| Type | Description |
|---|---|
CentroidConfmapsHead
|
The instantiated head with the specified configuration options. |
Source code in sleap_nn/architectures/heads.py
ClassMapsHead
¶
Bases: Head
Head for specifying class identity maps.
Attributes:
| Name | Type | Description |
|---|---|---|
classes |
List of string names of the classes. |
|
sigma |
Spread of the class maps around each node. |
|
output_stride |
Stride of the output head tensor. The input tensor is expected to be at the same stride. |
|
loss_weight |
Weight of the loss term for this head during optimization. |
|
class_output |
How the classes map to sleap-io objects ( |
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the object with the specified attributes. |
from_config |
Create this head from a set of configurations. |
Source code in sleap_nn/architectures/heads.py
activation
property
¶
Return the activation function of the head output layer.
channels
property
¶
Return the number of channels in the tensor output by this head.
__init__(classes, sigma=5.0, output_stride=1, loss_weight=1.0, class_output='track')
¶
Initialize the object with the specified attributes.
Source code in sleap_nn/architectures/heads.py
from_config(config, classes=None)
classmethod
¶
Create this head from a set of configurations.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
A |
|
classes |
List of string names of the classes that this head will predict.
This must be set if the |
Returns:
| Type | Description |
|---|---|
ClassMapsHead
|
The instantiated head with the specified configuration options. |
Source code in sleap_nn/architectures/heads.py
ClassVectorsHead
¶
Bases: Head
Head for specifying classification heads.
Attributes:
| Name | Type | Description |
|---|---|---|
classes |
List of string names of the classes. |
|
num_fc_layers |
Number of fully connected layers after flattening input features. |
|
num_fc_units |
Number of units (dimensions) in fully connected layers prior to classification output. |
|
output_stride |
Stride of the output head tensor. The input tensor is expected to be at the same stride. |
|
loss_weight |
Weight of the loss term for this head during optimization. |
|
class_output |
How the classes map to sleap-io objects ( |
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the object with the specified attributes. |
from_config |
Create this head from a set of configurations. |
make_head |
Make head output tensor from input feature tensor. |
Source code in sleap_nn/architectures/heads.py
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 | |
activation
property
¶
Return the activation function of the head output layer.
channels
property
¶
Return the number of channels in the tensor output by this head.
loss_function
property
¶
Return the name of the loss function to use for this head.
__init__(classes, num_fc_layers=1, num_fc_units=64, global_pool=True, output_stride=1, loss_weight=1.0, class_output='track')
¶
Initialize the object with the specified attributes.
Source code in sleap_nn/architectures/heads.py
from_config(config, classes=None)
classmethod
¶
Create this head from a set of configurations.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
A |
|
classes |
List of string names of the classes that this head will predict.
This must be set if the |
Returns:
| Type | Description |
|---|---|
ClassVectorsHead
|
The instantiated head with the specified configuration options. |
Source code in sleap_nn/architectures/heads.py
make_head(x_in)
¶
Make head output tensor from input feature tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_in
|
int
|
An int for the input shape after applying AdaptiveMaxPool2d on dim=1, assuming inputs of shape (B, C, H, W). |
required |
Returns:
| Type | Description |
|---|---|
Sequential
|
A |
Source code in sleap_nn/architectures/heads.py
EmbeddingHead
¶
Bases: Head
Head for crop -> embedding-vector (re-ID) models.
Mirrors ClassVectorsHead (a pooled, non-spatial head): [pool] -> Flatten ->
num_fc_layers x (Linear+ReLU) -> Linear(embedding_dim) -> [L2Norm]. The pooled
feature comes from the backbone's middle_output (lone head, empty decoder).
Attributes:
| Name | Type | Description |
|---|---|---|
embedding_dim |
Output embedding dimensionality. |
|
num_fc_layers |
Number of FC layers before the embedding output. |
|
num_fc_units |
Units in the pre-embedding FC layers. |
|
pool |
Pooling over the encoder feature map: |
|
normalize |
L2-normalize the output embedding. |
|
output_stride |
Should equal the backbone max_stride (so the decoder is empty
and the head taps |
|
loss_weight |
Weight of the loss term for this head. |
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the object with the specified attributes. |
from_config |
Create this head from a head-leaf configuration. |
make_head |
Make the head output module from the pooled-feature input channels. |
Source code in sleap_nn/architectures/heads.py
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 | |
activation
property
¶
Return the activation function of the head output layer.
channels
property
¶
Return the number of channels in the tensor output by this head.
loss_function
property
¶
Return the loss-function name (informational).
The contrastive loss is batch-level and is driven by the embedding
LightningModule's objective, not by this string.
__init__(embedding_dim=128, num_fc_layers=1, num_fc_units=256, pool='gem', normalize=True, output_stride=1, loss_weight=1.0)
¶
Initialize the object with the specified attributes.
Source code in sleap_nn/architectures/heads.py
from_config(config)
classmethod
¶
Create this head from a head-leaf configuration.
Source code in sleap_nn/architectures/heads.py
make_head(x_in)
¶
Make the head output module from the pooled-feature input channels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_in
|
int
|
Number of channels of the encoder feature map (its channel dim). |
required |
Returns:
| Type | Description |
|---|---|
Sequential
|
An |
Source code in sleap_nn/architectures/heads.py
GeM
¶
Bases: Module
Generalized-mean pooling: (mean(x.clamp(min=eps)^p))^(1/p) over HxW.
The exponent p is learnable (init 3.0). The clamp(min=eps) BEFORE the
fractional power guards against NaNs (a fractional power of a negative/zero base).
Returns a flattened [B, C] tensor.
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the pooling layer. |
forward |
Pool |
Source code in sleap_nn/architectures/heads.py
__init__(p=3.0, eps=1e-06, learnable=True)
¶
Initialize the pooling layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
float
|
Initial generalized-mean exponent ( |
3.0
|
eps
|
float
|
Small floor applied to the activation before the power to avoid a fractional power of a non-positive value. |
1e-06
|
learnable
|
bool
|
If |
True
|
Source code in sleap_nn/architectures/heads.py
forward(x)
¶
Pool [B, C, H, W] to [B, C] by the generalized mean over HxW.
Source code in sleap_nn/architectures/heads.py
Head
¶
Base class for model output heads.
Attributes:
| Name | Type | Description |
|---|---|---|
output_stride |
Stride of the output head tensor. The input tensor is expected to be at the same stride. |
|
loss_weight |
Weight of the loss term for this head during optimization. |
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the object with the specified attributes. |
make_head |
Make head output tensor from input feature tensor. |
Source code in sleap_nn/architectures/heads.py
activation
property
¶
Return the activation function of the head output layer.
channels
property
¶
Return the number of channels in the tensor output by this head.
loss_function
property
¶
Return the name of the loss function to use for this head.
name
property
¶
Name of the head.
__init__(output_stride=1, loss_weight=1.0)
¶
Initialize the object with the specified attributes.
make_head(x_in)
¶
Make head output tensor from input feature tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_in
|
int
|
An int input for the input channels. |
required |
Returns:
| Type | Description |
|---|---|
Sequential
|
A |
Source code in sleap_nn/architectures/heads.py
InstanceCenterHead
¶
Bases: Head
Head for predicting instance center heatmaps.
Outputs a single-channel Gaussian heatmap with peaks at each instance's mask centroid. Similar to CentroidConfmapsHead but for mask-derived centers.
Attributes:
| Name | Type | Description |
|---|---|---|
sigma |
Standard deviation of the Gaussian in pixels. |
|
output_stride |
Stride of the output head tensor. |
|
loss_weight |
Weight of the loss term for this head during optimization. |
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the object with the specified attributes. |
Source code in sleap_nn/architectures/heads.py
L2Norm
¶
Bases: Module
L2-normalize along dim (so embeddings live on the unit hypersphere).
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the layer. |
forward |
L2-normalize |
Source code in sleap_nn/architectures/heads.py
__init__(dim=1)
¶
Initialize the layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim
|
int
|
Dimension along which to L2-normalize. |
1
|
MultiInstanceConfmapsHead
¶
Bases: Head
Head for specifying multi-instance confidence maps.
Attributes:
| Name | Type | Description |
|---|---|---|
part_names |
List of strings specifying the part names associated with channels. |
|
sigma |
Spread of the confidence maps. |
|
output_stride |
Stride of the output head tensor. The input tensor is expected to be at the same stride. |
|
loss_weight |
Weight of the loss term for this head during optimization. |
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the object with the specified attributes. |
from_config |
Create this head from a set of configurations. |
Source code in sleap_nn/architectures/heads.py
channels
property
¶
Return the number of channels in the tensor output by this head.
__init__(part_names, sigma=5.0, output_stride=1, loss_weight=1.0)
¶
Initialize the object with the specified attributes.
Source code in sleap_nn/architectures/heads.py
from_config(config, part_names=None)
classmethod
¶
Create this head from a set of configurations.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
A |
|
part_names |
Text name of the body parts (nodes) that the head will be
configured to produce. The number of parts determines the number of
channels in the output. This must be provided if the |
Returns:
| Type | Description |
|---|---|
MultiInstanceConfmapsHead
|
The instantiated head with the specified configuration options. |
Source code in sleap_nn/architectures/heads.py
OffsetRefinementHead
¶
Bases: Head
Head for specifying offset refinement maps.
Attributes:
| Name | Type | Description |
|---|---|---|
part_names |
List of strings specifying the part names associated with channels. |
|
sigma_threshold |
Threshold of confidence map values to use for defining the boundary of the offset maps. |
|
output_stride |
Stride of the output head tensor. The input tensor is expected to be at the same stride. |
|
loss_weight |
Weight of the loss term for this head during optimization. |
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the object with the specified attributes. |
from_config |
Create this head from a set of configurations. |
Source code in sleap_nn/architectures/heads.py
channels
property
¶
Return the number of channels in the tensor output by this head.
__init__(part_names, sigma_threshold=0.2, output_stride=1, loss_weight=1.0)
¶
Initialize the object with the specified attributes.
Source code in sleap_nn/architectures/heads.py
from_config(config, part_names=None, sigma_threshold=0.2, loss_weight=1.0)
classmethod
¶
Create this head from a set of configurations.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
A |
|
part_names |
Text name of the body parts (nodes) that the head will be
configured to produce. The number of parts determines the number of
channels in the output. This must be provided if the |
|
sigma_threshold |
Minimum confidence map value below which offsets will be replaced with zeros. |
|
loss_weight |
Weight of the loss associated with this head. |
Returns:
| Type | Description |
|---|---|
OffsetRefinementHead
|
The instantiated head with the specified configuration options. |
Source code in sleap_nn/architectures/heads.py
PartAffinityFieldsHead
¶
Bases: Head
Head for specifying multi-instance part affinity fields.
Attributes:
| Name | Type | Description |
|---|---|---|
edges |
List of tuples of |
|
sigma |
Spread of the part affinity fields. |
|
output_stride |
Stride of the output head tensor. The input tensor is expected to be at the same stride. |
|
loss_weight |
Weight of the loss term for this head during optimization. |
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the object with the specified attributes. |
from_config |
Create this head from a set of configurations. |
Source code in sleap_nn/architectures/heads.py
channels
property
¶
Return the number of channels in the tensor output by this head.
__init__(edges, sigma=5.0, output_stride=1, loss_weight=1.0)
¶
Initialize the object with the specified attributes.
Source code in sleap_nn/architectures/heads.py
from_config(config, edges=None)
classmethod
¶
Create this head from a set of configurations.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
A |
|
edges |
List of 2-tuples of the form |
Returns:
| Type | Description |
|---|---|
PartAffinityFieldsHead
|
The instantiated head with the specified configuration options. |
Source code in sleap_nn/architectures/heads.py
SegmentationHead
¶
Bases: Head
Head for predicting binary foreground segmentation masks.
Outputs a single-channel map with sigmoid activation representing the probability that each pixel belongs to any instance (foreground).
Attributes:
| Name | Type | Description |
|---|---|---|
output_stride |
Stride of the output head tensor. |
|
loss_weight |
Weight of the loss term for this head during optimization. |
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the object with the specified attributes. |
Source code in sleap_nn/architectures/heads.py
activation
property
¶
Return the activation function of the head output layer.
channels
property
¶
Return the number of channels in the tensor output by this head.
loss_function
property
¶
Return the name of the loss function to use for this head.
__init__(output_stride=2, loss_weight=1.0)
¶
Initialize the object with the specified attributes.
SingleInstanceConfmapsHead
¶
Bases: Head
Head for specifying single instance confidence maps.
Attributes:
| Name | Type | Description |
|---|---|---|
part_names |
List of strings specifying the part names associated with channels. |
|
sigma |
Spread of the confidence maps. |
|
output_stride |
Stride of the output head tensor. The input tensor is expected to be at the same stride. |
|
loss_weight |
Weight of the loss term for this head during optimization. |
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the object with the specified attributes. |
from_config |
Create this head from a set of configurations. |
Source code in sleap_nn/architectures/heads.py
channels
property
¶
Return the number of channels in the tensor output by this head.
__init__(part_names, sigma=5.0, output_stride=1, loss_weight=1.0)
¶
Initialize the object with the specified attributes.
Source code in sleap_nn/architectures/heads.py
from_config(config, part_names=None)
classmethod
¶
Create this head from a set of configurations.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
A |
|
part_names |
Text name of the body parts (nodes) that the head will be
configured to produce. The number of parts determines the number of
channels in the output. This must be provided if the |
Returns:
| Type | Description |
|---|---|
SingleInstanceConfmapsHead
|
The instantiated head with the specified configuration options. |