Skip to content

vedantgoswami/Autoencoder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Autoencoder

In this repository i will show how the Autoencoder works and it's application of denoising the images.

The idea of auto encoders is to allow a feed forward neural network to figure out how to best encode and decode certain data.Autoencoders are an unsupervised learning approach to some of issues and techniques such as dimensionality reduction, noise reduction and data compression.

But we will focus on basic bulding of autoencoder which comprises of an encoder and a decoder. And then wee will use this autoencoder for image denoising. I will be using the MNIST image dataset to keep it simple.

Structure of AutoEncoder

In the above diagram the encoder part compress the image passed in input by using the convolutional layers and pooling layers.The final output of the encoder will a compressed representation of image which is then passed to the decoder as input. The decoder will then use the Upsampling layers or Transposed convolutional layers to reconstruct the original image back.

What is UpSampling?
As Max pooling is a sampling strategy that picks the maximum value from a window. Upsampling is just reverse of it each value can be surrounded with zeros to upsample the layer.

What is Transposed Convolution?
Convolution + Upsampling
Transposed Convolutions are used to upsample the input feature map to a desired output feature map using some learnable parameters.

Let's Build The Model

We Require the following models:

import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow import keras
import cv2
import random
import numpy as np

We are using MNIST images dataset, it comprises of 70000 28 pixels by 28 pixels images of handwritten digits and 70000 vectors containing information on which digit each one is.

Loading Dataset

(x_train,y_train),(x_test,y_test) = keras.datasets.mnist.load_data()

Normalising Data

Normalising data by dividing it by 255 should improve activation functions performance - sigmoid function works more efficiently with data range 0.0-1.0. ``` x_train = x_train/255.0 x_test = x_test/255.0 ```

Encoder Part

    encoder_input = keras.Input(shape=(28, 28, 1), name='img')
    x = keras.layers.Flatten()(encoder_input)
    encoder_output = keras.layers.Dense(level*level, activation="relu")(x)
    encoder = keras.Model(encoder_input, encoder_output, name='encoder')

The Encoder model takes images as input of shape (28 x 28) and then flattening it to vector of pixels of shape (786,).

Decoder Part

    decoder_input = keras.layers.Dense(level*level, activation="relu")(encoder_output)
    x = keras.layers.Dense(784, activation="relu")(decoder_input)
    decoder_output = keras.layers.Reshape((28, 28, 1))(x)

The Decoder takes encoded images or the compressed image and tries to reconstruct it to (28 x 28).

My Encoder Sample

As we can see that there is a data loss due to compression as the image is diminished but still it is recognizable as 7.

Let's see how to Denoise the image using autoencoder.

The autoencoder tries to reconstruct the input data. So, if we give corrupted images as input, the autoencoder will try to reconstruct noisy images only.A small tweak is all that is required here. Instead of using the input and the reconstructed output to compute the loss, we can calculate the loss by using the ground truth image and the reconstructed image. This diagram illustrates my point wonderfully:

Now we will add noise to images.

def add_noise(img,random_chance=10):
    noisy = []
    for row in img:
        new_row = []
        for pix in row:
            if(random.choice(range(100))<= random_chance):
                new_val = random.uniform(0,1)
                new_row.append(new_val)
            else:
                new_row.append(pix)
                
        noisy.append(new_row)
    return np.array(noisy)

All this function does is iterate through each pixel and randomly, with a default of 5%, change the pixel to be white.

I had added noise with probability of 10% . So after passing the image through our autoencoder, most of the noise got removed but some of the image pixel were dead but after all the we got is recognizable.

After Trying this on mnist image i will now work on the real RGB images and try them to denoise.... soon.

About

In this repository i will show how the Autoencoder works and it's application of denoising the images.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors