-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
344 lines (306 loc) · 12.1 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
import cv2
import gradio as gr
import numpy as np
import torch
from PIL import Image
from config import create_cfg, merge_possible_with_base
from modeling.model import build_model
from modeling.text_translation import TextTranslationDiffusion
from modeling.translation import TranslationDiffusion
SOURCE_TO_NUM = {"makeup": 0, "non-makeup": 1}
def copy_parameters(from_parameters: torch.nn.Parameter, to_parameters: torch.nn.Parameter):
to_parameters = list(to_parameters)
assert len(from_parameters) == len(to_parameters)
for s_param, param in zip(from_parameters, to_parameters):
param.data.copy_(s_param.to(param.device).data)
def create_diffusion_model(cfg_path: str, diffusion_model_pth: str, device: str):
cfg = create_cfg()
merge_possible_with_base(cfg, cfg_path)
cfg.EVAL.SCHEDULER = "ddpm"
cfg.EVAL.SAMPLE_STEPS = 1000
cfg.EVAL.ETA = 0.01
cfg.EVAL.REFINE_ITERATIONS = 0
cfg.EVAL.REFINE_STEPS = 10
model = build_model(cfg)
weight = torch.load(diffusion_model_pth, map_location="cpu")
copy_parameters(weight["ema_state_dict"]["shadow_params"], model.parameters())
del weight
model = model.to(device)
model.eval()
cycle_diffuser = TranslationDiffusion(cfg, device)
return cycle_diffuser, model
def create_translation_model(sd_model_path: str, device: str):
stable_cycle_diffuser = TextTranslationDiffusion(
img_size=512,
scheduler="ddpm",
device=device,
model_path=sd_model_path,
)
return stable_cycle_diffuser
def domain_translation(
config_file: str,
diffusion_model_path: str,
device: str,
source_label: str,
target_label: str,
image_input: Image.Image,
mask_input: Image.Image,
):
cycle_diffuser, diffusion_model = create_diffusion_model(
config_file, diffusion_model_path, device
)
transform_result = cycle_diffuser.domain_translation(
source_model=diffusion_model,
target_model=diffusion_model,
source_image=image_input,
source_class_label=SOURCE_TO_NUM[source_label],
target_class_label=SOURCE_TO_NUM[target_label],
parsing_mask=mask_input,
use_inversion=False,
)
del cycle_diffuser, diffusion_model
torch.cuda.empty_cache()
return Image.fromarray(
(transform_result[0].cpu().permute(1, 2, 0).numpy() * 255).astype("uint8")
)
def reference_translation(
config_file: str,
diffusion_model_path: str,
device: str,
source_label: str,
target_label: str,
source_image_input: Image.Image,
target_image_input: Image.Image,
source_mask_input: Image.Image,
target_mask_input: Image.Image,
):
cycle_diffuser, diffusion_model = create_diffusion_model(
config_file, diffusion_model_path, device
)
transform_result = cycle_diffuser.image_translation(
source_model=diffusion_model,
target_model=diffusion_model,
source_image=source_image_input,
target_image=target_image_input,
source_class_label=SOURCE_TO_NUM[source_label],
target_class_label=SOURCE_TO_NUM[target_label],
source_parsing_mask=source_mask_input,
target_parsing_mask=target_mask_input,
use_his_matching=False,
)
del cycle_diffuser, diffusion_model
torch.cuda.empty_cache()
return Image.fromarray(
(transform_result[0].cpu().permute(1, 2, 0).numpy() * 255).astype("uint8")
)
def text_manipulate(
stable_diffusion_path: str,
device: str,
source_image_with_brush_mask: Image.Image,
source_mask: Image.Image,
prompt: str,
guidance_scale: float,
):
text_translation = create_translation_model(stable_diffusion_path, device)
source_image = source_image_with_brush_mask["background"].convert("RGB")
source_brush_mask = source_image_with_brush_mask["layers"][0]
# Trick for transform all the other component to non-change and leave only the brush
source_brush_mask = np.array(source_brush_mask)[..., 3]
if np.sum(source_brush_mask) != 0:
source_brush_mask[source_brush_mask != 0] = 4 # Pretend it to be 1 and will not be filtered
contours, _ = cv2.findContours(
((source_brush_mask > 0) * 255).astype("uint8"), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE
)
mask = np.zeros((*source_brush_mask.shape, 3), dtype="uint8")
mask[source_brush_mask != 0] = np.array([255, 205, 235])
for c in contours:
cv2.drawContours(mask, [c], -1, (0, 255, 0), 2)
result = cv2.addWeighted(
mask, 0.5, cv2.cvtColor(np.array(source_image), cv2.COLOR_RGB2BGR), 0.5, 0
)
cv2.imwrite("temp.png", result)
source_brush_mask[source_brush_mask == 0] = 255
if source_mask is not None:
source_mask = np.array(source_mask.resize(source_brush_mask.shape[:2][::-1]))
source_brush_mask[(source_mask == 1) | (source_mask == 6)] = 255
transform_result = text_translation.modify_with_text(
image=source_image,
prompt=[prompt],
mask=source_brush_mask,
guidance_scale=guidance_scale,
start_from_step=180,
)
del text_translation
torch.cuda.empty_cache()
return source_mask, transform_result[0]
def domain_to_text(domain_image_output, domain_mask_input):
return domain_image_output, domain_mask_input
def reference_to_text(reference_image_output, reference_mask_input_source):
return reference_image_output, reference_mask_input_source
with gr.Blocks(title="Makeup Transfer") as demo:
with gr.Row():
with gr.Column():
config_file = gr.Dropdown(
["configs/model_256_256.yaml"],
value="configs/model_256_256.yaml",
label="Select config file",
)
diffusion_model_path = gr.Dropdown(
["makeup_checkpoint.pth"],
value="makeup_checkpoint.pth",
label="Select diffusion model path",
)
with gr.Column():
stable_diffusion_path = gr.Dropdown(
["text_checkpoint.pth"],
value="text_checkpoint.pth",
label="Select stable diffusion model path",
)
device = gr.Dropdown(["cpu", "cuda"], value="cuda", label="Select Device")
with gr.Tab("Domain Translation"):
with gr.Row():
domain_image_input = gr.Image(type="pil", label="Input Image")
domain_mask_input = gr.Image(type="pil", label="Input Mask", image_mode="L")
domain_image_output = gr.Image(type="pil", label="Output Image", interactive=False)
with gr.Row():
domain_source_label = gr.Dropdown(["makeup", "non-makeup"], label="Source Label")
domain_target_label = gr.Dropdown(["makeup", "non-makeup"], label="Target Label")
domain_to_text_button = gr.Button(value="Send to text manipulation")
domain_submit = gr.Button(value="Transform")
with gr.Row():
gr.Examples(
examples=["data/mtdataset/images/non-makeup/xfsy_0307.png"],
inputs=domain_image_input,
label="Image Example",
)
gr.Examples(
examples=["data/mtdataset/parsing/non-makeup/xfsy_0307.png"],
inputs=domain_mask_input,
label="Mask Example (pair with image)",
)
with gr.Tab("Reference Translation"):
with gr.Row():
reference_image_input_source = gr.Image(type="pil", label="Source Image")
reference_mask_input_source = gr.Image(type="pil", label="Source Mask", image_mode="L")
with gr.Row():
reference_image_input_target = gr.Image(type="pil", label="Reference Image")
reference_mask_input_target = gr.Image(
type="pil", label="Reference Mask", image_mode="L"
)
reference_image_output = gr.Image(
type="pil", label="Output Image", interactive=False, width="50%"
)
with gr.Row():
reference_source_label = gr.Dropdown(["makeup", "non-makeup"], label="Source Label")
reference_target_label = gr.Dropdown(["makeup", "non-makeup"], label="Target Label")
reference_to_text_button = gr.Button(value="Send to text manipulation")
reference_submit = gr.Button(value="Transform")
with gr.Row():
gr.Examples(
examples=["data/mtdataset/images/non-makeup/vSYYZ223.png"],
inputs=reference_image_input_source,
label="Source Image Example",
)
gr.Examples(
examples=["data/mtdataset/parsing/non-makeup/vSYYZ223.png"],
inputs=reference_mask_input_source,
label="Reference Mask Example (pair with image)",
)
with gr.Row():
gr.Examples(
examples=["data/mtdataset/images/makeup/vFG66.png"],
inputs=reference_image_input_target,
label="Reference Image Example",
)
gr.Examples(
examples=["data/mtdataset/parsing/makeup/vFG66.png"],
inputs=reference_mask_input_target,
label="Reference Mask Example (pair with image)",
)
with gr.Tab("Text Manipulation"):
with gr.Row():
text_image_input = gr.ImageMask(type="pil", label="Input Image")
text_mask_input = gr.Image(type="pil", label="Input Mask", image_mode="L")
with gr.Row():
text_brush_mask = gr.Image(
type="pil", label="Brush Mask", image_mode="L", interactive=False
)
text_image_output = gr.Image(type="pil", label="Output Image", interactive=False)
with gr.Row():
text_input = gr.Textbox(lines=1, label="Input Text")
text_guidance_scale = gr.Slider(minimum=0, maximum=30, value=15, label="Guidance Scale")
text_submit = gr.Button(value="Transform")
with gr.Row():
gr.Examples(
examples=[
"data/mtdataset/images/non-makeup/xfsy_0327.png",
"data/mtdataset/images/makeup/vFG805.png",
],
inputs=text_image_input,
label="Image Example",
)
gr.Examples(
examples=[
"data/mtdataset/parsing/non-makeup/xfsy_0327.png",
"data/mtdataset/parsing/makeup/vFG805.png",
],
inputs=text_mask_input,
label="Mask Example (pair with image)",
)
gr.Examples(
examples=["makeup with smoky eyeshadow"],
inputs=text_input,
label="Text Example",
)
domain_submit.click(
domain_translation,
[
config_file,
diffusion_model_path,
device,
domain_source_label,
domain_target_label,
domain_image_input,
domain_mask_input,
],
[domain_image_output],
)
domain_to_text_button.click(
domain_to_text,
[domain_image_output, domain_mask_input],
[text_image_input, text_mask_input],
)
reference_submit.click(
reference_translation,
[
config_file,
diffusion_model_path,
device,
reference_source_label,
reference_target_label,
reference_image_input_source,
reference_image_input_target,
reference_mask_input_source,
reference_mask_input_target,
],
[reference_image_output],
)
reference_to_text_button.click(
reference_to_text,
[reference_image_output, reference_mask_input_source],
[text_image_input, text_mask_input],
)
text_submit.click(
text_manipulate,
[
stable_diffusion_path,
device,
text_image_input,
text_mask_input,
text_input,
text_guidance_scale,
],
[text_brush_mask, text_image_output],
)
# Launch the demo
demo.queue(max_size=1).launch(share=False, debug=True)