# Efficient Classification of ECG Images Using a Lightweight CNN with Attention Module and IoT
## 3 Proposed Method
### Classification of ECG Images
#### 3.2.2 Lightweight CNN  
The proposed lightweight CNN consists of several components, as depicted in Figure 7.  
The initial component of the model is responsible for processing the input ECG data and extracting appropriate features from it. This lightweight CNN usually includes multiple convolutional layers, as well as pooling layers and activation functions. The purpose of the four-layer CNN is to effectively capture important patterns from the ECG data. The proposed lightweight CNN is employed to automatically extract ECG features. This CNN focuses on feature extraction and leaves feature optimization to the attention module, which is the method's main focus. Additionally, the inclusion of a BatchNormalization (BN) layer and a ReLU layer in the CNN helps to improve the training process and avoid overfitting. The attention module is a crucial component of the proposed method that enhances the characteristics appropriate to the identification category. This module is based on two primary strategies. The contextual encoding layer: This method enables the model to discover contextual connections between various input data [38], which is helpful for identifying dependencies in sequences. By understanding contextual relationships, the model can better capture dependencies and patterns in the ECG data. The depthwise separable convolution: This method is used to reduce the model's number of parameters. A lighter model with fewer parameters is produced via depthwise separable convolution, which separates the spatial and channel-wise processes [39]. This makes the model more effective for use in practical applications. Flattening and SoftMax Classifier  
The improved features are flattened into a one-dimensional vector after going through the attention module, turning the 2D or 3D tensor into a linear array. The SoftMax classifier is then fed this one-dimensional vector. A common option for multi-class classification tasks is the SoftMax classifier, which determines the probability of each identity category based on the ECG data retrieved by the preceding layers. The input ECG sequence's anticipated identity is the class with the highest probability. When an ECG image is input, the model goes through three stages of the lightweight CNN model to produce a label for the image based on the identified features. The model was trained using specific parameters listed in Table 3. The loss function employed for the model is categorical cross entropy, a common choice for multiclass classification tasks. Figure 7: Lightweight CNN. The rate of learning is set at 0.001, determining the step size of the optimizer during training. The model employs the Adam optimizer, a widely used optimization algorithm that updates model weights based on the gradient of the loss function. The training process spans 70 epochs, indicating that the complete dataset passes through the model 70 times. The dataset is divided into training and testing sets of 80% and 20%, respectively. The batch size used is 16, meaning that 16 samples are utilized to adjust model weights in each iteration of training. Finally, the activation function in the output layer is SoftMax, a standard choice for obtaining the probability distribution of expected classes in multiclass classification tasks. The pseudo-code of the proposed system, focusing on the IoT-based ECG framework and the classification of ECG images, is described below:  
1. IoT-based ECG Framework 1. Initialization: 1. Initialize ECG sensing system (wearable sensors) for continuous monitoring. 2. Initialize IoT cloud for data collection, transmission, analysis, and disease alert. 2. Data Collection and Transmission: 1. Read ECG data from wearable sensors continuously. 2. Send the collected ECG data to the IoT cloud using DSR or REL routing protocol. 3. ECG Investigation: 1. IoT cloud receives the ECG data and stores them. 2. IoT cloud performs data analysis to achieve essential features using the ECG signals. 4. Disease Alert: 1. IoT cloud processes the extracted features and detects potential heart diseases. 2.