{'fraction_non_alphanumeric': 0.03569541196252703, 'fraction_numerical': 0.01085755464809032, 'mean_word_length': 5.0494042429526305, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 0, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
ECG-Adv-GAN: Detecting ECG Adversarial Examples with Conditional Generative Adversarial Networks

Electrocardiogram (ECG) acquisition requires an automated system and analysis pipeline for understanding specific rhythm irregularities. Deep neural networks have become a popular technique for tracing ECG signals, outperforming human experts. Despite this, convolutional neural networks are susceptible to adversarial examples that can misclassify ECG signals and decrease the model's precision. Moreover, they do not generalize well on the out-of-distribution dataset. The GAN architecture has been employed in recent works to synthesize adversarial ECG signals to increase existing training data. However, they use a disjointed CNN-based classification architecture to detect arrhythmia. Till now, no versatile architecture has been proposed that can detect adversarial examples and classify arrhythmia simultaneously. To alleviate this, we propose a novel Conditional Generative Adversarial Network to simultaneously generate ECG signals for different categories and detect cardiac abnormalities. Moreover, the model is conditioned on class-specific ECG signals to synthesize realistic adversarial examples. Consequently, we compare our architecture and show how it outperforms other classification models in normal/abnormal ECG signal detection by benchmarking real world and adversarial signals.


I. INTRODUCTION
Electrocardiogram (ECG) is a re-polarization sequence in the human heart recorded over time using a standard noninvasive tool. It is used for clinically diagnosing cardiac diseases by understanding abnormalities in the signal space. In addition, automated arrhythmia recognition systems have become the standard as of late. Many popular systems utilize Deep Neural networks (DNN) in embedded devices to analyze certain rhythm irregularities in patients. For example, the Medtronic Linq monitor uses an injectable chip, the iRhythm Zio monitor uses a wearable patch, whereas Apple Watch Series 4 monitors rhythm irregularities from the wrist [1]. Even insurance companies and contractors partner with these device companies by providing subsidies and benefits to the customers. As there are many stakeholders involved financially, the reliability of these toolkits becomes questionable. Moreover, the companies have to develop creative ways to manage ECG readings as they become more susceptible to Adversarial Examples [2].
Many recent architectures incorporate convolutional or recurrent LSTM-based blocks to find inherent manifold features of different arrhythmia beats [3]- [5]. However, despite recent advances in ECG classification, these methods do not generalize well on out-of-distribution and real-world data. Another problem with deep-learning architecture is, they are susceptible to adversarial examples [6]. Quite recently, there has been a couple of works that adopted GAN for generating synthetic ECG data [7], [8]. However, the first one [7] uses a disjointed architecture to classify arrhythmia, which cannot predict if the signal is real or fake.Additionally, the architecture only utilizes an unbounded adversarial loss, which results in a random signal generation overlooking any class-specific signal features. Also, the authors only evaluate their model on real ECG signals, providing no benchmarks for the adversarial ECG test data. For the second work, [8], the authors focus more on the failures of ECG classification architectures in regards to adversarial attacks and provides no defense against such attacks.
To alleviate this, we propose a new conditional generative network, called ECG-Adv-GAN illustrated in Fig. 1, that can simultaneously synthesize and classify both arrhythmia and adversarial examples as an end-to-end architecture. We benchmark our method with other state-of-the-art architecture on the Intra-patient, and Inter-patient datasets acquired from the MIT-BIH dataset [9]. Moreover, we also synthesize the adversarial test data using our generator and then evaluate our discriminator's performance comparing with other architectures. Consequently, by utilizing LS-GAN loss [10] and reconstruction loss, our generator synthesizes visually realistic ECG signals with small perturbations that can be adopted in clinical applications and day-to-day activities.

II. RELATED WORKS A. Deep Learning Approaches
Lately, deep learning architectures have become a goto for achieving state-of-the-art accuracy for various ECG Fig. 1. Proposed ECG-Adv-GAN consists of a single Generator and Discriminator where the Generator takes the Real ECG signals, a noise vector, and the class labels as input. The Generator consists of an encoder and decoder modules through which it outputs the synthesized adversarial signals. The Discriminator has only an encoding module that takes Real or Adversarial signal as input. It works as a classifier with two output modules, one for categorizing signals and the other for detecting adversarial examples. classification tasks [3]- [5], [11]. Earlier works incorporated Support vector machines (SVM) and Independent component analysis on top of morphological and dynamic features of ECG signals to group them into classes [11]. The problem with this approach is, it relies on handcrafted features extracted by human experts. In contrast, deep learning approaches classify different Heartbeats from ECG signals by automating the feature extraction process. Convolutional neural network succeeds in extracting manifold features and generalizes better on large amounts of data. For example, Kachuee et al. [4] employed a 1D CNN-based model to identify Cardiac abnormalities and obtained almost 94% accuracy in detecting different arrhythmia diseases. Mousavi et al. [3] adopted a Seqto-seq architecture consisting of bidirectional LSTM (Long short-time memory) to achieve the state-of-the-art result in arrhythmia classification from ECG signals. Although these architectures obtain high accuracy in classification tasks, they often fail to identify adversarial examples in the wild. One of our fundamental contributions is to illustrate the failure of such models to classify the correct arrhythmia category when exposed to realistic adversarial examples.

B. Generative Adversarial Networks
Generative Adversarial Networks (GAN) is a synthesis architecture for generating high-quality images, time-series data, and other modalities of imaging [12]- [14]. Generally, the architecture consists of a generator network and a discriminator network. Medical data are highly senstivie in nature, so it can be pretty challenging to synthesize artificial data from real data while retaining the inherence features and preserving privacy. Many recent works incorporated GANs for ECG signal generation to tackle the problem of shortage of data [8], [12], [15]. Delaney et al. [12] proposed a GAN architecture comprising a Bidirectional LSTM-as Generator and LSTM-CNN as Discriminator to synthesize ECG data. However, the authors only focused on synthesizing ECG signals using the generator and provided no evaluation of the discriminator network for the arrhythmia classification task. Shaker et al. [8] used a two-stage unsupervised approach for synthesizing and then classifying ECG signals. In the first stage, the authors utilized DC-GAN [16] by taking Gaussian noise as input and generate random ECG signals for augmenting the training dataset. In the second stage, the authors utilized two separate networks for classifying the ECG signals into distinct categories.This approach has three underlying problems: 1) the generator model is not conditioned on unique class-specific signals and the authors utilized an unbounded adversarial loss, so the synthesis is random, 2) The classifier was not exposed to adversarial exmples while training, instead the author used a disjointed GAN training in first stage to augment the existing ECG data, 3) Utilizing two seperate architectures, 1) Generative Adversarial Network and, 2) ECG classifier is computationally expensive and the classifier can't determine if the ECG signals are real or adversarial. Moreover, the model's accuracy is relatively lower than other state-of-the-art ECG classification models. Our second contribution is, we propose a conditional GAN where the generator retains class-specific information by training it with a closely bounded reconstruction loss, and the discriminator network can simultaneously distinguish and classify real and adversarial ECG signals with high precision.

