-
Notifications
You must be signed in to change notification settings - Fork 8
/
run_editing_blended_latent_diffusion.py
231 lines (187 loc) · 8.32 KB
/
run_editing_blended_latent_diffusion.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
import numpy as np
from PIL import Image
import json
import os
import random
import argparse
from diffusers import DDIMScheduler, StableDiffusionPipeline
import torch
from utils.utils import txt_draw
def setup_seed(seed=1234):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def mask_decode(encoded_mask,image_shape=[512,512]):
length=image_shape[0]*image_shape[1]
mask_array=np.zeros((length,))
for i in range(0,len(encoded_mask),2):
splice_len=min(encoded_mask[i+1],length-encoded_mask[i])
for j in range(splice_len):
mask_array[encoded_mask[i]+j]=1
mask_array=mask_array.reshape(image_shape[0], image_shape[1])
# to avoid annotation errors in boundary
mask_array[0,:]=1
mask_array[-1,:]=1
mask_array[:,0]=1
mask_array[:,-1]=1
return mask_array
class BlendedLatnetDiffusion:
def __init__(self,model_path="stabilityai/stable-diffusion-2-1-base",device="cuda"):
self.model_path = model_path
self.device = device
self.load_models()
def load_models(self):
pipe = StableDiffusionPipeline.from_pretrained(
self.model_path, torch_dtype=torch.float16
)
self.vae = pipe.vae.to(self.device)
self.tokenizer = pipe.tokenizer
self.text_encoder = pipe.text_encoder.to(self.device)
self.unet = pipe.unet.to(self.device)
self.scheduler = DDIMScheduler(
beta_start=0.00085,
beta_end=0.012,
beta_schedule="scaled_linear",
clip_sample=False,
set_alpha_to_one=False,
)
@torch.no_grad()
def edit_image(
self,
image_path,
mask,
prompts,
height=512,
width=512,
num_inference_steps=50,
guidance_scale=7.5,
generator=torch.manual_seed(42),
blending_percentage=0.25,
):
image_ori = Image.open(image_path)
image_ori = image_ori.resize((height, width), Image.BILINEAR)
image_ori = np.array(image_ori)[:, :, :3]
source_latents = self._image2latent(image_ori)
latent_mask, org_mask = self._read_mask(mask)
text_input = self.tokenizer(
prompts,
padding="max_length",
max_length=self.tokenizer.model_max_length,
truncation=True,
return_tensors="pt",
)
text_embeddings = self.text_encoder(text_input.input_ids.to("cuda"))[0]
max_length = text_input.input_ids.shape[-1]
uncond_input = self.tokenizer(
[""],
padding="max_length",
max_length=max_length,
return_tensors="pt",
)
uncond_embeddings = self.text_encoder(uncond_input.input_ids.to("cuda"))[0]
text_embeddings = torch.cat([uncond_embeddings, text_embeddings])
latents = torch.randn(
(1, self.unet.in_channels, height // 8, width // 8),
generator=generator,
)
latents = latents.to("cuda").half()
self.scheduler.set_timesteps(num_inference_steps)
for t in self.scheduler.timesteps[
int(len(self.scheduler.timesteps) * blending_percentage) :
]:
# expand the latents if we are doing classifier-free guidance to avoid doing two forward passes.
latent_model_input = torch.cat([latents] * 2)
latent_model_input = self.scheduler.scale_model_input(
latent_model_input, timestep=t
)
# predict the noise residual
with torch.no_grad():
noise_pred = self.unet(
latent_model_input, t, encoder_hidden_states=text_embeddings
).sample
# perform guidance
noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
noise_pred = noise_pred_uncond + guidance_scale * (
noise_pred_text - noise_pred_uncond
)
# compute the previous noisy sample x_t -> x_t-1
latents = self.scheduler.step(noise_pred, t, latents).prev_sample
# Blending
noise_source_latents = self.scheduler.add_noise(
source_latents, torch.randn_like(latents), t
)
latents = latents * latent_mask + noise_source_latents * (1 - latent_mask)
latents = 1 / 0.18215 * latents
with torch.no_grad():
image = self.vae.decode(latents).sample
image = (image / 2 + 0.5).clamp(0, 1)
image = image.detach().cpu().permute(0, 2, 3, 1).numpy()
images = (image * 255).round().astype("uint8")
image_instruct = txt_draw(f"edit prompt: {prompts}")
return [image_instruct,image_ori,np.zeros_like(image_instruct),images[0]]
@torch.no_grad()
def _image2latent(self, image):
image = torch.from_numpy(image).float() / 127.5 - 1
image = image.permute(2, 0, 1).unsqueeze(0).to("cuda")
image = image.half()
latents = self.vae.encode(image)["latent_dist"].mean
latents = latents * 0.18215
return latents
def _read_mask(self, mask, dest_size=(64, 64)):
org_mask = mask
mask = org_mask.resize(dest_size, Image.NEAREST)
mask = np.array(mask)
mask[mask < 0.5] = 0
mask[mask >= 0.5] = 1
mask = mask[np.newaxis, np.newaxis, ...]
mask = torch.from_numpy(mask).half().to(self.device)
return mask, org_mask
image_save_paths={
"blended-latent-diffusion":"blended-latent-diffusion"
}
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--rerun_exist_images', action= "store_true") # rerun existing images
parser.add_argument('--data_path', type=str, default="data") # the editing category that needed to run
parser.add_argument('--output_path', type=str, default="output") # the editing category that needed to run
parser.add_argument('--edit_category_list', nargs = '+', type=str, default=["0","1","2","3","4","5","6","7","8","9"]) # the editing category that needed to run
parser.add_argument('--edit_method_list', nargs = '+', type=str, default=["blended-latent-diffusion"]) # the editing methods that needed to run
args = parser.parse_args()
rerun_exist_images=args.rerun_exist_images
data_path=args.data_path
output_path=args.output_path
edit_category_list=args.edit_category_list
edit_method_list=args.edit_method_list
bld = BlendedLatnetDiffusion()
with open(f"{data_path}/mapping_file.json", "r") as f:
editing_instruction = json.load(f)
for key, item in editing_instruction.items():
if item["editing_type_id"] not in edit_category_list:
continue
original_prompt = item["original_prompt"].replace("[", "").replace("]", "")
editing_prompt = item["editing_prompt"].replace("[", "").replace("]", "")
image_path = os.path.join(f"{data_path}/annotation_images", item["image_path"])
editing_instruction = item["editing_instruction"]
blended_word = item["blended_word"].split(" ") if item["blended_word"] != "" else []
mask = Image.fromarray(np.uint8(mask_decode(item["mask"])[:,:,np.newaxis].repeat(3,2))).convert("L")
for edit_method in edit_method_list:
present_image_save_path=image_path.replace(data_path, os.path.join(output_path,image_save_paths[edit_method]))
if ((not os.path.exists(present_image_save_path)) or rerun_exist_images):
print(f"editing image [{image_path}] with [{edit_method}]")
setup_seed()
torch.cuda.empty_cache()
edited_image = Image.fromarray(np.concatenate(bld.edit_image(
image_path,
mask,
prompts=[editing_prompt] * 1,
blending_percentage=0.25,
),1))
if not os.path.exists(os.path.dirname(present_image_save_path)):
os.makedirs(os.path.dirname(present_image_save_path))
edited_image.save(present_image_save_path)
print(f"finish")
else:
print(f"skip image [{image_path}] with [{edit_method}]")