Skip to content

Commit a09f3e7

Browse files
committed
download function with test
1 parent 2fd2470 commit a09f3e7

File tree

10 files changed

+268
-227
lines changed

10 files changed

+268
-227
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
__pycache__/
33
*.py[cod]
44
*$py.class
5+
.DS_store
56

67
# C extensions
78
*.so

dlclibrary/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
Licensed under GNU Lesser General Public License v3.0
99
"""
1010

11-
from dlclibrary.metrics import pairwisedistances
11+
from dlclibrary.dlcmodelzoo.modelzoo_download import download_hugginface_model
12+
from dlclibrary.version import __version__, VERSION

dlclibrary/dlcmodelzoo/__init__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# just expand this list with new projects
2+
3+
Modeloptions = [
4+
"full_human",
5+
"full_cat",
6+
"primate_face",
7+
"mouse_pupil_vclose",
8+
"horse_sideview",
9+
"full_macaque",
10+
"superanimal_mouse",
11+
]
12+
13+
"""
14+
Modeloptions = [
15+
"full_human",
16+
"full_cat",
17+
"full_dog",
18+
"primate_face",
19+
"mouse_pupil_vclose",
20+
"horse_sideview",
21+
"full_macaque",
22+
"full_cheetah",
23+
] # just expand this list with new projects
24+
"""
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import os
2+
3+
# just expand this list when adding new models:
4+
MODELOPTIONS = [
5+
"full_human",
6+
"full_cat",
7+
"primate_face",
8+
"mouse_pupil_vclose",
9+
"horse_sideview",
10+
"full_macaque",
11+
"superanimal_mouse",
12+
]
13+
14+
15+
def get_dlclibrary_path():
16+
"""Get path of where dlclibrary (this repo) is currently running"""
17+
import importlib.util
18+
return os.path.split(importlib.util.find_spec("dlclibrary").origin)[0]
19+
20+
21+
def loadmodelnames():
22+
"""Load URLs and commits for available models"""
23+
from ruamel.yaml import YAML
24+
fn = os.path.join(get_dlclibrary_path(),"modelzoo_urls.yaml")
25+
with open(fn) as file:
26+
return YAML().load(file)
27+
28+
29+
def download_hugginface_model(modelname, target_dir,removeHFfolder=True):
30+
"""
31+
Downloads a DeepLabCut Model Zoo Project from Hugging Face
32+
"""
33+
from huggingface_hub import hf_hub_download
34+
import tarfile, os
35+
from pathlib import Path
36+
37+
neturls = loadmodelnames()
38+
39+
if modelname in neturls.keys():
40+
print("Loading....", modelname)
41+
url = neturls[modelname].split("/")
42+
repo_id, targzfn = url[0] + "/" + url[1], str(url[-1])
43+
44+
hf_hub_download(repo_id, targzfn, cache_dir=str(target_dir))
45+
# creates a new subfolder as indicated below, unzipping from there and deleting this folder
46+
47+
# Building the HuggingFaceHub download path:
48+
hf_path = (
49+
"models--"
50+
+ url[0]
51+
+ "--"
52+
+ url[1]
53+
+ "/snapshots/"
54+
+ str(neturls[modelname + "_commit"])
55+
+ "/"
56+
+ targzfn
57+
)
58+
59+
filename = os.path.join(target_dir, hf_path)
60+
with tarfile.open(filename, mode="r:gz") as tar:
61+
for member in tar:
62+
if not member.isdir():
63+
fname = Path(member.name).name # getting the filename
64+
tar.makefile(member, target_dir + "/" + fname)
65+
# tar.extractall(target_dir, members=tarfilenamecutting(tar))
66+
67+
if removeHFfolder:
68+
# Removing folder
69+
import shutil
70+
shutil.rmtree(
71+
Path(os.path.join(target_dir, "models--" + url[0] + "--" + url[1]))
72+
)
73+
74+
else:
75+
models = [fn for fn in neturls.keys()]
76+
print("Model does not exist: ", modelname)
77+
print("Pick one of the following: ", MODELOPTIONS)
78+
79+
80+
if __name__ == "__main__":
81+
print("Randomly downloading a model for testing...")
82+
83+
import random
84+
#modelname = 'full_cat'
85+
modelname = random.choice(MODELOPTIONS)
86+
87+
target_dir = '/Users/alex/Downloads' # folder has to exist!
88+
download_hugginface_model(modelname, target_dir)

dlclibrary/metrics/distances.py

Lines changed: 0 additions & 37 deletions
This file was deleted.

dlclibrary/metrics/map.py

Lines changed: 0 additions & 183 deletions
This file was deleted.

dlclibrary/modelzoo_urls.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
#Model Zoo from HuggingFace
3+
full_human: mwmathis/DeepLabCutModelZoo-DLC_human_fullbody_resnet_101/DLC_human_fullbody_resnet_101.tar.gz
4+
full_human_commit: 7438131c2ee7754a9732bef6fbf46b8189a16c48
5+
6+
primate_face: mwmathis/DeepLabCutModelZoo-primate_face/DLC_primate_face_resnet_50_iteration-1_shuffle-1.tar.gz
7+
primate_face_commit: df7e8f6ad38899489758e5a890b58905cfb4d893
8+
9+
mouse_pupil_vclose: mwmathis/DeepLabCutModelZoo-mouse_pupil_vclose/DLC_mouse_pupil_vclose_resnet_50_iteration-0_shuffle-1.tar.gz
10+
mouse_pupil_vclose_commit: 93557f0e74390596a2b9fddaab7628a1cc79fb2d
11+
12+
horse_sideview: mwmathis/DeepLabCutModelZoo-horse_sideview/DLC_Horses_resnet_50_iteration-1_shuffle-1.tar.gz
13+
horse_sideview_commit: fd0329b2ffc8fe7a5e6eb3d4850ebca75987e92c
14+
15+
full_macaque: mwmathis/DeepLabCutModelZoo-macaque_full/DLC_macaque_full_resnet50.tar.gz
16+
full_macaque_commit: 4c7ebf2628d5b7eb0483356595256fb01b7e1a9e
17+
18+
superanimal_mouse: mwmathis/DeepLabCutModelZoo-SuperAnimal-TopViewMouse/DLC_ma_supertopview5k_resnet_50_iteration-0_shuffle-1.tar.gz
19+
superanimal_mouse_commit: a7d7df40c3307a3c7a0ceeb2593d46a783235b28
20+
21+
full_cat: AlexEMG/DeepLabCutModelZoo-cat/DLC_Cat_resnet_50_iteration-0_shuffle-0.tar.gz
22+
full_cat_commit: 972f82dc575c820ce556e140465495a32d18ee9b

0 commit comments

Comments
 (0)