-
Notifications
You must be signed in to change notification settings - Fork 0
/
cae_gal.py
347 lines (302 loc) · 11.1 KB
/
cae_gal.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
"""Tutorial on how to create a convolutional autoencoder w/ Tensorflow.
Parag K. Mital, Jan 2016
"""
import tensorflow as tf
import numpy as np
import math
import dataset
# from libs.activations import lrelu
# from libs.utils import corrupt
def lrelu(x, leak=0.2, name="lrelu"):
"""Leaky rectifier.
Parameters
----------
x : Tensor
The tensor to apply the nonlinearity to.
leak : float, optional
Leakage parameter.
name : str, optional
Variable scope to use.
Returns
-------
x : Tensor
Output of the nonlinearity.
"""
with tf.variable_scope(name):
f1 = 0.5 * (1 + leak)
f2 = 0.5 * (1 - leak)
return f1 * x + f2 * abs(x)
# %%
def autoencoder(input_shape,
n_filters=[1, 36, 24, 12],
filter_sizes=[7, 3, 3, 3],
corruption=False):
"""Build a deep denoising autoencoder w/ tied weights.
Parameters
----------
input_shape : list, optional
Description
n_filters : list, optional
Description
filter_sizes : list, optional
Description
Returns
-------
x : Tensor
Input placeholder to the network
z : Tensor
Inner-most latent representation
y : Tensor
Output reconstruction of the input
cost : Tensor
Overall cost to use for training
Raises
------
ValueError
Description
"""
# %%
# input to the network
x = tf.placeholder(
tf.float32, input_shape, name='x')
# %%
# ensure 2-d is converted to square tensor.
if len(x.get_shape()) == 2:
x_dim = np.sqrt(x.get_shape().as_list()[1])
if x_dim != int(x_dim):
raise ValueError('Unsupported input dimensions')
x_dim = int(x_dim)
x_tensor = tf.reshape(
x, [-1, x_dim, x_dim, n_filters[0]])
elif len(x.get_shape()) == 4:
x_tensor = x
else:
raise ValueError('Unsupported input dimensions')
current_input = x_tensor
# %%
# Build the encoder
encoder = []
shapes = []
for layer_i, n_output in enumerate(n_filters[1:]):
n_input = current_input.get_shape().as_list()[3]
shapes.append(current_input.get_shape().as_list())
W = tf.Variable(
tf.random_uniform([
filter_sizes[layer_i],
filter_sizes[layer_i],
n_input, n_output],
-1.0 / math.sqrt(n_input),
1.0 / math.sqrt(n_input)))
b = tf.Variable(tf.zeros([n_output]))
encoder.append(W)
output = tf.add(tf.nn.conv2d(
current_input, W, strides=[1, 2, 2, 1], padding='SAME'), b)
output_drop = tf.nn.dropout(x=output, keep_prob=0.5, name='conv_dropout')
current_input = output_drop
relu_z = lrelu(current_input)
drop_z = tf.nn.dropout(x=relu_z, keep_prob=0.5, name='relu_dropout')
# %%
# store the latent representation
z = drop_z
print z.shape
encoder.reverse()
shapes.reverse()
# %%
# Build the decoder using the same weights
for layer_i, shape in enumerate(shapes):
# W = tf.Variable(
# tf.random_uniform(encoder[layer_i].get_shape().as_list(),
# -1.0 / math.sqrt(encoder[layer_i].get_shape().as_list()[2]),
# 1.0 / math.sqrt(encoder[layer_i].get_shape().as_list()[2])))
W = encoder[layer_i]
b = tf.Variable(tf.zeros([W.get_shape().as_list()[2]]))
output = tf.add(
tf.nn.conv2d_transpose(
current_input, W,
tf.stack([tf.shape(x)[0], shape[1], shape[2], shape[3]]),
strides=[1, 2, 2, 1], padding='SAME'), b)
# If it's the last layer, don't add dropout
if (layer_i + 1) == len(shapes):
current_input = output
else:
output_drop = tf.nn.dropout(x=output, keep_prob=0.5, name='conv_dropout')
current_input = output_drop
# %%
# now have the reconstruction through the network
y = lrelu(current_input)
# cost function measures pixel-wise difference
cost = tf.losses.mean_squared_error(x_tensor, y)
# cost = tf.reduce_sum(tf.square(y - x_tensor))
# %%
return {'x': x, 'z': z, 'y': y, 'cost': cost}
def test_mastcam_slices():
import tensorflow as tf
from time import time
from os import mkdir
import cv2
# load mastcam data
mastcam = dataset.load_mcam_slices()
print mastcam.shape
mean_img = np.mean(mastcam, axis=0)
ae = autoencoder(input_shape=[None, 36, 40, 3])
learning_rate = 0.001
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(ae['cost'])
# We create a session to use the graph
sess = tf.Session()
sess.run(tf.global_variables_initializer())
# Fit all training data
n_epochs = 3
batch_size = 10
num_batches = mastcam.shape[0] / batch_size
print "num batches = %d", num_batches
for epoch_i in range(n_epochs):
for batch_i in range(num_batches):
idx = batch_i*batch_size
batch_xs = mastcam[idx:idx+batch_size]
train = np.array([img - mean_img for img in batch_xs])
sess.run(optimizer, feed_dict={ae['x']: train})
print(epoch_i, sess.run(ae['cost'], feed_dict={ae['x']: train}))
# %%
# Plot example reconstructions
n_examples = 10
test_xs = dataset.test_mcam_slices()[:batch_size]
#test_xs_norm = np.array([img - mean_img for img in test_xs])
recon = sess.run(ae['y'], feed_dict={ae['x']: test_xs})
#recon = np.array([img + mean_img for img in recon])
t = str(int(time()))
mkdir('./results/' + t)
for example_i in range(n_examples):
print 'one test example shape'
print test_xs[example_i].shape
cv2.imwrite('./results/' + t + '/' + str(example_i) + '_input.png', test_xs[example_i])
cv2.imwrite('./results/' + t + '/' + str(example_i) + '_recon.png', recon[example_i])
def test_mastcam_rgb():
import tensorflow as tf
from time import time
from os import mkdir
import cv2
# load mastcam data
mastcam = dataset.load_mcam_rgb()
print mastcam.shape
mean_img = np.mean(mastcam, axis=0)
std_img = np.std(mastcam, axis=0)
ae = autoencoder(input_shape=[None, 144, 160, 3])
learning_rate = 0.001
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(ae['cost'])
# We create a session to use the graph
sess = tf.Session()
sess.run(tf.global_variables_initializer())
# Fit all training data
n_epochs = 12
batch_size = 10
num_batches = mastcam.shape[0] / batch_size
print "num batches = %d", num_batches
for epoch_i in range(n_epochs):
for batch_i in range(num_batches):
idx = batch_i*batch_size
batch_xs = mastcam[idx:idx+batch_size]
train = np.array([img - mean_img for img in batch_xs])
sess.run(optimizer, feed_dict={ae['x']: train})
print(epoch_i, sess.run(ae['cost'], feed_dict={ae['x']: train}))
# %%
# Plot example reconstructions
n_examples = 10
test_xs = dataset.load_test_rgb()[:batch_size]
#test_xs_norm = np.array([img - mean_img for img in test_xs])
recon = sess.run(ae['y'], feed_dict={ae['x']: test_xs})
#recon = np.array([img + mean_img for img in recon])
t = str(int(time()))
mkdir('./results/' + t)
for example_i in range(n_examples):
print 'one test example shape'
print test_xs[example_i].shape
cv2.imwrite('./results/' + t + '/' + str(example_i) + '_input.png', test_xs[example_i])
cv2.imwrite('./results/' + t + '/' + str(example_i) + '_recon.png', recon[example_i])
def test_mastcam_gray():
import tensorflow as tf
from time import time
from os import mkdir
from matplotlib.pyplot import imsave
# load mastcam data
mastcam = dataset.load_mcam_gray()
mean_img = np.mean(mastcam, axis=0)
ae = autoencoder(input_shape=[None, 144*144])
learning_rate = 0.01
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(ae['cost'])
# We create a session to use the graph
sess = tf.Session()
sess.run(tf.global_variables_initializer())
# Fit all training data
batch_size = 10
n_epochs = 3
num_batches = mastcam.shape[0] / batch_size
print "num batches = %d", num_batches
for epoch_i in range(n_epochs):
for batch_i in range(num_batches):
idx = batch_i*batch_size
batch_xs = mastcam[idx:idx+batch_size]
train = np.array([img - mean_img for img in batch_xs])
sess.run(optimizer, feed_dict={ae['x']: train})
print(epoch_i, sess.run(ae['cost'], feed_dict={ae['x']: train}))
# %%
# Plot example reconstructions
n_examples = 10
test_xs = dataset.load_test_gray()[0:batch_size]
test_xs_norm = np.array([img - mean_img for img in test_xs])
recon, recon_err = sess.run([ae['y'], ae['cost']], feed_dict={ae['x']: test_xs_norm})
print(recon.shape)
t = str(int(time()))
mkdir('./results/' + t)
for example_i in range(n_examples):
imsave('./results/' + t + '/' + str(example_i) + '_input.png', np.reshape(test_xs[example_i, :], (144, 144)), cmap='gray')
imsave('./results/' + t + '/' + str(example_i) + '_' + str(int(recon_err)) + '_recon.png', np.reshape(np.reshape(recon[example_i, ...], (144*144,)) + mean_img, (144, 144)), cmap='gray')
# %%
def test_mnist():
"""Test the convolutional autoencder using MNIST."""
# %%
import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
import matplotlib.pyplot as plt
# %%
# load MNIST as before
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
mean_img = np.mean(mnist.train.images, axis=0)
ae = autoencoder(input_shape=[None, 784])
# %%
learning_rate = 0.01
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(ae['cost'])
# %%
# We create a session to use the graph
sess = tf.Session()
sess.run(tf.global_variables_initializer())
# %%
# Fit all training data
batch_size = 100
n_epochs = 10
for epoch_i in range(n_epochs):
for batch_i in range(mnist.train.num_examples // batch_size):
batch_xs, _ = mnist.train.next_batch(batch_size)
train = np.array([img - mean_img for img in batch_xs])
sess.run(optimizer, feed_dict={ae['x']: train})
print(epoch_i, sess.run(ae['cost'], feed_dict={ae['x']: train}))
# %%
# Plot example reconstructions
n_examples = 10
test_xs, _ = mnist.test.next_batch(n_examples)
test_xs_norm = np.array([img - mean_img for img in test_xs])
recon = sess.run(ae['y'], feed_dict={ae['x']: test_xs_norm})
print(recon.shape)
fig, axs = plt.subplots(2, n_examples, figsize=(10, 2))
for example_i in range(n_examples):
axs[0][example_i].imshow(
np.reshape(test_xs[example_i, :], (28, 28)), cmap='gray')
axs[1][example_i].imshow(
np.reshape(
np.reshape(recon[example_i, ...], (784,)) + mean_img,
(28, 28)), cmap='gray')
fig.show()
plt.draw()
plt.waitforbuttonpress()
# %%
if __name__ == '__main__':
test_mastcam_rgb()