utils
sleap_nn.config.utils
¶
Utilities for config building and validation.
Functions:
| Name | Description |
|---|---|
check_centroid_methods |
Validate every head config's centroid-method knobs (#586). |
check_output_strides |
Check max_stride and output_stride in backbone_config with head_config. |
check_tiling |
Validate + reconcile tiling geometry against the finalized backbone/head. |
check_tiling_parity |
Re-check inference tiling geometry against the trained model config. |
get_backbone_type_from_cfg |
Return the backbone type from the config. One of [unet, swint, convnext]. |
get_model_type_from_cfg |
Return the model type from the config. One of [single_instance, centroid, centered_instance, bottomup]. |
get_output_strides_from_heads |
Get list of output strides from head configs. |
oneof |
Ensure that the decorated attrs class only has a single attribute set. |
resolve_model_dir |
Resolve a user-supplied model path to its model directory. |
check_centroid_methods(config)
¶
Validate every head config's centroid-method knobs (#586).
A contradictory pair (anchor_part plus a non-anchor centroid_method)
or an unknown method name must fail at setup with a message naming the head,
not deep inside the first __getitem__ of a dataloader worker — where the
traceback is a multiprocessing wrapper and the run has already spent minutes
caching images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
OmegaConf
|
The full training job config. |
required |
Returns:
| Type | Description |
|---|---|
OmegaConf
|
The config, unchanged (validation only). |
Raises:
| Type | Description |
|---|---|
ValueError
|
For any head whose centroid knobs do not resolve. |
Source code in sleap_nn/config/utils.py
check_output_strides(config)
¶
Check max_stride and output_stride in backbone_config with head_config.
Source code in sleap_nn/config/utils.py
check_tiling(config)
¶
Validate + reconcile tiling geometry against the finalized backbone/head.
No-op unless data_config.preprocessing.tiling.enabled is True.
Must run after :func:check_output_strides so max_stride /
output_stride are finalized, and after _setup_tiling_config has
auto-sized tile_size / overlap from the labels. Enforces:
- GUARD: pretrained-encoder / non-(unet|convnext|swint) backbone -> ValueError.
- GUARD:
multi_class_topdown/ ClassVectorsHead -> ValueError. tile_sizedivisible bylcm(max_stride, output_stride)(rounds UP + warns).overlapdivisible byoutput_stride,>= min_overlap_fraction * tile_size(raises overlap + warns), and0 <= overlap < tile_size(else ValueError).
Guards are enforced explicitly here (not via attrs validators) because
_setup_tiling_config mutates the config in place on the OmegaConf object,
which does not re-run attrs validators.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
OmegaConf
|
The (finalized) training/inference config. |
required |
Returns:
| Type | Description |
|---|---|
OmegaConf
|
The config, mutated in place with reconciled tiling geometry. |
Source code in sleap_nn/config/utils.py
238 239 240 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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | |
check_tiling_parity(config, tile_size_override=None, overlap_override=None)
¶
Re-check inference tiling geometry against the trained model config.
Tiling requires train-time == infer-time geometry (scale parity); the trained geometry lives in the model config. A user-supplied geometry override that diverges from the trained values requires a retrain, so this raises on a mismatch. No-op unless tiling is enabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
OmegaConf
|
The loaded (trained) model config. |
required |
tile_size_override
|
Optional[int]
|
Optional inference-time |
None
|
overlap_override
|
Optional[int]
|
Optional inference-time |
None
|
Returns:
| Type | Description |
|---|---|
OmegaConf
|
The config unchanged (parity check only). |
Source code in sleap_nn/config/utils.py
get_backbone_type_from_cfg(config)
¶
Return the backbone type from the config. One of [unet, swint, convnext].
Source code in sleap_nn/config/utils.py
get_model_type_from_cfg(config)
¶
Return the model type from the config. One of [single_instance, centroid, centered_instance, bottomup].
Source code in sleap_nn/config/utils.py
get_output_strides_from_heads(head_configs)
¶
Get list of output strides from head configs.
Source code in sleap_nn/config/utils.py
oneof(attrs_cls, must_be_set=False)
¶
Ensure that the decorated attrs class only has a single attribute set.
This decorator is inspired by the oneof protobuffer field behavior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attrs_cls
|
An attrs decorated class. |
required | |
must_be_set
|
bool
|
If True, raise an error if none of the attributes are set. If not, error will only be raised if more than one attribute is set. |
False
|
Returns:
| Type | Description |
|---|---|
|
The |
Source code in sleap_nn/config/utils.py
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 | |
resolve_model_dir(model_path)
¶
Resolve a user-supplied model path to its model directory.
A trained model lives in a directory holding training_config.{yaml,json}
and a best.ckpt checkpoint. Callers historically had to pass that
directory. This helper additionally accepts a path to a file inside it —
either the training_config.{yaml,json} config or a .ckpt checkpoint —
and returns the containing directory, so users can point at best.ckpt or
training_config.yaml wherever a model directory is expected (issue #575).
The directory's contents are intentionally NOT validated here: the caller's
loader (e.g. :func:sleap_nn.inference.loaders._load_training_config) remains
the single source of truth for whether the resolved directory holds a usable
config and checkpoint, so its error messages stay attributable.
A directory is always loaded via its best.ckpt. If the path points at a
different checkpoint (e.g. last.ckpt), a warning is emitted and
best.ckpt is loaded anyway — use backbone_ckpt_path / head_ckpt_path
to load a specific checkpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_path
|
Union[str, Path]
|
A model directory, or a path to a |
required |
Returns:
| Type | Description |
|---|---|
str
|
The resolved model directory as a POSIX-style string. Relative paths are preserved (only the path separators are normalized); the path is not resolved against the filesystem root. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If |