-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathinterpolate_GAN.py
42 lines (35 loc) · 1.35 KB
/
interpolate_GAN.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
#!/usr/bin/env python3
# This script is a (super) slightly modified version of
# https://gist.github.com/matpalm/23dc5804c6d673b800093d0d15e5de0e
# By Mat Kelcey https://twitter.com/mat_kelcey
# Given two random latent vectors the GAN generates their
# corresponding images and linearly interpolates the images in
# between them
import os
from PIL import Image
import numpy as np
import tensorflow as tf
import tensorflow_hub as hub
# smooth values from point a to point b.
folder = "./interpolation/"
STEPS = 50
pt_a = np.random.normal(size=(512))
pt_b = np.random.normal(size=(512))
z = np.empty((STEPS, 512))
for i, alpha in enumerate(np.linspace(start=0.0, stop=1.0, num=STEPS)):
z[i] = (1-alpha) * pt_a + alpha * pt_b
# Choose a directory for which you have privileges
# where you download the tfhub model
print('Downloading the model.')
os.environ['TFHUB_CACHE_DIR'] = 'D:/NeuralNetworks/ProGAN'
generator = hub.Module("http://tfhub.dev/google/progan-128/1")
print('Model downloaded.')
# sample all z and write out as separate images.
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
imgs = sess.run(generator(z))
imgs = (imgs * 255).astype(np.uint8)
for i, img in enumerate(imgs):
Image.fromarray(img).save(folder + "foo_%02d.png" % i)
# save the latent vectors that generated the images
np.save(folder + "z_%02d" % i, z[i])