|
|
|
@@ -76,25 +76,6 @@ class LearnedGaussianDiffusion(GaussianDiffusion):
|
|
|
|
|
assert denoise_fn.out_dim == (denoise_fn.channels * 2), 'dimension out of unet must be twice the number of channels for learned variance - you can also set the `learned_variance` keyword argument on the Unet to be `True`'
|
|
|
|
|
self.vb_loss_weight = vb_loss_weight
|
|
|
|
|
|
|
|
|
|
def q_posterior_mean_variance(self, x_start, x_t, t):
|
|
|
|
|
"""
|
|
|
|
|
Compute the mean and variance of the diffusion posterior q(x_{t-1} | x_t, x_0)
|
|
|
|
|
"""
|
|
|
|
|
posterior_mean = (
|
|
|
|
|
extract(self.posterior_mean_coef1, t, x_t.shape) * x_start +
|
|
|
|
|
extract(self.posterior_mean_coef2, t, x_t.shape) * x_t
|
|
|
|
|
)
|
|
|
|
|
posterior_variance = extract(self.posterior_variance, t, x_t.shape)
|
|
|
|
|
posterior_log_variance_clipped = extract(self.posterior_log_variance_clipped, t, x_t.shape)
|
|
|
|
|
return posterior_mean, posterior_variance, posterior_log_variance_clipped
|
|
|
|
|
|
|
|
|
|
def predict_xstart_from_xprev(self, x_t, t, xprev):
|
|
|
|
|
# (xprev - coef2*x_t) / coef1
|
|
|
|
|
return (
|
|
|
|
|
extract(1. / self.posterior_mean_coef1, t, x_t.shape) * xprev -
|
|
|
|
|
extract(self.posterior_mean_coef2 / self.posterior_mean_coef1, t, x_t.shape) * x_t
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def p_mean_variance(self, *, x, t, clip_denoised, model_output = None):
|
|
|
|
|
model_output = default(model_output, lambda: self.denoise_fn(x, t))
|
|
|
|
|
pred_noise, var_interp_frac_unnormalized = model_output.chunk(2, dim = 1)
|
|
|
|
@@ -107,6 +88,10 @@ class LearnedGaussianDiffusion(GaussianDiffusion):
|
|
|
|
|
model_variance = model_log_variance.exp()
|
|
|
|
|
|
|
|
|
|
x_start = self.predict_start_from_noise(x, t, pred_noise)
|
|
|
|
|
|
|
|
|
|
if clip_denoised:
|
|
|
|
|
x_start.clamp_(-1., 1.)
|
|
|
|
|
|
|
|
|
|
model_mean, _, _ = self.q_posterior(x_start, x, t)
|
|
|
|
|
|
|
|
|
|
return model_mean, model_variance, model_log_variance
|
|
|
|
@@ -121,15 +106,17 @@ class LearnedGaussianDiffusion(GaussianDiffusion):
|
|
|
|
|
|
|
|
|
|
# calculating kl loss for learned variance (interpolation)
|
|
|
|
|
|
|
|
|
|
true_mean, _, true_log_variance_clipped = self.q_posterior_mean_variance(x_start = x_start, x_t = x_t, t = t)
|
|
|
|
|
true_mean, _, true_log_variance_clipped = self.q_posterior(x_start = x_start, x_t = x_t, t = t)
|
|
|
|
|
model_mean, _, model_log_variance = self.p_mean_variance(x = x_t, t = t, clip_denoised = clip_denoised, model_output = model_output)
|
|
|
|
|
|
|
|
|
|
# kl loss with detached model predicted mean, for stability reasons as in paper
|
|
|
|
|
|
|
|
|
|
kl = normal_kl(true_mean, true_log_variance_clipped, model_mean.detach(), model_log_variance)
|
|
|
|
|
detached_model_mean = model_mean.detach()
|
|
|
|
|
|
|
|
|
|
kl = normal_kl(true_mean, true_log_variance_clipped, detached_model_mean, model_log_variance)
|
|
|
|
|
kl = meanflat(kl) * NAT
|
|
|
|
|
|
|
|
|
|
decoder_nll = -discretized_gaussian_log_likelihood(x_start, means = model_mean, log_scales = 0.5 * model_log_variance)
|
|
|
|
|
decoder_nll = -discretized_gaussian_log_likelihood(x_start, means = detached_model_mean, log_scales = 0.5 * model_log_variance)
|
|
|
|
|
decoder_nll = meanflat(decoder_nll) * NAT
|
|
|
|
|
|
|
|
|
|
# at the first timestep return the decoder NLL, otherwise return KL(q(x_{t-1}|x_t,x_0) || p(x_{t-1}|x_t))
|
|
|
|
|