Instructions to use sid512206/CardioNet-XL with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use sid512206/CardioNet-XL with Keras:
# Available backend options are: "jax", "torch", "tensorflow". import os os.environ["KERAS_BACKEND"] = "jax" import keras model = keras.saving.load_model("hf://sid512206/CardioNet-XL") - Notebooks
- Google Colab
- Kaggle
Upload 4 files
Browse files- .gitattributes +1 -0
- Readme.md +52 -0
- ecg_cnn_model.keras +3 -0
- inference.py +68 -0
- requirements.txt +6 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
ecg_cnn_model.keras filter=lfs diff=lfs merge=lfs -text
|
Readme.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# CNN-Based Cardiac Abnormality Detection Using PTB-XL
|
| 2 |
+
|
| 3 |
+
This repository contains a deep learning model for automatic detection of
|
| 4 |
+
cardiac abnormalities from 12-lead ECG signals using the PTB-XL dataset.
|
| 5 |
+
|
| 6 |
+
The model is trained using a 1D Convolutional Neural Network (CNN) and
|
| 7 |
+
supports multi-label classification.
|
| 8 |
+
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
## 🔍 Supported Diagnostic Classes
|
| 12 |
+
|
| 13 |
+
- **NORM** – Normal ECG
|
| 14 |
+
- **MI** – Myocardial Infarction
|
| 15 |
+
- **STTC** – ST/T Wave Changes
|
| 16 |
+
- **CD** – Conduction Disturbance
|
| 17 |
+
- **HYP** – Hypertrophy
|
| 18 |
+
|
| 19 |
+
---
|
| 20 |
+
|
| 21 |
+
## 📥 Input Format
|
| 22 |
+
|
| 23 |
+
- Shape: **(1000, 12)**
|
| 24 |
+
- Sampling rate: **100 Hz**
|
| 25 |
+
- Duration: **10 seconds**
|
| 26 |
+
- Preprocessing:
|
| 27 |
+
- Bandpass filtering (0.5–40 Hz)
|
| 28 |
+
- Z-score normalization (per lead)
|
| 29 |
+
|
| 30 |
+
---
|
| 31 |
+
|
| 32 |
+
## 🧠 Model Architecture
|
| 33 |
+
|
| 34 |
+
- 1D CNN with multiple convolutional blocks
|
| 35 |
+
- Batch normalization & dropout
|
| 36 |
+
- Sigmoid output layer
|
| 37 |
+
- Multi-label classification
|
| 38 |
+
- Trained using Binary Cross-Entropy / Focal Loss
|
| 39 |
+
|
| 40 |
+
---
|
| 41 |
+
|
| 42 |
+
## 🚀 Inference Example
|
| 43 |
+
|
| 44 |
+
```python
|
| 45 |
+
import numpy as np
|
| 46 |
+
from inference import predict_ecg
|
| 47 |
+
|
| 48 |
+
ecg_signal = np.random.randn(1000, 12) # replace with real ECG
|
| 49 |
+
labels, probs = predict_ecg(ecg_signal)
|
| 50 |
+
|
| 51 |
+
print("Predicted labels:", labels)
|
| 52 |
+
print("Probabilities:", probs)
|
ecg_cnn_model.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:816592cf1556aea1529da3d11c211089fb2832a5bef8d5fce4bbefbca5882b16
|
| 3 |
+
size 49695056
|
inference.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
|
| 4 |
+
# Load trained model
|
| 5 |
+
MODEL_PATH = "ptbxl_ecg_cnn_model.keras"
|
| 6 |
+
model = tf.keras.models.load_model(MODEL_PATH, compile=False)
|
| 7 |
+
|
| 8 |
+
# Diagnostic labels
|
| 9 |
+
TARGET_NAMES = ["NORM", "MI", "STTC", "CD", "HYP"]
|
| 10 |
+
|
| 11 |
+
# Default thresholds (can be replaced with tuned ones)
|
| 12 |
+
DEFAULT_THRESHOLDS = {
|
| 13 |
+
"NORM": 0.5,
|
| 14 |
+
"MI": 0.5,
|
| 15 |
+
"STTC": 0.5,
|
| 16 |
+
"CD": 0.5,
|
| 17 |
+
"HYP": 0.5
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def predict_ecg(ecg_signal, thresholds=DEFAULT_THRESHOLDS):
|
| 22 |
+
"""
|
| 23 |
+
Predict cardiac abnormalities from a 12-lead ECG.
|
| 24 |
+
|
| 25 |
+
Parameters
|
| 26 |
+
----------
|
| 27 |
+
ecg_signal : np.ndarray
|
| 28 |
+
Shape (1000, 12), preprocessed ECG signal
|
| 29 |
+
thresholds : dict
|
| 30 |
+
Thresholds for each class
|
| 31 |
+
|
| 32 |
+
Returns
|
| 33 |
+
-------
|
| 34 |
+
predicted_labels : list
|
| 35 |
+
List of predicted diagnostic labels
|
| 36 |
+
probabilities : dict
|
| 37 |
+
Probability per diagnostic class
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
+
if ecg_signal.shape != (1000, 12):
|
| 41 |
+
raise ValueError("ECG signal must have shape (1000, 12)")
|
| 42 |
+
|
| 43 |
+
# Add batch dimension
|
| 44 |
+
ecg_signal = np.expand_dims(ecg_signal, axis=0)
|
| 45 |
+
|
| 46 |
+
# Model prediction
|
| 47 |
+
probs = model.predict(ecg_signal, verbose=0)[0]
|
| 48 |
+
|
| 49 |
+
predicted_labels = []
|
| 50 |
+
probabilities = {}
|
| 51 |
+
|
| 52 |
+
for i, label in enumerate(TARGET_NAMES):
|
| 53 |
+
probabilities[label] = float(probs[i])
|
| 54 |
+
if probs[i] >= thresholds[label]:
|
| 55 |
+
predicted_labels.append(label)
|
| 56 |
+
|
| 57 |
+
return predicted_labels, probabilities
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
# Example usage (for testing only)
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
dummy_ecg = np.random.randn(1000, 12)
|
| 63 |
+
labels, probs = predict_ecg(dummy_ecg)
|
| 64 |
+
|
| 65 |
+
print("Predicted labels:", labels)
|
| 66 |
+
print("Probabilities:")
|
| 67 |
+
for k, v in probs.items():
|
| 68 |
+
print(f"{k}: {v:.3f}")
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tensorflow>=2.13
|
| 2 |
+
numpy
|
| 3 |
+
scipy
|
| 4 |
+
wfdb
|
| 5 |
+
scikit-learn
|
| 6 |
+
matplotlib
|