Heart arrhythmia detection using Deep Learning | by Cristiano Aguiar | Towards Data Science

A common problem that Deep Learning is helping to solve lately involves time series classification. A classic approach to this kind of problem is generating features from the signals we have and training a machine learning model.
The process of handcrafting features might take a great chunk of your project schedule. For that matter, employing a combination of a Convolutional Neural Network with a Long Short-Term Memory Recurrent Neural Network. This architecture has proven to be effective in order to reduce the amount of time spent on feature engineering.
In this article, we're going to train a couple of models to detect irregular heart rhythms. The idea is to show step by step how to build a sequence model for time series classification. If you want a deep understanding of this problem, I highly recommend this article from the Stanford ML Group.
For this experiment, we'll be using the MIT-BIH Arrhythmia Database that contains 48 half-hour excerpts of two-channel ambulatory ECG recordings, obtained from 47 subjects. The dataset is annotated by a cardiologist, all the labels and a full explanation of how the data were collected can be found here.
For the purpose of this experiment, let's consider only two classes: normal beats and abnormal beats. Invalid beats were discarded from the labels. These are considered labels, the ones not on these lists are marked as normal.
Each record is composed of a signal and its annotations. Both files are read with the library Python WFDB. The record can be read using:
record = wfdb.rdrecord(filename)annotation = wfdb.rdann(filename, "atr")
The annotation contains each beat annotation. Each sample in our model will be a beat label as target and a sequence surrounding this beat as input (3 seconds each side).
Now let's define two functions that will help us to process the records. One to simply classify the record, given the list above, into a normal vs. abnormal beat. Another function will build a sample for our CNN/LSTM 1D model.
To better understand the second function, we need to understand how the input for the CNN or LSTM model is formatted. You'll have to input a three dimensional array with the shape (batch_size, sequence_size, number_of_features). In this case, each sample would be (1, sequence_size, 1) since we have only one feature in our model. Later, you're going to see that for CNN with LSTM we also need to define another dimension (subsequences).
Next, the following code will build a list of sequences (inputs) list of labels (target) and a map for each patient. The idea is to stratify for training and validation based on the rate of abnormal beats.
Let's try different models to train our classifiers. The main purpose here is to show how to setup the keras layers, the architectures aren't very well designed. The first one is a CNN only model.
This model looks like something below:
To train the model let's call the fit method from keras:
With this architecture, we can accomplish an accuracy of 0.82 in the validation set. It might not mean an amazing model, especially because of the loss over epochs that doesn't improve.
Another common architecture used for sequential models is CNN with LSTM. The idea is to have a CNN layer working as a feature extraction layer and an LSTM to interpret the sequence of subsequences.
This kind of architecture looks like this:
Building a model using the Sequential API doesn't change much from what was done above.
With this architecture, the accuracy on the validation set was 0.8.
Next step, you can improve those models by reducing overfit through dropout layers or kernel regularizations.
The code for this experiment is in the repo for this post.