Time Series Classification for Portable Medical Devices

INTRODUCTION: With the continuous progress of the medical Internet of Things, intelligent medical wearable devices are also gradually mature. Among them, medical wearable devices for arrhythmia detection have broad application prospects. Arrhythmia is a common cardiovascular disease. Arrhythmia causes millions of deaths every year and is one of the most noteworthy diseases. Medical mobile information systems (MMIS) provide many ECG signals, which can be used to train deep models to detect arrhythmia automatically. OBJECTIVES: Using deep models to detect arrhythmia is a research hot spot. However, the current algorithms for arrhythmia detection lack of attention to the unsupervised depth model. And they usually build a large comprehensive model for all users for arrhythmia detection, which has low flexibility and cannot extract personalized features from users. Therefore, this paper proposes a personalized arrhythmia detection system based on attention mechanism called personAD. METHODS: The personAD contains four modules: (1) Preprocessing module; (2) Training module; (3) Arrhythmia detection module and (4) User registration module. The personAD trains a separate autoencoder for each user to detect personalized arrhythmia. Using autoencoder to detect arrhythmia can avoid the imbalance of training data. The autoencoder combines a convolutional network and two attention mechanisms. RESULTS: Based on the results on MIT-BIH Arrhythmia Database, we can find that our arrhythmia detection system achieve 98.03% ACC and 99.32% AUC respectively. CONCLUSION: The personAD can effectively detect arrhythmia in ECG signals. The personAD has higher flexibility, and can easily modify the autoencoders for detecting arrhythmia for users .


Introduction
In the 21st century, Internet of Things technology (IoT) has developed rapidly [1] [2].IoT has invented a variety of sensor devices to collect data [3] [4].Researchers have proposed various methods to guarantee the data safety in the transmission process of IoT [5][6][7][8][9][10][11].The medical important basis to diagnose whether patients have cardiac arrhythmia.Professional doctors use medical instruments to collect the patient's ECG, which can assist in diagnosis.Therefore, the research on arrhythmia detection based on ECG is a hot spot in the medical field.If the patients with arrhythmia can be found in time at an early stage, the mortality of cardiovascular disease caused by arrhythmia will be greatly reduced.However, it is not easy to diagnose arrhythmia in time in reality.On the one hand, the diagnosis of arrhythmia requires a professional doctor to make a diagnosis according to the patient's ECG.On the other hand, patients need to go to the hospital to use professional medical devices before they can collect ECG.Therefore, in real life, it is difficult to make a diagnosis in time.
To solve these problems, researchers began to use intelligent algorithms to help detect arrhythmia [15][16][17].Some common machine learning methods has been used for arrhythmia detection.Support vector machine (SVM) is a representative of these methods.For example, both Salam [18] and Chen [19] use SVM to build a classifier to classify normal heartbeat and arrhythmia heartbeat.The classification system built by Chen [19] can also solve the problem of data imbalance.Machine learning algorithm can effectively classify different heartbeats, but its advantages and disadvantages are also obvious.Machine learning algorithms usually have strict mathematical reasoning process, so they are more interpretable.However, machine learning algorithms often have higher requirements for training data, and are not suitable to handle a huge amount of data.For example, the number of different categories of data in SVM training data should be as equal as possible, otherwise it will affect the generalization capability.Arrhythmia detection algorithms based on deep learning can better deal with large-scale datasets.Researchers at this stage tend to build a huge deep learning model with many network layers for arrhythmia detection.For example, Rajpurkar [20] proposed a depth classification model with a total of 34 convolutional layers for arrhythmia detection.Acharya [21] proposed a deep classification network with a total of 11 convolution layers for arrhythmia detection.Both of them have achieved good results on datasets.Similar methods actually train a large model for all users.Although the large comprehensive model has high classification accuracy, Training such a model will cost abounding time.In addition, methods mentioned above all use supervised models to detect arrhythmia.The training data of the supervised model must include all categories of data, and the amount of data in each category cannot vary too much, otherwise the imbalance of training data categories will occur.However, in the field of arrhythmia detection, category imbalance is a common problem.Because in ECG records of patients with arrhythmia, the number of normal heartbeats is usually much greater than that of arrhythmia.
Considering the problems raised above, some scholars tend to build unsupervised models to solve these problems [22][23][24].The most important feature of unsupervised model is that only normal data is needed in the training phase.
Therefore, there is no class imbalance problem in the training process of unsupervised model.There are many kinds of unsupervised models, among which the autoencoder is a common unsupervised depth neural network.A standard autoencoder consists of encoder and decoder.Generally speaking, both encoder and decoder is symmetrical.The encoder compresses the input information and then gets the encoding result which contains the abstract features of the input.Based on the output of encoder, decoder obtains the decoding result.The dimension of the decoding result is consistent with that of the input of autoencoder.And, they are much greater than that of the encoding result.The autoencoder judges whether the input sample is abnormal according to the reconstruction error of the input sample.Autoencoder has been used by some scholars to detect arrhythmia.Keiichi et al. [22] built an autoencoder to extract data features related to arrhythmia.They then used the extracted features to further detect arrhythmia.Thill et al. [23] used time convolution neural network (TCN) to build an autoencoder for arrhythmia detection.TCN consists of three main structures: Causal Convolution, Dilated Convolution and Residual Connections.TCN can learn the characteristics of time series by convolution operation.Hou et al. [24] used LSTM to build an autoencoder for arrhythmia detection.Although the local methods mentioned above all use unsupervised models to detect arrhythmia, they are still committed to training a relatively larger model for all users.This approach is not very flexible, nor can it capture the characteristics of each user.
To sum up, current researchers use a wide range of intelligent algorithms to automatically detect arrhythmias.However, these methods mainly have two problems: (1) the training data of the model is often unbalanced in categories; (2) the model used for arrhythmia detection is usually a large comprehensive model, which is often poor in flexibility and inconvenient to deploy in practical application.Therefore, we believe that it is necessary to design a flexible model without class imbalance for arrhythmia detection.
In order to solve the above problems, we propose a personalized arrhythmia detection system based on autoencoder called personAD.This system provides an independent autoencoder for each user to detect personalized arrhythmia.The autoencoder is based on the convolutional network and an attention mechanism.We only use normal heartbeat to train the autoencoder.It has achieved good performance on the dataset.Here are our major contributions: • we proposed a personalized arrhythmia detection system based on autoencoder.The system trains a separate high-precision autoencoder for each user.and has good performance in detecting the abnormalities in ECG data.
The outline of the rest is as below: Section 2 gives the information of the related work, Section 3 introduces the detail of personAD, Section 4 presents the experiments and Section 5 summarizes the work of the full text and introduces the future work.

