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 |
@@ -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)
|
||||||
|
|
||||||
|
|||||||
@@ -89,16 +89,15 @@ def Downsample(dim, dim_out = None):
|
|||||||
return nn.Conv2d(dim, default(dim_out, dim), 4, 2, 1)
|
return nn.Conv2d(dim, default(dim_out, dim), 4, 2, 1)
|
||||||
|
|
||||||
class LayerNorm(nn.Module):
|
class LayerNorm(nn.Module):
|
||||||
def __init__(self, dim, eps = 1e-5):
|
def __init__(self, dim):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.eps = eps
|
|
||||||
self.g = nn.Parameter(torch.ones(1, dim, 1, 1))
|
self.g = nn.Parameter(torch.ones(1, dim, 1, 1))
|
||||||
self.b = nn.Parameter(torch.zeros(1, dim, 1, 1))
|
|
||||||
|
|
||||||
def forward(self, x):
|
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)
|
var = torch.var(x, dim = 1, unbiased = False, keepdim = True)
|
||||||
mean = torch.mean(x, dim = 1, 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):
|
class PreNorm(nn.Module):
|
||||||
def __init__(self, dim, fn):
|
def __init__(self, dim, fn):
|
||||||
@@ -211,6 +210,8 @@ class LinearAttention(nn.Module):
|
|||||||
k = k.softmax(dim = -1)
|
k = k.softmax(dim = -1)
|
||||||
|
|
||||||
q = q * self.scale
|
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)
|
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)
|
out = torch.einsum('b h d e, b h d n -> b h e n', context, q)
|
||||||
@@ -479,7 +480,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)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -719,6 +720,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
|
||||||
@@ -739,7 +741,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.26.0',
|
version = '0.26.5',
|
||||||
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