C. Clinical Significance
Super realistic adversarial samples can be a temptation for companies in the context of drug and device approvals. In different clinical trials, regulatory bodies like FDA (Food and Drug Administration) affirmed interest in using algorithmic biomarkers as an endpoint [2]. In this context, adversarial examples can be a channel for companies to create favorable biased outcomes. For instance, a regulator requires a matched ECG signal for the patient before and after treatment. As a result, trialists could inject adversarial noise or create adversarial samples for post-treatment data, ensuring desired results [2], [17]. In such circumstances, an adversarial ECG detection system can play a pivotal role in identifying borderline fraudulent trials and, in the long run, can save lives.
Moreover, the FDA recently released guidelines and tools to collect Real-World Data (RWD) from research participants [18]. However, such extensive studies require a large amount of Patient-generated Health Data (PGHD), most of which are collected from wearable devices. It is both time-consuming and impractical for clinicians to analyze and do perturbation to address the data shortage [19]. So a more reliable method would be a generative architecture that can synthesize realistic ECG signals imitating distinct rhythmic irregularities.

III. METHODOLOGY
In this paper, we proposed a conditional generative adversarial network (GAN) comprising a generator for producing realistic ECG signals and a discriminator for classifying the real and generated signal's output labels. First, we discussed the proposed architecture in Section III-A. In Section III-B, we discuss the objective function and loss weight distributions for each architecture related to proposed model.

