659451a19 DL Exp5
659451a19 DL Exp5
Experiment No.05
(PART A : TO BE REFFERED BY
STUDENTS)
Aim: Design the architecture and implement the autoencoder model for Image Compression
Prerequisite: Basic Mathematics and Statistical Concepts, Linear Algebra, Machine Learning
Outcome:
After successful completion of this experiment students will be able to
The above figure is a two-layer vanilla autoencoder with one hidden layer. In deep learning
terminology, you will often notice that the input layer is never taken into account while counting the
total number of layers in an architecture. The total layers in an architecture only comprises of the
number of hidden layers and the ouput layer.
As shown in the image above, the input and output layers have the same number of neurons.
Autoencoder can be broken in to three parts
Encoder: this part of the network compresses or downsamples the input into a fewer number of bits.
The space represented by these fewer number of bits is often called the latent-space or bottleneck.
The bottleneck is also called the "maximum point of compression" since at this point the input is
compressed the maximum. These compressed bits that represent the original input are together called
an “encoding” of the input.
Decoder: this part of the network tries to reconstruct the input using only the encoding of the input.
When the decoder is able to reconstruct the input exactly as it was fed to the encoder, you can say
that the encoder is able to produce the best encodings for the input with which the decoder is able to
reconstruct well!
There are variety of autoencoders, such as the convolutional autoencoder, denoising autoencoder,
variational autoencoder and sparse autoencoder. However, as you read in the introduction, you'll
only focus on the convolutional and denoising ones in this tutorial.Convolutional Autoencoders in
Python with Keras
Algorithm:
# Import necessary libraries
import tensorflow as tf
from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D,
UpSampling2D from tensorflow.keras.models import Model
# Define the autoencoder model
def build_autoencoder(input_shape, encoding_dim):
# Encoder
input_img = Input(shape=input_shape)
x = Conv2D(32, (3, 3), activation='relu',
padding='same')(input_img) x = MaxPooling2D((2, 2),
padding='same')(x)
x = Conv2D(64, (3, 3), activation='relu',
padding='same')(x) encoded = MaxPooling2D((2, 2),
padding='same')(x)
# Decoder
x = Conv2D(64, (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)
# Build and compile the autoencoder model
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
PART B
(PART B : TO BE COMPLETED BY
STUDENTS)
(Students must submit the soft copy as per following segments within two hours of the practical.
The soft copy must be uploaded at the end of the practical)
import os
import keras
from keras.datasets import mnist
from keras.layers import Dense
from keras.models import Sequential
import matplotlib.pyplot as plt
# Preprocess images
x_train = x_train.reshape(-1, 784) / 255.0
x_test = x_test.reshape(-1, 784) / 255.0
# Encoder definition
encoder = Sequential([
Dense(128, activation='relu', input_shape=(784,)),
Dense(64, activation='relu')
])
# We can compile the model here, otherwise there will be a warning when it is
# used, but it is optional.
# Decoder definition
decoder = Sequential([
Dense(128, activation='relu', input_shape=(64,)),
Dense(784, activation='sigmoid')
])
# We can compile the model here, otherwise there will be a warning when it is
# used, but it is optional.
# Autoencoder definition
autoencoder = Sequential([
encoder,
decoder
])
# Evaluation
loss = autoencoder.evaluate(x_test, x_test, verbose=0)
print("Test Loss:", loss)
# Save models
encoder_model_path = 'encoder_model.h5'
decoder_model_path = 'decoder_model.h5'
encoder.save(encoder_model_path)
decoder.save(decoder_model_path)
def get_file_size(file_path):
size = os.path.getsize(file_path)
for unit in ['bytes', 'KB', 'MB', 'GB', 'TB']:
if size < 1024:
return f"{size:.2f} {unit}"
size /= 1024
encoder_model_size = get_file_size(encoder_model_path)
decoder_model_size = get_file_size(decoder_model_path)
print("Encoder model size:", encoder_model_size)
print("Decoder model size:", decoder_model_size)
# Visualization
n = 10
plt.figure(figsize=(20, 4))
for i in range(n):
# Original image
ax = plt.subplot(2, n, i + 1)
plt.imshow(x_test[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
# Reconstructed image
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()
Epoch 1/10
Epoch 2/10
Epoch 3/10
Epoch 4/10
Epoch 5/10
Epoch 6/10
Epoch 7/10
Epoch 8/10
Epoch 9/10
Epoch 10/10
Conclusion:
Implementing an autoencoder for image compression highlights the balance between reducing data
size and maintaining image quality. Careful tuning of model architecture and training parameters is
crucial for achieving effective compression without significant loss of visual fidelity.
Question of Curiosity
1. How do autoencoders reduce image size?
Ans: Autoencoders reduce image size by encoding the input image into a lower-dimensional latent
space using an encoder network. This compressed representation captures the essential features of
the image. The decoder network then reconstructs the image from this compressed representation,
ideally preserving important details while discarding less critical information.
2. What are the main challenges in using autoencoders for image compression?
Ans: The main challenges include maintaining high image quality while achieving a significant
compression ratio. Over-compression can lead to loss of important details and artifacts, while under-
compression may not sufficiently reduce file size. Additionally, selecting the right network
architecture and hyperparameters requires careful tuning to balance compression and reconstruction
performance.