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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Any, Union
|
2 |
+
|
3 |
+
import torch
|
4 |
+
import torch.nn as nn
|
5 |
+
from einops import rearrange
|
6 |
+
|
7 |
+
from ....util import default, instantiate_from_config
|
8 |
+
from ..lpips.loss.lpips import LPIPS
|
9 |
+
from ..lpips.model.model import NLayerDiscriminator, weights_init
|
10 |
+
from ..lpips.vqperceptual import hinge_d_loss, vanilla_d_loss
|
11 |
+
|
12 |
+
|
13 |
+
def adopt_weight(weight, global_step, threshold=0, value=0.0):
|
14 |
+
if global_step < threshold:
|
15 |
+
weight = value
|
16 |
+
return weight
|
17 |
+
|
18 |
+
|
19 |
+
class LatentLPIPS(nn.Module):
|
20 |
+
def __init__(
|
21 |
+
self,
|
22 |
+
decoder_config,
|
23 |
+
perceptual_weight=1.0,
|
24 |
+
latent_weight=1.0,
|
25 |
+
scale_input_to_tgt_size=False,
|
26 |
+
scale_tgt_to_input_size=False,
|
27 |
+
perceptual_weight_on_inputs=0.0,
|
28 |
+
):
|
29 |
+
super().__init__()
|
30 |
+
self.scale_input_to_tgt_size = scale_input_to_tgt_size
|
31 |
+
self.scale_tgt_to_input_size = scale_tgt_to_input_size
|
32 |
+
self.init_decoder(decoder_config)
|
33 |
+
self.perceptual_loss = LPIPS().eval()
|
34 |
+
self.perceptual_weight = perceptual_weight
|
35 |
+
self.latent_weight = latent_weight
|
36 |
+
self.perceptual_weight_on_inputs = perceptual_weight_on_inputs
|
37 |
+
|
38 |
+
def init_decoder(self, config):
|
39 |
+
self.decoder = instantiate_from_config(config)
|
40 |
+
if hasattr(self.decoder, "encoder"):
|
41 |
+
del self.decoder.encoder
|
42 |
+
|
43 |
+
def forward(self, latent_inputs, latent_predictions, image_inputs, split="train"):
|
44 |
+
log = dict()
|
45 |
+
loss = (latent_inputs - latent_predictions) ** 2
|
46 |
+
log[f"{split}/latent_l2_loss"] = loss.mean().detach()
|
47 |
+
image_reconstructions = None
|
48 |
+
if self.perceptual_weight > 0.0:
|
49 |
+
image_reconstructions = self.decoder.decode(latent_predictions)
|
50 |
+
image_targets = self.decoder.decode(latent_inputs)
|
51 |
+
perceptual_loss = self.perceptual_loss(
|
52 |
+
image_targets.contiguous(), image_reconstructions.contiguous()
|
53 |
+
)
|
54 |
+
loss = (
|
55 |
+
self.latent_weight * loss.mean()
|
56 |
+
+ self.perceptual_weight * perceptual_loss.mean()
|
57 |
+
)
|
58 |
+
log[f"{split}/perceptual_loss"] = perceptual_loss.mean().detach()
|
59 |
+
|
60 |
+
if self.perceptual_weight_on_inputs > 0.0:
|
61 |
+
image_reconstructions = default(
|
62 |
+
image_reconstructions, self.decoder.decode(latent_predictions)
|
63 |
+
)
|
64 |
+
if self.scale_input_to_tgt_size:
|
65 |
+
image_inputs = torch.nn.functional.interpolate(
|
66 |
+
image_inputs,
|
67 |
+
image_reconstructions.shape[2:],
|
68 |
+
mode="bicubic",
|
69 |
+
antialias=True,
|
70 |
+
)
|
71 |
+
elif self.scale_tgt_to_input_size:
|
72 |
+
image_reconstructions = torch.nn.functional.interpolate(
|
73 |
+
image_reconstructions,
|
74 |
+
image_inputs.shape[2:],
|
75 |
+
mode="bicubic",
|
76 |
+
antialias=True,
|
77 |
+
)
|
78 |
+
|
79 |
+
perceptual_loss2 = self.perceptual_loss(
|
80 |
+
image_inputs.contiguous(), image_reconstructions.contiguous()
|
81 |
+
)
|
82 |
+
loss = loss + self.perceptual_weight_on_inputs * perceptual_loss2.mean()
|
83 |
+
log[f"{split}/perceptual_loss_on_inputs"] = perceptual_loss2.mean().detach()
|
84 |
+
return loss, log
|
85 |
+
|
86 |
+
|
87 |
+
class GeneralLPIPSWithDiscriminator(nn.Module):
|
88 |
+
def __init__(
|
89 |
+
self,
|
90 |
+
disc_start: int,
|
91 |
+
logvar_init: float = 0.0,
|
92 |
+
pixelloss_weight=1.0,
|
93 |
+
disc_num_layers: int = 3,
|
94 |
+
disc_in_channels: int = 3,
|
95 |
+
disc_factor: float = 1.0,
|
96 |
+
disc_weight: float = 1.0,
|
97 |
+
perceptual_weight: float = 1.0,
|
98 |
+
disc_loss: str = "hinge",
|
99 |
+
scale_input_to_tgt_size: bool = False,
|
100 |
+
dims: int = 2,
|
101 |
+
learn_logvar: bool = False,
|
102 |
+
regularization_weights: Union[None, dict] = None,
|
103 |
+
):
|
104 |
+
super().__init__()
|
105 |
+
self.dims = dims
|
106 |
+
if self.dims > 2:
|
107 |
+
print(
|
108 |
+
f"running with dims={dims}. This means that for perceptual loss calculation, "
|
109 |
+
f"the LPIPS loss will be applied to each frame independently. "
|
110 |
+
)
|
111 |
+
self.scale_input_to_tgt_size = scale_input_to_tgt_size
|
112 |
+
assert disc_loss in ["hinge", "vanilla"]
|
113 |
+
self.pixel_weight = pixelloss_weight
|
114 |
+
self.perceptual_loss = LPIPS().eval()
|
115 |
+
self.perceptual_weight = perceptual_weight
|
116 |
+
# output log variance
|
117 |
+
self.logvar = nn.Parameter(torch.ones(size=()) * logvar_init)
|
118 |
+
self.learn_logvar = learn_logvar
|
119 |
+
|
120 |
+
self.discriminator = NLayerDiscriminator(
|
121 |
+
input_nc=disc_in_channels, n_layers=disc_num_layers, use_actnorm=False
|
122 |
+
).apply(weights_init)
|
123 |
+
self.discriminator_iter_start = disc_start
|
124 |
+
self.disc_loss = hinge_d_loss if disc_loss == "hinge" else vanilla_d_loss
|
125 |
+
self.disc_factor = disc_factor
|
126 |
+
self.discriminator_weight = disc_weight
|
127 |
+
self.regularization_weights = default(regularization_weights, {})
|
128 |
+
|
129 |
+
def get_trainable_parameters(self) -> Any:
|
130 |
+
return self.discriminator.parameters()
|
131 |
+
|
132 |
+
def get_trainable_autoencoder_parameters(self) -> Any:
|
133 |
+
if self.learn_logvar:
|
134 |
+
yield self.logvar
|
135 |
+
yield from ()
|
136 |
+
|
137 |
+
def calculate_adaptive_weight(self, nll_loss, g_loss, last_layer=None):
|
138 |
+
if last_layer is not None:
|
139 |
+
nll_grads = torch.autograd.grad(nll_loss, last_layer, retain_graph=True)[0]
|
140 |
+
g_grads = torch.autograd.grad(g_loss, last_layer, retain_graph=True)[0]
|
141 |
+
else:
|
142 |
+
nll_grads = torch.autograd.grad(
|
143 |
+
nll_loss, self.last_layer[0], retain_graph=True
|
144 |
+
)[0]
|
145 |
+
g_grads = torch.autograd.grad(
|
146 |
+
g_loss, self.last_layer[0], retain_graph=True
|
147 |
+
)[0]
|
148 |
+
|
149 |
+
d_weight = torch.norm(nll_grads) / (torch.norm(g_grads) + 1e-4)
|
150 |
+
d_weight = torch.clamp(d_weight, 0.0, 1e4).detach()
|
151 |
+
d_weight = d_weight * self.discriminator_weight
|
152 |
+
return d_weight
|
153 |
+
|
154 |
+
def forward(
|
155 |
+
self,
|
156 |
+
regularization_log,
|
157 |
+
inputs,
|
158 |
+
reconstructions,
|
159 |
+
optimizer_idx,
|
160 |
+
global_step,
|
161 |
+
last_layer=None,
|
162 |
+
split="train",
|
163 |
+
weights=None,
|
164 |
+
):
|
165 |
+
if self.scale_input_to_tgt_size:
|
166 |
+
inputs = torch.nn.functional.interpolate(
|
167 |
+
inputs, reconstructions.shape[2:], mode="bicubic", antialias=True
|
168 |
+
)
|
169 |
+
|
170 |
+
if self.dims > 2:
|
171 |
+
inputs, reconstructions = map(
|
172 |
+
lambda x: rearrange(x, "b c t h w -> (b t) c h w"),
|
173 |
+
(inputs, reconstructions),
|
174 |
+
)
|
175 |
+
|
176 |
+
rec_loss = torch.abs(inputs.contiguous() - reconstructions.contiguous())
|
177 |
+
if self.perceptual_weight > 0:
|
178 |
+
p_loss = self.perceptual_loss(
|
179 |
+
inputs.contiguous(), reconstructions.contiguous()
|
180 |
+
)
|
181 |
+
rec_loss = rec_loss + self.perceptual_weight * p_loss
|
182 |
+
|
183 |
+
nll_loss = rec_loss / torch.exp(self.logvar) + self.logvar
|
184 |
+
weighted_nll_loss = nll_loss
|
185 |
+
if weights is not None:
|
186 |
+
weighted_nll_loss = weights * nll_loss
|
187 |
+
weighted_nll_loss = torch.sum(weighted_nll_loss) / weighted_nll_loss.shape[0]
|
188 |
+
nll_loss = torch.sum(nll_loss) / nll_loss.shape[0]
|
189 |
+
|
190 |
+
# now the GAN part
|
191 |
+
if optimizer_idx == 0:
|
192 |
+
# generator update
|
193 |
+
logits_fake = self.discriminator(reconstructions.contiguous())
|
194 |
+
g_loss = -torch.mean(logits_fake)
|
195 |
+
|
196 |
+
if self.disc_factor > 0.0:
|
197 |
+
try:
|
198 |
+
d_weight = self.calculate_adaptive_weight(
|
199 |
+
nll_loss, g_loss, last_layer=last_layer
|
200 |
+
)
|
201 |
+
except RuntimeError:
|
202 |
+
assert not self.training
|
203 |
+
d_weight = torch.tensor(0.0)
|
204 |
+
else:
|
205 |
+
d_weight = torch.tensor(0.0)
|
206 |
+
|
207 |
+
disc_factor = adopt_weight(
|
208 |
+
self.disc_factor, global_step, threshold=self.discriminator_iter_start
|
209 |
+
)
|
210 |
+
loss = weighted_nll_loss + d_weight * disc_factor * g_loss
|
211 |
+
log = dict()
|
212 |
+
for k in regularization_log:
|
213 |
+
if k in self.regularization_weights:
|
214 |
+
loss = loss + self.regularization_weights[k] * regularization_log[k]
|
215 |
+
log[f"{split}/{k}"] = regularization_log[k].detach().mean()
|
216 |
+
|
217 |
+
log.update(
|
218 |
+
{
|
219 |
+
"{}/total_loss".format(split): loss.clone().detach().mean(),
|
220 |
+
"{}/logvar".format(split): self.logvar.detach(),
|
221 |
+
"{}/nll_loss".format(split): nll_loss.detach().mean(),
|
222 |
+
"{}/rec_loss".format(split): rec_loss.detach().mean(),
|
223 |
+
"{}/d_weight".format(split): d_weight.detach(),
|
224 |
+
"{}/disc_factor".format(split): torch.tensor(disc_factor),
|
225 |
+
"{}/g_loss".format(split): g_loss.detach().mean(),
|
226 |
+
}
|
227 |
+
)
|
228 |
+
|
229 |
+
return loss, log
|
230 |
+
|
231 |
+
if optimizer_idx == 1:
|
232 |
+
# second pass for discriminator update
|
233 |
+
logits_real = self.discriminator(inputs.contiguous().detach())
|
234 |
+
logits_fake = self.discriminator(reconstructions.contiguous().detach())
|
235 |
+
|
236 |
+
disc_factor = adopt_weight(
|
237 |
+
self.disc_factor, global_step, threshold=self.discriminator_iter_start
|
238 |
+
)
|
239 |
+
d_loss = disc_factor * self.disc_loss(logits_real, logits_fake)
|
240 |
+
|
241 |
+
log = {
|
242 |
+
"{}/disc_loss".format(split): d_loss.clone().detach().mean(),
|
243 |
+
"{}/logits_real".format(split): logits_real.detach().mean(),
|
244 |
+
"{}/logits_fake".format(split): logits_fake.detach().mean(),
|
245 |
+
}
|
246 |
+
return d_loss, log
|