-
Notifications
You must be signed in to change notification settings - Fork 6
/
denoising_autoencoder.py
59 lines (43 loc) · 2.05 KB
/
denoising_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
import numpy as np
from keras.layers import Conv2D, UpSampling2D, BatchNormalization
from keras.models import Sequential,
from keras.datasets import cifar10
import matplotlib.pyplot as plt
(x_train, _), (x_test, _) = cifar10.load_data()
x_train = x_train/255
x_test = x_test/255
# The next three methods to visualize input/output of our model side-by-side
def hstackimgs(min, max, images):
return np.hstack(images[i] for i in range(min, max))
def sqstackimgs(length, height, images):
return np.vstack(hstackimgs(i*length, (i+1)*length, images) for i in range(height))
def sbscompare(images1, images2, length, height):
A = sqstackimgs(length, height, images1)
B = sqstackimgs(length, height, images2)
C = np.ones((A.shape[0], 32, 3))
return np.hstack((A, C, B))
model = Sequential()
model.add(Conv2D(32, kernel_size=3, strides=1, padding='same', activation='relu', input_shape=(32, 32, 3)))
model.add(BatchNormalization()) # 32x32x32
model.add(Conv2D(32, kernel_size=3, strides=2, padding='same', activation='relu')) # 16x16x32
model.add(Conv2D(32, kernel_size=3, strides=1, padding='same', activation='relu')) # 16x16x32
model.add(BatchNormalization()) # 16x16x32
model.add(UpSampling2D())
model.add(Conv2D(32, kernel_size=3, strides=1, padding='same', activation='relu')) # 32x32x32
model.add(BatchNormalization())
model.add(Conv2D(3, kernel_size=1, strides=1, padding='same', activation='sigmoid')) # 32x32x3
model.compile(optimizer='adam', metrics=['accuracy'], loss='mean_squared_error')
model.summary()
# We want to add different noise vectors for each epoch
num_epochs = 3
NOISE = 0.3 # Set to 0 for a regular (non-denoising...) autoencoder
for i in range(num_epochs):
noise = np.random.normal(0, NOISE, x_train.shape)
model.fit(x_train + noise, x_train, epochs=1, batch_size=100)
x_test = x_test[:400]
noise = np.random.normal(0, NOISE, x_test.shape)
pred_imgs = model.predict(x_test + noise)
plt.imshow(sbscompare(x_test + noise, pred_imgs, 20, 20))
plt.axis('off')
plt.rcParams["figure.figsize"] = [60,60]
plt.show()