Skip to content

loaders

sleap_nn.inference.loaders

Standalone checkpoint loading for the inference pipeline.

Nothing user-facing here -- the public API remains :class:sleap_nn.inference.Predictor via :meth:Predictor.from_model_paths.

Classes:

Name Description
LoadedAssets

Everything the factory's _build_*_layer helpers need.

Functions:

Name Description
load_model_assets

Load checkpoints and build inference models.

LoadedAssets

Everything the factory's _build_*_layer helpers need.

Source code in sleap_nn/inference/loaders.py
@attrs.define(eq=False, repr=False)
class LoadedAssets:
    """Everything the factory's ``_build_*_layer`` helpers need."""

    inference_model: Any  # Union of all *InferenceModel types
    preprocess_config: "DictConfig"
    skeletons: list["sio.Skeleton"]

    bottomup_config: Optional["DictConfig"] = None
    backbone_type: Optional[str] = None
    max_stride: Optional[int] = None

    centroid_config: Optional["DictConfig"] = None
    confmap_config: Optional["DictConfig"] = None

    # Cap on instances per frame. Threaded through to the bottom-up grouping
    # stage (the legacy ``BottomUpInferenceModel`` has no such field, so the
    # value is carried on the assets and read by ``_build_bottomup_layer``).
    max_instances: Optional[int] = None

    # A ``bbox_*`` detector's own assets when it is the first stage of a top-down
    # pipeline (in place of a centroid model); these assets then hold the
    # crop-consuming stage 2, built as for its GT-centroid fallback.
    detector_assets: Optional["LoadedAssets"] = None

load_model_assets(model_paths, *, device='cpu', backbone_ckpt_path=None, head_ckpt_path=None, peak_threshold=0.2, integral_refinement='integral', integral_patch_size=5, max_instances=None, return_confmaps=False, preprocess_config=None, anchor_part=None, max_edge_length_ratio=0.25, dist_penalty_weight=1.0, n_points=10, min_instance_peaks=0, min_line_scores=0.25, fg_threshold=0.5, min_mask_area=0, center_nms_kernel=3, mask_cleanup=False, mask_cleanup_radius=0, distance_gate_alpha=None, merge_fragments=False, merge_method='greedy', merge_thresholds=(0.85, 0.6, 0.4), merge_w_valley=1.0, merge_w_offset=0.25, merge_dilate=1, full_res_masks=False, mask_output='mask', polygon_epsilon=0.01, bbox_nms_iou=None)

Load checkpoints and build inference models.

