Spaces:
Runtime error
Runtime error
Upload 23 files
Browse files- app.py +382 -131
- cloth/cloth/NAP_1647597315917349_in_post.png +0 -0
- cloth/cloth/NAP_1647597325621176_in_post.png +0 -0
- cloth/cloth/NAP_1647597325684024_in_post.png +0 -0
- cloth/cloth/NAP_1647597326026201_in_post.png +0 -0
- cloth/cloth/NAP_1647597326873307_in_post.png +0 -0
- cloth/cloth/NAP_1647597335012288_in_post.png +0 -0
- cloth/cloth/TPP_JVV1695795105796_in_post.png +0 -0
- cloth/cloth/TPP_JVV1713251711733_in_post.png +0 -0
- cloth/cloth_ori/NAP_1647597315917349_in.webp +0 -0
- cloth/cloth_ori/NAP_1647597325621176_in.webp +0 -0
- cloth/cloth_ori/NAP_1647597325684024_in.webp +0 -0
- cloth/cloth_ori/NAP_1647597326026201_in.webp +0 -0
- cloth/cloth_ori/NAP_1647597326873307_in.webp +0 -0
- cloth/cloth_ori/NAP_1647597335012288_in.webp +0 -0
- cloth/cloth_ori/TPP_JVV1695795105796_in.webp +0 -0
- cloth/cloth_ori/TPP_JVV1713251711733_in.webp +0 -0
- face/face/1.jpg +0 -0
- face/face/2.jpg +0 -0
- face/face/3333.jpg +0 -0
- pose/pose/00034_00.jpg +0 -0
- pose/pose/00121_00.jpg +0 -0
- pose/pose/01992_00.jpg +0 -0
app.py
CHANGED
|
@@ -1,146 +1,397 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
-
import
|
| 4 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import torch
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
pipe.enable_xformers_memory_efficient_attention()
|
| 13 |
-
pipe = pipe.to(device)
|
| 14 |
-
else:
|
| 15 |
-
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
|
| 16 |
-
pipe = pipe.to(device)
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
negative_prompt = negative_prompt,
|
| 31 |
-
guidance_scale = guidance_scale,
|
| 32 |
-
num_inference_steps = num_inference_steps,
|
| 33 |
-
width = width,
|
| 34 |
-
height = height,
|
| 35 |
-
generator = generator
|
| 36 |
-
).images[0]
|
| 37 |
-
|
| 38 |
-
return image
|
| 39 |
-
|
| 40 |
-
examples = [
|
| 41 |
-
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
| 42 |
-
"An astronaut riding a green horse",
|
| 43 |
-
"A delicious ceviche cheesecake slice",
|
| 44 |
-
]
|
| 45 |
-
|
| 46 |
-
css="""
|
| 47 |
-
#col-container {
|
| 48 |
-
margin: 0 auto;
|
| 49 |
-
max-width: 520px;
|
| 50 |
-
}
|
| 51 |
-
"""
|
| 52 |
-
|
| 53 |
-
if torch.cuda.is_available():
|
| 54 |
-
power_device = "GPU"
|
| 55 |
-
else:
|
| 56 |
-
power_device = "CPU"
|
| 57 |
-
|
| 58 |
-
with gr.Blocks(css=css) as demo:
|
| 59 |
-
|
| 60 |
-
with gr.Column(elem_id="col-container"):
|
| 61 |
-
gr.Markdown(f"""
|
| 62 |
-
# Text-to-Image Gradio Template
|
| 63 |
-
Currently running on {power_device}.
|
| 64 |
-
""")
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
label="Prompt",
|
| 70 |
-
show_label=False,
|
| 71 |
-
max_lines=1,
|
| 72 |
-
placeholder="Enter your prompt",
|
| 73 |
-
container=False,
|
| 74 |
-
)
|
| 75 |
-
|
| 76 |
-
run_button = gr.Button("Run", scale=0)
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
)
|
| 96 |
-
|
| 97 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 98 |
-
|
| 99 |
with gr.Row():
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
maximum=MAX_IMAGE_SIZE,
|
| 105 |
-
step=32,
|
| 106 |
-
value=512,
|
| 107 |
-
)
|
| 108 |
-
|
| 109 |
-
height = gr.Slider(
|
| 110 |
-
label="Height",
|
| 111 |
-
minimum=256,
|
| 112 |
-
maximum=MAX_IMAGE_SIZE,
|
| 113 |
-
step=32,
|
| 114 |
-
value=512,
|
| 115 |
-
)
|
| 116 |
-
|
| 117 |
with gr.Row():
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
-
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
|
| 3 |
+
sys.path.append('./')
|
| 4 |
+
from PIL import Image
|
| 5 |
import gradio as gr
|
| 6 |
import numpy as np
|
| 7 |
+
import cv2
|
| 8 |
+
from modelscope.outputs import OutputKeys
|
| 9 |
+
from modelscope.pipelines import pipeline
|
| 10 |
+
from modelscope.utils.constant import Tasks
|
| 11 |
+
from dressing_sd.pipelines.pipeline_sd import PipIpaControlNet
|
| 12 |
+
from diffusers.pipelines.stable_diffusion import StableDiffusionSafetyChecker
|
| 13 |
+
|
| 14 |
+
from torchvision import transforms
|
| 15 |
+
import cv2
|
| 16 |
+
from transformers import CLIPImageProcessor, CLIPVisionModelWithProjection
|
| 17 |
+
import diffusers
|
| 18 |
+
|
| 19 |
+
from transformers import CLIPTextModel, CLIPTokenizer, CLIPVisionModelWithProjection
|
| 20 |
+
from adapter.attention_processor import CacheAttnProcessor2_0, RefSAttnProcessor2_0, RefLoraSAttnProcessor2_0, LoRAIPAttnProcessor2_0
|
| 21 |
+
from diffusers import ControlNetModel, UNet2DConditionModel, \
|
| 22 |
+
AutoencoderKL, DDIMScheduler
|
| 23 |
+
from adapter.resampler import Resampler
|
| 24 |
+
|
| 25 |
+
from transformers import (
|
| 26 |
+
CLIPImageProcessor,
|
| 27 |
+
CLIPVisionModelWithProjection,
|
| 28 |
+
CLIPTextModel,
|
| 29 |
+
CLIPTextModelWithProjection,
|
| 30 |
+
)
|
| 31 |
+
from diffusers import DDPMScheduler, AutoencoderKL, UniPCMultistepScheduler
|
| 32 |
+
from typing import List
|
| 33 |
+
|
| 34 |
import torch
|
| 35 |
|
| 36 |
+
import argparse
|
| 37 |
+
import os
|
| 38 |
+
|
| 39 |
+
from controlnet_aux import OpenposeDetector
|
| 40 |
+
from insightface.app import FaceAnalysis
|
| 41 |
+
from insightface.utils import face_align
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# device = 'cuda:2' if torch.cuda.is_available() else 'cpu'
|
| 45 |
+
|
| 46 |
+
parser = argparse.ArgumentParser(description='ReferenceAdapter diffusion')
|
| 47 |
+
parser.add_argument('--if_resampler', type=bool, default=True)
|
| 48 |
+
parser.add_argument('--if_ipa', type=bool, default=True)
|
| 49 |
+
parser.add_argument('--if_control', type=bool, default=True)
|
| 50 |
+
|
| 51 |
+
parser.add_argument('--pretrained_model_name_or_path',
|
| 52 |
+
default="/home/sf/Realistic_Vision_V4.0_noVAE",
|
| 53 |
+
type=str)
|
| 54 |
+
parser.add_argument('--ip_ckpt',
|
| 55 |
+
default="/home/sf/ip_adapter/ip-adapter-faceid-plus_sd15.bin",
|
| 56 |
+
type=str)
|
| 57 |
+
parser.add_argument('--pretrained_image_encoder_path',
|
| 58 |
+
default="/home/sf/ip_adapter/image_encoder/",
|
| 59 |
+
type=str)
|
| 60 |
+
parser.add_argument('--pretrained_vae_model_path',
|
| 61 |
+
default="/home/sf/sd-vae-ft-mse/",
|
| 62 |
+
type=str)
|
| 63 |
+
parser.add_argument('--model_ckpt',
|
| 64 |
+
default="/home/sf/weights/sd_stage2/mp_rank_00_model_states_0628.pt",
|
| 65 |
+
type=str)
|
| 66 |
+
parser.add_argument('--output_path', type=str, default="./output_ipa_control_resampler")
|
| 67 |
+
parser.add_argument('--device', type=str, default="cuda:0")
|
| 68 |
+
args = parser.parse_args()
|
| 69 |
+
|
| 70 |
+
# svae path
|
| 71 |
+
output_path = args.output_path
|
| 72 |
+
|
| 73 |
+
if not os.path.exists(output_path):
|
| 74 |
+
os.makedirs(output_path)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
generator = torch.Generator(device=args.device).manual_seed(42)
|
| 78 |
+
vae = AutoencoderKL.from_pretrained(args.pretrained_vae_model_path).to(dtype=torch.float16, device=args.device)
|
| 79 |
+
tokenizer = CLIPTokenizer.from_pretrained(args.pretrained_model_name_or_path, subfolder="tokenizer")
|
| 80 |
+
text_encoder = CLIPTextModel.from_pretrained(args.pretrained_model_name_or_path, subfolder="text_encoder").to(
|
| 81 |
+
dtype=torch.float16, device=args.device)
|
| 82 |
+
image_encoder = CLIPVisionModelWithProjection.from_pretrained(args.pretrained_image_encoder_path).to(
|
| 83 |
+
dtype=torch.float16, device=args.device)
|
| 84 |
+
unet = UNet2DConditionModel.from_pretrained(args.pretrained_model_name_or_path, subfolder="unet").to(
|
| 85 |
+
dtype=torch.float16,device=args.device)
|
| 86 |
+
|
| 87 |
+
image_face_fusion = pipeline('face_fusion_torch', model='damo/cv_unet_face_fusion_torch', model_revision='v1.0.3')
|
| 88 |
|
| 89 |
+
#face_model
|
| 90 |
+
app = FaceAnalysis(providers=[('CUDAExecutionProvider', {"device_id": args.device})]) ##使用GPU:0, 默认使用buffalo_l就可以了
|
| 91 |
+
app.prepare(ctx_id=0, det_size=(640, 640))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
# def ref proj weight
|
| 94 |
+
image_proj = Resampler(
|
| 95 |
+
dim=unet.config.cross_attention_dim,
|
| 96 |
+
depth=4,
|
| 97 |
+
dim_head=64,
|
| 98 |
+
heads=12,
|
| 99 |
+
num_queries=16,
|
| 100 |
+
embedding_dim=image_encoder.config.hidden_size,
|
| 101 |
+
output_dim=unet.config.cross_attention_dim,
|
| 102 |
+
ff_mult=4
|
| 103 |
+
)
|
| 104 |
+
image_proj = image_proj.to(dtype=torch.float16, device=args.device)
|
| 105 |
|
| 106 |
+
# set attention processor
|
| 107 |
+
attn_procs = {}
|
| 108 |
+
st = unet.state_dict()
|
| 109 |
+
for name in unet.attn_processors.keys():
|
| 110 |
+
cross_attention_dim = None if name.endswith("attn1.processor") else unet.config.cross_attention_dim
|
| 111 |
+
if name.startswith("mid_block"):
|
| 112 |
+
hidden_size = unet.config.block_out_channels[-1]
|
| 113 |
+
elif name.startswith("up_blocks"):
|
| 114 |
+
block_id = int(name[len("up_blocks.")])
|
| 115 |
+
hidden_size = list(reversed(unet.config.block_out_channels))[block_id]
|
| 116 |
+
elif name.startswith("down_blocks"):
|
| 117 |
+
block_id = int(name[len("down_blocks.")])
|
| 118 |
+
hidden_size = unet.config.block_out_channels[block_id]
|
| 119 |
+
# lora_rank = hidden_size // 2 # args.lora_rank
|
| 120 |
+
if cross_attention_dim is None:
|
| 121 |
+
attn_procs[name] = RefLoraSAttnProcessor2_0(name, hidden_size)
|
| 122 |
+
else:
|
| 123 |
+
attn_procs[name] = LoRAIPAttnProcessor2_0(hidden_size=hidden_size, cross_attention_dim=cross_attention_dim)
|
| 124 |
|
| 125 |
+
unet.set_attn_processor(attn_procs)
|
| 126 |
+
adapter_modules = torch.nn.ModuleList(unet.attn_processors.values())
|
| 127 |
+
adapter_modules = adapter_modules.to(dtype=torch.float16, device=args.device)
|
| 128 |
+
del st
|
| 129 |
+
|
| 130 |
+
ref_unet = UNet2DConditionModel.from_pretrained(args.pretrained_model_name_or_path, subfolder="unet").to(
|
| 131 |
+
dtype=torch.float16,
|
| 132 |
+
device=args.device)
|
| 133 |
+
ref_unet.set_attn_processor(
|
| 134 |
+
{name: CacheAttnProcessor2_0() for name in ref_unet.attn_processors.keys()}) # set cache
|
| 135 |
+
|
| 136 |
+
# weights load
|
| 137 |
+
model_sd = torch.load(args.model_ckpt, map_location="cpu")["module"]
|
| 138 |
+
|
| 139 |
+
ref_unet_dict = {}
|
| 140 |
+
unet_dict = {}
|
| 141 |
+
image_proj_dict = {}
|
| 142 |
+
adapter_modules_dict = {}
|
| 143 |
+
for k in model_sd.keys():
|
| 144 |
+
if k.startswith("ref_unet"):
|
| 145 |
+
ref_unet_dict[k.replace("ref_unet.", "")] = model_sd[k]
|
| 146 |
+
elif k.startswith("unet"):
|
| 147 |
+
unet_dict[k.replace("unet.", "")] = model_sd[k]
|
| 148 |
+
elif k.startswith("proj"):
|
| 149 |
+
image_proj_dict[k.replace("proj.", "")] = model_sd[k]
|
| 150 |
+
elif k.startswith("adapter_modules") and 'ref' in k:
|
| 151 |
+
adapter_modules_dict[k.replace("adapter_modules.", "")] = model_sd[k]
|
| 152 |
+
else:
|
| 153 |
+
print(k)
|
| 154 |
+
|
| 155 |
+
ref_unet.load_state_dict(ref_unet_dict)
|
| 156 |
+
image_proj.load_state_dict(image_proj_dict)
|
| 157 |
+
adapter_modules.load_state_dict(adapter_modules_dict, strict=False)
|
| 158 |
+
|
| 159 |
+
noise_scheduler = DDIMScheduler(
|
| 160 |
+
num_train_timesteps=1000,
|
| 161 |
+
beta_start=0.00085,
|
| 162 |
+
beta_end=0.012,
|
| 163 |
+
beta_schedule="scaled_linear",
|
| 164 |
+
clip_sample=False,
|
| 165 |
+
set_alpha_to_one=False,
|
| 166 |
+
steps_offset=1,
|
| 167 |
+
)
|
| 168 |
+
# noise_scheduler = UniPCMultistepScheduler.from_config(args.pretrained_model_name_or_path, subfolder="scheduler")
|
| 169 |
+
|
| 170 |
+
control_net_openpose = ControlNetModel.from_pretrained(
|
| 171 |
+
"/home/sf/control_v11p_sd15_openpose",
|
| 172 |
+
torch_dtype=torch.float16).to(device=args.device)
|
| 173 |
+
# pipe = PipIpaControlNet(unet=unet, reference_unet=ref_unet, vae=vae, tokenizer=tokenizer,
|
| 174 |
+
# text_encoder=text_encoder, image_encoder=image_encoder,
|
| 175 |
+
# ip_ckpt=args.ip_ckpt,
|
| 176 |
+
# ImgProj=image_proj, controlnet=control_net_openpose,
|
| 177 |
+
# scheduler=noise_scheduler,
|
| 178 |
+
# safety_checker=StableDiffusionSafetyChecker,
|
| 179 |
+
# feature_extractor=CLIPImageProcessor)
|
| 180 |
+
|
| 181 |
+
img_transform = transforms.Compose([
|
| 182 |
+
transforms.Resize([640, 512], interpolation=transforms.InterpolationMode.BILINEAR),
|
| 183 |
+
transforms.ToTensor(),
|
| 184 |
+
transforms.Normalize([0.5], [0.5]),
|
| 185 |
+
])
|
| 186 |
+
|
| 187 |
+
openpose_model = OpenposeDetector.from_pretrained("/home/sf/ControlNet").to(args.device)
|
| 188 |
+
|
| 189 |
+
def resize_img(input_image, max_side=640, min_side=512, size=None,
|
| 190 |
+
pad_to_max_side=False, mode=Image.BILINEAR, base_pixel_number=64):
|
| 191 |
+
w, h = input_image.size
|
| 192 |
+
ratio = min_side / min(h, w)
|
| 193 |
+
w, h = round(ratio*w), round(ratio*h)
|
| 194 |
+
ratio = max_side / max(h, w)
|
| 195 |
+
input_image = input_image.resize([round(ratio*w), round(ratio*h)], mode)
|
| 196 |
+
w_resize_new = (round(ratio * w) // base_pixel_number) * base_pixel_number
|
| 197 |
+
h_resize_new = (round(ratio * h) // base_pixel_number) * base_pixel_number
|
| 198 |
+
input_image = input_image.resize([w_resize_new, h_resize_new], mode)
|
| 199 |
+
return input_image
|
| 200 |
+
|
| 201 |
+
def tryon_process(garm_img, face_img, pose_img, prompt, cloth_guidance_scale, caption_guidance_scale,
|
| 202 |
+
face_guidance_scale,self_guidance_scale, cross_guidance_scale,if_ipa, if_post, if_control, denoise_steps, seed=42):
|
| 203 |
+
# prompt = prompt + ', confident smile expression, fashion, best quality, amazing quality, very aesthetic'
|
| 204 |
+
if prompt is None:
|
| 205 |
+
prompt = "a photography of a model"
|
| 206 |
+
prompt = prompt + ', best quality, high quality'
|
| 207 |
+
print(prompt, cloth_guidance_scale, if_ipa, if_control, denoise_steps, seed)
|
| 208 |
+
clip_image_processor = CLIPImageProcessor()
|
| 209 |
+
# clothes_img = garm_img.convert("RGB")
|
| 210 |
+
if not garm_img:
|
| 211 |
+
raise gr.Error("请上传衣服 / Please upload garment")
|
| 212 |
+
clothes_img = resize_img(garm_img)
|
| 213 |
+
vae_clothes = img_transform(clothes_img).unsqueeze(0)
|
| 214 |
+
# print(vae_clothes.shape)
|
| 215 |
+
ref_clip_image = clip_image_processor(images=clothes_img, return_tensors="pt").pixel_values
|
| 216 |
+
|
| 217 |
+
if if_ipa:
|
| 218 |
+
# image = cv2.imread(face_img)
|
| 219 |
+
faces = app.get(face_img)
|
| 220 |
|
| 221 |
+
if not faces:
|
| 222 |
+
raise gr.Error("人脸检测异常,尝试其他肖像 / Abnormal face detection. Try another portrait")
|
| 223 |
+
faceid_embeds = torch.from_numpy(faces[0].normed_embedding).unsqueeze(0)
|
| 224 |
+
face_image = face_align.norm_crop(face_img, landmark=faces[0].kps, image_size=224) # you can also segment the face
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
|
| 226 |
+
# face_img = face_image[:, :, ::-1]
|
| 227 |
+
# face_img = Image.fromarray(face_image.astype('uint8'))
|
| 228 |
+
# face_img.save('face.png')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
+
face_clip_image = clip_image_processor(images=face_image, return_tensors="pt").pixel_values
|
| 231 |
+
else:
|
| 232 |
+
faceid_embeds = None
|
| 233 |
+
face_clip_image = None
|
| 234 |
+
|
| 235 |
+
if if_control:
|
| 236 |
+
pose_img = openpose_model(pose_img.convert("RGB"))
|
| 237 |
+
# pose_img.save('pose.png')
|
| 238 |
+
pose_image = diffusers.utils.load_image(pose_img)
|
| 239 |
+
else:
|
| 240 |
+
pose_image = None
|
| 241 |
+
# print(if_ipa, if_control)
|
| 242 |
+
# pipe, generator = prepare_pipeline(args, if_ipa, if_control, unet, ref_unet, vae, tokenizer, text_encoder,
|
| 243 |
+
# image_encoder, image_proj, control_net_openpose)
|
| 244 |
+
|
| 245 |
+
noise_scheduler = DDIMScheduler(
|
| 246 |
+
num_train_timesteps=1000,
|
| 247 |
+
beta_start=0.00085,
|
| 248 |
+
beta_end=0.012,
|
| 249 |
+
beta_schedule="scaled_linear",
|
| 250 |
+
clip_sample=False,
|
| 251 |
+
set_alpha_to_one=False,
|
| 252 |
+
steps_offset=1,
|
| 253 |
+
)
|
| 254 |
+
# noise_scheduler = UniPCMultistepScheduler.from_config(args.pretrained_model_name_or_path, subfolder="scheduler")
|
| 255 |
+
pipe = PipIpaControlNet(unet=unet, reference_unet=ref_unet, vae=vae, tokenizer=tokenizer,
|
| 256 |
+
text_encoder=text_encoder, image_encoder=image_encoder,
|
| 257 |
+
ip_ckpt=args.ip_ckpt,
|
| 258 |
+
ImgProj=image_proj, controlnet=control_net_openpose,
|
| 259 |
+
scheduler=noise_scheduler,
|
| 260 |
+
safety_checker=StableDiffusionSafetyChecker,
|
| 261 |
+
feature_extractor=CLIPImageProcessor)
|
| 262 |
+
output = pipe(
|
| 263 |
+
ref_image=vae_clothes,
|
| 264 |
+
prompt=prompt,
|
| 265 |
+
ref_clip_image=ref_clip_image,
|
| 266 |
+
pose_image=pose_image,
|
| 267 |
+
face_clip_image=face_clip_image,
|
| 268 |
+
faceid_embeds=faceid_embeds,
|
| 269 |
+
null_prompt='',
|
| 270 |
+
negative_prompt='bare, naked, nude, undressed, monochrome, lowres, bad anatomy, worst quality, low quality',
|
| 271 |
+
width=512,
|
| 272 |
+
height=640,
|
| 273 |
+
num_images_per_prompt=1,
|
| 274 |
+
guidance_scale=caption_guidance_scale,
|
| 275 |
+
image_scale=cloth_guidance_scale,
|
| 276 |
+
ipa_scale=face_guidance_scale,
|
| 277 |
+
s_lora_scale= self_guidance_scale,
|
| 278 |
+
c_lora_scale= cross_guidance_scale,
|
| 279 |
+
generator=generator,
|
| 280 |
+
num_inference_steps=denoise_steps,
|
| 281 |
+
).images
|
| 282 |
+
|
| 283 |
+
if if_post and if_ipa:
|
| 284 |
+
# 将 PIL 图像转换为 NumPy 数组
|
| 285 |
+
output_array = np.array(output[0])
|
| 286 |
+
# 将 RGB 图像转换为 BGR 图像
|
| 287 |
+
bgr_array = cv2.cvtColor(output_array, cv2.COLOR_RGB2BGR)
|
| 288 |
+
# 将 NumPy 数组转换为 PIL 图像
|
| 289 |
+
bgr_image = Image.fromarray(bgr_array)
|
| 290 |
+
result = image_face_fusion(dict(template=bgr_image, user=Image.fromarray(face_image.astype('uint8'))))
|
| 291 |
+
return result[OutputKeys.OUTPUT_IMG]
|
| 292 |
+
return output[0]
|
| 293 |
+
|
| 294 |
+
example_path = os.path.dirname(__file__)
|
| 295 |
+
|
| 296 |
+
garm_list = os.listdir(os.path.join(example_path, "cloth", 'cloth'))
|
| 297 |
+
garm_list_path = [os.path.join(example_path, "cloth", 'cloth', garm) for garm in garm_list]
|
| 298 |
+
|
| 299 |
+
face_list = os.listdir(os.path.join(example_path, "face", 'face'))
|
| 300 |
+
face_list_path = [os.path.join(example_path, "face", 'face', face) for face in face_list]
|
| 301 |
+
|
| 302 |
+
pose_list = os.listdir(os.path.join(example_path, "pose", 'pose'))
|
| 303 |
+
pose_list_path = [os.path.join(example_path, "pose", 'pose', pose) for pose in pose_list]
|
| 304 |
+
|
| 305 |
+
|
| 306 |
+
|
| 307 |
+
##default human
|
| 308 |
+
|
| 309 |
+
|
| 310 |
+
image_blocks = gr.Blocks().queue()
|
| 311 |
+
with image_blocks as demo:
|
| 312 |
+
gr.Markdown("## IMAGDressing-v1: Customizable Virtual Dressing 👕👔👚")
|
| 313 |
+
gr.Markdown(
|
| 314 |
+
"Customize your virtual look with ease—adjust your appearance, pose, and garment as you like<br>."
|
| 315 |
+
"If you enjoy this project, please check out the [source codes](https://github.com/muzishen/IMAGDressing) and [model](https://huggingface.co/feishen29/IMAGDressing). Do not hesitate to give us a star. Thank you!<br>"
|
| 316 |
+
"Your support fuels the development of new versions."
|
| 317 |
+
)
|
| 318 |
+
with gr.Row():
|
| 319 |
+
with gr.Column():
|
| 320 |
+
garm_img = gr.Image(label="Garment", sources='upload', type="pil")
|
| 321 |
+
example = gr.Examples(
|
| 322 |
+
inputs=garm_img,
|
| 323 |
+
examples_per_page=8,
|
| 324 |
+
examples=garm_list_path)
|
| 325 |
+
|
| 326 |
+
with gr.Column():
|
| 327 |
+
imgs = gr.Image(label="Face", sources='upload', type="numpy")
|
| 328 |
+
|
| 329 |
+
with gr.Row():
|
| 330 |
+
is_checked_face = gr.Checkbox(label="Yes", info="Use face ", value=False)
|
| 331 |
+
example = gr.Examples(
|
| 332 |
+
inputs=imgs,
|
| 333 |
+
examples_per_page=10,
|
| 334 |
+
examples=face_list_path
|
| 335 |
)
|
|
|
|
|
|
|
|
|
|
| 336 |
with gr.Row():
|
| 337 |
+
is_checked_postprocess = gr.Checkbox(label="Yes", info="Use postprocess ", value=False)
|
| 338 |
+
|
| 339 |
+
with gr.Column():
|
| 340 |
+
pose_img = gr.Image(label="Pose", sources='upload', type="pil")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
with gr.Row():
|
| 342 |
+
is_checked_pose = gr.Checkbox(label="Yes", info="Use pose ", value=False)
|
| 343 |
+
|
| 344 |
+
example = gr.Examples(
|
| 345 |
+
inputs=pose_img,
|
| 346 |
+
examples_per_page=8,
|
| 347 |
+
examples=pose_list_path)
|
| 348 |
+
|
| 349 |
+
# with gr.Column():
|
| 350 |
+
# # image_out = gr.Image(label="Output", elem_id="output-img", height=400)
|
| 351 |
+
# masked_img = gr.Image(label="Masked image output", elem_id="masked-img", show_share_button=False)
|
| 352 |
+
with gr.Column():
|
| 353 |
+
# image_out = gr.Image(label="Output", elem_id="output-img", height=400)
|
| 354 |
+
image_out = gr.Image(label="Output", elem_id="output-img", show_share_button=False)
|
| 355 |
+
# Add usage tips below the output image
|
| 356 |
+
gr.Markdown("""
|
| 357 |
+
### Usage Tips
|
| 358 |
+
- **Upload Images**: Upload your desired garment, face, and pose images in the respective sections.
|
| 359 |
+
- **Select Options**: Use the checkboxes to include face and pose in the generated output.
|
| 360 |
+
- **View Output**: The resulting image will be displayed in the Output section.
|
| 361 |
+
- **Examples**: Click on example images to quickly load and test different configurations.
|
| 362 |
+
- **Advanced Settings**: Click on **Advanced Settings** to edit captions and adjust hyperparameters.
|
| 363 |
+
- **Feedback**: If you have any issues or suggestions, please let us know through the [GitHub repository](https://github.com/muzishen/IMAGDressing).
|
| 364 |
+
""")
|
| 365 |
+
with gr.Column():
|
| 366 |
+
try_button = gr.Button(value="Dressing")
|
| 367 |
+
with gr.Accordion(label="Advanced Settings", open=False):
|
| 368 |
+
with gr.Row(elem_id="prompt-container"):
|
| 369 |
+
with gr.Row():
|
| 370 |
+
prompt = gr.Textbox(placeholder="Description of prompt ex) A beautiful woman dress Short Sleeve Round Neck T-shirts",value='A beautiful woman',
|
| 371 |
+
show_label=False, elem_id="prompt")
|
| 372 |
+
# with gr.Row():
|
| 373 |
+
# neg_prompt = gr.Textbox(placeholder="Description of neg prompt ex) Short Sleeve Round Neck T-shirts",
|
| 374 |
+
# show_label=False, elem_id="neg_prompt")
|
| 375 |
+
with gr.Row():
|
| 376 |
+
cloth_guidance_scale = gr.Slider(label="Cloth guidance Scale", minimum=0.0, maximum=1.0, value=0.9, step=0.1,
|
| 377 |
+
visible=True)
|
| 378 |
+
with gr.Row():
|
| 379 |
+
caption_guidance_scale = gr.Slider(label="Prompt Guidance Scale", minimum=1, maximum=10., value=7.0, step=0.1,
|
| 380 |
+
visible=True)
|
| 381 |
+
with gr.Row():
|
| 382 |
+
face_guidance_scale = gr.Slider(label="Face Guidance Scale", minimum=0.0, maximum=2.0, value=0.9, step=0.1,
|
| 383 |
+
visible=True)
|
| 384 |
+
with gr.Row():
|
| 385 |
+
self_guidance_scale = gr.Slider(label="Self-Attention Lora Scale", minimum=0.0, maximum=0.5, value=0.2, step=0.1,
|
| 386 |
+
visible=True)
|
| 387 |
+
with gr.Row():
|
| 388 |
+
cross_guidance_scale = gr.Slider(label="Cross-Attention Lora Scale", minimum=0.0, maximum=0.5, value=0.2, step=0.1,
|
| 389 |
+
visible=True)
|
| 390 |
+
with gr.Row():
|
| 391 |
+
denoise_steps = gr.Number(label="Denoising Steps", minimum=20, maximum=50, value=30, step=1)
|
| 392 |
+
seed = gr.Number(label="Seed", minimum=-1, maximum=2147483647, step=1, value=20240508)
|
| 393 |
+
|
| 394 |
+
try_button.click(fn=tryon_process, inputs=[garm_img, imgs, pose_img, prompt, cloth_guidance_scale, caption_guidance_scale, face_guidance_scale,self_guidance_scale, cross_guidance_scale, is_checked_face, is_checked_postprocess, is_checked_pose, denoise_steps, seed],
|
| 395 |
+
outputs=[image_out], api_name='tryon')
|
| 396 |
|
| 397 |
+
image_blocks.launch(server_port=20021) # 指定固定端口
|
cloth/cloth/NAP_1647597315917349_in_post.png
ADDED
|
cloth/cloth/NAP_1647597325621176_in_post.png
ADDED
|
cloth/cloth/NAP_1647597325684024_in_post.png
ADDED
|
cloth/cloth/NAP_1647597326026201_in_post.png
ADDED
|
cloth/cloth/NAP_1647597326873307_in_post.png
ADDED
|
cloth/cloth/NAP_1647597335012288_in_post.png
ADDED
|
cloth/cloth/TPP_JVV1695795105796_in_post.png
ADDED
|
cloth/cloth/TPP_JVV1713251711733_in_post.png
ADDED
|
cloth/cloth_ori/NAP_1647597315917349_in.webp
ADDED
|
cloth/cloth_ori/NAP_1647597325621176_in.webp
ADDED
|
cloth/cloth_ori/NAP_1647597325684024_in.webp
ADDED
|
cloth/cloth_ori/NAP_1647597326026201_in.webp
ADDED
|
cloth/cloth_ori/NAP_1647597326873307_in.webp
ADDED
|
cloth/cloth_ori/NAP_1647597335012288_in.webp
ADDED
|
cloth/cloth_ori/TPP_JVV1695795105796_in.webp
ADDED
|
cloth/cloth_ori/TPP_JVV1713251711733_in.webp
ADDED
|
face/face/1.jpg
ADDED
|
face/face/2.jpg
ADDED
|
face/face/3333.jpg
ADDED
|
pose/pose/00034_00.jpg
ADDED
|
pose/pose/00121_00.jpg
ADDED
|
pose/pose/01992_00.jpg
ADDED
|