Spaces:
Running
on
Zero
Running
on
Zero
File size: 22,212 Bytes
c20a702 b857866 f41b58f 0292eb7 c20a702 43c9440 c0e7580 47154ea e96f400 47154ea c6f871c c0e7580 c20a702 42824a3 8bb102d e2845d3 6de637b e2845d3 6de637b 2ff63f0 b6cb96e ce21f0a ceefe41 f8e9a8a 8bb102d bc8dd39 8bb102d bc8dd39 8bb102d 42824a3 c6f871c a8ca9e4 f41b58f c20a702 f41b58f c20a702 62151c8 c20a702 bc8dd39 43c9440 c20a702 62151c8 c20a702 43c9440 c20a702 62151c8 c20a702 f2e70cb 648c435 43c9440 c20a702 f2e70cb 648c435 43c9440 c20a702 f2e70cb 648c435 43c9440 c20a702 62151c8 c20a702 62151c8 43c9440 62151c8 43c9440 62151c8 c20a702 62151c8 c20a702 039a94c bc8dd39 c20a702 62151c8 c20a702 43c9440 747633e 43c9440 ee8f1f1 747633e 43c9440 c20a702 62151c8 bc8dd39 07c2dd5 c20a702 e6f9650 c20a702 e6f9650 0292eb7 e6f9650 c20a702 62151c8 c20a702 152624c c20a702 42824a3 c90aa74 42824a3 f6b6dd7 0003fa8 f6b6dd7 42824a3 cd99776 355ac45 cd99776 355ac45 cd99776 355ac45 cd99776 355ac45 cd99776 355ac45 cd99776 355ac45 cd99776 355ac45 cd99776 42824a3 c90aa74 c0e7580 e6f9650 c90aa74 e6f9650 c90aa74 e6f9650 42824a3 43c9440 c20a702 57e1caf e6f9650 0fdc043 57e1caf f6b6dd7 e6f9650 f045879 e6f9650 1a94351 e6f9650 0fdc043 57e1caf f6b6dd7 e6f9650 f045879 e6f9650 1a94351 43c9440 e6f9650 0fdc043 57e1caf f6b6dd7 e6f9650 f045879 e6f9650 1a94351 c091a06 42824a3 e6f9650 42824a3 b3e7963 c6f871c ceefe41 e6f9650 ceefe41 e6f9650 c20a702 57e1caf c091a06 c20a702 8bb102d bc8dd39 8bb102d 43c9440 e96f400 c20a702 42824a3 c20a702 62151c8 c20a702 43c9440 c20a702 c6f871c a8ca9e4 c90aa74 a8ca9e4 |
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 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 |
import os
import json
import torch
import gc
import numpy as np
import gradio as gr
from PIL import Image
from diffusers import StableDiffusionXLPipeline
import open_clip
from huggingface_hub import hf_hub_download
from IP_Composer.IP_Adapter.ip_adapter import IPAdapterXL
from IP_Composer.perform_swap import compute_dataset_embeds_svd, get_modified_images_embeds_composition
from IP_Composer.generate_text_embeddings import load_descriptions, generate_embeddings
import spaces
import random
device = "cuda" if torch.cuda.is_available() else "cpu"
# Initialize SDXL pipeline
base_model_path = "stabilityai/stable-diffusion-xl-base-1.0"
pipe = StableDiffusionXLPipeline.from_pretrained(
base_model_path,
torch_dtype=torch.float16,
add_watermarker=False,
)
# Initialize IP-Adapter
image_encoder_repo = 'h94/IP-Adapter'
image_encoder_subfolder = 'models/image_encoder'
ip_ckpt = hf_hub_download('h94/IP-Adapter', subfolder="sdxl_models", filename='ip-adapter_sdxl_vit-h.bin')
ip_model = IPAdapterXL(pipe, image_encoder_repo, image_encoder_subfolder, ip_ckpt, device)
# Initialize CLIP model
clip_model, _, preprocess = open_clip.create_model_and_transforms('hf-hub:laion/CLIP-ViT-H-14-laion2B-s32B-b79K')
clip_model.to(device)
tokenizer = open_clip.get_tokenizer('hf-hub:laion/CLIP-ViT-H-14-laion2B-s32B-b79K')
CONCEPTS_MAP={
"age": "age_descriptions.npy",
"animal fur": "fur_descriptions.npy",
"dogs": "dog_descriptions.npy",
"emotions": "emotion_descriptions.npy",
"flowers": "flower_descriptions.npy",
"fruit/vegtable": "fruit_vegetable_descriptions.npy",
"outfit type": "outfit_descriptions.npy",
"outfit pattern (including color)": "outfit_pattern_descriptions.npy",
"patterns": "pattern_descriptions.npy",
"patterns (including color)": "pattern_descriptions_with_colors.npy",
"vehicle": "vehicle_descriptions.npy",
"daytime": "times_of_day_descriptions.npy",
"pose": "person_poses_descriptions.npy",
"season": "season_descriptions.npy",
"material": "material_descriptions_with_gems.npy"
}
RANKS_MAP={
"age": 30,
"animal fur": 80,
"dogs": 30,
"emotions": 30,
"flowers": 30,
"fruit/vegtable": 30,
"outfit type": 30,
"outfit pattern (including color)": 80,
"patterns": 80,
"patterns (including color)": 80,
"vehicle": 30,
"daytime": 30,
"pose": 30,
"season": 30,
"material": 80,
}
concept_options = list(CONCEPTS_MAP.keys())
examples = [
['./IP_Composer/assets/patterns/base.jpg', './IP_Composer/assets/patterns/pattern.png', 'patterns (including color)', None, None, None, None, 80, 30, 30, None,1.0,0, 30],
['./IP_Composer/assets/flowers/base.png', './IP_Composer/assets/flowers/concept.png', 'flowers', None, None, None, None, 30, 30, 30, None,1.0,0, 30],
['./IP_Composer/assets/materials/base.png', './IP_Composer/assets/materials/concept.jpg', 'material', None, None, None, None, 80, 30, 30, None,1.0,0, 30],
['./IP_Composer/assets/vehicle/base.png', './IP_Composer/assets/vehicle/concept.png', 'vehicle', None, None, None, None, 30, 30, 30, None,1.0,0, 30],
['./IP_Composer/assets/dog_daytime/base.png', './IP_Composer/assets/dog_daytime/daytime.png', 'daytime', './IP_Composer/assets/dog_daytime/dog.png', 'dogs', None, None, 30, 140, 30, None,1.0,0, 30],
['./IP_Composer/assets/pose_material/base.png', './IP_Composer/assets/pose_material/material.jpg', 'material', './IP_Composer/assets/pose_material/pose.png', 'pose', None, None, 30, 80, 30, None,1.0,0, 30],
['./IP_Composer/assets/objects/mug.png', './IP_Composer/assets/patterns/splash.png', 'patterns (including color)', None, None, None, None, 80, 30, 30, None,1.0,0, 30],
['./IP_Composer/assets/objects/mug.png', './IP_Composer/assets/patterns/red_pattern.png', 'patterns (including color)', None, None, None, None, 100, 30, 30, None,1.0,0, 30],
['./IP_Composer/assets/emotions/joyful.png', './IP_Composer/assets/emotions/sad.png', 'emotions', './IP_Composer/assets/age/kid.png', 'age', None, None, 30, 30, 30, None,1.0,0, 30],
['./IP_Composer/assets/flowers/rose_1.jpg', './IP_Composer/assets/flowers/flowers_3.jpg', 'flowers', None, None, None, None, 30, 30, 30, None,1.0,0, 30],
]
def generate_examples(base_image,
concept_image1, concept_name1,
concept_image2, concept_name2,
concept_image3, concept_name3,
rank1, rank2, rank3,
prompt, scale, seed, num_inference_steps):
return process_and_display(base_image,
concept_image1, concept_name1,
concept_image2, concept_name2,
concept_image3, concept_name3,
rank1, rank2, rank3,
prompt, scale, seed, num_inference_steps)
MAX_SEED = np.iinfo(np.int32).max
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
if randomize_seed:
seed = random.randint(0, MAX_SEED)
return seed
def change_rank_default(concept_name):
return RANKS_MAP.get(concept_name, 30)
@spaces.GPU
def match_image_to_concept(image):
"""
Match an uploaded image to the closest concept type using CLIP embeddings
"""
if image is None:
return None
# Get image embeddings
img_pil = Image.fromarray(image).convert("RGB")
img_embed = get_image_embeds(img_pil, clip_model, preprocess, device)
# Calculate similarity to each concept
similarities = {}
for concept_name, concept_file in CONCEPTS_MAP.items():
try:
# Load concept embeddings
embeds_path = f"./IP_Composer/text_embeddings/{concept_file}"
with open(embeds_path, "rb") as f:
concept_embeds = np.load(f)
# Calculate similarity to each text embedding
sim_scores = []
for embed in concept_embeds:
# Normalize both embeddings
img_embed_norm = img_embed / np.linalg.norm(img_embed)
text_embed_norm = embed / np.linalg.norm(embed)
# Calculate cosine similarity
similarity = np.dot(img_embed_norm.flatten(), text_embed_norm.flatten())
sim_scores.append(similarity)
# Use the average of top 5 similarities for better matching
sim_scores.sort(reverse=True)
top_similarities = sim_scores[:min(5, len(sim_scores))]
avg_similarity = sum(top_similarities) / len(top_similarities)
similarities[concept_name] = avg_similarity
except Exception as e:
print(f"Error processing concept {concept_name}: {e}")
# Return the concept with highest similarity
if similarities:
matched_concept = max(similarities.items(), key=lambda x: x[1])[0]
# Display a notification to the user
gr.Info(f"Image automatically matched to concept: {matched_concept}")
return matched_concept
return None
@spaces.GPU
def get_image_embeds(pil_image, model=clip_model, preproc=preprocess, dev=device):
"""Get CLIP image embeddings for a given PIL image"""
image = preproc(pil_image)[np.newaxis, :, :, :]
with torch.no_grad():
embeds = model.encode_image(image.to(dev))
return embeds.cpu().detach().numpy()
@spaces.GPU
def process_images(
base_image,
concept_image1, concept_name1,
concept_image2=None, concept_name2=None,
concept_image3=None, concept_name3=None,
rank1=10, rank2=10, rank3=10,
prompt=None,
scale=1.0,
seed=420,
num_inference_steps=50,
concpet_from_file_1 = None,
concpet_from_file_2 = None,
concpet_from_file_3 = None,
use_concpet_from_file_1 = False,
use_concpet_from_file_2 = False,
use_concpet_from_file_3 = False
):
"""Process the base image and concept images to generate modified images"""
# Process base image
base_image_pil = Image.fromarray(base_image).convert("RGB")
base_embed = get_image_embeds(base_image_pil, clip_model, preprocess, device)
# Process concept images
concept_images = []
concept_descriptions = []
skip_load_concept =[False,False, False]
# for demo purposes we allow for up to 3 different concepts and corresponding concept images
if concept_image1 is not None:
concept_images.append(concept_image1)
if use_concpet_from_file_1 and concpet_from_file_1 is not None: # if concept is new from user input
concept_descriptions.append(concpet_from_file_1)
skip_load_concept[0] = True
else:
concept_descriptions.append(CONCEPTS_MAP[concept_name1])
else:
return None, "Please upload at least one concept image"
# Add second concept (optional)
if concept_image2 is not None:
concept_images.append(concept_image2)
if use_concpet_from_file_2 and concpet_from_file_2 is not None: # if concept is new from user input
concept_descriptions.append(concpet_from_file_2)
skip_load_concept[1] = True
else:
concept_descriptions.append(CONCEPTS_MAP[concept_name2])
# Add third concept (optional)
if concept_image3 is not None:
concept_images.append(concept_image3)
if use_concpet_from_file_3 and concpet_from_file_3 is not None: # if concept is new from user input
concept_descriptions.append(concpet_from_file_3)
skip_load_concept[2] = True
else:
concept_descriptions.append(CONCEPTS_MAP[concept_name3])
# Get all ranks
ranks = [rank1]
if concept_image2 is not None:
ranks.append(rank2)
if concept_image3 is not None:
ranks.append(rank3)
concept_embeds = []
projection_matrices = []
# for the demo, we assume 1 concept image per concept
# for each concept image, we calculate it's image embeedings and load the concepts textual embeddings to copmpute the projection matrix over it
for i, concept in enumerate(concept_descriptions):
img_pil = Image.fromarray(concept_images[i]).convert("RGB")
concept_embeds.append(get_image_embeds(img_pil, clip_model, preprocess, device))
if skip_load_concept[i]: # if concept is new from user input
all_embeds_in = concept
else:
embeds_path = f"./IP_Composer/text_embeddings/{concept}"
with open(embeds_path, "rb") as f:
all_embeds_in = np.load(f)
projection_matrix = compute_dataset_embeds_svd(all_embeds_in, ranks[i])
projection_matrices.append(projection_matrix)
# Create projection data structure for the composition
projections_data = [
{
"embed": embed,
"projection_matrix": proj_matrix
}
for embed, proj_matrix in zip(concept_embeds, projection_matrices)
]
# Generate modified images -
modified_images = get_modified_images_embeds_composition(
base_embed,
projections_data,
ip_model,
prompt=prompt,
scale=scale,
num_samples=1,
seed=seed,
num_inference_steps=num_inference_steps
)
return modified_images[0]
@spaces.GPU
def get_text_embeddings(concept_file):
print("generating text embeddings")
descriptions = load_descriptions(concept_file)
embeddings = generate_embeddings(descriptions, clip_model, tokenizer, device, batch_size=100)
print("text embeddings shape",embeddings.shape)
return embeddings, True
def process_and_display(
base_image,
concept_image1, concept_name1="age",
concept_image2=None, concept_name2=None,
concept_image3=None, concept_name3=None,
rank1=30, rank2=30, rank3=30,
prompt=None, scale=1.0, seed=0, num_inference_steps=50,
concpet_from_file_1 = None,
concpet_from_file_2 = None,
concpet_from_file_3 = None,
use_concpet_from_file_1 = False,
use_concpet_from_file_2 = False,
use_concpet_from_file_3 = False
):
if base_image is None:
raise gr.Error("Please upload a base image")
if concept_image1 is None:
raise gr.Error("Choose at least one concept image")
if concept_image1 is None:
raise gr.Error("Choose at least one concept type")
modified_images = process_images(
base_image,
concept_image1, concept_name1,
concept_image2, concept_name2,
concept_image3, concept_name3,
rank1, rank2, rank3,
prompt, scale, seed, num_inference_steps,
concpet_from_file_1,
concpet_from_file_2,
concpet_from_file_3,
use_concpet_from_file_1,
use_concpet_from_file_2,
use_concpet_from_file_3
)
return modified_images
# UI CSS
css = """
#col-container {
margin: 0 auto;
max-width: 800px;
}
.gradio-container{
max-width: 1024px !important;
margin: 0 auto
}
"""
example = """
Emotion Description
a photo of a person feeling joyful
a photo of a person feeling sorrowful
a photo of a person feeling enraged
a photo of a person feeling astonished
a photo of a person feeling disgusted
a photo of a person feeling terrified
...
"""
with gr.Blocks(css=css) as demo:
gr.Markdown(f"""# IP Composer π
βποΈ
### compose new images with visual concepts extracted from refrence images using CLIP & IP Adapter
#### π οΈ How to Use:
1. Upload a base image
2. Upload 1β3 concept images
3. Select a **concept type** to extract from each concept image:
- Choose a **predefined concept type** from the dropdown (e.g. pattern, emotion, pose), **or**
- Upload a **file with text variations of your concept** (e.g. prompts from an LLM).
- π If you're uploading a **new concept**, don't forget to **adjust the "rank" value** under **Advanced Options** for better results.
Following the algorithm proposed in IP-Composer: Semantic Composition of Visual Concepts by Dorfman et al.
[[Project page](https://ip-composer.github.io/IP-Composer/)] [[arxiv](https://arxiv.org/pdf/2502.13951)]
""")
concpet_from_file_1 = gr.State()
concpet_from_file_2 = gr.State()
concpet_from_file_3 = gr.State()
use_concpet_from_file_1 = gr.State()
use_concpet_from_file_2 = gr.State()
use_concpet_from_file_3 = gr.State()
with gr.Row():
with gr.Column():
base_image = gr.Image(label="Base Image (Required)", type="numpy", height=400, width=400)
with gr.Tab("Concept 1"):
with gr.Group():
concept_image1 = gr.Image(label="Concept Image 1", type="numpy", height=400, width=400)
with gr.Column():
concept_name1 = gr.Dropdown(concept_options, label="Concept 1", value=None, info="Pick concept type")
with gr.Accordion("π‘ Or use a new concept π", open=False):
gr.Markdown("1. Upload a file with text variations of your concept (e.g. ask an LLM)")
gr.Markdown("2. Prefereably with > 100 variations.")
with gr.Accordion("File example for the concept 'emotions'", open=False):
gr.Markdown(example)
concept_file_1 = gr.File(label="Concept variations", file_types=["text"])
with gr.Tab("Concept 2 (Optional)"):
with gr.Group():
concept_image2 = gr.Image(label="Concept Image 2", type="numpy", height=400, width=400)
with gr.Column():
concept_name2 = gr.Dropdown(concept_options, label="Concept 2", value=None, info="Pick concept type")
with gr.Accordion("π‘ Or use a new concept π", open=False):
gr.Markdown("1. Upload a file with text variations of your concept (e.g. ask an LLM)")
gr.Markdown("2. Prefereably with > 100 variations.")
with gr.Accordion("File example for the concept 'emotions'", open=False):
gr.Markdown(example)
concept_file_2 = gr.File(label="Concept variations", file_types=["text"])
with gr.Tab("Concept 3 (optional)"):
with gr.Group():
concept_image3 = gr.Image(label="Concept Image 3", type="numpy", height=400, width=400)
with gr.Column():
concept_name3 = gr.Dropdown(concept_options, label="Concept 3", value= None, info="Pick concept type")
with gr.Accordion("π‘ Or use a new concept π", open=False):
gr.Markdown("1. Upload a file with text variations of your concept (e.g. ask an LLM)")
gr.Markdown("2. Prefereably with > 100 variations.")
with gr.Accordion("File example for the concept 'emotions'", open=False):
gr.Markdown(example)
concept_file_3 = gr.File(label="Concept variations", file_types=["text"])
with gr.Accordion("Advanced options", open=False):
prompt = gr.Textbox(label="Guidance Prompt (Optional)", placeholder="Optional text prompt to guide generation")
num_inference_steps = gr.Slider(minimum=1, maximum=50, value=30, step=1, label="Num steps")
with gr.Row():
scale = gr.Slider(minimum=0.1, maximum=2.0, value=1.0, step=0.1, label="Scale")
randomize_seed = gr.Checkbox(value=True, label="Randomize seed")
seed = gr.Number(value=0, label="Seed", precision=0)
with gr.Column():
gr.Markdown("If a concept is not showing enough, try to increase the rank")
with gr.Row():
rank1 = gr.Slider(minimum=1, maximum=150, value=30, step=1, label="Rank concept 1")
rank2 = gr.Slider(minimum=1, maximum=150, value=30, step=1, label="Rank concept 2")
rank3 = gr.Slider(minimum=1, maximum=150, value=30, step=1, label="Rank concept 3")
with gr.Column():
output_image = gr.Image(label="Composed output", show_label=True,height=400, width=400 )
submit_btn = gr.Button("Generate")
gr.Examples(
examples,
inputs=[base_image,
concept_image1, concept_name1,
concept_image2, concept_name2,
concept_image3, concept_name3,
rank1, rank2, rank3,
prompt, scale, seed, num_inference_steps],
outputs=[output_image],
fn=generate_examples,
cache_examples=False
)
concept_file_1.upload(
fn=get_text_embeddings,
inputs=[concept_file_1],
outputs=[concpet_from_file_1, use_concpet_from_file_1]
)
concept_file_2.upload(
fn=get_text_embeddings,
inputs=[concept_file_2],
outputs=[concpet_from_file_2, use_concpet_from_file_2]
)
concept_file_3.upload(
fn=get_text_embeddings,
inputs=[concept_file_3],
outputs=[concpet_from_file_3, use_concpet_from_file_3]
)
concept_file_1.delete(
fn=lambda x: False,
inputs=[concept_file_1],
outputs=[use_concpet_from_file_1]
)
concept_file_2.delete(
fn=lambda x: False,
inputs=[concept_file_2],
outputs=[use_concpet_from_file_2]
)
concept_file_3.delete(
fn=lambda x: False,
inputs=[concept_file_3],
outputs=[use_concpet_from_file_3]
)
submit_btn.click(
fn=randomize_seed_fn,
inputs=[seed, randomize_seed],
outputs=seed,
).then(fn=process_and_display,
inputs=[
base_image,
concept_image1, concept_name1,
concept_image2, concept_name2,
concept_image3, concept_name3,
rank1, rank2, rank3,
prompt, scale, seed, num_inference_steps,
concpet_from_file_1,
concpet_from_file_2,
concpet_from_file_3,
use_concpet_from_file_1,
use_concpet_from_file_2,
use_concpet_from_file_3
],
outputs=[output_image]
)
concept_name1.select(
fn= change_rank_default,
inputs=[concept_name1],
outputs=[rank1]
)
concept_name2.select(
fn= change_rank_default,
inputs=[concept_name2],
outputs=[rank2]
)
concept_name3.select(
fn= change_rank_default,
inputs=[concept_name3],
outputs=[rank3]
)
concept_image1.upload(
fn=match_image_to_concept,
inputs=[concept_image1],
outputs=[concept_name1]
)
concept_image2.upload(
fn=match_image_to_concept,
inputs=[concept_image2],
outputs=[concept_name2]
)
concept_image3.upload(
fn=match_image_to_concept,
inputs=[concept_image3],
outputs=[concept_name3]
)
if __name__ == "__main__":
demo.launch() |