Skip to content

deeplabcut.pose_estimation_pytorch.utils

Functions:

Name Description
fix_seeds

Fixes the random seed for python, numpy and pytorch.

resolve_device

Determines which device should be used from the model config.

fix_seeds

fix_seeds(seed: int) -> None

Fixes the random seed for python, numpy and pytorch.

Parameters:

Name Type Description Default

seed

int

the seed to set

required
Source code in deeplabcut/pose_estimation_pytorch/utils.py
def fix_seeds(seed: int) -> None:
    """Fixes the random seed for python, numpy and pytorch.

    Args:
        seed: the seed to set
    """
    random.seed(seed)
    torch.manual_seed(seed)
    np.random.seed(seed)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False

resolve_device

resolve_device(model_config: PoseConfig | DetectorConfig) -> str

Determines which device should be used from the model config.

When the device is set to 'auto': If an Nvidia GPU is available, selects the device as cuda:0. Selects 'mps' if available (on macOS) and the net type is compatible. Otherwise, returns 'cpu'. Otherwise, simply returns the selected device

Parameters:

Name Type Description Default

model_config

PoseConfig | dict | str | Path

The PyTorch pose configuration.

required

Returns:

Type Description
str

the device on which training should be run

Source code in deeplabcut/pose_estimation_pytorch/utils.py
def resolve_device(model_config: PoseConfig | DetectorConfig) -> str:
    """Determines which device should be used from the model config.

    When the device is set to 'auto':
        If an Nvidia GPU is available, selects the device as cuda:0.
        Selects 'mps' if available (on macOS) and the net type is compatible.
        Otherwise, returns 'cpu'.
    Otherwise, simply returns the selected device

    Args:
        model_config (PoseConfig | dict | str | Path): The PyTorch pose configuration.

    Returns:
        the device on which training should be run
    """
    device = model_config.device

    if isinstance(model_config, DetectorConfig):
        supports_mps = False
    else:
        supports_mps = "resnet" in model_config.get("net_type", "")

    if device == "auto":
        if torch.cuda.is_available():
            return "cuda"
        elif supports_mps and torch.backends.mps.is_available():
            return "mps"
        return "cpu"
    return device