- AI/ML
- Research
EcgNet: deep learning that reads heart conditions from ECGs
A team research project classifying scanned 12-lead ECG images into four heart conditions. I built the image preprocessing pipeline and helped train and compare the models. Later, I audited my own code, found evaluation mistakes and documented how to fix them.

- Role
- Summer Research Intern, Morgan State University
- Timeframe
- May – July 2025
- Team
- Multidisciplinary research team
- Tools
- Python · TensorFlow · OpenCV · PyWavelets · CNNs · Transformers · Grad-CAM · LIME
Watch the presentation: How AI Reads the Human Heart
2 min 32 s
Our team's walkthrough, recorded in July 2025: the dataset, preprocessing, the models we compared, the results, and the Grad-CAM and LIME explanations. The numbers in it are the team's figures at the time. My later audit, described below, found they were optimistic.
Read the transcript
Ladies and gentlemen, a video demonstration from group project 5. Our dataset consisted of ECG images categorized into four heart conditions: myocardial infarction (MI), history of MI, normal and abnormal.
Before model training, we applied a series of preprocessing steps: cropping to remove unnecessary margins, denoising with Gaussian blur to reduce high-frequency noise, and a bandpass filter to enhance relevant signal patterns. This process produced a clean set ready for analysis.
We trained three unimodal models: 1D-CNN, 1D-CNN-Transformer and 2D-CNN. The 1D-CNN focused on time-domain features and achieved an accuracy of 91%. The Transformer model learned frequency-domain patterns and reached 87% accuracy. The 2D-CNN, which processed images in the time-frequency domain, performed the best with 96% accuracy, as shown by the orange line on the graph, which is the validation accuracy.
To explore the potential of combining model strengths, we applied weighted late fusion. The fusions of 1D-CNN plus 2D-CNN and Transformer plus 2D-CNN also reached 96% accuracy, matching the standalone 2D-CNN. Based on this, we decided to continue with the 2D-CNN for deeper analysis.
We then implemented explainable AI (XAI) to make the model’s decisions more transparent. We first added a prediction cell that outputs the predicted heart condition and the model’s confidence level. This step made the predictions clear before diving into the reasoning. Then we used Grad-CAM and LIME to visually highlight the regions of the ECG image that influenced the model’s decision, answering key questions like, “What part of the image guided the model’s classification?”
Thank you for watching, and over to David for an update on the future work.
The problem
Reading an ECG takes expertise, and some heart conditions look alike on paper. This study covered four classes: myocardial infarction (MI), a history of MI, abnormal heartbeat, and normal.
EcgNet set out to classify scanned 12-lead ECG images automatically — and to show which parts of each ECG drove the model’s decision.
My contribution
- Built the image preprocessing pipeline in Python: cropping, Gaussian denoising, wavelet denoising and bandpass filtering.
- Helped train and compare 1D-CNN, 1D-CNN + Transformer and 2D-CNN models and their weighted late fusion.
- Produced technical reports and presentations for the research team.
- After the internship, audited the code and documented six evaluation issues, with fixes.
Implementation
We used a public dataset of scanned 12-lead ECGs from a cardiology institute in Pakistan. Our working copy had 800 images: 239 MI, 172 history of MI, 105 abnormal heartbeat and 284 normal. It was split 80/20, giving a 160-image test set.
Each ECG image was cropped, denoised and filtered (Fig. 1). For the 1-D models, the image was reduced to a 1-D profile and cleaned with wavelet denoising and a bandpass filter (Fig. 2). This preprocessing work, in Python, was my main contribution.
We trained and compared a 1D-CNN and a 1D-CNN with a Transformer on the 1-D profile, and a 2D-CNN on the ECG image itself, then combined their outputs with weighted late fusion. In the team's final run, the 1D-CNN reached 91%, the Transformer 87%, the 2D-CNN 96%, and 1D + 2D fusion 96% (macro F1 0.96). My own rerun on cropped images gave 95%, 91%, 93% and 93%. On a 160-image test set, a handful of images can move the score several points.
We also generated Grad-CAM and LIME heatmaps to show which image regions drove each prediction.
Inside the project
(open full size in a new tab)
(open full size in a new tab)Outcomes
- test accuracy, 1D + 2D fusion, across two runs (160 images)
- 93–96%
- macro F1 across the four classes
- 0.91–0.96
- evaluation issues found in my own audit
- 6
The models separated the four classes well on our test set. Most mistakes were between MI, history of MI and abnormal heartbeat, the classes that look most alike.
Findings were shared with the multidisciplinary research team through technical reports and presentations.
These scores are likely optimistic. The same 160 images were used both to monitor training and to report results, and the model was never tested on ECGs from another hospital.
What I learned
- I later re-read my notebook and the team's final notebook line by line and found six problems. They taught me more than the original results did.
- The test set doubled as the validation set, and the fusion weights were set from test accuracy. Next time: separate train, validation and test sets, or cross-validation with confidence intervals.
- The class names in our reports were in the wrong order, so the confusion matrix labelled one class as another. The "48 of 48 normal ECGs correct" we reported were in fact MI ECGs. Next time: take names from the data, never type them by hand.
- The 1-D "signal" averaged the brightness of each pixel row, which is not the ECG waveform. That it still scored 91–95% suggests the models may have learned differences in how each hospital unit recorded its ECGs, not heart patterns.
- The explainability heatmaps came from a rebuilt model whose trained weights were never loaded, so they don't reflect the trained model. Next time: confirm the rebuilt model makes the same predictions before explaining it.
- Each whole 12-lead ECG was shrunk to 224 px, losing fine detail such as ST-segment shape. In my rerun, the 2D test images used for fusion were also resized differently from the training images.