Arrhythmia Categorization Using Convolutional Neural Networks

By Matt Brown, Katherine Lordi, Vince Minnocchi, Sam Zimmerman

Matt Brown
Bucknell AI & CogSci
9 min readDec 23, 2019

--

Our Motivation ❤

Our team sought to apply theories and methods related to Artificial Intelligence (AI) and Cognitive Science to applications in health to help improve quality of life. We were inspired by the ECG Heartbeat Categorization on Kaggle, which consists of pre-processed electrocardiogram (ECG) signals for normal and irregular heartbeats. A heart arrhythmia is any type of irregular heartbeat.

https://www.quora.com/High-heart-rate-variability-is-seen-as-a-healthy-thing-while-arrhythmia-is-potentially-deadly-What-is-the-difference-between-HRV-and-arrhythmia

Arrhythmias are a leading cause of morbidity and mortality in heart diseases and is a major cause of more than a quarter million deaths annually in the United States. Therefore, there have been technological advances in the detection and monitoring of arrhythmias. For example, Apple has an ECG app available on Apple Watches of Series 4 or later. However, it can only check for one form of arrhythmia called atrial fibrillation and cannot detect other conditions such as a heart attack. Fitbit, another leader in tech for healthcare wearables, is also starting starting to spot heart irregularities.

https://www.apple.com/newsroom/2019/03/ecg-app-and-irregular-rhythm-notification-on-apple-watch-available-today-across-europe-and-hong-kong/

Role in Wearables

In recent years, with the introduction of the Apple Watch, Apple has been making a large push into the health sector, adding a variety of health tracking features to all of their mobile devices. In 2018, with the introduction of the Apple Watch Series 4, a basic ecg sensor was added to the device that provided information on the regularity of the wearer’s heart rhythm. Adding this technology to wearables can provide more information to doctors outside of their office while the user is going about their normal life. In its current form, the sensors in the Apple Watch provide similar results to a single-lead ecg while, in a hospital setting, a 12-lead ecg is standard for diagnosing heart attacks and more advanced cardiac events.

Project Description

Our team’s goal is to train an AI model to detect and accurately classify different types of arrhythmias using input ECG heartbeat signals from the ECG Heartbeat Categorization dataset.

Ideally, this project can serve as a proof of concept for inclusion in a live system to give physicians more information when diagnosing different arrhythmias in patients. In an emergency, this technology could expedite the diagnosis, leading to lifesaving treatment sooner and improving the prognosis for the patient. Additionally, this technology could be included in wearables that detect the user’s heart rate or use ECG technology in order to detect signs of arrhythmia. Especially in situations where the user has a known heart condition, the device could alert the user to any irregularity before the situation becomes life-threatening.

Implementation

We focused on two different implementations both based on convolutional neural networks (CNNs). CNNs are one of the best options for extracting features from images. The convolutional layers create a type of encoding of the features in the image and shrink the image into smaller and smaller dimensions until the flattening layer can turn the 2-dimensional matrix into a 1D vector which can be further downsized to the final output. The convolutional layers apply a filter over the image and assign values based on what the filter sees (see image below).

https://hackernoon.com/visualizing-parts-of-convolutional-neural-networks-using-keras-and-cats-5cc01b214e59

As the neural network is trained, the values of the filter are updated until the network is achieving acceptable results. The image below demonstrates how the features are encoded from one layer to the next (taken from this website)

Our team’s 1D CNN implementation was inspired by Youness Mansar’s implementation of the paper “ECG Heartbeat Classification: A Deep Transferable Representation” by Mohammed Kachuee, Shayan Fazeli, and Majid Sarrafzadeh of the University of California, Los Angeles using Python Keras TensorFlow. We then modified the architecture according to the table below to discover the resulting impact on accuracy. When trained on 100% of the training data, this model was able to achieve about 98.4% accuracy.

Architecture changes and resulting impact
Architecture of Mansar’s implementation of the 1D CNN

The first step of the 2D CNN implementation was to preprocess the data into graphs so we could use them as input in the CNN. The way the original data was formatted, each row corresponds to one heartbeat, and the last column is an identifier, which places it in one of the five categories of arrhythmias. We wrote a script that reads in the dataset line by line and plots a grayscale graph, then saves it to the category folder within either the test or training plots folder. We attempted to standardize the graphs even more than they already are by detecting the index of the r-peak, and only graphing up to that index. Unfortunately, we were unable to produce repeatable and standard enough results with this method, so we abandoned it. Since the dataset was already very clean, we don’t believe not having this ability affected our results.

