mirror of
https://github.com/wassname/denoising-diffusion-pytorch.git
synced 2026-09-11 12:11:43 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
beb2f2d8dd |
@@ -1,4 +1,5 @@
|
|||||||
from math import sqrt
|
from math import sqrt
|
||||||
|
from random import random
|
||||||
import torch
|
import torch
|
||||||
from torch import nn, einsum
|
from torch import nn, einsum
|
||||||
import torch.nn.functional as F
|
import torch.nn.functional as F
|
||||||
@@ -52,7 +53,7 @@ class ElucidatedDiffusion(nn.Module):
|
|||||||
):
|
):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
assert net.learned_sinusoidal_cond
|
assert net.learned_sinusoidal_cond
|
||||||
assert not net.self_condition, 'not supported yet'
|
self.self_condition = net.self_condition
|
||||||
|
|
||||||
self.net = net
|
self.net = net
|
||||||
|
|
||||||
@@ -100,7 +101,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, clamp = False):
|
def preconditioned_network_forward(self, noised_images, sigma, self_cond = None, 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):
|
||||||
@@ -110,7 +111,8 @@ class ElucidatedDiffusion(nn.Module):
|
|||||||
|
|
||||||
net_out = self.net(
|
net_out = self.net(
|
||||||
self.c_in(padded_sigma) * noised_images,
|
self.c_in(padded_sigma) * noised_images,
|
||||||
self.c_noise(sigma)
|
self.c_noise(sigma),
|
||||||
|
self_cond
|
||||||
)
|
)
|
||||||
|
|
||||||
out = 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
|
||||||
@@ -161,6 +163,10 @@ class ElucidatedDiffusion(nn.Module):
|
|||||||
|
|
||||||
images = init_sigma * torch.randn(shape, device = self.device)
|
images = init_sigma * torch.randn(shape, device = self.device)
|
||||||
|
|
||||||
|
# for self conditioning
|
||||||
|
|
||||||
|
x_start = None
|
||||||
|
|
||||||
# gradually denoise
|
# gradually denoise
|
||||||
|
|
||||||
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'):
|
||||||
@@ -171,7 +177,9 @@ class ElucidatedDiffusion(nn.Module):
|
|||||||
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, clamp = clamp)
|
self_cond = x_start if self.self_condition else None
|
||||||
|
|
||||||
|
model_output = self.preconditioned_network_forward(images_hat, sigma_hat, self_cond, 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
|
||||||
@@ -179,11 +187,14 @@ 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, clamp = clamp)
|
self_cond = model_output if self.self_condition else None
|
||||||
|
|
||||||
|
model_output_next = self.preconditioned_network_forward(images_next, sigma_next, self_cond, 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)
|
||||||
|
|
||||||
images = images_next
|
images = images_next
|
||||||
|
x_start = model_output
|
||||||
|
|
||||||
images = images.clamp(-1., 1.)
|
images = images.clamp(-1., 1.)
|
||||||
return unnormalize_to_zero_to_one(images)
|
return unnormalize_to_zero_to_one(images)
|
||||||
@@ -211,7 +222,15 @@ class ElucidatedDiffusion(nn.Module):
|
|||||||
|
|
||||||
noised_images = images + padded_sigmas * noise # alphas are 1. in the paper
|
noised_images = images + padded_sigmas * noise # alphas are 1. in the paper
|
||||||
|
|
||||||
denoised = self.preconditioned_network_forward(noised_images, sigmas)
|
self_cond = None
|
||||||
|
|
||||||
|
if self.self_condition and random() < 0.5:
|
||||||
|
# from hinton's group's bit diffusion paper
|
||||||
|
with torch.no_grad():
|
||||||
|
self_cond = self.preconditioned_network_forward(noised_images, sigmas)
|
||||||
|
self_cond.detach_()
|
||||||
|
|
||||||
|
denoised = self.preconditioned_network_forward(noised_images, sigmas, self_cond)
|
||||||
|
|
||||||
losses = F.mse_loss(denoised, images, reduction = 'none')
|
losses = F.mse_loss(denoised, images, reduction = 'none')
|
||||||
losses = reduce(losses, 'b ... -> b', 'mean')
|
losses = reduce(losses, 'b ... -> b', 'mean')
|
||||||
|
|||||||
@@ -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.27.1',
|
version = '0.27.2',
|
||||||
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