-
Notifications
You must be signed in to change notification settings - Fork 1
/
cnn_autoencoder.py
executable file
·97 lines (71 loc) · 3.11 KB
/
cnn_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
#!/usr/bin/env python
from tensorflow import keras
from keras import layers
from keras import regularizers
from keras.datasets import mnist
import numpy as np
# This is the size of our encoded representations
encoding_dim = 32 # 32 floats -> compression of factor 24.5, assuming the input is 784 floats
# This is our input image
input_img = keras.Input(shape=(32, 32, 1))
x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
# "encoded" is the encoded representation of the input
encoded = layers.MaxPooling2D((2, 2), padding='same')(x)
# at this point the representation is (4, 4, 8) i.e. 128-dimensional
x = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = layers.UpSampling2D((2, 2))(x)
x = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = layers.UpSampling2D((2, 2))(x)
x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x)
x = layers.UpSampling2D((2, 2))(x)
# "decoded" is the lossy reconstruction of the input
decoded = layers.Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
# This model maps an input to its reconstruction
autoencoder = keras.Model(input_img, decoded)
print( autoencoder.summary() )
autoencoder.compile(optimizer='rmsprop', loss='mean_squared_error')
#(x_train, _), (x_test, _) = mnist.load_data()
data = np.load( 'pieces_2021-01-02_1415.npy' )
validation_split = int(len(data)*0.8)
x_train = data[:validation_split]
x_test = data[validation_split:]
# We will normalize all values between 0 and 1 and we will flatten the 32x32 images into vectors of size 784.
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train), 32, 32, 1))
x_test = np.reshape(x_test, (len(x_test), 32, 32, 1))
print(x_train.shape)
print(x_test.shape)
# Now let's train our autoencoder for 50 epochs:
autoencoder.fit(x_train, x_train,
epochs=150,
batch_size=256,
shuffle=True,
validation_data=(x_test, x_test))
# After 50 epochs, the autoencoder seems to reach a stable train/validation loss value of about 0.09.
# We can try to visualize the reconstructed inputs and the encoded representations. We will use Matplotlib.
# Encode and decode some digits
# Note that we take them from the *test* set
decoded_imgs = autoencoder.predict(x_test)
# Use Matplotlib (don't ask)
import matplotlib.pyplot as plt
n = 10 # How many digits we will display
plt.figure(figsize=(20, 4))
for i in range(n):
# Display original
ax = plt.subplot(2, n, i + 1)
plt.imshow(x_test[i].reshape(32, 32))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
# Display reconstruction
ax = plt.subplot(2, n, i + 1 + n)
plt.imshow(decoded_imgs[i].reshape(32, 32))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()