Automatic disease detection
Diabetic Eye Disease (DED) is a common eye disease.It troubles many people's lives.Therefore, automatic detection of DED can reduce the workload of doctors and timely remind potential patients to pay attention to eye protection.Sarki et al. [43] studied the problem of automatic detection of DED from retinal fundus images.It is worth noting that the authors have considered the issue of simultaneously detecting multiple categories of diabetic eye diseases from images, known as multi classification tasks.Their proposed model is based on a novel convolutional neural network and achieves good performance on public datasets.Arrhythmias are a common heart disease.It causes millions of deaths every year.Automatic detection of arrhythmia is a common research field.In recent years, researchers have begun to use methods based on deep learning and big data to detect arrhythmia.He et al. [44] proposed an arrhythmia detection framework based on the Internet of Things.This framework includes a data cleaning module and a heartbeat classification module.At the same time, the framework uses two strategies to classify heartbeats: feature engineering based and deep learning based arrhythmia detection methods.The author tested the performance of the proposed method on the MIT-BIH-AR dataset.Epilepsy is a nervous system disease.Automatic detection of epilepsy has also received widespread attention from researchers.Siuly Siuly et al. [45] reviewed various methods proposed by current researchers for automatic detection and classification of epilepsy based on electroencephalogram(EEG) data, and analyzed the characteristics and shortcomings of these methods, providing assistance and support for the future development of software for automatic detection and classification of epilepsy.

