Detecting Anomaly in ECG Data Using AutoEncoder with PyTorch

Description
  • Date: January 14, 2024
  • Categories: Deep LearningPython

 

View on Github

Project Overview

The project “Detecting Anomaly in ECG Data Using AutoEncoder with PyTorch” represents a leap forward in cardiac health care. Aimed at integrating cutting-edge machine learning techniques into everyday patient monitoring, this system is built to analyze ECG data and identify anomalies in real time. Our ambition is to transition from reactive to proactive healthcare, alerting patients and providers at the earliest signs of irregular heart activity.

Motivation Behind the Project

The “Detecting Anomaly in ECG Data Using AutoEncoder with PyTorch” project took root from two main inspirations. The first was the compelling use of deep generative models—typically harnessed for creation—for anomaly detection. The challenge of applying a generative approach, rather than conventional classification methods, to this problem presented an exciting and innovative avenue that piqued my interest. As a result, I delved into a thorough exploration of autoencoders and their application to carry out this project.

Secondly, Cardiovascular disease remains a growing problem in global health, and its reach is only surpassed by the urgency with which it needs to be addressed. The current state of ECG analysis is a manual, sluggish process that is at odds with the rapid progression of cardiac events. Recognizing the potential impact of timely and accurate detection of cardiac anomalies, my goal was to bridge this dangerous delay with a proactive, automated solution. By bringing the precision and speed of deep learning to ECG data, the project aspires to offer not just a technological advancement, but a lifeline.

Thus, the project was born from a dual desire: to explore the uncharted application of a deep generative model in anomaly detection and to address a pressing health issue with a technology-led intervention.

Preparing the Data

The foundation of this initiative relied on the ECG data supplied by TensorFlow which had around 4000 rows of data. Although the initial goal was to focus on tumour detection using image data, the limited availability of training datasets prompted a shift in the project’s direction towards ECG data. This change not only matched my interest in time-series data but also reflected my dedication to contributing to research in cardiac health. Upon the initial analysis of the data, here is a depiction of what a normal and abnormal ECG resembles where 1 stands for normal ECG and 0 stands for abnormal ECG.

Detecting Anomaly in ECG Data Using AutoEncoder with PyTorch
Image 1. Visualizing Normal ECG(1) AND Abnormal ECG(0)

Understanding the Methodology

The concept involved inputting normal ECG data into the Encoder and utilizing a Decoder to regenerate it, with the mean absolute error(MAE) between the actual and reconstructed data serving as the performance metric. As the model learns to regenerate normal ECG data with minimal error, we establish a threshold based on training and validation losses. The rationale is that when abnormal ECG data is presented, the model, having not been exposed to such data during training, performs poorly in regeneration, leading to a high error. Thus, when this error surpasses the set threshold, we classify it as abnormal. This forms the fundamental idea and concept behind anomaly detection using autoencoders.

Opting for an Autoencoder LSTM network implemented through PyTorch proved crucial due to its effectiveness in capturing the complex temporal patterns inherent in ECG data. Given the continuous nature of the data, LSTM was a logical choice and straightforward to implement. The dual-stage architecture of the LSTM autoencoder, involving compression and reconstruction of the input, provided an optimal framework for detecting deviations from typical heart patterns. Additional elements incorporated in the process included weight decay for regularization and the use of the Adam optimizer to address errors.

The model underwent training for 100 epochs, and I implemented a logic to visualize the actual reconstruction at every 5th iteration. This approach allowed me to observe the model’s learning process in regenerating ECG data throughout training.

Detecting Anomaly in ECG Data Using AutoEncoder with PyTorch
Image 2: Visualizing input vs reconstruction of ECG data at epoch 1

We can see at the first epoch, the reconstruction sequence is very poor  but it got better over the training and the best model resulted in an error of less than 10. In the below image, we can see how the model developed over the training.

Image 3. Visualizing input vs reconstruction of ECG data at epoch 80

Results

Following the successful completion of training, I visualized the distribution of validation errors using a density plot to identify a threshold. Determining the threshold involved a trial-and-error method, as there isn’t a standardized approach for this aspect.

Image 4. Distribution of loss in normal data

The analysis of error distribution reveals interesting insights. The majority of errors in the normal ECG data fall within the range of 5-13, with very few exceeding 20. This observation provides an understanding of the model’s performance on normal data.

Image 5. Distribution of loss in anomaly data

Additionally, the density distribution analysis of anomaly data demonstrates high error rates, consistently exceeding 20. This outcome aligns with our objective, indicating that the model struggles to regenerate abnormal data. This discrepancy in error rates between normal and anomaly data reaffirms the effectiveness of the model in discerning and accurately predicting abnormal ECG patterns, which is a key success criterion for anomaly detection

After conducting careful trials, I opted to set the threshold at 25. This threshold resulted in inaccurate predictions for 4 out of 145 instances of normal ECG, while accurately predicting all 145 instances of abnormal ECG, achieving a 100% accuracy in predicting abnormal ECG cases and with an overall accuracy of 98.26% on the test data or unseen data.

The following output displays the results obtained from the test data, showcasing both the actual and reconstructed ECG data, along with corresponding loss values. This visualization serves as evidence of the model’s commendable performance, as reflected in its ability to accurately reconstruct ECG patterns and maintain low loss levels.

Image 6. Visualizing results from test data

Conclusion

In conclusion,  the results of this project underscored the transformative potential of AI in healthcare. By harnessing state-of-the-art deep learning techniques, this system strives to revolutionize patient monitoring by enabling real-time analysis of ECG data for anomaly detection. The ultimate goal is to shift from a reactive healthcare approach to a proactive one, where the system promptly alerts both patients and healthcare providers at the earliest indications of irregular heart activity. This initiative holds great promise for enhancing early intervention and improving patient outcomes in the realm of cardiac health.

For those interested in delving deeper into the code and gaining a comprehensive understanding of the project, the notebook is available on my GitHub repository, where detailed explanations accompany the code implementation. Feel free to explore the repository.

References

  1. TensorFlow Datasets: A comprehensive collection of datasets preprocessed and ready for use with TensorFlow, facilitating easier access to ECG datasets.
  2. Anomaly Detection Using PyTorch Autoencoder and MNIST: A detailed case study and tutorial that demonstrates the use of a PyTorch-based autoencoder for anomaly detection within the MNIST dataset, providing insights into the practical application of autoencoders in identifying outliers.
  3. Deep Learning | Introduction to Long Short-Term Memory: An educational resource offering an introduction to Long Short-Term Memory (LSTM) networks, a type of recurrent neural network that excels in learning from sequences, crucial for tasks involving time series data like ECG analysis.
  4. Fraud Detection (Semi-Supervised): This reference explores techniques in semi-supervised learning, particularly applied to fraud detection, where labelled data can be scarce, drawing parallels to the challenges faced in anomaly detection within medical data.