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
| from tensorflow.keras.datasets import mnist from tensorflow.keras.models import Sequential, Model from tensorflow.keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D, Flatten, Reshape import tensorflow as tf import numpy as np import matplotlib.pyplot as plt
(X_train, _), (X_test, _) = mnist.load_data()
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1).astype('float32') / 255 X_test = X_test.reshape(X_test.shape[0],28,28,1).astype('float32') / 255
autoencoder = Sequential() autoencoder.add(Conv2D(16, kernel_size = 3, input_shape = X_train.shape[1:], activation='relu', padding = 'same'))
autoencoder.add(MaxPooling2D(2, padding='same'))
autoencoder.add(Conv2D(8, kernel_size =3, activation = 'relu', padding = 'same'))
autoencoder.add(MaxPooling2D(2, padding='same'))
autoencoder.add(Conv2D(8, kernel_size = 3, strides =2, padding= 'same', activation = 'relu'))
autoencoder.add(UpSampling2D())
autoencoder.add(Conv2D(8, kernel_size=3, padding='same', activation='relu'))
autoencoder.add(UpSampling2D())
autoencoder.add(Conv2D(16, kernel_size=3, activation='relu'))
autoencoder.add(UpSampling2D())
autoencoder.add(Conv2D(1, kernel_size = 3, padding='same', activation='sigmoid'))
with tf.device("/GPU:0"): autoencoder.compile(optimizer='adam', loss='binary_crossentropy') autoencoder.fit(X_train, X_train, epochs=20, batch_size=128, validation_data=(X_test, X_test))
random_test = np.random.randint(X_test.shape[0], size=5) ae_imgs = autoencoder.predict(X_test) plt.figure(figsize=(7,2))
for i, image_idx in enumerate(random_test): ax = plt.subplot(2,7, i+1) plt.imshow(X_test[image_idx].reshape(28,28)) ax = plt.subplot(2,7, 7+i+1) plt.imshow(ae_imgs[image_idx].reshape(28,28))
|