adaface-neurips commited on
Commit
a056202
·
1 Parent(s): a2d7ecf

Revert move_to_gpu, let face_app stays on cpu

Browse files
ConsistentID/lib/pipeline_ConsistentID.py CHANGED
@@ -72,15 +72,11 @@ class ConsistentIDPipeline(StableDiffusionPipeline):
72
  self.id_image_processor = CLIPImageProcessor()
73
  self.crop_size = 512
74
 
75
- '''
76
  # face_app: FaceAnalysis object
77
  self.face_app = FaceAnalysis(name="buffalo_l", root='models/insightface',
78
  providers=['CPUExecutionProvider'])
79
  # The original det_size=(640, 640) is too large and face_app often fails to detect faces.
80
- self.face_app.prepare(ctx_id=0, det_size=(256, 256))
81
- '''
82
- # Will be initialized within adaface.
83
- self.face_app = None
84
 
85
  if not os.path.exists(consistentID_weight_path):
86
  ### Download pretrained models
 
72
  self.id_image_processor = CLIPImageProcessor()
73
  self.crop_size = 512
74
 
 
75
  # face_app: FaceAnalysis object
76
  self.face_app = FaceAnalysis(name="buffalo_l", root='models/insightface',
77
  providers=['CPUExecutionProvider'])
78
  # The original det_size=(640, 640) is too large and face_app often fails to detect faces.
79
+ self.face_app.prepare(ctx_id=0, det_size=(512, 512))
 
 
 
80
 
81
  if not os.path.exists(consistentID_weight_path):
82
  ### Download pretrained models
adaface/face_id_to_ada_prompt.py CHANGED
@@ -606,7 +606,7 @@ class Arc2Face_ID2AdaPrompt(FaceID2AdaPrompt):
606
  # Note there are two "models" in the path.
607
  self.face_app = FaceAnalysis(name='antelopev2', root='models/insightface',
608
  providers=['CPUExecutionProvider'])
609
- self.face_app.prepare(ctx_id=0, det_size=(256, 256))
610
  print(f'Arc2Face Face encoder loaded on CPU.')
611
 
612
  self.text_to_image_prompt_encoder = CLIPTextModelWrapper.from_pretrained(
@@ -642,6 +642,7 @@ class Arc2Face_ID2AdaPrompt(FaceID2AdaPrompt):
642
 
643
  def _apply(self, fn):
644
  super()._apply(fn) # Call the parent _apply to handle parameters and buffers
 
645
  # A dirty hack to get the device of the model, passed from
646
  # parent.model.to(self.root_device) => parent._apply(convert) => module._apply(fn)
647
  test_tensor = torch.zeros(1) # Create a test tensor
@@ -654,7 +655,7 @@ class Arc2Face_ID2AdaPrompt(FaceID2AdaPrompt):
654
  if str(device) == 'cpu':
655
  self.face_app = FaceAnalysis(name='antelopev2', root='models/insightface',
656
  providers=['CPUExecutionProvider'])
657
- self.face_app.prepare(ctx_id=0, det_size=(256, 256))
658
  else:
659
  device_id = device.index
660
  self.face_app = FaceAnalysis(name='antelopev2', root='models/insightface',
@@ -663,7 +664,7 @@ class Arc2Face_ID2AdaPrompt(FaceID2AdaPrompt):
663
  "cudnn_conv_algo_search": "HEURISTIC",
664
  "gpu_mem_limit": 2 * 1024**3
665
  }])
666
- self.face_app.prepare(ctx_id=device_id, det_size=(256, 256))
667
 
668
  self.device = device
669
  print(f'Arc2Face Face encoder reloaded on {device}.')
@@ -790,6 +791,7 @@ class ConsistentID_ID2AdaPrompt(FaceID2AdaPrompt):
790
 
791
  def _apply(self, fn):
792
  super()._apply(fn) # Call the parent _apply to handle parameters and buffers
 
793
  # A dirty hack to get the device of the model, passed from
