mirror of
https://github.com/wassname/denoising-diffusion-pytorch.git
synced 2026-09-10 12:01:08 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c479adf960 | ||
|
|
d70fb08f8a | ||
|
|
e700a7c6de | ||
|
|
9c758662a3 |
@@ -34,8 +34,8 @@ loss = diffusion(training_images)
|
|||||||
loss.backward()
|
loss.backward()
|
||||||
# after a lot of training
|
# after a lot of training
|
||||||
|
|
||||||
sampled_images = diffusion.p_sample_loop((1, 3, 128, 128))
|
sampled_images = diffusion.sample(128, batch_size = 4)
|
||||||
sampled_images.shape # (1, 3, 128, 128)
|
sampled_images.shape # (4, 3, 128, 128)
|
||||||
```
|
```
|
||||||
|
|
||||||
Or, if you simply want to pass in a folder name and the desired image dimensions, you can use the `Trainer` class to easily train a model.
|
Or, if you simply want to pass in a folder name and the desired image dimensions, you can use the `Trainer` class to easily train a model.
|
||||||
|
|||||||
@@ -309,6 +309,26 @@ class GaussianDiffusion(nn.Module):
|
|||||||
img = self.p_sample(img, torch.full((b,), i, device=device, dtype=torch.long))
|
img = self.p_sample(img, torch.full((b,), i, device=device, dtype=torch.long))
|
||||||
return img
|
return img
|
||||||
|
|
||||||
|
@torch.no_grad()
|
||||||
|
def sample(self, image_size, batch_size = 16):
|
||||||
|
return self.p_sample_loop((16, 3, image_size, image_size))
|
||||||
|
|
||||||
|
@torch.no_grad()
|
||||||
|
def interpolate(self, x1, x2, t = None, lam = 0.5):
|
||||||
|
b, *_, device = *x1.shape, x1.device
|
||||||
|
t = default(t, self.num_timesteps - 1)
|
||||||
|
|
||||||
|
assert x1.shape == x2.shape
|
||||||
|
|
||||||
|
t_batched = torch.stack([torch.tensor(t, device=device)] * b)
|
||||||
|
xt1, xt2 = map(lambda x: self.q_sample(x, t=t_batched), (x1, x2))
|
||||||
|
|
||||||
|
img = (1 - lam) * xt1 + lam * xt2
|
||||||
|
for i in tqdm(reversed(range(0, t)), desc='interpolation sample time step', total=t):
|
||||||
|
img = self.p_sample(img, torch.full((b,), i, device=device, dtype=torch.long))
|
||||||
|
|
||||||
|
return img
|
||||||
|
|
||||||
def q_sample(self, x_start, t, noise=None):
|
def q_sample(self, x_start, t, noise=None):
|
||||||
noise = default(noise, lambda: torch.randn_like(x_start))
|
noise = default(noise, lambda: torch.randn_like(x_start))
|
||||||
|
|
||||||
@@ -403,7 +423,7 @@ class Trainer(object):
|
|||||||
milestone = ind // SAVE_AND_SAMPLE_EVERY
|
milestone = ind // SAVE_AND_SAMPLE_EVERY
|
||||||
all_images = self.model.p_sample_loop((64, 3, self.image_size, self.image_size))
|
all_images = self.model.p_sample_loop((64, 3, self.image_size, self.image_size))
|
||||||
utils.save_image(all_images, f'./sample-{milestone}.png', nrow=8)
|
utils.save_image(all_images, f'./sample-{milestone}.png', nrow=8)
|
||||||
torch.save(model.state_dict(), f'./model-{milestone}.pt')
|
torch.save(self.model.state_dict(), f'./model-{milestone}.pt')
|
||||||
|
|
||||||
ind += 1
|
ind += 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.1.1',
|
version = '0.1.3',
|
||||||
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