Spaces:
Running
on
Zero
Running
on
Zero
adaface-neurips
commited on
Commit
•
13d8b07
1
Parent(s):
81f8834
minor changes
Browse files- adaface/adaface_wrapper.py +22 -7
- app.py +7 -5
adaface/adaface_wrapper.py
CHANGED
@@ -84,7 +84,7 @@ class AdaFaceWrapper(nn.Module):
|
|
84 |
if self.use_ds_text_encoder:
|
85 |
# The dreamshaper v7 finetuned text encoder follows the prompt slightly better than the original text encoder.
|
86 |
# https://huggingface.co/Lykon/DreamShaper/tree/main/text_encoder
|
87 |
-
text_encoder = CLIPTextModel.from_pretrained("models/ds_text_encoder", torch_dtype=torch.float16)
|
88 |
else:
|
89 |
text_encoder = None
|
90 |
|
@@ -253,10 +253,13 @@ class AdaFaceWrapper(nn.Module):
|
|
253 |
self.update_text_encoder_subj_embs(adaface_subj_embs)
|
254 |
return adaface_subj_embs
|
255 |
|
256 |
-
def encode_prompt(self, prompt, negative_prompt=None, device=
|
257 |
if negative_prompt is None:
|
258 |
negative_prompt = self.negative_prompt
|
259 |
-
|
|
|
|
|
|
|
260 |
prompt = self.update_prompt(prompt)
|
261 |
if verbose:
|
262 |
print(f"Prompt: {prompt}")
|
@@ -264,10 +267,22 @@ class AdaFaceWrapper(nn.Module):
|
|
264 |
# For some unknown reason, the text_encoder is still on CPU after self.pipeline.to(self.device).
|
265 |
# So we manually move it to GPU here.
|
266 |
self.pipeline.text_encoder.to(device)
|
267 |
-
#
|
268 |
-
|
269 |
-
|
270 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
271 |
return prompt_embeds_, negative_prompt_embeds_
|
272 |
|
273 |
# ref_img_strength is used only in the img2img pipeline.
|
|
|
84 |
if self.use_ds_text_encoder:
|
85 |
# The dreamshaper v7 finetuned text encoder follows the prompt slightly better than the original text encoder.
|
86 |
# https://huggingface.co/Lykon/DreamShaper/tree/main/text_encoder
|
87 |
+
text_encoder = CLIPTextModel.from_pretrained("models/diffusers/ds_text_encoder", torch_dtype=torch.float16)
|
88 |
else:
|
89 |
text_encoder = None
|
90 |
|
|
|
253 |
self.update_text_encoder_subj_embs(adaface_subj_embs)
|
254 |
return adaface_subj_embs
|
255 |
|
256 |
+
def encode_prompt(self, prompt, negative_prompt=None, device=None, verbose=False):
|
257 |
if negative_prompt is None:
|
258 |
negative_prompt = self.negative_prompt
|
259 |
+
|
260 |
+
if device is None:
|
261 |
+
device = self.device
|
262 |
+
|
263 |
prompt = self.update_prompt(prompt)
|
264 |
if verbose:
|
265 |
print(f"Prompt: {prompt}")
|
|
|
267 |
# For some unknown reason, the text_encoder is still on CPU after self.pipeline.to(self.device).
|
268 |
# So we manually move it to GPU here.
|
269 |
self.pipeline.text_encoder.to(device)
|
270 |
+
# Compatible with older versions of diffusers.
|
271 |
+
if not hasattr(self.pipeline, "encode_prompt"):
|
272 |
+
# prompt_embeds_, negative_prompt_embeds_: [77, 768] -> [1, 77, 768].
|
273 |
+
prompt_embeds_, negative_prompt_embeds_ = \
|
274 |
+
self.pipeline._encode_prompt(prompt, device=device, num_images_per_prompt=1,
|
275 |
+
do_classifier_free_guidance=True, negative_prompt=negative_prompt)
|
276 |
+
prompt_embeds_ = prompt_embeds_.unsqueeze(0)
|
277 |
+
negative_prompt_embeds_ = negative_prompt_embeds_.unsqueeze(0)
|
278 |
+
else:
|
279 |
+
# prompt_embeds_, negative_prompt_embeds_: [1, 77, 768]
|
280 |
+
prompt_embeds_, negative_prompt_embeds_ = \
|
281 |
+
self.pipeline.encode_prompt(prompt, device=device,
|
282 |
+
num_images_per_prompt=1,
|
283 |
+
do_classifier_free_guidance=True,
|
284 |
+
negative_prompt=negative_prompt)
|
285 |
+
|
286 |
return prompt_embeds_, negative_prompt_embeds_
|
287 |
|
288 |
# ref_img_strength is used only in the img2img pipeline.
|
app.py
CHANGED
@@ -88,7 +88,8 @@ def gen_init_images(uploaded_image_paths, prompt, adaface_id_cfg_scale, out_imag
|
|
88 |
# Generate two images each time for the user to select from.
|
89 |
noise = torch.randn(out_image_count, 3, 512, 512)
|
90 |
# samples: A list of PIL Image instances.
|
91 |
-
|
|
|
92 |
|
93 |
face_paths = []
|
94 |
for sample in samples:
|
@@ -130,10 +131,11 @@ def generate_image(image_container, uploaded_image_paths, init_img_file_paths, i
|
|
130 |
# Reload the embedding manager
|
131 |
adaface.load_subj_basis_generator(adaface_ckpt_path)
|
132 |
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
|
|
137 |
|
138 |
# init_img_file_paths is a list of image paths. If not chose, init_img_file_paths is None.
|
139 |
if init_img_file_paths is not None:
|
|
|
88 |
# Generate two images each time for the user to select from.
|
89 |
noise = torch.randn(out_image_count, 3, 512, 512)
|
90 |
# samples: A list of PIL Image instances.
|
91 |
+
with torch.no_grad():
|
92 |
+
samples = adaface(noise, prompt, out_image_count=out_image_count, verbose=True)
|
93 |
|
94 |
face_paths = []
|
95 |
for sample in samples:
|
|
|
131 |
# Reload the embedding manager
|
132 |
adaface.load_subj_basis_generator(adaface_ckpt_path)
|
133 |
|
134 |
+
with torch.no_grad():
|
135 |
+
adaface.generate_adaface_embeddings(image_folder=None, image_paths=uploaded_image_paths,
|
136 |
+
out_id_embs_scale=adaface_id_cfg_scale, update_text_encoder=True)
|
137 |
+
# adaface_prompt_embeds: [1, 77, 768].
|
138 |
+
adaface_prompt_embeds, _ = adaface.encode_prompt(prompt)
|
139 |
|
140 |
# init_img_file_paths is a list of image paths. If not chose, init_img_file_paths is None.
|
141 |
if init_img_file_paths is not None:
|