Skip to content

Commit 15c13ef

Browse files
authored
Merge pull request #24 from DeepLabCut/update_urls
Add DLC3 detectors and pose estimation models
2 parents 7790cc2 + fa502e9 commit 15c13ef

File tree

2 files changed

+59
-45
lines changed

2 files changed

+59
-45
lines changed

dlclibrary/dlcmodelzoo/modelzoo_download.py

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#
99
# Licensed under GNU Lesser General Public License v3.0
1010
#
11+
from __future__ import annotations
12+
1113
import json
1214
import os
1315

@@ -20,10 +22,10 @@
2022
"mouse_pupil_vclose",
2123
"horse_sideview",
2224
"full_macaque",
23-
"superanimal_topviewmouse",
24-
"superanimal_quadruped",
25-
"superanimal_quadruped_HRNetw32",
26-
"superanimal_quadruped_ATP",
25+
"superanimal_topviewmouse_dlcrnet",
26+
"superanimal_quadruped_dlcrnet",
27+
"superanimal_topviewmouse_hrnetw32",
28+
"superanimal_quadruped_hrnetw32",
2729
]
2830

2931

@@ -50,7 +52,9 @@ def parse_available_supermodels():
5052
return json.load(file)
5153

5254

53-
def download_huggingface_model(modelname, target_dir=".", remove_hf_folder=True):
55+
def download_huggingface_model(
56+
modelname, target_dir=".", remove_hf_folder=True, rename_mapping: dict | None = None
57+
):
5458
"""
5559
Download a DeepLabCut Model Zoo Project from Hugging Face
5660
@@ -62,41 +66,58 @@ def download_huggingface_model(modelname, target_dir=".", remove_hf_folder=True)
6266
Directory where to store the model weights and pose_cfg.yaml file
6367
remove_hf_folder : bool, default True
6468
Whether to remove the directory structure provided by HuggingFace after downloading and decompressing data into DeepLabCut format.
69+
rename_mapping : dict, default None
70+
Dictionary to rename the downloaded file. If None, the original filename is used.
6571
"""
6672
from huggingface_hub import hf_hub_download
6773
import tarfile
6874
from pathlib import Path
75+
from ruamel.yaml.comments import CommentedBase
6976

7077
neturls = _load_model_names()
7178
if modelname not in neturls:
7279
raise ValueError(f"`modelname` should be one of: {', '.join(modelname)}.")
7380

7481
print("Loading....", modelname)
75-
url = neturls[modelname].split("/")
76-
repo_id, targzfn = url[0] + "/" + url[1], str(url[-1])
77-
78-
hf_hub_download(repo_id, targzfn, cache_dir=str(target_dir))
79-
80-
# Create a new subfolder as indicated below, unzipping from there and deleting this folder
81-
hf_folder = f"models--{url[0]}--{url[1]}"
82-
hf_path = os.path.join(
83-
hf_folder,
84-
"snapshots",
85-
str(neturls[modelname + "_commit"]),
86-
targzfn,
87-
)
88-
89-
filename = os.path.join(target_dir, hf_path)
90-
try:
91-
with tarfile.open(filename, mode="r:gz") as tar:
92-
for member in tar:
93-
if not member.isdir():
94-
fname = Path(member.name).name
95-
tar.makefile(member, os.path.join(target_dir, fname))
96-
except tarfile.ReadError: # The model is a .pt file
97-
os.rename(filename, os.path.join(target_dir, targzfn))
82+
urls = neturls[modelname]
83+
if isinstance(urls, CommentedBase):
84+
urls = list(urls)
85+
else:
86+
urls = [urls]
87+
88+
if not os.path.isabs(target_dir):
89+
target_dir = os.path.abspath(target_dir)
90+
91+
for url in urls:
92+
url = url.split("/")
93+
repo_id, targzfn = url[0] + "/" + url[1], str(url[-1])
94+
95+
hf_hub_download(repo_id, targzfn, cache_dir=str(target_dir))
96+
97+
# Create a new subfolder as indicated below, unzipping from there and deleting this folder
98+
hf_folder = f"models--{url[0]}--{url[1]}"
99+
path_ = os.path.join(target_dir, hf_folder, "snapshots")
100+
commit = os.listdir(path_)[0]
101+
filename = os.path.join(path_, commit, targzfn)
102+
try:
103+
with tarfile.open(filename, mode="r:gz") as tar:
104+
for member in tar:
105+
if not member.isdir():
106+
fname = Path(member.name).name
107+
tar.makefile(member, os.path.join(target_dir, fname))
108+
except tarfile.ReadError: # The model is a .pt file
109+
if rename_mapping is not None:
110+
targzfn = rename_mapping.get(targzfn, targzfn)
111+
if os.path.islink(filename):
112+
filename_ = os.readlink(filename)
113+
if not os.path.isabs(filename_):
114+
filename_ = os.path.abspath(os.path.join(os.path.dirname(filename), filename_))
115+
filename = filename_
116+
os.rename(filename, os.path.join(target_dir, targzfn))
98117

