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

AUTOENCODR - Deep Learning

The document describes the implementation of an autoencoder model for anomaly detection in image data using Keras. It includes the architecture of the autoencoder, training process, calculation of reconstruction errors, and visualization of anomalies through plots and heatmaps. The model identifies frames with high reconstruction errors as anomalies and displays them alongside their corresponding heatmaps.

Uploaded by

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

AUTOENCODR - Deep Learning

The document describes the implementation of an autoencoder model for anomaly detection in image data using Keras. It includes the architecture of the autoencoder, training process, calculation of reconstruction errors, and visualization of anomalies through plots and heatmaps. The model identifies frames with high reconstruction errors as anomalies and displays them alongside their corresponding heatmaps.

Uploaded by

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

def build_autoencoder(input_shape=(64, 64, 1)):

input_img = Input(shape=input_shape)

# Encoder
x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(16, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)

# Bottleneck
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)

# Decoder
x = UpSampling2D((2, 2))(x) # First upsampling, from 8x8 to 16x16
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x) # Second upsampling, from 16x16 to 32x32
x = Conv2D(16, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x) # Third upsampling, from 32x32 to 64x64
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)

# Output layer with 64x64 dimensions


decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

autoencoder = Model(input_img, decoded)


return autoencoder
autoencoder=build_autoencoder()
# Build and compile the autoencoder model
autoencoder.compile(optimizer='adam', loss='mse')

history = autoencoder.fit(train_data, train_data, epochs=50, batch_size=32,


validation_split=0.1)

import numpy as np

# Function to calculate reconstruction errors


def calculate_reconstruction_errors(model, data):
reconstructions = model.predict(data)
errors = np.mean(np.square(data - reconstructions), axis=(1, 2, 3))
return errors

# Calculate reconstruction errors on the test set


test_errors = calculate_reconstruction_errors(autoencoder, test_data)

# Define an anomaly threshold (can be adjusted)


threshold = np.percentile(test_errors, 95) # Setting threshold to 95th percentile
of errors
print("Anomaly threshold:", threshold)

# Identify frames with errors above the threshold as anomalies


anomalies = test_errors > threshold
print(f"Number of anomalies detected: {np.sum(anomalies)}")

import matplotlib.pyplot as plt

# Plot the reconstruction error across all test frames


plt.figure(figsize=(10, 6))
plt.plot(test_errors, label="Reconstruction Error")
plt.axhline(y=threshold, color='r', linestyle='--', label="Anomaly Threshold")
plt.xlabel("Frame")
plt.ylabel("Reconstruction Error")
plt.legend()
plt.title("Reconstruction Error across Test Frames")
plt.show()

def display_anomalous_frames(data, errors, threshold, num_frames=5):


anomaly_indices = np.where(errors > threshold)[0]
selected_indices = np.random.choice(anomaly_indices, min(num_frames,
len(anomaly_indices)), replace=False)

plt.figure(figsize=(15, 5))
for i, idx in enumerate(selected_indices):
frame = data[idx] # Reshape frame to 64x64 for visualization
plt.subplot(1, num_frames, i + 1)
plt.imshow(frame, cmap='gray')
plt.title(f"Anomalous Frame {idx} - Error: {errors[idx]:.4f}")
plt.axis('off')

plt.show()

# Display anomalous frames


display_anomalous_frames(test_data, test_errors, threshold)

import cv2
import matplotlib.pyplot as plt

def calculate_pixel_wise_errors(original, reconstructed):


"""
calculate the reconstruction error per pixel between the original image and the
reconstructed image.
"""
pixel_errors = np.abs(original - reconstructed)
return pixel_errors

def display_anomaly_heatmap(data, model, threshold, num_frames=5):


"""
Displays an anomaly heat map for images containing anomalies.
"""
# Predicting reconstructions
reconstructions = model.predict(data)

# Find the clues of abnormal images


errors = np.mean(np.square(data - reconstructions), axis=(1, 2, 3))
anomaly_indices = np.where(errors > threshold)[0]

# Select a subset of abnormal images


selected_indices = np.random.choice(anomaly_indices, min(num_frames,
len(anomaly_indices)), replace=False)

# Show heatmaps for each abnormal frame


plt.figure(figsize=(15, 5))
for i, idx in enumerate(selected_indices):
original_frame = data[idx].reshape(64, 64) # Original Image
reconstructed_frame = reconstructions[idx].reshape(64, 64) # Reconstructed
Image

# Calculate errors per pixel


pixel_errors = calculate_pixel_wise_errors(original_frame,
reconstructed_frame)

# Normalize for heatmap visualization


heatmap = cv2.normalize(pixel_errors, None, 0, 255, cv2.NORM_MINMAX)

# Show original image, reconstruction and heat map


plt.subplot(3, num_frames, i + 1)
plt.imshow(original_frame, cmap='gray')
plt.title(f"Original Frame {idx}")
plt.axis('off')

plt.subplot(3, num_frames, i + 1 + num_frames)


plt.imshow(reconstructed_frame, cmap='gray')
plt.title(f"Reconstructed Frame {idx}")
plt.axis('off')

plt.subplot(3, num_frames, i + 1 + 2 * num_frames)


plt.imshow(original_frame, cmap='gray')
plt.imshow(heatmap, cmap='jet', alpha=0.6) # Overlay the heat map
plt.title(f"Anomaly Heatmap {idx}")
plt.axis('off')

plt.tight_layout()
plt.show()

# Using the function


display_anomaly_heatmap(test_data, autoencoder, threshold)

You might also like