-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
51 lines (38 loc) · 1.71 KB
/
Copy pathutils.py
File metadata and controls
51 lines (38 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import torch
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from PIL import Image
from pathlib import Path
def postprocess(images):
images = (images / 2 + 0.5).clamp(0, 1)
images = images.cpu().permute(0, 2, 3, 1).numpy()
images = (images * 255).round().astype("uint8")
return images
def create_images_grid(images, rows, cols):
images = [Image.fromarray(image) for image in images]
w, h = images[0].size
grid = Image.new('RGB', size=(cols * w, rows * h))
for i, image in enumerate(images):
grid.paste(image, box=(i % cols * w, i // cols * h))
return grid
def create_sampling_animation(model, pipeline, config, interval=5, every_nth_image=1, rows=2, cols=3):
# Sample some images from random noise (this is the backward diffusion process).
# The default pipeline output type is `List[PIL.Image]`
noisy_sample = torch.randn(
config.eval_batch_size,
config.image_channels,
config.image_size,
config.image_size).to(config.device)
# images is a list of num_timesteps images batches, e.g. List[Tensor(NCHW)]
images = pipeline.sampling(model, noisy_sample, device=config.device, save_all_steps=True)
fig = plt.figure()
ims = []
for i in range(0, pipeline.num_timesteps, every_nth_image):
imgs = postprocess(images[i])
image_grid = create_images_grid(imgs, rows=rows, cols=cols)
im = plt.imshow(image_grid, animated=True)
ims.append([im])
plt.axis('off')
animate = animation.ArtistAnimation(fig, ims, interval=interval, blit=True, repeat_delay=5000)
path_to_save_animation = Path(config.output_dir, "samples", "diffusion.gif")
animate.save(str(path_to_save_animation))