Compare commits

..
1 Commits
3 changed files with 28 additions and 34 deletions
-2
View File
@@ -10,8 +10,6 @@ Youtube AI Educators - <a href="https://www.youtube.com/watch?v=W-O7AZNzbzQ">Yan
<a href="https://huggingface.co/blog/annotated-diffusion">Annotated code</a> by Research Scientists / Engineers from <a href="https://huggingface.co/">🤗 Huggingface</a> <a href="https://huggingface.co/blog/annotated-diffusion">Annotated code</a> by Research Scientists / Engineers from <a href="https://huggingface.co/">🤗 Huggingface</a>
Update: Turns out none of the technicalities really matters at all | <a href="https://arxiv.org/abs/2208.09392">"Cold Diffusion" paper</a>
<img src="./images/sample.png" width="500px"><img> <img src="./images/sample.png" width="500px"><img>
[![PyPI version](https://badge.fury.io/py/denoising-diffusion-pytorch.svg)](https://badge.fury.io/py/denoising-diffusion-pytorch) [![PyPI version](https://badge.fury.io/py/denoising-diffusion-pytorch.svg)](https://badge.fury.io/py/denoising-diffusion-pytorch)
@@ -37,9 +37,6 @@ def default(val, d):
return val return val
return d() if callable(d) else d return d() if callable(d) else d
def identity(t, *args, **kwargs):
return t
def cycle(dl): def cycle(dl):
while True: while True:
for data in dl: for data in dl:
@@ -100,11 +97,16 @@ class WeightStandardizedConv2d(nn.Conv2d):
eps = 1e-5 if x.dtype == torch.float32 else 1e-3 eps = 1e-5 if x.dtype == torch.float32 else 1e-3
weight = self.weight weight = self.weight
mean = reduce(weight, 'o ... -> o 1 1 1', 'mean') flattened_weights = rearrange(weight, 'o ... -> o (...)')
var = reduce(weight, 'o ... -> o 1 1 1', partial(torch.var, unbiased = False))
normalized_weight = (weight - mean) * (var + eps).rsqrt()
return F.conv2d(x, normalized_weight, self.bias, self.stride, self.padding, self.dilation, self.groups) mean = reduce(weight, 'o ... -> o 1 1 1', 'mean')
var = torch.var(flattened_weights, dim = -1, unbiased = False)
var = rearrange(var, 'o -> o 1 1 1')
weight = (weight - mean) * (var + eps).rsqrt()
return F.conv2d(x, weight, self.bias, self.stride, self.padding, self.dilation, self.groups)
class LayerNorm(nn.Module): class LayerNorm(nn.Module):
def __init__(self, dim): def __init__(self, dim):
@@ -430,7 +432,6 @@ class GaussianDiffusion(nn.Module):
): ):
super().__init__() super().__init__()
assert not (type(self) == GaussianDiffusion and model.channels != model.out_dim) assert not (type(self) == GaussianDiffusion and model.channels != model.out_dim)
assert not model.learned_sinusoidal_cond
self.model = model self.model = model
self.channels = self.model.channels self.channels = self.model.channels
@@ -520,19 +521,16 @@ class GaussianDiffusion(nn.Module):
posterior_log_variance_clipped = extract(self.posterior_log_variance_clipped, 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 return posterior_mean, posterior_variance, posterior_log_variance_clipped
def model_predictions(self, x, t, x_self_cond = None, clip_x_start = False): def model_predictions(self, x, t, x_self_cond = None):
model_output = self.model(x, t, x_self_cond) model_output = self.model(x, t, x_self_cond)
maybe_clip = partial(torch.clamp, min = -1., max = 1.) if clip_x_start else identity
if self.objective == 'pred_noise': if self.objective == 'pred_noise':
pred_noise = model_output pred_noise = model_output
x_start = self.predict_start_from_noise(x, t, pred_noise) x_start = self.predict_start_from_noise(x, t, model_output)
x_start = maybe_clip(x_start)
elif self.objective == 'pred_x0': elif self.objective == 'pred_x0':
pred_noise = self.predict_noise_from_start(x, t, model_output)
x_start = model_output x_start = model_output
x_start = maybe_clip(x_start)
pred_noise = self.predict_noise_from_start(x, t, x_start)
return ModelPrediction(pred_noise, x_start) return ModelPrediction(pred_noise, x_start)
@@ -574,30 +572,31 @@ class GaussianDiffusion(nn.Module):
def ddim_sample(self, shape, clip_denoised = True): def ddim_sample(self, shape, clip_denoised = True):
batch, device, total_timesteps, sampling_timesteps, eta, objective = shape[0], self.betas.device, self.num_timesteps, self.sampling_timesteps, self.ddim_sampling_eta, self.objective batch, device, total_timesteps, sampling_timesteps, eta, objective = shape[0], self.betas.device, self.num_timesteps, self.sampling_timesteps, self.ddim_sampling_eta, self.objective
times = torch.linspace(-1, total_timesteps - 1, steps=sampling_timesteps + 1) # [-1, 0, 1, 2, ..., T-1] when sampling_timesteps == total_timesteps times = torch.linspace(0., total_timesteps, steps = sampling_timesteps + 2)[:-1]
times = list(reversed(times.int().tolist())) times = list(reversed(times.int().tolist()))
time_pairs = list(zip(times[:-1], times[1:])) # [(T-1, T-2), (T-2, T-3), ..., (1, 0), (0, -1)] time_pairs = list(zip(times[:-1], times[1:]))
img = torch.randn(shape, device = device) img = torch.randn(shape, device = device)
x_start = None x_start = None
for time, time_next in tqdm(time_pairs, desc = 'sampling loop time step'): for time, time_next in tqdm(time_pairs, desc = 'sampling loop time step'):
time_cond = torch.full((batch,), time, device=device, dtype=torch.long) alpha = self.alphas_cumprod_prev[time]
alpha_next = self.alphas_cumprod_prev[time_next]
time_cond = torch.full((batch,), time, device = device, dtype = torch.long)
self_cond = x_start if self.self_condition else None self_cond = x_start if self.self_condition else None
pred_noise, x_start, *_ = self.model_predictions(img, time_cond, self_cond, clip_x_start = clip_denoised)
if time_next < 0: pred_noise, x_start, *_ = self.model_predictions(img, time_cond, self_cond)
img = x_start
continue
alpha = self.alphas_cumprod[time] if clip_denoised:
alpha_next = self.alphas_cumprod[time_next] x_start.clamp_(-1., 1.)
sigma = eta * ((1 - alpha / alpha_next) * (1 - alpha_next) / (1 - alpha)).sqrt() sigma = eta * ((1 - alpha / alpha_next) * (1 - alpha_next) / (1 - alpha)).sqrt()
c = (1 - alpha_next - sigma ** 2).sqrt() c = ((1 - alpha_next) - sigma ** 2).sqrt()
noise = torch.randn_like(img) noise = torch.randn_like(img) if time_next > 0 else 0.
img = x_start * alpha_next.sqrt() + \ img = x_start * alpha_next.sqrt() + \
c * pred_noise + \ c * pred_noise + \
@@ -810,10 +809,7 @@ class Trainer(object):
torch.save(data, str(self.results_folder / f'model-{milestone}.pt')) torch.save(data, str(self.results_folder / f'model-{milestone}.pt'))
def load(self, milestone): def load(self, milestone):
accelerator = self.accelerator data = torch.load(str(self.results_folder / f'model-{milestone}.pt'))
device = accelerator.device
data = torch.load(str(self.results_folder / f'model-{milestone}.pt'), map_location=device)
model = self.accelerator.unwrap_model(self.model) model = self.accelerator.unwrap_model(self.model)
model.load_state_dict(data['model']) model.load_state_dict(data['model'])
@@ -854,7 +850,6 @@ class Trainer(object):
accelerator.wait_for_everyone() accelerator.wait_for_everyone()
self.step += 1
if accelerator.is_main_process: if accelerator.is_main_process:
self.ema.to(device) self.ema.to(device)
self.ema.update() self.ema.update()
@@ -871,6 +866,7 @@ class Trainer(object):
utils.save_image(all_images, str(self.results_folder / f'sample-{milestone}.png'), nrow = int(math.sqrt(self.num_samples))) utils.save_image(all_images, str(self.results_folder / f'sample-{milestone}.png'), nrow = int(math.sqrt(self.num_samples)))
self.save(milestone) self.save(milestone)
self.step += 1
pbar.update(1) pbar.update(1)
accelerator.print('training complete') accelerator.print('training complete')
+2 -2
View File
@@ -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.27.9', version = '0.27.3',
license='MIT', license='MIT',
description = 'Denoising Diffusion Probabilistic Models - Pytorch', description = 'Denoising Diffusion Probabilistic Models - Pytorch',
author = 'Phil Wang', author = 'Phil Wang',
@@ -30,4 +30,4 @@ setup(
'License :: OSI Approved :: MIT License', 'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.6',
], ],
) )