Arrhythmia Classifier Based on Ultra-Lightweight Binary Neural Network
Reasonably and effectively monitoring arrhythmias through ECG signals has
significant implications for human health. With the development of deep
learning, numerous ECG classification algorithms based on deep learning have
emerged. However, most existing algorithms trade off high accuracy for complex
models, resulting in high storage usage and power consumption. This also
inevitably increases the difficulty of implementation on wearable Artificial
Intelligence-of-Things (AIoT) devices with limited resources. In this study, we
proposed a universally applicable ultra-lightweight binary neural network(BNN)
that is capable of 5-class and 17-class arrhythmia classification based on ECG
signals. Our BNN achieves 96.90% (full precision 97.09%) and 97.50% (full
precision 98.00%) accuracy for 5-class and 17-class classification,
respectively, with state-of-the-art storage usage (3.76 KB and 4.45 KB).
Compared to other binarization works, our approach excels in supporting two
multi-classification modes while achieving the smallest known storage space.
Moreover, our model achieves optimal accuracy in 17-class classification and
boasts an elegantly simple network architecture. The algorithm we use is
optimized specifically for hardware implementation. Our research showcases the
potential of lightweight deep learning models in the healthcare industry,
specifically in wearable medical devices, which hold great promise for
improving patient outcomes and quality of life. Code is available on:
https://github.com/xpww/ECG_BNN_Net
Ninghao Pu, Zhongxing Wu, Ao Wang, Hanshi Sun, Zijin Liu, Hao Liu
# Arrhythmia Classifier Based on Ultra-Lightweight Binary Neural Network

###### Abstract

