# ECGYOLO: Mask Detection Algorithm
## 3 YOLOv7 tiny Model Improvements
### Bounding Box Loss Function Improvement  
The IoU (intersection over union) is used to measure the extent of overlay between the real frame and the prediction frame in the target detection task [30]. However, IoU has a fatal flaw that the backpropagation gradient vanishes when the bounding box A and bounding box B do not overlap, so many IoU-based GIoU, DIoU, CIoU, SIoU, WIoU and EIoU appear. The loss function used in YOLOv7 tiny is CIoU, while EIoU was chosen in ECG, a more important reason being that EIoU allows for better regression of the bounding box. The GIoU (generalized IoU) [31] is a loss calculation method for bounding box prediction, which originated and extended from the IoU metric. In target detection tasks, the comparison between the predicted and actual labelled bounding boxes and the corresponding loss value calculation are crucial. Compared with IoU, GIoU considers the non-overlapping region of the bounding box and can exactly mirror the way of overlap between objects A and B. Therefore, compared to the traditional IoU indicator, GIoU is able to assess the overlap between bounding box B and bounding box A more accurately. Figure 6 shows that at IoU values all equal to 0.33, positioning from left to right becomes less and less effective, and the value of GIoU decreases in turn. B and A express two bounding boxes, and C represents the bounding box that can contain the area of bounding box A and bounding box B. This formula considers the effect of the area difference set of B and A by the ratio of the area intersection of B and A to the area union of B and A and subtracts it from the original IoU value to obtain a more accurate assessment of the degree of overlap. The GIoU metric is an important metric used in object detection tasks, which can help improve the performance of the simulator and enhance the precision of the detection outcome [32]. The IoU is shown in Equation (4). The GIoU is shown in Equation (5). \[\text{IoU}=\frac{\left|\text{A}\cap\text{B}\right|}{\left|\text{A}\cup\text{ B}\right|}, \tag{4}\]  
Figure 4: YOLOv7 tiny ELAN. Figure 5: ELAN. The value range of GIoU is \(-1\)-\(1\), which can be obtained from Equation (6), and its loss function range is \(0\)-\(2\). \[\mathrm{L_{GIoU}}=1-\mathrm{GIoU}(0\leq\mathrm{L_{GIoU}}\leq 2), \tag{6}\]  
The GIoU solves the problem of loss of \(0\) when bounding box A and bounding box B do not overlap to a certain extent, but there is also the problem that GIoU degenerates into IoU when real box and detection box are included, and the convergence of the two boxes is slow in the horizontal and vertical directions. The CIoU is based on GIoU, which further considers the geometric factor of the aspect ratio of the bounding box, thus making the regression of the bounding box more stable and exact, where \(\beta\) and \(\
u\) are the corresponding weights and aspect ratio coefficients, respectively. Specifically, in the calculation, \(\beta\) is used to balance the effect between the central point length and the length-width ratio, while \(\
u\) is used as a parameter to survey the uniformity of the length-width ratio. The formula of CIoU is shown in Equation (7). \[\mathrm{CIoU}=\mathrm{IoU}-\frac{\rho^{2}(\mathrm{A},\mathrm{B})}{c^{2}}- \beta\
u, \tag{7}\]  
From GIoU to CIoU, all three loss functions have excellent performance in dealing with the bounding box of the inclusion relation, unlike IoU which degenerates. However, when the central points of B and A overlap, CIoU degenerate to IoU and do not regress well on the bounding box. Therefore, the EIoU (efficient IoU) loss function comes into being, which separates the impact factors of A and B on the basis of CIoU and calculates the width and height of A and B. The EIoU equation is as follows, where \(\mathrm{h^{c}}\) and \(\mathrm{w^{c}}\) are the width and height of the bounding box C. The EIoU loss equation is as Equations (8) and (9).