794
  # parent.model.to(self.root_device) => parent._apply(convert) => module._apply(fn)
795
  test_tensor = torch.zeros(1) # Create a test tensor
@@ -802,7 +804,7 @@ class ConsistentID_ID2AdaPrompt(FaceID2AdaPrompt):
802
  if str(device) == 'cpu':
803
  self.face_app = FaceAnalysis(name='buffalo_l', root='models/insightface',
804
  providers=['CPUExecutionProvider'])
805
- self.face_app.prepare(ctx_id=0, det_size=(256, 256))
806
  else:
807
  device_id = device.index
808
  self.face_app = FaceAnalysis(name='buffalo_l', root='models/insightface',
@@ -811,7 +813,7 @@ class ConsistentID_ID2AdaPrompt(FaceID2AdaPrompt):
811
  "cudnn_conv_algo_search": "HEURISTIC",
812
  "gpu_mem_limit": 2 * 1024**3
813
  }])
814
- self.face_app.prepare(ctx_id=device_id, det_size=(256, 256))
815
 
816
  self.device = device
817
  self.pipe.face_app = self.face_app
 
606
  # Note there are two "models" in the path.
607
  self.face_app = FaceAnalysis(name='antelopev2', root='models/insightface',
608
  providers=['CPUExecutionProvider'])
609
+ self.face_app.prepare(ctx_id=0, det_size=(512, 512))
610
  print(f'Arc2Face Face encoder loaded on CPU.')
611
 
612
  self.text_to_image_prompt_encoder = CLIPTextModelWrapper.from_pretrained(
 
642
 
643
  def _apply(self, fn):
644
  super()._apply(fn) # Call the parent _apply to handle parameters and buffers
645
+ return
646
  # A dirty hack to get the device of the model, passed from
647
  # parent.model.to(self.root_device) => parent._apply(convert) => module._apply(fn)
648
  test_tensor = torch.zeros(1) # Create a test tensor
 
655
  if str(device) == 'cpu':
656
  self.face_app = FaceAnalysis(name='antelopev2', root='models/insightface',
657
  providers=['CPUExecutionProvider'])
658
+ self.face_app.prepare(ctx_id=0, det_size=(512, 512))
659
  else:
660
  device_id = device.index
661
  self.face_app = FaceAnalysis(name='antelopev2', root='models/insightface',
 
664
  "cudnn_conv_algo_search": "HEURISTIC",
665
  "gpu_mem_limit": 2 * 1024**3
666
  }])
667
+ self.face_app.prepare(ctx_id=device_id, det_size=(512, 512))
668
 
669
  self.device = device
670
  print(f'Arc2Face Face encoder reloaded on {device}.')
 
791
 
792
  def _apply(self, fn):
793
  super()._apply(fn) # Call the parent _apply to handle parameters and buffers
794
+ return
795
  # A dirty hack to get the device of the model, passed from
796
  # parent.model.to(self.root_device) => parent._apply(convert) => module._apply(fn)
797
  test_tensor = torch.zeros(1) # Create a test tensor
 
804
  if str(device) == 'cpu':
805
  self.face_app = FaceAnalysis(name='buffalo_l', root='models/insightface',
806
  providers=['CPUExecutionProvider'])
807
+ self.face_app.prepare(ctx_id=0, det_size=(512, 512))
808
  else:
809
  device_id = device.index
810
  self.face_app = FaceAnalysis(name='buffalo_l', root='models/insightface',
 
813
  "cudnn_conv_algo_search": "HEURISTIC",
814
  "gpu_mem_limit": 2 * 1024**3
815
  }])
816
+ self.face_app.prepare(ctx_id=device_id, det_size=(512, 512))
817
 
818
  self.device = device
819
  self.pipe.face_app = self.face_app
app.py CHANGED
@@ -23,20 +23,6 @@ def str2bool(v):
23
  def is_running_on_spaces():
24
  return os.getenv("SPACE_ID") is not None
