chaerinmin commited on
Commit
c683e32
·
1 Parent(s): 8f16411

no mask as default. add toggle to enable mask

Browse files
Files changed (2) hide show
  1. app.py +44 -29
  2. sbatch/sbatch_demo2.sh +40 -0
app.py CHANGED
@@ -18,6 +18,7 @@ from PIL import Image
18
  import random
19
  from copy import deepcopy
20
  from huggingface_hub import hf_hub_download
 
21
  try:
22
  import spaces
23
  except:
@@ -30,7 +31,6 @@ LENGTH = 480
30
  placeholder = cv2.cvtColor(cv2.imread("placeholder.png"), cv2.COLOR_BGR2RGB)
31
  NEW_MODEL = True
32
  MODEL_EPOCH = 6
33
- NO_MASK = False
34
  HF = False
35
  pre_device = "cpu" if HF else "cuda"
36
  spaces_60_fn = spaces.GPU(duration=60) if HF else (lambda f: f)
@@ -282,30 +282,34 @@ def prepare_anno(ref, ref_is_user):
282
  else:
283
  return img, None
284
 
285
- def get_ref_anno(img, keypts):
 
286
  if img.sum() == 0: # clear_all
287
  return None, gr.update(), None, gr.update(), True
288
  elif keypts is None: # hand not detected
289
  no_hands = cv2.resize(np.array(Image.open("no_hands.png"))[..., :3], (LENGTH, LENGTH))
290
  return None, no_hands, None, no_hands_open, False
291
  missing_keys, extra_keys = autoencoder.load_state_dict(vae_state_dict, strict=False)
292
- if isinstance(keypts, list):
293
- if len(keypts[0]) == 0:
294
- keypts[0] = np.zeros((21, 2))
295
- elif len(keypts[0]) == 21:
296
- keypts[0] = np.array(keypts[0], dtype=np.float32)
297
- else:
298
- gr.Info("Number of right hand keypoints should be either 0 or 21.")
299
- return None, None, None, gr.update(), gr.update()
300
- if len(keypts[1]) == 0:
301
- keypts[1] = np.zeros((21, 2))
302
- elif len(keypts[1]) == 21:
303
- keypts[1] = np.array(keypts[1], dtype=np.float32)
304
- else:
305
- gr.Info("Number of left hand keypoints should be either 0 or 21.")
306
- return None, None, None, gr.update(), gr.update()
307
- keypts = np.concatenate(keypts, axis=0)
308
- if NO_MASK:
 
 
 
309
  hand_mask = np.zeros_like(img[:,:, 0])
310
  ref_pose = visualize_hand(keypts, img)
311
  else:
@@ -391,7 +395,7 @@ def get_ref_anno(img, keypts):
391
  print(f"autoencoder encoder before operating dtype: {next(autoencoder.encoder.parameters()).dtype}")
392
  latent = opts.latent_scaling_factor * autoencoder.encode(image).sample()
393
  print(f"latent.max(): {latent.max()}, latent.min(): {latent.min()}")
394
- if NO_MASK:
395
  mask = torch.zeros_like(mask)
396
  print(f"heatmaps.max(): {heatmaps.max()}, heatmaps.min(): {heatmaps.min()}")
397
  print(f"mask.max(): {mask.max()}, mask.min(): {mask.min()}")
@@ -921,6 +925,7 @@ def clear_all():
921
  42,
922
  3.0,
923
  gr.update(interactive=False),
 
924
  )
925
 
926
  def fix_clear_all():
@@ -1365,6 +1370,9 @@ with gr.Blocks(css=custom_css, theme="soft") as demo:
1365
  target_auto_cond = gr.State(value=None)
1366
  target_cond = gr.State(value=None)
1367
 
 
 
 
1368
  # main tabs
1369
  with gr.Row():
1370
  # ref column
@@ -1386,7 +1394,8 @@ with gr.Blocks(css=custom_css, theme="soft") as demo:
1386
  crop_size="1:1",
