# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + colab={"base_uri": "https://localhost:8080/", "height": 34} colab_type="code" id="bO_R6pSVvWm2" outputId="a64eacf9-5fb4-460d-89b9-cbeb4827bf7e"
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Sequential, load_model
from keras.layers.core import Activation
from keras.models import Model
from keras import backend as K
from keras.utils import np_utils

from keras.datasets import mnist
import numpy as np

import matplotlib.pyplot as plt

from keras import regularizers

# + colab={"base_uri": "https://localhost:8080/", "height": 52} colab_type="code" id="xjh677yWvibw" outputId="a5fed97a-b4cc-485c-a1bc-e4ce5f16d37f"
(x_train, _), (x_test, _) = mnist.load_data()

x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1)) 
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1)) 

print("x_train shape is: ", x_train.shape)
print("x_test shape is: ", x_test.shape)


# + colab={} colab_type="code" id="ksTfirTyvndN"
# Add noise to original images
noise_factor = 0.5
x_train_noisy = x_train + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_train.shape) 
x_test_noisy = x_test + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_test.shape) 

# Clip the images to maximum value of 1 and minimum value to 0 since we will be using binary cross entropy loss
x_train_noisy = np.clip(x_train_noisy, 0., 1.)
x_test_noisy = np.clip(x_test_noisy, 0., 1.)

# + colab={"base_uri": "https://localhost:8080/", "height": 125} colab_type="code" id="T-sLvzQ1v11r" outputId="b973d1d8-4ba9-47b7-f646-ce4d4f66201f"
# Plot 9 noisy images
n = 10
plt.figure(figsize=(20, 2))
for i in range(1, n):
    ax = plt.subplot(1, n, i)
    plt.imshow(x_test_noisy[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
plt.show()

# + colab={"base_uri": "https://localhost:8080/", "height": 2253} colab_type="code" id="dIzfoC6xv80i" outputId="33e74996-9426-477d-9bc7-536bab366ec5"
# Input placeholder
input_img = Input(shape=(28, 28, 1))

# Encoder architecture
x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)

# Decoder architecture
x = Conv2D(32, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

# Autoencoder model
autoencoder = Model(input_img, decoded)

autoencoder.summary()

# Compile the model
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')

# train the model
autoencoder.fit(x_train_noisy, x_train,
                epochs=50,
                batch_size=256,
                shuffle=True,
                validation_data=(x_test_noisy, x_test))

# + colab={"base_uri": "https://localhost:8080/", "height": 244} colab_type="code" id="N2DiisvWwrPS" outputId="0ef1856f-bf45-4857-ae36-2b3439aa4d50"
# decoded_imgs - decoded representation of test image
decoded_imgs = autoencoder.predict(x_test_noisy)

n = 10  # Display 9 images
plt.figure(figsize=(20, 4))
for i in range(1, n):
    # display original
    ax = plt.subplot(2, n, i + 1)
    plt.imshow(x_test_noisy[i].reshape(28, 28))
    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(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
plt.show()

# + colab={} colab_type="code" id="GLXri7WFxxwY"


StarcoderdataJupyterScriptsDedupFiltered