Arrhythmia detection
There has been a lot of work using artificial intelligence algorithms to detect arrhythmias.Salam [18] and Chen [19] both used SVM to classify abnormal heartbeats.Rajpurkar [20] and Acharya [21] built a deep neural network using 34 layer and 11 layer convolution neural networks respectively to classify abnormal heartbeat.Kiranyaz [25] and Zubair [26] both applied 1D convolution to arrhythmia detection.In addition, some scholars do not use convolution when detecting arrhythmia based on ECG data.They treat ECG data as time series data.Recurrent neural network (RNN) also has great performance in processing time series.Therefore, some scholars use RNN to detect arrhythmia.For example, Chauhan et al. [27] used Long short-term memory network (LSTM) to detect arrhythmia, which is a classical kind of RNN.Xu et al. [28] used gate recurrent unit (GRU) and convolution layer to build a classifier to detect arrhythmia.GRU is an improved scheme of LSTM.Pandey et al. [29] used Bidirectional Long short-term memory network (BiLSTM) for arrhythmias diagnosis.On the basis of LSTM, BiLSTM transmits information between neurons in two directions.Therefore, BiLSTM can better capture the context information of time series data.These methods are summarized in Table 1.The column "Model Size" represents the overall size of the model.The column "Data Amount" represents the data that needs to be collected for training the model.can also be used for anomaly detection.The training process of one-class deep neural network only relies on the data without anomalies.Unsupervised method is represented by autoencoder [33].The autoencoder is also trained based on the data without anomalies.During the training, the autoencoder can learn how to compress and reconstruct normal samples.When the test data contains abnormal data, the reconstruction error of the autoencoder will be significantly larger.Therefore, such a loss can be a standard to classify test samples based on a given threshold.
Unsupervised models have been widely applied to anomaly detection in medical data.For example, Homayouni et al. [38] used an extended LSTM model to detect anomalies in COVID-19 time series data.The study processed time series data related to COVID-19, including case numbers, death tolls, recovery rates, and hospitalization rates, and split these time series into multiple time-correlated subsequences for anomaly detection.They also added data visualization to further explain anomalies and evaluate the abnormality level of the detected subsequences.Zhao et al. [39] developed an unsupervised anomaly detection framework for detecting anomalies in medical images.The framework learned patterns of normal data by encoding and reconstructing transformations between images and latent spaces.The framework included two unique constraint conditions and an unsupervised learning module, which effectively detected anomalies in medical images.Shvetsova et al. [40] proposed a framework for medical image anomaly detection based on an autoencoder and a retraining pipeline mechanism.The framework demonstrated good performance on two medical datasets containing radiology and digital pathology images, and proposed a new baseline for medical image anomaly detection tasks.Han et al. [41] proposed a new unsupervised medical anomaly detection method using a generative adversarial network for medical image anomaly detection.The method proposed two unique loss functions for reconstructing different stages of brain abnormalities based on adjacent slices of magnetic resonance images from multiple brains.As an unsupervised model, this method requires a large amount of healthy training data and can reliably detect subtle anatomical abnormalities and the accumulation of highintensity lesions.In addition to these unsupervised methods, some supervised methods have also been widely used in medical data anomaly detection.Many researchers detect suspicious medical images and segment regions of interest in these images.Recently, a variant of recurrent neural networks, Neural Memory Networks, has also been applied to medical anomaly detection.Neural Memory Networks provide an external memory stack for storing information.In reference [42], the authors combined a Neural Memory Network with a neural plasticity framework to identify tumors in magnetic resonance imaging and anomalies in electroencephalograms.

System Overview
This section introduces the overall structure of our personalized arrhythmia detection system: personAD.The overall structure of personAD is illustrated in Figure 1 and Algorithm 1.It mainly includes four parts: Preprocessing module, Training module, Arrhythmia detection module and User registration module.The preprocessing module converts the input ECG signal into Gramian matrices.The training model module trains a separate autoencoder for each user.The system of detecting arrhythmia uses a trained autoencoder to detect abnormal heartbeat.We will introduce the detailed contents of these modules in following articles.

Preprocessing module
We show each step of the preprocessing module in We uniformly resample the unprocessed ECG signal to 360HZ [34].Usually, in order to improve the classification accuracy, researchers will first denoise the original signal [35].Therefore, we denoise the resampled ECG signal according to the following formula: where  represents the power of 2 and  represents a integer multiple.We believe that it is more likely to extract effective features by processing each heartbeat separately than by processing continuous ECG data.Therefore, we divide the ECG signals that have gone through the first two steps into a set of heartbeats.Detecting R peak is a common method to segment heartbeat.Each heartbeat has a unique position of R peak, and we can use R peak to segment heartbeat.We use a mature R-peak localization method to [36] to find the specific position of R peaks in a ECG signal.Then, we intercept 99 and 200 timestamps before and after these positions.Those timestamps conspire to form complete single beats.After dividing a single heartbeat, we convert all the heartbeats into Gramian matrices one by one.Gramian matrices can represent the overall characteristics of time series.All the time series data as input in this article will be converted to Gramian matrices.AE decoder (•) consists of three convolution layers.Each layer uses a kernel of 3*3.Each convolution layer is followed by a BatchNormalization layer and a LeakyReLU activation layer.Between the first layer and the second layer, we use channel and spatial operator to further extract features.We use X to represent the input data.See the following formula for the working engineering of above two attention operators:

Training module
where P 1 is a convolution operator using a 3*3 convolution kernel. c is the channel operator. s is the spatial operator.⨂ indicates dot multiplication.We show the process of  c and  s in the following formula: where  is the sigmoid activation function.MLP is a simple perceptron network with only one hide layer.
AvgPool means average pooling operation.MaxPool indicates the maximum pool operation.
Finally, the output result Ψ ′′ of the attention map will get the encoding result Z through two convolution layers.The process is as follows: The encoding result Z will be used as input data to the decoder AE decoder (•) .AE decoder (•) contains three convolution layers.Similar to the encoder AE encoder (•), each convolution layer uses a 3*3 convolution kernel, and each convolution layer is followed by a BatchNormalization layer and a LeakyReLU activation layer.The working process of AE decoder (•) is as follows: where X ̂ is the output of the autoencoder.X ̂ and X has the same dimension.We trained the autocoder using the Gramian matrices obtained from normal heartbeat conversion.The aim of training the autoencoder is to minimize the reconstruction error.The reconstruction error is as follows: where X is the inputting information of our model and X ̂ is the output (also is the reconstitution result) of the model.‖ ‖ 2 2 is the quadratic power of the second norm of a matrix.

Arrhythmia detection module
After training the autoencoder for each user, the autoencoder can be used for personalized arrhythmia detection.The autoencoder only uses the heartbeat data of a certain user in the training process.Therefore, the autoencoder can extract unique features belonging to the user.The autoencoder only learns the mapping relationship of normal heartbeat reconstruction.When the input heartbeat data contains arrhythmia, the reconstruction error of the autoencoder will be too large.Therefore, we can set a threshold T ad to detect arrhythmia.If the loss of the sample is greater than the threshold T ad , the classified sample is an abnormal heartbeat.If the loss of the sample is less than or equal to the threshold T ad , the classified sample is a normal heartbeat.Based on the verification set, we select a value that can maximize the ( − ) value of the autoencoder.This value will be taken as T ad . and  refer to true positive rate and false positive rate respectively.And  and  are calculated as below: where  is true positive,  is false negative,  is false positive and  is true negative.

User registration module
Our system trains a separate autoencoder for arrhythmia detection for each user.Therefore, our system can easily register new users: we only need to collect the normal heartbeat data of new users and use these data to train a new autoencoder.Therefore, our system also has high flexibility.

Experimental Environment
We used MIT-BIH Arrhythmia Database (MIT-BIH-AR) for experiments.This database is a classical dataset in the field of arrhythmia detection.Each record in this database contains two channels.We only extract the first channel's data for experiment.ECG series are divided into training, test and verification set on the basis of the ratio of 4:1:1.See Table 2 for details of MIT-BIH-AR.We use five measures in our experiment, namely: accuracy (), precision (), recall (), f1-score (1) and area under curve ().Among them, the value of auc is calculated according to the area under the ROC curve. , 1 ,  , and  are calculated according to the following formulas:

Performance comparison with other popular models
We compare personAD with other popular methods.We use the training set part of each record to train an autoencoder.Then set the threshold T ad using the validation set part of the record.Finally, the autoencoder is tested using the test set part in the record.We take the average of all the autocoders as the performance of our method.Figure 4 and Table 3 shows our experimental results.According to Figure 4 and Table 3, we can find that our proposed method's performance is relatively good.The  of our method is 98.03%, which is only 0.89% lower than Chen's method.BeatGAN belongs to the binary classifier.Therefore, it also uses  to evaluate the performance of their model.The  of personAD is 4.57% higher than BeatGAN.In addition, all the methods in Table 3 except our method use all the training data in the dataset to train a large comprehensive classifier for all users to detect arrhythmia.However, our method trains an autoencoder using only the normal heartbeat in a record.Therefore, our method has lower requirements for training data, and our overall system is more flexible.

