Visitar URL original
Refactor `ctd_conditions` / `conditions_provider` + add `ConditionsConfig` + fix `InferenceConfig` by deruyter92 · Pull Request #3387 · DeepLabCut/DeepLabCut · GitHub
Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
dfaeabb
refactor ctd config (inference)
deruyter92 Jul 9, 2026
d698c85
refactor data/ctd.py: replace get_loader_and_snapshot with helper.
deruyter92 Jul 10, 2026
c13f25f
refactor data/ctd.py: remove ABC CondProvider
deruyter92 Jul 10, 2026
986ac8d
refactor ConditionsConfig: add build from shuffle
deruyter92 Jul 10, 2026
73f0d77
refactor ConditionsConfig: clean up build constructors
deruyter92 Jul 10, 2026
77807e2
Add affords_bu_inference property to ConditionsConfig
deruyter92 Jul 10, 2026
67229b9
refactor callers with ctd_conditions: use normalized ConditionsConfig…
deruyter92 Jul 10, 2026
0660100
refactor data/ctd.py and apis/ctd.py
deruyter92 Jul 10, 2026
8553294
add ConditionsShuffleConfig and `resolve_from_conditions()`
deruyter92 Jul 10, 2026
3ebd7b0
refactor analyze_images: clean normalization of ctd_conditions
deruyter92 Jul 14, 2026
1ed7e61
refactor analyze_image_folder (only accept resolved cond_provider)
deruyter92 Jul 14, 2026
35cf90a
refactor analyze_videos and analyze_images (strict ConditionsShuffleC…
deruyter92 Jul 14, 2026
38cca93
add missing shuffle literal
deruyter92 Jul 14, 2026
b87c970
Fix load_conditions_for_evaluation: add support for Typed ConditionsC…
deruyter92 Jul 14, 2026
3592b16
Improve docstrings and function signatures
deruyter92 Jul 14, 2026
03036d9
add tests for ctd conditions configurations
deruyter92 Jul 14, 2026
047dfa4
remove unused property "affords_bu_inference"
deruyter92 Jul 14, 2026
c37fc07
refactor tests_ctd_conditions, improve variable names and fix circula…
deruyter92 Jul 14, 2026
9642add
update test_ctd_conditions
deruyter92 Jul 14, 2026
7a36f4a
Merge branch 'main' into jaap/fix_ctd_conditions_config
deruyter92 Jul 20, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor data/ctd.py: replace get_loader_and_snapshot with helper.
  • Loading branch information
deruyter92 committed Jul 10, 2026
commit d698c85abeba09a13de6f509caeaaf89361ab574
98 changes: 65 additions & 33 deletions deeplabcut/pose_estimation_pytorch/data/ctd.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,66 @@
from deeplabcut.pose_estimation_pytorch.task import Task


def resolve_bu_shuffle(
config: str | Path,
shuffle: int,
trainset_index: int = 0,
modelprefix: str = "",
snapshot: str | None = None,
snapshot_index: int | None = None,
) -> tuple[DLCLoader, Snapshot]:
"""Resolves a DLC BU shuffle to a (loader, snapshot) pair.

Args:
config: Path to the DeepLabCut project config.
shuffle: The index of the shuffle for which to load data.
trainset_index: The index of the TrainingsetFraction for which to load data.
modelprefix: The modelprefix for the shuffle.
snapshot: The name of the snapshot to use. Takes priority over snapshot_index.
snapshot_index: The index of the snapshot to use. Defaults to -1 (last).

Returns:
loader: The DLCLoader for the BU shuffle.
snapshot: The BU Snapshot to use for conditions.

Raises:
ValueError: If the given shuffle is not for a BU model.
"""
loader = DLCLoader(
config,
trainset_index=trainset_index,
shuffle=shuffle,
modelprefix=modelprefix,
)
if loader.pose_task != Task.BOTTOM_UP:
raise ValueError(
"Conditions can only be loaded from shuffles for bottom-up models, but "
f"shuffle {shuffle} has task {loader.pose_task} (config={config}, "
f"trainset_index={trainset_index}, modelprefix={modelprefix})."
)

if snapshot is not None:
snapshot_path = loader.model_folder / snapshot
if not snapshot_path.exists():
raise ValueError(f"Snapshot file {snapshot_path} does not exist.")
bu_snapshot = Snapshot.from_path(snapshot_path)
else:
if snapshot_index is None:
snapshot_index = -1

snapshots = loader.snapshots()
if len(snapshots) == 0:
raise ValueError(f"No snapshots found for shuffle={shuffle} in {loader.model_folder}")

if snapshot_index > len(snapshots):
snapshot_str = "\n".join([f" {i}: {s.path.name}" for i, s in enumerate(snapshots)])
raise ValueError(f"Snapshot index {snapshot_index} is out of range. Existing snapshots: {snapshot_str}")

bu_snapshot = snapshots[snapshot_index]

return loader, bu_snapshot


class CondProvider(ABC):
"""A class providing conditions for a CTD model."""

Expand All @@ -39,8 +99,6 @@ def get_loader_and_snapshot(
) -> tuple[DLCLoader, Snapshot]:
"""Creates a DLCLoader for the BU shuffle and the path to conditions snapshot.

One of `snapshot` or `snapshot_index` must be provided.

Args:
config: Path to the DeepLabCut project config, or the project config itself
trainset_index: The index of the TrainingsetFraction for which to load data
Expand All @@ -57,40 +115,14 @@ def get_loader_and_snapshot(
Raises:
ValueError: If the given shuffle is not for a BU model.
"""
loader = DLCLoader(
config,
trainset_index=trainset_index,
return resolve_bu_shuffle(
config=config,
shuffle=shuffle,
trainset_index=trainset_index,
modelprefix=modelprefix,
snapshot=snapshot,
snapshot_index=snapshot_index,
)
if loader.pose_task != Task.BOTTOM_UP:
raise ValueError(
"Conditions can only be loaded from shuffles for bottom-up models, but "
f"shuffle {shuffle} has task {loader.pose_task} (config={config}, "
f"trainset_index={trainset_index}, modelprefix={modelprefix})."
)

if snapshot is not None:
snapshot_path = loader.model_folder / snapshot
if not snapshot_path.exists():
raise ValueError(f"Snapshot file {snapshot_path} does not exist.")
bu_snapshot = Snapshot.from_path(snapshot_path)

else:
if snapshot_index is None:
snapshot_index = -1

snapshots = loader.snapshots()
if len(snapshots) == 0:
raise ValueError(f"No snapshots found for shuffle={shuffle} in {loader.model_folder}")

if snapshot_index > len(snapshots):
snapshot_str = "\n".join([f" {i}: {s.path.name}" for i, s in enumerate(snapshots)])
raise ValueError(f"Snapshot index {snapshot_index} is out of range. Existing snapshots: {snapshot_str}")

bu_snapshot = snapshots[snapshot_index]

return loader, bu_snapshot


class CondFromFile(CondProvider):
Expand Down