-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathautoenc.py
94 lines (73 loc) · 2.83 KB
/
autoenc.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
"""This module implements a convolution autoencoder on MNIST data."""
import numpy as np
import matplotlib.pyplot as plt
from keras.datasets import mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
from keras.layers import Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Model, Sequential
from keras.optimizers import Adam
from keras import backend as k
# for resizing images
from scipy.misc import imresize
def reshape(x):
"""Reshape images to 14*14"""
img = imresize(x.reshape(28,28), (14, 14))
return img
# create 14*14 low resolution train and test images
XX_train = np.array([*map(reshape, X_train.astype(float))])
XX_test = np.array([*map(reshape, X_test.astype(float))])
# scale images to range between 0 and 1
#14*14 train images
XX_train = XX_train/255
#28*28 train label images
X_train = X_train/255
#14*14 test images
XX_test = XX_test/255
#28*28 test label images
X_test = X_test/255
batch_size = 128
epochs = 40
input_shape = (14,14,1)
def make_autoencoder(input_shape):
generator = Sequential()
generator.add(Conv2D(64, (3, 3), activation='relu', padding='same',
input_shape=input_shape))
generator.add(MaxPooling2D(pool_size=(2, 2)))
generator.add(Conv2D(128, (3, 3), activation='relu', padding='same'))
generator.add(Conv2D(128, (3, 3), activation='relu', padding='same'))
generator.add(UpSampling2D((2, 2)))
generator.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
generator.add(UpSampling2D((2, 2)))
generator.add(Conv2D(1, (3, 3), activation='sigmoid', padding='same'))
return generator
autoencoder = make_autoencoder(input_shape)
autoencoder.compile(loss='mean_squared_error', optimizer = Adam(lr=0.0002,
beta_1=0.5))
autoencoder_train = autoencoder.fit(XX_train.reshape(-1,14,14,1),
X_train.reshape(-1,28,28,1),
batch_size=batch_size,
epochs=epochs, verbose=1,
validation_split = 0.2)
loss = autoencoder_train.history['loss']
val_loss = autoencoder_train.history['val_loss']
epochs_ = [x for x in range(epochs)]
plt.figure()
plt.plot(epochs_, loss, label='Training loss', marker = 'D')
plt.plot(epochs_, val_loss, label='Validation loss', marker = 'o')
plt.title('Training and validation loss')
plt.legend()
plt.show()
print('Input')
plt.figure(figsize=(5,5))
for i in range(9):
plt.subplot(331 + i)
plt.imshow(np.squeeze(XX_test.reshape(-1,14,14)[i]), cmap='gray')
plt.show()
# Test set results
print('GENERATED')
plt.figure(figsize=(5,5))
for i in range(9):
pred = autoencoder.predict(XX_test.reshape(-1,14,14,1)[i:i+1], verbose=0)
plt.subplot(331 + i)
plt.imshow(pred[0].reshape(28,28), cmap='gray')
plt.show()