A. Proposed Architecture
Incorporating generative networks with an auxiliary classification module has been shown to produce high-quality image synthesis and highly accurate class prediction as observed in [?]. Inspired by this, we adopt this feature in our architecture using a class conditioned generator and a multi-headed discriminator for categorical classification and adversarial example detection as visualized in Fig. 1. The generator concatenates the original signal x, label y, and a noise vector z as input and synthesizes G(x, z, y) ECG signal of a distinct category. The noise vector, z is smoothed with Gaussian filter with σ = 4 before pushing as input. The generators consist of multiple Convolution, Transposed Convolution, Batch-Norm, and Leaky-ReLU layers. We use convolution for downsampling 3× and use transposed convolution to upsample 3× again to make the dimension the same as the input. For normal convolution we use kernel size, k = 3, stride, s = 1 and padding, p = 1. For downsampling convolution and transposed convolution we use kernel size, k = 3, stride, s = 2 and padding, p = 1. On the other hand, the discriminator takes both real signal x and adversarial ECG signals G(x, z, y) as input. It simultaneously predicts if the example is real or adversarial and classifies the signal as one of four categories. The discriminator consists of multiple Convolution, Batch-Norm, Leaky-ReLU, and Fully Connected layers. We use convolution for downsampling 4 times. For convolution we use kernel size, k = 3, stride, s = 1 and padding, p = 1, except for downsampling convolution where we use stride, s = 2. The convolution layer is succeeded by Batch-normalization and Leaky-ReLU layers. After that, we use two fully connected layers. The encoder consists of eight convolution layers and two dense layers. The number of features are for each layer is [E1, E2, E3, E4, E5, E6, E7, E8, E9, E10]= [16,16,32,32,128,128,256,64,32]. We use two output activation: classification with Softmax for four classes and Sigmoid for real/adversarial prediction.

B. Weighted Cost Function and Adversarial Loss
We use LSGAN [10] for calculating the adversarial loss and training our Generative Network. The objective function for our conditional GAN is given in Eq. 1.
In Eq. 1, we first train the discriminators on the real ECG signals, x. After that, we train with the synthesized signal, G(x). We begin by batch-wise training the discriminators,D on the training data. Following that, we train the G while keeping the weights of the discriminators frozen. In a similar fashion, we train G on a batch of training samples while keeping the weights of all the discriminators frozen. For classification of different normal and arrhythmia signals, we use categorical cross-entropy as in Eq. 2.
The generators also incorporate the reconstruction loss as shown in Eq. 3. We ensure that the synthesized signal contains representative features and bounded by the L2 reconstruction loss. This in turn helps with the generator to output realistic signals with small perturbations and the discriminator to be robust against such adversarial examples, as shown in [20], [21].
By incorporating Eq. 1, 2 we can formulate our final objective function as given in Eq. 4.
Here, λ rec , and λ class denote different weights, that are multiplied with their respective losses. The loss weighting decides which architecture to prioritize while training.

A. Data Set
PhysioNet MIT-BIH Arrhythmia dataset includes the ECG signals for 48 subjects, including 25 men and 22 women, recorded at the sampling rate of 360Hz. American association of medical instrumentation (AAMI) recommends the database as it contains the five fundamental arrhythmia groups.

B. Signal Pre-processing and Data Preparation
We first find the R-Peak for every signal and use sampling rate of 280 centering on R-Peak. We then normalize the amplitude of the signals between [0, 1]. To test our model's robustness against adversarial examples, we divided the original data into two 1) inter-patient and 2) intra-patient sets, as done in [3]. In the intra-patient data, we randomly choose the training and the test sets from the same patient records. In contrast, inter-patient data consists of training and testing set with different patient records. Following Chazal et al. [ [23].

C. Hyper-parameters
For adversarial training, we used least-square-GAN [10]. We chose λ class = 10 and λ rec = 1 (Eq. 4). For optimizer, we used Adam with a learning rate of α = 0.0002, β 1 = 0.5 and β 2 = 0.999. We used Tensorflow 2.0 to train the model in mini-batches with the batch size, b = 64 in 200 epochs which took around 8 hours to train on NVIDIA RTX2080 GPU. We generated random float values in a range of [0, 1] for initializing the noise vector, z. The inference time of our generator and discriminator are 0.0987 and 0.053 milliseconds per instance.

