Interpatient ECG Heartbeat Classification with an Adversarial Convolutional Neural Network

Discovering shared, invariant feature representations across subjects in electrocardiogram (ECG) classification tasks is crucial for improving the generalization of models to unknown patients. Although deep neural networks have recently been emerging in extracting generalizable ECG features, they usually rely on labeled samples from a large number of subjects to guarantee generalization. Extracting invariant representations to intersubject variabilities from a small number of subjects is still a challenge today due to individual physical differences. To address this problem, we propose an adversarial deep neural network framework for interpatient heartbeat classification by integrating adversarial learning into a convolutional neural network to learn subject-invariant, class-discriminative features. The proposed method was evaluated on the MIT-BIH arrhythmia database which is a publicly available ECG dataset collected from 47 patients. Compared with the state-of-the-art methods, the proposed method achieves the highest performance for detecting supraventricular ectopic beats (SVEBs), which are very challenging to identify, and also gains comparable performance on the detection of ventricular ectopic beats (VEBs). The sensitivities of SVEBs and VEBs are 78.8% and 92.5%, respectively. The precisions of SVEBs and VEBs are 90.8% and 94.3%, respectively. With high performance in the detection of pathological classes (i.e., SVEBs and VEBs), this work provides a promising method for ECG classification tasks when the number of patients is limited.


