vertopal.com_LSTM_Autoencoder_
vertopal.com_LSTM_Autoencoder_
import tarfile
with tarfile.open(gz_file_path, 'r:gz') as tar:
tar.extractall('/content/UCSD_Anomaly_Dataset')
import numpy as np
import os
from PIL import Image
# Example usage
train_directory_ped1 =
'/content/UCSD_Anomaly_Dataset/UCSD_Anomaly_Dataset.v1p2/UCSDped1/
Train'
train_directory_ped2 =
'/content/UCSD_Anomaly_Dataset/UCSD_Anomaly_Dataset.v1p2/UCSDped2/
Train'
train_videos_ped1 = load_and_preprocess_frames(train_directory_ped1)
train_videos_ped2 = load_and_preprocess_frames(train_directory_ped2)
train_videos = train_videos_ped1 + train_videos_ped2
def split_into_sequences(video_frames, sequence_length=16):
num_frames = len(video_frames)
sequences = []
for i in range(0, num_frames - sequence_length + 1,
sequence_length):
sequence = video_frames[i:i + sequence_length]
sequences.append(sequence)
return np.array(sequences)
/usr/local/lib/python3.10/dist-packages/keras/src/layers/rnn/
rnn.py:204: UserWarning: Do not pass an `input_shape`/`input_dim`
argument to a layer. When using Sequential models, prefer using an
`Input(shape)` object as the first layer in the model instead.
super().__init__(**kwargs)
<keras.src.callbacks.history.History at 0x7c147a365bd0>
reconstruction_errors = [
mean_squared_error(original.flatten(), reconstructed.flatten())
for original, reconstructed in zip(test_sequences,
reconstructed_test_data)
]
import numpy as np
print(f"Precision: {precision:.4f}")
print(f"Recall: {recall:.4f}")
print(f"F1 Score: {f1:.4f}")
Precision: 0.5364
Recall: 0.8519
F1 Score: 0.6583
INTERPRETATION
** Recall (0.8519):** A high recall means that the model is good at identifying most of the actual
anomalies.
** Precision (0.5364):** Precision is lower, indicating that there are some normal sequences
labeled as anomalies.
The model is slightly too sensitive, flagging some normal sequences as anomalies.