mirror of
https://github.com/wassname/denoising-diffusion-pytorch.git
synced 2026-09-10 12:01:08 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b4fb8804d2 | ||
|
|
9fd05f1b1f |
@@ -125,7 +125,7 @@ class ContinuousTimeGaussianDiffusion(nn.Module):
|
|||||||
p2_loss_weight_k = 1
|
p2_loss_weight_k = 1
|
||||||
):
|
):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
assert not denoise_fn.sinusoidal_cond_mlp
|
assert denoise_fn.learned_sinusoidal_cond
|
||||||
|
|
||||||
self.denoise_fn = denoise_fn
|
self.denoise_fn = denoise_fn
|
||||||
|
|
||||||
|
|||||||
@@ -73,20 +73,6 @@ class Residual(nn.Module):
|
|||||||
def forward(self, x, *args, **kwargs):
|
def forward(self, x, *args, **kwargs):
|
||||||
return self.fn(x, *args, **kwargs) + x
|
return self.fn(x, *args, **kwargs) + x
|
||||||
|
|
||||||
class SinusoidalPosEmb(nn.Module):
|
|
||||||
def __init__(self, dim):
|
|
||||||
super().__init__()
|
|
||||||
self.dim = dim
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
device = x.device
|
|
||||||
half_dim = self.dim // 2
|
|
||||||
emb = math.log(10000) / (half_dim - 1)
|
|
||||||
emb = torch.exp(torch.arange(half_dim, device=device) * -emb)
|
|
||||||
emb = x[:, None] * emb[None, :]
|
|
||||||
emb = torch.cat((emb.sin(), emb.cos()), dim=-1)
|
|
||||||
return emb
|
|
||||||
|
|
||||||
def Upsample(dim):
|
def Upsample(dim):
|
||||||
return nn.ConvTranspose2d(dim, dim, 4, 2, 1)
|
return nn.ConvTranspose2d(dim, dim, 4, 2, 1)
|
||||||
|
|
||||||
@@ -115,6 +101,39 @@ class PreNorm(nn.Module):
|
|||||||
x = self.norm(x)
|
x = self.norm(x)
|
||||||
return self.fn(x)
|
return self.fn(x)
|
||||||
|
|
||||||
|
# sinusoidal positional embeds
|
||||||
|
|
||||||
|
class SinusoidalPosEmb(nn.Module):
|
||||||
|
def __init__(self, dim):
|
||||||
|
super().__init__()
|
||||||
|
self.dim = dim
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
device = x.device
|
||||||
|
half_dim = self.dim // 2
|
||||||
|
emb = math.log(10000) / (half_dim - 1)
|
||||||
|
emb = torch.exp(torch.arange(half_dim, device=device) * -emb)
|
||||||
|
emb = x[:, None] * emb[None, :]
|
||||||
|
emb = torch.cat((emb.sin(), emb.cos()), dim=-1)
|
||||||
|
return emb
|
||||||
|
|
||||||
|
class LearnedSinusoidalPosEmb(nn.Module):
|
||||||
|
""" following @crowsonkb 's lead with learned sinusoidal pos emb """
|
||||||
|
""" https://github.com/crowsonkb/v-diffusion-jax/blob/master/diffusion/models/danbooru_128.py#L8 """
|
||||||
|
|
||||||
|
def __init__(self, dim):
|
||||||
|
super().__init__()
|
||||||
|
assert (dim % 2) == 0
|
||||||
|
half_dim = dim // 2
|
||||||
|
self.weights = nn.Parameter(torch.randn(half_dim))
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
x = rearrange(x, 'b -> b 1')
|
||||||
|
freqs = x * rearrange(self.weights, 'd -> 1 d') * 2 * math.pi
|
||||||
|
fouriered = torch.cat((freqs.sin(), freqs.cos()), dim = -1)
|
||||||
|
fouriered = torch.cat((x, fouriered), dim = -1)
|
||||||
|
return fouriered
|
||||||
|
|
||||||
# building block modules
|
# building block modules
|
||||||
|
|
||||||
class Block(nn.Module):
|
class Block(nn.Module):
|
||||||
@@ -158,6 +177,7 @@ class ResnetBlock(nn.Module):
|
|||||||
h = self.block1(x, scale_shift = scale_shift)
|
h = self.block1(x, scale_shift = scale_shift)
|
||||||
|
|
||||||
h = self.block2(h)
|
h = self.block2(h)
|
||||||
|
|
||||||
return h + self.res_conv(x)
|
return h + self.res_conv(x)
|
||||||
|
|
||||||
class LinearAttention(nn.Module):
|
class LinearAttention(nn.Module):
|
||||||
@@ -213,18 +233,6 @@ class Attention(nn.Module):
|
|||||||
|
|
||||||
# model
|
# model
|
||||||
|
|
||||||
def MLP(dim_in, dim_hidden):
|
|
||||||
return nn.Sequential(
|
|
||||||
Rearrange('... -> ... 1'),
|
|
||||||
nn.Linear(1, dim_hidden),
|
|
||||||
nn.GELU(),
|
|
||||||
nn.LayerNorm(dim_hidden),
|
|
||||||
nn.Linear(dim_hidden, dim_hidden),
|
|
||||||
nn.GELU(),
|
|
||||||
nn.LayerNorm(dim_hidden),
|
|
||||||
nn.Linear(dim_hidden, dim_hidden)
|
|
||||||
)
|
|
||||||
|
|
||||||
class Unet(nn.Module):
|
class Unet(nn.Module):
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@@ -235,7 +243,8 @@ class Unet(nn.Module):
|
|||||||
channels = 3,
|
channels = 3,
|
||||||
resnet_block_groups = 8,
|
resnet_block_groups = 8,
|
||||||
learned_variance = False,
|
learned_variance = False,
|
||||||
sinusoidal_cond_mlp = True
|
learned_sinusoidal_cond = False,
|
||||||
|
learned_sinusoidal_dim = 16
|
||||||
):
|
):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
@@ -255,17 +264,21 @@ class Unet(nn.Module):
|
|||||||
|
|
||||||
time_dim = dim * 4
|
time_dim = dim * 4
|
||||||
|
|
||||||
self.sinusoidal_cond_mlp = sinusoidal_cond_mlp
|
self.learned_sinusoidal_cond = learned_sinusoidal_cond
|
||||||
|
|
||||||
if sinusoidal_cond_mlp:
|
if learned_sinusoidal_cond:
|
||||||
self.time_mlp = nn.Sequential(
|
sinu_pos_emb = LearnedSinusoidalPosEmb(learned_sinusoidal_dim)
|
||||||
SinusoidalPosEmb(dim),
|
fourier_dim = learned_sinusoidal_dim + 1
|
||||||
nn.Linear(dim, time_dim),
|
|
||||||
nn.GELU(),
|
|
||||||
nn.Linear(time_dim, time_dim)
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
self.time_mlp = MLP(1, time_dim)
|
sinu_pos_emb = SinusoidalPosEmb(dim)
|
||||||
|
fourier_dim = dim
|
||||||
|
|
||||||
|
self.time_mlp = nn.Sequential(
|
||||||
|
sinu_pos_emb,
|
||||||
|
nn.Linear(fourier_dim, time_dim),
|
||||||
|
nn.GELU(),
|
||||||
|
nn.Linear(time_dim, time_dim)
|
||||||
|
)
|
||||||
|
|
||||||
# layers
|
# layers
|
||||||
|
|
||||||
@@ -301,10 +314,8 @@ class Unet(nn.Module):
|
|||||||
default_out_dim = channels * (1 if not learned_variance else 2)
|
default_out_dim = channels * (1 if not learned_variance else 2)
|
||||||
self.out_dim = default(out_dim, default_out_dim)
|
self.out_dim = default(out_dim, default_out_dim)
|
||||||
|
|
||||||
self.final_conv = nn.Sequential(
|
self.final_res_block = block_klass(dim * 2, dim, time_emb_dim = time_dim)
|
||||||
block_klass(dim * 2, dim),
|
self.final_conv = nn.Conv2d(dim, self.out_dim, 1)
|
||||||
nn.Conv2d(dim, self.out_dim, 1)
|
|
||||||
)
|
|
||||||
|
|
||||||
def forward(self, x, time):
|
def forward(self, x, time):
|
||||||
x = self.init_conv(x)
|
x = self.init_conv(x)
|
||||||
@@ -333,6 +344,8 @@ class Unet(nn.Module):
|
|||||||
x = upsample(x)
|
x = upsample(x)
|
||||||
|
|
||||||
x = torch.cat((x, r), dim = 1)
|
x = torch.cat((x, r), dim = 1)
|
||||||
|
|
||||||
|
x = self.final_res_block(x, t)
|
||||||
return self.final_conv(x)
|
return self.final_conv(x)
|
||||||
|
|
||||||
# gaussian diffusion trainer class
|
# gaussian diffusion trainer class
|
||||||
|
|||||||
@@ -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.19.2',
|
version = '0.20.1',
|
||||||
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