Reasonably and effectively monitoring arrhythmias through ECG signals has significant implications for human health. With the development of deep learning, numerous ECG classification algorithms based on deep learning have emerged. However, most existing algorithms trade off high accuracy for complex models, resulting in high storage usage and power consumption. This also inevitably increases the difficulty of implementation on wearable Artificial Intelligence-of-Things (AIoT) devices with limited resources. In this study, we proposed a universally applicable ultra-lightweight binary neural network(BNN) that is capable of 5-class and 17-class arrhythmia classification based on ECG signals. Our BNN achieves 96.90% (full precision 97.09%) and 97.50% (full precision 98.00%) accuracy for 5-class and 17-class classification, respectively, with state-of-the-art storage usage (3.76 KB and 4.45 KB). Compared to other binarization works, our approach excels in supporting two multi-classification modes while achieving the smallest known storage space. Moreover, our model achieves optimal accuracy in 17-class classification and boasts an elegantly simple network architecture. The algorithm we use is optimized specifically for hardware implementation. Our research showcases the potential of lightweight deep learning models in the healthcare industry, specifically in wearable medical devices, which hold great promise for improving patient outcomes and quality of life. Code is available on: [https://github.com/xpww/ECG_BNN_Net](https://github.com/xpww/ECG_BNN_Net).

 arrhythmia, ECG classification algorithm, Artificial Intelligence-of-Things, ultra-lightweight binary neural network, wearable medical device.

## I Introduction

Cardiovascular diseases (CVDs) are the primary cause of death worldwide, killing more people than any other cause [1]. Arrhythmia, a type of CVDs, is considered to be responsible for most cases of sudden cardiac death that occur each year [2]. Since the global outbreak of the COVID-19 virus in 2019, arrhythmia has become a common manifestation of cardiovascular disease in COVID-19 patients, and a series of clinical case studies of hospitalised patients in China reported palpitation as the initial symptom in 7.3% and arrhythmia in 16.7% [3]. Electrocardiography currently plays an irreplaceable role in the clinical diagnosis of heart disease and can be used to help medical staff identify the specific type of arrhythmia [4].

As wearable AIoT technology continues to advance, there is growing interest in implementing ECG classification algorithms on wearable devices. This would enable real-time monitoring of heart health status without the need to visit a hospital. Low-power and low-computational-cost ECG monitoring algorithms are becoming increasingly popular [6, 7, 8], but most of them still rely on multi-bit fixed-point computation, which is a significant contributor to computational resource consumption and storage costs. Moreover, the majority of studies also design complex algorithms for pre-processing ECG signals.

To address the aforementioned issues and further reduce hardware costs while conserving computational resources, we employed binarization algorithm. This technique converts the weights and activations of the neural network to \(\{+1,-1\}\), which can be stored in hardware as 1-bit \(\{0,1\}\) values, effectively reducing storage costs by 32x. Additionally, the convolution operation is converted to bit-wise XNOR-POPCOUNT, aligning seamlessly with the underlying hardware arithmetic logic [9]. The primary contributions of this article are as follows:

* Firstly, a universal hardware-friendly network has been proposed. By quantizing the activations and weights to \(\{+1,-1\}\), fixing the network structure parameters, operator fusion, and replacing the softmax layer, we have created a binary neural network that is easy to program for hardware and can switch between 5-class and 17-class classification arrhythmia monitoring modes.
* Secondly, our structure eliminates the need for fully connected(FC) layers. FC layers are almost universally employed in neural networks, their removal resulting in a significant reduction in both storage requirements and computational complexity of the model.
* Finally, our 5-class and 17-class arrhythmia classification models achieved remarkable storage compression rates of 29.71x and 29.85x, respectively, with storage usages of 3.76KB and 4.45KB, while maintaining high accuracies of 96.90% and 97.50%, respectively. The storage consumption of the model and the accuracy of the 17-class arrhythmia classification model have reached the state-of-the-art level among known lightweight models.

## II Related Work

Pattern recognition is widely used for monitoring arrhythmias. Prior to classification, signal preprocessing techniques such as filtering and wavelet transformation are applied to extract features [10, 11, 12]. While this method can achieve satisfactory accuracy, implementing feature extraction algorithms such as filtering and wavelet transformation on wearable devices requires additional resources. Additionally, the high dependence of algorithm performance on the extracted features also greatly reduces the model's generalization ability [13, 14].

In recent years, with the development of computing power, the computation cost and power consumption of neural networks have increased rapidly. One of the optimal solutions to address these issues is neural network quantization. The most commonly used method of quantization is converting floating-point numbers to 8-bit fixed-point [15]. Further advances in quantization have resulted in 4-bit quantization, which has only led to approximately 2% loss in accuracy for AlexNet and VGG16 [16]. The most extreme form of quantization is Binary Neural Networks (BNN), which convert both weights and activations to \(\{+1,-1\}\)[9].

With the development of lightweight models and the focus on implementing ECG classification algorithms in wearable devices, an increasing number of lightweight neural networks have emerged. In [17], a hierarchical quantization algorithm based on a greedy algorithm was used to classify ECG with varying bit-widths for different network layers due to their importance. In [18], the activations and weights of the ECG arrhythmia classification CNN were both quantized to 8 bits. Farag et al. [19] proposed a lightweight independent short-time Fourier transform (STFT) convolutional neural network model and quantized it to 8 bits. In [20], an adaptive sensor node architecture was designed, and the proposed CNN model was quantized to 8 bits. Sivapalan et al. [21] converted floating-point operations to 16-bit and 32-bit fixed-point operations for implementation on an embedded platform. Sun el al. [22] used the Adaptive Loss-aware Quantization (ALQ) algorithm to classify 17-class arrhythmias. Janveja et al. [6] proposed a low power work and also quantized the model.

In the existing body of research, there are relatively few studies that focus on quantizing the weights and activations of neural networks to \(\{+1,-1\}\) for ECG classification. Wu et al. [23] designed a full-precision CNN with 9 layers of 1D convolution and 1 fully connected layer, which is then binarized. However, they only utilized F1-score as the evaluation metric for their model, without providing other relevant information such as accuracy, which makes it difficult to compare their results with other models in a comprehensive manner. Wong et al. [24] designed a BNN to classify ECG signals into V-class and non-V-class, implemented on an FPGA. Their model demonstrates exceptional energy efficiency and superior accuracy. However, we observed that the proposed network's fully connected layer constitutes more than 97% of the total parameters, and dividing ECG data into only two categories falls short of meeting the needs of both doctors and patients. Wang et al. [25] proposed a 15-layer CNN to classify ECG and binarized the model. The accuracy of their model requires improvement, and optimization of the batch normalization layer has not been taken into account. During batch normalization computation, floating-point operations are still necessary. Additionally, the parameters in the CNN architecture are not uniform, which poses considerable challenges for the hardware design of BNN. Yun et al. [26] binarized the CNN+LSTM model used for classifying psychological health based on ECG, greatly reducing inference latency and energy consumption. Their model achieves 87% accuracy with a memory footprint of 47KB, indicating ample room for further improvement.

## III Methodology

This section will introduce our network architecture and the binarization method. Our approach has systematically addressed the deficiencies in the relevant literature.

### _Convolutional Neural Network (CNN) Architecture_

To simplify software and hardware programming and facilitate future hardware design, the main structure of the CNN is composed of numerous blocks with standardized parameters such as convolution kernel size, pooling size, pooling stride, etc. To minimize the number of weights in the CNN, we creatively removed the fully connected layers, which are commonly seen in CNN classification networks. This also eliminates the need to separately consider Processing Elements (PE) for FC layers. To reduce the dimensionality of the convolutional output for subsequent classification, we implemented GSP [27], which eliminates the division operation in GAP and renders the entire CNN architecture almost free of division operations. To avoid the exponential operation caused by using the softmax function for classification, we replaced it with a comparator, with the index of the maximum value serving as the classification result.

By setting the number of channels in the output of the last basic block to the desired number of classes and combining it with GSP's dimension reduction, we can simply compare the output of the last dimension of GSP to obtain the specific type of arrhythmia.

Through the aforementioned efforts, we have addressed the following issues in works [23, 24, 25, 26]: insufficient number of
categories and limited model flexibility, substantial parameter consumption resulting from the presence of fully connected layers, and hardware design difficulties stemming from the intricate model architecture. Fig. 1 depicts our baseline CNN structure.

### _Method of Binarization_

Floating-point operations are costly and not supported by most embedded hardware. Binary neural network, on the other hand, utilizes XNOR and POPCOUNT operations to convert the floating-point MAC operations of convolutional and fully connected layers into bit-wise operations. This maximizes the reduction in computational complexity and memory usage of weights. We use (1) to transform the weights and activation values into \(\{+1,-1\}\).

\[x^{b}=Sign(x)=\left\{\begin{aligned} &+1,& x\geq 0\\ &-1,& otherwise\end{aligned}\right. \tag{1}\]

During network training, network are performed using the transformed \(\{+1,-1\}\) values. However, when updating weights using loss values, the floating-point values of the weights prior to being converted by the Sign function will be updated. The gradient of the Sign function is infinite at 0 and 0 elsewhere, making it impossible to perform gradient calculations based on the chain rule. We adopt Ding et al.'s Information-Enhanced Estimator(IEE) [32] to approximate the derivative of the Sign function.

### _Optimizing Batch Normalization Through Operator Fusion_

The aforementioned methodology enabled the successful training of BNN. However, conventional network architectures entail additional operations such as activation functions, batch normalization, and pooling. Prior works [23, 25, 26] have utilized batch normalization, but without any additional processing optimizations, this operation will introduce additional floating-point computations, consume hardware resources, and increase computational latency.

Note that PReLU, batch normalization, and Sign function can be mathematically fused into a threshold function, we introduce this optimization method in the ECG classification network to address the aforementioned issue. We use PReLU instead of the more common ReLU because it can improve the representation of BNN [31]. Below are the specific computational procedures.

\[f(x)=\begin{cases}x&\text{if }x\geq 0\\ ax&\text{otherwise}\end{cases} \tag{2}\]

\[y=\frac{x-\mu}{\sqrt{\sigma^{2}+\varepsilon}}\gamma+\beta \tag{3}\]

Equation (2) and (3) correspond to PReLU and batch normalization respectively. If batch normalization is placed after PReLU in the network, we can merge them into (4).

\[y=\begin{cases}kx+b&\text{if }x\geq 0\\ akx+b&\text{otherwise}\end{cases} \tag{4}\]

Here are the values for k and b:

\[k=\frac{\gamma}{\sqrt{\sigma^{2}+\varepsilon}} \tag{5}\] \[b=\beta-\frac{\mu\gamma}{\sqrt{\sigma^{2}+\varepsilon}} \tag{6}\]

If the function following the batch normalization is Sign function, then (4) can be further merged with sign to obtain (7), which \(\delta_{+}\) is \(\frac{-b}{k}\) and \(\delta_{-}\) is \(\frac{-b}{ak}\).

\[y=\begin{cases}\text{sign}_{\delta_{+}}(x)&\text{if }x\geq 0\\ \text{sign}_{\delta_{-}}(x)&\text{otherwise}\end{cases} \tag{7}\]

Therefore, the order of operators within the basic block can be determined as **Conv, MaxPool, PReLU and BatchNorm**. The reason why the Sign function was not listed is because it is integrated within the Convolution layer of the next basic block. Specifically, the binarization of the previous layer's activations and the convolution layer's own weights are both carried out within the convolution layer itself.

## IV Experiment

In this section, we first introduce the dataset and evaluation metrics used in the experiment. Then, we present the specific parameters of the CNN network structure. Next, we present the details and data of the experiment. Finally, we compare our experimental results with the state-of-the-art lightweight models developed in recent years.

### _Dateset_

The network in this experiment is used for both 17-classification and 5-classification of ECG data. All data used in the experiment are sourced from the MIT-BIH Arrhythmia database of PhysioNet [28].

The 17-class dataset is the same as Plawiak et al. [29], which includes normal sinus rhythm, pacemaker rhythm, and 15 types of heart failure (with at least 10 signal segments collected for each type). Plawiak et al. randomly selected 1000 10-second ECG signal segments for analysis.

The 5-class dataset contains 7740 10-second segments, and the classification follows the AAMI EC57 standard [30], dividing all data into five categories: ventricular ectopic beat (V), normal or bundle branch block beat (N), supraventricular ectopic beat (S), fusion beat (F), and unknown beat (Q).

Regardless of whether it is the 5-class or 17-class data, we used a signal from one lead (MLII), recorded at 360HZ with 3600 samples in 10 seconds.

Fig. 1: Baseline convolutional neural network (CNN) structure
### _Evaluation Metrics_

The utilization of diverse and rational evaluation metrics allows for a comprehensive assessment of network performance from various perspectives. To holistically evaluate model performance, we employ a variety of indicators including accuracy (ACC), sensitivity (SEN), specificity (SPE), precision (PRE), and F1-score.

### _Specific Parameters of the Network Structure_

After conducting extensive experiments at full precision, we have determined that the optimal number of blocks for CNN is 6. The structures of the 5-class model and the 17-class classification model are almost identical, with the only difference being the output channels of block 6, which correspond to the number of categories for the corresponding arrhythmia. Therefore, by modifying only the final output channel and replacing the network weights, the classifier can switch between 5-class and 17-class classification, endowing the model with universality. Table I presents the specific structural parameters of the proposed CNN. Based on this structure, our model can achieve an accuracy of 97.09% for 5-class classification and 98.00% for 17-class classification at full precision.

### _Experimentation of Binarization Methods_

For the 5-class classification of arrhythmia, our experimental setup utilized a batch size of 512 and a learning rate of 0.02. For the 17-class classification of arrhythmia, we employed a batch size of 64 and a learning rate of 0.002. All datasets were partitioned such that 80% was allocated to the training set and 20% to the testing set, and the models were trained for 1000 epochs.

We tried several popular BNN optimization methods and found that IEE [32] achieved the best accuracy of 96.90% in the 5-class classification model, which is only 0.19% lower than the full precision model. As for the 17-class classification BNN model of arrhythmia, the accuracy always fluctuates between 96.50% and 97.50%, and no method can further improve its accuracy. We refer to the models with accuracies of **96.90%** and **97.50%** as the BP(Better Performance) models.

At the end, we also quantize the ECG signal to \(\{+1,-1\}\) and treat it as an "extreme denoising" technique. Under this circumstance, the first convolution layer can be replaced with XNOR+POPCOUNT. We refer to the model processed in this way as an LP (Low Power) model. Experimental results demonstrate that even under such stringent conditions, our ECG 5-class and 17-class classification models can achieve accuracies of **91.50%** and **92.00%**, respectively.

The storage space required for the 5-class classification model is **3.76KB**, achieving a compression ratio of 29.71x. The corresponding arrhythmia 17-class classification model requires a total storage of **4.45KB**. This results in a memory compression of 29.85x.

Due to space limitations, we only provide the confusion matrix of the BP model in Fig. 2 and Fig. 3.

Fig. 3: Confusion matrix for the 17-class ECG classification model

Fig. 2: Confusion matrix for the 5-class ECG classification model
### _Comparison with State-of-the-Art Lightweight ECG Classification Models in Recent Years_

Table II compares the performance of the proposed model with state-of-the-art lightweight models from various perspectives.We have noticed a dearth of research on 17-class lightweight models for ECG. In order to ensure objective and fair comparison results, we have included Tuncer's full precision work [33] in Table II.

Methodologically, our 1D model is easier to implement in hardware compared to Scrugi et al.'s [20] and Wong et al.'s [24] 2D models.

In terms of bit-width, our model employs 1-bit values for both the activations and weights. This results in a storage footprint that is 8 times smaller than that of the model in work [20], and 16-32 times smaller than that of the model in reference [21] when the number of weights are same.

Regarding the number of distinguishable arrhythmia classes, our model is capable of recognizing 5 and 17 categories, whereas the works [21] and [24] are only able to recognize 2 categories.

In terms of evaluation metrics, we used 5 evaluation metrics, which made up for the shortcomings of insufficient evaluation metrics in work [23]. And the 5-class classification model has the highest specificity of 98.5% among 1-bit models, while the accuracy, specificity, and precision of 17-class classification model are among the highest in all 17-class models, with values of 97.5%, 99.8%, and 96.4%, respectively.

In terms of data processing, our work only standardizes the data. This avoids the hardware considerations brought about by the complex data processing methods of works [20, 21, 23, 33]. Compared to works [22] and [25], which normalize the data, our accuracy is also 1.66% and 1.23% higher than theirs respectively.

Finally, our model's storage occupation is at the state-of-the-art level, with only 3.76 KB of storage required for the 5-class classification model and 4.45 KB for the 17-class classification model.

### _Discussion_

In summary, by binarizing the weights and activations and fusing the operators into a threshold function, our arrhythmia 5-class classification model and 17-class classification model achieved a storage compression of 29.71 and 29.85 times, respectively, with only a slight decrease in accuracy of 0.19% and 0.50%. Even after transforming all the data in the dataset to \(\{+1,-1\}\), our network still achieves an accuracy of no less than 91.50%, which demonstrates its excellent anti-interference capability. Removing the fully connected layer and replacing the softmax layer further simplified the model's complexity and computational burden. While achieving the most advanced storage usage known, the accuracy was superior or equal to that of other models under the same classification standards. Additionally, the universal structure enables wearable devices to switch between 5-class and 17-class monitoring modes.

Due to the limitation of weights and activations to \(\{+1,-1\}\) in the network, our BNN inevitably shows some gaps in evaluation metrics such as SEN and F1-score when compared to the full-precision network. In order to narrow the gap between the BNN and full-precision network in all aspects while maintaining the hardware friendliness of the BNN, this remains a challenging problem to be tackled in future research.

## V Conclusion

In this study, we propose a universal ultra lightweight BNN for 5-class and 17-class classification of long-term ECG signals. Computationally, all convolution operations in the model can be performed using XNOR gates, and the remaining main operations are mathematically fused into a threshold function, which almost completely eliminates multi-bit MAC and further reduces hardware energy consumption. Structurally, the regular structure facilitates future hardware implementation. In terms of model size, the memory consumed by storing the model is at the lowest known level. In the future, we will focus on the FPGA implementation of the BNN model
to test its hardware acceleration effect and other performance parameters in real-life situations.
