Compare commits

..
5 Commits
Author SHA1 Message Date
Phil Wang 1b85379d3a add clamping option to elucidated diffusion 2022-06-29 08:32:10 -07:00
Phil Wang 8859864f63 patch 2022-06-29 07:55:11 -07:00
Phil Wang 32657f035f Merge pull request #52 from AryaAftab/patch-1
Solve issue #26
2022-06-29 07:54:48 -07:00
Arya Aftab d97bc0278c Solve issue #26 2022-06-29 11:42:54 +04:30
Phil Wang 8408775cfc fix bug in elucidating sampling 2022-06-28 17:52:02 -07:00
3 changed files with 18 additions and 8 deletions
@@ -15,12 +15,17 @@ from torch.optim import Adam
from torchvision import transforms, utils from torchvision import transforms, utils
from PIL import Image from PIL import Image
from tqdm import tqdm
from einops import rearrange, reduce from einops import rearrange, reduce
from einops.layers.torch import Rearrange from einops.layers.torch import Rearrange
from ema_pytorch import EMA from ema_pytorch import EMA
import sys
if 'ipykernel' in sys.modules:
from tqdm.notebook import tqdm
else:
from tqdm import tqdm
# helpers functions # helpers functions
def exists(x): def exists(x):
@@ -122,7 +122,7 @@ class ElucidatedDiffusion(nn.Module):
# preconditioned network output # preconditioned network output
# equation (7) in the paper # equation (7) in the paper
def preconditioned_network_forward(self, noised_images, sigma): def preconditioned_network_forward(self, noised_images, sigma, clamp = False):
batch, device = noised_images.shape[0], noised_images.device batch, device = noised_images.shape[0], noised_images.device
if isinstance(sigma, float): if isinstance(sigma, float):
@@ -135,12 +135,17 @@ class ElucidatedDiffusion(nn.Module):
self.c_noise(sigma) self.c_noise(sigma)
) )
return self.c_skip(padded_sigma) * noised_images + self.c_out(padded_sigma) * net_out out = self.c_skip(padded_sigma) * noised_images + self.c_out(padded_sigma) * net_out
if clamp:
out = out.clamp(-1., 1.)
return out
# sampling # sampling
@torch.no_grad() @torch.no_grad()
def sample(self, batch_size = 16, num_sample_steps = None): def sample(self, batch_size = 16, num_sample_steps = None, clamp = True):
num_sample_steps = default(num_sample_steps, self.num_sample_steps) num_sample_steps = default(num_sample_steps, self.num_sample_steps)
shape = (batch_size, self.channels, self.image_size, self.image_size) shape = (batch_size, self.channels, self.image_size, self.image_size)
@@ -168,12 +173,12 @@ class ElucidatedDiffusion(nn.Module):
for sigma, sigma_next, gamma in tqdm(sigmas_and_gammas, desc = 'sampling time step'): for sigma, sigma_next, gamma in tqdm(sigmas_and_gammas, desc = 'sampling time step'):
sigma, sigma_next, gamma = map(lambda t: t.item(), (sigma, sigma_next, gamma)) sigma, sigma_next, gamma = map(lambda t: t.item(), (sigma, sigma_next, gamma))
eps = gamma * torch.randn(shape, device = self.device) eps = self.S_noise * torch.randn(shape, device = self.device) # stochastic sampling
sigma_hat = sigma + gamma * sigma sigma_hat = sigma + gamma * sigma
images_hat = images + sqrt(sigma_hat ** 2 - sigma ** 2) * eps images_hat = images + sqrt(sigma_hat ** 2 - sigma ** 2) * eps
model_output = self.preconditioned_network_forward(images_hat, sigma_hat) model_output = self.preconditioned_network_forward(images_hat, sigma_hat, clamp = clamp)
denoised_over_sigma = (images_hat - model_output) / sigma_hat denoised_over_sigma = (images_hat - model_output) / sigma_hat
images_next = images_hat + (sigma_next - sigma_hat) * denoised_over_sigma images_next = images_hat + (sigma_next - sigma_hat) * denoised_over_sigma
@@ -181,7 +186,7 @@ class ElucidatedDiffusion(nn.Module):
# second order correction, if not the last timestep # second order correction, if not the last timestep
if sigma_next != 0: if sigma_next != 0:
model_output_next = self.preconditioned_network_forward(images_next, sigma_next) model_output_next = self.preconditioned_network_forward(images_next, sigma_next, clamp = clamp)
denoised_prime_over_sigma = (images_next - model_output_next) / sigma_next denoised_prime_over_sigma = (images_next - model_output_next) / sigma_next
images_next = images_hat + 0.5 * (sigma_next - sigma_hat) * (denoised_over_sigma + denoised_prime_over_sigma) images_next = images_hat + 0.5 * (sigma_next - sigma_hat) * (denoised_over_sigma + denoised_prime_over_sigma)
+1 -1
View File
@@ -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.23.0', version = '0.23.3',
license='MIT', license='MIT',
description = 'Denoising Diffusion Probabilistic Models - Pytorch', description = 'Denoising Diffusion Probabilistic Models - Pytorch',
author = 'Phil Wang', author = 'Phil Wang',