mirror of
https://github.com/wassname/denoising-diffusion-pytorch.git
synced 2026-09-11 12:11:43 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f4615599bc | ||
|
|
eb6e1b508e |
@@ -95,7 +95,7 @@ def Upsample(dim):
|
|||||||
return nn.ConvTranspose2d(dim, dim, 4, 2, 1)
|
return nn.ConvTranspose2d(dim, dim, 4, 2, 1)
|
||||||
|
|
||||||
def Downsample(dim):
|
def Downsample(dim):
|
||||||
return nn.Conv2d(dim, dim, 3, 2, 1)
|
return nn.Conv2d(dim, dim, 4, 2, 1)
|
||||||
|
|
||||||
class LayerNorm(nn.Module):
|
class LayerNorm(nn.Module):
|
||||||
def __init__(self, dim, eps = 1e-5):
|
def __init__(self, dim, eps = 1e-5):
|
||||||
@@ -135,10 +135,9 @@ class ConvNextBlock(nn.Module):
|
|||||||
|
|
||||||
self.net = nn.Sequential(
|
self.net = nn.Sequential(
|
||||||
LayerNorm(dim) if norm else nn.Identity(),
|
LayerNorm(dim) if norm else nn.Identity(),
|
||||||
nn.Conv2d(dim, dim_out * mult, 1),
|
nn.Conv2d(dim, dim_out * mult, 3, padding = 1),
|
||||||
nn.GELU(),
|
nn.GELU(),
|
||||||
LayerNorm(dim_out * mult),
|
nn.Conv2d(dim_out * mult, dim_out, 3, padding = 1)
|
||||||
nn.Conv2d(dim_out * mult, dim_out, 1)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
self.res_conv = nn.Conv2d(dim, dim_out, 1) if dim != dim_out else nn.Identity()
|
self.res_conv = nn.Conv2d(dim, dim_out, 1) if dim != dim_out else nn.Identity()
|
||||||
@@ -176,6 +175,29 @@ class LinearAttention(nn.Module):
|
|||||||
out = rearrange(out, 'b h c (x y) -> b (h c) x y', h = self.heads, x = h, y = w)
|
out = rearrange(out, 'b h c (x y) -> b (h c) x y', h = self.heads, x = h, y = w)
|
||||||
return self.to_out(out)
|
return self.to_out(out)
|
||||||
|
|
||||||
|
class Attention(nn.Module):
|
||||||
|
def __init__(self, dim, heads = 4, dim_head = 32):
|
||||||
|
super().__init__()
|
||||||
|
self.scale = dim_head ** -0.5
|
||||||
|
self.heads = heads
|
||||||
|
hidden_dim = dim_head * heads
|
||||||
|
self.to_qkv = nn.Conv2d(dim, hidden_dim * 3, 1, bias = False)
|
||||||
|
self.to_out = nn.Conv2d(hidden_dim, dim, 1)
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
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()
|
||||||
|
attn = sim.softmax(dim = -1)
|
||||||
|
|
||||||
|
out = einsum('b h i j, b h d j -> b h i d', attn, v)
|
||||||
|
out = rearrange(out, 'b h (x y) d -> b (h d) x y', x = h, y = w)
|
||||||
|
return self.to_out(out)
|
||||||
|
|
||||||
# model
|
# model
|
||||||
|
|
||||||
class Unet(nn.Module):
|
class Unet(nn.Module):
|
||||||
@@ -221,7 +243,7 @@ class Unet(nn.Module):
|
|||||||
|
|
||||||
mid_dim = dims[-1]
|
mid_dim = dims[-1]
|
||||||
self.mid_block1 = ConvNextBlock(mid_dim, mid_dim, time_emb_dim = time_dim)
|
self.mid_block1 = ConvNextBlock(mid_dim, mid_dim, time_emb_dim = time_dim)
|
||||||
self.mid_attn = Residual(PreNorm(mid_dim, LinearAttention(mid_dim)))
|
self.mid_attn = Residual(PreNorm(mid_dim, Attention(mid_dim)))
|
||||||
self.mid_block2 = ConvNextBlock(mid_dim, mid_dim, time_emb_dim = time_dim)
|
self.mid_block2 = ConvNextBlock(mid_dim, mid_dim, time_emb_dim = time_dim)
|
||||||
|
|
||||||
for ind, (dim_in, dim_out) in enumerate(reversed(in_out[1:])):
|
for ind, (dim_in, dim_out) in enumerate(reversed(in_out[1:])):
|
||||||
|
|||||||
@@ -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.7.0',
|
version = '0.8.0',
|
||||||
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