(self, batch, N=8, n_row=4, sample=True, ddim_steps=200, ddim_eta=1., return_keys=None,
quantize_denoised=True, inpaint=True, plot_denoise_rows=False, plot_progressive_rows=True,
plot_diffusion_rows=True, unconditional_guidance_scale=1., unconditional_guidance_label=None,
use_ema_scope=True,
**kwargs)
| 1559 | |
| 1560 | @torch.no_grad() |
| 1561 | def log_images(self, batch, N=8, n_row=4, sample=True, ddim_steps=200, ddim_eta=1., return_keys=None, |
| 1562 | quantize_denoised=True, inpaint=True, plot_denoise_rows=False, plot_progressive_rows=True, |
| 1563 | plot_diffusion_rows=True, unconditional_guidance_scale=1., unconditional_guidance_label=None, |
| 1564 | use_ema_scope=True, |
| 1565 | **kwargs): |
| 1566 | ema_scope = self.ema_scope if use_ema_scope else nullcontext |
| 1567 | use_ddim = ddim_steps is not None |
| 1568 | |
| 1569 | log = dict() |
| 1570 | z, c, x, xrec, xc = self.get_input(batch, self.first_stage_key, bs=N, return_first_stage_outputs=True) |
| 1571 | c_cat, c = c["c_concat"][0], c["c_crossattn"][0] |
| 1572 | N = min(x.shape[0], N) |
| 1573 | n_row = min(x.shape[0], n_row) |
| 1574 | log["inputs"] = x |
| 1575 | log["reconstruction"] = xrec |
| 1576 | if self.model.conditioning_key is not None: |
| 1577 | if hasattr(self.cond_stage_model, "decode"): |
| 1578 | xc = self.cond_stage_model.decode(c) |
| 1579 | log["conditioning"] = xc |
| 1580 | elif self.cond_stage_key in ["caption", "txt"]: |
| 1581 | xc = log_txt_as_img((x.shape[2], x.shape[3]), batch[self.cond_stage_key], size=x.shape[2] // 25) |
| 1582 | log["conditioning"] = xc |
| 1583 | elif self.cond_stage_key in ['class_label', 'cls']: |
| 1584 | xc = log_txt_as_img((x.shape[2], x.shape[3]), batch["human_label"], size=x.shape[2] // 25) |
| 1585 | log['conditioning'] = xc |
| 1586 | elif isimage(xc): |
| 1587 | log["conditioning"] = xc |
| 1588 | if ismap(xc): |
| 1589 | log["original_conditioning"] = self.to_rgb(xc) |
| 1590 | |
| 1591 | if not (self.c_concat_log_start is None and self.c_concat_log_end is None): |
| 1592 | log["c_concat_decoded"] = self.decode_first_stage(c_cat[:, self.c_concat_log_start:self.c_concat_log_end]) |
| 1593 | |
| 1594 | if plot_diffusion_rows: |
| 1595 | # get diffusion row |
| 1596 | diffusion_row = list() |
| 1597 | z_start = z[:n_row] |
| 1598 | for t in range(self.num_timesteps): |
| 1599 | if t % self.log_every_t == 0 or t == self.num_timesteps - 1: |
| 1600 | t = repeat(torch.tensor([t]), '1 -> b', b=n_row) |
| 1601 | t = t.to(self.device).long() |
| 1602 | noise = torch.randn_like(z_start) |
| 1603 | z_noisy = self.q_sample(x_start=z_start, t=t, noise=noise) |
| 1604 | diffusion_row.append(self.decode_first_stage(z_noisy)) |
| 1605 | |
| 1606 | diffusion_row = torch.stack(diffusion_row) # n_log_step, n_row, C, H, W |
| 1607 | diffusion_grid = rearrange(diffusion_row, 'n b c h w -> b n c h w') |
| 1608 | diffusion_grid = rearrange(diffusion_grid, 'b n c h w -> (b n) c h w') |
| 1609 | diffusion_grid = make_grid(diffusion_grid, nrow=diffusion_row.shape[0]) |
| 1610 | log["diffusion_row"] = diffusion_grid |
| 1611 | |
| 1612 | if sample: |
| 1613 | # get denoise row |
| 1614 | with ema_scope("Sampling"): |
| 1615 | samples, z_denoise_row = self.sample_log(cond={"c_concat": [c_cat], "c_crossattn": [c]}, |
| 1616 | batch_size=N, ddim=use_ddim, |
| 1617 | ddim_steps=ddim_steps, eta=ddim_eta) |
| 1618 | # samples, z_denoise_row = self.sample(cond=c, batch_size=N, return_intermediates=True) |
nothing calls this directly
no test coverage detected