|
|
|
@@ -7,21 +7,16 @@ from inspect import isfunction
|
|
|
|
|
from functools import partial
|
|
|
|
|
|
|
|
|
|
from torch.utils import data
|
|
|
|
|
from torch.cuda.amp import autocast, GradScaler
|
|
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from torch.optim import Adam
|
|
|
|
|
from torchvision import transforms, utils
|
|
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
from tqdm import tqdm
|
|
|
|
|
from einops import rearrange
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
from apex import amp
|
|
|
|
|
APEX_AVAILABLE = True
|
|
|
|
|
except:
|
|
|
|
|
APEX_AVAILABLE = False
|
|
|
|
|
|
|
|
|
|
# helpers functions
|
|
|
|
|
|
|
|
|
|
def exists(x):
|
|
|
|
@@ -45,13 +40,6 @@ def num_to_groups(num, divisor):
|
|
|
|
|
arr.append(remainder)
|
|
|
|
|
return arr
|
|
|
|
|
|
|
|
|
|
def loss_backwards(fp16, loss, optimizer, **kwargs):
|
|
|
|
|
if fp16:
|
|
|
|
|
with amp.scale_loss(loss, optimizer) as scaled_loss:
|
|
|
|
|
scaled_loss.backward(**kwargs)
|
|
|
|
|
else:
|
|
|
|
|
loss.backward(**kwargs)
|
|
|
|
|
|
|
|
|
|
# small helper modules
|
|
|
|
|
|
|
|
|
|
class EMA():
|
|
|
|
@@ -204,6 +192,7 @@ class Unet(nn.Module):
|
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
dim,
|
|
|
|
|
init_dim = None,
|
|
|
|
|
out_dim = None,
|
|
|
|
|
dim_mults=(1, 2, 4, 8),
|
|
|
|
|
channels = 3,
|
|
|
|
@@ -212,16 +201,19 @@ class Unet(nn.Module):
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.channels = channels
|
|
|
|
|
|
|
|
|
|
dims = [channels, *map(lambda m: dim * m, dim_mults)]
|
|
|
|
|
init_dim = default(init_dim, dim // 3 * 2)
|
|
|
|
|
self.init_conv = nn.Conv2d(channels, init_dim, 7, padding = 3)
|
|
|
|
|
|
|
|
|
|
dims = [init_dim, *map(lambda m: dim * m, dim_mults)]
|
|
|
|
|
in_out = list(zip(dims[:-1], dims[1:]))
|
|
|
|
|
|
|
|
|
|
if with_time_emb:
|
|
|
|
|
time_dim = dim
|
|
|
|
|
time_dim = dim * 4
|
|
|
|
|
self.time_mlp = nn.Sequential(
|
|
|
|
|
SinusoidalPosEmb(dim),
|
|
|
|
|
nn.Linear(dim, dim * 4),
|
|
|
|
|
nn.Linear(dim, time_dim),
|
|
|
|
|
nn.GELU(),
|
|
|
|
|
nn.Linear(dim * 4, dim)
|
|
|
|
|
nn.Linear(time_dim, time_dim)
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
time_dim = None
|
|
|
|
@@ -263,6 +255,8 @@ class Unet(nn.Module):
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def forward(self, x, time):
|
|
|
|
|
x = self.init_conv(x)
|
|
|
|
|
|
|
|
|
|
t = self.time_mlp(time) if exists(self.time_mlp) else None
|
|
|
|
|
|
|
|
|
|
h = []
|
|
|
|
@@ -305,11 +299,11 @@ def cosine_beta_schedule(timesteps, s = 0.008):
|
|
|
|
|
as proposed in https://openreview.net/forum?id=-NEXDKk8gZ
|
|
|
|
|
"""
|
|
|
|
|
steps = timesteps + 1
|
|
|
|
|
x = np.linspace(0, steps, steps)
|
|
|
|
|
alphas_cumprod = np.cos(((x / steps) + s) / (1 + s) * np.pi * 0.5) ** 2
|
|
|
|
|
x = torch.linspace(0, timesteps, steps)
|
|
|
|
|
alphas_cumprod = torch.cos(((x / timesteps) + s) / (1 + s) * torch.pi * 0.5) ** 2
|
|
|
|
|
alphas_cumprod = alphas_cumprod / alphas_cumprod[0]
|
|
|
|
|
betas = 1 - (alphas_cumprod[1:] / alphas_cumprod[:-1])
|
|
|
|
|
return np.clip(betas, a_min = 0, a_max = 0.999)
|
|
|
|
|
return torch.clip(betas, 0, 0.999)
|
|
|
|
|
|
|
|
|
|
class GaussianDiffusion(nn.Module):
|
|
|
|
|
def __init__(
|
|
|
|
@@ -319,50 +313,48 @@ class GaussianDiffusion(nn.Module):
|
|
|
|
|
image_size,
|
|
|
|
|
channels = 3,
|
|
|
|
|
timesteps = 1000,
|
|
|
|
|
loss_type = 'l1',
|
|
|
|
|
betas = None
|
|
|
|
|
loss_type = 'l1'
|
|
|
|
|
):
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.channels = channels
|
|
|
|
|
self.image_size = image_size
|
|
|
|
|
self.denoise_fn = denoise_fn
|
|
|
|
|
|
|
|
|
|
if exists(betas):
|
|
|
|
|
betas = betas.detach().cpu().numpy() if isinstance(betas, torch.Tensor) else betas
|
|
|
|
|
else:
|
|
|
|
|
betas = cosine_beta_schedule(timesteps)
|
|
|
|
|
betas = cosine_beta_schedule(timesteps)
|
|
|
|
|
|
|
|
|
|
alphas = 1. - betas
|
|
|
|
|
alphas_cumprod = np.cumprod(alphas, axis=0)
|
|
|
|
|
alphas_cumprod_prev = np.append(1., alphas_cumprod[:-1])
|
|
|
|
|
alphas_cumprod = torch.cumprod(alphas, axis=0)
|
|
|
|
|
alphas_cumprod_prev = F.pad(alphas_cumprod[:-1], (1, 0), value = 1.)
|
|
|
|
|
|
|
|
|
|
timesteps, = betas.shape
|
|
|
|
|
self.num_timesteps = int(timesteps)
|
|
|
|
|
self.loss_type = loss_type
|
|
|
|
|
|
|
|
|
|
to_torch = partial(torch.tensor, dtype=torch.float32)
|
|
|
|
|
|
|
|
|
|
self.register_buffer('betas', to_torch(betas))
|
|
|
|
|
self.register_buffer('alphas_cumprod', to_torch(alphas_cumprod))
|
|
|
|
|
self.register_buffer('alphas_cumprod_prev', to_torch(alphas_cumprod_prev))
|
|
|
|
|
self.register_buffer('betas', betas)
|
|
|
|
|
self.register_buffer('alphas_cumprod', alphas_cumprod)
|
|
|
|
|
self.register_buffer('alphas_cumprod_prev', alphas_cumprod_prev)
|
|
|
|
|
|
|
|
|
|
# calculations for diffusion q(x_t | x_{t-1}) and others
|
|
|
|
|
self.register_buffer('sqrt_alphas_cumprod', to_torch(np.sqrt(alphas_cumprod)))
|
|
|
|
|
self.register_buffer('sqrt_one_minus_alphas_cumprod', to_torch(np.sqrt(1. - alphas_cumprod)))
|
|
|
|
|
self.register_buffer('log_one_minus_alphas_cumprod', to_torch(np.log(1. - alphas_cumprod)))
|
|
|
|
|
self.register_buffer('sqrt_recip_alphas_cumprod', to_torch(np.sqrt(1. / alphas_cumprod)))
|
|
|
|
|
self.register_buffer('sqrt_recipm1_alphas_cumprod', to_torch(np.sqrt(1. / alphas_cumprod - 1)))
|
|
|
|
|
|
|
|
|
|
self.register_buffer('sqrt_alphas_cumprod', torch.sqrt(alphas_cumprod))
|
|
|
|
|
self.register_buffer('sqrt_one_minus_alphas_cumprod', torch.sqrt(1. - alphas_cumprod))
|
|
|
|
|
self.register_buffer('log_one_minus_alphas_cumprod', torch.log(1. - alphas_cumprod))
|
|
|
|
|
self.register_buffer('sqrt_recip_alphas_cumprod', torch.sqrt(1. / alphas_cumprod))
|
|
|
|
|
self.register_buffer('sqrt_recipm1_alphas_cumprod', torch.sqrt(1. / alphas_cumprod - 1))
|
|
|
|
|
|
|
|
|
|
# calculations for posterior q(x_{t-1} | x_t, x_0)
|
|
|
|
|
|
|
|
|
|
posterior_variance = betas * (1. - alphas_cumprod_prev) / (1. - alphas_cumprod)
|
|
|
|
|
|
|
|
|
|
# above: equal to 1. / (1. / (1. - alpha_cumprod_tm1) + alpha_t / beta_t)
|
|
|
|
|
self.register_buffer('posterior_variance', to_torch(posterior_variance))
|
|
|
|
|
|
|
|
|
|
self.register_buffer('posterior_variance', posterior_variance)
|
|
|
|
|
|
|
|
|
|
# below: log calculation clipped because the posterior variance is 0 at the beginning of the diffusion chain
|
|
|
|
|
self.register_buffer('posterior_log_variance_clipped', to_torch(np.log(np.maximum(posterior_variance, 1e-20))))
|
|
|
|
|
self.register_buffer('posterior_mean_coef1', to_torch(
|
|
|
|
|
betas * np.sqrt(alphas_cumprod_prev) / (1. - alphas_cumprod)))
|
|
|
|
|
self.register_buffer('posterior_mean_coef2', to_torch(
|
|
|
|
|
(1. - alphas_cumprod_prev) * np.sqrt(alphas) / (1. - alphas_cumprod)))
|
|
|
|
|
|
|
|
|
|
self.register_buffer('posterior_log_variance_clipped', torch.log(posterior_variance.clamp(min =1e-20)))
|
|
|
|
|
self.register_buffer('posterior_mean_coef1', betas * torch.sqrt(alphas_cumprod_prev) / (1. - alphas_cumprod))
|
|
|
|
|
self.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
|
|
|
|
@@ -505,7 +497,7 @@ class Trainer(object):
|
|
|
|
|
train_lr = 2e-5,
|
|
|
|
|
train_num_steps = 100000,
|
|
|
|
|
gradient_accumulate_every = 2,
|
|
|
|
|
fp16 = False,
|
|
|
|
|
amp = False,
|
|
|
|
|
step_start_ema = 2000,
|
|
|
|
|
update_ema_every = 10,
|
|
|
|
|
save_and_sample_every = 1000,
|
|
|
|
@@ -531,11 +523,8 @@ class Trainer(object):
|
|
|
|
|
|
|
|
|
|
self.step = 0
|
|
|
|
|
|
|
|
|
|
assert not fp16 or fp16 and APEX_AVAILABLE, 'Apex must be installed in order for mixed precision training to be turned on'
|
|
|
|
|
|
|
|
|
|
self.fp16 = fp16
|
|
|
|
|
if fp16:
|
|
|
|
|
(self.model, self.ema_model), self.opt = amp.initialize([self.model, self.ema_model], self.opt, opt_level='O1')
|
|
|
|
|
self.amp = amp
|
|
|
|
|
self.scaler = GradScaler(enabled = amp)
|
|
|
|
|
|
|
|
|
|
self.results_folder = Path(results_folder)
|
|
|
|
|
self.results_folder.mkdir(exist_ok = True)
|
|
|
|
@@ -555,7 +544,8 @@ class Trainer(object):
|
|
|
|
|
data = {
|
|
|
|
|
'step': self.step,
|
|
|
|
|
'model': self.model.state_dict(),
|
|
|
|
|
'ema': self.ema_model.state_dict()
|
|
|
|
|
'ema': self.ema_model.state_dict(),
|
|
|
|
|
'scaler': self.scaler.state_dict()
|
|
|
|
|
}
|
|
|
|
|
torch.save(data, str(self.results_folder / f'model-{milestone}.pt'))
|
|
|
|
|
|
|
|
|
@@ -565,18 +555,21 @@ class Trainer(object):
|
|
|
|
|
self.step = data['step']
|
|
|
|
|
self.model.load_state_dict(data['model'])
|
|
|
|
|
self.ema_model.load_state_dict(data['ema'])
|
|
|
|
|
self.scaler.load_state_dict(data['scaler'])
|
|
|
|
|
|
|
|
|
|
def train(self):
|
|
|
|
|
backwards = partial(loss_backwards, self.fp16)
|
|
|
|
|
|
|
|
|
|
while self.step < self.train_num_steps:
|
|
|
|
|
for i in range(self.gradient_accumulate_every):
|
|
|
|
|
data = next(self.dl).cuda()
|
|
|
|
|
loss = self.model(data)
|
|
|
|
|
print(f'{self.step}: {loss.item()}')
|
|
|
|
|
backwards(loss / self.gradient_accumulate_every, self.opt)
|
|
|
|
|
|
|
|
|
|
self.opt.step()
|
|
|
|
|
with autocast(enabled = self.amp):
|
|
|
|
|
loss = self.model(data)
|
|
|
|
|
self.scaler.scale(loss / self.gradient_accumulate_every).backward()
|
|
|
|
|
|
|
|
|
|
print(f'{self.step}: {loss.item()}')
|
|
|
|
|
|
|
|
|
|
self.scaler.step(self.opt)
|
|
|
|
|
self.scaler.update()
|
|
|
|
|
self.opt.zero_grad()
|
|
|
|
|
|
|
|
|
|
if self.step % self.update_ema_every == 0:
|
|
|
|
|