D. Qualitative Evaluation
For finding the structural similarity with the original ECG, we benchmark synthesized adversarial signals using four different metrics, i) Mean Squared Error (MSE), ii) Structural Similarity Index (SSIM), iii) Cross-correlation coefficient, and iv) Normalized Mean Squared Error (NRMSE). We use the same Intra and Inter Patient test set for synthesizing adversarial examples.  Fig. 2. The columns contain Normal (N), Atrial Premature (A), Premature Ventricular (V), and Fusion (F) beats. In contrast, the row contains Real and Adversarial Signals successively. It is apparent from the figure that the synthesized signals are quite vivid and realistic visually.

E. Quantitative Evaluation
For normal and arrhythmia beat classification tasks, we compare our model with other state-of-the-art architectures on Intra-Patient and Inter-Patient test sets as given in Table. II  and Table. III. We further experiment on adversarial examples synthesized using our Generator from the above two tests set, which we also provide in Table. II and Table. III. For metrics, we use Accuracy (ACC), Sensitivity(SEN), and Specificity(SPEC). We can see for the first experiment, except for the architecture given in [3], our model achieves the best score compared to other deep learning and machine learning derived architectures. The architecture in [3] uses Seq2Seq, consisting of Bidirectional LSTM and RNN layers. On the other hand, [4], [5], [8] uses 1D Convolution based architecture. Out of these models, Shaker et al. [8] adopt DC-GAN, a generative network [16] for adversarial signal generation. However, their classification architecture is trained separately, and they provide results only on real ECG signals.
As for the second experiment, we evaluate our proposed ECG-Adv-GAN and other architectures on adversarial examples generated from the Intra-patient and Inter-patient data-    We can see that our ECG-adv-GAN synthesizes realistic looking adversarial ECGs over time set. The authors of these architectures failed to provide any pre-trained weight that we can use to evaluate. So for a fair comparison, we train and test them on the same set. This evaluation's main idea is to test the model's effectiveness and robustness on out-of-distribution and real-world data, which the adversarial example imitates. As shown in Table. II, our model outperforms other the proposed methods for the Intra-Patient data-set. In Table. III for the Inter-Patient data-set, our model achieves the best overall accuracy and sensitivity across all the normal and abnormal beat categories. Only the architecture by Acharya et al. [5] scores better specificity for Normal and Fusion beats. We can also see that Mousavi et al. [3] performs worse for out-of-distribution adversarial data-set, which confirms signs of over-fitting on the real ECG signals.
For the third experimentation, we evaluate our discriminator and check its accuracy to detect adversarial examples as given in Table. IV. We use a test set with 50% adversarial and 50% real data to carry out this benchmark. By combining the real and the adversarial examples synthesized by our generator, we can have this 50/50 test split. So for the Intra-Patient and Inter-Patient set of 19327 and 19152 signals, we end up having 38654 and 38304 samples successively. We use Accuracy, Sen-sitivity, Precision, F1-score, and AUC as standard metrics for measuring our model's performance. As shown in Table. IV, both our model scores high accuracy, precision, sensitivity, and AUC for the Intra-Patient and Inter-Patient data-set. However, the model trained on Intra-Patient data performs better for detecting both and real adversarial examples with more than 80% F1-score and Precision. Another important insight is that the model detects adversarial examples far better than real ECG signals. Consequently, it confirms the hypothesis that Generative Networks are far superior for detecting adversarial examples.

F. Stability of Signal Generation
To evaluate the stability of GAN during training time, we illustrate distinct signals synthesized by the generator network in Figure 3. In the early stage of training, the ECG signal's complexity sometimes prevents the generator from learning a select number of features to fool the discriminator network, as seen in the first row of the image (Epoch 1). Over time, we can see that the outputs synthesized by the generator become more visually realistic. For example, on Epoch-200, the peak of the signal for every class (N, S, V, F) is not differentiable from the original ECG-Signal. To validate, we monitored the GAN's stability by visually evaluating the synthesized samples for each epoch. The signals in Figure 3 produced by the ECG-Adv-GAN were tested with our Discriminator and other stateof-the-art classifiers to determine if the models can accurately classify adversarial samples. From Tables II and III, we can see that our Discriminator outperforms other architecture in classifying adversarial examples.

V. CONCLUSION
In this paper, we introduced ECG-Adv-GAN, a novel conditional Generative Adversarial Network for simultaneous synthesis of adversarial examples and detecting arrhythmia. Our architecture outperforms previous techniques by adopting a dual learning task of synthesizing adversarial ECG signals while predicting the signal category. The model is best suited for real-time ECG monitoring, where it can perform robustly and effectively. One future direction to this work is to incorporate and defend against adversarial attacks in ECG signals.