Introduction
Classifying electrocardiogram (ECG) heartbeat is essential for cardiac diseases (e.g., cardiac arrhythmia) diagnosis. However, it is time consuming for cardiologists to inspect a long-term electrocardiogram (ECG) manually, making automatic ECG analysis useful. Currently, a large number of methods have been proposed for ECG classification. Two paradigms, known as intrapatient and interpatient paradigms, are usually adopted for evaluating ECG classification methods. In the intrapatient paradigm, the heartbeats from different patients are divided into the training and evaluation sets randomly. is evaluation paradigm is not highly reliable in the real world since the heartbeats from the same patients may be used for both the training and the testing, making the evaluation of the generalization of the classifier biased. In practice, an automatic ECG classification system should provide an accurate diagnosis for any unknown patient (patient not in the training set). e interpatient paradigm specifies that the heartbeats used for the training and the testing are from different individuals to obtain a more realistic evaluation. However, automatic interpatient ECG classification is a challenge today due to variations in ECG morphology and rhythm caused by individual physiological differences.
As illustrated in Figure 1, an ECG heartbeat mainly consists of a P wave, QRS complex wave, and T wave, which reflect electrical activities of depolarization and repolarization processes of the atria and ventricle. In general, a complete ECG classification system consists of three procedures: (1) ECG signal preprocessing, such as baseline wander removal and heartbeat segmentation; (2) feature extraction, mainly including morphological features [1][2][3][4], statistical features [5][6][7], P-QRS-T features [8][9][10], and wavelet features [11][12][13]; and (3) classification, such as support vector machine (SVM) [3,9,14,15] and artificial neural network (ANN) [8,16]. Chen et al. [9] combined projected ECG features and weighted RR interval features and then input these features into SVM for heartbeat classification. While their method yielded a high classification performance under the intrapatient evaluation paradigm, the sensitivity and precision metrics for detecting supraventricular ectopic beats were only 29.5% and 38.4% under the interpatient evaluation paradigm on the MIT-BIH arrhythmia database. Raj et al. [17] introduced a sparse representation technique to extract features representing ECG signals and used machine learning techniques (such as SVM and k-nearest neighbor) to classify these features, which obtain a good result in detecting supraventricular ectopic beats. Mondejar et al. [4] extracted morphological features and the features based on wavelets, high-order statistics, local binary patterns, and RR intervals. ey proposed to feed each type of feature into a single SVM to train and obtain specific SVM models. en, the predictions of these SVM models were combined to obtain the final prediction, which achieved an overall good performance for interpatient heartbeat classification. ese methods rely on expert knowledge and experience for feature engineering.
us, the classification performance could be very sensitive to the quality of extracted features.
Recently, many studies on ECG classification are increasingly focusing on deep learning due to its powerful ability for automatic feature learning and classification. When the training dataset is sufficient, deep neural networks (e.g., convolutional neural network (CNN)) are shown to be very predominant in classification tasks [18][19][20][21][22]. Hannun et al. presented a 34-layer deep CNN trained on 91232 ECG recordings collected from 53549 individuals, which achieved cardiologist-level accuracy in arrhythmia classification. However, complex models such as the CNN are prone to overfitting when the number of patients is limited (e.g., 47 different patients included in the MIT-BIH arrhythmia database), making it difficult for classifying the heartbeats of unknown patients. In fact, some deep learningbased methods [23][24][25] have achieved satisfactory results on small databases such as the MIT-BIH arrhythmia database for interpatient ECG classification. Li et al. [23] developed a multiscale convolutional neural network in which 3D features containing morphological characteristic, beat-to-beat correlation feature, and RR interval were taken as inputs. Niu et al. [24] proposed a deep-learning framework that introduces a symbolization approach to represent the rhythm and morphology of the heartbeat and feeds the symbolic representation into a multiperspective convolutional neural network. However, current methods lacked explicit mechanisms to explore ECG feature invariance across subjects. ey usually stand on the assumption that their proposed models can intrinsically learn generalizable features during training. is implicit learning is naturally restrained by the amount of individual ECG data. erefore, how to explicitly learn invariant representations against intersubject variations is a critical issue, especially when the number of patients is limited.
In this paper, we propose an adversarial ECG heartbeat classification framework based on a convolutional neural network, as illustrated in Figure 2. e framework integrates adversarial learning into a convolutional neural network, which extends deep-learning models for ECG identification tasks. e adversarial CNN is composed of an encoder, classifier, and adversary networks. e encoder network extracts features from ECG heartbeat signals and corresponding RR intervals. e classifier and adversary networks are responsible for maximizing the class labels prediction and minimizing the subject ID identification. By this adversary game, the encoder is trained to learn subject-invariant, class-discriminative features. e proposed method was evaluated on the MIT-BIH arrhythmia database which is a publicly available ECG dataset collected from 47 patients. Ablation studies show that our adversarial subject-invariant feature learning significantly enhances interpatient ECG heartbeat classification accuracy compared to conventional deep-learning methods.
e main contributions of this paper are concluded as follows: (1) Our goal is that the features learned by a deep-learning model can generalize to unknown patients well for ECG identification/classification tasks. To this end, a deeplearning-based ECG heartbeat classification framework is proposed for tackling the learning of generalizable features. Specifically, we introduce an adversary loss into the convolutional neural network, encouraging the model to learn subject-invariant, class-discriminative representations from an insufficient number of subjects through the adversary game. (2) e experiments on the publicly available and commonly used dataset, MIT-BIH database, demonstrate that the proposed method can achieve the state-ofthe-art performance on the detection of pathological classes when the number of subjects is limited. number of X i . e reasonable assumption here is ECG data X being jointly dependent on class labels y and subject IDs s. e task of ECG classification is to predict y given X. In the real world, this task requires the predictions invariant to s, namely, a generalizable model across subjects is necessary. In this study, we regard s as the nuisance variable and aim to develop a convolutional neural network model to learn generalizable features across subjects that are invariant to s.

Data Preprocessing and Feature
Extraction. All original ECG recordings are preprocessed to generate the input of the proposed adversarial convolutional neural network, as presented in Figure 2(a). First, we segment the original ECG recordings into heartbeats according to the locations of R peaks annotated by the MIT-BIH arrhythmia database. Specifically, the 50 points after the previous R peak and the 100 points after the current R peak are taken as a heartbeat.
is segmentation allows heartbeats to contain a more robust P-QRS-T complex waveform since the heart rate is constantly changing, and the fixed starting point relative to the current R peak may introduce disturbance information (heartbeats with a short RR interval) or lose information (heartbeats with a wide waveform). Our segmentation will result in heartbeats of different lengths; however, CNNs fail to accept the varied-length input. erefore, in the second step, we resample all heartbeats to the same length 128.
ird, the average of all heartbeat segments is subtracted to suppress the baseline wander.
In addition to the preprocessed heartbeat signal, the heartbeat rhythm (RR interval information) is extracted as another part of the input, as shown in Figure 2(b). e pre-RR interval (the interval between the current R peak and the previous one) is a typical RR interval feature, which generally can distinguish arrhythmias from normal heartbeats of a person [27]. However, the pre-RR interval distribution of arrhythmic heartbeats may overlap with that of normal heartbeats as the individual basic heart rate is different, especially for the patient population. To eliminate the overlap, we extract the pre-RR ratio (the ratio of the current  pre-RR interval to the average of all pre-RR intervals of the corresponding recording) to unify everyone's basic heart rate. Furthermore, the near-pre-RR ratio (the ratio of the current pre-RR interval to the average of the previous ten pre-RR intervals) is also extracted since the individual basic heart rate changes with mood and movement state [1]. To build the input of the adversarial convolutional neural network, we duplicate these two scalar features as vectors with a length of 128 and then concatenate with the preprocessed heartbeat signal.

