<jupyter_start><jupyter_text>Image Denoising with Autoencoders Introduction and Importing Libraries___Note: If you are starting the notebook from this task, you can run cells from all previous tasks in the kernel by going to the top menu and then selecting Kernel > Restart and Run All___<jupyter_code>import numpy as np

from tensorflow.keras.datasets import mnist
from matplotlib import pyplot as plt
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Dense, Input
from tensorflow.keras.callbacks import EarlyStopping, LambdaCallback
from tensorflow.keras.utils import to_categorical

%matplotlib inline<jupyter_output><empty_output><jupyter_text>Data Preprocessing___Note: If you are starting the notebook from this task, you can run cells from all previous tasks in the kernel by going to the top menu and then selecting Kernel > Restart and Run All___<jupyter_code>(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.astype('float')/255.
x_test  = x_test.astype('float')/255.

x_train = np.reshape(x_train, (60000, 784))
x_test  = np.reshape(x_test, (10000, 784))<jupyter_output>Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11493376/11490434 [==============================] - 0s 0us/step<jupyter_text>Adding Noise___Note: If you are starting the notebook from this task, you can run cells from all previous tasks in the kernel by going to the top menu and then selecting Kernel > Restart and Run All___<jupyter_code>x_train_noisy = x_train + np.random.rand(60000, 784)*0.9
x_test_noisy  = x_test + np.random.rand(10000, 784)*0.9
x_train_noisy = np.clip(x_train_noisy, 0., 1.)
x_test_noisy  = np.clip(x_test_noisy, 0., 1.)
def Plot(x, p, labels = False):
    plt.figure(figsize = (20, 2))
    for i in range(10):
        plt.subplot(1, 10, i + 1)
        plt.imshow(x[i].reshape(28,28), cmap = 'viridis')
        plt.xticks([])
        plt.yticks([])
        if labels:
            plt.xlabel(np.argmax(p[i]))
    plt.show()
    
Plot(x_train, None)
Plot(x_train_noisy, None)<jupyter_output><empty_output><jupyter_text>Building and Training a Classifier___Note: If you are starting the notebook from this task, you can run cells from all previous tasks in the kernel by going to the top menu and then selecting Kernel > Restart and Run All___<jupyter_code>classifier = Sequential([
    Dense(256, activation = 'relu', input_shape = (784,)),
    Dense(256, activation = 'relu'),
    Dense(256, activation = 'softmax')
])

classifier.compile(optimizer = 'adam',
                  loss = 'sparse_categorical_crossentropy',
                  metrics = ['accuracy'])

classifier.fit(x_train, y_train, batch_size = 512, epochs = 3)
loss, acc = classifier.evaluate(x_test, y_test)

print(acc)
loss, acc = classifier.evaluate(x_test_noisy, y_test)

print(acc)<jupyter_output>313/313 [==============================] - 0s 1ms/step - loss: 11.9475 - accuracy: 0.1621
0.16210000216960907<jupyter_text>Building the Autoencoder___Note: If you are starting the notebook from this task, you can run cells from all previous tasks in the kernel by going to the top menu and then selecting Kernel > Restart and Run All___<jupyter_code>input_image = Input(shape = (784,))
encoded = Dense(64, activation = 'relu')(input_image)
decoded = Dense(784, activation = 'sigmoid')(encoded)

autoencoder = Model(input_image, decoded)
autoencoder.compile(loss = 'binary_crossentropy', optimizer = 'adam')<jupyter_output><empty_output><jupyter_text>Training the Autoencoder___Note: If you are starting the notebook from this task, you can run cells from all previous tasks in the kernel by going to the top menu and then selecting Kernel > Restart and Run All___<jupyter_code>autoencoder.fit(
    x_train_noisy, 
    x_train,
    epochs = 100,
    batch_size = 512,
    validation_split = 0.2,
    verbose = False,
    callbacks = [
        EarlyStopping(monitor = 'val_loss', patience = 5),
        LambdaCallback(on_epoch_end = lambda e,l: print('{:.3f}'.format(l['val_loss']), end = ' _ '))
    ]
)

print(' _ ')
print('Training is complete!')<jupyter_output>0.261 _ 0.236 _ 0.204 _ 0.187 _ 0.176 _ 0.166 _ 0.158 _ 0.151 _ 0.146 _ 0.141 _ 0.138 _ 0.134 _ 0.132 _ 0.129 _ 0.127 _ 0.125 _ 0.123 _ 0.122 _ 0.121 _ 0.119 _ 0.118 _ 0.117 _ 0.117 _ 0.116 _ 0.115 _ 0.115 _ 0.114 _ 0.114 _ 0.113 _ 0.113 _ 0.113 _ 0.113 _ 0.112 _ 0.112 _ 0.112 _ 0.112 _ 0.112 _ 0.112 _ 0.112 _ 0.112 _ 0.112 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.111 _ 0.110 _ 0.110 _ 0.110 _  _ 
Training is complete!<jupyter_text>Denoised Images___Note: If you are starting the notebook from this task, you can run cells from all previous tasks in the kernel by going to the top menu and then selecting Kernel > Restart and Run All___<jupyter_code>preds = autoencoder.predict(x_test_noisy)
Plot(x_test_noisy, None)
Plot(preds, None)
loss, acc = classifier.evaluate(preds, y_test)
print(acc)<jupyter_output>313/313 [==============================] - 0s 2ms/step - loss: 0.2170 - accuracy: 0.9334
0.9333999752998352<jupyter_text>Composite Model___Note: If you are starting the notebook from this task, you can run cells from all previous tasks in the kernel by going to the top menu and then selecting Kernel > Restart and Run All___<jupyter_code>input_image=Input(shape=(784,))
x=autoencoder(input_image)
y=classifier(x)

denoise_and_classfiy = Model(input_image, y)
predictions=denoise_and_classfiy.predict(x_test_noisy)
Plot(x_test_noisy, predictions, True)
Plot(x_test, to_categorical(y_test), True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11413770053475936, 'fraction_numerical': 0.09792780748663102, 'mean_word_length': 4.231643356643357, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 37, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '1552', 'license': 'MIT', 'n_tokens_mistral': 2558, 'n_tokens_neox': 2105, 'n_words': 605, 'path': 'Image_Noise_Reduction.ipynb', 'repo_name': 'Hevenicio/Image-Noise-Reduction-with-Auto-encoders-using-TensorFlow', 'size': '5984'}