Method Article

Convolutional Neural Network-Transformer Model to Predict and Classify Early Arrhythmia Using Electrocardiogram Signal

DOI:

10.3791/68227

July 3rd, 2025

In This Article

Summary

Loading...
$$\rightleftharpoonup{xx}$$ $$\longleftharp{xx}$$, $$\longrightharp{xx}$$,

The developed model aims to classify early arrhythmias into N, L,R, V, and A classes. Here, all of the datasets are combined to create a principal dataset, which the model uses as input to produce different arrhythmia classes as output.

Abstract

Loading...
$$\rightleftharpoonup{xx}$$ $$\longleftharp{xx}$$, $$\longrightharp{xx}$$,

As a major cause of death worldwide, cardiovascular diseases-especially arrhythmias-require the creation of precise and automated technologies for early diagnosis and detection. To identify arrhythmias from electrocardiogram (ECG) signals, this paper introduces a deep learning-based classification model that focuses on five main heartbeat types: Normal (N), Left Bundle Branch Block (L), Right Bundle Branch Block (R), Atrial Premature Beat (A), and Premature Ventricular Contraction (V). We leverage Lead I signals from several sources, such as the INCART 12-lead, Sudden Cardiac Death Holter, Supraventricular, and MIT-BIH Arrhythmia databases, yielding more than 3.9 million training and 112,575 testing segments.

Examples of data preparation include 180 sample, fixed-window segmentation, Min-Max normalization, and class balancing with the Synthetic Minority Over-sampling Technique (SMOTE). The hybrid architecture uses Transformer layers to model temporal dependencies and 1D Convolutional Neural Networks (CNNs) to extract spatial features. The Adam optimizer with dropout and batch normalization for regularization trains the model.

The proposed system outperforms the TN4 model and other cutting-edge benchmarks, achieving 99.99% accuracy, precision, and F1-score across all classes. Feature robustness is further improved by applying deep hybrid architectures and convolutional neural networks, which were motivated by earlier studies. The suggested paradigm advances artificial intelligence-driven, individualized digital healthcare and has great promise for scalable, real-time arrhythmia identification.

Introduction

Loading...
$$\rightleftharpoonup{xx}$$ $$\longleftharp{xx}$$, $$\longrightharp{xx}$$,

Cardiovascular diseases (CVDs) remain one of the leading health concerns globally, responsible for nearly 31% of deaths worldwide each year, according to the World Health Organization (WHO)1. A significant subset of these cases involves arrhythmias-irregularities in the heart's rhythm that can range from benign to life-threatening. Arrhythmias are often marked by irregular times. These disruptions substantially contribute to patient morbidity and mortality, heightening the risk of severe health issues such as stroke, heart failure, and sudden cardiac arrest. Early identification and accurate classification of arrhythmias are therefore crucial for improving patient outcomes, managing healthcare costs, and advancing the quality of cardiac care2.

Electrocardiography (ECG) remains a key non-invasive diagnostic tool for detecting arrhythmias. By capturing the heart's electrical activity as visual waveforms, ECG enables clinicians to identify subtle variations that may point to specific arrhythmic patterns3. However, manually interpreting ECG signals is time-consuming and prone to variability due to individual differences among practitioners, which introduces a margin of human error. These challenges are magnified when handling extensive datasets or recognizing nuanced arrhythmias. With the global shift towards digital healthcare, the need for reliable and automated diagnostic systems capable of delivering consistent, real-time ECG analysis is increasingly vital evident4.

Traditional machine learning models have been applied to ECG analysis with moderate success; however, their reliance on handcrafted features and domain-specific expertise poses notable limitations. To address this limitation, the model employs the Synthetic Minority Over-sampling Technique (SMOTE), which generates synthetic data points for underrepresented classes, thereby balancing the dataset and improving the model's capacity to generalize across all heartbeat types. The integration of SMOTE with our deep learning model enhances classification performance, especially for rare arrhythmias, and supports a more equitable diagnostic tool5. The primary objectives of this research are threefold. First, the aim is to develop an accurate, scalable model capable of real-time arrhythmia detection, contributing to the global shift toward digital and personalized healthcare. Second, to demonstrate the effectiveness of a hybrid CNN-Transformer architecture in ECG analysis, highlighting its potential to outperform traditional methods in both accuracy and robustness6.