Each entry in model_paths may be a model directory, or a path to its best.ckpt checkpoint or training_config.{yaml,json} config file; every form is resolved to the model directory (#575).

The five bottom-up PAF grouping knobs (max_edge_length_ratio, dist_penalty_weight, n_points, min_instance_peaks, min_line_scores) are forwarded ONLY to the plain bottom-up builder (legacy applied them only to BottomUpPredictor); they are inert for other model types. Likewise fg_threshold / min_mask_area are forwarded ONLY to the bottom-up segmentation builder, and bbox_nms_iou only to the bbox_* detector builder.

Returns:

Type Description
tuple[LoadedAssets, List[str]]

(loaded_assets, model_types) — model_types is the list of detected model types (one per path in model_paths).

Source code in sleap_nn/inference/loaders.py
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
def load_model_assets(
    model_paths: List[str],
    *,
    device: str = "cpu",
    backbone_ckpt_path: Optional[str] = None,
    head_ckpt_path: Optional[str] = None,
    peak_threshold: Union[float, List[float]] = 0.2,
    integral_refinement: str = "integral",
    integral_patch_size: int = 5,
    max_instances: Optional[int] = None,
    return_confmaps: bool = False,
    preprocess_config: Optional["DictConfig"] = None,
    anchor_part: Optional[str] = None,
    max_edge_length_ratio: float = 0.25,
    dist_penalty_weight: float = 1.0,
    n_points: int = 10,
    min_instance_peaks: Union[int, float] = 0,
    min_line_scores: float = 0.25,
    fg_threshold: float = 0.5,
    min_mask_area: int = 0,
    center_nms_kernel: int = 3,
    mask_cleanup: bool = False,
    mask_cleanup_radius: int = 0,
    distance_gate_alpha: Optional[float] = None,
    merge_fragments: bool = False,
    merge_method: str = "greedy",
    merge_thresholds: tuple = (0.85, 0.6, 0.4),
    merge_w_valley: float = 1.0,
    merge_w_offset: float = 0.25,
    merge_dilate: int = 1,
    full_res_masks: bool = False,
    mask_output: str = "mask",
    polygon_epsilon: float = 0.01,
    bbox_nms_iou: Optional[float] = None,
) -> tuple[LoadedAssets, List[str]]:
    """Load checkpoints and build inference models.

    Each entry in ``model_paths`` may be a model directory, or a path to its
    ``best.ckpt`` checkpoint or ``training_config.{yaml,json}`` config file; every
    form is resolved to the model directory (#575).

    The five bottom-up PAF grouping knobs (``max_edge_length_ratio``,
    ``dist_penalty_weight``, ``n_points``, ``min_instance_peaks``,
    ``min_line_scores``) are forwarded ONLY to the plain bottom-up builder
    (legacy applied them only to ``BottomUpPredictor``); they are inert for
    other model types. Likewise ``fg_threshold`` / ``min_mask_area`` are
    forwarded ONLY to the bottom-up segmentation builder, and ``bbox_nms_iou``
    only to the ``bbox_*`` detector builder.

    Returns:
        ``(loaded_assets, model_types)`` — *model_types* is the list of
        detected model types (one per path in *model_paths*).
    """
    if preprocess_config is None:
        preprocess_config = OmegaConf.create(
            {
                "ensure_rgb": None,
                "ensure_grayscale": None,
                "crop_size": None,
                "max_width": None,
                "max_height": None,
                "scale": None,
            }
        )

    # Accept a model directory, a best.ckpt path, or a training_config.{yaml,json}
    # path for each entry; resolve every form to its model directory before
    # detection/dispatch (the builders below index back into model_paths and join
    # `best.ckpt` onto it). #575.
    model_paths = [resolve_model_dir(mp) for mp in model_paths]

    model_types: List[str] = []
    configs: List[Any] = []
    for mp in model_paths:
        cfg, _ = _load_training_config(mp)
        configs.append(cfg)
        model_types.append(get_model_type_from_cfg(config=cfg))

    # Reject duplicate model types up front. The dispatch below picks the
    # FIRST path of a given type via `model_types.index(...)`, so passing two
    # paths of the same type (e.g. two centroid dirs) would otherwise
    # silently use only one and drop the other with no indication anything
    # was wrong. (Detecting an *unrelated* extra path -- one whose type isn't
    # consumed by whichever branch below ends up winning -- would need
    # dispatch-branch-aware validation; that's a separate, bigger follow-up,
    # not covered here.)
    _seen_type_paths: dict = {}
    for mp, mt in zip(model_paths, model_types):
        if mt in _seen_type_paths:
            raise ValueError(
                f"Duplicate model type {mt!r} in --model_paths: got both "
                f"{_seen_type_paths[mt]!r} and {mp!r}. Pass only one model "
                "directory per type."
            )
        _seen_type_paths[mt] = mp

    common_kwargs = dict(
        device=device,
        backbone_ckpt_path=backbone_ckpt_path,
        head_ckpt_path=head_ckpt_path,
        peak_threshold=peak_threshold,
        integral_refinement=integral_refinement,
        integral_patch_size=integral_patch_size,
        return_confmaps=return_confmaps,
        preprocess_config=preprocess_config,
    )

    # Dispatch on detected model type. The order (bottomup checked before the
    # topdown family) differs from legacy but is inert for parity: each model
    # path is exactly one type and bottom-up is a single-stage model never
    # combined with a centroid/centered-instance pair, so the branches are
    # mutually exclusive (a mixed bottomup+topdown path list is not a supported
    # workflow and falls through to the final ValueError). #584.
    if "single_instance" in model_types:
        path = model_paths[model_types.index("single_instance")]
        assets = _build_single_instance(path, **common_kwargs)

    elif "bottomup" in model_types:
        path = model_paths[model_types.index("bottomup")]
        assets = _build_bottomup(
            path,
            max_instances=max_instances,
            max_edge_length_ratio=max_edge_length_ratio,
            dist_penalty_weight=dist_penalty_weight,
            n_points=n_points,
            min_instance_peaks=min_instance_peaks,
            min_line_scores=min_line_scores,
            **common_kwargs,
        )

    elif "multi_class_bottomup" in model_types:
        path = model_paths[model_types.index("multi_class_bottomup")]
        assets = _build_bottomup_multiclass(
            path, max_instances=max_instances, **common_kwargs
        )

    elif "bottomup_segmentation" in model_types:
        path = model_paths[model_types.index("bottomup_segmentation")]
        assets = _build_bottomup_segmentation(
            path,
            fg_threshold=fg_threshold,
            min_mask_area=min_mask_area,
            max_instances=max_instances,
            center_nms_kernel=center_nms_kernel,
            mask_cleanup=mask_cleanup,
            mask_cleanup_radius=mask_cleanup_radius,
            distance_gate_alpha=distance_gate_alpha,
            merge_fragments=merge_fragments,
            merge_method=merge_method,
            merge_thresholds=merge_thresholds,
            merge_w_valley=merge_w_valley,
            merge_w_offset=merge_w_offset,
            merge_dilate=merge_dilate,
            full_res_masks=full_res_masks,
            mask_output=mask_output,
            polygon_epsilon=polygon_epsilon,
            **common_kwargs,
        )

    elif "semantic_segmentation" in model_types:
        # Whole-frame semantic (foreground/background) segmentation. A single-stage
        # whole-frame model (never combined with a centroid), so it gets its own
        # top-level branch alongside bottomup_segmentation.
        path = model_paths[model_types.index("semantic_segmentation")]
        assets = _build_semantic_segmentation(
            path,
            fg_threshold=fg_threshold,
            min_mask_area=min_mask_area,
            full_res_masks=full_res_masks,
            mask_output=mask_output,
            polygon_epsilon=polygon_epsilon,
            **common_kwargs,
        )

    elif "bbox_centernet" in model_types or "bbox_ssd" in model_types:
        # Whole-frame bounding-box detectors. Alone they are single-stage; with a
        # crop-consuming top-down model they replace the centroid model as stage 1.
        model_type = "bbox_centernet" if "bbox_centernet" in model_types else "bbox_ssd"
        if "bbox_centernet" in model_types and "bbox_ssd" in model_types:
            raise ValueError(
                "Got two bounding-box detectors (bbox_centernet and bbox_ssd); pass "
                "one."
            )
        path = model_paths[model_types.index(model_type)]
        stage_2 = [t for t in BBOX_STAGE_2_MODEL_TYPES if t in model_types]
        detector_kwargs = dict(common_kwargs)
        # `_resolve_preprocess_config` fills its argument in place: give the
        # detector its own copy of the caller's overrides, so its training values
        # (input scale, size matcher) do not leak into stage 2's resolution.
        detector_kwargs["preprocess_config"] = copy.deepcopy(preprocess_config)
        if isinstance(peak_threshold, (list, tuple)):
            # A per-stage [stage 1, stage 2] threshold: stage 1 is the detector.
            detector_kwargs["peak_threshold"] = peak_threshold[0]
        detector = _build_bbox(
            path,
            model_type=model_type,
            max_instances=max_instances,
            nms_iou=bbox_nms_iou,
            **detector_kwargs,
        )
        if not stage_2:
            if "centroid" in model_types:
                raise ValueError(
                    "Got a centroid model and a bounding-box detector but no "
                    "top-down model for them to feed; pass one first stage plus a "
                    "centered_instance / multi_class_topdown / "
                    "centered_instance_segmentation model, or the detector alone."
                )
            assets = detector
        else:
            if "centroid" in model_types:
                raise ValueError(
                    "Got both a centroid model and a bounding-box detector: pass one "
                    "of them as the first stage of the top-down pipeline."
                )
            if len(stage_2) > 1:
                raise ValueError(
                    f"A bounding-box detector feeds one top-down model; got {stage_2}."
                )
            stage_2_path = model_paths[model_types.index(stage_2[0])]
            builder = {
                "centered_instance": _build_topdown,
                "multi_class_topdown": _build_topdown_multiclass,
            }.get(stage_2[0])
            if builder is not None:
                assets = builder(
                    None,
                    stage_2_path,
                    max_instances=max_instances,
                    anchor_part=anchor_part,
                    **common_kwargs,
                )
            else:
                assets = _build_topdown_segmentation(
                    None,
                    stage_2_path,
                    max_instances=max_instances,
                    anchor_part=anchor_part,
                    fg_threshold=fg_threshold,
                    mask_output=mask_output,
                    polygon_epsilon=polygon_epsilon,
                    **common_kwargs,
                )
            assets.detector_assets = detector

    elif "embedding" in model_types:
        # Appearance-embedding (re-ID). Checked BEFORE the topdown family block:
        # a centroid + embedding pair has "centroid" in model_types, so it would
        # otherwise enter that block and silently drop the embedding dir.
        #   - centroid + embedding -> compose centroid -> crop -> embed on raw
        #     video (TopDownEmbeddingLayer).
        #   - embedding alone -> single-stage, mask-driven case (EmbeddingLayer).
        emb_path = model_paths[model_types.index("embedding")]
        centroid_path = (
            model_paths[model_types.index("centroid")]
            if "centroid" in model_types
            else None
        )
        if centroid_path is not None:
            assets = _build_topdown_embedding(
                centroid_path,
                emb_path,
                max_instances=max_instances,
                anchor_part=anchor_part,
                **common_kwargs,
            )
        else:
            assets = _build_embedding(emb_path, **common_kwargs)

    elif "centered_instance_segmentation" in model_types:
        # Top-down (crop-centered) instance segmentation. MUST be checked BEFORE
        # the topdown family block below: a centroid + centered_instance_segmentation
        # pair has "centroid" in model_types, so it would otherwise enter that block
        # and — finding neither "centered_instance" nor "multi_class_topdown" — fall
        # to the centroid-only else, SILENTLY DROPPING the seg dir (verify/v2 #1).
        seg_path = model_paths[model_types.index("centered_instance_segmentation")]
        centroid_path = (
            model_paths[model_types.index("centroid")]
            if "centroid" in model_types
            else None
        )
        assets = _build_topdown_segmentation(
            centroid_path,
            seg_path,
            max_instances=max_instances,
            anchor_part=anchor_part,
            fg_threshold=fg_threshold,
            mask_output=mask_output,
            polygon_epsilon=polygon_epsilon,
            **common_kwargs,
        )

    elif (
        "centroid" in model_types
        or "centered_instance" in model_types
        or "multi_class_topdown" in model_types
    ):
        centroid_path = None
        confmap_path = None
        if "centroid" in model_types:
            centroid_path = model_paths[model_types.index("centroid")]
        if "centered_instance" in model_types:
            confmap_path = model_paths[model_types.index("centered_instance")]
            assets = _build_topdown(
                centroid_path,
                confmap_path,
                max_instances=max_instances,
                anchor_part=anchor_part,
                **common_kwargs,
            )
        elif "multi_class_topdown" in model_types:
            confmap_path = model_paths[model_types.index("multi_class_topdown")]
            assets = _build_topdown_multiclass(
                centroid_path,
                confmap_path,
                max_instances=max_instances,
                anchor_part=anchor_part,
                **common_kwargs,
            )
        else:
            # centroid-only: still goes through _build_topdown with confmap=None
            assets = _build_topdown(
                centroid_path,
                None,
                max_instances=max_instances,
                anchor_part=anchor_part,
                **common_kwargs,
            )
    else:
        raise ValueError(
            f"Could not create inference assets from model paths:\n{model_paths}\n"
            f"Detected types: {model_types}"
        )

    _check_crop_stage(assets, model_types)
    return assets, model_types