|
| 1 | +import torch |
| 2 | +from torch import nn |
| 3 | +import torch.nn.functional as F |
| 4 | +from einops import repeat |
| 5 | + |
| 6 | +from taming.modules.discriminator.model import NLayerDiscriminator, weights_init |
| 7 | +from taming.modules.losses.lpips import LPIPS |
| 8 | +from taming.modules.losses.vqperceptual import hinge_d_loss, vanilla_d_loss |
| 9 | + |
| 10 | + |
| 11 | +def hinge_d_loss_with_exemplar_weights(logits_real, logits_fake, weights): |
| 12 | + assert weights.shape[0] == logits_real.shape[0] == logits_fake.shape[0] |
| 13 | + loss_real = torch.mean(F.relu(1. - logits_real), dim=[1,2,3]) |
| 14 | + loss_fake = torch.mean(F.relu(1. + logits_fake), dim=[1,2,3]) |
| 15 | + loss_real = (weights * loss_real).sum() / weights.sum() |
| 16 | + loss_fake = (weights * loss_fake).sum() / weights.sum() |
| 17 | + d_loss = 0.5 * (loss_real + loss_fake) |
| 18 | + return d_loss |
| 19 | + |
| 20 | +def adopt_weight(weight, global_step, threshold=0, value=0.): |
| 21 | + if global_step < threshold: |
| 22 | + weight = value |
| 23 | + return weight |
| 24 | + |
| 25 | + |
| 26 | +def measure_perplexity(predicted_indices, n_embed): |
| 27 | + # src: https://github.com/karpathy/deep-vector-quantization/blob/main/model.py |
| 28 | + # eval cluster perplexity. when perplexity == num_embeddings then all clusters are used exactly equally |
| 29 | + encodings = F.one_hot(predicted_indices, n_embed).float().reshape(-1, n_embed) |
| 30 | + avg_probs = encodings.mean(0) |
| 31 | + perplexity = (-(avg_probs * torch.log(avg_probs + 1e-10)).sum()).exp() |
| 32 | + cluster_use = torch.sum(avg_probs > 0) |
| 33 | + return perplexity, cluster_use |
| 34 | + |
| 35 | +def l1(x, y): |
| 36 | + return torch.abs(x-y) |
| 37 | + |
| 38 | + |
| 39 | +def l2(x, y): |
| 40 | + return torch.pow((x-y), 2) |
| 41 | + |
| 42 | + |
| 43 | +class VQLPIPSWithDiscriminator(nn.Module): |
| 44 | + def __init__(self, disc_start, codebook_weight=1.0, pixelloss_weight=1.0, |
| 45 | + disc_num_layers=3, disc_in_channels=3, disc_factor=1.0, disc_weight=1.0, |
| 46 | + perceptual_weight=1.0, use_actnorm=False, disc_conditional=False, |
| 47 | + disc_ndf=64, disc_loss="hinge", n_classes=None, perceptual_loss="lpips", |
| 48 | + pixel_loss="l1"): |
| 49 | + super().__init__() |
| 50 | + assert disc_loss in ["hinge", "vanilla"] |
| 51 | + assert perceptual_loss in ["lpips", "clips", "dists"] |
| 52 | + assert pixel_loss in ["l1", "l2"] |
| 53 | + self.codebook_weight = codebook_weight |
| 54 | + self.pixel_weight = pixelloss_weight |
| 55 | + if perceptual_loss == "lpips": |
| 56 | + print(f"{self.__class__.__name__}: Running with LPIPS.") |
| 57 | + self.perceptual_loss = LPIPS().eval() |
| 58 | + else: |
| 59 | + raise ValueError(f"Unknown perceptual loss: >> {perceptual_loss} <<") |
| 60 | + self.perceptual_weight = perceptual_weight |
| 61 | + |
| 62 | + if pixel_loss == "l1": |
| 63 | + self.pixel_loss = l1 |
| 64 | + else: |
| 65 | + self.pixel_loss = l2 |
| 66 | + |
| 67 | + self.discriminator = NLayerDiscriminator(input_nc=disc_in_channels, |
| 68 | + n_layers=disc_num_layers, |
| 69 | + use_actnorm=use_actnorm, |
| 70 | + ndf=disc_ndf |
| 71 | + ).apply(weights_init) |
| 72 | + self.discriminator_iter_start = disc_start |
| 73 | + if disc_loss == "hinge": |
| 74 | + self.disc_loss = hinge_d_loss |
| 75 | + elif disc_loss == "vanilla": |
| 76 | + self.disc_loss = vanilla_d_loss |
| 77 | + else: |
| 78 | + raise ValueError(f"Unknown GAN loss '{disc_loss}'.") |
| 79 | + print(f"VQLPIPSWithDiscriminator running with {disc_loss} loss.") |
| 80 | + self.disc_factor = disc_factor |
| 81 | + self.discriminator_weight = disc_weight |
| 82 | + self.disc_conditional = disc_conditional |
| 83 | + self.n_classes = n_classes |
| 84 | + |
| 85 | + def calculate_adaptive_weight(self, nll_loss, g_loss, last_layer=None): |
| 86 | + if last_layer is not None: |
| 87 | + nll_grads = torch.autograd.grad(nll_loss, last_layer, retain_graph=True)[0] |
| 88 | + g_grads = torch.autograd.grad(g_loss, last_layer, retain_graph=True)[0] |
| 89 | + else: |
| 90 | + nll_grads = torch.autograd.grad(nll_loss, self.last_layer[0], retain_graph=True)[0] |
| 91 | + g_grads = torch.autograd.grad(g_loss, self.last_layer[0], retain_graph=True)[0] |
| 92 | + |
| 93 | + d_weight = torch.norm(nll_grads) / (torch.norm(g_grads) + 1e-4) |
| 94 | + d_weight = torch.clamp(d_weight, 0.0, 1e4).detach() |
| 95 | + d_weight = d_weight * self.discriminator_weight |
| 96 | + return d_weight |
| 97 | + |
| 98 | + def forward(self, codebook_loss, inputs, reconstructions, optimizer_idx, |
| 99 | + global_step, last_layer=None, cond=None, split="train", predicted_indices=None): |
| 100 | + if not exists(codebook_loss): |
| 101 | + codebook_loss = torch.tensor([0.]).to(inputs.device) |
| 102 | + #rec_loss = torch.abs(inputs.contiguous() - reconstructions.contiguous()) |
| 103 | + rec_loss = self.pixel_loss(inputs.contiguous(), reconstructions.contiguous()) |
| 104 | + if self.perceptual_weight > 0: |
| 105 | + p_loss = self.perceptual_loss(inputs.contiguous(), reconstructions.contiguous()) |
| 106 | + rec_loss = rec_loss + self.perceptual_weight * p_loss |
| 107 | + else: |
| 108 | + p_loss = torch.tensor([0.0]) |
| 109 | + |
| 110 | + nll_loss = rec_loss |
| 111 | + #nll_loss = torch.sum(nll_loss) / nll_loss.shape[0] |
| 112 | + nll_loss = torch.mean(nll_loss) |
| 113 | + |
| 114 | + # now the GAN part |
| 115 | + if optimizer_idx == 0: |
| 116 | + # generator update |
| 117 | + if cond is None: |
| 118 | + assert not self.disc_conditional |
| 119 | + logits_fake = self.discriminator(reconstructions.contiguous()) |
| 120 | + else: |
| 121 | + assert self.disc_conditional |
| 122 | + logits_fake = self.discriminator(torch.cat((reconstructions.contiguous(), cond), dim=1)) |
| 123 | + g_loss = -torch.mean(logits_fake) |
| 124 | + |
| 125 | + try: |
| 126 | + d_weight = self.calculate_adaptive_weight(nll_loss, g_loss, last_layer=last_layer) |
| 127 | + except RuntimeError: |
| 128 | + assert not self.training |
| 129 | + d_weight = torch.tensor(0.0) |
| 130 | + |
| 131 | + disc_factor = adopt_weight(self.disc_factor, global_step, threshold=self.discriminator_iter_start) |
| 132 | + loss = nll_loss + d_weight * disc_factor * g_loss + self.codebook_weight * codebook_loss.mean() |
| 133 | + |
| 134 | + log = {"{}/total_loss".format(split): loss.clone().detach().mean(), |
| 135 | + "{}/quant_loss".format(split): codebook_loss.detach().mean(), |
| 136 | + "{}/nll_loss".format(split): nll_loss.detach().mean(), |
| 137 | + "{}/rec_loss".format(split): rec_loss.detach().mean(), |
| 138 | + "{}/p_loss".format(split): p_loss.detach().mean(), |
| 139 | + "{}/d_weight".format(split): d_weight.detach(), |
| 140 | + "{}/disc_factor".format(split): torch.tensor(disc_factor), |
| 141 | + "{}/g_loss".format(split): g_loss.detach().mean(), |
| 142 | + } |
| 143 | + if predicted_indices is not None: |
| 144 | + assert self.n_classes is not None |
| 145 | + with torch.no_grad(): |
| 146 | + perplexity, cluster_usage = measure_perplexity(predicted_indices, self.n_classes) |
| 147 | + log[f"{split}/perplexity"] = perplexity |
| 148 | + log[f"{split}/cluster_usage"] = cluster_usage |
| 149 | + return loss, log |
| 150 | + |
| 151 | + if optimizer_idx == 1: |
| 152 | + # second pass for discriminator update |
| 153 | + if cond is None: |
| 154 | + logits_real = self.discriminator(inputs.contiguous().detach()) |
| 155 | + logits_fake = self.discriminator(reconstructions.contiguous().detach()) |
| 156 | + else: |
| 157 | + logits_real = self.discriminator(torch.cat((inputs.contiguous().detach(), cond), dim=1)) |
| 158 | + logits_fake = self.discriminator(torch.cat((reconstructions.contiguous().detach(), cond), dim=1)) |
| 159 | + |
| 160 | + disc_factor = adopt_weight(self.disc_factor, global_step, threshold=self.discriminator_iter_start) |
| 161 | + d_loss = disc_factor * self.disc_loss(logits_real, logits_fake) |
| 162 | + |
| 163 | + log = {"{}/disc_loss".format(split): d_loss.clone().detach().mean(), |
| 164 | + "{}/logits_real".format(split): logits_real.detach().mean(), |
| 165 | + "{}/logits_fake".format(split): logits_fake.detach().mean() |
| 166 | + } |
| 167 | + return d_loss, log |
0 commit comments