Finally, the aim is to develop a model with meaningful applications in clinical diagnostics, enhancing early detection of cardiac abnormalities. This study seeks to bridge the gap between deep learning advancements and practical medical applications, positioning our approach as a valuable tool in pursuing improved cardiac care and patient outcomes6.

Access restricted. Please log in or start a trial to view this content.

Protocol

Loading...
$$\rightleftharpoonup{xx}$$ $$\longleftharp{xx}$$, $$\longrightharp{xx}$$,

1. Acquiring datasets

  1. Acquire publicly available electrocardiogram (ECG) datasets to develop and validate the deep learning model for arrhythmia classification7.
  2. Combine Lead-I datasets from MIT-BIH Arrhythmia Database, MIT-BIH Supraventricular Arrhythmia Database, the St. Petersburg INCART 12-lead Arrhythmia Database, and the Sudden Cardiac Death Holter Database.
    NOTE: Datasets are chosen for their diversity in patient demographics and arrhythmia types, ensuring the model can generalize across varied cases. Each dataset provides high-quality annotated ECG recordings, covering a range of heartbeat classes. Only Lead I data was extracted and used across all datasets to standardize the input, maintaining consistency and focusing on the most diagnostically relevant signal. As shown in Figure 1, Lead I measures the potential difference between the right arm (negative electrode) and the left arm (positive electrode), and Lead II measures the potential difference between the right arm (negative electrode) and the left leg (positive electrode). Table 1 illustrates that the principal dataset is created by combining four datasets and eliminating null values, and the file is named Alldata.CSV.

2. Data preprocessing

  1. Divide the data from Alldata.CSV into two parts-train and test. In preprocessing 2, filter the N, L, R, A, V classes from Alldata.CSV and then divide it into two parts-Train and test parts (Figure 2).
  2. Divide each patient's ECG signal into fixed 180-ms windows, each annotated according to the corresponding arrhythmia classes such as N, L, R, A, and V classes (Figure 3).
    NOTE: This segmentation allowed for consistent input length across samples and captured the temporal characteristics within each heartbeat segment.
  3. Read raw ECG files of format (.dat, .hea, and .atr) using Python's Library.

3. Normalization

  1. To ensure uniformity in signal amplitude, normalize all segmented ECG data using Min-Max scaling, transforming each sample to a range between 0 and 1.
    NOTE: The normalization technique used here is MinmaxScaler8.

4. Class balancing with SMOTE

NOTE: A common challenge in arrhythmia classification is the significant class imbalance, where normal heartbeats vastly outnumber abnormal ones.

  1. To address the above challenge, apply the Synthetic Minority Over-sampling Technique (SMOTE) to the training set, which generates synthetic samples for minority classes by interpolating between existing instances9.
    NOTE: The model's capacity to generalize across all arrhythmia types is enhanced by new synthetic samples, which guarantee that each class is fairly represented.

5. Train-test split

  1. After preprocessing, split the data into training and testing sets using a 70-30 split, with stratification to maintain the class distribution across sets.
    NOTE: This split ensured that each arrhythmia class is adequately represented in both the training and testing phases, allowing for a reliable evaluation of the model's performance.

6. Dataset splitting and class distribution

  1. Split the final Dataset into training and testing sets, with 70% of the data used for training and 30% for testing.
  2. Apply SMOTE to balance the class distribution, ensuring that each arrhythmia type is adequately represented in both the training and testing sets, as shown in Table 2.
    NOTE: In total, 3,966,620 ECG segments are used for training, and 112,575 ECG segments are used for testing the model. This large volume of data, along with the diversity of the arrhythmia types, contributed to the model's robustness in detecting different types of arrhythmias in real-world ECG signals.

