deeplabcut.pose_estimation_pytorch.apis.utils
Functions:
| Name | Description |
|---|---|
build_bboxes_dict_for_dataframe |
Creates a dictionary with bounding boxes from predictions. |
build_predictions_dataframe |
Builds a pandas DataFrame from pose prediction data. The resulting DataFrame |
ensure_multianimal_df_format |
Convert dataframe to 'multianimal' format (with an "individuals" columns index) |
get_detector_inference_runner |
Builds an inference runner for object detection. |
get_filtered_coco_detector_inference_runner |
Builds a detector inference runner using a pretrained COCO detector from |
get_inference_runners |
Builds the runners for pose estimation. |
get_model_snapshots |
Args: |
get_pose_inference_runner |
Builds an inference runner for pose estimation. |
get_scorer_name |
Get the scorer name for a particular PyTorch DeepLabCut shuffle. |
get_scorer_uid |
Args: |
parse_snapshot_index_for_analysis |
Gets the index of the snapshots to use for data analysis (e.g. video analysis) |
return_train_network_path |
Args: |
build_bboxes_dict_for_dataframe
build_bboxes_dict_for_dataframe(
predictions: dict[str, dict[str, ndarray]], image_name_to_index: Callable[[str], tuple[str, ...]] | None = None
) -> dict
Creates a dictionary with bounding boxes from predictions.
The keys of the dictionary are the same as the index of the dataframe created by build_predictions_dataframe. Therefore, the structures returned by build_predictions_dataframe and by build_bboxes_dict_for_dataframe can be accessed with the same keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict[str, dict[str, ndarray]]
|
Dictionary containing the evaluation results |
required |
|
Callable[[str], tuple[str, ...]] | None
|
a transform to apply on each image_name |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with sames keys as in the dataframe returned by build_predictions_dataframe, and respective bounding boxes and scores, if any. |
Source code in deeplabcut/pose_estimation_pytorch/apis/utils.py
build_predictions_dataframe
build_predictions_dataframe(
scorer: str,
predictions: dict[str, dict[str, ndarray]],
parameters: PoseDatasetParameters,
image_name_to_index: Callable[[str], tuple[str, ...]] | None = None,
) -> pd.DataFrame
Builds a pandas DataFrame from pose prediction data. The resulting DataFrame includes properly formatted indices and column names for compatibility with DeepLabCut workflows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The name of the scorer used to generate the predictions. |
required |
|
dict[str, dict[str, ndarray]]
|
A dictionary where each key is an image name and its value is another dictionary. The inner dictionary contains prediction data for "bodyparts" and optionally "unique_bodyparts". The "bodyparts" and "unique_bodyparts" data arrays are expected to be 3-dimensional, containing pose predictions in format (num_predicted_individuals, num_bodyparts, 3). |
required |
|
PoseDatasetParameters
|
Dataset-specific parameters required for constructing DataFrame columns. |
required |
|
Callable[[str], tuple[str, ...]] | None
|
A callable function that takes an image name and returns a tuple representing the DataFrame index. If None, indices will be generated without transformation. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
A pandas DataFrame containing the processed prediction data for all provided
images. The DataFrame index corresponds to the image names or their
transformed values (if |
Source code in deeplabcut/pose_estimation_pytorch/apis/utils.py
ensure_multianimal_df_format
Convert dataframe to 'multianimal' format (with an "individuals" columns index)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
DataFrame
|
the dataframe to convert |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
the dataframe in MA format |
Source code in deeplabcut/pose_estimation_pytorch/apis/utils.py
get_detector_inference_runner
get_detector_inference_runner(
model_config: dict,
snapshot_path: str | Path,
batch_size: int = 1,
device: str | None = None,
max_individuals: int | None = None,
transform: BaseCompose | None = None,
inference_cfg: InferenceConfig | dict | None = None,
min_bbox_score: float | None = None,
) -> DetectorInferenceRunner
Builds an inference runner for object detection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict
|
the pytorch configuration file |
required |
|
str | Path
|
the path of the snapshot from which to load the weights |
required |
|
int | None
|
the maximum number of individuals per image |
None
|
|
int
|
the batch size to use for the pose model. |
1
|
|
str | None
|
if defined, overwrites the device selection from the model config |
None
|
|
BaseCompose | None
|
the transform for pose estimation. if None, uses the transform defined in the config. |
None
|
|
InferenceConfig | dict | None
|
Configuration for the InferenceRunner. If None - uses the inference config defined in the model_config |
None
|
|
float | None
|
Minimum score threshold for filtering bounding boxes from the detector. Only bounding boxes with scores higher than this threshold are kept. If None, no filtering is applied. |
None
|
Returns:
| Type | Description |
|---|---|
DetectorInferenceRunner
|
an inference runner for object detection |
Source code in deeplabcut/pose_estimation_pytorch/apis/utils.py
get_filtered_coco_detector_inference_runner
get_filtered_coco_detector_inference_runner(
model_name: str,
category_id: int,
batch_size: int = 1,
device: str | None = None,
box_score_thresh: float = 0.6,
max_individuals: int | None = None,
color_mode: str | None = None,
model_config: dict | None = None,
transform: BaseCompose | None = None,
inference_cfg: InferenceConfig | dict | None = None,
min_bbox_score: float | None = None,
) -> DetectorInferenceRunner
Builds a detector inference runner using a pretrained COCO detector from torchvision.
This function loads a pretrained object detection model from torchvision.models.detection,
wraps it in a FilteredDetector that keeps only detections for a specified COCO category,
and packages it into a DetectorInferenceRunner ready for inference.
You can optionally provide a model configuration dictionary to resolve device, max_individuals,
and color_mode. If no model_config is given, these must be specified explicitly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Name of the torchvision detection model to load. Supported values include: "fasterrcnn_resnet50_fpn", "fasterrcnn_resnet50_fpn_v2", "fasterrcnn_mobilenet_v3_large_fpn". |
required |
|
int
|
The COCO category ID to retain in the detections. |
required |
|
int
|
Batch size for inference. Defaults to 1. |
1
|
|
str or None
|
Device to run the model on (e.g., "cuda", "cpu", or "mps"). If None, resolved from model_config or defaults to CUDA. |
None
|
|
float
|
Confidence threshold for filtering bounding boxes. Defaults to 0.6. |
0.6
|
|
int or None
|
Maximum number of individuals to retain per image. If None, resolved from model_config. |
None
|
|
str or None
|
Color mode used for preprocessing (e.g., "RGB"). If None, resolved from model_config. |
None
|
|
dict or None
|
Optional configuration dictionary used to resolve
|
None
|
|
BaseCompose or None
|
Optional preprocessing pipeline. If None, uses the model's default transform. |
None
|
|
InferenceConfig | dict | None
|
Configuration for the InferenceRunner. If None - uses the inference config defined in the model_config |
None
|
|
float or None
|
Minimum score threshold for filtering bounding boxes from the detector. Only bounding boxes with scores higher than this threshold are kept. If None, no filtering is applied. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
DetectorInferenceRunner |
DetectorInferenceRunner
|
A configured detector inference runner. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in deeplabcut/pose_estimation_pytorch/apis/utils.py
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 778 779 780 781 782 783 784 785 786 | |
get_inference_runners
get_inference_runners(
model_config: dict,
snapshot_path: str | Path,
max_individuals: int | None = None,
num_bodyparts: int | None = None,
num_unique_bodyparts: int | None = None,
batch_size: int = 1,
device: str | None = None,
with_identity: bool = False,
transform: BaseCompose | None = None,
detector_batch_size: int = 1,
detector_path: str | Path | None = None,
detector_transform: BaseCompose | None = None,
dynamic: DynamicCropper | None = None,
inference_cfg: InferenceConfig | dict | None = None,
min_bbox_score: float | None = None,
) -> tuple[InferenceRunner, InferenceRunner | None]
Builds the runners for pose estimation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict
|
the pytorch configuration file |
required |
|
str | Path
|
the path of the snapshot from which to load the weights |
required |
|
int | None
|
the maximum number of individuals per image (if None, uses the individuals defined in the model_config metadata) |
None
|
|
int | None
|
the number of bodyparts predicted by the model (if None, uses the bodyparts defined in the model_config metadata) |
None
|
|
int | None
|
the number of unique_bodyparts predicted by the model (if None, uses the unique bodyparts defined in the model_config metadata) |
None
|
|
int
|
the batch size to use for the pose model. |
1
|
|
bool
|
whether the pose model has an identity head |
False
|
|
str | None
|
if defined, overwrites the device selection from the model config |
None
|
|
BaseCompose | None
|
the transform for pose estimation. if None, uses the transform defined in the config. |
None
|
|
int
|
the batch size to use for the detector |
1
|
|
str | Path | None
|
the path to the detector snapshot from which to load weights, for top-down models (if a detector runner is needed) |
None
|
|
BaseCompose | None
|
the transform for object detection. if None, uses the transform defined in the config. |
None
|
|
DynamicCropper | None
|
The DynamicCropper used for video inference, or None if dynamic cropping should not be used. Only for bottom-up pose estimation models. Should only be used when creating inference runners for video pose estimation with batch size 1. |
None
|
|
InferenceConfig | dict | None
|
Configuration for the InferenceRunner. If None - uses the inference config defined in the model_config |
None
|
|
float | None
|
Minimum score threshold for filtering bounding boxes from the detector. Only bounding boxes with scores higher than this threshold are kept. If None, no filtering is applied. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[InferenceRunner, InferenceRunner | None]
|
a runner for pose estimation a runner for detection, if detector_path is not None |
Source code in deeplabcut/pose_estimation_pytorch/apis/utils.py
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 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 | |
get_model_snapshots
get_model_snapshots(
index: int | str, model_folder: Path, task: Task, snapshot_filter: list[str] | None = None
) -> list[Snapshot]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
int | str
|
Passing an index returns the snapshot with that index (where snapshots based on their number of training epochs, and the last snapshot is the "best" model based on validation metrics if one exists). Passing "best" returns the best snapshot from the training run. Passing "all" returns all snapshots. |
required |
|
Path
|
The path to the folder containing the snapshots |
required |
|
Task
|
The task for which to return the snapshot |
required |
|
list[str] | None
|
List of snapshot names to return (e.g. ["snapshot-50",
"snapshot-75"]). If defined, |
None
|
Returns:
| Type | Description |
|---|---|
list[Snapshot]
|
If index=="all", returns all snapshots. Otherwise, returns a list containing a single snapshot, with the desired index. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the index given is not valid |
ValueError
|
If index=="best" but there is no saved best model |
Source code in deeplabcut/pose_estimation_pytorch/apis/utils.py
get_pose_inference_runner
get_pose_inference_runner(
model_config: dict,
snapshot_path: str | Path,
batch_size: int = 1,
device: str | None = None,
max_individuals: int | None = None,
transform: BaseCompose | None = None,
dynamic: DynamicCropper | None = None,
cond_provider: CondFromModel | None = None,
ctd_tracking: bool | CTDTrackingConfig = False,
inference_cfg: InferenceConfig | dict | None = None,
) -> PoseInferenceRunner
Builds an inference runner for pose estimation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict
|
the pytorch configuration file |
required |
|
str | Path
|
the path of the snapshot from which to load the weights |
required |
|
int | None
|
the maximum number of individuals per image |
None
|
|
int
|
the batch size to use for the pose model. |
1
|
|
str | None
|
if defined, overwrites the device selection from the model config |
None
|
|
BaseCompose | None
|
the transform for pose estimation. if None, uses the transform defined in the config. |
None
|
|
DynamicCropper | None
|
The DynamicCropper used for video inference, or None if dynamic
cropping should not be used. Should only be used when creating inference
runners for video pose estimation with batch size 1. For top-down pose
estimation models, a |
None
|
|
CondFromModel | None
|
Only for CTD models. If None, the CondProvider is created from the pytorch_cfg. |
None
|
|
bool | CTDTrackingConfig
|
Only for CTD models. Conditional top-down models can be used to directly track individuals. Poses from frame T are given as conditions for frame T+1. This also means a BU model is only needed to "initialize" the pose in the first frame, and for the remaining frames only the CTD model is needed. To configure conditional pose tracking differently, you can pass a CTDTrackingConfig instance. |
False
|
|
InferenceConfig | dict | None
|
Configuration for the InferenceRunner. If None - uses the inference config defined in the model_config |
None
|
Returns:
| Type | Description |
|---|---|
PoseInferenceRunner
|
an inference runner for pose estimation |
Source code in deeplabcut/pose_estimation_pytorch/apis/utils.py
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 | |
get_scorer_name
get_scorer_name(
cfg: dict,
shuffle: int,
train_fraction: float,
snapshot_index: int | None = None,
detector_index: int | None = None,
snapshot_uid: str | None = None,
modelprefix: str = "",
) -> str
Get the scorer name for a particular PyTorch DeepLabCut shuffle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict
|
The project configuration. |
required |
|
int
|
The index of the shuffle for which to get the scorer |
required |
|
float
|
The training fraction for the shuffle. |
required |
|
int | None
|
The index of the snapshot used. If None, the value is loaded from the project's config.yaml file. |
None
|
|
int | None
|
For top-down models, the index of the detector used. If None, the value is loaded from the project's config.yaml file. |
None
|
|
str | None
|
If the snapshot_uid is not None, this value will be used instead of loading the snapshot and detector with given indices and calling utils.get_scorer_uid. |
None
|
|
str
|
The model prefix, if one was used. |
''
|
Returns:
| Type | Description |
|---|---|
str
|
the scorer name |
Source code in deeplabcut/pose_estimation_pytorch/apis/utils.py
get_scorer_uid
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Snapshot
|
the snapshot for which to get the scorer UID |
required |
|
Snapshot | None
|
if a top-down model is used with a detector, the detector snapshot for which to get the scorer UID |
required |
Returns:
| Type | Description |
|---|---|
str
|
the uid to use for the scorer |
Source code in deeplabcut/pose_estimation_pytorch/apis/utils.py
parse_snapshot_index_for_analysis
parse_snapshot_index_for_analysis(
cfg: dict, model_cfg: dict, snapshot_index: int | str | None, detector_snapshot_index: int | str | None
) -> tuple[int, int | None]
Gets the index of the snapshots to use for data analysis (e.g. video analysis)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict
|
The project configuration. |
required |
|
dict
|
The model configuration. |
required |
|
int | str | None
|
The index of the snapshot to use, if one was given by the user. |
required |
|
int | str | None
|
The index of the detector snapshot to use, if one was given by the user. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
snapshot_index |
tuple[int, int | None]
|
the snapshot index to use for analysis detector_snapshot_index: the detector index to use for analysis, or None if no detector should be used |
Source code in deeplabcut/pose_estimation_pytorch/apis/utils.py
return_train_network_path
return_train_network_path(
config: str, shuffle: int = 1, trainingsetindex: int = 0, modelprefix: str = ""
) -> tuple[Path, Path, Path]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Full path of the config.yaml file as a string. |
required |
|
int
|
The shuffle index to select for training |
1
|
|
int
|
Which TrainingsetFraction to use (note that TrainingFraction is a list in config.yaml) |
0
|
|
str
|
the modelprefix for the model |
''
|
Returns:
| Type | Description |
|---|---|
tuple[Path, Path, Path]
|
the path to the training pytorch pose configuration file the path to the test pytorch pose configuration file the path to the folder containing the snapshots |