ECG anomaly detection method based on AE-GAN
Through the ECG anomaly detection method based on AE-GAN, the encoder and generator are trained using adversarial loss and reconstruction loss, and pseudo-samples are generated for training classifiers, which solves the problems of scarcity of data and insufficient generalization capabilities in ECG anomaly detection, and achieves robust detection performance and high accuracy.
Patent Information
- Application Number
- CN202411723980.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-28
- Publication Date
- 2025-03-14
AI Technical Summary
In ECG anomaly detection, existing deep learning methods face the problems of scarcity of data and insufficient generalization capabilities, especially the decline in detection performance in different populations and scenarios.
A ECG exception detection method based on AE-GAN is proposed. By constructing an AE-GAN model including an encoder, a generator, a discriminator and a classifier, the encoder and generator are trained using adversarial loss and reconstruction loss, and pseudo-normal and pseudo-exception samples are generated for training the classifier, realizing unsupervised anomaly detection.
This method can be trained with only a small number of positive samples, has robust detection performance, and is suitable for ECG anomaly detection in different populations and scenarios, improving the generalization ability and detection accuracy of the model.
Smart Images

Figure CN119622583A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of artificial intelligence and relates to an ECG anomaly detection method based on AE-GAN. Background Art
[0002] In medicine, deep learning applications span disease diagnosis, medical image processing, personalized treatment, drug discovery, and continuous health monitoring. For example, the U-Net architecture was designed for biomedical image segmentation, helping to identify structures such as tumors, organs, and blood vessels. Enhanced versions such as 3D U-Net are particularly effective in segmenting 3D scans such as CT and MRI, which is essential for accurate diagnosis, treatment planning, and monitoring of disease progression. Deep learning has also significantly advanced research in medical image reconstruction. Generative models, such as generative adversarial networks (GANs), can improve the quality of images produced by a variety of medical imaging devices. These models can reconstruct high-quality images from low-dose scans and improve the resolution of MRI images, thereby enabling more accurate diagnoses. Deep learning models, especially convolutional neural networks (CNNs), have shown great potential in automated disease diagnosis. For example, CNNs can identify and classify lung nodules in CT scans for lung cancer detection. Compared with traditional methods, deep learning models provide real-time data analysis, improving the speed and accuracy of diagnosis.
[0003] Despite these advances, deep learning models still require large datasets for effective feature learning. This requirement is particularly challenging in anomaly detection tasks due to the scarcity of abnormal data. Due to the difficulty in obtaining abnormal data, unsupervised algorithms have become a practical alternative. Unsupervised learning does not require labeled abnormal data. The model learns the probability distribution of normal data by training only on normal data. The model determines whether the data is normal by identifying whether there is a deviation between the input data and the probability distribution of normal data. Therefore, for high-dimensional time series data (such as ECG), anomaly detection is best done using unsupervised learning techniques, which need to be sensitive to irregularities in complex feature spaces. Therefore, effective anomaly detection methods must be accurate, scalable, and unsupervised.
[0004] In unsupervised deep learning anomaly detection, common methods include reconstruction-based methods such as autoencoders (AE) or generative adversarial networks (GANs), which learn complex data representations to detect anomalies that deviate from normal patterns. For example, autoencoders are trained to reconstruct normal data samples, and reconstruction errors can be used to indicate potential anomalies. Autoencoders are trained using normal data, and we can think that: for the input test sample, if it is a normal sample, the autoencoder can reconstruct the sample well with a small reconstruction error; if it is an abnormal sample, the reconstruction effect is very poor and the reconstruction error is large. Variants such as sparse autoencoders, denoising autoencoders, and variational autoencoders are widely used in this context. However, studies have found that autoencoders often overfit and can also obtain a good reconstruction result on abnormal data, resulting in poor model discrimination performance. GANs consist of a generator and a discriminator, and learn the distribution of normal data through adversarial training. However, GANs may also have problems in generalization ability, occasionally accurately reconstructing abnormal inputs, resulting in abnormal data being judged as normal data. Another promising approach is to simulate abnormal data through synthesis methods, which use normal samples to estimate the decision boundary between normal and abnormal samples. However, synthetic samples often lack realism, resulting in imprecise boundaries in the feature space, which can hinder accurate classification. A more advanced approach is to use a trained generator to create more realistic anomaly samples by deviating the input feature vector from the normal data distribution (usually by adding noise). This requires an efficient feature extractor, and pre-trained feature extractors (pre-trained on large datasets) are an option. However, the success of this approach relies on the similarity of the pre-training data to the target task, which limits its generalization ability. In addition, while synthetic data has been well explored in the field of computer vision, its application to sequence data (such as ECG) is still limited.
[0005] In summary, deep learning methods have found many applications in the medical field. However, there are still two major challenges in using deep learning methods for electrocardiogram (ECG) anomaly detection. First, collecting and sharing large-scale ECG datasets raises privacy and security issues, as ECG data involves sensitive biometric information and requires annotation by trained medical professionals. These factors limit the availability of high-quality, accurately annotated ECG data, as abnormal samples are much rarer than normal samples, making it difficult to train robust machine learning models. In addition, the annotation process is labor-intensive and costly, further exacerbating the challenge of building a comprehensive dataset. Second, ECG signals vary significantly across different populations and scenarios, leading to the risk of overfitting or underfitting, as a model trained on a certain population or scenario may perform poorly on other populations or scenarios. This variation affects the generalization ability of the model, resulting in reduced performance in detecting anomalies in different patient populations and real-world environments.
[0006] Therefore, a new ECG abnormality detection method is urgently needed to solve the above problems. Summary of the invention
[0007] In view of this, the purpose of the present invention is to provide an ECG anomaly detection method based on AE-GAN, and propose an ECG anomaly data intelligent detection model based on deep learning unsupervised learning method, which can train an artificial neural network model with only a small number of positive samples as training data to detect whether the input ECG signal is abnormal. In addition, it ensures robust detection performance under different population data and scenarios, that is, when training and testing using data from different individuals, the availability of the model can still be guaranteed.
[0008] In order to achieve the above object, the present invention provides the following technical solutions:
[0009] An ECG anomaly detection method based on AE-GAN specifically includes the following steps:
[0010] S1: Construct AE-GAN, including encoder (MemSEencoder), generator (Generator), discriminator (Discriminator) and classifier; AE-GAN represents the generative adversarial network (GAN) based on autoencoder (AE);
[0011] S2: Training AE-GAN for ECG (electrocardiogram) anomaly detection includes the following two stages:
[0012] Phase 1: encoder training;
[0013] AE-GAN is trained on normal ECG samples, using adversarial loss and reconstruction loss to optimize the encoder (MemSEencoder), generator (Generator) and discriminator (Discriminator); the encoder (MemSEencoder) is used to learn the latent feature space of normal samples; the generator reconstructs the input normal samples based on this latent feature space, while the discriminator distinguishes between normal samples and reconstructed samples;
[0014] Phase 2: classifier training and testing;
[0015] AE-GAN uses the trained encoder (MemSEencoder) to extract low-dimensional features from real samples, and adds weak random Gaussian noise to generate pseudo-normal samples, and adds strong random Gaussian noise to generate pseudo-abnormal samples; the generated pseudo-normal samples and pseudo-abnormal samples are then used to train the classifier; the classifier is trained using the binary cross entropy (BCE) loss function to distinguish normal samples (including real normal samples and pseudo-normal samples) from pseudo-abnormal samples; by optimizing the classification accuracy in the feature space, the binary cross entropy (BCE) loss ensures that the classifier effectively detects anomalies; in the testing phase, the trained classifier uses the features extracted by the encoder (MemSEencoder) to detect and distinguish real normal samples from real abnormal samples.
[0016] Furthermore, in step S1, the encoder (MemSEencoder) combines the channel attention (SE) module and the memory (Memory) module. The specific structure is: it consists of four convolution blocks, one channel attention (SE) module is located after the first convolution block, and the other is located before the last convolution block, followed by a flattening layer, a linear layer and a memory (Memory) module; each convolution block contains two convolution layers with ReLU activation, followed by a maximum pooling layer for downsampling.
[0017] Furthermore, in step S1, the structure of the channel attention (SE) module is as follows: first, mean extraction is performed, and then two fully connected layers with ReLU and Sigmoid activations are passed to enhance the model's ability to focus on important features and improve the efficiency of the convolutional layer.
[0018] Furthermore, in step S1, the memory module consists of a learnable memory matrix and an attention mechanism, so as to refine the input features by focusing on key patterns and improve the representation of data features.
[0019] Furthermore, in step S1, the generator includes an expansion layer, a conversion layer and a mapping layer; the expansion layer of the generator is responsible for increasing the dimension of the input potential vector; through linear transformation and Sigmoid activation function, the expanded potential vector value is limited to between 0 and 1; the conversion layer contains a single-layer LSTM network, which processes the potential vector sequentially; the mapping layer is a linear transformation, which projects the output sequence of the LSTM into the required feature space to ensure that the processed potential vector is converted into high-dimensional data.
[0020] Further, in step S1, the discriminator (Discriminator) includes an expansion layer, a channel attention (SE) module, an LSTM layer and a linear block, and the specific structure is: the discriminator first converts the input data into a high-dimensional feature space through the extension layer of the discriminator; this is achieved through a 1D convolution layer, followed by a ReLU activation function; the convolution operation captures local patterns in the input data, and ReLU introduces nonlinearity, enabling the model to extract more complex features; then, the channel attention (SE) module enhances the model's ability to focus on the most important features in the data; next, as the core of the discriminator, the LSTM layer captures the temporal dependencies and sequential patterns in the attention-weighted features; the LSTM layer of the discriminator consists of 9 layers, each with a hidden layer size of 9, enabling the model to process inputs sequentially and learn complex data representations; this deep LSTM structure ensures that the discriminator is very sensitive to both short-term and long-term patterns in the data, which is crucial for accurate discrimination. The final stage of the discriminator is a linear block that projects the output of the LSTM layer into a binary decision space; this linear block consists of a linear layer and a sigmoid activation function, providing a probability score indicating whether the input data is real or generated.
[0021] Furthermore, in step S1, the classifier is composed of four consecutive convolutional blocks and a channel attention (SE) module, followed by a linear layer.
[0022] Furthermore, in step S2, the AE-GAN model uses the reconstruction loss L Rec To minimize the error in reconstructing normal samples, ensure that the encoder accurately captures the potential features; adversarial loss L Adv Drive the generator to generate real data that matches the normal data distribution; In order to optimize these two losses during model training, weight coefficients β1 and β2 are introduced to balance their contributions;
[0023] The overall training objective of the first stage is expressed as:
[0024] min E,G min D L=β1L Adv +β2L Rec
[0025] Among them, β1 and β2 are hyperparameters, and β2 is set to a smaller value;
[0026]
[0027] Among them, α is a hyperparameter in training; Represents the real sample set X r Sample x in r , find the expectation of a given expression; R(xr ,G(E(x r ))) represents the reconstruction error of the data generated by the generator, x r represents the real sample, G(E(x r )) represents the generator input E(x r ) after the generated data, E(x r ) represents x r The feature vector of the latent space obtained after encoding by the encoder; Represents the entropy loss of the memory module, and the subscript i represents the vector The i-th element in Represents the memory addressing weight of each sample;
[0028]
[0029] in, Represents the generated data distribution X g Take the expected value of the expression after sampling, Represents the distribution of interpolated data Sampling and taking the expected value of the expression, D(·) represents the data output by the discriminator, λ is a constant, X g represents the data generated by the autoencoder; Indicates that through the real sample x r And generate samples x g The new samples generated by interpolation belong to the interpolation data distribution Represents the discriminator for the interpolation sample The gradient of ), is obtained by interpolating X r and X g obtained, and It is used to penalize the degree to which the gradient deviates from 1 in adversarial training, which can indirectly ensure that the discriminator satisfies the Lipschitz constraint.
[0030] Furthermore, in step S2, the binary cross entropy (BCE) loss function L used by the classifier is c The expression is:
[0031]
[0032] Among them, z i is the input feature; y i ∈{0,1} represents the label, 0 represents the pseudo-abnormal feature, and 1 includes the real normal feature and the pseudo-normal feature; C(z i ) indicates that the input is zi The output of the classifier, N represents the total number of training data.
[0033] The beneficial effects of the present invention are as follows: the AE-GAN model proposed in the present invention only requires normal data and can be trained without the use of labeled abnormal data. Moreover, when the amount of normal data is small, the present invention can perform data enhancement by generating pseudo-positive samples, thereby increasing the amount of training data, so that the trained model has better effect. The AE-GAN model of the present invention can train an artificial neural network model to detect whether the input ECG signal is abnormal when only a small number of positive samples are needed as training data. In addition, it ensures robust detection performance under different population data and scenarios, that is, when training and testing using data from different individuals, the availability of the model can still be guaranteed.
[0034] Other advantages, objectives and features of the present invention will be described in the following description to some extent, and to some extent, will be obvious to those skilled in the art based on the following examination and study, or can be taught from the practice of the present invention. The objectives and other advantages of the present invention can be realized and obtained through the following description. BRIEF DESCRIPTION OF THE DRAWINGS
[0035] In order to make the purpose, technical solutions and advantages of the present invention more clear, the present invention will be described in detail below in conjunction with the accompanying drawings, wherein:
[0036] Figure 1 This is the overall structure diagram of AE-GAN;
[0037] Figure 2 This is the structural diagram of the encoder MemSEncoder;
[0038] Figure 3 It is the structural diagram of SE module;
[0039] Figure 4 This is the structure diagram of the generator;
[0040] Figure 5 It is the structural diagram of the discriminator;
[0041] Figure 6 It is the structural diagram of the classifier;
[0042] Figure 7 ROC curves of experimental results of different models on MIT-BIH and ECG5000;
[0043] Figure 8 Comparison between normal samples and generated samples. DETAILED DESCRIPTION
[0044] The following describes the embodiments of the present invention by specific examples, and those skilled in the art can easily understand other advantages and effects of the present invention from the contents disclosed in this specification. The present invention can also be implemented or applied through other different specific embodiments, and the details in this specification can also be modified or changed in various ways based on different viewpoints and applications without departing from the spirit of the present invention. It should be noted that the illustrations provided in the following embodiments only illustrate the basic concept of the present invention in a schematic manner, and the following embodiments and features in the embodiments can be combined with each other without conflict.
[0045] See also Figures 1 to 8 , the present invention proposes a generative adversarial network (GAN) based on autoencoder (AE), referred to as AE-GAN, for ECG anomaly detection. The work of AE-GAN is divided into two stages: GAN based on autoencoder is used for encoder training and classifier training and detection. In the first stage, AE-GAN trains the autoencoder GAN only on normal ECG samples, and uses adversarial loss and reconstruction loss to optimize the encoder (MemSEencoder), generator (Generator) and discriminator (Discriminator). MemSEencoder is an encoder that combines the channel attention SE module and the memory module, which can effectively learn the latent feature space of normal samples. MemSEencoder can accurately extract features from samples of different populations during training and testing, ensuring robust detection performance under different demographics and scenarios. In the second stage, AE-GAN uses the trained MemSEencoder to extract low-dimensional features from real samples. In order to train the classifier, the present invention generates pseudo-normal samples and pseudo-abnormal samples by adding weak noise and strong noise to the extracted features respectively. The trained classifier can perform ECG anomaly detection in actual scenarios. Given the test ECG data, the classifier is able to detect whether the data is abnormal.
[0046] The model training of the present invention includes an autoencoder (AE) and a generative adversarial network (GAN) in the first stage, and a classifier (abbreviated as C) is trained in the second stage. For the convenience of naming and description, the ECG anomaly detection method based on AE-GAN proposed in the present invention is named AECGAN.
[0047] like Figure 1 As shown in FIG. 1 , the AECGAN proposed in the present invention uses a normal sample training model to detect electrocardiogram (ECG) anomalies. The method trains the generator (Generator) through an autoencoder, and uses the trained encoder (MemSEncoder) to generate normal and abnormal samples for classifier training and detection. Figure 1As shown, the AECGAN architecture consists of two stages: encoder training based on the autoencoder GAN, and classifier training and detection.
[0048] In the first stage, AECGAN uses AE-based GAN for encoder training, adopting reconstruction loss and adversarial loss. The AE-GAN consists of three modules: encoder MemSEncoder ( Figure 1 Indicated by E), generator ( Figure 1 denoted by G in the figure) and the discriminator ( Figure 1 Indicated by D in the figure). MemSEncoder is an encoder that combines a memory module and an SE (Squeeze-and-Excitation) module to learn the latent feature space of normal samples. The generator reconstructs the input normal samples based on this latent feature space, while the discriminator distinguishes between normal samples and reconstructed samples. In order to train GAN based on autoencoders, AE-GAN uses reconstruction loss and adversarial loss to ensure that the output of the generator is consistent with the real data, thereby effectively extracting features. The adversarial loss ensures adversarial training between the generator and the discriminator, so that the distribution of generated samples is highly similar to that of normal samples. The reconstruction loss minimizes the reconstruction error, allowing MemSEncoder to accurately capture the latent features of generated samples and real samples.
[0049] In the second stage, AECGAN uses the trained MemSEncoder to extract features in the low-dimensional space and adds weak random Gaussian noise to generate pseudo-normal samples and strong random Gaussian noise to generate pseudo-abnormal samples. The generated pseudo-normal samples and pseudo-abnormal samples are then used to train the classifier ( Figure 1 The classifier is trained using the binary cross entropy (BCE) loss function to distinguish normal samples (including real normal samples and pseudo normal samples) from pseudo abnormal samples. By optimizing the classification accuracy in the feature space, the BCE loss ensures that the classifier effectively detects anomalies. In the testing phase, the trained classifier uses the features extracted by MemSEncoder to detect and distinguish real normal samples from real abnormal samples.
[0050] Phase 1: Encoder training based on AE’s GAN
[0051] 1. Encoder (MemSEncoder)
[0052] The MemSEncoder module learns the latent feature space of normal samples. The present invention designs the MemSEncoder architecture, which combines the memory module and the channel attention squeeze-and-excitation (SE) module to improve the feature representation of the convolutional neural network (CNN). The memory module effectively stores the typical patterns of normal samples, thereby facilitating better feature retrieval. The SE module adaptively recalibrates the channel weights to emphasize the key features most relevant to the task, thereby enhancing the overall feature extraction. Figure 2 As shown in Figure 1, MemSEncoder consists of four convolutional blocks, one SE module after the first convolutional block and another before the last convolutional block, followed by a flattening layer, a linear layer, and a memory module. Each convolutional block contains two convolutional layers with ReLU activations, followed by a maximum pooling layer for downsampling.
[0053] SE modules such as Figure 3 As shown in the figure, the mean is first extracted, and then the channel weights are calculated through two fully connected layers (with ReLU and Sigmoid activation), thereby enhancing the model's ability to focus on important features and improving the efficiency of the convolutional layer. In addition, the Memory module consists of a learnable memory matrix and an attention mechanism, which refines the input features by focusing on key patterns and improves the representation of data features.
[0054] MemSEncoder receives a real sample x of shape (1,256) r ∈X r The first convolutional block applies a 1D convolutional layer to increase the number of channels to 64 while downsampling the features to x r The following convolutional blocks and SE modules further compress the features into a vector of shape (8, 16). The vector is then flattened and projected into a 1D vector of length 20 through a linear layer. Finally, the Memory module outputs a 1D latent vector z of dimension 20, representing the encoded features.
[0055] Since we need to add Gaussian noise when generating data, it is also necessary to ensure that the scale of the output feature vector is consistent. The output of the Memory module is normalized by z-score:
[0056] The architecture dynamically adjusts the importance of different channels and enhances the ability to capture key feature patterns, making it highly effective in anomaly detection, image classification, and other computer vision applications. The combination of SE modules and Memory modules ensures that the model always focuses on the most relevant information, thereby improving the depth and quality of feature representation.
[0057] 2. Generator
[0058] The generator module maps the latent vector to high-dimensional data, thereby realizing the generation of real samples. The generator architecture designed by the present invention combines a series of linear transformations and long short-term memory networks (LSTM), enabling the generator to effectively capture complex patterns and time dependencies. Figure 4 As shown, the generator consists of an extension layer (Extension G ), conversion layer and mapping layer. Specifically, the extension layer G ) is responsible for increasing the dimensionality of the input latent vector. Through linear transformation and Sigmoid activation function, the expanded latent vector values are clamped between 0 and 1. This normalization is essential to provide stable input to subsequent layers.
[0059] As the core of the generator, the transformation layer contains a single-layer LSTM network, which plays a key role in capturing temporal dependencies and learning complex patterns in the extended latent vector. By sequentially processing the latent vector, LSTM enables the generator to develop rich and complex data representations, which is essential for generating realistic outputs. The mapping layer is a linear transformation that projects the output sequence of the LSTM into the required feature space, ensuring that the processed latent vector is converted into high-dimensional data to reflect the structure and pattern of real-world data.
[0060] Using the output z(1,20) from MemSEncoder, the generator first expands z into a tensor of shape (256,9) in the expansion layer. Then, the tensor is processed through the LSTM conversion layer and serialized into data of shape (256,9). Finally, the mapping layer maps the data to the generated sample, generating data x of shape (1,256) g .
[0061] The architecture effectively captures spatial and temporal dependencies, enabling the generator to produce realistic and coherent data. The combination of linear transformation and LSTM network makes the generator module particularly suitable for processing tasks involving sequence or time series data, as well as other applications that require rich, structured data generation.
[0062] 3. Discriminator
[0063] The discriminator module is a key component of the adversarial network, which is specially designed to distinguish between real samples and generated samples. The discriminator architecture designed in this paper is designed to effectively capture and evaluate complex patterns in input data, thereby achieving powerful and accurate recognition. The discriminator is mainly composed of an extension layer (Extension D ), SE modules, LSTM layers and linear blocks, such as Figure 5 shown.
[0064] Specifically, the discriminator first passes through the extension layer D ) transforms the input data into a high-dimensional feature space. This is achieved through a 1D convolutional layer followed by a ReLU activation function. The convolution operation captures local patterns in the input data, while the ReLU introduces nonlinearity, enabling the model to extract more complex features. The SE module then enhances the model's ability to focus on the most important features in the data. Next, as the core of the discriminator, the LSTM layer captures temporal dependencies and sequential patterns in the attention-weighted features. The LSTM network of the discriminator consists of 9 layers, each with a hidden layer size of 9, enabling the model to process inputs sequentially and learn complex data representations. This deep LSTM structure ensures that the discriminator is very sensitive to both short-term and long-term patterns in the data, which is critical for accurate discrimination. The final stage of the discriminator is the linear block, which projects the output of the LSTM layer into a binary decision space. This block consists of a linear layer and a sigmoid activation function, providing a probability score indicating whether the input data is real or generated.
[0065] Input data x g or x r The shape is (1,256), first through the extension layer (Extension D ) and outputs a feature of shape (9,256), effectively expanding the spatial dimension to 9. Next, this feature passes through the SE module to emphasize the key spatial dimension and forms a vector of shape (256,9) after transposition. Then, the LSTM layer extracts the temporal features to form a feature vector of shape (1,9). Finally, based on this 1D vector, the linear block outputs a value indicating whether the input sample is a generated sample or a real sample.
[0066] The combination of SE modules and LSTM layers ensures that the discriminator can effectively process complex, sequential and context-dependent data, making it highly robust in adversarial tasks.
[0067] 4. Loss function and training
[0068] This model adopts reconstruction loss (L Rec ) to minimize the error in reconstructing normal samples, ensuring that the encoder accurately captures the latent features. In addition, the adversarial loss (L Adv ) drives the generator to generate realistic data that matches the normal data distribution. In order to optimize these two losses during model training, we introduce weight coefficients β1 and β2 to balance their contributions. Figure 1As shown in Figure 1, the autoencoder-based GAN integrates the traditional encoder-decoder network (E and G) with an additional discriminator (D). This integration ensures that the data output by the generator is consistent with the real data while maintaining diversity, which is achieved by optimizing the network to use both reconstruction loss and adversarial loss. In addition, training the autoencoder-based GAN enables the encoder to act as an effective feature extractor, which is crucial for the second stage of our training process. In the autoencoder, the encoder extracts features from the high-dimensional input data and represents it as a feature vector in the latent space, while G reconstructs the data from this feature vector, and the reconstruction error of the generated data is calculated as:
[0069]
[0070] Among them, x r represents the real sample.
[0071] Inspired by MemAE, E introduces a Memory module to record the typical patterns of normal samples and help remember the distribution of real data. represents the memory addressing weight of each sample. In MemSEncoder, for a given input x, E first processes it to obtain the query z q Then, according to the memory item and z q The similarity calculation weight w. Perform a hard reduction operation on w to obtain the weight vector This vector is used in the Memory module. We use the following formula to express the calculation from x The process:
[0072] The entropy loss of the Memory module is defined as:
[0073]
[0074] The subscript i represents a vector Therefore, the reconstruction loss of the autoencoder network, regularized by the Memory module, can be formalized as:
[0075]
[0076] Among them, α is a hyperparameter in training.
[0077] For the discriminator D, the present invention adopts the method proposed by WGAN to achieve stable adversarial training between the autoencoder network and the discriminator D. The optimization objective between them is formalized as:
[0078]
[0079] Where λ is a constant, Xr represents the real data, X g represents the data generated by the autoencoder, is obtained by interpolating X r and X g obtained, and Used to enforce the Lipschitz condition of the discriminator D during adversarial training.
[0080] Finally, the present invention introduces β1 and β2 as coefficients to balance L Adv and L Rec The overall training goal of phase 1 can be expressed as:
[0081] min E,G min D L=β1L Adv +β2L Rec (5)
[0082] Among them, β1 and β2 are hyperparameters, and β2 is set to a smaller value.
[0083] Phase 2: Classifier training and testing
[0084] In the second stage, if Figure 1 As shown, the present invention uses the MemSEncoder trained in the first stage to generate pseudo-normal and pseudo-abnormal features in the latent space for classifier training. In this way, the classifier can learn richer feature representations, thereby improving the ability to distinguish normal samples from abnormal samples. The generated pseudo samples not only expand the training data set, but also enhance the robustness of the classifier, enabling it to detect anomalies more effectively in practical applications.
[0085] 1. Training samples
[0086] The pseudo-normal and pseudo-abnormal features used to train the classifier are generated by adding Gaussian noise ∈ ~N(0,σ 2 ) is added to the real normal sample feature vector z0 extracted by MemSEncoder. We generate noise of different strengths by adjusting the hyperparameter σ: a larger σ produces strong noise ∈ strong , while a smaller σ produces slight noise ∈ slight Since the eigenvectors of strong noise deviate significantly from the original probability distribution, we - =z0+∈ strong is considered as a pseudo-anomalous feature. In contrast, the feature vector of slight noise is still close to the original distribution, making z + =z0+∈ slight Suitable as pseudo-normal features. This synthesis algorithm enables us to generate unlimited training samples based on limited real normal data, providing rich training data for the classifier.
[0087] 2. Classifier
[0088] The classifier is based on the CNN architecture, which consists of four sets of consecutive convolutional blocks and SE modules, followed by a linear layer, such as Figure 6 As shown in Figure 2. Similar to the generator and discriminator described previously, the combination of convolutional blocks and SE modules in the classifier is designed to capture complex patterns and dependencies in the input features. The fully connected linear layer maps the outputs of these convolutional layers into confidence scores that reflect the likelihood that the input feature is classified as pseudo-normal or pseudo-anomaly.
[0089] like Figure 6 As shown, the first 1D convolution block is used to transform the input features (z - or + ) is increased to a tensor of shape (64, 10), and the following SE module is used to enhance the key channels. After four sets of combined feature extraction, the output 1D compressed feature dimension is 8, which is input to the final linear block to obtain the confidence score.
[0090] To optimize the classifier, binary cross entropy loss (BCE) is used as the loss function:
[0091]
[0092] Among them, z i is the input feature, y i ∈{0,1} represents the label (0 indicates pseudo-abnormal features, 1 includes both real normal features and pseudo-normal features).
[0093] experiment:
[0094] 1. Dataset
[0095] In this experiment, two public datasets are utilized to evaluate the proposed method: the MIT-BIH Arrhythmia Database and the ECG5000 dataset.
[0096] The MIT-BIH Arrhythmia Database is a widely used anomaly detection dataset. The ECG signals in this database are from 47 subjects. All records are collected continuously for 30 minutes. Records numbered 102, 104, 107, and 217 are usually excluded because they contain pacing beats. ECG signals are recorded on two leads, producing two-channel data at each time step. One channel captures the signal from the MLII (mV) lead, representing the upper signal, while the other captures the lower signal. Usually, only the upper signal channel is used for anomaly detection and performance evaluation. In our experiments, we use this dataset for two tasks: ECG anomaly detection and cross-population ECG anomaly detection. In the former, the test samples come from subjects that the model has been exposed to during training, while in the latter, the test subjects are never seen by the model. The training-test split strategy of this dataset differs between the two tasks. For details, see the data processing section of the corresponding experiment.
[0097] The ECG5000 dataset is a public benchmark ECG dataset from the UCR Time Series Classification Archive. The dataset contains 5,000 real-world ECG heartbeat time series, each with a single channel and fixed to 140 time steps. The dataset includes normal ECG signals as well as abnormal signals from heart disease patients (involving PVC, R-on-T, SP, and UB). In its official division, the dataset provides 500 samples for training and 4,500 samples for testing. However, in our experiments, we only train the model on normal samples, so we redesigned the dataset division based on the full 5,000 samples, as detailed in Table 2.
[0098] 2. Experimental Setup
[0099] (1) Experimental details
[0100] This experiment uses the Python deep learning framework PyTorch to implement the proposed AECGAN model. The server configuration used in the experiment includes an Intel(R) Core(TM) i9-10900X CPU@3.70GHz (10 cores) and a GeForce GTX 3090 GPU with 24GB of video memory. We use the Adam optimizer to optimize the model parameters, where the learning rate of the encoder and generator is set to 0.001, and the learning rate of the discriminator and classifier is set to 0.0001. In formula (5), β1 and β2 are set to 0.1 and 1, respectively. The capacity of the memory module is set to 512, and α is set to 0.0002. When synthesizing samples, a strong noise σ is used. strong = 0.7 and weak noise σ slight= 0.16. For the ECG5000 dataset, data augmentation is applied during classifier training to increase the number of normal and abnormal samples to six times of the original training data.
[0101] (2) Benchmark Algorithms and Performance Indicators
[0102] This experiment conducts ECG anomaly detection experiments on the MIT-BIH and ECG5000 datasets and compares the results with the latest methods in recent years. The benchmark methods include OCSVM, BeatGAN, ECG-ADGAN, MemAE, AnomalyTransformer, and ECG-NET. To evaluate the model performance, we use multiple metrics: accuracy, precision, recall, and F1 score. Accuracy refers to the proportion of correctly classified instances among all instances. Precision refers to the proportion of true positive instances among all instances classified as positive. Recall refers to the proportion of true positive instances among all actual positive instances. The F1 score is the harmonic mean of precision and recall, balancing the two metrics into a single evaluation. The calculation formulas for these metrics are:
[0103]
[0104] In addition, this experiment uses the receiver operating characteristic curve (ROC) to evaluate performance, which is a visualization tool for evaluating the performance of binary classification models. It shows the relationship between the true positive rate (TPR) and the false positive rate (FPR) of the model at different classification thresholds. The closer the curve is to the upper left corner, the better the classification performance of the model. The area under the ROC curve (AUC) value ranges from 0 to 1. AUC values close to 1 indicate that the model has stronger classification ability, while an AUC value of 0.5 indicates that the model performs as well as random guessing. AUC is used as a numerical indicator, and the larger the AUC value, the better the performance. ROC curves and AUC evaluations do not depend on a specific classification threshold and can fully reflect the performance of the model. Therefore, they are widely used in fields such as medicine, finance, and machine learning.
[0105] We also considered the equal error rate (EER), which is the point where the false rejection rate (FRR) is equal to the false acceptance rate (FAR). In the experiments, we adjusted the threshold to obtain multiple pairs of FRR and FAR values, and selected the two closest pairs (denoted as FAR* and FRR*) to calculate the EER. A lower EER indicates better performance. The definitions of FPR, TPR, FRR, FAR, and EER are as follows:
[0106]
[0107] 3. ECG abnormality detection
[0108] (1) Data processing
[0109] For the MIT-BIH dataset, this experiment divides the heartbeat records into samples according to the position of the R wave peak. Specifically, we use a fixed-size window with a window size of 256 time steps, centered on the R wave peak, of which 127 time steps are located before the peak and 128 time steps are located after the peak to form each sample. According to the AAMI classification rules, all samples extracted from the MIT-BIH records can be classified and counted, as shown in Table 1. It is worth noting that samples in the "unknown" category cannot be classified as normal or abnormal, so they are excluded in both training and testing. Among the abnormal samples, there is a significant difference in the number of samples between different categories. For example, the S category has only 2 samples, while the A or V category contains tens of thousands of samples. Based on this fact, we apply the following rule to construct the test set: for categories with more than 150 samples, a maximum of 150 samples are randomly selected, while for categories with less than 150 samples, all samples are included. Finally, there are 957 abnormal samples in the test set. In order to maintain balance, this experiment randomly selects 957 normal samples (319 samples in each category) from the N, L, and R categories to participate in the test set. Therefore, the test set of this experiment consists of a total of 1914 samples. The normal samples not included in the test set constitute the training set, with a total of 88,841 normal samples.
[0110] Table 1 Composition of the MIT-BIH dataset for ECG anomaly detection task
[0111]
[0112]
[0113] Table 2 Composition of the ECG5000 dataset for ECG anomaly detection task
[0114]
[0115] For the ECG5000 dataset, this experiment uses the 292 normal samples recommended by the official guidelines as the training set to train the model, and then uses the test set samples recommended by the official guidelines (including normal and abnormal samples) for testing. The composition of the experimental dataset is detailed in Table 2, which includes 4 types of abnormal heartbeats, namely PVC, R-on-T, SP, and UB.
[0116] (2) Performance display
[0117] Table 3 Test performance of different models on MIT-BIH and ECG5000 datasets (%)
[0118]
[0119] To evaluate the performance of AECGAN, this experiment conducts anomaly detection experiments on the MIT-BIH and ECG5000 datasets and compares it with recent SOTA (state of the art) models such as OCSVM, BeatGAN, MemAE, Anomaly Transformer, ECG-ADGAN, and ECG-NET. Under the same experimental settings, this experiment conducted experiments on these SOTA models according to the released source code, except that the settings of ECG-NET on the ECG5000 dataset are the same as those of the present invention. Table 3 shows the performance of these models on the two datasets, including F1 score, AUC, accuracy, precision, recall, and EER. We use bold fonts to mark the best performing model under each metric, and underlined fonts to mark the second best performing model. As shown in Table 3, the proposed AECGAN significantly outperforms these SOTA models, achieving the highest F1 score, accuracy, precision, and recall, as well as the lowest EER on the MIT-BIH dataset. It is worth noting that all models perform better on the simpler ECG5000 dataset than on the more complex MIT-BIH dataset. On the MIT-BIH dataset, the model of the present invention achieves the best performance with an F1 score of 94.08%, an AUC of 97.23%, an accuracy of 93.99%, a precision of 93.50%, a recall of 94.67%, and an EER of 6.06%. On the ECG5000 dataset, the F1 score is 97.62%, the AUC is 99.31%, the accuracy is 97.20%, the precision is 97.75%, the recall is 97.49%, and the EER is 2.93%. In addition, in order to provide a more intuitive model comparison, this experiment is performed on Figure 7 The ROC curves of the model of the present invention and the SOTA model on the MIT-BIH and ECG5000 datasets are shown in FIG.
[0120] Overall, all models perform better on the ECG5000 dataset than on the MIT-BIH dataset, as the former contains cleaner and more standardized samples, while the latter exhibits greater noise and variability. Despite these differences, AECGAN still achieves the best performance on the MIT-BIH dataset.
[0121] (3) Visualization Analysis
[0122] In order to verify the effectiveness of the method of generating abnormal samples and enhancing normal samples in this paper, two visualization experiments were conducted. First, we visualized several types of samples: the original normal samples (no noise), the pseudo-normal samples with slight noise generation, and the pseudo-abnormal samples with strong noise generation. Figure 8 In , the first column shows two selected normal heartbeat data samples. The second column shows the samples generated by adding slight noise to the feature vector, which are less different from the normal samples and can be used for data enhancement. The third column shows the samples generated by adding strong noise to the feature vector z, which are significantly different from the normal samples and can be regarded as abnormal samples. It can be observed that the pseudo-normal samples generated with slight noise are very similar to the real normal samples both locally and overall. In contrast, the pseudo-abnormal samples generated with strong noise are similar to the real normal samples in overall structure, but there are significant differences in local parts, reflecting the characteristics of normal and abnormal ECG signals in real scenarios.
[0123] 4. ECG abnormality detection across populations
[0124] In practical applications, it is impractical to collect training data from every individual. Instead, the model should be trained on data from only a subset of individuals while maintaining the performance of anomaly detection on samples from other individuals, which reflects the generalization ability of the model. In order to evaluate the generalization ability of our proposed method, we designed experiments to evaluate its anomaly detection performance on unseen individual samples in the MIT-BIH dataset.
[0125] (1) Data processing
[0126] This time, the dataset containing 44 records (each record corresponds to an individual) was divided into 5 groups for 5-fold cross validation. One group contained 8 records, while the remaining 4 groups contained 9 records each. Then, we sampled each record using the procedure described in the data processing section of the ECG anomaly detection experiment. Next, we used 4 groups for training, containing only normal samples, and the remaining 1 group that was not used for training was used as a test set. In this way, the individuals sampled in the test set were different from the individuals used in training, ensuring that the model had never seen normal and abnormal samples of individuals in the test set before. However, the number of abnormal samples in the test set was significantly less than the number of normal samples. In order to balance the test set, we randomly selected a portion of normal samples whose number was equal to the number of abnormal samples. Finally, this balanced test set was used to evaluate the performance of the trained model.
[0127] (2) Performance display
[0128] The same metrics as the ECG anomaly detection experiment are used to evaluate the performance of the model. The final results are obtained by averaging the metrics of the five test sets in the 5-fold cross validation. For comparison, we conducted the same experiment on the SOTA model and the results are shown in Table 4. The best performing model under each metric is marked in bold font, and the second best performing model is marked in underlined font. As shown in Table 4, our model outperforms all SOTA models in all metrics, achieving 84.31% F1 score, 84.46% AUC, 83.57% accuracy, 81.35% precision, 88.29% recall, and 18.75% EER.
[0129] Cross-population ECG anomaly detection experiments simulating real scenarios demonstrate the excellent generalization ability of AECGAN.
[0130] Table 4 Test performance of different models for cross-crowd anomaly detection on the MIT-BIH dataset (%)
[0131]
[0132] Finally, it should be noted that the above embodiments are only used to illustrate the technical solution of the present invention rather than to limit it. Although the present invention has been described in detail with reference to the preferred embodiments, those skilled in the art should understand that the technical solution of the present invention can be modified or replaced by equivalents without departing from the purpose and scope of the technical solution, which should be included in the scope of the claims of the present invention.
Claims
1. An ECG anomaly detection method based on AE-GAN, characterized in that: The method specifically comprises the following steps: S1: Construct AE-GAN, including encoder, generator, discriminator and classifier; AE-GAN stands for Generative Adversarial Network based on Autoencoder; S2: Training AE-GAN for ECG anomaly detection consists of the following two stages: Phase 1: encoder training; AE-GAN is trained on normal ECG samples, using adversarial loss and reconstruction loss to optimize the encoder, generator, and discriminator; the encoder is used to learn the latent feature space of normal samples; the generator reconstructs the input normal samples based on this latent feature space, while the discriminator distinguishes between normal samples and reconstructed samples; Phase 2: classifier training and testing; AE-GAN uses the trained encoder to extract low-dimensional features from real samples, and adds weak random Gaussian noise to generate pseudo-normal samples, and adds strong random Gaussian noise to generate pseudo-abnormal samples; the generated pseudo-normal samples and pseudo-abnormal samples are then used to train the classifier; the classifier is trained using the binary cross entropy loss function to distinguish normal samples from pseudo-abnormal samples; by optimizing the classification accuracy in the feature space, the binary cross entropy loss ensures that the classifier effectively detects anomalies; in the testing phase, the trained classifier uses the features extracted by the encoder to detect and distinguish real normal samples from real abnormal samples.
2. The ECG abnormality detection method according to claim 1, characterized in that: In step S1, the encoder combines the channel attention module and the memory module. The specific structure is: it consists of four convolution blocks, one channel attention module is located after the first convolution block, and the other is located before the last convolution block, followed by a flattening layer, a linear layer and a memory module; each convolution block contains two convolution layers with ReLU activation, followed by a maximum pooling layer for downsampling.
3. The ECG abnormality detection method according to claim 2, characterized in that: In step S1, the structure of the channel attention module is: first, mean extraction is performed, and then two fully connected layers with ReLU and Sigmoid activations are passed.
4. The ECG abnormality detection method according to claim 2, characterized in that: In step S1, the memory module consists of a learnable memory matrix and an attention mechanism.
5. The ECG abnormality detection method according to claim 1, characterized in that: In step S1, the generator includes an expansion layer, a conversion layer and a mapping layer; the expansion layer of the generator is responsible for increasing the dimension of the input potential vector; through linear transformation and Sigmoid activation function, the expanded potential vector value is limited to between 0 and 1; the conversion layer contains a single-layer LSTM network, which processes the potential vector sequentially; the mapping layer is a linear transformation that projects the output sequence of the LSTM into the required feature space to ensure that the processed potential vector is converted into high-dimensional data.
6. The ECG abnormality detection method according to claim 1, characterized in that: In step S1, the discriminator includes an expansion layer, a channel attention module, an LSTM layer and a linear block. The specific structure is as follows: the discriminator first converts the input data into a high-dimensional feature space through the discriminator's expansion layer; this is achieved through a 1D convolution layer, followed by a ReLU activation function; the convolution operation captures local patterns in the input data, while ReLU introduces nonlinearity, enabling the model to extract more complex features; then, the channel attention module enhances the model's ability to focus on the most important features in the data; next, the LSTM layer captures the temporal dependencies and sequential patterns in the attention-weighted features; the LSTM layer of the discriminator consists of 9 layers, each with a hidden layer size of 9, enabling the model to process inputs sequentially and learn complex data representations; the final stage of the discriminator is a linear block, which projects the output of the LSTM layer into a binary decision space; This linear block consists of a linear layer and a sigmoid activation function, providing a probability score indicating whether the input data is real or generated.
7. The ECG abnormality detection method according to claim 1, characterized in that: In step S1, the classifier is composed of four consecutive convolutional blocks and channel attention modules, followed by a linear layer.
8. The ECG abnormality detection method according to claim 1, characterized in that: In step S2, the AE-GAN model uses the reconstruction loss L Rec To minimize the error in reconstructing normal samples, ensure that the encoder accurately captures the potential features; adversarial loss L Adv Drive the generator to generate real data that matches the normal data distribution; In order to optimize these two losses during model training, weight coefficients β1 and β2 are introduced to balance their contributions; The overall training objective of the first stage is expressed as: minutes E,G minutes D L=β1L Adv +β2L Rec Among them, β1 and β2 are hyperparameters, and β2 is set to a smaller value; Among them, α is a hyperparameter in training; Represents the real sample set X r Sample x in r , find the expectation of a given expression; R(x r ,G(E(x r ))) represents the reconstruction error of the data generated by the generator, represents the real sample, G(E(x r )) represents the generator input E(x r ) after the generated data, E(x r ) represents x r The feature vector of the latent space obtained after encoding by the encoder; Represents the entropy loss of the memory module, and the subscript i represents the vector The i-th element in Represents the memory addressing weight of each sample; in, Represents the generated data distribution X g Take the expected value of the expression after sampling, Represents the distribution of interpolated data Sampling and taking the expected value of the expression, D(·) represents the data output by the discriminator, λ is a constant, X g represents the data generated by the autoencoder; Indicates that through the real sample x r And generate samples x g The new samples generated by interpolation belong to the interpolation data distribution Represents the discriminator for the interpolation sample The gradient of is obtained by interpolating X r and X g obtained, and It is used to penalize the degree to which the gradient deviates from 1 in adversarial training, which can indirectly ensure that the discriminator satisfies the Lipschitz constraint.
9. The ECG abnormality detection method according to claim 1, characterized in that: In step S2, the binary cross entropy loss function L used by the classifier is c The expression is: Among them, z i is the input feature; y i ∈{0,1} represents the label, 0 represents the pseudo-abnormal feature, and 1 includes the real normal feature and the pseudo-normal feature; C(z i ) indicates that the input is z i The output of the classifier, N represents the total number of training data.
Citation Information
Patent Citations
Negative sample adversarial generation method with noise learning
CN111428853A
Unsupervised learning image anomaly detection method based on auto-encoder
CN112101426A
Heterogeneous bidirectional generative adversarial network model and time sequence anomaly detection method
CN113127705A
Electrocardiogram anomaly detection method based on VQ-VAE2 and deep neural network method
CN113171106A
Electrocardiosignal processing method, device and equipment and storage medium
CN115067963A
Cited By
Cross-subject multi-stage brain wave data enhancement method for epilepsy disease diagnosis
CN119833126A
Molecular attribute prediction method and device based on adversarial training auto-encoder architecture
CN120126614A
Man-machine asynchronous diagnosis method, device and equipment for mechanical ventilation patient and storage medium
CN120296528A
Electrocardiogram abnormal data analysis method and system based on artificial intelligence
CN120918671A
Traffic disguise method and device, electronic equipment, storage medium and product
CN121485999A