mirror of
https://github.com/wassname/denoising-diffusion-pytorch.git
synced 2026-09-11 12:11:43 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6b504c4ae9 | ||
|
|
37334ae824 | ||
|
|
6eba6cdd50 | ||
|
|
555566c188 |
@@ -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
|
## 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>
|
<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)
|
[](https://badge.fury.io/py/denoising-diffusion-pytorch)
|
||||||
|
|
||||||
|
|||||||
@@ -58,6 +58,9 @@ def convert_image_to(img_type, image):
|
|||||||
return image.convert(img_type)
|
return image.convert(img_type)
|
||||||
return image
|
return image
|
||||||
|
|
||||||
|
def l2norm(t):
|
||||||
|
return F.normalize(t, dim = -1)
|
||||||
|
|
||||||
# normalization functions
|
# normalization functions
|
||||||
|
|
||||||
def normalize_to_neg_one_to_one(img):
|
def normalize_to_neg_one_to_one(img):
|
||||||
@@ -215,9 +218,9 @@ class LinearAttention(nn.Module):
|
|||||||
return self.to_out(out)
|
return self.to_out(out)
|
||||||
|
|
||||||
class Attention(nn.Module):
|
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__()
|
super().__init__()
|
||||||
self.scale = dim_head ** -0.5
|
self.scale = scale
|
||||||
self.heads = heads
|
self.heads = heads
|
||||||
hidden_dim = dim_head * heads
|
hidden_dim = dim_head * heads
|
||||||
self.to_qkv = nn.Conv2d(dim, hidden_dim * 3, 1, bias = False)
|
self.to_qkv = nn.Conv2d(dim, hidden_dim * 3, 1, bias = False)
|
||||||
@@ -227,10 +230,10 @@ class Attention(nn.Module):
|
|||||||
b, c, h, w = x.shape
|
b, c, h, w = x.shape
|
||||||
qkv = self.to_qkv(x).chunk(3, dim = 1)
|
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, 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)
|
q, k = map(l2norm, (q, k))
|
||||||
sim = sim - sim.amax(dim = -1, keepdim = True).detach()
|
|
||||||
|
sim = einsum('b h d i, b h d j -> b h i j', q, k) * self.scale
|
||||||
attn = sim.softmax(dim = -1)
|
attn = sim.softmax(dim = -1)
|
||||||
|
|
||||||
out = einsum('b h i j, b h d j -> b h i d', attn, v)
|
out = einsum('b h i j, b h d j -> b h i d', attn, v)
|
||||||
@@ -476,7 +479,7 @@ class GaussianDiffusion(nn.Module):
|
|||||||
|
|
||||||
def predict_noise_from_start(self, x_t, t, x0):
|
def predict_noise_from_start(self, x_t, t, x0):
|
||||||
return (
|
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)
|
extract(self.sqrt_recipm1_alphas_cumprod, t, x_t.shape)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -716,6 +719,7 @@ class Trainer(object):
|
|||||||
self.ds = Dataset(folder, self.image_size, augment_horizontal_flip = augment_horizontal_flip, convert_image_to = convert_image_to)
|
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 = 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)
|
self.dl = cycle(dl)
|
||||||
|
|
||||||
# optimizer
|
# optimizer
|
||||||
@@ -736,7 +740,7 @@ class Trainer(object):
|
|||||||
|
|
||||||
# prepare model, dataloader, optimizer with accelerator
|
# 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):
|
def save(self, milestone):
|
||||||
if not self.accelerator.is_local_main_process:
|
if not self.accelerator.is_local_main_process:
|
||||||
|
|||||||
|
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(
|
setup(
|
||||||
name = 'denoising-diffusion-pytorch',
|
name = 'denoising-diffusion-pytorch',
|
||||||
packages = find_packages(),
|
packages = find_packages(),
|
||||||
version = '0.25.3',
|
version = '0.26.3',
|
||||||
license='MIT',
|
license='MIT',
|
||||||
description = 'Denoising Diffusion Probabilistic Models - Pytorch',
|
description = 'Denoising Diffusion Probabilistic Models - Pytorch',
|
||||||
author = 'Phil Wang',
|
author = 'Phil Wang',
|
||||||
|
|||||||
Reference in New Issue
Block a user