image_output_1.png
from keras.layers import  Input,Conv2D,MaxPool2D, UpSampling2D,Dense, Dropout

from keras.models import Model

from tensorflow.keras import layers, losses

from tensorflow.keras import losses

import tensorflow as tf

inputs = Input(shape=(28, 28, 1))
x = Conv2D(32, 3, activation='relu', padding='same')(inputs)
x = MaxPool2D()(x)
x = Dropout(0.2)(x)
x = Conv2D(32, 3, activation='relu', padding='same')(x)
encoded = MaxPool2D()(x)
x = Conv2D(32, 3, activation='relu', padding='same')(encoded)
x = UpSampling2D()(x)
x = Dropout(0.2)(x)
x = Conv2D(32, 3, activation='relu', padding='same')(x)
x = UpSampling2D()(x)
decoded = Conv2D(1, 3, activation='sigmoid', padding='same')(x)

from tensorflow.keras import losses
autoencoder1 = Model(inputs, decoded)
autoencoder1.compile(optimizer='adam', loss=losses.binary_crossentropy)
autoencoder1.summary()

from tensorflow.keras import layers, losses
class Denoise(Model):
    """__init__ constructor in OOP
    This method called when an object is created from the class and 
    it allow the class to initialize the attributes of a class.
    super()function used to give access to 
    methods and properties of a parent or sibling class
    """
    def __init__(self):
        super(Denoise, self).__init__()
        self.encoder = tf.keras.Sequential([layers.Input(shape=(28, 28, 1)), layers.Conv2D(16, (3, 3), activation='relu', padding='same', strides=2), layers.Conv2D(8, (3, 3), activation='relu', padding='same', strides=2)])
        self.decoder = tf.keras.Sequential([layers.Conv2DTranspose(8, kernel_size=3, strides=2, activation='relu', padding='same'), layers.Conv2DTranspose(16, kernel_size=3, strides=2, activation='relu', padding='same'), layers.Conv2D(1, kernel_size=(3, 3), activation='sigmoid', padding='same')])
    def call(self, x):
        encoded = self.encoder(x)
        decoded = self.decoder(encoded)
        return decoded
autoencoder = Denoise()

autoencoder.compile(optimizer='rmsprop', loss=losses.MeanSquaredError())

history = autoencoder.fit(noisy_train_data, train_data, epochs=10, batch_size=256, shuffle=True, validation_data=(noisy_test_data, test_data))