mirror of
https://github.com/wassname/denoising-diffusion-pytorch.git
synced 2026-09-10 12:01:08 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
84ebb9ad13 | ||
|
|
caa5af170d | ||
|
|
55c658b967 | ||
|
|
e0f26677d6 |
@@ -339,7 +339,8 @@ class GaussianDiffusion(nn.Module):
|
|||||||
image_size,
|
image_size,
|
||||||
channels = 3,
|
channels = 3,
|
||||||
timesteps = 1000,
|
timesteps = 1000,
|
||||||
loss_type = 'l1'
|
loss_type = 'l1',
|
||||||
|
objective = 'pred_noise'
|
||||||
):
|
):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
assert not (type(self) == GaussianDiffusion and denoise_fn.channels != denoise_fn.out_dim)
|
assert not (type(self) == GaussianDiffusion and denoise_fn.channels != denoise_fn.out_dim)
|
||||||
@@ -347,6 +348,7 @@ class GaussianDiffusion(nn.Module):
|
|||||||
self.channels = channels
|
self.channels = channels
|
||||||
self.image_size = image_size
|
self.image_size = image_size
|
||||||
self.denoise_fn = denoise_fn
|
self.denoise_fn = denoise_fn
|
||||||
|
self.objective = objective
|
||||||
|
|
||||||
betas = cosine_beta_schedule(timesteps)
|
betas = cosine_beta_schedule(timesteps)
|
||||||
|
|
||||||
@@ -388,12 +390,6 @@ class GaussianDiffusion(nn.Module):
|
|||||||
register_buffer('posterior_mean_coef1', betas * torch.sqrt(alphas_cumprod_prev) / (1. - alphas_cumprod))
|
register_buffer('posterior_mean_coef1', betas * torch.sqrt(alphas_cumprod_prev) / (1. - alphas_cumprod))
|
||||||
register_buffer('posterior_mean_coef2', (1. - alphas_cumprod_prev) * torch.sqrt(alphas) / (1. - alphas_cumprod))
|
register_buffer('posterior_mean_coef2', (1. - alphas_cumprod_prev) * torch.sqrt(alphas) / (1. - alphas_cumprod))
|
||||||
|
|
||||||
def q_mean_variance(self, x_start, t):
|
|
||||||
mean = extract(self.sqrt_alphas_cumprod, t, x_start.shape) * x_start
|
|
||||||
variance = extract(1. - self.alphas_cumprod, t, x_start.shape)
|
|
||||||
log_variance = extract(self.log_one_minus_alphas_cumprod, t, x_start.shape)
|
|
||||||
return mean, variance, log_variance
|
|
||||||
|
|
||||||
def predict_start_from_noise(self, x_t, t, noise):
|
def predict_start_from_noise(self, x_t, t, noise):
|
||||||
return (
|
return (
|
||||||
extract(self.sqrt_recip_alphas_cumprod, t, x_t.shape) * x_t -
|
extract(self.sqrt_recip_alphas_cumprod, t, x_t.shape) * x_t -
|
||||||
@@ -410,12 +406,19 @@ class GaussianDiffusion(nn.Module):
|
|||||||
return posterior_mean, posterior_variance, posterior_log_variance_clipped
|
return posterior_mean, posterior_variance, posterior_log_variance_clipped
|
||||||
|
|
||||||
def p_mean_variance(self, x, t, clip_denoised: bool):
|
def p_mean_variance(self, x, t, clip_denoised: bool):
|
||||||
x_recon = self.predict_start_from_noise(x, t=t, noise=self.denoise_fn(x, t))
|
model_output = self.denoise_fn(x, t)
|
||||||
|
|
||||||
|
if self.objective == 'pred_noise':
|
||||||
|
x_start = self.predict_start_from_noise(x, t = t, noise = model_output)
|
||||||
|
elif self.objective == 'pred_x0':
|
||||||
|
x_start = model_output
|
||||||
|
else:
|
||||||
|
raise ValueError(f'unknown objective {self.objective}')
|
||||||
|
|
||||||
if clip_denoised:
|
if clip_denoised:
|
||||||
x_recon.clamp_(-1., 1.)
|
x_start.clamp_(-1., 1.)
|
||||||
|
|
||||||
model_mean, posterior_variance, posterior_log_variance = self.q_posterior(x_start=x_recon, x_t=x, t=t)
|
model_mean, posterior_variance, posterior_log_variance = self.q_posterior(x_start = x_start, x_t = x, t = t)
|
||||||
return model_mean, posterior_variance, posterior_log_variance
|
return model_mean, posterior_variance, posterior_log_variance
|
||||||
|
|
||||||
@torch.no_grad()
|
@torch.no_grad()
|
||||||
@@ -481,10 +484,17 @@ class GaussianDiffusion(nn.Module):
|
|||||||
b, c, h, w = x_start.shape
|
b, c, h, w = x_start.shape
|
||||||
noise = default(noise, lambda: torch.randn_like(x_start))
|
noise = default(noise, lambda: torch.randn_like(x_start))
|
||||||
|
|
||||||
x_noisy = self.q_sample(x_start=x_start, t=t, noise=noise)
|
x = self.q_sample(x_start=x_start, t=t, noise=noise)
|
||||||
x_recon = self.denoise_fn(x_noisy, t)
|
model_out = self.denoise_fn(x, t)
|
||||||
|
|
||||||
loss = self.loss_fn(noise, x_recon)
|
if self.objective == 'pred_noise':
|
||||||
|
target = noise
|
||||||
|
elif self.objective == 'pred_x0':
|
||||||
|
target = x_start
|
||||||
|
else:
|
||||||
|
raise ValueError(f'unknown objective {self.objective}')
|
||||||
|
|
||||||
|
loss = self.loss_fn(model_out, target)
|
||||||
return loss
|
return loss
|
||||||
|
|
||||||
def forward(self, x, *args, **kwargs):
|
def forward(self, x, *args, **kwargs):
|
||||||
|
|||||||
@@ -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`'
|
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
|
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):
|
def p_mean_variance(self, *, x, t, clip_denoised, model_output = None):
|
||||||
model_output = default(model_output, lambda: self.denoise_fn(x, t))
|
model_output = default(model_output, lambda: self.denoise_fn(x, t))
|
||||||
pred_noise, var_interp_frac_unnormalized = model_output.chunk(2, dim = 1)
|
pred_noise, var_interp_frac_unnormalized = model_output.chunk(2, dim = 1)
|
||||||
@@ -125,15 +106,17 @@ class LearnedGaussianDiffusion(GaussianDiffusion):
|
|||||||
|
|
||||||
# calculating kl loss for learned variance (interpolation)
|
# 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)
|
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 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
|
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
|
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))
|
# 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))
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ from setuptools import setup, find_packages
|
|||||||
setup(
|
setup(
|
||||||
name = 'denoising-diffusion-pytorch',
|
name = 'denoising-diffusion-pytorch',
|
||||||
packages = find_packages(),
|
packages = find_packages(),
|
||||||
version = '0.14.1',
|
version = '0.15.0',
|
||||||
license='MIT',
|
license='MIT',
|
||||||
description = 'Denoising Diffusion Probabilistic Models - Pytorch',
|
description = 'Denoising Diffusion Probabilistic Models - Pytorch',
|
||||||
author = 'Phil Wang',
|
author = 'Phil Wang',
|
||||||
|
|||||||
Reference in New Issue
Block a user