Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,28 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
from src.tryon_pipeline import StableDiffusionXLInpaintPipeline as TryonPipeline
|
| 5 |
from src.unet_hacked_garmnet import UNet2DConditionModel as UNet2DConditionModel_ref
|
|
@@ -16,6 +39,7 @@ from typing import List
|
|
| 16 |
import torch
|
| 17 |
import os
|
| 18 |
from transformers import AutoTokenizer
|
|
|
|
| 19 |
import numpy as np
|
| 20 |
from utils_mask import get_mask_location
|
| 21 |
from torchvision import transforms
|
|
@@ -241,6 +265,126 @@ def start_tryon(dict,garm_img,garment_des,is_checked,is_checked_crop,denoise_ste
|
|
| 241 |
return images[0], mask_gray
|
| 242 |
# return images[0], mask_gray
|
| 243 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
garm_list = os.listdir(os.path.join(example_path,"cloth"))
|
| 245 |
garm_list_path = [os.path.join(example_path,"cloth",garm) for garm in garm_list]
|
| 246 |
|
|
@@ -302,12 +446,21 @@ with image_blocks as demo:
|
|
| 302 |
denoise_steps = gr.Number(label="Denoising Steps", minimum=20, maximum=40, value=30, step=1)
|
| 303 |
seed = gr.Number(label="Seed", minimum=-1, maximum=2147483647, step=1, value=42)
|
| 304 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
|
| 306 |
|
| 307 |
try_button.click(fn=start_tryon, inputs=[imgs, garm_img, prompt, is_checked,is_checked_crop, denoise_steps, seed], outputs=[image_out,masked_img], api_name='tryon')
|
|
|
|
| 308 |
|
| 309 |
-
|
| 310 |
|
|
|
|
| 311 |
|
| 312 |
-
image_blocks.launch()
|
| 313 |
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
file_path = "/home/user/.pyenv/versions/3.10.16/lib/python3.10/site-packages/diffusers/utils/dynamic_modules_utils.py"
|
| 4 |
+
if os.path.exists(file_path):
|
| 5 |
+
with open(file_path, "r") as f:
|
| 6 |
+
lines = f.readlines()
|
| 7 |
+
|
| 8 |
+
# Modify the import line
|
| 9 |
+
new_lines = []
|
| 10 |
+
for line in lines:
|
| 11 |
+
if "from huggingface_hub import cached_download" in line:
|
| 12 |
+
new_lines.append("from huggingface_hub import hf_hub_download, model_info\n") # Remove cached_download
|
| 13 |
+
else:
|
| 14 |
+
new_lines.append(line)
|
| 15 |
+
|
| 16 |
+
# Write back the changes
|
| 17 |
+
with open(file_path, "w") as f:
|
| 18 |
+
f.writelines(new_lines)
|
| 19 |
+
|
| 20 |
+
print("Patched diffusers successfully!")
|
| 21 |
+
else:
|
| 22 |
+
print("File not found:", file_path)
|
| 23 |
+
|
| 24 |
import gradio as gr
|
| 25 |
+
from diffusers.utils import load_image
|
| 26 |
from PIL import Image
|
| 27 |
from src.tryon_pipeline import StableDiffusionXLInpaintPipeline as TryonPipeline
|
| 28 |
from src.unet_hacked_garmnet import UNet2DConditionModel as UNet2DConditionModel_ref
|
|
|
|
| 39 |
import torch
|
| 40 |
import os
|
| 41 |
from transformers import AutoTokenizer
|
| 42 |
+
import spaces
|
| 43 |
import numpy as np
|
| 44 |
from utils_mask import get_mask_location
|
| 45 |
from torchvision import transforms
|
|
|
|
| 265 |
return images[0], mask_gray
|
| 266 |
# return images[0], mask_gray
|
| 267 |
|
| 268 |
+
|
| 269 |
+
@spaces.GPU
|
| 270 |
+
def start_tryon2(main_img_path, garm_img_path,garment_des,is_checked,is_checked_crop,denoise_steps,seed):
|
| 271 |
+
device = "cuda"
|
| 272 |
+
|
| 273 |
+
openpose_model.preprocessor.body_estimation.model.to(device)
|
| 274 |
+
pipe.to(device)
|
| 275 |
+
pipe.unet_encoder.to(device)
|
| 276 |
+
|
| 277 |
+
main_img = load_image(main_img_path)
|
| 278 |
+
garm_img = load_image(garm_img_path)
|
| 279 |
+
|
| 280 |
+
garm_img= garm_img.convert("RGB").resize((768,1024))
|
| 281 |
+
human_img_orig = main_img.convert("RGB")
|
| 282 |
+
|
| 283 |
+
if is_checked_crop:
|
| 284 |
+
width, height = human_img_orig.size
|
| 285 |
+
target_width = int(min(width, height * (3 / 4)))
|
| 286 |
+
target_height = int(min(height, width * (4 / 3)))
|
| 287 |
+
left = (width - target_width) / 2
|
| 288 |
+
top = (height - target_height) / 2
|
| 289 |
+
right = (width + target_width) / 2
|
| 290 |
+
bottom = (height + target_height) / 2
|
| 291 |
+
cropped_img = human_img_orig.crop((left, top, right, bottom))
|
| 292 |
+
crop_size = cropped_img.size
|
| 293 |
+
human_img = cropped_img.resize((768,1024))
|
| 294 |
+
else:
|
| 295 |
+
human_img = human_img_orig.resize((768,1024))
|
| 296 |
+
|
| 297 |
+
|
| 298 |
+
if is_checked:
|
| 299 |
+
keypoints = openpose_model(human_img.resize((384,512)))
|
| 300 |
+
model_parse, _ = parsing_model(human_img.resize((384,512)))
|
| 301 |
+
mask, mask_gray = get_mask_location('hd', "upper_body", model_parse, keypoints)
|
| 302 |
+
mask = mask.resize((768,1024))
|
| 303 |
+
|
| 304 |
+
mask_gray = (1-transforms.ToTensor()(mask)) * tensor_transfrom(human_img)
|
| 305 |
+
mask_gray = to_pil_image((mask_gray+1.0)/2.0)
|
| 306 |
+
|
| 307 |
+
|
| 308 |
+
human_img_arg = _apply_exif_orientation(human_img.resize((384,512)))
|
| 309 |
+
human_img_arg = convert_PIL_to_numpy(human_img_arg, format="BGR")
|
| 310 |
+
|
| 311 |
+
|
| 312 |
+
|
| 313 |
+
args = apply_net.create_argument_parser().parse_args(('show', './configs/densepose_rcnn_R_50_FPN_s1x.yaml', './ckpt/densepose/model_final_162be9.pkl', 'dp_segm', '-v', '--opts', 'MODEL.DEVICE', 'cuda'))
|
| 314 |
+
# verbosity = getattr(args, "verbosity", None)
|
| 315 |
+
pose_img = args.func(args,human_img_arg)
|
| 316 |
+
pose_img = pose_img[:,:,::-1]
|
| 317 |
+
pose_img = Image.fromarray(pose_img).resize((768,1024))
|
| 318 |
+
|
| 319 |
+
with torch.no_grad():
|
| 320 |
+
# Extract the images
|
| 321 |
+
with torch.cuda.amp.autocast():
|
| 322 |
+
with torch.no_grad():
|
| 323 |
+
prompt = "model is wearing " + garment_des
|
| 324 |
+
negative_prompt = "monochrome, lowres, bad anatomy, worst quality, low quality"
|
| 325 |
+
with torch.inference_mode():
|
| 326 |
+
(
|
| 327 |
+
prompt_embeds,
|
| 328 |
+
negative_prompt_embeds,
|
| 329 |
+
pooled_prompt_embeds,
|
| 330 |
+
negative_pooled_prompt_embeds,
|
| 331 |
+
) = pipe.encode_prompt(
|
| 332 |
+
prompt,
|
| 333 |
+
num_images_per_prompt=1,
|
| 334 |
+
do_classifier_free_guidance=True,
|
| 335 |
+
negative_prompt=negative_prompt,
|
| 336 |
+
)
|
| 337 |
+
|
| 338 |
+
prompt = "a photo of " + garment_des
|
| 339 |
+
negative_prompt = "monochrome, lowres, bad anatomy, worst quality, low quality"
|
| 340 |
+
if not isinstance(prompt, List):
|
| 341 |
+
prompt = [prompt] * 1
|
| 342 |
+
if not isinstance(negative_prompt, List):
|
| 343 |
+
negative_prompt = [negative_prompt] * 1
|
| 344 |
+
with torch.inference_mode():
|
| 345 |
+
(
|
| 346 |
+
prompt_embeds_c,
|
| 347 |
+
_,
|
| 348 |
+
_,
|
| 349 |
+
_,
|
| 350 |
+
) = pipe.encode_prompt(
|
| 351 |
+
prompt,
|
| 352 |
+
num_images_per_prompt=1,
|
| 353 |
+
do_classifier_free_guidance=False,
|
| 354 |
+
negative_prompt=negative_prompt,
|
| 355 |
+
)
|
| 356 |
+
|
| 357 |
+
|
| 358 |
+
|
| 359 |
+
pose_img = tensor_transfrom(pose_img).unsqueeze(0).to(device,torch.float16)
|
| 360 |
+
garm_tensor = tensor_transfrom(garm_img).unsqueeze(0).to(device,torch.float16)
|
| 361 |
+
generator = torch.Generator(device).manual_seed(seed) if seed is not None else None
|
| 362 |
+
images = pipe(
|
| 363 |
+
prompt_embeds=prompt_embeds.to(device,torch.float16),
|
| 364 |
+
negative_prompt_embeds=negative_prompt_embeds.to(device,torch.float16),
|
| 365 |
+
pooled_prompt_embeds=pooled_prompt_embeds.to(device,torch.float16),
|
| 366 |
+
negative_pooled_prompt_embeds=negative_pooled_prompt_embeds.to(device,torch.float16),
|
| 367 |
+
num_inference_steps=denoise_steps,
|
| 368 |
+
generator=generator,
|
| 369 |
+
strength = 1.0,
|
| 370 |
+
pose_img = pose_img.to(device,torch.float16),
|
| 371 |
+
text_embeds_cloth=prompt_embeds_c.to(device,torch.float16),
|
| 372 |
+
cloth = garm_tensor.to(device,torch.float16),
|
| 373 |
+
mask_image=mask,
|
| 374 |
+
image=human_img,
|
| 375 |
+
height=1024,
|
| 376 |
+
width=768,
|
| 377 |
+
ip_adapter_image = garm_img.resize((768,1024)),
|
| 378 |
+
guidance_scale=2.0,
|
| 379 |
+
)[0]
|
| 380 |
+
|
| 381 |
+
if is_checked_crop:
|
| 382 |
+
out_img = images[0].resize(crop_size)
|
| 383 |
+
human_img_orig.paste(out_img, (int(left), int(top)))
|
| 384 |
+
return human_img_orig, mask_gray
|
| 385 |
+
else:
|
| 386 |
+
return images[0], mask_gray
|
| 387 |
+
|
| 388 |
garm_list = os.listdir(os.path.join(example_path,"cloth"))
|
| 389 |
garm_list_path = [os.path.join(example_path,"cloth",garm) for garm in garm_list]
|
| 390 |
|
|
|
|
| 446 |
denoise_steps = gr.Number(label="Denoising Steps", minimum=20, maximum=40, value=30, step=1)
|
| 447 |
seed = gr.Number(label="Seed", minimum=-1, maximum=2147483647, step=1, value=42)
|
| 448 |
|
| 449 |
+
with gr.Column():
|
| 450 |
+
try_button2 = gr.Button(value="Try-on-experimental")
|
| 451 |
+
with gr.Row():
|
| 452 |
+
main_img_path = gr.Textbox(placeholder="main_img_path", show_label=False, elem_id="mainimgpath")
|
| 453 |
+
garm_img_path = gr.Textbox(placeholder="main_img_path", show_label=False, elem_id="garmimgpath")
|
| 454 |
+
|
| 455 |
+
|
| 456 |
+
|
| 457 |
|
| 458 |
|
| 459 |
try_button.click(fn=start_tryon, inputs=[imgs, garm_img, prompt, is_checked,is_checked_crop, denoise_steps, seed], outputs=[image_out,masked_img], api_name='tryon')
|
| 460 |
+
try_button2.click(fn=start_tryon2, inputs=[main_img_path, garm_img_path, prompt, is_checked,is_checked_crop, denoise_steps, seed], outputs=[image_out,masked_img], api_name='tryon2')
|
| 461 |
|
|
|
|
| 462 |
|
| 463 |
+
|
| 464 |
|
|
|
|
| 465 |
|
| 466 |
+
image_blocks.launch(show_error=True)
|