How Machine learning helped me analyse ECG reports?
Last year in January my father survived a heart attack, we did an ECG, Echocardiogram, and blood tests and waited for his reports to come. The wait time felt like an eternity with the fear looming of witnessing another heart attack.
Eventually, we got the results and his treatment started!
But the anxiety we felt during that time was exhausting. I thought to myself, why do we need to wait so long to get the reports when technology is advancing exponentially?
So I took matters into my own hands and looked into how I could use my knowledge of machine learning to help medical professionals generate these reports faster.
Before building any machine learning solutions we need — you guessed it right! DATA.
Luckily, I found a research paper published by Khan, Ali Haider; Hussain, Muzammil (2020), “ECG Images dataset of Cardiac and COVID-19 Patients”, Mendeley Data, V1, doi: 10.17632/gwbz3fsgp8.1.
This data included images of ECGs of 1937 distinct patients suffering from either Myocardial Infarction, a history of Myocardial Infarction, Abnormal heartbeat or SARS-COV2 infection. The dataset also included ECG images of normal cardiac functioning. (If you want to download the data click here.)
Now we have the dataset, the next step is preprocessing the images. Preprocessing steps are crucial for preparing the ECG images for model training. The steps were: Resizing Images: Standardizing image dimensions to ensure uniformity across the dataset. Resized the images into 128 by 128-pixel size. Then flatten it for a Convolutional Neural Network and apply labels to each of them.
As the normal ECG images were the majority I tried two over-sampling methods one was using RandomOverSampler and another one was SMOTE(Synthetic Minority Oversampling Technique).
Now as the classes were uniform I split the data into testing and training datasets. Then applied MinMax scaler to avoid data leakage now the image pixels are scaled into the [0,1] range.
I wanted to make an ML model without shutting my laptop down so I tried various ML algorithms which are said to work perfectly well for image classification tasks like Random Forest, Decision Trees, Convolutional Neural Network and also SVM.
The result was astonishing!
I know what you are thinking — accuracy is not the perfect metric for classification tasks so I also looked into RECALL which ensures the model identifies as many true positives as possible. But sadly recall was not up to the mark so I moved to CNN.
The accuracy was 87% and recall was okay too.
Here class 0 is Abnormal Heartbeat, class 1 is COVID-19 patients, class 2 is ECG images of Myocardial Infarction, class 3 is patients having history of MI and class 4 is normal heartbeats.
Finally, I saved the model using model.save in keras.model and uploaded the ECG report I had in my hand by clicking a picture of it, model successfully classified that image to class 3 which was the class of patients having a history of myocardial infarction.
Conclusion
We are using AI in our day-to-day lives, but traditional Machine learning is also here to stay. By building such models on larger scales we can aid medical practitioners in their practice and make diagnoses early without making patients wait longer times.
If you want to read in-depth technical tidbits follow this link and build such model on your own.