Application of personAD
personAD is a highly flexible system for detecting arrhythmia (see Figure 5).Arrhythmia detection is a widely concerned issue.Accurately detecting arrhythmia can greatly reduce the mortality rate of patients.personAD only uses the data from a single user to train an independent autoencoder for detecting arrhythmia in that user.This autoencoder can effectively extract personalized features of users and it has high accuracy.Furthermore, updating the user's autoencoder is very convenient if their health status changes.Compared to the methods in Table 3, personAD only needs to re-collect the user's ECG data and retrain his/her autoencoder, rather than retraining a large deep model.As a result, personAD can avoid the significant computational resources required to train a large deep model when updating the model.Overall, personAD has high accuracy, flexibility, and efficiency.

Conclusion and Future Work
This paper proposes a personalized arrhythmia detection system: personAD.The system contains four modules: (1) Preprocessing module; (2) Training module; (3) Arrhythmia detection module and (4) User registration module.The system trains a separate autoencoder for each user to detect arrhythmia.The autoencoder combines convolution neural network and two attention mechanisms, and can effectively extract the features of users' ECG data.Moreover, training the autoencoder only requires to use heartbeats without arrhythmias, which can effectively avoid the imbalance of training data.On the whole, personAD has higher flexibility, and can easily modify the autoencoder for detecting arrhythmia for a single user.We conducted experiments on MIT-BIH Arrhythmia Database.Our method can achieve 98.03% ACC and 99.32% AUC on MIT-BIH Arrhythmia Database.
Although the arrhythmia detection system proposed in this paper can effectively detect arrhythmia based on user's personalized data, personAD still faces some challenges.For example, personalized arrhythmia detection strategies only allow the use of the user's own data when training the model, which may result in the model's training effectiveness still having a lot of room for improvement.
In the future, we plan to use data from different modalities (such as using both ECG and EEG signals) to diagnose multiple diseases simultaneously.We also plan to use lighter models to diagnose diseases more efficiently.
The autoencoder is used to detect user specific arrhythmia signals.• we propose an autoencoder for the detection of potential arrhythmias.The autoencoder is based on convolutional network and two attention mechanism, EAI Endorsed Transactions on Scalable Information Systems | Volume 10 | Issue 4 |

6 :
Use X to train an independent autoencoder AE(•) for the user.

7 : 3 ( 11 :
Module Arrhythmia detection module) 8: Input user's test samples.9: set a threshold T ad by maximizing the ( − ) value of the autoencoder based on validation set.10: classify test samples by comparing the reconstruction loss and T ad .Module 4 (User registration module) 12: Input the new user's ECG signals.13: Train a new AE(•) for the new user.

Figure 2 .
An unprocessed ECG signal can only be used as the input data of the autoencoder after four steps.These steps are: Resampling, Wavelet Denoising, Beat Segmentation and Convert to Gramian matrices.Gramian matrices can transform time series into two-dimensional matrix.At the same time, Gramian matrices can also retain the correlation of data at different time points as much as possible.ECG signals collected from different devices are likely to have different sample rates.So we first resample the ECG signal.

3 .
In the Training module, we train a separate autoencoder for each user.The input of the autoencoder is the Gramian matrices output by the preprocessing module.We propose an autocoder AE(•) based on attention mechanism to detect arrhythmia.The structure of AE(•) is illustrated in Figure As it shown, AE(•) is mainly composed of AE encoder (•) and AE decoder (•).AE encoder (•) uses convolution layer to extract the pattern of information input into AE(•) and output compressed representation.AE decoder (•) uses the convolution layer to reconstitute the compressed representation and output the reconstituted outcome.We also take advantage of two special attention mechanism: channel and spatial attention mechanism, to extract features in AE encoder (•).

Figure 2 .
Figure 2. The process of preprocessing module.

Figure 3 .
Figure 3.The structure of our proposed autoencoder.

Figure 4 .
Figure 4.The performance of personAD based on MIT-BIH-AR.

Table 1 .
Comparison of different arrhythmia detection methods [32]aly detection is a common academic problem in artificial intelligence.Researchers usually use some unsupervised or one-class methods to detect abnormal samples.During the training period of unsupervised models and one-class methods, sample labels (used to identify whether the samples are normal or abnormal) are not required.Therefore, these models do not require labeled datasets and can avoid category imbalance issues.One-class SVM[30]is a classical machine learning algorithm for detecting anomaly.It builds the boundary between normal samples and abnormal samples through hyperplane.one-classSVMlearns the parameters of these hyperplanes through the normal data in the trainset.Similar to one-class SVM, one-class deep neural network[31][32]

Table 2 .
The information of dataset.