|
| 1 | +#!/usr/bin/env python |
| 2 | +__author__ = "Sreenivas Bhattiprolu" |
| 3 | +__license__ = "Feel free to copy, I appreciate if you acknowledge Python for Microscopists" |
| 4 | + |
| 5 | +# https://www.youtube.com/watch?v=Sm54KXD-L1k |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +from tensorflow.keras.datasets import mnist |
| 10 | +from tensorflow.keras.layers import Conv2D, MaxPooling2D, UpSampling2D |
| 11 | +from tensorflow.keras.models import Sequential |
| 12 | + |
| 13 | +import numpy as np |
| 14 | +import matplotlib.pyplot as plt |
| 15 | + |
| 16 | +(x_train, _), (x_test, _) = mnist.load_data() |
| 17 | + |
| 18 | +x_train = x_train.astype('float32') / 255. |
| 19 | +x_test = x_test.astype('float32') / 255. |
| 20 | +x_train = np.reshape(x_train, (len(x_train), 28, 28, 1)) |
| 21 | +x_test = np.reshape(x_test, (len(x_test), 28, 28, 1)) |
| 22 | + |
| 23 | +#adding some noise |
| 24 | +noise_factor = 0.5 |
| 25 | +x_train_noisy = x_train + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_train.shape) |
| 26 | +x_test_noisy = x_test + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_test.shape) |
| 27 | + |
| 28 | +x_train_noisy = np.clip(x_train_noisy, 0., 1.) |
| 29 | +x_test_noisy = np.clip(x_test_noisy, 0., 1.) |
| 30 | + |
| 31 | +#Displaying images with noise |
| 32 | +plt.figure(figsize=(20, 2)) |
| 33 | +for i in range(1,10): |
| 34 | + ax = plt.subplot(1, 10, i) |
| 35 | + plt.imshow(x_test_noisy[i].reshape(28, 28), cmap="binary") |
| 36 | +plt.show() |
| 37 | + |
| 38 | + |
| 39 | +model = Sequential() |
| 40 | +model.add(Conv2D(32, (3, 3), activation='relu', padding='same', input_shape=(28, 28, 1))) |
| 41 | +model.add(MaxPooling2D((2, 2), padding='same')) |
| 42 | +model.add(Conv2D(8, (3, 3), activation='relu', padding='same')) |
| 43 | +model.add(MaxPooling2D((2, 2), padding='same')) |
| 44 | +model.add(Conv2D(8, (3, 3), activation='relu', padding='same')) |
| 45 | + |
| 46 | + |
| 47 | +model.add(MaxPooling2D((2, 2), padding='same')) |
| 48 | + |
| 49 | +model.add(Conv2D(8, (3, 3), activation='relu', padding='same')) |
| 50 | +model.add(UpSampling2D((2, 2))) |
| 51 | +model.add(Conv2D(8, (3, 3), activation='relu', padding='same')) |
| 52 | +model.add(UpSampling2D((2, 2))) |
| 53 | +model.add(Conv2D(32, (3, 3), activation='relu')) |
| 54 | +model.add(UpSampling2D((2, 2))) |
| 55 | +model.add(Conv2D(1, (3, 3), activation='relu', padding='same')) |
| 56 | + |
| 57 | +model.compile(optimizer='adam', loss='mean_squared_error') |
| 58 | + |
| 59 | +model.summary() |
| 60 | + |
| 61 | +model.fit(x_train_noisy, x_train, epochs=10, batch_size=256, shuffle=True, |
| 62 | + validation_data=(x_test_noisy, x_test)) |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +model.evaluate(x_test_noisy, x_test) |
| 67 | + |
| 68 | +model.save('denoising_autoencoder.model') |
| 69 | + |
| 70 | +no_noise_img = model.predict(x_test_noisy) |
| 71 | + |
| 72 | +plt.figure(figsize=(40, 4)) |
| 73 | +for i in range(10): |
| 74 | + # display original |
| 75 | + ax = plt.subplot(3, 20, i + 1) |
| 76 | + plt.imshow(x_test_noisy[i].reshape(28, 28), cmap="binary") |
| 77 | + |
| 78 | + # display reconstructed (after noise removed) image |
| 79 | + ax = plt.subplot(3, 20, 40 +i+ 1) |
| 80 | + plt.imshow(no_noise_img[i].reshape(28, 28), cmap="binary") |
| 81 | + |
| 82 | +plt.show() |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
0 commit comments