25
 
26
- @spaces.GPU(duration=120)
27
- def move_to_gpu(adaface):
28
- if is_running_on_spaces():
29
- device = 'cuda:0'
30
- else:
31
- if args.gpu is None:
32
- device = "cuda"
33
- else:
34
- device = f"cuda:{args.gpu}"
35
-
36
- print(f"Device: {device}")
37
-
38
- adaface.to(device)
39
-
40
  import argparse
41
  parser = argparse.ArgumentParser()
42
  parser.add_argument("--adaface_encoder_types", type=str, nargs="+", default=["consistentID", "arc2face"],
@@ -101,7 +87,6 @@ if not args.test_ui_only:
101
  shrink_cross_attn=False,
102
  q_lora_updates_query=args.q_lora_updates_query,
103
  device='cpu')
104
- move_to_gpu(adaface)
105
 
106
  def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
107
  if randomize_seed:
@@ -127,8 +112,21 @@ def generate_image(image_paths, image_paths2, guidance_scale, perturb_std,
127
  ablate_prompt_embed_type, nonmix_prompt_emb_weight,
128
  composition_level, seed, disable_adaface, subj_name_sig, progress=gr.Progress(track_tqdm=True)):
129
 
130
- global adaface
 
 
 
 
 
 
 
 
 
 
131
 
 
 
 
132
  if image_paths is None or len(image_paths) == 0:
133
  raise gr.Error(f"Cannot find any input face image! Please upload a face image.")
134
 
@@ -149,10 +147,10 @@ def generate_image(image_paths, image_paths2, guidance_scale, perturb_std,
149
 
150
  # Sometimes the pipeline is on CPU, although we've put it on CUDA (due to some offloading mechanism).
151
  # Therefore we set the generator to the correct device.
152
- generator = torch.Generator(device=device).manual_seed(seed)
153
  print(f"Manual seed: {seed}.")
154
  # Generate two images each time for the user to select from.
155
- noise = torch.randn(num_images, 3, 512, 512, device=device, generator=generator)
156
  #print(noise.abs().sum())
157
  # samples: A list of PIL Image instances.
158
  if highlight_face:
 
23
  def is_running_on_spaces():
24
  return os.getenv("SPACE_ID") is not None
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  import argparse
27
  parser = argparse.ArgumentParser()
28
  parser.add_argument("--adaface_encoder_types", type=str, nargs="+", default=["consistentID", "arc2face"],
 
87
  shrink_cross_attn=False,
88
  q_lora_updates_query=args.q_lora_updates_query,
89
  device='cpu')
 
90
 
91
  def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
92
  if randomize_seed:
 
112
  ablate_prompt_embed_type, nonmix_prompt_emb_weight,
113
  composition_level, seed, disable_adaface, subj_name_sig, progress=gr.Progress(track_tqdm=True)):
114
 
115
+ global adaface, args
116
+
117
+ if is_running_on_spaces():
118
+ device = 'cuda:0'
119
+ else:
120
+ if args.gpu is None:
121
+ device = "cuda"
122
+ else:
123
+ device = f"cuda:{args.gpu}"
124
+
125
+ print(f"Device: {device}")
126
 
127
+ adaface.to(device)
128
+ args.device = device
129
+
130
  if image_paths is None or len(image_paths) == 0:
131
  raise gr.Error(f"Cannot find any input face image! Please upload a face image.")
132
 
 
147
 
148
  # Sometimes the pipeline is on CPU, although we've put it on CUDA (due to some offloading mechanism).
149
  # Therefore we set the generator to the correct device.
150
+ generator = torch.Generator(device=args.device).manual_seed(seed)
151
  print(f"Manual seed: {seed}.")
152
  # Generate two images each time for the user to select from.
153
+ noise = torch.randn(num_images, 3, 512, 512, device=args.device, generator=generator)
154
  #print(noise.abs().sum())
155
  # samples: A list of PIL Image instances.
156
  if highlight_face: