Robert001 commited on
Commit
2c98956
1 Parent(s): 72253cb

first commit

Browse files
annotator/midas/__init__.py CHANGED
@@ -22,9 +22,9 @@ from .api import MiDaSInference
22
 
23
  class MidasDetector:
24
  def __init__(self):
25
- self.model = MiDaSInference(model_type="dpt_large").cuda()
26
 
27
- def __call__(self, input_image, a=np.pi * 0.2, bg_th=0.02):
28
  assert input_image.ndim == 3
29
  image_depth = input_image
30
  with torch.no_grad():
 
22
 
23
  class MidasDetector:
24
  def __init__(self):
25
+ self.model = MiDaSInference(model_type="dpt_hybrid").cuda()
26
 
27
+ def __call__(self, input_image, a=np.pi * 2.0, bg_th=0.1):
28
  assert input_image.ndim == 3
29
  image_depth = input_image
30
  with torch.no_grad():
annotator/midas/api.py CHANGED
@@ -30,8 +30,8 @@ ISL_PATHS = {
30
  "midas_v21_small": "",
31
  }
32
 
33
- # remote_model_path = "https://huggingface.co/lllyasviel/ControlNet/resolve/main/annotator/ckpts/dpt_hybrid-midas-501f0c75.pt"
34
- remote_model_path = "https://storage.googleapis.com/sfr-unicontrol-data-research/annotator/ckpts/dpt_large_384.pt" #"https://huggingface.co/Salesforce/UniControl/blob/main/annotator/ckpts/dpt_large_384.pt"
35
 
36
  def disabled_train(self, mode=True):
37
  """Overwrite model.train with this function to make sure train/eval mode
 
30
  "midas_v21_small": "",
31
  }
32
 
33
+ remote_model_path = "https://huggingface.co/lllyasviel/ControlNet/resolve/main/annotator/ckpts/dpt_hybrid-midas-501f0c75.pt"
34
+ # remote_model_path = "https://storage.googleapis.com/sfr-unicontrol-data-research/annotator/ckpts/dpt_large_384.pt" #"https://huggingface.co/Salesforce/UniControl/blob/main/annotator/ckpts/dpt_large_384.pt"
35
 
36
  def disabled_train(self, mode=True):
37
  """Overwrite model.train with this function to make sure train/eval mode
annotator/openpose/__init__.py CHANGED
@@ -31,15 +31,15 @@ hand_model_path = "https://huggingface.co/lllyasviel/ControlNet/resolve/main/ann
31
  class OpenposeDetector:
32
  def __init__(self):
33
  body_modelpath = os.path.join(annotator_ckpts_path, "body_pose_model.pth")
34
- hand_modelpath = os.path.join(annotator_ckpts_path, "hand_pose_model.pth")
35
 
36
  if not os.path.exists(hand_modelpath):
37
  from basicsr.utils.download_util import load_file_from_url
38
  load_file_from_url(body_model_path, model_dir=annotator_ckpts_path)
39
- load_file_from_url(hand_model_path, model_dir=annotator_ckpts_path)
40
 
41
  self.body_estimation = Body(body_modelpath)
42
- self.hand_estimation = Hand(hand_modelpath)
43
 
44
  def __call__(self, oriImg, hand=False):
45
  oriImg = oriImg[:, :, ::-1].copy()
@@ -47,13 +47,13 @@ class OpenposeDetector:
47
  candidate, subset = self.body_estimation(oriImg)
48
  canvas = np.zeros_like(oriImg)
49
  canvas = util.draw_bodypose(canvas, candidate, subset)
50
- if hand:
51
- hands_list = util.handDetect(candidate, subset, oriImg)
52
- all_hand_peaks = []
53
- for x, y, w, is_left in hands_list:
54
- peaks = self.hand_estimation(oriImg[y:y+w, x:x+w, :])
55
- peaks[:, 0] = np.where(peaks[:, 0] == 0, peaks[:, 0], peaks[:, 0] + x)
56
- peaks[:, 1] = np.where(peaks[:, 1] == 0, peaks[:, 1], peaks[:, 1] + y)
57
- all_hand_peaks.append(peaks)
58
- canvas = util.draw_handpose(canvas, all_hand_peaks)
59
  return canvas, dict(candidate=candidate.tolist(), subset=subset.tolist())
 
31
  class OpenposeDetector:
32
  def __init__(self):
33
  body_modelpath = os.path.join(annotator_ckpts_path, "body_pose_model.pth")
34
+ # hand_modelpath = os.path.join(annotator_ckpts_path, "hand_pose_model.pth")
35
 
36
  if not os.path.exists(hand_modelpath):
37
  from basicsr.utils.download_util import load_file_from_url
38
  load_file_from_url(body_model_path, model_dir=annotator_ckpts_path)
39
+ # load_file_from_url(hand_model_path, model_dir=annotator_ckpts_path)
40
 
41
  self.body_estimation = Body(body_modelpath)
42
+ # self.hand_estimation = Hand(hand_modelpath)
43
 
44
  def __call__(self, oriImg, hand=False):
45
  oriImg = oriImg[:, :, ::-1].copy()
 
47
  candidate, subset = self.body_estimation(oriImg)
48
  canvas = np.zeros_like(oriImg)
49
  canvas = util.draw_bodypose(canvas, candidate, subset)
50
+ # if hand:
51
+ # hands_list = util.handDetect(candidate, subset, oriImg)
52
+ # all_hand_peaks = []
53
+ # for x, y, w, is_left in hands_list:
54
+ # peaks = self.hand_estimation(oriImg[y:y+w, x:x+w, :])
55
+ # peaks[:, 0] = np.where(peaks[:, 0] == 0, peaks[:, 0], peaks[:, 0] + x)
56
+ # peaks[:, 1] = np.where(peaks[:, 1] == 0, peaks[:, 1], peaks[:, 1] + y)
57
+ # all_hand_peaks.append(peaks)
58
+ # canvas = util.draw_handpose(canvas, all_hand_peaks)
59
  return canvas, dict(candidate=candidate.tolist(), subset=subset.tolist())
model.py CHANGED
@@ -315,7 +315,6 @@ class ControlNet(nn.Module):
315
  num_heads = ch // num_head_channels
316
  dim_head = num_head_channels
317
  if legacy:
318
- # num_heads = 1
319
  dim_head = ch // num_heads if use_spatial_transformer else num_head_channels
320
  self.middle_block = TimestepEmbedSequential(
321
  ResBlock(
@@ -360,7 +359,6 @@ class ControlNet(nn.Module):
360
  hint -> 4, 3, 512, 512
361
  context - > 4, 77, 768
362
  '''
363
- BS = 1 # x.shape[0], one batch one task
364
  BS_Real = x.shape[0]
365
  if kwargs is not None:
366
  task_name = kwargs['task']['name']
@@ -407,7 +405,6 @@ class ControlLDM(LatentDiffusion):
407
  super().__init__(*args, **kwargs)
408
  self.mapping_task = {"control_hed": "hed edge to image", "control_canny": "canny edge to image", "control_seg": "segmentation map to image", "control_depth": "depth map to image", "control_normal": "normal surface map to image", "control_img": "image editing", "control_openpose": "human pose skeleton to image", "control_hedsketch": "sketch to image", "control_bbox": "bounding box to image", "control_outpainting": "image outpainting", "control_grayscale": "gray image to color image", "control_blur": "deblur image to clean image", "control_inpainting": "image inpainting"}
409
  self.all_tasks_num = len(self.mapping_task)
410
- # self.task_weight_all = nn.Parameter(torch.zeros(self.all_tasks_num,), requires_grad=True)
411
  self.task_loss_ema = torch.zeros(self.all_tasks_num,)
412
 
413
  self.control_model = instantiate_from_config(control_stage_config) # -> ControlNet
@@ -552,11 +549,6 @@ class ControlLDM(LatentDiffusion):
552
  c_cat, c = c["c_concat"][0][:N], c["c_crossattn"][0][:N]
553
  N = min(z.shape[0], N)
554
  n_row = min(z.shape[0], n_row)
555
- # log["reconstruction"] = self.decode_first_stage(z)
556
- # log["control"] = c_cat * 2.0 - 1.0
557
- # log["conditioning"] = log_txt_as_img((512, 512), batch[self.cond_stage_key], size=16)
558
-
559
-
560
 
561
  uc_cross = self.get_unconditional_conditioning(N)
562
  uc_cat = c_cat # torch.zeros_like(c_cat)
 
315
  num_heads = ch // num_head_channels
316
  dim_head = num_head_channels
317
  if legacy:
 
318
  dim_head = ch // num_heads if use_spatial_transformer else num_head_channels
319
  self.middle_block = TimestepEmbedSequential(
320
  ResBlock(
 
359
  hint -> 4, 3, 512, 512
360
  context - > 4, 77, 768
361
  '''
 
362
  BS_Real = x.shape[0]
363
  if kwargs is not None:
364
  task_name = kwargs['task']['name']
 
405
  super().__init__(*args, **kwargs)
406
  self.mapping_task = {"control_hed": "hed edge to image", "control_canny": "canny edge to image", "control_seg": "segmentation map to image", "control_depth": "depth map to image", "control_normal": "normal surface map to image", "control_img": "image editing", "control_openpose": "human pose skeleton to image", "control_hedsketch": "sketch to image", "control_bbox": "bounding box to image", "control_outpainting": "image outpainting", "control_grayscale": "gray image to color image", "control_blur": "deblur image to clean image", "control_inpainting": "image inpainting"}
407
  self.all_tasks_num = len(self.mapping_task)
 
408
  self.task_loss_ema = torch.zeros(self.all_tasks_num,)
409
 
410
  self.control_model = instantiate_from_config(control_stage_config) # -> ControlNet
 
549
  c_cat, c = c["c_concat"][0][:N], c["c_crossattn"][0][:N]
550
  N = min(z.shape[0], N)
551
  n_row = min(z.shape[0], n_row)
 
 
 
 
 
552
 
553
  uc_cross = self.get_unconditional_conditioning(N)
554
  uc_cat = c_cat # torch.zeros_like(c_cat)