# P-Wave Detection Using a Fully Convolutional Neural Network in Electrocardiogram Images
## 2 Materials and Methods
### Proposed P-Wave Image-Based Detection Algorithm  
In this study, a knowledge-based P-wave detection algorithm based on fully convolutional networks is proposed and evaluated. This algorithm is inspired by the framework proposed in [49], which is mainly used for the segmentation of biomedical images, such as brain lesion segmentation [50], tumor segmentation [51], and many other segmentation applications. We build upon this approach to automatically detect P-waves from ECG images, taking into consideration that an ECG is always printed on a grid paper. The grid background can be removed by the algorithm proposed in [2], as a pre-processing stage. Our proposed algorithm is then used to detect P-waves in the image-based ECG.  
Segmentation via U-Net Architecture  
The U-Net is a fully convolutional network that was developed in [49] for biomedical image segmentation. The goal of the U-Net is to produce a semantic segmentation, with an output that is the same size as the original input image, but in which each pixel in the image is colored one of X colors, where X represents the number of classes to be segmented.  
The U-Net architecture, as shown in Figure 3, consists of two paths. The first path is the encoder path, which is used to capture the image's contents. The encoder is just traditional blocks of convolutional and maximum pooling layers. The convolutional operation has two inputs: the input image of size n*n* number of channels, where n*n is the image pixel size and there is a set of '\(K\)' filters (also called kernels or feature extractors), each one of size f x f x number of channels, where f is  
Figure 2: Samples of manually annotated P-waves in the MIT-BIH AF database, where the P-wave is absent in case of atrial fibrillation, (**a**) 07162 record; (**b**) 04126 record; (**c**) 06426 record; and (**d**) 06995 record.  
typically 3 [52]. The output of a convolutional operation is also a 3D output image of size (\(n_{out}\times n_{out}\times k\)). The relationship between \(n_{in}\) and \(n_{out}\) is given by [49]  
\[n_{out}=\frac{n_{in}+2p-k}{s} \tag{1}\]  
where \(n_{in}\) is the number of input features, \(n_{out}\) is the number of output features, \(k\) is the convolution kernel size, \(p\) is the convolution padding size, and \(s\) is the convolution stride size.  
The maximum pooling layer is then used to retain the maximum value of a pre-defined area and stores its position in a switch variable. This creates a pooling map that reduces the feature map size, which results in fewer network parameters.  
The filter and stride sizes are two important parameters in the maximum pooling operation. The idea is to retain only the important features of the maximum valued pixels from each region and neglect the rest. Both the convolution operation and the pooling operation result in down-sampled images. When applying the convolution operation again, the filters in the next layer will be able to show the larger context. As the number of filters used gradually increases, more complex features from the image can be extracted. By down-sampling, the model better understands what is present in the image, but it loses the information of where it is present.  
The second path is the decoder, which is used to obtain precise localization using transposed convolutions. Transposed convolution (deconvolution) is a technique to perform the up-sampling of an image with learnable parameters. Transposed convolution is exactly the opposite process of a normal convolution, where the input volume is a low-resolution image and the output volume is a high-resolution image with location information. It is followed by the merged layer, where the output of the transposed convolution layers is merged with the feature maps from the encoder at the same level to obtain more precise locations.