-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualize.py
85 lines (70 loc) · 2.37 KB
/
visualize.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import matplotlib.pyplot as plt
import numpy as np
import glob
import os
from PIL import Image
#######################################
## Methods for CPPN visualization
#######################################
def get_zs(size, distribution):
"""Get a randomly sampled latent vector
"""
dist_dict = dict([('normal', np.random.normal), ('uniform', np.random.uniform)])
return dist_dict[distribution](size=(2, 1, size))
def show_image(image):
"""Show a single image
"""
plt.imshow(image, interpolation='nearest')
plt.axis('off')
plt.show()
def show_images(images, cols):
"""Show sampled images
"""
rows = np.ceil(len(images)/cols).astype(int)
fig, axes = plt.subplots(rows, cols)
if rows == 1: axes = [axes]
fig.subplots_adjust(hspace=0, wspace=0)
for i, ax_x in enumerate(axes):
for j, ax_j in enumerate(ax_x):
try:
ax_j.axis('off')
ax_j.imshow(images[j+i*cols])
except:
ax_j.axis('off')
ax_j.set_aspect('auto')
plt.show()
def save_image(image, name, path='images/'):
"""Save generated images
"""
to_image(image).save(path + name)
def save_images(images, name):
"""Save a batch of generated images
"""
for i, im in enumerate(images):
file_name = name.split('.')
save_image(im, "{0}_{1}.{2}".format(file_name[0], i+1, file_name[1]))
def to_image(image):
"""Converts numpy to PIL
"""
return Image.fromarray(np.uint8(255 * image.squeeze()))
def clear_image_dir():
"""Empty the gif image directory to produce a new gif
"""
for f in glob.glob('gifs/gif_imgs/*'):
os.remove(f)
def generate_gif(model, frames, size, name, distribution='normal'):
"""Generate an interpolation gif
"""
# Get interpolation information
z1, z2 = get_zs(size, distribution)
coeff = np.linspace(0, 1, frames+2)
interp = lambda a: (1-a) * z1 + a * z2
# Store the interpolated images
images = [to_image(model(latent_code=interp(l))) for l in coeff]
# append the reverse sequence
images += images[::-1][1:]
durations = [0.5] + frames*[0.1] + [1.0] + frames*[0.1] + [0.5]
# Write to GIF
print("Writing images to gif")
images[0].save('./gifs/' + name, save_all=True, append_images=images[1:],
duration=durations, loop=0)