File size: 13,868 Bytes
5ba0490 |
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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 |
import glob
import os
import random
import gradio as gr
import numpy as np
import torch
import torch.utils.checkpoint
from PIL import Image
from diffusers import (
AutoencoderKL,
UNet2DConditionModel,
UniPCMultistepScheduler,
)
from diffusers import StableDiffusionControlNetInpaintPipeline, ControlNetModel
from torchvision.transforms import transforms
from transformers import AutoTokenizer, PretrainedConfig
from face_parsing import inference as face_parsing_inference
# ----------------------------------------------------------------
# Define model paths and other parameters
# sd 1.5
# pretrained_model_name_or_path = "stable-diffusion-v1-5/stable-diffusion-v1-5"
# controlnet_path = "siijiawei/gorgeous-mafor-sd1-5"
# sd 2.1
pretrained_model_name_or_path = "stabilityai/stable-diffusion-2-1-base"
controlnet_path = "siijiawei/gorgeous-mafor-sd2-1"
image_sets = sorted(glob.glob("makeup_assets/*"))
textual_inversion_paths = sorted(glob.glob("makeup_assets/*"))
prompt_template = "A woman with {} makeup on face"
MAX_SEED = np.iinfo(np.int32).max
device = "cuda" if torch.cuda.is_available() else "cpu"
dtype = torch.float16 if torch.cuda.is_available() else torch.float32
# ----------------------------------------------------------------
def import_model_class_from_model_name_or_path(
pretrained_model_name_or_path: str, revision: str
):
text_encoder_config = PretrainedConfig.from_pretrained(
pretrained_model_name_or_path,
subfolder="text_encoder",
revision=revision,
)
model_class = text_encoder_config.architectures[0]
if model_class == "CLIPTextModel":
from transformers import CLIPTextModel
return CLIPTextModel
elif model_class == "RobertaSeriesModelWithTransformation":
from diffusers.pipelines.alt_diffusion.modeling_roberta_series import (
RobertaSeriesModelWithTransformation,
)
return RobertaSeriesModelWithTransformation
else:
raise ValueError(f"{model_class} is not supported.")
# ----------------------------------------------------------------
# Initialize components
tokenizer = AutoTokenizer.from_pretrained(
pretrained_model_name_or_path,
subfolder="tokenizer",
use_fast=False,
)
text_encoder_cls = import_model_class_from_model_name_or_path(
pretrained_model_name_or_path, "main"
)
text_encoder = text_encoder_cls.from_pretrained(
pretrained_model_name_or_path, subfolder="text_encoder"
)
vae = AutoencoderKL.from_pretrained(pretrained_model_name_or_path, subfolder="vae")
unet = UNet2DConditionModel.from_pretrained(
pretrained_model_name_or_path, subfolder="unet"
)
controlnet = ControlNetModel.from_pretrained(
controlnet_path,
use_safetensors=True,
torch_dtype=torch.float16,
# subfolder="controlnet",
).to(device)
vae.to(device, dtype=dtype)
unet.to(device, dtype=dtype)
text_encoder.to(device, dtype=dtype)
controlnet.to(device, dtype=dtype)
pipeline = StableDiffusionControlNetInpaintPipeline.from_pretrained(
pretrained_model_name_or_path,
vae=vae,
text_encoder=text_encoder,
tokenizer=tokenizer,
unet=unet,
controlnet=controlnet,
safety_checker=None,
torch_dtype=dtype,
use_safetensors=True,
)
pipeline.scheduler = UniPCMultistepScheduler.from_config(pipeline.scheduler.config)
pipeline.to(device)
textual_inversion_tokens = [f"<v{i}>" for i in range(len(textual_inversion_paths))]
pipeline.load_textual_inversion(textual_inversion_paths, token=textual_inversion_tokens)
generator = torch.Generator(device=device).manual_seed(42)
preprocess_transform = transforms.Compose(
[transforms.Resize(512), transforms.CenterCrop(512)]
)
# ----------------------------------------------------------------
# Helper functions
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
if randomize_seed:
seed = random.randint(0, MAX_SEED)
return seed
def make_inpaint_condition(image, image_mask):
image = np.array(image.convert("RGB")).astype(np.float32) / 255.0
image_mask = np.array(image_mask.convert("L")).astype(np.float32) / 255.0
assert image.shape[0:1] == image_mask.shape[0:1]
image[image_mask > 0.5] = -1.0 # set as masked pixel
image = np.expand_dims(image, 0).transpose(0, 3, 1, 2)
image = torch.from_numpy(image)
return image
# ----------------------------------------------------------------
def create_image(
idea_set_target,
input_image,
prompt,
n_prompt,
control_scale,
guidance_scale,
num_inference_steps,
seed,
):
if input_image is not None:
# Generate mask
input_image_path = "input_image.png"
input_image.save(input_image_path)
input_image = preprocess_transform(input_image)
mask_image = face_parsing_inference.get_face_mask(input_image).convert("L")
print("idea_set_target", idea_set_target)
set_index = int(idea_set_target.split(":")[0].replace("Set ", "")) - 1 # start from 1
# Prepare prompt
token = textual_inversion_tokens[set_index]
prompt = prompt.replace("{}", token)
print(prompt)
# Generate image
blurred_mask = pipeline.mask_processor.blur(mask_image, blur_factor=10)
masked_image = make_inpaint_condition(input_image, blurred_mask)
generator = torch.Generator(device=device).manual_seed(seed)
with torch.autocast("cuda"):
output = pipeline(
prompt=prompt,
image=input_image,
mask_image=blurred_mask,
control_image=input_image,
num_inference_steps=int(num_inference_steps),
generator=generator,
negative_prompt=n_prompt,
controlnet_conditioning_scale=float(control_scale),
guidance_scale=float(guidance_scale),
)
output_image = output.images[0]
return output_image
return None
# ----------------------------------------------------------------
def read_image_from_dirpath(dirpath):
img_paths = sorted(
glob.glob(dirpath + "/*.png")
+ glob.glob(dirpath + "/*.jpeg")
+ glob.glob(dirpath + "/*.jpg")
)
imgs = [Image.open(p) for p in img_paths[:5]]
if len(imgs) < 5:
imgs += [Image.new(mode="RGB", size=(200, 200)) for _ in range(5 - len(imgs))]
return imgs
image_sets = [
{
"label": f"Set {i + 1}: {os.path.basename(image_sets[i])}",
"images": read_image_from_dirpath(image_sets[i]),
}
for i in range(len(image_sets))
]
labels = [image_set["label"] for image_set in image_sets]
def display_images(set_label):
print("?")
set_index = int(set_label.split(":")[0].replace("Set ", "")) - 1 # start from 1
image_set = image_sets[set_index]
return [image_set["label"]] + image_set["images"]
# ----------------------------------------------------------------
# Gradio UI setup
block = gr.Blocks(
css="""
footer {visibility: hidden}
.title-background {
background-color: #f7e4da; /* Light brown background */
color: #1d1d1d; /* Dark text color */
padding: 20px; /* Padding for top and bottom */
text-align: center;
width: 100%; /* Set width to 100% */
margin: 0 auto; /* Center alignment */
max-width: 1200px; /* Max width to keep content centered */
box-sizing: border-box; /* Ensure padding is inside the box model */
}
.gr-button {
background-color: #c2410c !important; /* Brown color for buttons */
color: white !important; /* Text color */
}
.gr-dropdown, .gr-slider, .gr-textbox {
border-color: #c2410c !important; /* Brown color for borders */
}
.gr-label, .gr-markdown {
color: #c2410c !important; /* Brown color for text */
}
.content-description {
text-align: center;
max-width: 1200px; /* Ensure same max width as title */
margin: 0 auto; /* Center alignment */
box-sizing: border-box;
}
"""
).queue(max_size=10, api_open=False)
with block:
# Title with background
gr.Markdown(
"""
<div class="title-background">
<h1 style='font-weight: 10px; font-size: 40px;'>💄<b>Gorgeous</b>: Creating Narrative-Driven Makeup Ideas via Image Prompt 💡</h1>
</div>
"""
)
# Description with center alignment
gr.Markdown(
"""
<div class="content-description">Introducing \( \textbf{Gorgeous} \), a diffusion-based generative method that revolutionizes
the makeup industry by empowering user creativity via image prompts. Unlike
traditional makeup transfer methods that focus on replicating existing make-
ups, Gorgeous, for the first time, empowers users to integrate narrative elements
into makeup ideation using image prompts. The result is a makeup concept
that vividly reflects user’s expression via images, offering imaginative makeup
ideas for physical makeup applications. To achieve this, Gorgeous establishes a
foundational framework, ensuring the model learns “what makeup is” before inte-
grating narrative elements. A pseudo-pairing strategy, utilizing a face parsing and
content-style disentangling network, addresses unpaired data challenges, enabling
the model to do makeup training on bare faces. Users can input images repre-
senting their ideas (e.g., fire), from which Gorgeous extracts context embeddings
to guide our proposed makeup inpainting algorithm, conceptualizing creative,
narrative-driven makeup ideas for targeted facial regions. Comprehensive exper-
iments underscore the effectiveness of Gorgeous, paving a way for a
new dimension in digital makeup artistry and application!</div>
"""
)
with gr.Tabs():
with gr.Row():
with gr.Column():
with gr.Row():
image_pil = gr.Image(
label="Targeted face (e.g., your face)", type="pil", height=256
)
generated_image = gr.Image(
label="Generated Image", type="pil", height=256
)
with gr.Row():
set_dropdown = gr.Dropdown(
choices=[
labels[i]
for i in range(len(image_sets))
],
label="Select Image Set",
value=labels[0],
)
image_label = gr.Label()
image_boxes = [gr.Image() for _ in range(5)]
set_dropdown.change(
display_images,
set_dropdown,
outputs=[image_label] + image_boxes,
)
with gr.Row():
scale = gr.Slider(
minimum=0,
maximum=30,
step=0.01,
value=20.0,
label="Guidance scale (Adjust the slider to steer the influence of the idea chosen on the generation.)",
)
control_scale = gr.Slider(
minimum=0,
maximum=1,
step=0.01,
value=1,
label="Control scale (Adjust the slider to control face fidelity.)",
)
num_inference_steps = gr.Slider(
minimum=20,
maximum=100,
step=1,
value=50,
label="Number of inference steps",
)
# prompt_template = "A woman with {} makeup on face"
with gr.Row():
prompt = gr.Textbox(
label='Prompt (the set is represented by "{}")',
value="A photo of a woman with {} on face",
)
with gr.Row():
n_prompt = gr.Textbox(
label="Negative Prompt",
value="worst quality, normal quality, low quality, low res, blurry, text, watermark, logo, banner, extra digits, cropped, jpeg artifacts, signature, username, error, sketch ,duplicate, ugly, monochrome, horror, geometry, mutation, disgusting",
)
with gr.Row():
seed = gr.Slider(
minimum=0, maximum=MAX_SEED, value=1, step=1, label="Seed Value"
)
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
generate_button = gr.Button("Generate Image")
generate_button.click(
fn=randomize_seed_fn,
inputs=[seed, randomize_seed],
outputs=seed,
queue=False,
api_name=False,
).then(
fn=create_image,
inputs=[
set_dropdown,
image_pil,
prompt,
n_prompt,
control_scale,
scale,
num_inference_steps,
seed,
],
outputs=generated_image,
)
gr.Markdown("### Article")
block.launch(debug=True)
|