Adversarial Model Learning.
e proposed adversarial ECG heartbeat classification model mainly consists of three parts: an encoder, classifier, and adversary subnetworks, as illustrated in Figure 2(c).
e encoder network f(X; θ e ) parameterized by θ e is used to learn representations h. In implementation, the convolution neural network is as the encoder, which is detailed in Section 2.4. e encoder outputs the representations h, and h are fed into the classifier q θ c (y|h) parameterized by θ c and the adversary network q θ a (s|h) parameterized by θ a separately. e classifier and adversary, consisting of a fully connected layer with softmax function, are used to classify the representations h into heartbeat classes y and subject IDs s, respectively. To eliminate interferences caused by s that are embedded in h, we present an adversarial game. Here, the adversary is trained to predict subject IDs s by maximizing the likelihood q θ a (s|h), while at the same time, the encoder is trained to conceal information regarding s within h by minimizing this likelihood and retain sufficient discriminative information for the classifier to estimate class labels y by maximizing q θ c (y|h). Overall, we train the encoder, classifier, and adversary networks jointly towards the objective: where L is the cross-entropy loss function, defined by where λ denotes the adversarial weight trading off between stronger invariance with task-discriminative performance. A higher λ( > , 0) enhances invariance to subjects, whereas λ < 0 forces the encoder to learn features that are discriminative for class labels, as well as subject IDs, which is not expected in our ECG classification task.

Convolutional Network
Architecture. e ECG feature encoder is composed of 7 convolution layers and three spatiotemporal attention modules in total. e specific configuration of the encoder network is shown in Table 1. Following the first convolution layer, three residual convolution blocks with average pooling shortcuts are built to facilitate the optimization of the network and gain classification accuracy. e second (the last) convolution layer of each residual block uses the dilation rate of 3 to enlarge the receptive field without increasing the parameter amount. After all convolution layers, batch normalization (BN) [28] is used to accelerate model convergence by renormalizing the distribution of training minibatch. e Rectified Linear Unit (ReLU) function [29] is applied to activate the output of each BN layer, which could prevent the vanishing gradient problem well. Furthermore, we introduce a spatiotemporal attention mechanism [30], including spatial and temporal attention modules, which is embedded after each residual convolution block. is mechanism could focus on more informative features by assigning different weights to both channels and temporal segments of the feature map.
Learned representations h by the encoder network are input to the classifier and adversary for task discrimination (heartbeat class) and subject ID discrimination. Both the classifier and adversary consist of a fully connected layer with C and S softmax units, respectively, to output normalized log-probabilities that will be used to calculate the loss L in equation (2).

Dataset.
e MIT-BIH arrhythmia database [31] is used for evaluating the performance of the proposed method. is database consists of 48 two-lead ambulatory ECG recordings collected from 47 individuals, where recordings 201 and 202 were obtained from the same subjects. Each recording lasts about 30 minutes and is sampled at 360 Hz. According to ANSI/AAMI EC57:1998 [32], all heartbeats can be grouped into five superclasses: heartbeats originating in the sinus node (N), supraventricular ectopic beats (SVEBs or S), ventricular ectopic beats (VEBs or V), fusion beats (F), and unknown beat type (Q).
Following the AAMI-recommended practice, four paced recordings are not used. To obtain a more realistic evaluation, De Chazal et al. [33] recommended dividing the remaining 44 recordings into DS1 and DS2 sets for the training and test, respectively. is division splits the recordings by considering the identification of patients and the balance of classes, which guarantees that the heartbeats in the training and testing sets are from different patients. e detailed heartbeat distribution used in this paper is shown in Table 2.