1387
  )
1388
  gr.Examples(example_ref_imgs, [ref], examples_per_page=20)
1389
- with gr.Accordion(label="See hand pose", open=False):
 
1390
  with gr.Tab("Automatic hand keypoints"):
1391
  ref_pose = gr.Image(
1392
  type="numpy",
@@ -1478,9 +1487,9 @@ with gr.Blocks(css=custom_css, theme="soft") as demo:
1478
  interactive=False,
1479
  visible=True
1480
  )
1481
- gr.Markdown(
1482
- """<p style="text-align: center;">Optionally flip the hand</p>"""
1483
- )
1484
  ref_flip = gr.Checkbox(
1485
  value=False, label="Flip Handedness (Reference)", interactive=False
1486
  )
@@ -1596,9 +1605,9 @@ with gr.Blocks(css=custom_css, theme="soft") as demo:
1596
  interactive=False,
1597
  visible=True
1598
  )
1599
- gr.Markdown(
1600
- """<p style="text-align: center;">Optionally flip the hand</p>"""
1601
- )
1602
  target_flip = gr.Checkbox(
1603
  value=False, label="Flip Handedness (Target)", interactive=False
1604
  )
@@ -1687,7 +1696,8 @@ with gr.Blocks(css=custom_css, theme="soft") as demo:
1687
  ref.change(prepare_anno, [ref, ref_is_user], [ref_im_raw, ref_kp_raw])
1688
  ref_kp_raw.change(lambda x: x, ref_im_raw, ref_manual_kp_right)
1689
  ref_kp_raw.change(lambda x: x, ref_im_raw, ref_manual_kp_left)
1690
- ref_kp_raw.change(get_ref_anno, [ref_im_raw, ref_kp_raw], [ref_img, ref_pose, ref_auto_cond, ref, ref_is_user])
 
1691
  ref_pose.change(enable_component, [ref_kp_raw, ref_pose], ref_use_auto)
1692
  ref_pose.change(enable_component, [ref_img, ref_pose], ref_flip)
1693
  ref_auto_cond.change(lambda x: x, ref_auto_cond, ref_cond)
@@ -1733,7 +1743,7 @@ with gr.Blocks(css=custom_css, theme="soft") as demo:
1733
  )
1734
  ref_manual_done.click(visible_component, [gr.State(0), ref_manual_pose], ref_manual_pose)
1735
  ref_manual_done.click(visible_component, [gr.State(0), ref_use_manual], ref_use_manual)
1736
- ref_manual_done.click(get_ref_anno, [ref_im_raw, ref_kp_got], [ref_img, ref_manual_pose, ref_manual_cond])
1737
  ref_manual_pose.change(enable_component, [ref_manual_pose, ref_manual_pose], ref_manual_done)
1738
  ref_manual_pose.change(enable_component, [ref_img, ref_manual_pose], ref_flip)
1739
  ref_manual_cond.change(lambda x: x, ref_manual_cond, ref_cond)
@@ -1850,6 +1860,7 @@ with gr.Blocks(css=custom_css, theme="soft") as demo:
1850
  seed,
1851
  cfg,
1852
  ref_kp_raw,
 
1853
  ],
1854
  )
1855
  clear.click(
@@ -2156,6 +2167,10 @@ with gr.Blocks(css=custom_css, theme="soft") as demo:
2156
  postprocess=False,
2157
  elem_id="fix_examples_all",
2158
  )
 
 
 
 
2159
 
2160
  # tutorial video
2161
  with gr.Accordion("Tutorial Videos of Demo 2", elem_id="accordion_bold_large_center"):
 
18
  import random
19
  from copy import deepcopy
20
  from huggingface_hub import hf_hub_download
21
+ from gradio_toggle import Toggle
22
  try:
23
  import spaces
24
  except:
 
31
  placeholder = cv2.cvtColor(cv2.imread("placeholder.png"), cv2.COLOR_BGR2RGB)
32
  NEW_MODEL = True
