mirror of
https://github.com/wassname/denoising-diffusion-pytorch.git
synced 2026-09-10 12:01:08 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8408775cfc | ||
|
|
86fcb6785b | ||
|
|
c535d31fc5 | ||
|
|
5db64fec4b | ||
|
|
b87ea27781 | ||
|
|
a8403b83fe | ||
|
|
f4b1d7a67c | ||
|
|
618493714f | ||
|
|
76b79aa847 | ||
|
|
be2bd8d320 | ||
|
|
c3d1607019 | ||
|
|
06b2e52645 | ||
|
|
09b8a1c805 | ||
|
|
d26acbcae6 | ||
|
|
9939a48139 | ||
|
|
75ea49a7ef | ||
|
|
8c3609a6e3 | ||
|
|
1586d1a8a0 |
@@ -133,3 +133,13 @@ Samples and model checkpoints will be logged to `./results` periodically
|
|||||||
volume = {abs/2204.00227}
|
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.learned_gaussian_diffusion import LearnedGaussianDiffusion
|
||||||
from denoising_diffusion_pytorch.continuous_time_gaussian_diffusion import ContinuousTimeGaussianDiffusion
|
from denoising_diffusion_pytorch.continuous_time_gaussian_diffusion import ContinuousTimeGaussianDiffusion
|
||||||
from denoising_diffusion_pytorch.weighted_objective_gaussian_diffusion import WeightedObjectiveGaussianDiffusion
|
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
|
import torch
|
||||||
from torch import sqrt
|
from torch import sqrt
|
||||||
from torch import nn, einsum
|
from torch import nn, einsum
|
||||||
@@ -66,7 +67,7 @@ def beta_linear_log_snr(t):
|
|||||||
return -log(expm1(1e-4 + 10 * (t ** 2)))
|
return -log(expm1(1e-4 + 10 * (t ** 2)))
|
||||||
|
|
||||||
def alpha_cosine_log_snr(t, s = 0.008):
|
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):
|
class learned_noise_schedule(nn.Module):
|
||||||
""" described in section H and then I.2 of the supplementary material for variational ddpm paper """
|
""" described in section H and then I.2 of the supplementary material for variational ddpm paper """
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ from tqdm import tqdm
|
|||||||
from einops import rearrange, reduce
|
from einops import rearrange, reduce
|
||||||
from einops.layers.torch import Rearrange
|
from einops.layers.torch import Rearrange
|
||||||
|
|
||||||
|
from ema_pytorch import EMA
|
||||||
|
|
||||||
# helpers functions
|
# helpers functions
|
||||||
|
|
||||||
def exists(x):
|
def exists(x):
|
||||||
@@ -50,21 +52,6 @@ def unnormalize_to_zero_to_one(t):
|
|||||||
|
|
||||||
# small helper modules
|
# 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):
|
class Residual(nn.Module):
|
||||||
def __init__(self, fn):
|
def __init__(self, fn):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
@@ -73,11 +60,14 @@ class Residual(nn.Module):
|
|||||||
def forward(self, x, *args, **kwargs):
|
def forward(self, x, *args, **kwargs):
|
||||||
return self.fn(x, *args, **kwargs) + x
|
return self.fn(x, *args, **kwargs) + x
|
||||||
|
|
||||||
def Upsample(dim):
|
def Upsample(dim, dim_out = None):
|
||||||
return nn.ConvTranspose2d(dim, dim, 4, 2, 1)
|
return nn.Sequential(
|
||||||
|
nn.Upsample(scale_factor = 2, mode = 'nearest'),
|
||||||
|
nn.Conv2d(dim, default(dim_out, dim), 3, padding = 1)
|
||||||
|
)
|
||||||
|
|
||||||
def Downsample(dim):
|
def Downsample(dim, dim_out = None):
|
||||||
return nn.Conv2d(dim, dim, 4, 2, 1)
|
return nn.Conv2d(dim, default(dim_out, dim), 4, 2, 1)
|
||||||
|
|
||||||
class LayerNorm(nn.Module):
|
class LayerNorm(nn.Module):
|
||||||
def __init__(self, dim, eps = 1e-5):
|
def __init__(self, dim, eps = 1e-5):
|
||||||
@@ -290,10 +280,10 @@ class Unet(nn.Module):
|
|||||||
is_last = ind >= (num_resolutions - 1)
|
is_last = ind >= (num_resolutions - 1)
|
||||||
|
|
||||||
self.downs.append(nn.ModuleList([
|
self.downs.append(nn.ModuleList([
|
||||||
block_klass(dim_in, dim_out, time_emb_dim = time_dim),
|
block_klass(dim_in, dim_in, time_emb_dim = time_dim),
|
||||||
block_klass(dim_out, dim_out, time_emb_dim = time_dim),
|
block_klass(dim_in, dim_in, time_emb_dim = time_dim),
|
||||||
Residual(PreNorm(dim_out, LinearAttention(dim_out))),
|
Residual(PreNorm(dim_in, LinearAttention(dim_in))),
|
||||||
Downsample(dim_out) if not is_last else nn.Identity()
|
Downsample(dim_in, dim_out) if not is_last else nn.Conv2d(dim_in, dim_out, 3, padding = 1)
|
||||||
]))
|
]))
|
||||||
|
|
||||||
mid_dim = dims[-1]
|
mid_dim = dims[-1]
|
||||||
@@ -305,10 +295,10 @@ class Unet(nn.Module):
|
|||||||
is_last = ind == (len(in_out) - 1)
|
is_last = ind == (len(in_out) - 1)
|
||||||
|
|
||||||
self.ups.append(nn.ModuleList([
|
self.ups.append(nn.ModuleList([
|
||||||
block_klass(dim_out * 2, dim_in, time_emb_dim = time_dim),
|
block_klass(dim_out + dim_in, dim_out, time_emb_dim = time_dim),
|
||||||
block_klass(dim_in, dim_in, time_emb_dim = time_dim),
|
block_klass(dim_out + dim_in, dim_out, time_emb_dim = time_dim),
|
||||||
Residual(PreNorm(dim_in, LinearAttention(dim_in))),
|
Residual(PreNorm(dim_out, LinearAttention(dim_out))),
|
||||||
Upsample(dim_in) if not is_last else nn.Identity()
|
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)
|
default_out_dim = channels * (1 if not learned_variance else 2)
|
||||||
@@ -327,9 +317,12 @@ class Unet(nn.Module):
|
|||||||
|
|
||||||
for block1, block2, attn, downsample in self.downs:
|
for block1, block2, attn, downsample in self.downs:
|
||||||
x = block1(x, t)
|
x = block1(x, t)
|
||||||
|
h.append(x)
|
||||||
|
|
||||||
x = block2(x, t)
|
x = block2(x, t)
|
||||||
x = attn(x)
|
x = attn(x)
|
||||||
h.append(x)
|
h.append(x)
|
||||||
|
|
||||||
x = downsample(x)
|
x = downsample(x)
|
||||||
|
|
||||||
x = self.mid_block1(x, t)
|
x = self.mid_block1(x, t)
|
||||||
@@ -339,8 +332,11 @@ class Unet(nn.Module):
|
|||||||
for block1, block2, attn, upsample in self.ups:
|
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 = block1(x, t)
|
||||||
|
|
||||||
|
x = torch.cat((x, h.pop()), dim = 1)
|
||||||
x = block2(x, t)
|
x = block2(x, t)
|
||||||
x = attn(x)
|
x = attn(x)
|
||||||
|
|
||||||
x = upsample(x)
|
x = upsample(x)
|
||||||
|
|
||||||
x = torch.cat((x, r), dim = 1)
|
x = torch.cat((x, r), dim = 1)
|
||||||
@@ -368,7 +364,7 @@ def cosine_beta_schedule(timesteps, s = 0.008):
|
|||||||
"""
|
"""
|
||||||
steps = timesteps + 1
|
steps = timesteps + 1
|
||||||
x = torch.linspace(0, timesteps, steps, dtype = torch.float64)
|
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]
|
alphas_cumprod = alphas_cumprod / alphas_cumprod[0]
|
||||||
betas = 1 - (alphas_cumprod[1:] / alphas_cumprod[:-1])
|
betas = 1 - (alphas_cumprod[1:] / alphas_cumprod[:-1])
|
||||||
return torch.clip(betas, 0, 0.999)
|
return torch.clip(betas, 0, 0.999)
|
||||||
@@ -597,23 +593,22 @@ class Trainer(object):
|
|||||||
folder,
|
folder,
|
||||||
*,
|
*,
|
||||||
ema_decay = 0.995,
|
ema_decay = 0.995,
|
||||||
image_size = 128,
|
|
||||||
train_batch_size = 32,
|
train_batch_size = 32,
|
||||||
train_lr = 1e-4,
|
train_lr = 1e-4,
|
||||||
train_num_steps = 100000,
|
train_num_steps = 100000,
|
||||||
gradient_accumulate_every = 2,
|
gradient_accumulate_every = 2,
|
||||||
amp = False,
|
amp = False,
|
||||||
step_start_ema = 2000,
|
step_start_ema = 2000,
|
||||||
update_ema_every = 10,
|
ema_update_every = 10,
|
||||||
save_and_sample_every = 1000,
|
save_and_sample_every = 1000,
|
||||||
results_folder = './results',
|
results_folder = './results',
|
||||||
augment_horizontal_flip = True
|
augment_horizontal_flip = True
|
||||||
):
|
):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
self.image_size = diffusion_model.image_size
|
||||||
|
|
||||||
self.model = diffusion_model
|
self.model = diffusion_model
|
||||||
self.ema = EMA(ema_decay)
|
self.ema = EMA(diffusion_model, beta = ema_decay, update_every = ema_update_every)
|
||||||
self.ema_model = copy.deepcopy(self.model)
|
|
||||||
self.update_ema_every = update_ema_every
|
|
||||||
|
|
||||||
self.step_start_ema = step_start_ema
|
self.step_start_ema = step_start_ema
|
||||||
self.save_and_sample_every = save_and_sample_every
|
self.save_and_sample_every = save_and_sample_every
|
||||||
@@ -623,9 +618,9 @@ class Trainer(object):
|
|||||||
self.gradient_accumulate_every = gradient_accumulate_every
|
self.gradient_accumulate_every = gradient_accumulate_every
|
||||||
self.train_num_steps = train_num_steps
|
self.train_num_steps = train_num_steps
|
||||||
|
|
||||||
self.ds = Dataset(folder, image_size, augment_horizontal_flip = augment_horizontal_flip)
|
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.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.opt = Adam(diffusion_model.parameters(), lr = train_lr)
|
||||||
|
|
||||||
self.step = 0
|
self.step = 0
|
||||||
|
|
||||||
@@ -635,22 +630,11 @@ class Trainer(object):
|
|||||||
self.results_folder = Path(results_folder)
|
self.results_folder = Path(results_folder)
|
||||||
self.results_folder.mkdir(exist_ok = True)
|
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):
|
def save(self, milestone):
|
||||||
data = {
|
data = {
|
||||||
'step': self.step,
|
'step': self.step,
|
||||||
'model': self.model.state_dict(),
|
'model': self.model.state_dict(),
|
||||||
'ema': self.ema_model.state_dict(),
|
'ema': self.ema.state_dict(),
|
||||||
'scaler': self.scaler.state_dict()
|
'scaler': self.scaler.state_dict()
|
||||||
}
|
}
|
||||||
torch.save(data, str(self.results_folder / f'model-{milestone}.pt'))
|
torch.save(data, str(self.results_folder / f'model-{milestone}.pt'))
|
||||||
@@ -660,7 +644,7 @@ class Trainer(object):
|
|||||||
|
|
||||||
self.step = data['step']
|
self.step = data['step']
|
||||||
self.model.load_state_dict(data['model'])
|
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'])
|
self.scaler.load_state_dict(data['scaler'])
|
||||||
|
|
||||||
def train(self):
|
def train(self):
|
||||||
@@ -680,15 +664,15 @@ class Trainer(object):
|
|||||||
self.scaler.update()
|
self.scaler.update()
|
||||||
self.opt.zero_grad()
|
self.opt.zero_grad()
|
||||||
|
|
||||||
if self.step % self.update_ema_every == 0:
|
self.ema.update()
|
||||||
self.step_ema()
|
|
||||||
|
|
||||||
if self.step != 0 and self.step % self.save_and_sample_every == 0:
|
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)
|
all_images = torch.cat(all_images_list, dim=0)
|
||||||
utils.save_image(all_images, str(self.results_folder / f'sample-{milestone}.png'), nrow = 6)
|
utils.save_image(all_images, str(self.results_folder / f'sample-{milestone}.png'), nrow = 6)
|
||||||
self.save(milestone)
|
self.save(milestone)
|
||||||
|
|||||||
@@ -0,0 +1,217 @@
|
|||||||
|
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):
|
||||||
|
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)
|
||||||
|
)
|
||||||
|
|
||||||
|
return self.c_skip(padded_sigma) * noised_images + self.c_out(padded_sigma) * net_out
|
||||||
|
|
||||||
|
# sampling
|
||||||
|
|
||||||
|
@torch.no_grad()
|
||||||
|
def sample(self, batch_size = 16, num_sample_steps = None):
|
||||||
|
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)
|
||||||
|
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)
|
||||||
|
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(
|
setup(
|
||||||
name = 'denoising-diffusion-pytorch',
|
name = 'denoising-diffusion-pytorch',
|
||||||
packages = find_packages(),
|
packages = find_packages(),
|
||||||
version = '0.20.1',
|
version = '0.23.1',
|
||||||
license='MIT',
|
license='MIT',
|
||||||
description = 'Denoising Diffusion Probabilistic Models - Pytorch',
|
description = 'Denoising Diffusion Probabilistic Models - Pytorch',
|
||||||
author = 'Phil Wang',
|
author = 'Phil Wang',
|
||||||
@@ -16,6 +16,7 @@ setup(
|
|||||||
],
|
],
|
||||||
install_requires=[
|
install_requires=[
|
||||||
'einops',
|
'einops',
|
||||||
|
'ema-pytorch',
|
||||||
'pillow',
|
'pillow',
|
||||||
'torch',
|
'torch',
|
||||||
'torchvision',
|
'torchvision',
|
||||||
|
|||||||
Reference in New Issue
Block a user