utils
sleap_nn.export.utils
¶
Utilities for export workflows.
Functions:
| Name | Description |
|---|---|
build_bottomup_candidate_template |
Build candidate template matching ONNX wrapper's line_scores ordering. |
load_training_config |
Load training configuration from a model directory. |
resolve_anchor_part |
Resolve anchor_part from config for the model types that center a crop. |
resolve_backbone_source |
Resolve a human-readable backbone weight source for metadata. |
resolve_backbone_type |
Return backbone type from config. |
resolve_background_fill |
Resolve the masked-out background fill the trained embedder used (burn-in). |
resolve_burn_in |
Resolve whether the trained embedder masked the crop (mask burn-in). |
resolve_centroid_method |
Resolve the trained centroid method for the model types that center a crop. |
resolve_class_maps_output_stride |
Resolve class maps output stride for multiclass bottom-up models. |
resolve_class_names |
Resolve class names for multiclass models. |
resolve_crop_size |
Resolve crop size from preprocessing config. |
resolve_edge_inds |
Resolve edge indices for metadata. |
resolve_embedding_dim |
Resolve the embedding (output vector) dimensionality for an embedding model. |
resolve_embedding_input_channels |
Resolve the DATA channels an embedding crop is fed with (not the backbone). |
resolve_input_channels |
Resolve input channels from backbone config. |
resolve_input_scale |
Resolve preprocessing scale from config. |
resolve_input_shape |
Resolve a dummy input shape for export. |
resolve_model_type |
Return model type from config. |
resolve_n_classes |
Resolve number of classes for multiclass models. |
resolve_node_names |
Resolve node names for metadata. |
resolve_normalize |
Resolve whether the embedding head L2-normalizes its output. |
resolve_output_stride |
Resolve output stride from head config. |
resolve_pafs_output_stride |
Resolve PAFs output stride for bottom-up models. |
warn_on_tiled_export |
Warn that a tiled model is exported without its tiling wrapper (deferred). |
build_bottomup_candidate_template(n_nodes, max_peaks_per_node, edge_inds)
¶
Build candidate template matching ONNX wrapper's line_scores ordering.
The ONNX BottomUpONNXWrapper produces line_scores with shape (n_edges, k*k) where for each edge connecting (src_node, dst_node), position i*k + j corresponds to: - src peak flat index: src_node * k + i - dst peak flat index: dst_node * k + j
This function builds edge_inds and edge_peak_inds tensors that match this exact ordering, so that line_scores_flat[idx] corresponds to edge_peak_inds[idx].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_nodes
|
int
|
Number of nodes in the skeleton. |
required |
max_peaks_per_node
|
int
|
Maximum peaks per node (k) used during export. |
required |
edge_inds
|
List[Tuple[int, int]]
|
List of (src_node, dst_node) tuples defining skeleton edges. |
required |
Returns:
| Type | Description |
|---|---|
Tuple['torch.Tensor', 'torch.Tensor', 'torch.Tensor']
|
Tuple of (peak_channel_inds, edge_inds_tensor, edge_peak_inds_tensor): - peak_channel_inds: (n_nodes * k,) tensor mapping flat peak index to node - edge_inds_tensor: (n_edges * k * k,) tensor of edge indices for each candidate - edge_peak_inds_tensor: (n_edges * k * k, 2) tensor of (src, dst) peak indices |
Example
from sleap_nn.export.utils import build_bottomup_candidate_template peak_ch, edge_inds, edge_peaks = build_bottomup_candidate_template( ... n_nodes=15, max_peaks_per_node=20, edge_inds=[(1, 2), (1, 5)] ... )
Use with ONNX output:¶
line_scores_flat = line_scores.reshape(-1) valid_scores = line_scores_flat[valid_mask] valid_edge_peaks = edge_peaks[valid_mask]
Note
This function is necessary because get_connection_candidates() in
sleap_nn.inference.paf_grouping uses unstable argsort, which shuffles
peak indices within each node and breaks alignment with ONNX output ordering.
Source code in sleap_nn/export/utils.py
load_training_config(model_dir)
¶
Load training configuration from a model directory.
Source code in sleap_nn/export/utils.py
resolve_anchor_part(cfg, model_type)
¶
Resolve anchor_part from config for the model types that center a crop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
|
DictConfig
|
The training job configuration. |
required |
model_type
|
str
|
The model type (e.g., "centroid", "centered_instance", "embedding"). |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The anchor part name if configured, None otherwise. Only returns a value
for the "centroid", "centered_instance" and "embedding" model types -- the
heads that carry the crop/centroid-center knobs (#586). For an
|
Source code in sleap_nn/export/utils.py
resolve_backbone_source(cfg)
¶
Resolve a human-readable backbone weight source for metadata.
Returns "imagenet" if the backbone was initialized from pretrained ImageNet
weights (convnext/swint pre_trained_weights or unet
pretrained_backbone_weights), else "scratch".
Source code in sleap_nn/export/utils.py
resolve_backbone_type(cfg)
¶
resolve_background_fill(cfg)
¶
Resolve the masked-out background fill the trained embedder used (burn-in).
Source code in sleap_nn/export/utils.py
resolve_burn_in(cfg)
¶
Resolve whether the trained embedder masked the crop (mask burn-in).
A burn_in=True model standardizes over the foreground only and replaces the
background, which the single-input ONNX wrapper (maskless whole-crop standardize)
cannot reproduce — so this drives the export-time divergence warning + metadata.
Mirrors the canonical default in PreprocessingConfig.burn_in (False).
Source code in sleap_nn/export/utils.py
resolve_centroid_method(cfg, model_type)
¶
Resolve the trained centroid method for the model types that center a crop.
Companion to :func:resolve_anchor_part. Recorded in the export metadata so a
consumer of the exported model can tag predicted centroids with the method the
model was actually trained on (#586) — without it, a bbox_center model's
predictions would be recorded as center_of_mass.
The same applies to an embedding (re-ID) model, more strongly: its
consumer must produce the crops itself, so the crop-center recipe the
embedder was trained with is what makes its vectors comparable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
|
DictConfig
|
The training job configuration. |
required |
model_type
|
str
|
The model type (e.g., "centroid", "centered_instance", "embedding"). |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The resolved method (one of
|
Source code in sleap_nn/export/utils.py
resolve_class_maps_output_stride(cfg)
¶
Resolve class maps output stride for multiclass bottom-up models.
Source code in sleap_nn/export/utils.py
resolve_class_names(cfg, model_type)
¶
Resolve class names for multiclass models.
Source code in sleap_nn/export/utils.py
resolve_crop_size(cfg)
¶
Resolve crop size from preprocessing config.
Source code in sleap_nn/export/utils.py
resolve_edge_inds(cfg, node_names)
¶
Resolve edge indices for metadata.
Source code in sleap_nn/export/utils.py
resolve_embedding_dim(cfg)
¶
Resolve the embedding (output vector) dimensionality for an embedding model.
Source code in sleap_nn/export/utils.py
resolve_embedding_input_channels(cfg)
¶
Resolve the DATA channels an embedding crop is fed with (not the backbone).
The embedder forces grayscale by default (1 channel); a 3-channel ImageNet
backbone repeats the gray channel internally (Model.forward). Only when the
user explicitly opts into RGB (ensure_rgb) are the crops 3-channel. This is
the channel count the exported graph should accept, independent of the backbone
in_channels.
Source code in sleap_nn/export/utils.py
resolve_input_channels(cfg)
¶
Resolve input channels from backbone config.
resolve_input_scale(cfg)
¶
Resolve preprocessing scale from config.
Source code in sleap_nn/export/utils.py
resolve_input_shape(cfg, input_height=None, input_width=None)
¶
Resolve a dummy input shape for export.
Source code in sleap_nn/export/utils.py
resolve_model_type(cfg)
¶
resolve_n_classes(cfg, model_type)
¶
Resolve number of classes for multiclass models.
resolve_node_names(cfg, model_type)
¶
Resolve node names for metadata.
Source code in sleap_nn/export/utils.py
resolve_normalize(cfg)
¶
Resolve whether the embedding head L2-normalizes its output.
Source code in sleap_nn/export/utils.py
resolve_output_stride(cfg, model_type)
¶
Resolve output stride from head config.
Source code in sleap_nn/export/utils.py
resolve_pafs_output_stride(cfg)
¶
Resolve PAFs output stride for bottom-up models.
Source code in sleap_nn/export/utils.py
warn_on_tiled_export(cfg)
¶
Warn that a tiled model is exported without its tiling wrapper (deferred).
Tiled ONNX/TensorRT export is not yet implemented (design DQ15): the exported graph is the plain per-frame network, so an exported tiled model runs whole-frame (no sliding-window tiling / stitching). PyTorch inference still tiles correctly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
|
DictConfig
|
A loaded training config. |
required |