How can autoencoder be generated using an encoder and decoder using Python?


Tensorflow is a machine learning framework that is provided by Google. It is an open-source framework used in conjunction with Python to implement algorithms, deep learning applications and much more. It is used in research and for production purposes.

The ‘tensorflow’ package can be installed on Windows using the below line of code −

pip install tensorflow

Tensor is a data structure used in TensorFlow. It helps connect edges in a flow diagram. This flow diagram is known as the ‘Data flow graph’. Tensors are nothing but a multidimensional array or a list.

Keras was developed as a part of research for the project ONEIROS (Open ended Neuro-Electronic Intelligent Robot Operating System). Keras is a deep learning API, which is written in Python. It is a high-level API that has a productive interface that helps solve machine learning problems. It runs on top of Tensorflow framework. It was built to help experiment in a quick manner. It provides essential abstractions and building blocks that are essential in developing and encapsulating machine learning solutions.

Keras is already present within the Tensorflow package. It can be accessed using the below line of code.

import tensorflow
from tensorflow import keras

The Keras functional API helps create models that are more flexible in comparison to models created using sequential API. The functional API can work with models that have non-linear topology, can share layers and work with multiple inputs and outputs. A deep learning model is usually a directed acyclic graph (DAG) that contains multiple layers. The functional API helps build the graph of layers.

We are using Google Colaboratory to run the below code. Google Colab or Colaboratory helps run Python code over the browser and requires zero configuration and free access to GPUs (Graphical Processing Units). Colaboratory has been built on top of Jupyter Notebook. Following is the code snippet see how autoencoder be generated using an encoder and decoder −

Example

encoder_input = keras.Input(shape=(28, 28, 1), name="img")
print("Adding layers to the model")
x = layers.Conv2D(16, 3, activation="relu")(encoder_input)
x = layers.Conv2D(32, 3, activation="relu")(x)
x = layers.MaxPooling2D(3)(x)
x = layers.Conv2D(32, 3, activation="relu")(x)
x = layers.Conv2D(16, 3, activation="relu")(x)
print("Performing global max pooling")
encoder_output = layers.GlobalMaxPooling2D()(x)
print("Creating a model using the layers")
encoder = keras.Model(encoder_input, encoder_output, name="encoder")
print("More information about the model")
encoder.summary()

print("Reshaping the layers in the model")
x = layers.Reshape((4, 4, 1))(encoder_output)
x = layers.Conv2DTranspose(16, 3, activation="relu")(x)
x = layers.Conv2DTranspose(32, 3, activation="relu")(x)
x = layers.UpSampling2D(3)(x)
x = layers.Conv2DTranspose(16, 3, activation="relu")(x)
decoder_output = layers.Conv2DTranspose(1, 3, activation="relu")(x)

autoencoder = keras.Model(encoder_input, decoder_output, name="autoencoder")
print("More information about the autoencoder")
autoencoder.summary()

Code credit − https://www.tensorflow.org/guide/keras/functional

Output

Adding layers to the model
Performing global max pooling
Creating a model using the layers
More information about the model
Model: "encoder"
_________________________________________________________________
Layer (type)                Output Shape             Param #
=================================================================
img (InputLayer)            [(None, 28, 28, 1)]       0
_________________________________________________________________
conv2d (Conv2D)             (None, 26, 26, 16)       160
_________________________________________________________________
conv2d_1 (Conv2D)           (None, 24, 24, 32)       4640
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 8, 8, 32)          0
_________________________________________________________________
conv2d_2 (Conv2D)             (None, 6, 6, 32)       9248
_________________________________________________________________
conv2d_3 (Conv2D)             (None, 4, 4, 16)       4624
_________________________________________________________________
global_max_pooling2d          (Global (None, 16)       0
=================================================================
Total params: 18,672
Trainable params: 18,672
Non-trainable params: 0
_________________________________________________________________
Reshaping the layers in the model
More information about the autoencoder
Model: "autoencoder"
_________________________________________________________________
Layer (type)                Output Shape          Param #
=================================================================
img (InputLayer)            [(None, 28, 28, 1)]    0
_________________________________________________________________
conv2d (Conv2D)             (None, 26, 26, 16)    160
_________________________________________________________________
conv2d_1 (Conv2D)           (None, 24, 24, 32)    4640
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 8, 8, 32)       0
_________________________________________________________________
conv2d_2 (Conv2D)          (None, 6, 6, 32)       9248
_________________________________________________________________
conv2d_3 (Conv2D)          (None, 4, 4, 16)       4624
_________________________________________________________________
global_max_pooling2d       (Global (None, 16)       0
_________________________________________________________________
reshape (Reshape)          (None, 4, 4, 1)          0
_________________________________________________________________
conv2d_transpose (Conv2DTran (None, 6, 6, 16)       160
_________________________________________________________________
conv2d_transpose_1 (Conv2DTr (None, 8, 8, 32)       4640
_________________________________________________________________
up_sampling2d (UpSampling2D) (None, 24, 24, 32)       0
_________________________________________________________________
conv2d_transpose_2 (Conv2DTr (None, 26, 26, 16)       4624
_________________________________________________________________
conv2d_transpose_3 (Conv2DTr (None, 28, 28, 1)       145
=================================================================
Total params: 28,241
Trainable params: 28,241
Non-trainable params: 0
_________________________________________________________________

Explanation

  • Layers are added to the model.

  • Global max pooling is performed on these layers

  • A model is created using the layers.

  • More information about the model can be displayed using the ‘summary’ method.

  • Using the functional API, models are created after specifying the inputs and outputs for the graph-of-layers.

  • This indicates that a single graph can be used to generate multiple models.

  • Here, stack of layers are used to instantiate two models- an encoder that turns image inputs into 16-dimensional vectors and an autoencoder model that is used for training.

Updated on: 18-Jan-2021

180 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements