mirror of
https://github.com/wassname/denoising-diffusion-pytorch.git
synced 2026-09-10 12:01:08 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1b85379d3a | ||
|
|
8859864f63 | ||
|
|
32657f035f | ||
|
|
d97bc0278c | ||
|
|
8408775cfc | ||
|
|
86fcb6785b | ||
|
|
c535d31fc5 | ||
|
|
5db64fec4b | ||
|
|
b87ea27781 | ||
|
|
a8403b83fe | ||
|
|
f4b1d7a67c | ||
|
|
618493714f | ||
|
|
76b79aa847 | ||
|
|
be2bd8d320 | ||
|
|
c3d1607019 | ||
|
|
06b2e52645 | ||
|
|
09b8a1c805 | ||
|
|
d26acbcae6 | ||
|
|
9939a48139 | ||
|
|
75ea49a7ef | ||
|
|
8c3609a6e3 | ||
|
|
1586d1a8a0 | ||
|
|
b4fb8804d2 | ||
|
|
9fd05f1b1f | ||
|
|
ec2397f0ba | ||
|
|
844e557dfb | ||
|
|
8b30be8042 | ||
|
|
f2f3994b92 | ||
|
|
8ec4ea56a5 | ||
|
|
99cf9b5b96 | ||
|
|
ecc6f30901 | ||
|
|
f900f40f14 |
@@ -6,6 +6,8 @@ Implementation of <a href="https://arxiv.org/abs/2006.11239">Denoising Diffusion
|
||||
|
||||
This implementation was transcribed from the official Tensorflow version <a href="https://github.com/hojonathanho/diffusion">here</a>
|
||||
|
||||
Youtube AI Educators - <a href="https://www.youtube.com/watch?v=W-O7AZNzbzQ">Yannic Kilcher</a> | <a href="https://www.youtube.com/watch?v=344w5h24-h8">AI Coffeebreak with Letitia</a> | <a href="https://www.youtube.com/watch?v=HoKDTa5jHvg">Outlier</a>
|
||||
|
||||
<a href="https://huggingface.co/blog/annotated-diffusion">Annotated code</a> by Research Scientists / Engineers from <a href="https://huggingface.co/">🤗 Huggingface</a>
|
||||
|
||||
<img src="./sample.png" width="500px"><img>
|
||||
@@ -131,3 +133,13 @@ Samples and model checkpoints will be logged to `./results` periodically
|
||||
volume = {abs/2204.00227}
|
||||
}
|
||||
```
|
||||
|
||||
```bibtex
|
||||
@article{Karras2022ElucidatingTD,
|
||||
title = {Elucidating the Design Space of Diffusion-Based Generative Models},
|
||||
author = {Tero Karras and Miika Aittala and Timo Aila and Samuli Laine},
|
||||
journal = {ArXiv},
|
||||
year = {2022},
|
||||
volume = {abs/2206.00364}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -3,3 +3,4 @@ from denoising_diffusion_pytorch.denoising_diffusion_pytorch import GaussianDiff
|
||||
from denoising_diffusion_pytorch.learned_gaussian_diffusion import LearnedGaussianDiffusion
|
||||
from denoising_diffusion_pytorch.continuous_time_gaussian_diffusion import ContinuousTimeGaussianDiffusion
|
||||
from denoising_diffusion_pytorch.weighted_objective_gaussian_diffusion import WeightedObjectiveGaussianDiffusion
|
||||
from denoising_diffusion_pytorch.elucidated_diffusion import ElucidatedDiffusion
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import math
|
||||
import torch
|
||||
from torch import sqrt
|
||||
from torch import nn, einsum
|
||||
@@ -5,7 +6,7 @@ import torch.nn.functional as F
|
||||
from torch.special import expm1
|
||||
|
||||
from tqdm import tqdm
|
||||
from einops import rearrange, repeat
|
||||
from einops import rearrange, repeat, reduce
|
||||
from einops.layers.torch import Rearrange
|
||||
|
||||
# helpers
|
||||
@@ -66,7 +67,7 @@ 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)
|
||||
return -log((torch.cos((t + s) / (1 + s) * math.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 """
|
||||
@@ -125,7 +126,7 @@ class ContinuousTimeGaussianDiffusion(nn.Module):
|
||||
p2_loss_weight_k = 1
|
||||
):
|
||||
super().__init__()
|
||||
assert not denoise_fn.sinusoidal_cond_mlp
|
||||
assert denoise_fn.learned_sinusoidal_cond
|
||||
|
||||
self.denoise_fn = denoise_fn
|
||||
|
||||
@@ -268,7 +269,7 @@ class ContinuousTimeGaussianDiffusion(nn.Module):
|
||||
model_out = self.denoise_fn(x, log_snr)
|
||||
|
||||
losses = self.loss_fn(model_out, noise, reduction = 'none')
|
||||
losses = losses.mean(dim = tuple(range(1, losses.ndim)))
|
||||
losses = reduce(losses, 'b ... -> b', 'mean')
|
||||
|
||||
if self.p2_loss_weight_gamma >= 0:
|
||||
# following eq 8. in https://arxiv.org/abs/2204.00227
|
||||
|
||||
@@ -7,6 +7,7 @@ from inspect import isfunction
|
||||
from functools import partial
|
||||
|
||||
from torch.utils import data
|
||||
from multiprocessing import cpu_count
|
||||
from torch.cuda.amp import autocast, GradScaler
|
||||
|
||||
from pathlib import Path
|
||||
@@ -14,10 +15,17 @@ from torch.optim import Adam
|
||||
from torchvision import transforms, utils
|
||||
from PIL import Image
|
||||
|
||||
from tqdm import tqdm
|
||||
from einops import rearrange
|
||||
from einops import rearrange, reduce
|
||||
from einops.layers.torch import Rearrange
|
||||
|
||||
from ema_pytorch import EMA
|
||||
|
||||
import sys
|
||||
if 'ipykernel' in sys.modules:
|
||||
from tqdm.notebook import tqdm
|
||||
else:
|
||||
from tqdm import tqdm
|
||||
|
||||
# helpers functions
|
||||
|
||||
def exists(x):
|
||||
@@ -49,21 +57,6 @@ def unnormalize_to_zero_to_one(t):
|
||||
|
||||
# small helper modules
|
||||
|
||||
class EMA():
|
||||
def __init__(self, beta):
|
||||
super().__init__()
|
||||
self.beta = beta
|
||||
|
||||
def update_model_average(self, ma_model, current_model):
|
||||
for current_params, ma_params in zip(current_model.parameters(), ma_model.parameters()):
|
||||
old_weight, up_weight = ma_params.data, current_params.data
|
||||
ma_params.data = self.update_average(old_weight, up_weight)
|
||||
|
||||
def update_average(self, old, new):
|
||||
if old is None:
|
||||
return new
|
||||
return old * self.beta + (1 - self.beta) * new
|
||||
|
||||
class Residual(nn.Module):
|
||||
def __init__(self, fn):
|
||||
super().__init__()
|
||||
@@ -72,25 +65,14 @@ class Residual(nn.Module):
|
||||
def forward(self, x, *args, **kwargs):
|
||||
return self.fn(x, *args, **kwargs) + x
|
||||
|
||||
class SinusoidalPosEmb(nn.Module):
|
||||
def __init__(self, dim):
|
||||
super().__init__()
|
||||
self.dim = dim
|
||||
def Upsample(dim, dim_out = None):
|
||||
return nn.Sequential(
|
||||
nn.Upsample(scale_factor = 2, mode = 'nearest'),
|
||||
nn.Conv2d(dim, default(dim_out, dim), 3, padding = 1)
|
||||
)
|
||||
|
||||
def forward(self, x):
|
||||
device = x.device
|
||||
half_dim = self.dim // 2
|
||||
emb = math.log(10000) / (half_dim - 1)
|
||||
emb = torch.exp(torch.arange(half_dim, device=device) * -emb)
|
||||
emb = x[:, None] * emb[None, :]
|
||||
emb = torch.cat((emb.sin(), emb.cos()), dim=-1)
|
||||
return emb
|
||||
|
||||
def Upsample(dim):
|
||||
return nn.ConvTranspose2d(dim, dim, 4, 2, 1)
|
||||
|
||||
def Downsample(dim):
|
||||
return nn.Conv2d(dim, dim, 4, 2, 1)
|
||||
def Downsample(dim, dim_out = None):
|
||||
return nn.Conv2d(dim, default(dim_out, dim), 4, 2, 1)
|
||||
|
||||
class LayerNorm(nn.Module):
|
||||
def __init__(self, dim, eps = 1e-5):
|
||||
@@ -114,6 +96,39 @@ class PreNorm(nn.Module):
|
||||
x = self.norm(x)
|
||||
return self.fn(x)
|
||||
|
||||
# sinusoidal positional embeds
|
||||
|
||||
class SinusoidalPosEmb(nn.Module):
|
||||
def __init__(self, dim):
|
||||
super().__init__()
|
||||
self.dim = dim
|
||||
|
||||
def forward(self, x):
|
||||
device = x.device
|
||||
half_dim = self.dim // 2
|
||||
emb = math.log(10000) / (half_dim - 1)
|
||||
emb = torch.exp(torch.arange(half_dim, device=device) * -emb)
|
||||
emb = x[:, None] * emb[None, :]
|
||||
emb = torch.cat((emb.sin(), emb.cos()), dim=-1)
|
||||
return emb
|
||||
|
||||
class LearnedSinusoidalPosEmb(nn.Module):
|
||||
""" following @crowsonkb 's lead with learned sinusoidal pos emb """
|
||||
""" https://github.com/crowsonkb/v-diffusion-jax/blob/master/diffusion/models/danbooru_128.py#L8 """
|
||||
|
||||
def __init__(self, dim):
|
||||
super().__init__()
|
||||
assert (dim % 2) == 0
|
||||
half_dim = dim // 2
|
||||
self.weights = nn.Parameter(torch.randn(half_dim))
|
||||
|
||||
def forward(self, x):
|
||||
x = rearrange(x, 'b -> b 1')
|
||||
freqs = x * rearrange(self.weights, 'd -> 1 d') * 2 * math.pi
|
||||
fouriered = torch.cat((freqs.sin(), freqs.cos()), dim = -1)
|
||||
fouriered = torch.cat((x, fouriered), dim = -1)
|
||||
return fouriered
|
||||
|
||||
# building block modules
|
||||
|
||||
class Block(nn.Module):
|
||||
@@ -157,6 +172,7 @@ class ResnetBlock(nn.Module):
|
||||
h = self.block1(x, scale_shift = scale_shift)
|
||||
|
||||
h = self.block2(h)
|
||||
|
||||
return h + self.res_conv(x)
|
||||
|
||||
class LinearAttention(nn.Module):
|
||||
@@ -212,18 +228,6 @@ class Attention(nn.Module):
|
||||
|
||||
# model
|
||||
|
||||
def MLP(dim_in, dim_hidden):
|
||||
return nn.Sequential(
|
||||
Rearrange('... -> ... 1'),
|
||||
nn.Linear(1, dim_hidden),
|
||||
nn.GELU(),
|
||||
nn.LayerNorm(dim_hidden),
|
||||
nn.Linear(dim_hidden, dim_hidden),
|
||||
nn.GELU(),
|
||||
nn.LayerNorm(dim_hidden),
|
||||
nn.Linear(dim_hidden, dim_hidden)
|
||||
)
|
||||
|
||||
class Unet(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
@@ -234,7 +238,8 @@ class Unet(nn.Module):
|
||||
channels = 3,
|
||||
resnet_block_groups = 8,
|
||||
learned_variance = False,
|
||||
sinusoidal_cond_mlp = True
|
||||
learned_sinusoidal_cond = False,
|
||||
learned_sinusoidal_dim = 16
|
||||
):
|
||||
super().__init__()
|
||||
|
||||
@@ -242,7 +247,7 @@ class Unet(nn.Module):
|
||||
|
||||
self.channels = channels
|
||||
|
||||
init_dim = default(init_dim, dim // 3 * 2)
|
||||
init_dim = default(init_dim, dim)
|
||||
self.init_conv = nn.Conv2d(channels, init_dim, 7, padding = 3)
|
||||
|
||||
dims = [init_dim, *map(lambda m: dim * m, dim_mults)]
|
||||
@@ -254,17 +259,21 @@ class Unet(nn.Module):
|
||||
|
||||
time_dim = dim * 4
|
||||
|
||||
self.sinusoidal_cond_mlp = sinusoidal_cond_mlp
|
||||
self.learned_sinusoidal_cond = learned_sinusoidal_cond
|
||||
|
||||
if sinusoidal_cond_mlp:
|
||||
self.time_mlp = nn.Sequential(
|
||||
SinusoidalPosEmb(dim),
|
||||
nn.Linear(dim, time_dim),
|
||||
nn.GELU(),
|
||||
nn.Linear(time_dim, time_dim)
|
||||
)
|
||||
if learned_sinusoidal_cond:
|
||||
sinu_pos_emb = LearnedSinusoidalPosEmb(learned_sinusoidal_dim)
|
||||
fourier_dim = learned_sinusoidal_dim + 1
|
||||
else:
|
||||
self.time_mlp = MLP(1, time_dim)
|
||||
sinu_pos_emb = SinusoidalPosEmb(dim)
|
||||
fourier_dim = dim
|
||||
|
||||
self.time_mlp = nn.Sequential(
|
||||
sinu_pos_emb,
|
||||
nn.Linear(fourier_dim, time_dim),
|
||||
nn.GELU(),
|
||||
nn.Linear(time_dim, time_dim)
|
||||
)
|
||||
|
||||
# layers
|
||||
|
||||
@@ -276,10 +285,10 @@ class Unet(nn.Module):
|
||||
is_last = ind >= (num_resolutions - 1)
|
||||
|
||||
self.downs.append(nn.ModuleList([
|
||||
block_klass(dim_in, dim_out, time_emb_dim = time_dim),
|
||||
block_klass(dim_out, dim_out, time_emb_dim = time_dim),
|
||||
Residual(PreNorm(dim_out, LinearAttention(dim_out))),
|
||||
Downsample(dim_out) if not is_last else nn.Identity()
|
||||
block_klass(dim_in, dim_in, time_emb_dim = time_dim),
|
||||
block_klass(dim_in, dim_in, time_emb_dim = time_dim),
|
||||
Residual(PreNorm(dim_in, LinearAttention(dim_in))),
|
||||
Downsample(dim_in, dim_out) if not is_last else nn.Conv2d(dim_in, dim_out, 3, padding = 1)
|
||||
]))
|
||||
|
||||
mid_dim = dims[-1]
|
||||
@@ -287,35 +296,38 @@ class Unet(nn.Module):
|
||||
self.mid_attn = Residual(PreNorm(mid_dim, Attention(mid_dim)))
|
||||
self.mid_block2 = block_klass(mid_dim, mid_dim, time_emb_dim = time_dim)
|
||||
|
||||
for ind, (dim_in, dim_out) in enumerate(reversed(in_out[1:])):
|
||||
is_last = ind >= (num_resolutions - 1)
|
||||
for ind, (dim_in, dim_out) in enumerate(reversed(in_out)):
|
||||
is_last = ind == (len(in_out) - 1)
|
||||
|
||||
self.ups.append(nn.ModuleList([
|
||||
block_klass(dim_out * 2, dim_in, time_emb_dim = time_dim),
|
||||
block_klass(dim_in, dim_in, time_emb_dim = time_dim),
|
||||
Residual(PreNorm(dim_in, LinearAttention(dim_in))),
|
||||
Upsample(dim_in) if not is_last else nn.Identity()
|
||||
block_klass(dim_out + dim_in, dim_out, time_emb_dim = time_dim),
|
||||
block_klass(dim_out + dim_in, dim_out, time_emb_dim = time_dim),
|
||||
Residual(PreNorm(dim_out, LinearAttention(dim_out))),
|
||||
Upsample(dim_out, dim_in) if not is_last else nn.Conv2d(dim_out, dim_in, 3, padding = 1)
|
||||
]))
|
||||
|
||||
default_out_dim = channels * (1 if not learned_variance else 2)
|
||||
self.out_dim = default(out_dim, default_out_dim)
|
||||
|
||||
self.final_conv = nn.Sequential(
|
||||
block_klass(dim, dim),
|
||||
nn.Conv2d(dim, self.out_dim, 1)
|
||||
)
|
||||
self.final_res_block = block_klass(dim * 2, dim, time_emb_dim = time_dim)
|
||||
self.final_conv = nn.Conv2d(dim, self.out_dim, 1)
|
||||
|
||||
def forward(self, x, time):
|
||||
x = self.init_conv(x)
|
||||
r = x.clone()
|
||||
|
||||
t = self.time_mlp(time)
|
||||
|
||||
h = []
|
||||
|
||||
for block1, block2, attn, downsample in self.downs:
|
||||
x = block1(x, t)
|
||||
h.append(x)
|
||||
|
||||
x = block2(x, t)
|
||||
x = attn(x)
|
||||
h.append(x)
|
||||
|
||||
x = downsample(x)
|
||||
|
||||
x = self.mid_block1(x, t)
|
||||
@@ -323,12 +335,18 @@ class Unet(nn.Module):
|
||||
x = self.mid_block2(x, t)
|
||||
|
||||
for block1, block2, attn, upsample in self.ups:
|
||||
x = torch.cat((x, h.pop()), dim=1)
|
||||
x = torch.cat((x, h.pop()), dim = 1)
|
||||
x = block1(x, t)
|
||||
|
||||
x = torch.cat((x, h.pop()), dim = 1)
|
||||
x = block2(x, t)
|
||||
x = attn(x)
|
||||
|
||||
x = upsample(x)
|
||||
|
||||
x = torch.cat((x, r), dim = 1)
|
||||
|
||||
x = self.final_res_block(x, t)
|
||||
return self.final_conv(x)
|
||||
|
||||
# gaussian diffusion trainer class
|
||||
@@ -351,7 +369,7 @@ def cosine_beta_schedule(timesteps, s = 0.008):
|
||||
"""
|
||||
steps = timesteps + 1
|
||||
x = torch.linspace(0, timesteps, steps, dtype = torch.float64)
|
||||
alphas_cumprod = torch.cos(((x / timesteps) + s) / (1 + s) * torch.pi * 0.5) ** 2
|
||||
alphas_cumprod = torch.cos(((x / timesteps) + s) / (1 + s) * math.pi * 0.5) ** 2
|
||||
alphas_cumprod = alphas_cumprod / alphas_cumprod[0]
|
||||
betas = 1 - (alphas_cumprod[1:] / alphas_cumprod[:-1])
|
||||
return torch.clip(betas, 0, 0.999)
|
||||
@@ -366,7 +384,9 @@ class GaussianDiffusion(nn.Module):
|
||||
timesteps = 1000,
|
||||
loss_type = 'l1',
|
||||
objective = 'pred_noise',
|
||||
beta_schedule = 'cosine'
|
||||
beta_schedule = 'cosine',
|
||||
p2_loss_weight_gamma = 0., # p2 loss weight, from https://arxiv.org/abs/2204.00227 - 0 is equivalent to weight of 1 across time - 1. is recommended
|
||||
p2_loss_weight_k = 1
|
||||
):
|
||||
super().__init__()
|
||||
assert not (type(self) == GaussianDiffusion and denoise_fn.channels != denoise_fn.out_dim)
|
||||
@@ -421,6 +441,10 @@ class GaussianDiffusion(nn.Module):
|
||||
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))
|
||||
|
||||
# calculate p2 reweighting
|
||||
|
||||
register_buffer('p2_loss_weight', (p2_loss_weight_k + alphas_cumprod / (1 - alphas_cumprod)) ** -p2_loss_weight_gamma)
|
||||
|
||||
def predict_start_from_noise(self, x_t, t, noise):
|
||||
return (
|
||||
extract(self.sqrt_recip_alphas_cumprod, t, x_t.shape) * x_t -
|
||||
@@ -527,8 +551,11 @@ class GaussianDiffusion(nn.Module):
|
||||
else:
|
||||
raise ValueError(f'unknown objective {self.objective}')
|
||||
|
||||
loss = self.loss_fn(model_out, target)
|
||||
return loss
|
||||
loss = self.loss_fn(model_out, target, reduction = 'none')
|
||||
loss = reduce(loss, 'b ... -> b (...)', 'mean')
|
||||
|
||||
loss = loss * extract(self.p2_loss_weight, t, loss.shape)
|
||||
return loss.mean()
|
||||
|
||||
def forward(self, img, *args, **kwargs):
|
||||
b, c, h, w, device, img_size, = *img.shape, img.device, self.image_size
|
||||
@@ -541,7 +568,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 +576,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()
|
||||
])
|
||||
@@ -571,22 +598,22 @@ class Trainer(object):
|
||||
folder,
|
||||
*,
|
||||
ema_decay = 0.995,
|
||||
image_size = 128,
|
||||
train_batch_size = 32,
|
||||
train_lr = 1e-4,
|
||||
train_num_steps = 100000,
|
||||
gradient_accumulate_every = 2,
|
||||
amp = False,
|
||||
step_start_ema = 2000,
|
||||
update_ema_every = 10,
|
||||
ema_update_every = 10,
|
||||
save_and_sample_every = 1000,
|
||||
results_folder = './results'
|
||||
results_folder = './results',
|
||||
augment_horizontal_flip = True
|
||||
):
|
||||
super().__init__()
|
||||
self.image_size = diffusion_model.image_size
|
||||
|
||||
self.model = diffusion_model
|
||||
self.ema = EMA(ema_decay)
|
||||
self.ema_model = copy.deepcopy(self.model)
|
||||
self.update_ema_every = update_ema_every
|
||||
self.ema = EMA(diffusion_model, beta = ema_decay, update_every = ema_update_every)
|
||||
|
||||
self.step_start_ema = step_start_ema
|
||||
self.save_and_sample_every = save_and_sample_every
|
||||
@@ -596,9 +623,9 @@ class Trainer(object):
|
||||
self.gradient_accumulate_every = gradient_accumulate_every
|
||||
self.train_num_steps = train_num_steps
|
||||
|
||||
self.ds = Dataset(folder, image_size)
|
||||
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)
|
||||
self.ds = Dataset(folder, self.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, num_workers = cpu_count()))
|
||||
self.opt = Adam(diffusion_model.parameters(), lr = train_lr)
|
||||
|
||||
self.step = 0
|
||||
|
||||
@@ -608,22 +635,11 @@ class Trainer(object):
|
||||
self.results_folder = Path(results_folder)
|
||||
self.results_folder.mkdir(exist_ok = True)
|
||||
|
||||
self.reset_parameters()
|
||||
|
||||
def reset_parameters(self):
|
||||
self.ema_model.load_state_dict(self.model.state_dict())
|
||||
|
||||
def step_ema(self):
|
||||
if self.step < self.step_start_ema:
|
||||
self.reset_parameters()
|
||||
return
|
||||
self.ema.update_model_average(self.ema_model, self.model)
|
||||
|
||||
def save(self, milestone):
|
||||
data = {
|
||||
'step': self.step,
|
||||
'model': self.model.state_dict(),
|
||||
'ema': self.ema_model.state_dict(),
|
||||
'ema': self.ema.state_dict(),
|
||||
'scaler': self.scaler.state_dict()
|
||||
}
|
||||
torch.save(data, str(self.results_folder / f'model-{milestone}.pt'))
|
||||
@@ -633,7 +649,7 @@ class Trainer(object):
|
||||
|
||||
self.step = data['step']
|
||||
self.model.load_state_dict(data['model'])
|
||||
self.ema_model.load_state_dict(data['ema'])
|
||||
self.ema.load_state_dict(data['ema'])
|
||||
self.scaler.load_state_dict(data['scaler'])
|
||||
|
||||
def train(self):
|
||||
@@ -653,15 +669,15 @@ class Trainer(object):
|
||||
self.scaler.update()
|
||||
self.opt.zero_grad()
|
||||
|
||||
if self.step % self.update_ema_every == 0:
|
||||
self.step_ema()
|
||||
self.ema.update()
|
||||
|
||||
if self.step != 0 and self.step % self.save_and_sample_every == 0:
|
||||
self.ema_model.eval()
|
||||
self.ema.ema_model.eval()
|
||||
with torch.no_grad():
|
||||
milestone = self.step // self.save_and_sample_every
|
||||
batches = num_to_groups(36, self.batch_size)
|
||||
all_images_list = list(map(lambda n: self.ema.ema_model.sample(batch_size=n), batches))
|
||||
|
||||
milestone = self.step // self.save_and_sample_every
|
||||
batches = num_to_groups(36, self.batch_size)
|
||||
all_images_list = list(map(lambda n: self.ema_model.sample(batch_size=n), batches))
|
||||
all_images = torch.cat(all_images_list, dim=0)
|
||||
utils.save_image(all_images, str(self.results_folder / f'sample-{milestone}.png'), nrow = 6)
|
||||
self.save(milestone)
|
||||
|
||||
@@ -0,0 +1,222 @@
|
||||
from math import sqrt
|
||||
import torch
|
||||
from torch import nn, einsum
|
||||
import torch.nn.functional as F
|
||||
|
||||
from tqdm import tqdm
|
||||
from einops import rearrange, repeat, reduce
|
||||
|
||||
# helpers
|
||||
|
||||
def exists(val):
|
||||
return val is not None
|
||||
|
||||
def default(val, d):
|
||||
if exists(val):
|
||||
return val
|
||||
return d() if callable(d) else d
|
||||
|
||||
# tensor helpers
|
||||
|
||||
def log(t, eps = 1e-20):
|
||||
return torch.log(t.clamp(min = eps))
|
||||
|
||||
# normalization functions
|
||||
|
||||
def normalize_to_neg_one_to_one(img):
|
||||
return img * 2 - 1
|
||||
|
||||
def unnormalize_to_zero_to_one(t):
|
||||
return (t + 1) * 0.5
|
||||
|
||||
# main class
|
||||
|
||||
class ElucidatedDiffusion(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
net,
|
||||
*,
|
||||
image_size,
|
||||
channels = 3,
|
||||
num_sample_steps = 32, # number of sampling steps
|
||||
sigma_min = 0.002, # min noise level
|
||||
sigma_max = 80, # max noise level
|
||||
sigma_data = 0.5, # standard deviation of data distribution
|
||||
rho = 7, # controls the sampling schedule
|
||||
P_mean = -1.2, # mean of log-normal distribution from which noise is drawn for training
|
||||
P_std = 1.2, # standard deviation of log-normal distribution from which noise is drawn for training
|
||||
S_churn = 80, # parameters for stochastic sampling - depends on dataset, Table 5 in apper
|
||||
S_tmin = 0.05,
|
||||
S_tmax = 50,
|
||||
S_noise = 1.003,
|
||||
):
|
||||
super().__init__()
|
||||
assert net.learned_sinusoidal_cond
|
||||
|
||||
self.net = net
|
||||
|
||||
# image dimensions
|
||||
|
||||
self.channels = channels
|
||||
self.image_size = image_size
|
||||
|
||||
# parameters
|
||||
|
||||
self.sigma_min = sigma_min
|
||||
self.sigma_max = sigma_max
|
||||
self.sigma_data = sigma_data
|
||||
|
||||
self.rho = rho
|
||||
|
||||
self.P_mean = P_mean
|
||||
self.P_std = P_std
|
||||
|
||||
self.num_sample_steps = num_sample_steps # otherwise known as N in the paper
|
||||
|
||||
self.S_churn = S_churn
|
||||
self.S_tmin = S_tmin
|
||||
self.S_tmax = S_tmax
|
||||
self.S_noise = S_noise
|
||||
|
||||
@property
|
||||
def device(self):
|
||||
return next(self.net.parameters()).device
|
||||
|
||||
# derived preconditioning params - Table 1
|
||||
|
||||
def c_skip(self, sigma):
|
||||
return (self.sigma_data ** 2) / (sigma ** 2 + self.sigma_data ** 2)
|
||||
|
||||
def c_out(self, sigma):
|
||||
return sigma * self.sigma_data * (self.sigma_data ** 2 + sigma ** 2) ** -0.5
|
||||
|
||||
def c_in(self, sigma):
|
||||
return 1 * (sigma ** 2 + self.sigma_data ** 2) ** -0.5
|
||||
|
||||
def c_noise(self, sigma):
|
||||
return log(sigma) * 0.25
|
||||
|
||||
# noise distribution
|
||||
|
||||
def noise_distribution(self, batch_size):
|
||||
return (self.P_mean + self.P_std * torch.randn((batch_size,), device = self.device)).exp()
|
||||
|
||||
def loss_weight(self, sigma):
|
||||
return (sigma ** 2 + self.sigma_data ** 2) * (sigma * self.sigma_data) ** -2
|
||||
|
||||
# sample schedule
|
||||
# equation (5) in the paper
|
||||
|
||||
def sample_schedule(self, num_sample_steps = None):
|
||||
num_sample_steps = default(num_sample_steps, self.num_sample_steps)
|
||||
|
||||
N = num_sample_steps
|
||||
inv_rho = 1 / self.rho
|
||||
|
||||
steps = torch.arange(num_sample_steps, device = self.device, dtype = torch.float32)
|
||||
sigmas = (self.sigma_max ** inv_rho + steps / (N - 1) * (self.sigma_min ** inv_rho - self.sigma_max ** inv_rho)) ** self.rho
|
||||
|
||||
sigmas = F.pad(sigmas, (0, 1), value = 0.) # last step is sigma value of 0.
|
||||
return sigmas
|
||||
|
||||
# preconditioned network output
|
||||
# equation (7) in the paper
|
||||
|
||||
def preconditioned_network_forward(self, noised_images, sigma, clamp = False):
|
||||
batch, device = noised_images.shape[0], noised_images.device
|
||||
|
||||
if isinstance(sigma, float):
|
||||
sigma = torch.full((batch,), sigma, device = device)
|
||||
|
||||
padded_sigma = rearrange(sigma, 'b -> b 1 1 1')
|
||||
|
||||
net_out = self.net(
|
||||
self.c_in(padded_sigma) * noised_images,
|
||||
self.c_noise(sigma)
|
||||
)
|
||||
|
||||
out = self.c_skip(padded_sigma) * noised_images + self.c_out(padded_sigma) * net_out
|
||||
|
||||
if clamp:
|
||||
out = out.clamp(-1., 1.)
|
||||
|
||||
return out
|
||||
|
||||
# sampling
|
||||
|
||||
@torch.no_grad()
|
||||
def sample(self, batch_size = 16, num_sample_steps = None, clamp = True):
|
||||
num_sample_steps = default(num_sample_steps, self.num_sample_steps)
|
||||
|
||||
shape = (batch_size, self.channels, self.image_size, self.image_size)
|
||||
|
||||
# get the schedule, which is returned as (sigma, gamma) tuple, and pair up with the next sigma and gamma
|
||||
|
||||
sigmas = self.sample_schedule(num_sample_steps)
|
||||
|
||||
gammas = torch.where(
|
||||
(sigmas >= self.S_tmin) & (sigmas <= self.S_tmax),
|
||||
min(self.S_churn / num_sample_steps, sqrt(2) - 1),
|
||||
0.
|
||||
)
|
||||
|
||||
sigmas_and_gammas = list(zip(sigmas[:-1], sigmas[1:], gammas[:-1]))
|
||||
|
||||
# images is noise at the beginning
|
||||
|
||||
init_sigma = sigmas[0]
|
||||
|
||||
images = init_sigma * torch.randn(shape, device = self.device)
|
||||
|
||||
# gradually denoise
|
||||
|
||||
for sigma, sigma_next, gamma in tqdm(sigmas_and_gammas, desc = 'sampling time step'):
|
||||
sigma, sigma_next, gamma = map(lambda t: t.item(), (sigma, sigma_next, gamma))
|
||||
|
||||
eps = self.S_noise * torch.randn(shape, device = self.device) # stochastic sampling
|
||||
|
||||
sigma_hat = sigma + gamma * sigma
|
||||
images_hat = images + sqrt(sigma_hat ** 2 - sigma ** 2) * eps
|
||||
|
||||
model_output = self.preconditioned_network_forward(images_hat, sigma_hat, clamp = clamp)
|
||||
denoised_over_sigma = (images_hat - model_output) / sigma_hat
|
||||
|
||||
images_next = images_hat + (sigma_next - sigma_hat) * denoised_over_sigma
|
||||
|
||||
# second order correction, if not the last timestep
|
||||
|
||||
if sigma_next != 0:
|
||||
model_output_next = self.preconditioned_network_forward(images_next, sigma_next, clamp = clamp)
|
||||
denoised_prime_over_sigma = (images_next - model_output_next) / sigma_next
|
||||
images_next = images_hat + 0.5 * (sigma_next - sigma_hat) * (denoised_over_sigma + denoised_prime_over_sigma)
|
||||
|
||||
images = images_next
|
||||
|
||||
images = images.clamp(-1., 1.)
|
||||
return unnormalize_to_zero_to_one(images)
|
||||
|
||||
# training
|
||||
|
||||
def forward(self, images):
|
||||
batch_size, c, h, w, device, image_size, channels = *images.shape, images.device, self.image_size, self.channels
|
||||
|
||||
assert h == image_size and w == image_size, f'height and width of image must be {image_size}'
|
||||
assert c == channels, 'mismatch of image channels'
|
||||
|
||||
images = normalize_to_neg_one_to_one(images)
|
||||
|
||||
sigmas = self.noise_distribution(batch_size)
|
||||
padded_sigmas = rearrange(sigmas, 'b -> b 1 1 1')
|
||||
|
||||
noise = torch.randn_like(images)
|
||||
|
||||
noised_images = images + padded_sigmas * noise # alphas are 1. in the paper
|
||||
|
||||
denoised = self.preconditioned_network_forward(noised_images, sigmas)
|
||||
|
||||
losses = F.mse_loss(denoised, images, reduction = 'none')
|
||||
losses = reduce(losses, 'b ... -> b', 'mean')
|
||||
|
||||
losses = losses * self.loss_weight(sigmas)
|
||||
|
||||
return losses.mean()
|
||||
@@ -3,7 +3,7 @@ from setuptools import setup, find_packages
|
||||
setup(
|
||||
name = 'denoising-diffusion-pytorch',
|
||||
packages = find_packages(),
|
||||
version = '0.18.0',
|
||||
version = '0.23.3',
|
||||
license='MIT',
|
||||
description = 'Denoising Diffusion Probabilistic Models - Pytorch',
|
||||
author = 'Phil Wang',
|
||||
@@ -16,6 +16,7 @@ setup(
|
||||
],
|
||||
install_requires=[
|
||||
'einops',
|
||||
'ema-pytorch',
|
||||
'pillow',
|
||||
'torch',
|
||||
'torchvision',
|
||||
|
||||
Reference in New Issue
Block a user