Skip to content

Commit f2765c4

Browse files
committed
update with new and improved cosine noise scheduler
1 parent 3d96532 commit f2765c4

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

README.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ model = Unet(
2727

2828
diffusion = GaussianDiffusion(
2929
model,
30-
beta_start = 0.0001,
31-
beta_end = 0.02,
32-
num_diffusion_timesteps = 1000, # number of steps
33-
loss_type = 'l1' # L1 or L2 (wavegrad paper claims l1 is better?)
30+
timesteps = 1000, # number of steps
31+
loss_type = 'l1' # L1 or L2
3432
)
3533

3634
training_images = torch.randn(8, 3, 128, 128)
@@ -54,10 +52,8 @@ model = Unet(
5452

5553
diffusion = GaussianDiffusion(
5654
model,
57-
beta_start = 0.0001,
58-
beta_end = 0.02,
59-
num_diffusion_timesteps = 1000, # number of steps
60-
loss_type = 'l1' # L1 or L2
55+
timesteps = 1000, # number of steps
56+
loss_type = 'l1' # L1 or L2
6157
).cuda()
6258

6359
trainer = Trainer(

denoising_diffusion_pytorch/denoising_diffusion_pytorch.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
SAVE_AND_SAMPLE_EVERY = 1000
2828
UPDATE_EMA_EVERY = 10
29-
EXTS = ['jpg', 'png']
29+
EXTS = ['jpg', 'jpeg', 'png']
3030

3131
# helpers functions
3232

@@ -263,24 +263,36 @@ def noise_like(shape, device, repeat=False):
263263
noise = lambda: torch.randn(shape, device=device)
264264
return repeat_noise() if repeat else noise()
265265

266+
def cosine_beta_schedule(timesteps, s = 0.008):
267+
"""
268+
cosine schedule
269+
as proposed in https://openreview.net/forum?id=-NEXDKk8gZ
270+
"""
271+
steps = timesteps + 1
272+
x = np.linspace(0, steps, steps)
273+
alphas_cumprod = np.cos(((x / steps) + s) / (1 + s) * np.pi * 0.5) ** 2
274+
alphas_cumprod = alphas_cumprod / alphas_cumprod[0]
275+
betas = 1 - (alphas_cumprod[1:] / alphas_cumprod[:-1])
276+
return np.clip(betas, a_min = 0, a_max = 0.999)
277+
266278
class GaussianDiffusion(nn.Module):
267-
def __init__(self, denoise_fn, beta_start=0.0001, beta_end=0.02, num_diffusion_timesteps=1000, loss_type='l1', betas = None):
279+
def __init__(self, denoise_fn, timesteps=1000, loss_type='l1', betas = None):
268280
super().__init__()
269281
self.denoise_fn = denoise_fn
270282

271283
if exists(betas):
272-
self.np_betas = betas.detach().cpu().numpy() if isinstance(betas, torch.Tensor) else betas
284+
betas = betas.detach().cpu().numpy() if isinstance(betas, torch.Tensor) else betas
273285
else:
274-
self.np_betas = betas = np.linspace(beta_start, beta_end, num_diffusion_timesteps).astype(np.float64)
275-
276-
timesteps, = betas.shape
277-
self.num_timesteps = int(timesteps)
278-
self.loss_type = loss_type
286+
betas = cosine_beta_schedule(timesteps)
279287

280288
alphas = 1. - betas
281289
alphas_cumprod = np.cumprod(alphas, axis=0)
282290
alphas_cumprod_prev = np.append(1., alphas_cumprod[:-1])
283291

292+
timesteps, = betas.shape
293+
self.num_timesteps = int(timesteps)
294+
self.loss_type = loss_type
295+
284296
to_torch = partial(torch.tensor, dtype=torch.float32)
285297

286298
self.register_buffer('betas', to_torch(betas))

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
setup(
44
name = 'denoising-diffusion-pytorch',
55
packages = find_packages(),
6-
version = '0.4.0',
6+
version = '0.5.0',
77
license='MIT',
88
description = 'Denoising Diffusion Probabilistic Models - Pytorch',
99
author = 'Phil Wang',

0 commit comments

Comments
 (0)