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.
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.
The model is a hybrid CNN-LSTM classifier built in PyTorch.
-
CNN Feature Extractor (3 Blocks):
- Three 1D-Convolutional blocks (filters: 32 → 64 → 128) with
BatchNorm,ReLU,MaxPool, andDropout. - This part acts as a powerful feature extractor, learning spatial patterns in the raw ECG signal.
- Three 1D-Convolutional blocks (filters: 32 → 64 → 128) with
-
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.
- A 2-layer Bidirectional LSTM (128 hidden units) with
-
Classifier Head (Dense):
- A final fully-connected block (256 → 64 → 5) with
Dropoutthat outputs the final 5 class probabilities. - Total Trainable Parameters: 728,261
- A final fully-connected block (256 → 64 → 5) with
The model was trained for 50 epochs on a T4 GPU, achieving the target accuracy.
[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] |
The model trained for 50 epochs, with validation accuracy consistently improving. The learning rate was reduced several times by the ReduceLROnPlateau scheduler.
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.
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.
-
Clone the repository:
git clone [https://github.com/](https://github.com/saoud30/ECG_Arrhythmia_Classifier.git) cd ECG_Arrhythmia_Classifier -
Install dependencies:
pip install -r requirements.txt
-
Set up Kaggle API:
- Download your
kaggle.jsonAPI token from your Kaggle account. - Place it in the root of the project folder.
- Download your
-
Run the notebook:
- Open
ECG_Arrhythmia_Detection.ipynbin VS Code or Jupyter. - Run all cells sequentially. The script will handle data download, preprocessing, training, and evaluation.
- Open
.
├── 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


