-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathapp.py
368 lines (338 loc) · 15.7 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import numpy as np
from PIL import Image
from huggingface_hub import snapshot_download
from leffa.transform import LeffaTransform
from leffa.model import LeffaModel
from leffa.inference import LeffaInference
from leffa_utils.garment_agnostic_mask_predictor import AutoMasker
from leffa_utils.densepose_predictor import DensePosePredictor
from leffa_utils.utils import resize_and_center, list_dir, get_agnostic_mask_hd, get_agnostic_mask_dc, preprocess_garment_image
from preprocess.humanparsing.run_parsing import Parsing
from preprocess.openpose.run_openpose import OpenPose
import gradio as gr
# Download checkpoints
snapshot_download(repo_id="franciszzj/Leffa", local_dir="./ckpts")
class LeffaPredictor(object):
def __init__(self):
self.mask_predictor = AutoMasker(
densepose_path="./ckpts/densepose",
schp_path="./ckpts/schp",
)
self.densepose_predictor = DensePosePredictor(
config_path="./ckpts/densepose/densepose_rcnn_R_50_FPN_s1x.yaml",
weights_path="./ckpts/densepose/model_final_162be9.pkl",
)
self.parsing = Parsing(
atr_path="./ckpts/humanparsing/parsing_atr.onnx",
lip_path="./ckpts/humanparsing/parsing_lip.onnx",
)
self.openpose = OpenPose(
body_model_path="./ckpts/openpose/body_pose_model.pth",
)
vt_model_hd = LeffaModel(
pretrained_model_name_or_path="./ckpts/stable-diffusion-inpainting",
pretrained_model="./ckpts/virtual_tryon.pth",
dtype="float16",
)
self.vt_inference_hd = LeffaInference(model=vt_model_hd)
vt_model_dc = LeffaModel(
pretrained_model_name_or_path="./ckpts/stable-diffusion-inpainting",
pretrained_model="./ckpts/virtual_tryon_dc.pth",
dtype="float16",
)
self.vt_inference_dc = LeffaInference(model=vt_model_dc)
pt_model = LeffaModel(
pretrained_model_name_or_path="./ckpts/stable-diffusion-xl-1.0-inpainting-0.1",
pretrained_model="./ckpts/pose_transfer.pth",
dtype="float16",
)
self.pt_inference = LeffaInference(model=pt_model)
def leffa_predict(
self,
src_image_path,
ref_image_path,
control_type,
ref_acceleration=False,
step=50,
scale=2.5,
seed=42,
vt_model_type="viton_hd",
vt_garment_type="upper_body",
vt_repaint=False,
preprocess_garment=False
):
# Open and resize the source image.
src_image = Image.open(src_image_path)
src_image = resize_and_center(src_image, 768, 1024)
# For virtual try-on, optionally preprocess the garment (reference) image.
if control_type == "virtual_tryon" and preprocess_garment:
if isinstance(ref_image_path, str) and ref_image_path.lower().endswith('.png'):
# preprocess_garment_image returns a 768x1024 image.
ref_image = preprocess_garment_image(ref_image_path)
else:
raise ValueError("Reference garment image must be a PNG file when preprocessing is enabled.")
else:
# Otherwise, load the reference image.
ref_image = Image.open(ref_image_path)
ref_image = resize_and_center(ref_image, 768, 1024)
src_image_array = np.array(src_image)
if control_type == "virtual_tryon":
src_image = src_image.convert("RGB")
model_parse, _ = self.parsing(src_image.resize((384, 512)))
keypoints = self.openpose(src_image.resize((384, 512)))
if vt_model_type == "viton_hd":
mask = get_agnostic_mask_hd(model_parse, keypoints, vt_garment_type)
elif vt_model_type == "dress_code":
mask = get_agnostic_mask_dc(model_parse, keypoints, vt_garment_type)
mask = mask.resize((768, 1024))
elif control_type == "pose_transfer":
mask = Image.fromarray(np.ones_like(src_image_array) * 255)
if control_type == "virtual_tryon":
if vt_model_type == "viton_hd":
src_image_seg_array = self.densepose_predictor.predict_seg(src_image_array)[:, :, ::-1]
src_image_seg = Image.fromarray(src_image_seg_array)
densepose = src_image_seg
elif vt_model_type == "dress_code":
src_image_iuv_array = self.densepose_predictor.predict_iuv(src_image_array)
src_image_seg_array = src_image_iuv_array[:, :, 0:1]
src_image_seg_array = np.concatenate([src_image_seg_array] * 3, axis=-1)
src_image_seg = Image.fromarray(src_image_seg_array)
densepose = src_image_seg
elif control_type == "pose_transfer":
src_image_iuv_array = self.densepose_predictor.predict_iuv(src_image_array)[:, :, ::-1]
src_image_iuv = Image.fromarray(src_image_iuv_array)
densepose = src_image_iuv
transform = LeffaTransform()
data = {
"src_image": [src_image],
"ref_image": [ref_image],
"mask": [mask],
"densepose": [densepose],
}
data = transform(data)
if control_type == "virtual_tryon":
if vt_model_type == "viton_hd":
inference = self.vt_inference_hd
elif vt_model_type == "dress_code":
inference = self.vt_inference_dc
elif control_type == "pose_transfer":
inference = self.pt_inference
output = inference(
data,
ref_acceleration=ref_acceleration,
num_inference_steps=step,
guidance_scale=scale,
seed=seed,
repaint=vt_repaint,
)
gen_image = output["generated_image"][0]
return np.array(gen_image), np.array(mask), np.array(densepose)
def leffa_predict_vt(self, src_image_path, ref_image_path, ref_acceleration, step, scale, seed, vt_model_type, vt_garment_type, vt_repaint, preprocess_garment):
return self.leffa_predict(
src_image_path,
ref_image_path,
"virtual_tryon",
ref_acceleration,
step,
scale,
seed,
vt_model_type,
vt_garment_type,
vt_repaint,
preprocess_garment, # Pass through the new flag.
)
def leffa_predict_pt(self, src_image_path, ref_image_path, ref_acceleration, step, scale, seed):
return self.leffa_predict(
src_image_path,
ref_image_path,
"pose_transfer",
ref_acceleration,
step,
scale,
seed,
)
if __name__ == "__main__":
leffa_predictor = LeffaPredictor()
example_dir = "./ckpts/examples"
person1_images = list_dir(f"{example_dir}/person1")
person2_images = list_dir(f"{example_dir}/person2")
garment_images = list_dir(f"{example_dir}/garment")
title = "## Leffa: Learning Flow Fields in Attention for Controllable Person Image Generation"
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)
Star ⭐ us if you like it!
"""
news = """## News
- 09/Jan/2025. Inference defaults to float16, generating an image in 6 seconds (on A100).
More news can be found in the [GitHub repository](https://github.com/franciszzj/Leffa).
"""
description = "Leffa is a unified framework for controllable person image generation that enables precise manipulation of both appearance (i.e., virtual try-on) and pose (i.e., pose transfer)."
note = "Note: The models used in the demo are trained solely on academic datasets. Virtual try-on uses VITON-HD/DressCode, and pose transfer uses DeepFashion."
with gr.Blocks(theme=gr.themes.Default(primary_hue=gr.themes.colors.pink, secondary_hue=gr.themes.colors.red)).queue() as demo:
gr.Markdown(title)
gr.Markdown(link)
gr.Markdown(news)
gr.Markdown(description)
with gr.Tab("Control Appearance (Virtual Try-on)"):
with gr.Row():
with gr.Column():
gr.Markdown("#### Person Image")
vt_src_image = gr.Image(
sources=["upload"],
type="filepath",
label="Person Image",
width=512,
height=512,
)
gr.Examples(
inputs=vt_src_image,
examples_per_page=10,
examples=person1_images,
)
with gr.Column():
gr.Markdown("#### Garment Image")
vt_ref_image = gr.Image(
sources=["upload"],
type="filepath",
label="Garment Image",
width=512,
height=512,
)
# New checkbox to choose preprocessing.
preprocess_garment_checkbox = gr.Checkbox(
label="Preprocess Garment Image (PNG only)",
value=False
)
gr.Examples(
inputs=vt_ref_image,
examples_per_page=10,
examples=garment_images,
)
with gr.Column():
gr.Markdown("#### Generated Image")
vt_gen_image = gr.Image(
label="Generated Image",
width=512,
height=512,
)
with gr.Row():
vt_gen_button = gr.Button("Generate")
with gr.Accordion("Advanced Options", open=False):
vt_model_type = gr.Radio(
label="Model Type",
choices=[("VITON-HD (Recommended)", "viton_hd"),
("DressCode (Experimental)", "dress_code")],
value="viton_hd",
)
vt_garment_type = gr.Radio(
label="Garment Type",
choices=[("Upper", "upper_body"),
("Lower", "lower_body"),
("Dress", "dresses")],
value="upper_body",
)
vt_ref_acceleration = gr.Radio(
label="Accelerate Reference UNet (may slightly reduce performance)",
choices=[("True", True), ("False", False)],
value=False,
)
vt_repaint = gr.Radio(
label="Repaint Mode",
choices=[("True", True), ("False", False)],
value=False,
)
vt_step = gr.Number(
label="Inference Steps", minimum=30, maximum=100, step=1, value=30)
vt_scale = gr.Number(
label="Guidance Scale", minimum=0.1, maximum=5.0, step=0.1, value=2.5)
vt_seed = gr.Number(
label="Random Seed", minimum=-1, maximum=2147483647, step=1, value=42)
with gr.Accordion("Debug", open=False):
vt_mask = gr.Image(
label="Generated Mask",
width=256,
height=256,
)
vt_densepose = gr.Image(
label="Generated DensePose",
width=256,
height=256,
)
# Pass the new checkbox value as an extra input.
vt_gen_button.click(
fn=leffa_predictor.leffa_predict_vt,
inputs=[
vt_src_image, vt_ref_image, vt_ref_acceleration,
vt_step, vt_scale, vt_seed, vt_model_type,
vt_garment_type, vt_repaint, preprocess_garment_checkbox
],
outputs=[vt_gen_image, vt_mask, vt_densepose]
)
with gr.Tab("Control Pose (Pose Transfer)"):
with gr.Row():
with gr.Column():
gr.Markdown("#### Person Image")
pt_ref_image = gr.Image(
sources=["upload"],
type="filepath",
label="Person Image",
width=512,
height=512,
)
gr.Examples(
inputs=pt_ref_image,
examples_per_page=10,
examples=person1_images,
)
with gr.Column():
gr.Markdown("#### Target Pose Person Image")
pt_src_image = gr.Image(
sources=["upload"],
type="filepath",
label="Target Pose Person Image",
width=512,
height=512,
)
gr.Examples(
inputs=pt_src_image,
examples_per_page=10,
examples=person2_images,
)
with gr.Column():
gr.Markdown("#### Generated Image")
pt_gen_image = gr.Image(
label="Generated Image",
width=512,
height=512,
)
with gr.Row():
pose_transfer_gen_button = gr.Button("Generate")
with gr.Accordion("Advanced Options", open=False):
pt_ref_acceleration = gr.Radio(
label="Accelerate Reference UNet",
choices=[("True", True), ("False", False)],
value=False,
)
pt_step = gr.Number(
label="Inference Steps", minimum=30, maximum=100, step=1, value=30)
pt_scale = gr.Number(
label="Guidance Scale", minimum=0.1, maximum=5.0, step=0.1, value=2.5)
pt_seed = gr.Number(
label="Random Seed", minimum=-1, maximum=2147483647, step=1, value=42)
with gr.Accordion("Debug", open=False):
pt_mask = gr.Image(
label="Generated Mask",
width=256,
height=256,
)
pt_densepose = gr.Image(
label="Generated DensePose",
width=256,
height=256,
)
pose_transfer_gen_button.click(
fn=leffa_predictor.leffa_predict_pt,
inputs=[pt_src_image, pt_ref_image, pt_ref_acceleration, pt_step, pt_scale, pt_seed],
outputs=[pt_gen_image, pt_mask, pt_densepose]
)
gr.Markdown(note)
demo.launch(share=True, server_port=7860, allowed_paths=["./ckpts/examples"])