99118
if remove_hf_folder:
100119
import shutil
101120

102121
shutil.rmtree(os.path.join(target_dir, hf_folder))
122+
123+
'../../blobs/6c9c66d48f25cac9f8adaea7a485b07f4bd781ba656785bc4e077d9064e8e5df'

dlclibrary/dlcmodelzoo/modelzoo_urls.yaml

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,29 @@
99
## Licensed under GNU Lesser General Public License v3.0
1010
##
1111

12-
13-
1412
#Model Zoo from HuggingFace
1513
full_human: mwmathis/DeepLabCutModelZoo-DLC_human_fullbody_resnet_101/DLC_human_dancing_resnet_101_iteration-0_shuffle-1.tar.gz
16-
full_human_commit: 01acf989bfec7a25110b94677449d9e861b5ea81
14+
1715
full_cat: AlexEMG/DeepLabCutModelZoo-cat/DLC_Cat_resnet_50_iteration-0_shuffle-0.tar.gz
18-
full_cat_commit: d39b3922a04d7077a84a05d141243c044cc18af6
16+
1917
full_dog: AlexEMG/DeepLabCutModelZoo-dog/DLC_Dog_resnet_50_iteration-0_shuffle-0.tar.gz
20-
full_dog_commit: b420a859f881b3cabdfd0cc4a0eb2d1c7fb592c5
2118

2219
primate_face: mwmathis/DeepLabCutModelZoo-primate_face/DLC_primate_face_resnet_50_iteration-1_shuffle-1.tar.gz
23-
primate_face_commit: df7e8f6ad38899489758e5a890b58905cfb4d893
2420

2521
mouse_pupil_vclose: mwmathis/DeepLabCutModelZoo-mouse_pupil_vclose/DLC_mouse_pupil_vclose_resnet_50_iteration-0_shuffle-1.tar.gz
26-
mouse_pupil_vclose_commit: 93557f0e74390596a2b9fddaab7628a1cc79fb2d
2722

2823
horse_sideview: mwmathis/DeepLabCutModelZoo-horse_sideview/DLC_Horses_resnet_50_iteration-1_shuffle-1.tar.gz
29-
horse_sideview_commit: fd0329b2ffc8fe7a5e6eb3d4850ebca75987e92c
3024

3125
full_macaque: mwmathis/DeepLabCutModelZoo-macaque_full/DLC_macaque_full_resnet50.tar.gz
32-
full_macaque_commit: 4c7ebf2628d5b7eb0483356595256fb01b7e1a9e
3326

34-
superanimal_topviewmouse: mwmathis/DeepLabCutModelZoo-SuperAnimal-TopViewMouse/DLC_ma_supertopview5k_resnet_50_iteration-0_shuffle-1.tar.gz
35-
superanimal_topviewmouse_commit: 1781f384b56ddb9b58c0d4acae98993b9a17e901
27+
superanimal_topviewmouse_dlcrnet: mwmathis/DeepLabCutModelZoo-SuperAnimal-TopViewMouse/DLC_ma_supertopview5k_resnet_50_iteration-0_shuffle-1.tar.gz
3628

37-
superanimal_quadruped: mwmathis/DeepLabCutModelZoo-SuperAnimal-Quadruped/DLC_ma_superquadruped_resnet_50_iteration-0_shuffle-1.tar.gz
38-
superanimal_quadruped_commit: 673140e6dd9f7be492d77cab957f31c73a192f67
29+
superanimal_quadruped_dlcrnet: mwmathis/DeepLabCutModelZoo-SuperAnimal-Quadruped_DLC2/DLC_ma_superquadruped_resnet_50_iteration-0_shuffle-1.tar.gz
3930

40-
superanimal_quadruped_HRNetw32: mwmathis/SuperAnimal_Quadruped_pt/blob/main/SuperAnimal_Quadruped_pt_HRNetw32.pth
41-
superanimal_quadruped_HRNetw32_commit: b3d7ddf5f82922a57fb7d7388c292e7b357add18
31+
superanimal_topviewmouse_hrnetw32:
32+
- mwmathis/DeepLabCutModelZoo-SuperAnimal-TopViewMouse/pose_model.pth
33+
- mwmathis/DeepLabCutModelZoo-SuperAnimal-TopViewMouse/detector.pt
4234

43-
superanimal_quadruped_ATP: mwmathis/SuperAnimal_quadruped_ATP_pt/blob/main/AnimalTokenPose_SplitD.pth
44-
superanimal_quadruped_ATP_commit: 41186b920187e92b897a6483b2a4f053a8ef3700
35+
superanimal_quadruped_hrnetw32:
36+
- mwmathis/DeepLabCutModelZoo-SuperAnimal-Quadruped/pose_model.pth
37+
- mwmathis/DeepLabCutModelZoo-SuperAnimal-Quadruped/detector.pt

0 commit comments

Comments
 (0)