33
  MODEL_EPOCH = 6
 
34
  HF = False
35
  pre_device = "cpu" if HF else "cuda"
36
  spaces_60_fn = spaces.GPU(duration=60) if HF else (lambda f: f)
 
282
  else:
283
  return img, None
284
 
285
+ def get_ref_anno(img, keypts, use_mask, use_pose):
286
+ no_mask, no_pose = not use_mask, not use_pose
287
  if img.sum() == 0: # clear_all
288
  return None, gr.update(), None, gr.update(), True
289
  elif keypts is None: # hand not detected
290
  no_hands = cv2.resize(np.array(Image.open("no_hands.png"))[..., :3], (LENGTH, LENGTH))
291
  return None, no_hands, None, no_hands_open, False
292
  missing_keys, extra_keys = autoencoder.load_state_dict(vae_state_dict, strict=False)
293
+ if no_pose:
294
+ keypts = np.zeros((42, 2))
295
+ else:
296
+ if isinstance(keypts, list):
297
+ if len(keypts[0]) == 0:
298
+ keypts[0] = np.zeros((21, 2))
299
+ elif len(keypts[0]) == 21:
300
+ keypts[0] = np.array(keypts[0], dtype=np.float32)
301
+ else:
302
+ gr.Info("Number of right hand keypoints should be either 0 or 21.")
303
+ return None, None, None, gr.update(), gr.update()
304
+ if len(keypts[1]) == 0:
305
+ keypts[1] = np.zeros((21, 2))
306
+ elif len(keypts[1]) == 21:
307
+ keypts[1] = np.array(keypts[1], dtype=np.float32)
308
+ else:
309
+ gr.Info("Number of left hand keypoints should be either 0 or 21.")
310
+ return None, None, None, gr.update(), gr.update()
311
+ keypts = np.concatenate(keypts, axis=0)
312
+ if no_mask:
313
  hand_mask = np.zeros_like(img[:,:, 0])
314
  ref_pose = visualize_hand(keypts, img)
315
  else:
 
395
  print(f"autoencoder encoder before operating dtype: {next(autoencoder.encoder.parameters()).dtype}")
396
  latent = opts.latent_scaling_factor * autoencoder.encode(image).sample()
397
  print(f"latent.max(): {latent.max()}, latent.min(): {latent.min()}")
398
+ if no_mask:
399
  mask = torch.zeros_like(mask)
400
  print(f"heatmaps.max(): {heatmaps.max()}, heatmaps.min(): {heatmaps.min()}")
401
  print(f"mask.max(): {mask.max()}, mask.min(): {mask.min()}")
 
925
  42,
926
  3.0,
927
  gr.update(interactive=False),
928
+ False,
929
  )
930
 
931
  def fix_clear_all():
 
1370
  target_auto_cond = gr.State(value=None)
1371
  target_cond = gr.State(value=None)
1372
 
1373
+ # config
1374
+ use_pose = gr.State(value=True)
1375
+
1376
  # main tabs
1377
  with gr.Row():
1378
  # ref column
 
1394
  crop_size="1:1",
1395
  )
1396
  gr.Examples(example_ref_imgs, [ref], examples_per_page=20)
1397
+ use_mask = Toggle(label="Use mask", value=False, interactive=True)
1398
+ with gr.Accordion(label="See hand pose & mask", open=False):
1399
  with gr.Tab("Automatic hand keypoints"):
1400
  ref_pose = gr.Image(
1401
  type="numpy",
 
1487
  interactive=False,
1488
  visible=True
1489
  )
1490
+ # gr.Markdown(
1491
+ # """<p style="text-align: center;">Optionally flip the hand</p>"""
1492
+ # )
1493
  ref_flip = gr.Checkbox(
1494
  value=False, label="Flip Handedness (Reference)", interactive=False
1495
  )
 
1605
  interactive=False,
1606
  visible=True
1607
  )