The next step was creating the correct CNN architecture and structure that would allow us to get accurate results. For this we referenced the work from a project from the School of Computing, in Korea who tackled this problem previously. (Jun, 2018)

Architecture of 2D CNN

We took the structure they used, and applied some modifications so it would fit our implementation better. There were multiple convolutional layers, which are the layers that use a filter to attempt to extract the important image features that will allow for classification. Pooling was used to decrease the size of the matrix, while still retaining the important information held within it. Batch normalization is also used to normalize the inputs into hidden layers, which should enable for faster learning as well as enable the layers to learn more independently of one another. Flattening forces the matrix from two dimensions down to one. This is used as the input into more traditional neural network layers were the previous layers were rather specific to the 2D CNN. After the network was set up, we were able to train then subsequently test our model.

Unfortunately, the 2D implementation faced issues with always classifying each sample as the same thing even though the validation done during training indicated that the network was performing well (see image below).

2D CNN Training and Validation

Possible Ethical Issues

When attempting to implement an artificially intelligent system into the real world, we must take the time to look at any possible ethical implications that come with the implementation of the system. For our project, we defined two main ethical issues, the accuracy and cost of the system. The possible miscategorization of a healthy heart beat to an arrhythmia could cause a user an untold amount of stress, along with possible expensive medical treatment. Or the reverse situation, where someone with an arrhythmia isn’t alerted to it and is left to suffer the possible consequences. It wouldn’t be unreasonable for someone to place blame on the system since it is the systems fault that all of these negative effects were placed upon the wearer. So as a developer you need to be able to decide what you would rather have: false positives, where the system incorrectly detects an arrhythmia when one wasn’t present, or false negatives, where the system fails to detect a present arrhythmia. In both cases the system is causing some harm to its users, but as developers you need to decide which case is worse and implement the other one. For our project we thought that having a false positive is less of a problem than having a false negative. Since, with a false negative someone could potentially die due to our system while a false negative could just result with someone having an unnecessarily stressful time.

https://cdn57.androidauthority.net/wp-content/uploads/2018/02/yapay-zeka-silikon-vadisi.jpg

As for the ethical dilemma due to cost, we were worried about the possible future of this wearable tech. If this product were to be made extremely expensive so that many people would be unable to buy it, this product would only serve to increase the levels of inequality already present in the healthcare system when comparing people of differing economic situations. This system only being available to the mega-rich as a luxury health item would not be the systems’ fault, however, when you are developing software, you must always look at how it can be used. Things with innocent intended uses can be used for nefarious purposes if given to the wrong people.

Utopian/Dystopian Hypotheticals

In a utopian society this sort of technology would be given as a free wearable for those who are at risk of heart related issues and the alerts would be sent to the nearest hospital so that a doctor could come over to check on any patient with an alert and administer the right treatment. The data would be kept private and secure and the system would be highly vetted so that there are no false positives or false negatives. This would ultimately improve society and result in a healthier population. The data from these devices could also be responsibly used in medical research to provide more information on the general population and allow for discovery of more rare heart conditions and lead to potential treatments. Presumably, a healthier overall society would lead to longer life spans and greater productivity, allowing for more rapid advancement of society.

In a dystopian society, this technology would be used to sort those who have healthy hearts and those who have unhealthy hearts. Those with healthy hearts will live and those with unhealthy hearts will be killed. All of the users data is sold to any company who wants it and the system is highly flawed and will often misread. The technology in wearables could be used to track people for any variety of reasons. This could lead to systematic oppression by the state or individuals could hack this location data and use it to commit any number of crimes.

This could lead to a Black Mirror-esque world where people are treated as lower class the second they exhibit any kind of heart condition. The “upper class” that has never exhibited a heart condition might choose to only marry each other in an attempt to selectively reproduce only healthy hearts.

Conclusion

This project demonstrates that cardiac arrhythmias can be accurately identified by a CNN. The 1D CNN performed reasonably well although the medical community would need to weigh in on how accurate the system needs to be in order to implement it properly and how to manage false positives or negatives.

The main ethical concerns of this technology are primarily related to privacy, the potential for further harm to already disadvantaged groups, and the potential for emotional or physical harm to all users. Some of these risks might be mitigated with further work, and all of them would have to be studied thoroughly to determine if they make the technology too risky in its current state. Similarly, caution would be necessary when implementing this technology to avoid the dystopian scenario highlighted above and trend closer to a utopian world.

Our full research paper and code can be found on gitlab.

--

--

Matt Brown
Bucknell AI & CogSci

Matt is a Computer Science researcher at Bucknell University interested in ethical technology design.