Spaces:
Running
on
Zero
Running
on
Zero
File size: 27,780 Bytes
0a63786 c8013a6 0a63786 c8013a6 0a63786 c8013a6 0a63786 |
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 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 |
import torch
import numpy as np
from typing import Optional, Union, Tuple, List, Callable, Dict
from tqdm import tqdm
import torch
from diffusers import DDPMScheduler, DDIMScheduler, PNDMScheduler
import torch.nn.functional as nnf
import numpy as np
from einops import rearrange
from misc_utils.flow_utils import warp_image, RAFTFlow, resize_flow
from functools import partial
def rescale_noise_cfg(noise_cfg, noise_pred_text, guidance_rescale=0.0):
"""
Rescale `noise_cfg` according to `guidance_rescale`. Based on findings of [Common Diffusion Noise Schedules and
Sample Steps are Flawed](https://arxiv.org/pdf/2305.08891.pdf). See Section 3.4
"""
std_text = noise_pred_text.std(dim=list(range(1, noise_pred_text.ndim)), keepdim=True)
std_cfg = noise_cfg.std(dim=list(range(1, noise_cfg.ndim)), keepdim=True)
# rescale the results from guidance (fixes overexposure)
noise_pred_rescaled = noise_cfg * (std_text / std_cfg)
# mix with the original results from guidance by factor guidance_rescale to avoid "plain looking" images
noise_cfg = guidance_rescale * noise_pred_rescaled + (1 - guidance_rescale) * noise_cfg
return noise_cfg
class Inference():
def __init__(
self,
unet,
scheduler='ddim',
beta_start=0.00085, beta_end=0.012, beta_schedule="scaled_linear",
num_ddim_steps=20, guidance_scale=5,
):
self.unet = unet
if scheduler == 'ddim':
scheduler_cls = DDIMScheduler
scheduler_kwargs = {'set_alpha_to_one': False, 'steps_offset': 1, 'clip_sample': False}
elif scheduler == 'ddpm':
scheduler_cls = DDPMScheduler
scheduler_kwargs = {'clip_sample': False}
else:
raise NotImplementedError()
self.scheduler = scheduler_cls(
beta_start = beta_start,
beta_end = beta_end,
beta_schedule = beta_schedule,
**scheduler_kwargs
)
self.scheduler.set_timesteps(num_ddim_steps)
self.num_ddim_steps = num_ddim_steps
self.guidance_scale = guidance_scale
@torch.no_grad()
def __call__(
self,
latent: torch.Tensor,
context: torch.Tensor,
uncond_context: torch.Tensor=None,
start_time: int = 0,
null_embedding: List[torch.Tensor]=None,
context_kwargs={},
model_kwargs={},
):
all_latent = []
all_pred = [] # x0_hat
do_classifier_free_guidance = self.guidance_scale > 1 and ((uncond_context is not None) or (null_embedding is not None))
for i, t in enumerate(tqdm(self.scheduler.timesteps[start_time:])):
t = int(t)
if do_classifier_free_guidance:
latent_input = torch.cat([latent, latent], dim=0)
if null_embedding is not None:
context_input = torch.cat([null_embedding[i], context], dim=0)
else:
context_input = torch.cat([uncond_context, context], dim=0)
else:
latent_input = latent
context_input = context
noise_pred = self.unet(
latent_input,
torch.full((len(latent_input),), t, device=latent_input.device, dtype=torch.long),
context={ 'text': context_input, **context_kwargs},
**model_kwargs
)
if do_classifier_free_guidance:
noise_pred_uncond, noise_pred_cond = noise_pred.chunk(2, dim=0)
noise_pred = noise_pred_uncond + self.guidance_scale * (noise_pred_cond - noise_pred_uncond)
pred_samples = self.scheduler.step(noise_pred, t, latent)
latent = pred_samples.prev_sample
pred = pred_samples.pred_original_sample
all_latent.append(latent.detach())
all_pred.append(pred.detach())
return {
'latent': latent,
'all_latent': all_latent,
'all_pred': all_pred
}
class InferenceIP2PEditRef(Inference):
def zeros(self, x):
return torch.zeros_like(x)
@torch.no_grad()
def __call__(
self,
latent: torch.Tensor,
text_cond: torch.Tensor,
text_uncond: torch.Tensor,
img_cond: torch.Tensor,
edit_cond: torch.Tensor,
text_cfg = 7.5,
img_cfg = 1.2,
edit_cfg = 1.2,
start_time: int = 0,
):
'''
latent1 | latent2 | latent3 | latent4
text x x x v
edit x x v v
img x v v v
'''
all_latent = []
all_pred = [] # x0_hat
for i, t in enumerate(tqdm(self.scheduler.timesteps[start_time:])):
t = int(t)
latent1 = torch.cat([latent, self.zeros(img_cond), self.zeros(edit_cond)], dim=1)
latent2 = torch.cat([latent, img_cond, self.zeros(edit_cond)], dim=1)
latent3 = torch.cat([latent, img_cond, edit_cond], dim=1)
latent4 = latent3.clone()
latent_input = torch.cat([latent1, latent2, latent3, latent4], dim=0)
context_input = torch.cat([text_uncond, text_uncond, text_uncond, text_cond], dim=0)
noise_pred = self.unet(
latent_input,
torch.full((len(latent_input),), t, device=latent_input.device, dtype=torch.long),
context={ 'text': context_input},
)
noise_pred1, noise_pred2, noise_pred3, noise_pred4 = noise_pred.chunk(4, dim=0)
noise_pred = (
noise_pred1 +
img_cfg * (noise_pred2 - noise_pred1) +
edit_cfg * (noise_pred3 - noise_pred2) +
text_cfg * (noise_pred4 - noise_pred3)
) # when edit_cfg == img_cfg, noise_pred2 is not used
pred_samples = self.scheduler.step(noise_pred, t, latent)
latent = pred_samples.prev_sample
pred = pred_samples.pred_original_sample
all_latent.append(latent.detach())
all_pred.append(pred.detach())
return {
'latent': latent,
'all_latent': all_latent,
'all_pred': all_pred
}
class InferenceIP2PVideo(Inference):
def zeros(self, x):
return torch.zeros_like(x)
@torch.no_grad()
def __call__(
self,
latent: torch.Tensor,
text_cond: torch.Tensor,
text_uncond: torch.Tensor,
img_cond: torch.Tensor,
text_cfg = 7.5,
img_cfg = 1.2,
start_time: int = 0,
guidance_rescale: float = 0.0,
):
'''
latent1 | latent2 | latent3
text x x v
img x v v
'''
# all_latent = []
# all_pred = [] # x0_hat
for i, t in enumerate(tqdm(self.scheduler.timesteps[start_time:])):
t = int(t)
latent1 = torch.cat([latent, self.zeros(img_cond)], dim=2)
latent2 = torch.cat([latent, img_cond], dim=2)
latent3 = latent2.clone()
latent_input = torch.cat([latent1, latent2, latent3], dim=0)
context_input = torch.cat([text_uncond, text_uncond, text_cond], dim=0)
latent_input = rearrange(latent_input, 'b f c h w -> b c f h w')
noise_pred = self.unet(
latent_input,
torch.full((len(latent_input),), t, device=latent_input.device, dtype=torch.long),
encoder_hidden_states=context_input,
).sample
noise_pred = rearrange(noise_pred, 'b c f h w -> b f c h w')
noise_pred1, noise_pred2, noise_pred3 = noise_pred.chunk(3, dim=0)
noise_pred = (
noise_pred1 +
img_cfg * (noise_pred2 - noise_pred1) +
text_cfg * (noise_pred3 - noise_pred2)
)
if guidance_rescale > 0:
noise_pred = rescale_noise_cfg(noise_pred, noise_pred1, guidance_rescale=guidance_rescale)
pred_samples = self.scheduler.step(noise_pred, t, latent)
latent = pred_samples.prev_sample
pred = pred_samples.pred_original_sample
del noise_pred, noise_pred1, noise_pred2, noise_pred3, pred_samples
del latent_input, context_input
torch.cuda.empty_cache()
# all_latent.append(latent.detach())
# all_pred.append(pred.detach())
return {
'latent': latent,
# 'all_latent': all_latent,
# 'all_pred': all_pred
}
@torch.no_grad()
def second_clip_forward(
self,
latent: torch.Tensor,
text_cond: torch.Tensor,
text_uncond: torch.Tensor,
img_cond: torch.Tensor,
latent_ref: torch.Tensor,
noise_correct_step: float = 1.,
text_cfg = 7.5,
img_cfg = 1.2,
start_time: int = 0,
guidance_rescale: float = 0.0,
):
'''
latent1 | latent2 | latent3
text x x v
img x v v
'''
num_ref_frames = latent_ref.shape[1]
all_latent = []
all_pred = [] # x0_hat
for i, t in enumerate(tqdm(self.scheduler.timesteps[start_time:])):
t = int(t)
latent1 = torch.cat([latent, self.zeros(img_cond)], dim=2)
latent2 = torch.cat([latent, img_cond], dim=2)
latent3 = latent2.clone()
latent_input = torch.cat([latent1, latent2, latent3], dim=0)
context_input = torch.cat([text_uncond, text_uncond, text_cond], dim=0)
latent_input = rearrange(latent_input, 'b f c h w -> b c f h w')
noise_pred = self.unet(
latent_input,
torch.full((len(latent_input),), t, device=latent_input.device, dtype=torch.long),
encoder_hidden_states=context_input,
).sample
noise_pred = rearrange(noise_pred, 'b c f h w -> b f c h w')
noise_pred1, noise_pred2, noise_pred3 = noise_pred.chunk(3, dim=0)
noise_pred = (
noise_pred1 +
img_cfg * (noise_pred2 - noise_pred1) +
text_cfg * (noise_pred3 - noise_pred2)
)
if guidance_rescale > 0:
noise_pred = rescale_noise_cfg(noise_pred, noise_pred1, guidance_rescale=guidance_rescale)
# 所谓的再inference阶段加入 Long Video Sampling Correction(LVSC)
if noise_correct_step * self.num_ddim_steps > i:
alpha_prod_t = self.scheduler.alphas_cumprod[t]
beta_prod_t = 1 - alpha_prod_t
noise_ref = (latent[:, 0:num_ref_frames] - (alpha_prod_t ** 0.5) * latent_ref) / (beta_prod_t ** 0.5) # b 1 c h w
delta_noise_ref = noise_ref - noise_pred[:, 0:num_ref_frames]
delta_noise_remaining = delta_noise_ref.mean(dim=1, keepdim=True)
noise_pred[:, :num_ref_frames] = noise_pred[:, :num_ref_frames] + delta_noise_ref
noise_pred[:, num_ref_frames:] = noise_pred[:, num_ref_frames:] + delta_noise_remaining
pred_samples = self.scheduler.step(noise_pred, t, latent)
latent = pred_samples.prev_sample
pred = pred_samples.pred_original_sample
all_latent.append(latent.detach())
all_pred.append(pred.detach())
return {
'latent': latent,
'all_latent': all_latent,
'all_pred': all_pred
}
class InferenceIP2PVideoEnsemble(Inference):
def zeros(self, x):
return torch.zeros_like(x)
@torch.no_grad()
def __call__(
self,
latent: torch.Tensor,
text_cond: torch.Tensor,
text_uncond: torch.Tensor,
img_cond: torch.Tensor,
text_cfg = 7.5,
img_cfg = 1.2,
start_time: int = 0,
guidance_rescale: float = 0.0,
):
'''
latent1 | latent2 | latent3
text x x v
img x v v
'''
all_latent = []
all_pred = [] # x0_hat
for i, t in enumerate(tqdm(self.scheduler.timesteps[start_time:])):
t = int(t)
latent1 = torch.cat([latent, self.zeros(img_cond)], dim=2)
latent2 = torch.cat([latent, img_cond], dim=2)
latent3 = latent2.clone()
latent_input = torch.cat([latent1, latent2, latent3], dim=0)
context_input = torch.cat([text_uncond, text_uncond, text_cond], dim=0)
latent_input = rearrange(latent_input, 'b f c h w -> b c f h w')
noise_pred = self.unet(
latent_input,
torch.full((len(latent_input),), t, device=latent_input.device, dtype=torch.long),
encoder_hidden_states=context_input,
).sample
noise_pred = rearrange(noise_pred, 'b c f h w -> b f c h w')
noise_pred1, noise_pred2, noise_pred3 = noise_pred.chunk(3, dim=0)
noise_pred = (
noise_pred1 +
img_cfg * (noise_pred2 - noise_pred1) +
text_cfg * (noise_pred3 - noise_pred2)
)
if guidance_rescale > 0:
noise_pred = rescale_noise_cfg(noise_pred, noise_pred1, guidance_rescale=guidance_rescale)
pred_samples = self.scheduler.step(noise_pred, t, latent)
latent = pred_samples.prev_sample
# average over all three samples.
latent = latent.mean(dim=0, keepdim=True).repeat(latent.shape[0], 1, 1, 1, 1)
# latent = latent[[0]].repeat(latent.shape[0], 1, 1, 1, 1)
pred = pred_samples.pred_original_sample
all_latent.append(latent.detach())
all_pred.append(pred.detach())
return {
'latent': latent,
'all_latent': all_latent,
'all_pred': all_pred
}
@torch.no_grad()
def second_clip_forward(
self,
latent: torch.Tensor,
text_cond: torch.Tensor,
text_uncond: torch.Tensor,
img_cond: torch.Tensor,
latent_ref: torch.Tensor,
noise_correct_step: float = 1.,
text_cfg = 7.5,
img_cfg = 1.2,
start_time: int = 0,
guidance_rescale: float = 0.0,
):
'''
latent1 | latent2 | latent3
text x x v
img x v v
'''
num_ref_frames = latent_ref.shape[1]
all_latent = []
all_pred = [] # x0_hat
for i, t in enumerate(tqdm(self.scheduler.timesteps[start_time:])):
t = int(t)
latent1 = torch.cat([latent, self.zeros(img_cond)], dim=2)
latent2 = torch.cat([latent, img_cond], dim=2)
latent3 = latent2.clone()
latent_input = torch.cat([latent1, latent2, latent3], dim=0)
context_input = torch.cat([text_uncond, text_uncond, text_cond], dim=0)
latent_input = rearrange(latent_input, 'b f c h w -> b c f h w')
noise_pred = self.unet(
latent_input,
torch.full((len(latent_input),), t, device=latent_input.device, dtype=torch.long),
encoder_hidden_states=context_input,
).sample
noise_pred = rearrange(noise_pred, 'b c f h w -> b f c h w')
noise_pred1, noise_pred2, noise_pred3 = noise_pred.chunk(3, dim=0)
noise_pred = (
noise_pred1 +
img_cfg * (noise_pred2 - noise_pred1) +
text_cfg * (noise_pred3 - noise_pred2)
)
if guidance_rescale > 0:
noise_pred = rescale_noise_cfg(noise_pred, noise_pred1, guidance_rescale=guidance_rescale)
# 所谓的再inference阶段加入 Long Video Sampling Correction(LVSC)
if noise_correct_step * self.num_ddim_steps > i:
alpha_prod_t = self.scheduler.alphas_cumprod[t]
beta_prod_t = 1 - alpha_prod_t
noise_ref = (latent[:, 0:num_ref_frames] - (alpha_prod_t ** 0.5) * latent_ref) / (beta_prod_t ** 0.5) # b 1 c h w
delta_noise_ref = noise_ref - noise_pred[:, 0:num_ref_frames]
delta_noise_remaining = delta_noise_ref.mean(dim=1, keepdim=True)
noise_pred[:, :num_ref_frames] = noise_pred[:, :num_ref_frames] + delta_noise_ref
noise_pred[:, num_ref_frames:] = noise_pred[:, num_ref_frames:] + delta_noise_remaining
pred_samples = self.scheduler.step(noise_pred, t, latent)
latent = pred_samples.prev_sample
pred = pred_samples.pred_original_sample
all_latent.append(latent.detach())
all_pred.append(pred.detach())
return {
'latent': latent,
'all_latent': all_latent,
'all_pred': all_pred
}
class InferenceIP2PVideoHDR(Inference):
def zeros(self, x):
return torch.zeros_like(x)
@torch.no_grad()
def __call__(
self,
latent: torch.Tensor,
text_cond: torch.Tensor,
text_uncond: torch.Tensor,#(1,77,768)
hdr_cond: torch.Tensor, #(1,3,768)
img_cond: torch.Tensor,
text_cfg = 7.5,
img_cfg = 1.2,
hdr_cfg = 7.5,
start_time: int = 0,
guidance_rescale: float = 0.0,
):
'''
latent1 | latent2 | latent3 | latent4
text x x v v
img x v v v
hdr x x x v
'''
all_latent = []
all_pred = [] # x0_hat
for i, t in enumerate(tqdm(self.scheduler.timesteps[start_time:])):
t = int(t)
latent1 = torch.cat([latent, self.zeros(img_cond)], dim=2)
latent2 = torch.cat([latent, img_cond], dim=2)
latent3 = latent2.clone()
latent4 = latent2.clone()
latent_input = torch.cat([latent1, latent2, latent3, latent4], dim=0)
context_input = torch.cat([text_uncond, text_uncond, text_cond, text_cond], dim=0) #(4,77,768)
hdr_uncond = self.zeros(hdr_cond)
hdr_input = torch.cat([hdr_uncond, hdr_uncond, hdr_uncond, hdr_cond]) #(4,3,768)
model_kwargs1 = {'hdr_latents': hdr_input, 'encoder_hidden_states': context_input}
latent_input = rearrange(latent_input, 'b f c h w -> b c f h w')
noise_pred = self.unet(
latent_input,
torch.full((len(latent_input),), t, device=latent_input.device, dtype=torch.long),
encoder_hidden_states=model_kwargs1,
).sample
noise_pred = rearrange(noise_pred, 'b c f h w -> b f c h w')
noise_pred1, noise_pred2, noise_pred3, noise_pred4 = noise_pred.chunk(4, dim=0)
noise_pred = (
noise_pred1 +
img_cfg * (noise_pred2 - noise_pred1) +
text_cfg * (noise_pred3 - noise_pred2) +
hdr_cfg * (noise_pred4 - noise_pred3)
)
if guidance_rescale > 0:
noise_pred = rescale_noise_cfg(noise_pred, noise_pred1, guidance_rescale=guidance_rescale)
pred_samples = self.scheduler.step(noise_pred, t, latent)
latent = pred_samples.prev_sample
pred = pred_samples.pred_original_sample
all_latent.append(latent.detach())
all_pred.append(pred.detach())
return {
'latent': latent,
'all_latent': all_latent,
'all_pred': all_pred
}
@torch.no_grad()
def second_clip_forward(
self,
latent: torch.Tensor,
text_cond: torch.Tensor,
text_uncond: torch.Tensor,
img_cond: torch.Tensor,
latent_ref: torch.Tensor,
noise_correct_step: float = 1.,
text_cfg = 7.5,
img_cfg = 1.2,
start_time: int = 0,
guidance_rescale: float = 0.0,
):
'''
latent1 | latent2 | latent3
text x x v
img x v v
'''
num_ref_frames = latent_ref.shape[1]
all_latent = []
all_pred = [] # x0_hat
for i, t in enumerate(tqdm(self.scheduler.timesteps[start_time:])):
t = int(t)
latent1 = torch.cat([latent, self.zeros(img_cond)], dim=2)
latent2 = torch.cat([latent, img_cond], dim=2)
latent3 = latent2.clone()
latent_input = torch.cat([latent1, latent2, latent3], dim=0)
context_input = torch.cat([text_uncond, text_uncond, text_cond], dim=0)
latent_input = rearrange(latent_input, 'b f c h w -> b c f h w')
noise_pred = self.unet(
latent_input,
torch.full((len(latent_input),), t, device=latent_input.device, dtype=torch.long),
encoder_hidden_states=context_input,
).sample
noise_pred = rearrange(noise_pred, 'b c f h w -> b f c h w')
noise_pred1, noise_pred2, noise_pred3 = noise_pred.chunk(3, dim=0)
noise_pred = (
noise_pred1 +
img_cfg * (noise_pred2 - noise_pred1) +
text_cfg * (noise_pred3 - noise_pred2)
)
if guidance_rescale > 0:
noise_pred = rescale_noise_cfg(noise_pred, noise_pred1, guidance_rescale=guidance_rescale)
# 所谓的再inference阶段加入 Long Video Sampling Correction(LVSC)
if noise_correct_step * self.num_ddim_steps > i:
alpha_prod_t = self.scheduler.alphas_cumprod[t]
beta_prod_t = 1 - alpha_prod_t
noise_ref = (latent[:, 0:num_ref_frames] - (alpha_prod_t ** 0.5) * latent_ref) / (beta_prod_t ** 0.5) # b 1 c h w
delta_noise_ref = noise_ref - noise_pred[:, 0:num_ref_frames]
delta_noise_remaining = delta_noise_ref.mean(dim=1, keepdim=True)
noise_pred[:, :num_ref_frames] = noise_pred[:, :num_ref_frames] + delta_noise_ref
noise_pred[:, num_ref_frames:] = noise_pred[:, num_ref_frames:] + delta_noise_remaining
pred_samples = self.scheduler.step(noise_pred, t, latent)
latent = pred_samples.prev_sample
pred = pred_samples.pred_original_sample
all_latent.append(latent.detach())
all_pred.append(pred.detach())
return {
'latent': latent,
'all_latent': all_latent,
'all_pred': all_pred
}
class InferenceIP2PVideoOpticalFlow(InferenceIP2PVideo):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.flow_estimator = RAFTFlow().cuda() # 使用光流估计器
def obtain_delta_noise(self, delta_noise_ref, flow):
flow = resize_flow(flow, delta_noise_ref.shape[2:])
warped_delta_noise_ref = warp_image(delta_noise_ref, flow) # 根据光流扭曲参考帧的噪声差异
valid_mask = torch.ones_like(delta_noise_ref)[:, :1]
valid_mask = warp_image(valid_mask, flow)
return warped_delta_noise_ref, valid_mask
def obtain_flow_batched(self, ref_images, query_images):
ref_images = ref_images.to()
warp_funcs = []
for query_image in query_images:
query_image = query_image.unsqueeze(0).repeat(len(ref_images), 1, 1, 1)
flow = self.flow_estimator(query_image, ref_images) # 估计光流
warp_func = partial(self.obtain_delta_noise, flow=flow)
warp_funcs.append(warp_func)
return warp_funcs
@torch.no_grad()
def second_clip_forward(
self,
latent: torch.Tensor,
text_cond: torch.Tensor,
text_uncond: torch.Tensor,
img_cond: torch.Tensor,
latent_ref: torch.Tensor,
ref_images: torch.Tensor,
query_images: torch.Tensor,
noise_correct_step: float = 1.,
text_cfg = 7.5,
img_cfg = 1.2,
start_time: int = 0,
guidance_rescale: float = 0.0,
):
'''
latent1 | latent2 | latent3
text x x v
img x v v
'''
assert ref_images.shape[0] == 1, 'only support batch size 1'
warp_funcs = self.obtain_flow_batched(ref_images[0], query_images[0])
num_ref_frames = latent_ref.shape[1]
all_latent = []
all_pred = [] # x0_hat
for i, t in enumerate(tqdm(self.scheduler.timesteps[start_time:])):
t = int(t)
latent1 = torch.cat([latent, self.zeros(img_cond)], dim=2)
latent2 = torch.cat([latent, img_cond], dim=2)
latent3 = latent2.clone()
latent_input = torch.cat([latent1, latent2, latent3], dim=0)
context_input = torch.cat([text_uncond, text_uncond, text_cond], dim=0)
latent_input = rearrange(latent_input, 'b f c h w -> b c f h w')
noise_pred = self.unet(
latent_input,
torch.full((len(latent_input),), t, device=latent_input.device, dtype=torch.long),
encoder_hidden_states=context_input,
).sample
noise_pred = rearrange(noise_pred, 'b c f h w -> b f c h w')
noise_pred1, noise_pred2, noise_pred3 = noise_pred.chunk(3, dim=0)
noise_pred = (
noise_pred1 +
img_cfg * (noise_pred2 - noise_pred1) +
text_cfg * (noise_pred3 - noise_pred2)
)
if guidance_rescale > 0:
noise_pred = rescale_noise_cfg(noise_pred, noise_pred1, guidance_rescale=guidance_rescale)
if noise_correct_step * self.num_ddim_steps > i:
alpha_prod_t = self.scheduler.alphas_cumprod[t]
beta_prod_t = 1 - alpha_prod_t
noise_ref = (latent[:, 0:num_ref_frames] - (alpha_prod_t ** 0.5) * latent_ref) / (beta_prod_t ** 0.5) # b 1 c h w
delta_noise_ref = noise_ref - noise_pred[:, 0:num_ref_frames]
noise_pred[:, :num_ref_frames] = noise_pred[:, :num_ref_frames] + delta_noise_ref
for refed_index, warp_func in zip(range(num_ref_frames, noise_pred.shape[1]), warp_funcs):
delta_noise_remaining, delta_noise_mask = warp_func(delta_noise_ref[0])
mask_sum = delta_noise_mask[None].sum(dim=1, keepdim=True)
delta_noise_remaining = torch.where(
mask_sum > 0.5,
delta_noise_remaining[None].sum(dim=1, keepdim=True) / mask_sum,
0.
)
noise_pred[:, refed_index: refed_index+1] += torch.where(
mask_sum > 0.5,
delta_noise_remaining,
0
) # 将这个扭曲的噪声差异应用到当前帧,确保帧之间的噪声变化符合视频中物体的移动
pred_samples = self.scheduler.step(noise_pred, t, latent)
latent = pred_samples.prev_sample
pred = pred_samples.pred_original_sample
all_latent.append(latent.detach())
all_pred.append(pred.detach())
return {
'latent': latent,
'all_latent': all_latent,
'all_pred': all_pred
} |