-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdiff_harmon.py
584 lines (456 loc) · 24 KB
/
diff_harmon.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
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
import numpy as np
import torch
from PIL import Image
from typing import Optional, List
from tqdm import tqdm
from torch import optim
from attentionControl import EmptyControl, AttentionStore
from utils import view_images, aggregate_attention
import torchvision
from thirdparty.edge_detector import get_edge, Initialize_PidNet, pidnet_args, PidNet
from torchvision import transforms
def init_latent(latent, model, height, width, generator, batch_size):
if latent is None:
latent = torch.randn(
(1, model.unet.in_channels, height // 8, width // 8),
generator=generator,
)
latents = latent.expand(batch_size, model.unet.in_channels, height // 8, width // 8).to(model.device)
return latent, latents
def diffusion_step(model, controller, latents, context, t, guidance_scale):
latents_input = torch.cat([latents] * 2)
noise_pred = model.unet(latents_input, t, encoder_hidden_states=context)["sample"]
noise_pred_uncond, noise_prediction_text = noise_pred.chunk(2)
noise_pred = noise_pred_uncond + guidance_scale * (noise_prediction_text - noise_pred_uncond)
latents = model.scheduler.step(noise_pred, t, latents)["prev_sample"]
latents = controller.step_callback(latents)
return latents
def latent2image(vae, latents):
latents = 1 / 0.18215 * latents
image = vae.decode(latents)['sample']
image = (image / 2 + 0.5).clamp(0, 1)
image = image.cpu().permute(0, 2, 3, 1).numpy()
image = (image * 255).astype(np.uint8)
return image
def latent2image_edge_fusion(vae, latents):
latents = 1 / 0.18215 * latents
image = vae.decode(latents)['sample']
image = (image / 2 + 0.5).clamp(0, 1)
# normalize the image: mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225]
image_edge = image.squeeze(dim=0)
image_edge = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])(image_edge)
image_edge = image_edge.unsqueeze(dim=0)
return image, image_edge
def preprocess(image, size):
image = image.resize((size, size), resample=Image.LANCZOS)
image = np.array(image).astype(np.float32) / 255.0
image = image[None].transpose(0, 3, 1, 2)
image = torch.from_numpy(image)[:, :3, :, :].cuda()
return 2.0 * image - 1.0
def preprocess_pidnet(image, size):
image = image.convert('RGB')
transform = transforms.Compose([transforms.Resize((size, size)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])])
image = transform(image)
image = image.unsqueeze(dim=0).cuda()
return image
def encoder(image, model, generator=None, size=512):
image = preprocess(image, size)
gpu_generator = torch.Generator(device=image.device)
gpu_generator.manual_seed(generator.initial_seed())
return 0.18215 * model.vae.encode(image).latent_dist.sample(generator=gpu_generator)
def text_embed_reforward(self, optim_embeddings, position_fg, position_bg):
def forward(
input_ids: Optional[torch.LongTensor] = None,
position_ids: Optional[torch.LongTensor] = None,
inputs_embeds: Optional[torch.FloatTensor] = None,
) -> torch.Tensor:
seq_length = input_ids.shape[-1] if input_ids is not None else inputs_embeds.shape[-2]
if position_ids is None:
position_ids = self.position_ids[:, :seq_length]
if inputs_embeds is None:
inputs_embeds = self.token_embedding(input_ids)
position_embeddings = self.position_embedding(position_ids)
embeddings = inputs_embeds + position_embeddings
'''First step to initialize the embed, then leverage it for replacing in the following steps'''
fg_emb, bg_emb = torch.chunk(embeddings, 2, dim=0)
if optim_embeddings == [None, None]:
optim_embeddings[0] = [fg_emb.detach()[:, x].clone() for x in position_fg]
optim_embeddings[1] = [bg_emb.detach()[:, x].clone() for x in position_bg]
else:
for ind, x in enumerate(position_fg):
embeddings[:1, x] = optim_embeddings[0][ind]
for ind, x in enumerate(position_bg):
embeddings[1:, x] = optim_embeddings[1][ind]
return embeddings
return forward
def reset_text_embed_reforward(self):
def forward(
input_ids: Optional[torch.LongTensor] = None,
position_ids: Optional[torch.LongTensor] = None,
inputs_embeds: Optional[torch.FloatTensor] = None,
) -> torch.Tensor:
seq_length = input_ids.shape[-1] if input_ids is not None else inputs_embeds.shape[-2]
if position_ids is None:
position_ids = self.position_ids[:, :seq_length]
if inputs_embeds is None:
inputs_embeds = self.token_embedding(input_ids)
position_embeddings = self.position_embedding(position_ids)
embeddings = inputs_embeds + position_embeddings
return embeddings
return forward
def register_attention_control(model, controller):
def ca_forward(self, place_in_unet):
def forward(x, context=None, mask=None):
batch_size, sequence_length, dim = x.shape
h = self.heads
q = self.to_q(x)
is_cross = context is not None
context = context if is_cross else x
k = self.to_k(context)
v = self.to_v(context)
q = self.reshape_heads_to_batch_dim(q)
k = self.reshape_heads_to_batch_dim(k)
v = self.reshape_heads_to_batch_dim(v)
sim = torch.einsum("b i d, b j d -> b i j", q, k) * self.scale
# attention, what we cannot get enough of
attn = sim.softmax(dim=-1)
# store attention, and for following processing
attn = controller(attn, is_cross, place_in_unet)
out = torch.einsum("b i j, b j d -> b i d", attn, v)
out = self.reshape_batch_dim_to_heads(out)
# linear proj
out = self.to_out[0](out)
# dropout
out = self.to_out[1](out)
return out
return forward
def register_recr(net_, count, place_in_unet):
if net_.__class__.__name__ == 'CrossAttention':
net_.forward = ca_forward(net_, place_in_unet)
return count + 1
elif hasattr(net_, 'children'):
for net__ in net_.children():
count = register_recr(net__, count, place_in_unet)
return count
cross_att_count = 0
sub_nets = model.unet.named_children()
for net in sub_nets:
if "down" in net[0]:
cross_att_count += register_recr(net[1], 0, "down")
elif "up" in net[0]:
cross_att_count += register_recr(net[1], 0, "up")
elif "mid" in net[0]:
cross_att_count += register_recr(net[1], 0, "mid")
controller.num_att_layers = cross_att_count
def reset_attention_control(model):
def ca_forward(self):
def forward(x, context=None, mask=None):
q = self.to_q(x)
is_cross = context is not None
context = context if is_cross else x
k = self.to_k(context)
v = self.to_v(context)
q = self.reshape_heads_to_batch_dim(q)
k = self.reshape_heads_to_batch_dim(k)
v = self.reshape_heads_to_batch_dim(v)
sim = torch.einsum("b i d, b j d -> b i j", q, k) * self.scale
# attention, what we cannot get enough of
attn = sim.softmax(dim=-1)
out = torch.einsum("b i j, b j d -> b i d", attn, v)
out = self.reshape_batch_dim_to_heads(out)
# linear proj
out = self.to_out[0](out)
# dropout
out = self.to_out[1](out)
return out
return forward
def register_recr(net_):
if net_.__class__.__name__ == 'CrossAttention':
net_.forward = ca_forward(net_)
elif hasattr(net_, 'children'):
for net__ in net_.children():
register_recr(net__)
sub_nets = model.unet.named_children()
for net in sub_nets:
if "down" in net[0]:
register_recr(net[1])
elif "up" in net[0]:
register_recr(net[1])
elif "mid" in net[0]:
register_recr(net[1])
@torch.no_grad()
def ddim_reverse_sample(image, prompt, model, num_inference_steps: int = 50, guidance_scale: float = 7.5,
generator: Optional[torch.Generator] = None, args=None):
"""
=== DDIM Inversion ===
"""
batch_size = len(prompt)
text_input = model.tokenizer(
prompt,
padding="max_length",
max_length=model.tokenizer.model_max_length,
truncation=True,
return_tensors="pt",
)
text_embeddings = model.text_encoder(text_input.input_ids.to(model.device))[0]
max_length = text_input.input_ids.shape[-1]
uncond_input = model.tokenizer(
[""] * batch_size, padding="max_length", max_length=max_length, return_tensors="pt"
)
uncond_embeddings = model.text_encoder(uncond_input.input_ids.to(model.device))[0]
context = [uncond_embeddings, text_embeddings]
context = torch.cat(context)
model.scheduler.set_timesteps(num_inference_steps)
latents = encoder(image, model, generator=generator, size=args.size)
timesteps = model.scheduler.timesteps.flip(0)
all_latents = [latents]
# Not inverse the last step, as the alpha_bar_next will be set to 0 which is not aligned to its real value (~0.003)
# and this will lead to a bad result.
for t in tqdm(timesteps[:-1], desc="DDIM_inverse"):
latents_input = torch.cat([latents] * 2)
noise_pred = model.unet(latents_input, t, encoder_hidden_states=context)["sample"]
noise_pred_uncond, noise_prediction_text = noise_pred.chunk(2)
noise_pred = noise_pred_uncond + guidance_scale * (noise_prediction_text - noise_pred_uncond)
next_timestep = t + model.scheduler.config.num_train_timesteps // model.scheduler.num_inference_steps
alpha_bar_next = model.scheduler.alphas_cumprod[next_timestep] \
if next_timestep <= model.scheduler.config.num_train_timesteps else torch.tensor(0.0)
"leverage reversed_x0"
reverse_x0 = (1 / torch.sqrt(model.scheduler.alphas_cumprod[t]) * (
latents - noise_pred * torch.sqrt(1 - model.scheduler.alphas_cumprod[t])))
latents = reverse_x0 * torch.sqrt(alpha_bar_next) + torch.sqrt(1 - alpha_bar_next) * noise_pred
all_latents.append(latents)
# all_latents[N] -> N: DDIM steps (X_{T-1} ~ X_0)
return latents, all_latents
def attention_constraint_text_optimization(prompt, model, mask, latent, size=512, args=None):
"""
Text Embedding Refinement design. Please refer to Section 3.2 in our paper-v2 for more information.
"""
batch_size = len(prompt)
mask = (torchvision.transforms.Resize([size // 32, size // 32])(mask[0, 0].unsqueeze(0)) > 100 / 255.).float()
mask = torch.cat([mask, 1 - mask], dim=0)
text_input = model.tokenizer(
prompt,
padding="max_length",
max_length=model.tokenizer.model_max_length,
truncation=True,
return_tensors="pt",
)
max_length = text_input.input_ids.shape[-1]
uncond_input = model.tokenizer(
[""] * batch_size, padding="max_length", max_length=max_length, return_tensors="pt"
)
uncond_embeddings = model.text_encoder(uncond_input.input_ids.to(model.device))[0].detach()
uncond_embeddings.requires_grad_(False)
"""Extract the text embeddings"""
optim_embeddings = [None, None]
position_start = len(model.tokenizer.encode(prompt[0].split()[0])) - 1
position_end_fg = len(model.tokenizer.encode(prompt[0])) - 2
position_end_bg = len(model.tokenizer.encode(prompt[1])) - 2
position_fg = [x for x in range(position_start, position_end_fg + 1)]
position_bg = [x for x in range(position_start, position_end_bg + 1)]
model.text_encoder.text_model.embeddings.forward = text_embed_reforward(model.text_encoder.text_model.embeddings,
optim_embeddings, position_fg, position_bg)
model.text_encoder(text_input.input_ids.to(model.device))[0].detach()
basis_embeddings_fg = torch.cat(optim_embeddings[0]).clone()
basis_embeddings_bg = torch.cat(optim_embeddings[1]).clone()
optim_embeddings_fg = [x.requires_grad_() for x in optim_embeddings[0]]
optim_embeddings_bg = [x.requires_grad_() for x in optim_embeddings[1]]
weight_emb_bg = torch.nn.Parameter(torch.ones(len(position_bg), device=model.device) / len(position_bg))
optimizer = optim.AdamW(optim_embeddings_fg + optim_embeddings_bg + [weight_emb_bg],
lr=args.op_style_lr)
loss_func = torch.nn.MSELoss()
model.text_encoder.text_model.embeddings.forward = text_embed_reforward(model.text_encoder.text_model.embeddings,
[optim_embeddings_fg, optim_embeddings_bg],
position_fg, position_bg)
text_embeddings = model.text_encoder(text_input.input_ids.to(model.device))[0]
context = [uncond_embeddings, text_embeddings]
context = torch.cat(context)
"""
For optimizing the text embeddings, we design two implementations: optimizing style and training style. We here
only present optimizing style.
"""
"""optimizing style"""
latent, latents = init_latent(latent, model, size, size, None, batch_size)
# Collect all optimized text embeddings in the intermediate diffusion steps.
intermediate_optimized_text_embed = []
pbar = tqdm(model.scheduler.timesteps[1:], desc="Optimize_text_embed")
# The DDIM should begin from 1, as the inversion cannot access X_T but only X_{T-1}
for ind, t in enumerate(pbar):
for _ in range(args.op_style_iters):
controller = AttentionStore()
# Change the `forward()` in CrossAttention module of Diffusion Models.
register_attention_control(model, controller)
diffusion_step(model, controller, latents, context, t, 0)
"""For loss function calculation, please refer to Eq. 2~4 in our paper."""
attention_map_fg = aggregate_attention(prompt, controller, size // 32, ("up", "down"), True, 0).cuda()
attention_map_bg = aggregate_attention(prompt, controller, size // 32, ("up", "down"), True, 1).cuda()
optimizer.zero_grad()
loss_emb = 0
attention_map = 0
for ind, idd in enumerate(position_fg):
attention_map += attention_map_fg[:, :, idd]
loss_emb += loss_func(mask[0], (attention_map / attention_map.max()))
attention_map = 0
for ind, idd in enumerate(position_bg):
attention_map += attention_map_bg[:, :, idd] * weight_emb_bg[ind]
loss_emb += loss_func(mask[1], (attention_map / attention_map.max()))
"""For `loss_reg`, please refer to Eq. (3) in our paper."""
loss_reg = (loss_func(torch.cat(optim_embeddings_fg), basis_embeddings_fg) + loss_func(
torch.cat(optim_embeddings_bg), basis_embeddings_bg)) * args.regulation_weight
pbar.set_postfix_str(
f"loss: {loss_emb.item() + loss_reg.item()}\ttext_emb_loss: {loss_emb.item()}\treg_loss: {loss_reg.item()}")
loss = loss_emb + loss_reg
loss.backward()
optimizer.step()
model.text_encoder.text_model.embeddings.forward = text_embed_reforward(
model.text_encoder.text_model.embeddings,
optim_embeddings, position_fg, position_bg)
text_embeddings = model.text_encoder(text_input.input_ids.to(model.device))[0]
context = [uncond_embeddings, text_embeddings]
context = torch.cat(context)
with torch.no_grad():
weight_emb_bg.data = torch.nn.functional.softmax(weight_emb_bg, dim=0).data
fuse_emb = (text_embeddings[1, position_bg, :].detach().clone()) * weight_emb_bg.unsqueeze(1)
fuse_emb = fuse_emb.sum(0).unsqueeze(0)
text_embeddings[1, position_bg, :] = fuse_emb
intermediate_optimized_text_embed.append(text_embeddings.detach().clone())
with torch.no_grad():
latents = diffusion_step(model, EmptyControl(), latents, context, t, 0)
# reset the `forward()` functions.
reset_attention_control(model)
model.text_encoder.text_model.embeddings.forward = reset_text_embed_reforward(
model.text_encoder.text_model.embeddings)
return intermediate_optimized_text_embed
@torch.enable_grad()
def run(
model,
init_prompt: List[str],
controller,
num_inference_steps: int = 50,
guidance_scale: float = 7.5,
generator: Optional[torch.Generator] = None,
latent: Optional[torch.FloatTensor] = None,
inversion_latents=None,
mask=None,
size=512,
args=None,
original_image=None,
):
"""
Below are the scripts related to Section 3.2 in our paper-v2
Step 1: Optimize the text conditional embeddings to ensure the text embedding can well describe the
foreground/background environment. (See `Text Embedding Refinement` in our Sec. 3.2)
Step 2: Based on the optimized text embedding, optimize the unconditional embeddings to align the edge maps from
sobel and PidNet and also ensure the optimized text embeddings can reconstruct the initial image. (See
Null-Text https://arxiv.org/abs/2211.09794) Note that Step 2 cannot be placed before Step 1, or there
will be no any benefit on the content preservation. (See `Content Structure Preservation` in our Sec. 3.2)
Step 3: Leverage the prompt-to-prompt technique (https://arxiv.org/abs/2208.01626) to harmonize the images. The
core of this step is to fix the cross-attention maps corresponding to foreground text, and then replace
the foreground text embeddings with the background ones. For content retention, we also fix the self-attention
maps. (See `Foreground Editing` and `Content Structure Preservation` in our Sec. 3.2)
"""
"""Detach Diffusion Model parameters to save memory."""
model.vae.requires_grad_(False)
model.text_encoder.requires_grad_(False)
model.unet.requires_grad_(False)
"""Original mask"""
original_mask = mask.resize((size, size), resample=Image.LANCZOS)
original_mask = np.array(original_mask).astype(np.float32) / 255.0
if len(original_mask.shape) == 2:
original_mask = original_mask[:, :, None]
original_mask = original_mask[None].transpose(0, 3, 1, 2)
original_mask = torch.from_numpy(original_mask[:, 0:1])
original_mask = (original_mask > 100 / 255.).float().to(latent.device)
"""Resize the mask to the size of the latent space"""
mask = mask.resize(latent.shape[-2:], resample=Image.LANCZOS)
mask = np.array(mask).astype(np.float32) / 255.0
if len(mask.shape) == 2:
mask = mask[:, :, None]
mask = mask[None].transpose(0, 3, 1, 2)
mask = torch.from_numpy(mask[:, 0:1]).expand_as(latent)
mask = (mask > 100 / 255.).float().to(latent.device)
"""
Step 1
Optimize Text Embedding to ensure the text embedding can well describe the foreground/background environment.
=== See details in Section 3.2 `Text Embedding Refinement` in our paper-v2. ===
"""
constraint_text_emb = attention_constraint_text_optimization(init_prompt, model, mask, latent,
size=size, args=args)
"""
Step 2
Optimize a better unconditional embedding that can align the edge maps from sobel and PidNet and also reconstruct
the original image. The optimization follows `Null-text inversion for editing real images using guided diffusion models`
(https://arxiv.org/abs/2211.09794).
=== See details in the beginning of Section 3.2 `Content Structure Preservation`in our paper-v2 ===
"""
prompt = [init_prompt[0]] # Only use the first prompt, which describes the foreground.
batch_size = len(prompt)
text_input = model.tokenizer(
prompt,
padding="max_length",
max_length=model.tokenizer.model_max_length,
truncation=True,
return_tensors="pt",
)
max_length = text_input.input_ids.shape[-1]
uncond_input = model.tokenizer(
[""] * batch_size, padding="max_length", max_length=max_length, return_tensors="pt"
)
uncond_embeddings = model.text_encoder(uncond_input.input_ids.to(model.device))[0].detach()
uncond_embeddings.requires_grad_(True)
optimizer = optim.AdamW([uncond_embeddings], lr=args.uncond_optimized_lr)
loss_func = torch.nn.MSELoss()
context = [uncond_embeddings, constraint_text_emb[0][:1]]
context = torch.cat(context)
latent, latents = init_latent(latent, model, size, size, generator, batch_size)
# Collect all optimized unconditional embeddings in the intermediate diffusion steps.
intermediate_optimized_uncond_emb = []
# Calculate the original's edge map.
pidnet = Initialize_PidNet(pidnet_args)
original_edge = PidNet(pidnet, preprocess_pidnet(original_image, 512))
original_sobel = get_edge(preprocess(original_image, 512))
# The DDIM should begin from 1, as the inversion cannot access X_T but only X_{T-1}
for ind, t in enumerate(tqdm(model.scheduler.timesteps[1:], desc="Optimize_uncond_embed")):
for _ in range(ind // 10 + 1):
out_latents = diffusion_step(model, EmptyControl(), latents, context, t, guidance_scale)
image_sobel, image_edge = latent2image_edge_fusion(model.vae, out_latents)
# Calculate the harmonized image's edge maps.
out_edge = PidNet(pidnet, image_edge)
out_sobel = get_edge(image_sobel)
optimizer.zero_grad()
loss_null_text = loss_func(out_latents, inversion_latents[ind + 1])
loss_edge_all = 0.1 * loss_func(out_edge * original_mask, original_edge * original_mask) + loss_func(
out_sobel * original_mask, original_sobel * original_mask)
loss = 10 * loss_null_text + (loss_edge_all if args.use_edge_map else 0)
loss.backward()
optimizer.step()
context = [uncond_embeddings, constraint_text_emb[ind][:1]]
context = torch.cat(context)
with torch.no_grad():
latents = diffusion_step(model, EmptyControl(), latents, context, t, guidance_scale).detach()
intermediate_optimized_uncond_emb.append(uncond_embeddings.detach().clone())
image = latent2image(model.vae, latents)
view_images(image)
"""
Step 3
After getting the optimized text embeddings, we can use them to harmonize the image. The technique used here is
prompt to prompt (https://arxiv.org/abs/2208.01626).
=== See details in `Foreground Editing` of Section 3.2 in our paper-v2 ===
"""
# Change the `forward()` in CrossAttention module of Diffusion Models.
register_attention_control(model, controller)
batch_size = len(init_prompt)
context = [[torch.cat([intermediate_optimized_uncond_emb[i]] * batch_size), constraint_text_emb[i]] for i in
range(len(intermediate_optimized_uncond_emb))]
context = [torch.cat(i) for i in context]
latent, latents = init_latent(latent, model, size, size, generator, batch_size)
model.scheduler.set_timesteps(num_inference_steps)
# The DDIM should begin from 1 + start_step, as the inversion cannot access X_T but only X_{T-1}
for ind, t in enumerate(tqdm(model.scheduler.timesteps[1:], desc="P2P_harmon_process")):
latents = diffusion_step(model, controller, latents, context[ind], t, guidance_scale)
image = latent2image(model.vae, latents)
reset_attention_control(model)
return image, latent