EX NO: 4 AUTOENCODERS
DATE: 21.01.2025
AIM:
Build an autoencoder neural network architecture with:
• An input layer that matches the feature size of the dataset.
• One or more hidden layers in the encoder to reduce the dimensionality.
• One or more hidden layers in the decoder to reconstruct the original data.
• An output layer with the same size as the input data.
• Train the autoencoder on the training data.
• Use Mean Squared Error (MSE) as the loss function and an appropriate optimizer (e.g., Adam).
• Evaluate the model performance on the testing set.
• Visualize the reconstruction error.
• Compare the original and reconstructed data to assess the autoencoder’s ability to compress
and reconstruct the input data.
• Extract the latent space (the output of the encoder).
• Visualize the latent representations (e.g., using t-SNE or PCA) to examine how the model
compresses the data.
• Discuss any patterns or clusters you observe in the latent space and how well the encoder has
captured
Algorithm:
1. Read and normalize the grayscale image.
2. Apply Gaussian noise to the image.
3. Duplicate noisy and original images, reshape for training.
4. Create training and testing sets, Build a simple encoder-decoder model.
5. Train the autoencoder using noisy and clean images.
6. Use the trained model to denoise the noisy image.
7. Display original, noisy, and denoised images.
Code:
from [Link] import Model
from [Link] import Input, Dense, Dropout
from [Link] import Adam
from skimage import io, util, img_as_float
import [Link] as plt
import numpy as np
from sklearn.model_selection
import train_test_split
# Load the uploaded image
image_path = "ex_4.jpeg"
image = [Link](image_path, as_gray=True)
image = img_as_float(image)
# Display the uploaded original image
[Link](figsize=(8, 8))
[Link](image, cmap='gray')
[Link]("Uploaded Original Image")
[Link]("off")
[Link]()
# Add noise to the image
noise_factor = 0.5
noisy_image = util.random_noise(image, mode='gaussian', var=noise_factor**2)
# Prepare data for training
# Since we only have one image, we create multiple samples by sliding patches or duplicating
x_data = [noisy_image] * 50 # Duplicate the noisy image to create a dataset
x_data = [Link](x_data).reshape(-1, [Link][0], [Link][1], 1)
y_data = [image] * 50 # The original image as the target
y_data = [Link](y_data).reshape(-1, [Link][0], [Link][1], 1)
# Split data into training and testing sets
x_train, x_test, y_train, y_test = train_test_split(x_data, y_data, test_size=0.2, random_state=42)
# Define the autoencoder model
input_img = Input(shape=([Link][0], [Link][1], 1))
encoded = Dense(64, activation='relu')(input_img)
encoded = Dropout(0.2)(encoded)
decoded = Dense(1, activation='sigmoid')(encoded)
# Build the autoencoder
autoencoder = Model(input_img, decoded)
[Link](optimizer=Adam(learning_rate=0.001), loss='binary_crossentropy')
# Train the model
history = [Link](
x_train, y_train,
epochs=20,
batch_size=8,
shuffle=True,
validation_data = (x_test, y_test) )
# Generate denoised image
denoised_image = [Link](noisy_image.reshape(1, [Link][0],
[Link][1], 1))
denoised_image = denoised_image.reshape([Link])
# Display the results
[Link](figsize=(20, 8))
# Original image
[Link](1, 3, 1)
[Link](image, cmap='gray')
[Link]("Original Image")
[Link]("off")
# Noisy image
[Link](1, 3, 2)
[Link](noisy_image, cmap='gray')
[Link]("Noisy Image")
[Link]("off")
# Denoised image
[Link](1, 3, 3)
[Link](denoised_image, cmap='gray')
[Link]("Denoised Image")
[Link]("off")
[Link]()
Output: