mirror of
https://github.com/wassname/denoising-diffusion-pytorch.git
synced 2026-09-10 12:01:08 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
79c5f045e1 | ||
|
|
479f60c178 | ||
|
|
96bb2ff310 |
@@ -121,3 +121,13 @@ Samples and model checkpoints will be logged to `./results` periodically
|
||||
url = {https://openreview.net/forum?id=2LdBqxc1Yv}
|
||||
}
|
||||
```
|
||||
|
||||
```bibtex
|
||||
@article{Choi2022PerceptionPT,
|
||||
title = {Perception Prioritized Training of Diffusion Models},
|
||||
author = {Jooyoung Choi and Jungbeom Lee and Chaehun Shin and Sungwon Kim and Hyunwoo J. Kim and Sung-Hoon Yoon},
|
||||
journal = {ArXiv},
|
||||
year = {2022},
|
||||
volume = {abs/2204.00227}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -59,11 +59,14 @@ class MonotonicLinear(nn.Module):
|
||||
|
||||
# log(snr) that approximates the original linear schedule
|
||||
|
||||
def beta_linear_log_snr(t):
|
||||
return -torch.log(expm1(1e-4 + 10 * (t ** 2)))
|
||||
def log(t, eps = 1e-20):
|
||||
return torch.log(t.clamp(min = eps))
|
||||
|
||||
def alpha_cosine_log_snr(t):
|
||||
raise NotImplementedError
|
||||
def beta_linear_log_snr(t):
|
||||
return -log(expm1(1e-4 + 10 * (t ** 2)))
|
||||
|
||||
def alpha_cosine_log_snr(t, s = 0.008):
|
||||
return -log((torch.cos((t + s) / (1 + s) * torch.pi * 0.5) ** -2) - 1, eps = 1e-5)
|
||||
|
||||
class learned_noise_schedule(nn.Module):
|
||||
""" described in section H and then I.2 of the supplementary material for variational ddpm paper """
|
||||
@@ -117,7 +120,9 @@ class ContinuousTimeGaussianDiffusion(nn.Module):
|
||||
num_sample_steps = 500,
|
||||
clip_sample_denoised = True,
|
||||
learned_schedule_net_hidden_dim = 1024,
|
||||
learned_noise_schedule_frac_gradient = 1. # between 0 and 1, determines what percentage of gradients go back, so one can update the learned noise schedule more slowly
|
||||
learned_noise_schedule_frac_gradient = 1., # between 0 and 1, determines what percentage of gradients go back, so one can update the learned noise schedule more slowly
|
||||
p2_loss_weight_gamma = 0., # p2 loss weight, from https://arxiv.org/abs/2204.00227 - 0 is equivalent to weight of 1 across time
|
||||
p2_loss_weight_k = 1
|
||||
):
|
||||
super().__init__()
|
||||
assert not denoise_fn.sinusoidal_cond_mlp
|
||||
@@ -135,6 +140,8 @@ class ContinuousTimeGaussianDiffusion(nn.Module):
|
||||
|
||||
if noise_schedule == 'linear':
|
||||
self.log_snr = beta_linear_log_snr
|
||||
elif noise_schedule == 'cosine':
|
||||
self.log_snr = alpha_cosine_log_snr
|
||||
elif noise_schedule == 'learned':
|
||||
log_snr_max, log_snr_min = [beta_linear_log_snr(torch.tensor([time])).item() for time in (0., 1.)]
|
||||
|
||||
@@ -152,6 +159,14 @@ class ContinuousTimeGaussianDiffusion(nn.Module):
|
||||
self.num_sample_steps = num_sample_steps
|
||||
self.clip_sample_denoised = clip_sample_denoised
|
||||
|
||||
# p2 loss weight
|
||||
# proposed https://arxiv.org/abs/2204.00227
|
||||
|
||||
assert p2_loss_weight_gamma <= 2, 'in paper, they noticed any gamma greater than 2 is harmful'
|
||||
|
||||
self.p2_loss_weight_gamma = p2_loss_weight_gamma # recommended to be 0.5 or 1
|
||||
self.p2_loss_weight_k = p2_loss_weight_k
|
||||
|
||||
@property
|
||||
def device(self):
|
||||
return next(self.denoise_fn.parameters()).device
|
||||
@@ -250,9 +265,17 @@ class ContinuousTimeGaussianDiffusion(nn.Module):
|
||||
noise = default(noise, lambda: torch.randn_like(x_start))
|
||||
|
||||
x, log_snr = self.q_sample(x_start = x_start, times = times, noise = noise)
|
||||
|
||||
model_out = self.denoise_fn(x, log_snr)
|
||||
return self.loss_fn(model_out, noise)
|
||||
|
||||
losses = self.loss_fn(model_out, noise, reduction = 'none')
|
||||
losses = losses.mean(dim = tuple(range(1, losses.ndim)))
|
||||
|
||||
if self.p2_loss_weight_gamma >= 0:
|
||||
# following eq 8. in https://arxiv.org/abs/2204.00227
|
||||
loss_weight = (self.p2_loss_weight_k + log_snr.exp()) ** -self.p2_loss_weight_gamma
|
||||
losses = losses * loss_weight
|
||||
|
||||
return losses.mean()
|
||||
|
||||
def forward(self, img, *args, **kwargs):
|
||||
b, c, h, w, device, img_size, = *img.shape, img.device, self.image_size
|
||||
|
||||
@@ -541,7 +541,7 @@ class GaussianDiffusion(nn.Module):
|
||||
# dataset classes
|
||||
|
||||
class Dataset(data.Dataset):
|
||||
def __init__(self, folder, image_size, exts = ['jpg', 'jpeg', 'png']):
|
||||
def __init__(self, folder, image_size, exts = ['jpg', 'jpeg', 'png'], augment_horizontal_flip = False):
|
||||
super().__init__()
|
||||
self.folder = folder
|
||||
self.image_size = image_size
|
||||
@@ -549,7 +549,7 @@ class Dataset(data.Dataset):
|
||||
|
||||
self.transform = transforms.Compose([
|
||||
transforms.Resize(image_size),
|
||||
transforms.RandomHorizontalFlip(),
|
||||
transforms.RandomHorizontalFlip() if augment_horizontal_flip else nn.Identity(),
|
||||
transforms.CenterCrop(image_size),
|
||||
transforms.ToTensor()
|
||||
])
|
||||
@@ -580,7 +580,8 @@ class Trainer(object):
|
||||
step_start_ema = 2000,
|
||||
update_ema_every = 10,
|
||||
save_and_sample_every = 1000,
|
||||
results_folder = './results'
|
||||
results_folder = './results',
|
||||
augment_horizontal_flip = True
|
||||
):
|
||||
super().__init__()
|
||||
self.model = diffusion_model
|
||||
@@ -596,7 +597,7 @@ class Trainer(object):
|
||||
self.gradient_accumulate_every = gradient_accumulate_every
|
||||
self.train_num_steps = train_num_steps
|
||||
|
||||
self.ds = Dataset(folder, image_size)
|
||||
self.ds = Dataset(folder, image_size, augment_horizontal_flip = augment_horizontal_flip)
|
||||
self.dl = cycle(data.DataLoader(self.ds, batch_size = train_batch_size, shuffle=True, pin_memory=True))
|
||||
self.opt = Adam(diffusion_model.parameters(), lr=train_lr)
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ from setuptools import setup, find_packages
|
||||
setup(
|
||||
name = 'denoising-diffusion-pytorch',
|
||||
packages = find_packages(),
|
||||
version = '0.17.6',
|
||||
version = '0.18.1',
|
||||
license='MIT',
|
||||
description = 'Denoising Diffusion Probabilistic Models - Pytorch',
|
||||
author = 'Phil Wang',
|
||||
|
||||
Reference in New Issue
Block a user