# ME-YOLO: Improved YOLOv5 for Detecting Medical Personal Protective Equipment
## 3 Proposed Method
### Improvement of the Loss Function  
As the loss function can determine the difference between the prediction of the model and the actual object, it is crucial in the training process. Choosing a suitable loss function can speed up the convergence of the model and help to obtain a better model. The principle of the traditional IoU loss [29] function is 1 minus the ratio of the intersection of the prediction box and the ground-truth box to the concatenation of the prediction box and the ground-truth box. The traditional IoU loss function is formulated as follows:  
\[\rm{IoU}=\left|\begin{array}{cc}B\ \cap\ B_{i}\\ B\ \cup\ B_{i}\end{array}\right| \tag{4}\]  
Figure 4: E-MHSA module. While we use 4 heads, we do not show them on the figure for simplicity.  
\[L_{\text{IoU}}=1-\left|\begin{array}{ccc}B&\cap&B_{i}\\ \hline B&\cup&B_{i}\\ \end{array}\right| \tag{5}\]  
where, \(B\) is the area of the prediction box and \(B_{i}\) is the area of the ground-truth box. The IoU loss function has the advantages of symmetry, non-negativity, and homogeneity, but when the ground-truth box and the prediction box are not intersected, the IoU is 0 and the model cannot continue to learn.  
To avoid the above issue, YOLOv5 is based on a CIoU loss for border regression. The CIoU loss function is formulated, as follows:  
\[L_{\text{CIoU}}=1-\text{IoU}+\frac{\rho^{2}\big{(}b,b^{gt}\big{)}}{c^{2}}+\alpha v \tag{6}\]  
where \(b\) is the centroid of the prediction box, \(b^{gt}\) is the centroid of the ground-truth box, \(\rho\) denotes the Euclidean distance between them, \(c\) is the length of the diagonal of the smallest outer rectangle formed by the intersection of the prediction box and the ground-truth box. \(\alpha\) is a learnable weight parameter, \(v\) is the aspect ratio, and \(\alpha\) and \(v\) are defined, as follows.  
\[\alpha=\frac{v}{(1-\text{IoU})+v} \tag{7}\]  
\[v=\frac{4}{\pi^{2}}(\arctan\frac{w^{gt}}{h^{gt}}-\arctan\frac{w}{h})^{2} \tag{8}\]  
Although the CIoU loss function introduces the aspect ratio of the border as a penalty term in the loss function, which continuously makes the prediction box close to the ground-truth box through iteration, it is too complicated to measure the aspect ratio, and the following two issues reduce the speed of convergence of the regression of the prediction box: (1) The difference in aspect ratio cannot fully reflect the true difference in width and height. When \(h=h^{gt}\) and \(w=w^{gt}\), the penalty term no longer works. (2) When the aspect ratio of the prediction box and the ground-truth box is linear, the width and height of the prediction box cannot change at the same time when regressing.  
To solve the above problems, in this paper, we introduce the EIoU loss function [30], which takes into account the centroid distance and the aspect ratio. As shown in Equation (9), the EIoU loss function consists of three components: loss of overlapping area, loss of distance to the central point and loss of aspect ratio. The first two losses continue to follow the CIoU method, while the addition of the aspect ratio loss solves the problem of the CIoU loss where the length and width cannot be increased or decreased at the same time. Compared with the CIoU loss function, the EIoU loss function converges faster and with more accurate regression.  
\[L_{\text{EIoU}}=L_{\text{IoU}}+L_{dis}+L_{asp}=1-\text{IoU}+\frac{\rho^{2} \big{(}b,b^{gt}\big{)}}{c^{2}}+\frac{\rho^{2}\big{(}w,w^{gt}\big{)}}{c_{w}^{2 }}+\frac{\rho^{2}\big{(}h,h^{gt}\big{)}}{c_{h}^{2}} \tag{9}\]