sabrinabenas commited on
Commit
9183030
β€’
1 Parent(s): f577f91

add url models

Browse files
Files changed (3) hide show
  1. app.py +11 -10
  2. model/models.py +60 -0
  3. model/pretrained_model_urls.yaml +9 -0
app.py CHANGED
@@ -18,6 +18,7 @@ import math
18
  # import json
19
  import os
20
  import yaml
 
21
 
22
  import pdb
23
 
@@ -37,7 +38,7 @@ DLC_models = {'full_cat': "model/DLC_Cat_resnet_50_iteration-0_shuffle-0",
37
  'primate_face': "model/DLC_FacialLandmarks_resnet_50_iteration-1_shuffle-1",
38
  'full_human': "model/DLC_human_dancing_resnet_101_iteration-0_shuffle-1",
39
  'full_macaque': 'model/DLC_monkey_resnet_50_iteration-0_shuffle-1'}
40
-
41
  #########################################
42
  # Draw keypoints on image
43
  def draw_keypoints_on_image(image,
@@ -78,15 +79,15 @@ def draw_keypoints_on_image(image,
78
  keypoints_x = tuple([im_width * x for x in keypoints_x])
79
  keypoints_y = tuple([im_height * y for y in keypoints_y])
80
 
81
- cmap = matplotlib.cm.get_cmap('viridis')
82
  cmap2 = matplotlib.cm.get_cmap('Greys')
83
  # draw ellipses around keypoints
84
  for i, (keypoint_x, keypoint_y) in enumerate(zip(keypoints_x, keypoints_y)):
85
- round_fill = [round(num*255) for num in list(cmap(i*len(keypoints_x)))[:3]]
86
  round_outline = [round(num*255) for num in list(cmap2(alpha[i]))[:3]]
87
  draw.ellipse([(keypoint_x - marker_size, keypoint_y - marker_size),
88
  (keypoint_x + marker_size, keypoint_y + marker_size)],
89
- fill=tuple(round_fill), outline= tuple(round_outline), width=3)
90
 
91
  # add string labels around keypoints
92
  if flag_show_str_labels:
@@ -160,7 +161,7 @@ def predict_dlc(list_np_crops,
160
  kpts_likelihood_th,
161
  DLCmodel,
162
  dlc_proc):
163
-
164
  # run dlc thru list of crops
165
  dlc_live = DLCLive(DLCmodel, processor=dlc_proc)
166
  dlc_live.init_inference(list_np_crops[0])
@@ -198,9 +199,9 @@ def predict_pipeline(img_input,
198
  ## Get DLC model and labels as strings
199
  # TODO: make a dict as for megadetector
200
  # pdb.set_trace()
201
- path_to_DLCmodel = DLC_models[dlc_model_input_str]
202
- pose_cfg_path = os.path.join(path_to_DLCmodel,'pose_cfg.yaml')
203
-
204
  # extract map label ids to strings
205
  # pose_cfg_dict['all_joints'] is a list of one-element lists,
206
  with open(pose_cfg_path, "r") as stream:
@@ -290,7 +291,7 @@ gr_image_input = gr.inputs.Image(type="pil", label="Input Image")
290
 
291
 
292
  # Models
293
- gr_dlc_model_input = gr.inputs.Dropdown(choices=list(DLC_models.keys()), # choices
294
  default='full_cat', # default option
295
  type='value', # Type of value to be returned by component. "value" returns the string of the choice selected, "index" returns the index of the choice selected.
296
  label='Select DeepLabCut model')
@@ -318,7 +319,7 @@ gr_labels_font_style = gr.inputs.Dropdown(choices=['amiko', 'nature', 'painter',
318
  label='Select keypoint label font')
319
  gr_slider_font_size = gr.inputs.Slider(5,30,1,8,
320
  label='Set font size')
321
- gr_slider_marker_size = gr.inputs.Slider(1,50,10,10,
322
  label='Set marker size')
323
 
324
  # list of inputs
 
18
  # import json
19
  import os
20
  import yaml
21
+ from model.models import DownloadModel
22
 
23
  import pdb
24
 
 
38
  'primate_face': "model/DLC_FacialLandmarks_resnet_50_iteration-1_shuffle-1",
39
  'full_human': "model/DLC_human_dancing_resnet_101_iteration-0_shuffle-1",
40
  'full_macaque': 'model/DLC_monkey_resnet_50_iteration-0_shuffle-1'}
41
+ DLC_models_list = ['full_cat', 'full_dog','primate_face', 'full_human', 'full_macaque']
42
  #########################################
43
  # Draw keypoints on image
44
  def draw_keypoints_on_image(image,
 
79
  keypoints_x = tuple([im_width * x for x in keypoints_x])
80
  keypoints_y = tuple([im_height * y for y in keypoints_y])
81
 
82
+ cmap = matplotlib.cm.get_cmap('hsv')
83
  cmap2 = matplotlib.cm.get_cmap('Greys')
84
  # draw ellipses around keypoints
85
  for i, (keypoint_x, keypoint_y) in enumerate(zip(keypoints_x, keypoints_y)):
86
+ round_fill = [round(num*255) for num in list(cmap(i*10))[:3]] #check!
87
  round_outline = [round(num*255) for num in list(cmap2(alpha[i]))[:3]]
88
  draw.ellipse([(keypoint_x - marker_size, keypoint_y - marker_size),
89
  (keypoint_x + marker_size, keypoint_y + marker_size)],
90
+ fill=tuple(round_fill), outline= tuple(round_outline), width=2) #fill and outline: [0,255]
91
 
92
  # add string labels around keypoints
93
  if flag_show_str_labels:
 
161
  kpts_likelihood_th,
162
  DLCmodel,
163
  dlc_proc):
164
+ print(DLCmodel)
165
  # run dlc thru list of crops
166
  dlc_live = DLCLive(DLCmodel, processor=dlc_proc)
167
  dlc_live.init_inference(list_np_crops[0])
 
199
  ## Get DLC model and labels as strings
200
  # TODO: make a dict as for megadetector
201
  # pdb.set_trace()
202
+ path_to_DLCmodel = DownloadModel(dlc_model_input_str, '/home/sabrina/MegaDetector_DeepLabCut/model/')
203
+ pose_cfg_path = '/home/sabrina/MegaDetector_DeepLabCut/model/pose_cfg.yaml'
204
+ #pdb.set_trece()
205
  # extract map label ids to strings
206
  # pose_cfg_dict['all_joints'] is a list of one-element lists,
207
  with open(pose_cfg_path, "r") as stream:
 
291
 
292
 
293
  # Models
294
+ gr_dlc_model_input = gr.inputs.Dropdown(choices=list(DLC_models_list), # choices
295
  default='full_cat', # default option
296
  type='value', # Type of value to be returned by component. "value" returns the string of the choice selected, "index" returns the index of the choice selected.
297
  label='Select DeepLabCut model')
 
319
  label='Select keypoint label font')
320
  gr_slider_font_size = gr.inputs.Slider(5,30,1,8,
321
  label='Set font size')
322
+ gr_slider_marker_size = gr.inputs.Slider(1,20,1,5,
323
  label='Set marker size')
324
 
325
  # list of inputs
model/models.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import urllib.request
2
+ import tarfile
3
+ from tqdm import tqdm
4
+ import os
5
+ import yaml
6
+ from ruamel.yaml import YAML
7
+
8
+ def read_plainconfig(configname):
9
+ if not os.path.exists(configname):
10
+ raise FileNotFoundError(
11
+ f"Config {configname} is not found. Please make sure that the file exists."
12
+ )
13
+ with open(configname) as file:
14
+ return YAML().load(file)
15
+
16
+ def DownloadModel(modelname, target_dir):
17
+ """
18
+ Downloads a DeepLabCut Model Zoo Project
19
+ """
20
+
21
+ def show_progress(count, block_size, total_size):
22
+ pbar.update(block_size)
23
+
24
+ def tarfilenamecutting(tarf):
25
+ """' auxfun to extract folder path
26
+ ie. /xyz-trainsetxyshufflez/
27
+ """
28
+ for memberid, member in enumerate(tarf.getmembers()):
29
+ if memberid == 0:
30
+ parent = str(member.path)
31
+ l = len(parent) + 1
32
+ if member.path.startswith(parent):
33
+ member.path = member.path[l:]
34
+ yield member
35
+
36
+ neturls = read_plainconfig("/home/sabrina/MegaDetector_DeepLabCut/model/pretrained_model_urls.yaml") #FIXME
37
+
38
+ if modelname in neturls.keys():
39
+ url = neturls[modelname]
40
+ print(url)
41
+ response = urllib.request.urlopen(url)
42
+ print(
43
+ "Downloading the model from the DeepLabCut server @Harvard -> Go Crimson!!! {}....".format(
44
+ url
45
+ )
46
+ )
47
+ total_size = int(response.getheader("Content-Length"))
48
+ pbar = tqdm(unit="B", total=total_size, position=0)
49
+ filename, _ = urllib.request.urlretrieve(url, reporthook=show_progress)
50
+ with tarfile.open(filename, mode="r:gz") as tar:
51
+ tar.extractall(target_dir, members=tarfilenamecutting(tar))
52
+ else:
53
+ models = [
54
+ fn
55
+ for fn in neturls.keys()
56
+ if "resnet_" not in fn and "mobilenet_" not in fn
57
+ ]
58
+ print("Model does not exist: ", modelname)
59
+ print("Pick one of the following: ", models)
60
+ return target_dir
model/pretrained_model_urls.yaml ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ #Model Zoo from where the Charles flow(s)
2
+ full_human: http://deeplabcut.rowland.harvard.edu/models/DLC_human_fullbody_resnet_101.tar.gz
3
+ full_dog: http://deeplabcut.rowland.harvard.edu/models/DLC_Dog_resnet_50_iteration-0_shuffle-0.tar.gz
4
+ full_cat: http://deeplabcut.rowland.harvard.edu/models/DLC_Cat_resnet_50_iteration-0_shuffle-0.tar.gz
5
+ primate_face: http://deeplabcut.rowland.harvard.edu/models/DLC_primate_face_resnet_50_iteration-1_shuffle-1.tar.gz
6
+ mouse_pupil_vclose: http://deeplabcut.rowland.harvard.edu/models/DLC_mouse_pupil_vclose_resnet_50_iteration-0_shuffle-1.tar.gz
7
+ horse_sideview: http://deeplabcut.rowland.harvard.edu/models/DLC_Horses_resnet_50_iteration-1_shuffle-1.tar.gz
8
+ full_macaque: http://deeplabcut.rowland.harvard.edu/models/DLC_macaque_full_resnet50.tar.gz
9
+ full_cheetah: http://deeplabcut.rowland.harvard.edu/models/DLC_full_cheetah_resnet_152.tar.