Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

ECG Arrhythmia Detection with Hybrid CNN-LSTM

A deep learning project to classify 5 types of heartbeats from the MIT-BIH Arrhythmia Database. This model uses a hybrid architecture combining 1D Convolutional Neural Networks (CNN) for feature extraction and a Bidirectional LSTM (BiLSTM) for temporal sequence modeling.

Target: 97%+ accuracy on 5-class arrhythmia classification.


📋 Table of Contents


🤖 About the Project

This project aims to accurately classify ECG (electrocardiogram) signals into five distinct arrhythmia categories using the MIT-BIH Arrhythmia Database. The dataset is highly imbalanced, with 'Normal' beats (N) comprising over 82% of the data.

Classes:

  • N - Normal (0)
  • S - Supraventricular (1)
  • V - Ventricular (2)
  • F - Fusion (3)
  • Q - Unclassifiable (4)

To handle the severe class imbalance, this model uses a Weighted Cross-Entropy Loss function, which penalizes misclassifications of rare classes more heavily.


🏗️ Model Architecture

The model is a hybrid CNN-LSTM classifier built in PyTorch.

  1. CNN Feature Extractor (3 Blocks):

    • Three 1D-Convolutional blocks (filters: 32 → 64 → 128) with BatchNorm, ReLU, MaxPool, and Dropout.
    • This part acts as a powerful feature extractor, learning spatial patterns in the raw ECG signal.
  2. Temporal Encoder (BiLSTM):

    • A 2-layer Bidirectional LSTM (128 hidden units) with Dropout.
    • This part reads the features extracted by the CNN and models the temporal dependencies and time-series nature of the signal.
  3. Classifier Head (Dense):

    • A final fully-connected block (256 → 64 → 5) with Dropout that outputs the final 5 class probabilities.
    • Total Trainable Parameters: 728,261

📊 Final Results

The model was trained for 50 epochs on a T4 GPU, achieving the target accuracy.

Performance Metrics

[cite_start]The final model achieved an overall Test Accuracy of 97.29% [cite: 2] [cite_start]and a Macro-Average AUC of 0.9928[cite: 2].

Metric Score
Test Accuracy [cite_start]97.29% [cite: 2]
Validation Accuracy [cite_start]97.40% [cite: 2]
Macro-Average AUC [cite_start]0.9928 [cite: 2]

Per-Class AUC Scores:

Class AUC Score
Normal (N) [cite_start]0.9955 [cite: 2]
Supraventricular (S) [cite_start]0.9758 [cite: 2]
Ventricular (V) [cite_start]0.9984 [cite: 2]
Fusion (F) [cite_start]0.9945 [cite: 2]
Unclassifiable (Q) [cite_start]0.9999 [cite: 2]

Training History

The model trained for 50 epochs, with validation accuracy consistently improving. The learning rate was reduced several times by the ReduceLROnPlateau scheduler.

Training Curves

Confusion Matrix

The normalized confusion matrix shows excellent performance, with most classes having >90% recall. The rarest classes (S and F) were the most challenging, sometimes being confused with Normal (N) beats.

Normalized Confusion Matrix

ROC Curves

The Receiver Operating Characteristic (ROC) curves are near-perfect, with all classes achieving an AUC score well above 0.97. This indicates the model has outstanding discriminative power between classes.

ROC Curves


🚀 Usage

  1. Clone the repository:

    git clone [https://github.com/](https://github.com/saoud30/ECG_Arrhythmia_Classifier.git)
    cd ECG_Arrhythmia_Classifier
  2. Install dependencies:

    pip install -r requirements.txt
  3. Set up Kaggle API:

    • Download your kaggle.json API token from your Kaggle account.
    • Place it in the root of the project folder.
  4. Run the notebook:

    • Open ECG_Arrhythmia_Detection.ipynb in VS Code or Jupyter.
    • Run all cells sequentially. The script will handle data download, preprocessing, training, and evaluation.

📁 File Structure

.
├── ECG_Arrhythmia_Classifier.ipynb   # The main project notebook
├── README.md                        # Project documentation (this file)
├── requirements.txt                 # Python dependencies
│
├── best_cnn_lstm_model.pth          # Saved PyTorch model weights
├── model_summary.txt                # Text file with architecture and results
├── training_history.csv             # Epoch-by-epoch training logs
├── test_predictions.csv             # Model's predictions on the test set
│
-Visualization
├── class_distribution.png           # EDA: Class imbalance plot
├── sample_ecg_signals.png           # EDA: Sample waveforms plot
├── training_curves.png              # Results: Loss/Accuracy/LR plots
├── confusion_matrix.png             # Results: Raw confusion matrix
├── confusion_matrix_normalized.png  # Results: Normalized confusion matrix
├── roc_curves.png                   # Results: ROC/AUC plot
└── sample_predictions.png           # Results: Sample prediction visualizations

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages