-
Notifications
You must be signed in to change notification settings - Fork 5
/
autoencoder.py
184 lines (144 loc) · 7.92 KB
/
autoencoder.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
import numpy as np
import os
import shutil
import seaborn as sns
from matplotlib.colors import ListedColormap
sns.set_style(style='white')
# Network parameters
tf.flags.DEFINE_float('learning_rate', .0005, 'Initial learning rate.')
tf.flags.DEFINE_integer('epochs', 20, 'Number of steps to run trainer.')
tf.flags.DEFINE_integer('batch_size', 128, 'Minibatch size')
tf.flags.DEFINE_integer('latent_dim', 2, 'Number of latent dimensions')
tf.flags.DEFINE_integer('test_image_number', 5, 'Number of test images to recover during training')
tf.flags.DEFINE_integer('inputs_decoder', 49, 'Size of decoder input layer')
tf.flags.DEFINE_string('dataset', 'mnist', 'Dataset name [mnist, fashion-mnist]')
tf.flags.DEFINE_string('logdir', './logs', 'Logs folder')
tf.flags.DEFINE_bool('plot_latent', True, 'Plot latent space')
FLAGS = tf.flags.FLAGS
# Define and create results folders
results_folder = os.path.join('Results', FLAGS.dataset)
[os.makedirs(os.path.join(results_folder, folder)) for folder in ['Test', 'Train']
if not os.path.exists(os.path.join(results_folder, folder))]
# Empty log folder
try:
if not len(os.listdir(FLAGS.logdir)) == 0:
shutil.rmtree(FLAGS.logdir)
except:
pass
# Get data
data = keras.datasets.mnist if FLAGS.dataset == 'mnist' else keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = data.load_data()
# Create tf dataset
with tf.variable_scope("DataPipe"):
dataset = tf.data.Dataset.from_tensor_slices(train_images)
dataset = dataset.map(lambda x: tf.image.convert_image_dtype([x], dtype=tf.float32))
dataset = dataset.batch(batch_size=FLAGS.batch_size).prefetch(FLAGS.batch_size)
iterator = dataset.make_initializable_iterator()
input_batch = iterator.get_next()
input_batch = tf.reshape(input_batch, shape=[-1, 28, 28, 1])
def encoder(X):
activation = tf.nn.relu
with tf.variable_scope("Encoder"):
x = tf.layers.conv2d(X, filters=64, kernel_size=4, strides=2, padding='same', activation=activation)
x = tf.layers.conv2d(x, filters=64, kernel_size=4, strides=2, padding='same', activation=activation)
x = tf.layers.conv2d(x, filters=64, kernel_size=4, strides=1, padding='same', activation=activation)
x = tf.layers.flatten(x)
# Local latent variables
mean_ = tf.layers.dense(x, units=FLAGS.latent_dim, name='mean')
std_dev = tf.nn.softplus(tf.layers.dense(x, units=FLAGS.latent_dim), name='std_dev') # softplus to force >0
# Reparametrization trick
epsilon = tf.random_normal(tf.stack([tf.shape(x)[0], FLAGS.latent_dim]), name='epsilon')
z = mean_ + tf.multiply(epsilon, std_dev)
return z, mean_, std_dev
def decoder(z):
activation = tf.nn.relu
with tf.variable_scope("Decoder"):
x = tf.layers.dense(z, units=FLAGS.inputs_decoder, activation=activation)
x = tf.layers.dense(x, units=FLAGS.inputs_decoder, activation=activation)
recovered_size = int(np.sqrt(FLAGS.inputs_decoder))
x = tf.reshape(x, [-1, recovered_size, recovered_size, 1])
x = tf.layers.conv2d_transpose(x, filters=64, kernel_size=4, strides=1, padding='same', activation=activation)
x = tf.layers.conv2d_transpose(x, filters=64, kernel_size=4, strides=1, padding='same', activation=activation)
x = tf.layers.conv2d_transpose(x, filters=64, kernel_size=4, strides=1, padding='same', activation=activation)
x = tf.contrib.layers.flatten(x)
x = tf.layers.dense(x, units=28 * 28, activation=None)
x = tf.layers.dense(x, units=28 * 28, activation=tf.nn.sigmoid)
img = tf.reshape(x, shape=[-1, 28, 28, 1])
return img
# Link encoder and decoder
z, mean_, std_dev = encoder(input_batch)
output = decoder(z)
# Reshape input and output to flat vectors
flat_output = tf.reshape(output, [-1, 28 * 28])
flat_input = tf.reshape(input_batch, [-1, 28 * 28])
with tf.name_scope('loss'):
img_loss = tf.reduce_sum(flat_input * -tf.log(flat_output) + (1 - flat_input) * -tf.log(1 - flat_output), 1)
latent_loss = 0.5 * tf.reduce_sum(tf.square(mean_) + tf.square(std_dev) - tf.log(tf.square(std_dev)) - 1, 1)
loss = tf.reduce_mean(img_loss + latent_loss)
tf.summary.scalar('batch_loss', loss)
optimizer = tf.train.AdamOptimizer(FLAGS.learning_rate).minimize(loss)
init_vars = [tf.local_variables_initializer(), tf.global_variables_initializer()]
gpu_options = tf.GPUOptions(allow_growth=True)
# Training loop
with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
writer = tf.summary.FileWriter('./logs', sess.graph)
sess.run(init_vars)
merged_summary_op = tf.summary.merge_all()
for epoch in range(FLAGS.epochs):
sess.run(iterator.initializer)
print('Actual epoch: {}'.format(epoch))
flag = True # Show only first batch of epoch
while True:
try:
sess.run(optimizer)
if flag:
# Get input and recover output images comparison
summ, target, output_ = sess.run([merged_summary_op, input_batch, output])
f, axarr = plt.subplots(FLAGS.test_image_number, 2)
for j in range(FLAGS.test_image_number):
for pos, im in enumerate([target, output_]):
axarr[j, pos].imshow(im[j].reshape((28, 28)), cmap='gray')
axarr[j, pos].axis('off')
plt.savefig(os.path.join(results_folder, 'Train/Epoch_{}').format(epoch))
plt.close(f)
flag = False
writer.add_summary(summ, epoch)
# Create artificial image from unit norm sample
artificial_image = sess.run(output, feed_dict={z: np.random.normal(0, 1, (1, FLAGS.latent_dim))})
plt.figure()
with sns.axes_style("white"):
plt.imshow(artificial_image[0].reshape((28, 28)), cmap='gray')
plt.savefig(os.path.join(results_folder, 'Test/{}'.format(epoch)))
plt.close()
# Create plot of latent space (only if latent dimensions are 2)
if FLAGS.latent_dim == 2 and FLAGS.plot_latent:
coords = sess.run(z, feed_dict={input_batch: test_images[..., np.newaxis]/255.})
colormap = ListedColormap(sns.color_palette(sns.hls_palette(10, l=.45 , s=.8)).as_hex())
plt.scatter(coords[:, 0], coords[:, 1], c=test_labels, cmap=colormap)
cbar = plt.colorbar()
if FLAGS.dataset == 'fashion-mnist':
cbar.ax.set_yticklabels(['T-shirt', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal',
'Shirt', 'Sneaker', 'Bag', 'Ankle boot'])
# plt.axis('off')
plt.title('Latent space')
plt.savefig(os.path.join(results_folder, 'Test/Latent_{}'.format(epoch)))
plt.close()
except tf.errors.OutOfRangeError:
break
# Create mesh grid of values
values = np.arange(-3, 4, .5)
xx, yy = np.meshgrid(values, values)
input_holder = np.zeros((1, 2))
# Matrix that will contain the grid of images
container = np.zeros((28 * len(values), 28 * len(values)))
for row in range(xx.shape[0]):
for col in range(xx.shape[1]):
input_holder[0, :] = [xx[row, col], yy[row, col]]
artificial_image = sess.run(output, feed_dict={z: input_holder})
container[row * 28: (row + 1) * 28, col * 28: (col + 1) * 28] = np.squeeze(artificial_image)
plt.imshow(container, cmap='gray')
plt.savefig(os.path.join(results_folder, 'Test/Space_{}'.format(epoch)))
plt.close( )