Training
Setting. 20% of the training data is randomly chosen as the validation data, and the remaining data are used as the training samples. We set the adversarial weight λ to 0.005 by finetuning this parameter. e proposed adversarial deep-learning framework is trained by using an adaptive moment estimation (Adam) optimizer [34] with an initial learning rate of 0.001. During training, the model parameters are updated iteratively based on batches of 128 training samples. When the loss L c of the validation data remains undeclined for 10 epochs, the learning rate decreases to 0.0001, while for 20 epochs, the training will terminate. e best-performing model on validation data for heartbeat classification is saved.

Evaluation Metrics.
Four typical metrics, including accuracy (Acc), sensitivity (Sen), precision (Pre), and F 1 score, are used to measure the classification performance of the proposed method. Here, accuracy measures the overall classification performance of the proposed method, whereas sensitivity and precision metrics are calculated for each specific class. F 1 score is the harmonic mean of precision and recall. ese metrics are defined as Acc � TP + TN TP + TN + FP + FN , where TP, TN, FP, and FN refer to the sample number of true positive, true negative, false positive, and false negative, respectively. Actually, the accuracy metric is largely dominated by the class (class N) with larger number of samples.
To saliently reflect the classification performance of a model for pathological classes S and V, in addition to class-level F 1 scores F 1 S and F 1 V for these two classes, we further define the average F 1 score of S and V as

Classification Performance.
Following the AAMI recommendation, we particularly focus on the classification performance of classes S and V since the proportions of training samples for these two arrhythmic classes are much higher (2.8% and 7.0%) and cover the majority of arrhythmias. e training samples of classes F and Q are very scarce (0.8% of the whole dataset), and the detection accuracy is usually pretty low in the literature. Figure 3 presents the confusion matrix for the heartbeat classification results on DS2, where the darker color indicates the more accurate prediction. Overall, the proposed method achieves high ECG heartbeat classification performance on classes N, S, and V. Most instances of classes N, S, and V are correctly classified. Nevertheless, the classification of classes F and Q is unsatisfactory. It is mainly due to the considerable small number of training samples for these two classes, as seen in Table 2. Furthermore, we evaluate the record-level  . is is partially because class S has a smaller sample size but more subclasses than class V. Table 4 compares the interpatient heartbeat classification performance of several other methods and ours. Same as our evaluation scheme, these methods trained their models using the DS1 set and were evaluated on DS2, ensuring a fair comparison. As mentioned above, we focus more on the classification performance for classes S and V rather than the overall accuracy which is mainly governed by class N with the very large instances (90% of the whole dataset). In clinic, missing diagnosis is particularly serious, which can be reflected by sensitivity metric. Also, precise diagnosis is necessary. us, the comparison focuses on F 1 scores for pathological classes S and V, taking into account both sensitivity and precision metrics. Moreover, it is easy to make a comparison of a single metric between different methods. us, pat F 1 score, which is the average value of F 1 S and F 1 V for pathological classes S and V, is used as the final metric.

Performance Comparison.
In [3,4,17,35], the traditional ECG classification pipeline is adopted, which extracts features based on experiences from raw or preprocessed ECG signals and then inputs these extracted features into a classifier. Compared with these methods, the proposed method has a higher pat F 1 score of 11.4%-25%. [23,24], and ours utilized a deep-learning model to automatically extract useful features and classification, coupled with some hand-craft features. e proposed adversarial CNN outperforms [23,24] by 17.2% and 5.8% pat F 1 scores, respectively. It can be observed that the proposed method achieves the highest pat F 1 score. On the whole, the proposed method has an advantage in detecting pathological classes, especially class S which is challenging to identify in the MIT-BIH dataset, and also obtains a satisfactory performance (F 1 score of >90%) in detecting class V.

