diff --git a/denoising_diffusion_pytorch/denoising_diffusion_pytorch.py b/denoising_diffusion_pytorch/denoising_diffusion_pytorch.py index 974dc83..8477c3a 100644 --- a/denoising_diffusion_pytorch/denoising_diffusion_pytorch.py +++ b/denoising_diffusion_pytorch/denoising_diffusion_pytorch.py @@ -118,20 +118,27 @@ class PreNorm(nn.Module): class Block(nn.Module): def __init__(self, dim, dim_out, groups = 8): super().__init__() - self.block = nn.Sequential( - nn.Conv2d(dim, dim_out, 3, padding = 1), - nn.GroupNorm(groups, dim_out), - nn.SiLU() - ) - def forward(self, x): - return self.block(x) + self.proj = nn.Conv2d(dim, dim_out, 3, padding = 1) + self.norm = nn.GroupNorm(groups, dim_out) + self.act = nn.SiLU() + + def forward(self, x, scale_shift = None): + x = self.proj(x) + x = self.norm(x) + + if exists(scale_shift): + scale, shift = scale_shift + x = x * (scale + 1) + shift + + x = self.act(x) + return x class ResnetBlock(nn.Module): def __init__(self, dim, dim_out, *, time_emb_dim = None, groups = 8): super().__init__() self.mlp = nn.Sequential( nn.SiLU(), - nn.Linear(time_emb_dim, dim_out) + nn.Linear(time_emb_dim, dim_out * 2) ) if exists(time_emb_dim) else None self.block1 = Block(dim, dim_out, groups = groups) @@ -139,11 +146,14 @@ class ResnetBlock(nn.Module): self.res_conv = nn.Conv2d(dim, dim_out, 1) if dim != dim_out else nn.Identity() def forward(self, x, time_emb = None): - h = self.block1(x) + scale_shift = None if exists(self.mlp) and exists(time_emb): time_emb = self.mlp(time_emb) - h = rearrange(time_emb, 'b c -> b c 1 1') + h + time_emb = rearrange(time_emb, 'b c -> b c 1 1') + scale_shift = time_emb.chunk(2, dim = 1) + + h = self.block1(x, scale_shift = scale_shift) h = self.block2(h) return h + self.res_conv(x) diff --git a/setup.py b/setup.py index 4e68f67..8f4653d 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ from setuptools import setup, find_packages setup( name = 'denoising-diffusion-pytorch', packages = find_packages(), - version = '0.15.7', + version = '0.16.0', license='MIT', description = 'Denoising Diffusion Probabilistic Models - Pytorch', author = 'Phil Wang',