1608
+ # gr.Markdown(
1609
+ # """<p style="text-align: center;">Optionally flip the hand</p>"""
1610
+ # )
1611
  target_flip = gr.Checkbox(
1612
  value=False, label="Flip Handedness (Target)", interactive=False
1613
  )
 
1696
  ref.change(prepare_anno, [ref, ref_is_user], [ref_im_raw, ref_kp_raw])
1697
  ref_kp_raw.change(lambda x: x, ref_im_raw, ref_manual_kp_right)
1698
  ref_kp_raw.change(lambda x: x, ref_im_raw, ref_manual_kp_left)
1699
+ ref_kp_raw.change(get_ref_anno, [ref_im_raw, ref_kp_raw, use_mask, use_pose], [ref_img, ref_pose, ref_auto_cond, ref, ref_is_user])
1700
+ use_mask.input(get_ref_anno, [ref_im_raw, ref_kp_raw, use_mask, use_pose], [ref_img, ref_pose, ref_auto_cond, ref, ref_is_user])
1701
  ref_pose.change(enable_component, [ref_kp_raw, ref_pose], ref_use_auto)
1702
  ref_pose.change(enable_component, [ref_img, ref_pose], ref_flip)
1703
  ref_auto_cond.change(lambda x: x, ref_auto_cond, ref_cond)
 
1743
  )
1744
  ref_manual_done.click(visible_component, [gr.State(0), ref_manual_pose], ref_manual_pose)
1745
  ref_manual_done.click(visible_component, [gr.State(0), ref_use_manual], ref_use_manual)
1746
+ ref_manual_done.click(get_ref_anno, [ref_im_raw, ref_kp_got, use_mask, use_pose], [ref_img, ref_manual_pose, ref_manual_cond])
1747
  ref_manual_pose.change(enable_component, [ref_manual_pose, ref_manual_pose], ref_manual_done)
1748
  ref_manual_pose.change(enable_component, [ref_img, ref_manual_pose], ref_flip)
1749
  ref_manual_cond.change(lambda x: x, ref_manual_cond, ref_cond)
 
1860
  seed,
1861
  cfg,
1862
  ref_kp_raw,
1863
+ use_mask,
1864
  ],
1865
  )
1866
  clear.click(
 
2167
  postprocess=False,
2168
  elem_id="fix_examples_all",
2169
  )
2170
+ with gr.Row():
2171
+ gr.Markdown(
2172
+ """<p style="text-align: left;">⚠️ If brushed image doesn't load, please click the example again</p>"""
2173
+ )
2174
 
2175
  # tutorial video
2176
  with gr.Accordion("Tutorial Videos of Demo 2", elem_id="accordion_bold_large_center"):
sbatch/sbatch_demo2.sh ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ # job name
4
+ #SBATCH -J demo2
5
+
6
+ # partition
7
+ #SBATCH -p 3090-gcondo --gres=gpu:1
8
+
9
+ ##SBATCH --partition=ssrinath-gcondo --gres=gpu:1 --gres-flags=enforce-binding
10
+ ##SBATCH --account=ssrinath-gcondo
11
+
12
+ # ensures all allocated cores are on the same node
13
+ #SBATCH -N 1
14
+
15
+ # cpu cores
16
+ #SBATCH --ntasks-per-node=4
17
+
18
+ # memory per node
19
+ #SBATCH --mem=32G
20
+
21
+ # runtime
22
+ #SBATCH -t 240:00:00
23
+
24
+ # output
25
+ #SBATCH -o out/demo2.out
26
+
27
+ # error
28
+ #SBATCH -e err/demo2.err
29
+
30
+ # email notifiaction
31
+ # SBATCH --mail-type=ALL
32
+
33
+ module load miniconda3/23.11.0s
34
+ source /oscar/runtime/software/external/miniconda3/23.11.0/etc/profile.d/conda.sh
35
+ conda activate handdiff
36
+
37
+ cd $HOME/hdd/FoundHand_demo
38
+ echo Directory is `pwd`
39
+
40
+ python -u app.py