7. Methodology

  1. Model architecture
    1. To achieve accurate arrhythmia classification, implement a hybrid deep learning model comprising 1D Convolutional Neural Networks (CNNs) and Transformer layers.
      NOTE: Hybrid model architecture is specifically designed to leverage the strengths of both in spatial and temporal features from ECG signals. It combines feature extraction and sequence modelling capabilities.
  2. 1D-Convolutional Neural Network (CNN) Blocks
    1. Set up the CNN block to consist of two 1D convolutional layers, each followed by a ReLU activation function and a max-pooling layer. These layers extract spatial dependencies from the input ECG signal. To enhance feature extraction, add more ReLU activation functions after each transformation step in the CNN layers10.
      NOTE: This configuration enables the model to learn spatial dependencies within each segment effectively.
    2. First Convolutional Layer: Use this layer to apply 32 filters of size 3 to the input signal. The operation is expressed as:
      Neural network activation function formula, yi=σ(Σwj·x(i+j)+b), mathematical expression. (1)
      Where yi represents the output, wj are the weights, x(i+j) is the input ECG segment, b is the bias term, and σ is the activation function (ReLU, in this case)11.
    3. Pooling Layer: After each convolution, set up a max-pooling operation to reduce the spatial dimension by a factor of 2, defined as:
      Mathematical equation yᵢ=max(x₂ᵢ,x₂ᵢ₊₁), formula representation, data analysis method. (2)
      NOTE: This operation retains the most salient features while reducing the computational complexity.
    4. Second Convolutional Layer: Use this layer to apply 64 filters of size 3 to the feature maps from the previous layer, extracting higher-level features12. Apply a ReLU activation function after convolution to introduce non-linearity:
      ReLU activation function formula \( y'_i = \max(0, y_i) \). (3)
      1. Additional Activation Layers: apply ReLU activation at every intermediate stage after convolution to enhance the expressive power of the network, ensuring that the model focuses on positive activations13.
    5. Dropout: Specify a Dropout rate of 0.5 in the fully connected layer section14.
  3. Transformer block:
    1. To follow this protocol, set up the Transformer block to include two multi-head self-attention layers, enabling the model to capture temporal dependencies across different parts of the ECG signal15.
    2. Multi-Head Self-Attention: This mechanism computes relationships between every pair of sequence elements. For a sequence with query Q, key K, and value V, calculate the attention as:
      Attention mechanism formula; softmax function; mathematical expression; neural networks. (4)
      Where Q, K, and V are the query, key, and value matrices, respectively, and dk represents the dimensionality of the key vectors.
      NOTE: By allowing the model to weigh different segments according to their relevance, this mechanism enhances the model's ability to distinguish between arrhythmias with subtle temporal characteristics.
    3. Activation Functions: Use ReLU explicitly across layers.
  4. Continuous Wavelet Transform (CWT):
    1. Before feeding the data into the CNN layers, apply continuous wavelet transforms to convert the ECG signals into time-frequency representations16.
      ​NOTE: This transformation provides a more comprehensive feature set by capturing the frequency variations over time, which are crucial for distinguishing between different arrhythmia types.
  5. Training process:
    1. To train the model, use the Adam optimizer, which dynamically adjusts the learning rate based on the first and second moments of the gradient. Define the update rule for Adam as:
      Gradient descent formula diagram, Adam optimizer update, parameter tuning method. (5)
      Where mt and vt are the first and second moment estimates, α is the learning rate, and ε is a small constant to prevent division by zero17.
    2. Conduct training over 100 epochs, with a batch size of 64, utilizing early stopping to mitigate overfitting.
      ​NOTE: The training data were fed into the model via the PyTorch DataLoader utility, and both dropout and batch normalization were applied to regularize the network and improve convergence.
  6. Hyperparameter tuning
    1. Perform manual tuning using early stopping, Adam optimizer, dropout, batch normalization, and a batch size of 1024.
      ​NOTE: While grid or Bayesian search is not explicitly mentioned in this paper, these choices reflect practical tuning strategies.
  7. Validation techniques
    1. Use a 70-30 stratified train-test split after applying SMOTE18.
      NOTE: While k-fold is not used, stratification preserves class distribution, which is often sufficient with large datasets.

Access restricted. Please log in or start a trial to view this content.

Results

Loading...
$$\rightleftharpoonup{xx}$$ $$\longleftharp{xx}$$, $$\longrightharp{xx}$$,

Performance metrics of the proposed model
The accuracy, sensitivity, specificity, and F1-score of the proposed model are calculated for each arrhythmia class. The model's performance is evaluated on the MIT-BIH and other pertinent ECG databases. Key results are summarized as follows:

Accuracy: The hybrid CNN-Transformer model achieved an accuracy of 99.32% on the MITDB dataset and 97.15% on combined databases, demonstrating the model's robustness across different ECG sourc...

Access restricted. Please log in or start a trial to view this content.

Discussion

Loading...
$$\rightleftharpoonup{xx}$$ $$\longleftharp{xx}$$, $$\longrightharp{xx}$$,

This study's results indicate that the hybrid CNN-Transformer model proficiently classifies ECG arrhythmias with elevated accuracy, sensitivity, specificity, and F1-score, markedly surpassing conventional CNN-only and CNN-LSTM models. Incorporating Transformer layers has enhanced the model's ability to capture temporal dependencies, a crucial element of ECG analysis. Moreover, continuous wavelet transformations (CWT) provide extensive time-frequency characteristics, enabling the CNN layers to differentiate between subtle...

Access restricted. Please log in or start a trial to view this content.

Disclosures

Loading...
$$\rightleftharpoonup{xx}$$ $$\longleftharp{xx}$$, $$\longrightharp{xx}$$,

The authors have no conflicts of interest to declare.

Acknowledgements

Loading...
$$\rightleftharpoonup{xx}$$ $$\longleftharp{xx}$$, $$\longrightharp{xx}$$,

I wish to thank Dr. Azadeh Amoozegar, Senior Lecturer, INTI International University , for providing online resources to train on the datasets.

Access restricted. Please log in or start a trial to view this content.

Materials

List of materials used in this article
NameCompanyCatalog NumberComments
Computer system(For training) Processor: AMD Ryzen 7 7840HS, CPU RAM:16 GB, GPU RAM:6GBNVIDIA GeForce RTX 3050
imbalanced-learnpython package used for resampling
pytorchPyTorch is a Python package that provides two high-level features:
- Tensor computation (like NumPy) with strong GPU acceleration
- Deep neural networks built on a tape-based autograd system
seabornSeaborn is a Python visualization library based on matplotlib. 
wfdbused for reading ,writing, processing, and plotting physiological signal and annotation data

References

Loading...
$$\rightleftharpoonup{xx}$$ $$\longleftharp{xx}$$, $$\longrightharp{xx}$$,
  1. Jamil, S., Rahman, M. A. Novel deep-learning-based framework for the classification of cardiac arrhythmia. J Imaging. 9 (3), 70(2020).
  2. Reegu, F. A., et al. Blockchain-based framework for interoperable electronic health records for an improved healthcare system. Sustainability. 15 (8), 6337(2023).
  3. Aseeri, A. O. Uncertainty-aware deep learning-based cardiac arrhythmias classification model of electrocardiogram signals. Computers. 11 (6), 82(2021).
  4. Tesfai, H., et al. Lightweight ShuffleNet-based CNN for arrhythmia classification. IEEE Access. 12, 111842-111854 (2022).
  5. Pandey, S. K., Janghel, R. R. Automatic detection of arrhythmia from imbalanced ECG database using CNN model with SMOTE. Australas Phys Eng Sci Med. 42 (4), 1129-1139 (2019).
  6. Hu, R., Chen, J., Zhou, L. Transformer-based deep neural network for arrhythmia detection using continuous ECG signals. Comput Biol Med. 105, 325(2022).
  7. Dang, H., et al. Novel deep arrhythmia-diagnosis network for atrial fibrillation classification using electrocardiogram signals. IEEE Access. 7, 75577-75590 (2019).
  8. Li, J., Zhang, Y., Gao, L., Li, X. Arrhythmia classification using biased dropout and morphology-rhythm feature with incremental broad learning. IEEE Access. 9, 66132-66140 (2021).
  9. Joddoa, A. S. Heart disease prediction system using SMOTE-balanced dataset and decision classifier. AIP Conf. Proc. 2834, 050006(2023).
  10. Li, Y., Qian, R., Li, K. Inter-patient arrhythmia classification with improved deep residual convolutional neural network. Comput Methods Programs Biomed. 214, 106582(2022).
  11. Kiranyaz, S., Ince, T., Gabbouj, M. Real-time patient-specific ECG classification by 1-D convolutional neural networks. IEEE Trans Biomed Eng. 63 (3), 664-675 (2016).
  12. Attention is all you need. Vaswani, A., et al. Proceedings of the 31st International Conference on Neural Information Processing Systems, Long Beach, California, USA, , 6000-6010 (2017).
  13. Rajpurkar, P., et al. Cardiologist-level arrhythmia detection with convolutional neural networks. arXiv. , preprint arXiv:1707.01836 (2017).
  14. Zihlmann, M., Perekrestenko, D., Tschannen, M. Convolutional recurrent neural networks for electrocardiogram classification. arXiv. , preprint arXiv:1710.06122 (2018).
  15. Kim, D., Lee, K. Novel hybrid CNN-transformer model for arrhythmia detection without R-peak identification using Stockwell transform. Sci Rep. 15, 7817(2025).
  16. Diker, A., Aydin, K. Transformer-based attention model for arrhythmia detection using ECG signals. Biomed Signal Process Control. 68, 102679(2021).
  17. Convolutional neural network hyperparameter tuning with Adam optimizer for ECG classification. Sen, S. Y., Ozkurt, N. 2020 Innovations in Intelligent Systems and Applications Conference (ASYU), , 50717(2020).
  18. Smote, N. V. C. Synthetic minority over-sampling technique for handling class imbalance. J Artif Intell Res. 6, 321-357 (2002).
  19. Xia, Y., Wulan, N., Wang, K., Zhang, H. Detecting atrial fibrillation by deep convolutional neural networks. Comput Biol Med. 116, 103345(2020).
  20. Mohonta, S. C., Motin, M. A., Kumar, D. K. Electrocardiogram-based arrhythmia classification using wavelet transform with deep learning model. Sensing Bio-Sensing Res. 37, 100502(2022).
  21. Oh, S. L., Ng, E. Y. K., Tan, R. S., Acharya, U. R. Automated diagnosis of arrhythmia using combination of CNN and LSTM techniques with variable-length heart beats. Comput Biol Med. 102, 278-287 (2018).
  22. Izci, E., Ozdemir, M. A., Degirmenci, M., Akan, A. Cardiac arrhythmia detection from 2D ECG images by using deep learning technique. 2019 Medical Technologies Congress (TIPTEKNO). , 1-4 (2019).
  23. Zheng, Z., Chen, Z., Hu, F., Zhu, J., Tang, Q., Liang, Y. Automatic diagnosis of arrhythmias using a combination of CNN and LSTM technology. Electronics. 9 (1), 121(2020).
  24. Isin, A., Ozdalili, S. Cardiac arrhythmia detection using deep learning. Procedia Comput Sci. 120, 268-275 (2017).
  25. Huang, J., Chen, B., Yao, B., He, W. ECG arrhythmia classification using STFT-based spectrogram and convolutional neural network. IEEE Access. 7, 92871-92880 (2019).
  26. Wang, T., Lu, C., Sun, Y., Yang, M., Liu, C., Ou, C. Automatic ECG classification using continuous wavelet transform and convolutional neural network. Entropy. 23 (1), 119(2021).

Access restricted. Please log in or start a trial to view this content.

Reprints and Permissions

Request permission to reuse the text or figures of this JoVE article

Request Permission

Tags

Arrhythmia ClassificationDeep Learning ModelHeartbeat ClassificationLead I ECGTemporal DependenciesClass BalancingDigital Healthcare
Video Coming Soon

Related Articles