Fix Space model downloads and torch deps
Browse filesAdd a Hugging Face Spaces pre-requirements file to force a newer pip before installing app dependencies, and switch PyTorch model loading to download SuperAnimal weights into a writable local directory instead of the read-only site-packages model cache. The requirements update also pins Spaces to CPU-only PyTorch wheels to avoid pulling unnecessary CUDA packages.
- pre-requirements.txt +1 -0
- pytorch_utils.py +20 -7
- requirements.txt +2 -0
pre-requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
pip>=26.2
|
pytorch_utils.py
CHANGED
|
@@ -1,10 +1,12 @@
|
|
| 1 |
import threading
|
|
|
|
| 2 |
|
| 3 |
import numpy as np
|
| 4 |
import PIL
|
| 5 |
from deeplabcut.pose_estimation_pytorch.apis.utils import get_inference_runners
|
| 6 |
from deeplabcut.pose_estimation_pytorch.config.pose import PoseConfig
|
| 7 |
-
from deeplabcut.pose_estimation_pytorch.modelzoo.utils import
|
|
|
|
| 8 |
|
| 9 |
# SuperAnimal (pose model, detector) used by the PyTorch backend
|
| 10 |
PYTORCH_MODELS = {
|
|
@@ -14,17 +16,28 @@ PYTORCH_MODELS = {
|
|
| 14 |
|
| 15 |
MAX_INDIVIDUALS = 10
|
| 16 |
MAX_IMAGE_SIZE = 1280 # longest side fed to the models (and drawn on)
|
|
|
|
|
|
|
| 17 |
|
| 18 |
_runners = {}
|
| 19 |
_build_lock = threading.Lock()
|
| 20 |
|
| 21 |
|
| 22 |
##########################################
|
| 23 |
-
def
|
| 24 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
with _build_lock:
|
| 29 |
if superanimal not in _runners:
|
| 30 |
pose_model, detector = PYTORCH_MODELS[superanimal]
|
|
@@ -39,8 +52,8 @@ def load_superanimal(superanimal, device="auto"):
|
|
| 39 |
cfg["detector"]["model"]["box_score_thresh"] = 0.05
|
| 40 |
pose_runner, detector_runner = get_inference_runners(
|
| 41 |
cfg,
|
| 42 |
-
snapshot_path=
|
| 43 |
-
detector_path=
|
| 44 |
max_individuals=MAX_INDIVIDUALS,
|
| 45 |
inference_cfg={"multithreading": {"enabled": False}},
|
| 46 |
)
|
|
|
|
| 1 |
import threading
|
| 2 |
+
from pathlib import Path
|
| 3 |
|
| 4 |
import numpy as np
|
| 5 |
import PIL
|
| 6 |
from deeplabcut.pose_estimation_pytorch.apis.utils import get_inference_runners
|
| 7 |
from deeplabcut.pose_estimation_pytorch.config.pose import PoseConfig
|
| 8 |
+
from deeplabcut.pose_estimation_pytorch.modelzoo.utils import MODEL_FILENAME_MAPPING
|
| 9 |
+
from dlclibrary import download_huggingface_model
|
| 10 |
|
| 11 |
# SuperAnimal (pose model, detector) used by the PyTorch backend
|
| 12 |
PYTORCH_MODELS = {
|
|
|
|
| 16 |
|
| 17 |
MAX_INDIVIDUALS = 10
|
| 18 |
MAX_IMAGE_SIZE = 1280 # longest side fed to the models (and drawn on)
|
| 19 |
+
# next to the TF models, not deeplabcut/modelzoo/checkpoints: site-packages is read-only for the Space's non-root user
|
| 20 |
+
WEIGHTS_DIR = Path(__file__).parent / "DLC_models" / "pytorch"
|
| 21 |
|
| 22 |
_runners = {}
|
| 23 |
_build_lock = threading.Lock()
|
| 24 |
|
| 25 |
|
| 26 |
##########################################
|
| 27 |
+
def snapshot_path(superanimal, model_name):
|
| 28 |
+
"""Path to a SuperAnimal snapshot in WEIGHTS_DIR, downloaded on first use (as deeplabcut does)."""
|
| 29 |
+
name = f"{superanimal}_{model_name}"
|
| 30 |
+
path = WEIGHTS_DIR / f"{name}.pt"
|
| 31 |
+
if not path.exists():
|
| 32 |
+
source = MODEL_FILENAME_MAPPING.get(name, path.name)
|
| 33 |
+
rename = None if source == path.name else {source: path.name}
|
| 34 |
+
download_huggingface_model(name, target_dir=str(WEIGHTS_DIR), rename_mapping=rename)
|
| 35 |
+
return path
|
| 36 |
|
| 37 |
+
|
| 38 |
+
##########################################
|
| 39 |
+
def load_superanimal(superanimal, device="auto"):
|
| 40 |
+
"""Build (once) the detector and pose runners for a SuperAnimal model; weights are downloaded on first use."""
|
| 41 |
with _build_lock:
|
| 42 |
if superanimal not in _runners:
|
| 43 |
pose_model, detector = PYTORCH_MODELS[superanimal]
|
|
|
|
| 52 |
cfg["detector"]["model"]["box_score_thresh"] = 0.05
|
| 53 |
pose_runner, detector_runner = get_inference_runners(
|
| 54 |
cfg,
|
| 55 |
+
snapshot_path=snapshot_path(superanimal, pose_model),
|
| 56 |
+
detector_path=snapshot_path(superanimal, detector),
|
| 57 |
max_individuals=MAX_INDIVIDUALS,
|
| 58 |
inference_cfg={"multithreading": {"enabled": False}},
|
| 59 |
)
|
requirements.txt
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
# Hugging Face Spaces install from this file; keep in sync with pyproject.toml dependencies.
|
|
|
|
|
|
|
| 2 |
gradio==6.29.0
|
| 3 |
gitpython>=3.1.30
|
| 4 |
seaborn
|
|
|
|
| 1 |
# Hugging Face Spaces install from this file; keep in sync with pyproject.toml dependencies.
|
| 2 |
+
# CPU-only PyTorch wheels: the Space has no GPU, and the default Linux wheels pull ~2-3 GB of CUDA libraries
|
| 3 |
+
--extra-index-url https://download.pytorch.org/whl/cpu
|
| 4 |
gradio==6.29.0
|
| 5 |
gitpython>=3.1.30
|
| 6 |
seaborn
|