mirror of
https://github.com/wassname/denoising-diffusion-pytorch.git
synced 2026-09-10 12:01:08 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eba44498d1 | ||
|
|
12f95b33d8 | ||
|
|
6b504c4ae9 | ||
|
|
37334ae824 | ||
|
|
6eba6cdd50 | ||
|
|
555566c188 | ||
|
|
2b742dd2cc |
@@ -1,4 +1,4 @@
|
||||
<img src="./denoising-diffusion.png" width="500px"></img>
|
||||
<img src="./images/denoising-diffusion.png" width="500px"></img>
|
||||
|
||||
## Denoising Diffusion Probabilistic Model, in Pytorch
|
||||
|
||||
@@ -10,7 +10,7 @@ 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>
|
||||
|
||||
<img src="./sample.png" width="500px"><img>
|
||||
<img src="./images/sample.png" width="500px"><img>
|
||||
|
||||
[](https://badge.fury.io/py/denoising-diffusion-pytorch)
|
||||
|
||||
@@ -69,7 +69,7 @@ trainer = Trainer(
|
||||
diffusion,
|
||||
'path/to/your/images',
|
||||
train_batch_size = 32,
|
||||
train_lr = 1e-4,
|
||||
train_lr = 8e-5,
|
||||
train_num_steps = 700000, # total training steps
|
||||
gradient_accumulate_every = 2, # gradient accumulation steps
|
||||
ema_decay = 0.995, # exponential moving average decay
|
||||
|
||||
@@ -58,6 +58,9 @@ def convert_image_to(img_type, image):
|
||||
return image.convert(img_type)
|
||||
return image
|
||||
|
||||
def l2norm(t):
|
||||
return F.normalize(t, dim = -1)
|
||||
|
||||
# normalization functions
|
||||
|
||||
def normalize_to_neg_one_to_one(img):
|
||||
@@ -86,16 +89,15 @@ 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):
|
||||
def __init__(self, dim):
|
||||
super().__init__()
|
||||
self.eps = eps
|
||||
self.g = nn.Parameter(torch.ones(1, dim, 1, 1))
|
||||
self.b = nn.Parameter(torch.zeros(1, dim, 1, 1))
|
||||
|
||||
def forward(self, x):
|
||||
eps = 1e-5 if x.dtype == torch.float32 else 1e-3
|
||||
var = torch.var(x, dim = 1, unbiased = False, keepdim = True)
|
||||
mean = torch.mean(x, dim = 1, keepdim = True)
|
||||
return (x - mean) / (var + self.eps).sqrt() * self.g + self.b
|
||||
return (x - mean) * (var + eps).rsqrt() * self.g
|
||||
|
||||
class PreNorm(nn.Module):
|
||||
def __init__(self, dim, fn):
|
||||
@@ -208,6 +210,8 @@ class LinearAttention(nn.Module):
|
||||
k = k.softmax(dim = -1)
|
||||
|
||||
q = q * self.scale
|
||||
v = v / (h * w)
|
||||
|
||||
context = torch.einsum('b h d n, b h e n -> b h d e', k, v)
|
||||
|
||||
out = torch.einsum('b h d e, b h d n -> b h e n', context, q)
|
||||
@@ -215,9 +219,9 @@ class LinearAttention(nn.Module):
|
||||
return self.to_out(out)
|
||||
|
||||
class Attention(nn.Module):
|
||||
def __init__(self, dim, heads = 4, dim_head = 32):
|
||||
def __init__(self, dim, heads = 4, dim_head = 32, scale = 16):
|
||||
super().__init__()
|
||||
self.scale = dim_head ** -0.5
|
||||
self.scale = scale
|
||||
self.heads = heads
|
||||
hidden_dim = dim_head * heads
|
||||
self.to_qkv = nn.Conv2d(dim, hidden_dim * 3, 1, bias = False)
|
||||
@@ -227,10 +231,10 @@ class Attention(nn.Module):
|
||||
b, c, h, w = x.shape
|
||||
qkv = self.to_qkv(x).chunk(3, dim = 1)
|
||||
q, k, v = map(lambda t: rearrange(t, 'b (h c) x y -> b h c (x y)', h = self.heads), qkv)
|
||||
q = q * self.scale
|
||||
|
||||
sim = einsum('b h d i, b h d j -> b h i j', q, k)
|
||||
sim = sim - sim.amax(dim = -1, keepdim = True).detach()
|
||||
q, k = map(l2norm, (q, k))
|
||||
|
||||
sim = einsum('b h d i, b h d j -> b h i j', q, k) * self.scale
|
||||
attn = sim.softmax(dim = -1)
|
||||
|
||||
out = einsum('b h i j, b h d j -> b h i d', attn, v)
|
||||
@@ -476,7 +480,7 @@ class GaussianDiffusion(nn.Module):
|
||||
|
||||
def predict_noise_from_start(self, x_t, t, x0):
|
||||
return (
|
||||
(x0 - extract(self.sqrt_recip_alphas_cumprod, t, x_t.shape) * x_t) / \
|
||||
(extract(self.sqrt_recip_alphas_cumprod, t, x_t.shape) * x_t - x0) / \
|
||||
extract(self.sqrt_recipm1_alphas_cumprod, t, x_t.shape)
|
||||
)
|
||||
|
||||
@@ -681,6 +685,7 @@ class Trainer(object):
|
||||
train_num_steps = 100000,
|
||||
ema_update_every = 10,
|
||||
ema_decay = 0.995,
|
||||
adam_betas = (0.9, 0.99),
|
||||
save_and_sample_every = 1000,
|
||||
num_samples = 25,
|
||||
results_folder = './results',
|
||||
@@ -715,11 +720,12 @@ class Trainer(object):
|
||||
self.ds = Dataset(folder, self.image_size, augment_horizontal_flip = augment_horizontal_flip, convert_image_to = convert_image_to)
|
||||
dl = DataLoader(self.ds, batch_size = train_batch_size, shuffle = True, pin_memory = True, num_workers = cpu_count())
|
||||
|
||||
dl = self.accelerator.prepare(dl)
|
||||
self.dl = cycle(dl)
|
||||
|
||||
# optimizer
|
||||
|
||||
self.opt = Adam(diffusion_model.parameters(), lr = train_lr)
|
||||
self.opt = Adam(diffusion_model.parameters(), lr = train_lr, betas = adam_betas)
|
||||
|
||||
# for logging results in a folder periodically
|
||||
|
||||
@@ -735,7 +741,7 @@ class Trainer(object):
|
||||
|
||||
# prepare model, dataloader, optimizer with accelerator
|
||||
|
||||
self.model, self.dl, self.opt = self.accelerator.prepare(self.model, self.dl, self.opt)
|
||||
self.model, self.opt = self.accelerator.prepare(self.model, self.opt)
|
||||
|
||||
def save(self, milestone):
|
||||
if not self.accelerator.is_local_main_process:
|
||||
@@ -772,14 +778,19 @@ class Trainer(object):
|
||||
|
||||
while self.step < self.train_num_steps:
|
||||
|
||||
total_loss = 0.
|
||||
|
||||
for _ in range(self.gradient_accumulate_every):
|
||||
data = next(self.dl).to(device)
|
||||
|
||||
with self.accelerator.autocast():
|
||||
loss = self.model(data)
|
||||
self.accelerator.backward(loss / self.gradient_accumulate_every)
|
||||
loss = loss / self.gradient_accumulate_every
|
||||
total_loss += loss.item()
|
||||
|
||||
pbar.set_description(f'loss: {loss.item():.4f}')
|
||||
self.accelerator.backward(loss)
|
||||
|
||||
pbar.set_description(f'loss: {total_loss:.4f}')
|
||||
|
||||
accelerator.wait_for_everyone()
|
||||
|
||||
|
||||
|
Before Width: | Height: | Size: 40 KiB After Width: | Height: | Size: 40 KiB |
|
Before Width: | Height: | Size: 842 KiB After Width: | Height: | Size: 842 KiB |
@@ -3,7 +3,7 @@ from setuptools import setup, find_packages
|
||||
setup(
|
||||
name = 'denoising-diffusion-pytorch',
|
||||
packages = find_packages(),
|
||||
version = '0.25.2',
|
||||
version = '0.26.5',
|
||||
license='MIT',
|
||||
description = 'Denoising Diffusion Probabilistic Models - Pytorch',
|
||||
author = 'Phil Wang',
|
||||
|
||||
Reference in New Issue
Block a user