0% found this document useful (0 votes)
3 views6 pages

Ex No: 4 Autoencoders DATE: 21.01.2025

The document outlines the process of building an autoencoder neural network to denoise images, including architecture specifications, training procedures, and evaluation methods. It details steps for data preparation, model training using Mean Squared Error as the loss function, and visualization of results. The code provided implements these steps, demonstrating how to handle noisy images and visualize original, noisy, and denoised outputs.

Uploaded by

MNBVCXZ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Ex No: 4 Autoencoders DATE: 21.01.2025

The document outlines the process of building an autoencoder neural network to denoise images, including architecture specifications, training procedures, and evaluation methods. It details steps for data preparation, model training using Mean Squared Error as the loss function, and visualization of results. The code provided implements these steps, demonstrating how to handle noisy images and visualize original, noisy, and denoised outputs.

Uploaded by

MNBVCXZ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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 tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense, Dropout
from tensorflow.keras.optimizers import Adam
from skimage import io, util, img_as_float
import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection
import train_test_split

# Load the uploaded image


image_path = "ex_4.jpeg"
image = io.imread(image_path, as_gray=True)
image = img_as_float(image)

# Display the uploaded original image


plt.figure(figsize=(8, 8))
plt.imshow(image, cmap='gray')
plt.title("Uploaded Original Image")
plt.axis("off")
plt.show()

# 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 = np.array(x_data).reshape(-1, image.shape[0], image.shape[1], 1)
y_data = [image] * 50 # The original image as the target
y_data = np.array(y_data).reshape(-1, image.shape[0], image.shape[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=(image.shape[0], image.shape[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)
autoencoder.compile(optimizer=Adam(learning_rate=0.001), loss='binary_crossentropy')

# Train the model


history = autoencoder.fit(
x_train, y_train,
epochs=20,
batch_size=8,
shuffle=True,
validation_data = (x_test, y_test) )
# Generate denoised image
denoised_image = autoencoder.predict(noisy_image.reshape(1, image.shape[0],
image.shape[1], 1))
denoised_image = denoised_image.reshape(image.shape)

# Display the results


plt.figure(figsize=(20, 8))

# Original image
plt.subplot(1, 3, 1)
plt.imshow(image, cmap='gray')
plt.title("Original Image")
plt.axis("off")

# Noisy image
plt.subplot(1, 3, 2)
plt.imshow(noisy_image, cmap='gray')
plt.title("Noisy Image")
plt.axis("off")

# Denoised image
plt.subplot(1, 3, 3)
plt.imshow(denoised_image, cmap='gray')
plt.title("Denoised Image")
plt.axis("off")
plt.show()

Output:

You might also like