Effects of RR Ratio Features.
To explore the effect of the pre-RR ratio and near-pre-RR ratio for classifying arrhythmias (i.e., classes N, S, V, F, and Q), the box plots that show the distribution of these two RR ratios among classes are given as Figure 4. It is obviously observed that two RR ratios can distinguish pathological classes S and V from class N well. Nevertheless, it is difficult to distinguish between S and V. is is reasonable due to some shared characteristics between pathological ECG recordings, such as too fast or too slow rhythm. erefore, additional ECG feature learning by other techniques is necessary, such as deep learning used in this paper. Class F, which is the fusion of ventricular and normal beats, has a distribution of two RR ratios close to that of class N. Class Q consists of unknown beats. us, its RR ratios span a wide range of distribution. e comparison for classification performance between with/without the pre-RR ratio and near-pre-RR ratio is shown in Table 5. e experimental results demonstrate that these two RR ratio features greatly improve the sensitivity and precision in detecting pathological classes S and V by providing more prior knowledge about heart rhythms to the deep network. CNN vs. Adversarial CNN. Here, the regular CNN indicates the encoder-classifier network. We remove the adversary subnetwork from the proposed framework to validate the effectiveness of adversarial learning. e same data processing, feature extraction, and experiment setting are performed between the regular CNN and the proposed adversarial CNN. e comparison for classification performance is shown in Table 6. It is obvious that the proposed adversarial CNN is far superior to the regular CNN, except that the precision metric for class V is slightly lower. e regular CNN is data driven in essence. However, the ECG recordings provided in the MIT-BIH database are collected from an insufficient number of subjects. erefore, it is challenging to capture the robust features against intersubject variabilities using the regular CNN, and the learned features could be subject related. On the contrary, the proposed adversarial CNN works out concealing the information of subject IDs by the adversarial game. e experimental result suggests that the adversarial learning can significantly facilitate learning generalizable features across subjects that are invariant to subjects.

Choosing the Adversarial Weight Parameter.
e adversarial weight λ makes a tradeoff between the invariance to subjects and task-discriminative performance. A very strong λ will promote the encoder to learn subject-invariant information. However, increasing λ can result in losing taskdiscriminative information. Here, we implemented several experiments to analyze the effect of different adversarial weights λ. Table 7 shows experimental results. For class N, the sensitivity and precision of different λ are all higher than 90%, which should be attributed to a large sample number of class N. For classes S and V, it can be seen that the performance of a higher λ is low (when λ � 0.01, 0.05, and 0.1). When λ � 0.005, the overall performance is the highest.

Visualization of Learned Features.
e t-distributed stochastic neighbor embedding (t-SNE) [36] can reduce high-dimensional data to a two-dimensional map nonlinearly. Here, we applied t-SNE to evaluate the proposed method visually. e preprocessed heartbeat segment is 256dimensional vectors (the length is 128 and the channel number is 2). Combining RR ratio features with the heartbeat segment, 768-dimensional vectors (two RR ratio features and the heartbeat segment are all 256-dimensional vectors) were used as the input of the proposed adversarial CNN. We extracted the outputs from different layers. e visualizations are shown in Figure 5. e sample size of class N was reduced in the figures for a good visualization. It can be observed from Figures 5(a) and 5(b) that no obvious clusters exist in the input feature vectors. As the layer deepens, the clusters become apparent (Figures 5(c) and 5(f )). However, in the first three residual blocks, the clustering of each class is still separated. is means that these feature vectors fail to distinguish classes N, S, V, F, and Q well and further nonlinear operations are required. For the feature vectors output by the global average-pooling layer ( Figure 5(f )), the clustering is very apparent. Figure 5(f ) demonstrates that the extracted features by the proposed method are discriminative to classify multiclass arrhythmias. It is noted that each class may contain multiple clusters. is is because each class consists of multiple subclasses in which some features are different. For example, bundle branch block beat and normal beat belong to class N, while they have different QRS complex durations.

Conclusions
is paper presents a CNN-based adversarial deep-learning framework for interpatient heartbeat classification using a small subject number of ECG signals. e proposed framework consists of an encoder, classifier, and adversary networks. e encoder is used to learn representations from input data generated by raw signal preprocessing and feature extraction procedures. en, these representations are fed separately into the classifier and adversary to classify heartbeats and subject IDs. e overall framework is trained by minimizing the heartbeat classification loss and maximizing the subject ID identification loss, enforcing the encoder to conceal information regarding subject IDs and retain sufficient discriminative information for task (heartbeat) classification. e proposed framework can help to eliminate the interpatient variability and obtain invariant representations across subjects by utilizing the adversarial learning. erefore, it is especially suitable for ECG classification tasks with an insufficient number of patients.

Data Availability
e MIT-BIH Arrhythmia Database used to support the findings of this study is publicly available and can be downloaded at https://physionet.org/content/mitdb/1.0.0/.

Conflicts of Interest
e authors declare that they have no conflicts of interest.