# An Improved Algorithm for Detecting Pneumonia Based on YOLOv3
## 2 Materials and Methods
### Methods
#### 2.2.1 PYolo Detection Process  
As shown in Figure 3a, in the location pre-processing, PYolo uses double K-means to produce the anchor box of a lesion. As shown in Figure 3b, PYolo uses DarkNet53 to extract features, uses MaskFPN to fuse features of different levels, and uses a multi-branch convolution module to obtain multi-perception field information. Unlike Yolov3, PYolo only detects the features of the module output. The input image size of DarkNet53 is \(416\times 416\) pixels, and the output features are [F1, F2, F3] with the sizes of \(\{13\times 13,\,26\times 26,\,52\times 52\}\), respectively. In the experiment, the input image was scaled to \(416\times 416\) pixels in the pre-processing stage. The difference between MaskFPN and FPN is that MaskFPN uses the information of high-level feature as prior knowledge to generate a weight map, and then multiplies the weight map with low-level features linearly to suppress the output of inaccurate semantic information of low-level features. By contrast, FPN directly combines high-level features and low-level features, directly overcoming the problem of inaccurate semantic information in low-level features. In an object detection algorithm, the ratios of positive and negative samples are critical to the performance of the algorithm. As shown in Figure 4, like Yolov3, PYolo corresponds to the feature points by dividing the image into grid cells. In the training phase, the real bounding box is mapped to the corresponding coordinates on the feature map by dividing by the stride; in the detection phase, the predicted bounding box on the feature map is mapped to the corresponding coordinates on the original image by multiplying the stride. The dimensions of the output features of PYolo are [S, S, A * (B + Conf + Cls)]. S \(\times\) S is the number of grid cells; B is the predicted bounding box; Conf is the confidence level of the output object; Cls is the class of the dataset; and A is the number of scales for each anchor. With respect to the selection of positive and negative samples, anchors with the Intersection over Union (IOU) with the ground-truth bounding boxes were used for evaluation. Anchors that have IOU with any ground-truth box greater than 0.5 were included as training samples. The center points of the anchor and ground-truth bounding boxes that fall on the same grid were designated as positive samples, and other anchors were designated as negative samples. There is still a problem of imbalance between positive and negative samples in the screened sample set. To overcome the problem of imbalance between positive and negative samples [21], a hyper-parameter \(\lambda\) = 200 is introduced in the loss function to strengthen the learning intensity for negative samples and accelerate the speed of the convergence of the model. The localization loss function is different from the function in Yolov3. Smooth L1 loss was adopted as the localization loss function as it has a higher level of smoothness compared to others. The loss functions of the model are as follows:  
\[L_{loc}=\sum_{i\in Pos}^{N}\sum_{m\in[x,y,w,h]}\text{smooth}_{\text{L1}}(g_{m }-p_{m}) \tag{1}\]  
\[L_{cls}=-\sum_{i\in Pos}C_{i}\log(X_{i}) \tag{2}\]  
\[L_{pos}=-\sum_{i\in Pos}M_{i}\log(Y_{i}) \tag{3}\]  
\[L_{neg}=-\sum_{i\in Neg}M_{i}\log(Y_{i}) \tag{4}\]  
\[L_{total}=L_{loc}+L_{cls}+L_{pos}+\lambda L_{neg} \tag{5}\]  
where \(L_{loc}\), \(L_{cls}\), \(L_{pos}\), and \(L_{neg}\) represent localization loss, classification loss, positive sample loss, and negative sample loss, respectively, and \(g\), \(p\), \(C\), \(X\), \(M\), and \(Y\) refer to the actual coordinates, predicted coordinates, probability of the actual class, and probability of the predicted class, actual set of positive  
Figure 3: Structure of Pneumonia Yolo (PYolo). The highlighted red text indicates improvements based on Yolov3 in this study. In the multi-branch convolution module, 1, 3, 6, and 12 represent different dilation rates: (**a**) location pre-processing; (**b**) detection process.