File size: 7,642 Bytes
1d4dc56 |
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 |
"""
Copyright 2023 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import os
import torch
from diffusers import StableDiffusionPipeline
import numpy as np
import matplotlib.pyplot as plt
from transformers import CLIPImageProcessor, CLIPModel
import glob
def load_tokens(pipe, data, device):
"""
Adds the new learned tokens into the predefined dictionary of pipe.
"""
added_tokens = []
for t_ in data.keys():
added_tokens.append(t_)
num_added_tokens = pipe.tokenizer.add_tokens(added_tokens)
pipe.text_encoder.resize_token_embeddings(len(pipe.tokenizer))
for token_ in data.keys():
ref_token = pipe.tokenizer.tokenize(token_)
ref_indx = pipe.tokenizer.convert_tokens_to_ids(ref_token)[0]
embd_cur = data[token_].to(device).to(dtype=torch.float16)
pipe.text_encoder.text_model.embeddings.token_embedding.weight[ref_indx] = embd_cur
def save_rev_samples(output_path, path_to_embed, model_id, device):
if not os.path.exists(f"{output_path}/final_samples"):
os.mkdir(f"{output_path}/final_samples")
prompts_title = ["Vl", "Vr", "Vl Vr"]
prompt_to_vec = {}
assert os.path.exists(path_to_embed)
data = torch.load(path_to_embed)
combined = []
prompts = []
for w_ in data.keys():
prompt_to_vec[w_] = data[w_]
combined.append(w_)
prompts.append(w_)
prompts.append(" ".join(combined))
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16, safety_checker=None, requires_safety_checker=False).to(device)
load_tokens(pipe, prompt_to_vec, device)
print("Prompts loaded to pipe ...")
print(prompt_to_vec.keys())
gen_seeds = [4321, 95, 11, 87654]
num_images_per_seed = 10
plt.figure(figsize=(20,20))
for i in range(len(prompts)):
images_per_seed = []
for gen_seed in gen_seeds:
with torch.no_grad():
torch.manual_seed(gen_seed)
images = pipe(prompt=[prompts[i]] * num_images_per_seed, num_inference_steps=25, guidance_scale=7.5).images
images_per_seed.extend(images)
# plot results
plot_stacked = []
for j in range(int(len(images_per_seed) / 4)):
images_staked_h = np.hstack([np.asarray(img) for img in images_per_seed[j * 4:j * 4 + 4]])
plot_stacked.append(images_staked_h)
im_stack = np.vstack(plot_stacked)
plt.subplot(1,len(prompts), i + 1)
plt.imshow(im_stack)
plt.axis("off")
plt.title(prompts_title[i], size=24)
plt.savefig(f"{output_path}/final_samples.jpg")
def generate_training_data(code_path, node, output_path, device, model_id, model_id_clip):
node_code = torch.load(code_path)[node]
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16, safety_checker=None, requires_safety_checker=False).to(device)
load_tokens(pipe, {"<*>": node_code}, device)
print("Prompts loaded to pipe ...")
gen_seeds = [4321, 95, 11, 87654]
num_images_per_seed = 10
clip_model = CLIPModel.from_pretrained(model_id_clip)
preprocess = CLIPImageProcessor.from_pretrained(model_id_clip)
plt.figure(figsize=(20,20))
images_per_seed = []
for gen_seed in gen_seeds:
with torch.no_grad():
torch.manual_seed(gen_seed)
images = pipe(prompt=["<*>"] * num_images_per_seed, num_inference_steps=25, guidance_scale=7.5).images
images_per_seed.extend(images)
# saves the clip embeddings for all images
with torch.no_grad():
images_preprocess = [preprocess(image, return_tensors="pt")["pixel_values"] for image in images_per_seed]
stacked_images = torch.cat(images_preprocess)
embedding_a = clip_model.get_image_features(stacked_images)
emb_norm = torch.norm(embedding_a, dim=1)
clip_embed_all = embedding_a / emb_norm.unsqueeze(1)
sim_mat = (clip_embed_all @ clip_embed_all.T)
mean_over_rows = sim_mat.mean(dim=0)
sorted_inds = np.array(mean_over_rows).argsort()[-10:][::-1]
best_images = []
for j in sorted_inds:
best_images.append(images_per_seed[j])
images_per_seed[j].save(f"{output_path}/{j}.jpg")
# plot results
plot_stacked = []
for j in range(int(len(images_per_seed) / 4)):
images_staked_h = np.hstack([np.asarray(img) for img in images_per_seed[j * 4:j * 4 + 4]])
plot_stacked.append(images_staked_h)
im_stack = np.vstack(plot_stacked)
plt.subplot(1, 2, 1)
plt.imshow(im_stack)
plt.axis("off")
plt.title(node, size=24)
plot_stacked = []
for j in range(int(len(best_images) / 5)):
images_staked_h = np.hstack([np.asarray(img) for img in best_images[j * 5:j * 5 + 5]])
plot_stacked.append(images_staked_h)
im_stack = np.vstack(plot_stacked)
plt.subplot(1, 2, 2)
plt.imshow(im_stack)
plt.axis("off")
plt.title("Chosen", size=24)
if not os.path.exists(f"{output_path}/generated_images_summary"):
os.mkdir(f"{output_path}/generated_images_summary")
plt.savefig(f"{output_path}/generated_images_summary/final_samples.jpg")
del clip_model
del pipe
torch.cuda.empty_cache()
def save_children_nodes(parent_node, children_node_path, concept_output_path, device, MODEL_ID, MODEL_ID_CLIP):
node_number = int(parent_node[1:])
left_child_number = node_number * 2 + 1
right_child_number = left_child_number + 1
data = torch.load(children_node_path)
left_child_code = {f"v{left_child_number}" :data["<*>"]}
right_child_code = {f"v{right_child_number}": data["<&>"]}
if not os.path.exists(f"{concept_output_path}/v{left_child_number}"):
os.mkdir(f"{concept_output_path}/v{left_child_number}")
if not os.path.exists(f"{concept_output_path}/v{right_child_number}"):
os.mkdir(f"{concept_output_path}/v{right_child_number}")
torch.save(left_child_code, f"{concept_output_path}/v{left_child_number}/embeds.bin")
torch.save(right_child_code, f"{concept_output_path}/v{right_child_number}/embeds.bin")
print(f"Results saved to:\n[{concept_output_path}/v{left_child_number}/embeds.bin]\n[{concept_output_path}/v{right_child_number}/embeds.bin]")
files_l = glob.glob(f"{concept_output_path}/v{left_child_number}/*.png") + glob.glob(f"{concept_output_path}/v{left_child_number}/*.jpg") + glob.glob(f"{concept_output_path}/v{left_child_number}/*.jpeg")
files_r = glob.glob(f"{concept_output_path}/v{right_child_number}/*.png") + glob.glob(f"{concept_output_path}/v{right_child_number}/*.jpg") + glob.glob(f"{concept_output_path}/v{right_child_number}/*.jpeg")
if not len(files_l):
generate_training_data(f"{concept_output_path}/v{left_child_number}/embeds.bin", f"v{left_child_number}", f"{concept_output_path}/v{left_child_number}", device, MODEL_ID, MODEL_ID_CLIP)
if not len(files_r):
generate_training_data(f"{concept_output_path}/v{right_child_number}/embeds.bin", f"v{right_child_number}", f"{concept_output_path}/v{right_child_number}", device, MODEL_ID, MODEL_ID_CLIP)
|