Spaces:
Running
on
Zero
Running
on
Zero
Migrate to ZeroGPU
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
from PIL import Image
|
| 3 |
from huggingface_hub import snapshot_download
|
|
@@ -15,148 +16,144 @@ import gradio as gr
|
|
| 15 |
# Download checkpoints
|
| 16 |
snapshot_download(repo_id="franciszzj/Leffa", local_dir="./ckpts")
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
# DensePose
|
| 102 |
-
if control_type == "virtual_tryon":
|
| 103 |
-
if vt_model_type == "viton_hd":
|
| 104 |
-
src_image_seg_array = self.densepose_predictor.predict_seg(
|
| 105 |
-
src_image_array)[:, :, ::-1]
|
| 106 |
-
src_image_seg = Image.fromarray(src_image_seg_array)
|
| 107 |
-
densepose = src_image_seg
|
| 108 |
-
elif vt_model_type == "dress_code":
|
| 109 |
-
src_image_iuv_array = self.densepose_predictor.predict_iuv(
|
| 110 |
-
src_image_array)
|
| 111 |
-
src_image_seg_array = src_image_iuv_array[:, :, 0:1]
|
| 112 |
-
src_image_seg_array = np.concatenate(
|
| 113 |
-
[src_image_seg_array] * 3, axis=-1)
|
| 114 |
-
src_image_seg = Image.fromarray(src_image_seg_array)
|
| 115 |
-
densepose = src_image_seg
|
| 116 |
-
elif control_type == "pose_transfer":
|
| 117 |
-
src_image_iuv_array = self.densepose_predictor.predict_iuv(
|
| 118 |
src_image_array)[:, :, ::-1]
|
| 119 |
-
|
| 120 |
-
densepose =
|
| 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 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
|
| 157 |
if __name__ == "__main__":
|
| 158 |
|
| 159 |
-
leffa_predictor = LeffaPredictor()
|
| 160 |
example_dir = "./ckpts/examples"
|
| 161 |
person1_images = list_dir(f"{example_dir}/person1")
|
| 162 |
person2_images = list_dir(f"{example_dir}/person2")
|
|
@@ -164,7 +161,7 @@ if __name__ == "__main__":
|
|
| 164 |
|
| 165 |
title = "## Leffa: Learning Flow Fields in Attention for Controllable Person Image Generation"
|
| 166 |
link = """[π Paper](https://arxiv.org/abs/2412.08486) - [π€ Code](https://github.com/franciszzj/Leffa) - [π₯ Demo](https://huggingface.co/spaces/franciszzj/Leffa) - [π€ Model](https://huggingface.co/franciszzj/Leffa)
|
| 167 |
-
|
| 168 |
Star β us if you like it!
|
| 169 |
"""
|
| 170 |
news = """## News
|
|
@@ -277,7 +274,7 @@ if __name__ == "__main__":
|
|
| 277 |
height=256,
|
| 278 |
)
|
| 279 |
|
| 280 |
-
vt_gen_button.click(fn=
|
| 281 |
vt_src_image, vt_ref_image, vt_ref_acceleration, vt_step, vt_scale, vt_seed, vt_model_type, vt_garment_type, vt_repaint], outputs=[vt_gen_image, vt_mask, vt_densepose])
|
| 282 |
|
| 283 |
with gr.Tab("Control Pose (Pose Transfer)"):
|
|
@@ -354,7 +351,7 @@ if __name__ == "__main__":
|
|
| 354 |
height=256,
|
| 355 |
)
|
| 356 |
|
| 357 |
-
pose_transfer_gen_button.click(fn=
|
| 358 |
pt_src_image, pt_ref_image, pt_ref_acceleration, pt_step, pt_scale, pt_seed], outputs=[pt_gen_image, pt_mask, pt_densepose])
|
| 359 |
|
| 360 |
gr.Markdown(note)
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
from huggingface_hub import snapshot_download
|
|
|
|
| 16 |
# Download checkpoints
|
| 17 |
snapshot_download(repo_id="franciszzj/Leffa", local_dir="./ckpts")
|
| 18 |
|
| 19 |
+
# Initialize models as global variables
|
| 20 |
+
mask_predictor = AutoMasker(
|
| 21 |
+
densepose_path="./ckpts/densepose",
|
| 22 |
+
schp_path="./ckpts/schp",
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
densepose_predictor = DensePosePredictor(
|
| 26 |
+
config_path="./ckpts/densepose/densepose_rcnn_R_50_FPN_s1x.yaml",
|
| 27 |
+
weights_path="./ckpts/densepose/model_final_162be9.pkl",
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
parsing = Parsing(
|
| 31 |
+
atr_path="./ckpts/humanparsing/parsing_atr.onnx",
|
| 32 |
+
lip_path="./ckpts/humanparsing/parsing_lip.onnx",
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
openpose = OpenPose(
|
| 36 |
+
body_model_path="./ckpts/openpose/body_pose_model.pth",
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
vt_model_hd = LeffaModel(
|
| 40 |
+
pretrained_model_name_or_path="./ckpts/stable-diffusion-inpainting",
|
| 41 |
+
pretrained_model="./ckpts/virtual_tryon.pth",
|
| 42 |
+
dtype="float16",
|
| 43 |
+
)
|
| 44 |
+
vt_inference_hd = LeffaInference(model=vt_model_hd)
|
| 45 |
+
|
| 46 |
+
vt_model_dc = LeffaModel(
|
| 47 |
+
pretrained_model_name_or_path="./ckpts/stable-diffusion-inpainting",
|
| 48 |
+
pretrained_model="./ckpts/virtual_tryon_dc.pth",
|
| 49 |
+
dtype="float16",
|
| 50 |
+
)
|
| 51 |
+
vt_inference_dc = LeffaInference(model=vt_model_dc)
|
| 52 |
+
|
| 53 |
+
pt_model = LeffaModel(
|
| 54 |
+
pretrained_model_name_or_path="./ckpts/stable-diffusion-xl-1.0-inpainting-0.1",
|
| 55 |
+
pretrained_model="./ckpts/pose_transfer.pth",
|
| 56 |
+
dtype="float16",
|
| 57 |
+
)
|
| 58 |
+
pt_inference = LeffaInference(model=pt_model)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
@spaces.GPU(duration=120)
|
| 62 |
+
def leffa_predict(
|
| 63 |
+
src_image_path,
|
| 64 |
+
ref_image_path,
|
| 65 |
+
control_type,
|
| 66 |
+
ref_acceleration=False,
|
| 67 |
+
step=50,
|
| 68 |
+
scale=2.5,
|
| 69 |
+
seed=42,
|
| 70 |
+
vt_model_type="viton_hd",
|
| 71 |
+
vt_garment_type="upper_body",
|
| 72 |
+
vt_repaint=False,
|
| 73 |
+
):
|
| 74 |
+
assert control_type in [
|
| 75 |
+
"virtual_tryon", "pose_transfer"], "Invalid control type: {}".format(control_type)
|
| 76 |
+
src_image = Image.open(src_image_path)
|
| 77 |
+
ref_image = Image.open(ref_image_path)
|
| 78 |
+
src_image = resize_and_center(src_image, 768, 1024)
|
| 79 |
+
ref_image = resize_and_center(ref_image, 768, 1024)
|
| 80 |
+
|
| 81 |
+
src_image_array = np.array(src_image)
|
| 82 |
+
|
| 83 |
+
# Mask
|
| 84 |
+
if control_type == "virtual_tryon":
|
| 85 |
+
src_image = src_image.convert("RGB")
|
| 86 |
+
model_parse, _ = parsing(src_image.resize((384, 512)))
|
| 87 |
+
keypoints = openpose(src_image.resize((384, 512)))
|
| 88 |
+
if vt_model_type == "viton_hd":
|
| 89 |
+
mask = get_agnostic_mask_hd(
|
| 90 |
+
model_parse, keypoints, vt_garment_type)
|
| 91 |
+
elif vt_model_type == "dress_code":
|
| 92 |
+
mask = get_agnostic_mask_dc(
|
| 93 |
+
model_parse, keypoints, vt_garment_type)
|
| 94 |
+
mask = mask.resize((768, 1024))
|
| 95 |
+
elif control_type == "pose_transfer":
|
| 96 |
+
mask = Image.fromarray(np.ones_like(src_image_array) * 255)
|
| 97 |
+
|
| 98 |
+
# DensePose
|
| 99 |
+
if control_type == "virtual_tryon":
|
| 100 |
+
if vt_model_type == "viton_hd":
|
| 101 |
+
src_image_seg_array = densepose_predictor.predict_seg(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
src_image_array)[:, :, ::-1]
|
| 103 |
+
src_image_seg = Image.fromarray(src_image_seg_array)
|
| 104 |
+
densepose = src_image_seg
|
| 105 |
+
elif vt_model_type == "dress_code":
|
| 106 |
+
src_image_iuv_array = densepose_predictor.predict_iuv(
|
| 107 |
+
src_image_array)
|
| 108 |
+
src_image_seg_array = src_image_iuv_array[:, :, 0:1]
|
| 109 |
+
src_image_seg_array = np.concatenate(
|
| 110 |
+
[src_image_seg_array] * 3, axis=-1)
|
| 111 |
+
src_image_seg = Image.fromarray(src_image_seg_array)
|
| 112 |
+
densepose = src_image_seg
|
| 113 |
+
elif control_type == "pose_transfer":
|
| 114 |
+
src_image_iuv_array = densepose_predictor.predict_iuv(
|
| 115 |
+
src_image_array)[:, :, ::-1]
|
| 116 |
+
src_image_iuv = Image.fromarray(src_image_iuv_array)
|
| 117 |
+
densepose = src_image_iuv
|
| 118 |
+
|
| 119 |
+
# Leffa
|
| 120 |
+
transform = LeffaTransform()
|
| 121 |
+
|
| 122 |
+
data = {
|
| 123 |
+
"src_image": [src_image],
|
| 124 |
+
"ref_image": [ref_image],
|
| 125 |
+
"mask": [mask],
|
| 126 |
+
"densepose": [densepose],
|
| 127 |
+
}
|
| 128 |
+
data = transform(data)
|
| 129 |
+
if control_type == "virtual_tryon":
|
| 130 |
+
if vt_model_type == "viton_hd":
|
| 131 |
+
inference = vt_inference_hd
|
| 132 |
+
elif vt_model_type == "dress_code":
|
| 133 |
+
inference = vt_inference_dc
|
| 134 |
+
elif control_type == "pose_transfer":
|
| 135 |
+
inference = pt_inference
|
| 136 |
+
output = inference(
|
| 137 |
+
data,
|
| 138 |
+
ref_acceleration=ref_acceleration,
|
| 139 |
+
num_inference_steps=step,
|
| 140 |
+
guidance_scale=scale,
|
| 141 |
+
seed=seed,
|
| 142 |
+
repaint=vt_repaint,)
|
| 143 |
+
gen_image = output["generated_image"][0]
|
| 144 |
+
return np.array(gen_image), np.array(mask), np.array(densepose)
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
def leffa_predict_vt(src_image_path, ref_image_path, ref_acceleration, step, scale, seed, vt_model_type, vt_garment_type, vt_repaint):
|
| 148 |
+
return leffa_predict(src_image_path, ref_image_path, "virtual_tryon", ref_acceleration, step, scale, seed, vt_model_type, vt_garment_type, vt_repaint)
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
def leffa_predict_pt(src_image_path, ref_image_path, ref_acceleration, step, scale, seed):
|
| 152 |
+
return leffa_predict(src_image_path, ref_image_path, "pose_transfer", ref_acceleration, step, scale, seed)
|
| 153 |
|
| 154 |
|
| 155 |
if __name__ == "__main__":
|
| 156 |
|
|
|
|
| 157 |
example_dir = "./ckpts/examples"
|
| 158 |
person1_images = list_dir(f"{example_dir}/person1")
|
| 159 |
person2_images = list_dir(f"{example_dir}/person2")
|
|
|
|
| 161 |
|
| 162 |
title = "## Leffa: Learning Flow Fields in Attention for Controllable Person Image Generation"
|
| 163 |
link = """[π Paper](https://arxiv.org/abs/2412.08486) - [π€ Code](https://github.com/franciszzj/Leffa) - [π₯ Demo](https://huggingface.co/spaces/franciszzj/Leffa) - [π€ Model](https://huggingface.co/franciszzj/Leffa)
|
| 164 |
+
|
| 165 |
Star β us if you like it!
|
| 166 |
"""
|
| 167 |
news = """## News
|
|
|
|
| 274 |
height=256,
|
| 275 |
)
|
| 276 |
|
| 277 |
+
vt_gen_button.click(fn=leffa_predict_vt, inputs=[
|
| 278 |
vt_src_image, vt_ref_image, vt_ref_acceleration, vt_step, vt_scale, vt_seed, vt_model_type, vt_garment_type, vt_repaint], outputs=[vt_gen_image, vt_mask, vt_densepose])
|
| 279 |
|
| 280 |
with gr.Tab("Control Pose (Pose Transfer)"):
|
|
|
|
| 351 |
height=256,
|
| 352 |
)
|
| 353 |
|
| 354 |
+
pose_transfer_gen_button.click(fn=leffa_predict_pt, inputs=[
|
| 355 |
pt_src_image, pt_ref_image, pt_ref_acceleration, pt_step, pt_scale, pt_seed], outputs=[pt_gen_image, pt_mask, pt_densepose])
|
| 356 |
|
| 357 |
gr.Markdown(note)
|