(self, x_in, continous=False, get_interm_fm=False)
| 443 | |
| 444 | @torch.no_grad() |
| 445 | def p_sample_loop(self, x_in, continous=False, get_interm_fm=False): |
| 446 | device = self.betas.device |
| 447 | clip_noise = True if exists(self.clamp_range) else False |
| 448 | sample_inter = 1 | (self.num_timesteps // 10) |
| 449 | |
| 450 | # self-conditioning |
| 451 | x_start = None |
| 452 | |
| 453 | if not self.conditional: |
| 454 | shape = x_in |
| 455 | b = shape[0] |
| 456 | img = torch.randn(shape, device=device) |
| 457 | ret_img = img |
| 458 | for i in tqdm( |
| 459 | reversed(range(0, self.num_timesteps)), |
| 460 | desc="ddpm sampling loop time step", |
| 461 | total=self.num_timesteps, |
| 462 | ): |
| 463 | self_cond = x_start if self.self_condition else None |
| 464 | img = self.p_sample( |
| 465 | img, |
| 466 | torch.full((b,), i, device=device, dtype=torch.long), |
| 467 | self_cond=self_cond, |
| 468 | clip_denoised=clip_noise, |
| 469 | get_interm_fm=get_interm_fm, |
| 470 | ) |
| 471 | |
| 472 | if i % sample_inter == 0 and continous: |
| 473 | ret_img = torch.cat([ret_img, img], dim=0) |
| 474 | |
| 475 | x_start = img |
| 476 | return img |
| 477 | else: |
| 478 | x = x_in # sr |
| 479 | shape = x_in.shape[-2:] |
| 480 | if isinstance(x, list): |
| 481 | b = x[0].shape[0] |
| 482 | elif isinstance(x, Tensor): |
| 483 | b = x.shape[0] |
| 484 | img = torch.randn((b, self.channels, *shape), device=device) |
| 485 | ret_img = img |
| 486 | for i in tqdm( |
| 487 | reversed(range(0, self.num_timesteps)), |
| 488 | desc="sampling loop time step", |
| 489 | total=self.num_timesteps, |
| 490 | ): |
| 491 | self_cond = x_start if self.self_condition else None |
| 492 | img = self.p_sample( |
| 493 | img, |
| 494 | torch.full((b,), i, device=device, dtype=torch.long), |
| 495 | condition_x=x, |
| 496 | self_cond=self_cond, |
| 497 | clip_denoised=clip_noise, |
| 498 | get_interm_fm=get_interm_fm, |
| 499 | ) |
| 500 | if i % sample_inter == 0 and continous: |
| 501 | ret_img = torch.cat([ret_img, img], dim=0) |
| 502 | x_start = img |
no test coverage detected