# Mask-Wearing Detection in Complex Environments Based on Improved YOLOv7
## 3 Improved YOLOv7
### Improved Bounding Box Regression Loss Function  
The bounding box regression loss function is a crucial component of the object detection loss function, playing a pivotal role in the detection accuracy of object detection models. The YOLOX model employs the IoU [28] as the bounding box regression loss function, which reflects the quality of predictions by considering the intersection over union between the predicted boxes and ground truth boxes. However, calculation methods based on geometric properties like the IoU assume that the annotated ground truth boxes in the dataset are all high-quality samples, reinforcing the fitting between the predicted and ground truth boxes. In reality, annotated datasets often contain a significant number of low-quality samples. Strengthening regression on these low-quality samples may hinder the improvement of detection model performance. Therefore, this paper proposes an improvement to the bounding box regression loss function using the WIoU [29]. It is a dynamic and non-monotonic focusing mechanism that evaluates the quality of all anchor boxes using \"outlierness\". Through a gradient gain allocation strategy, it focuses on the anchor boxes of average quality, reducing the adverse gradients generated by extremely high- or low-quality samples. This improvement addresses the deficiencies of the IoU loss function in calculating the regression for low-quality samples, thereby reducing the loss function value and accelerating the model convergence speed.  
The IoU schematic diagram for the WIoU bounding box loss function is illustrated in Figure 8. The predicted box is represented by \(\overset{\rightarrow}{B}=[x\,y\,w\,h]\), while the ground truth box is represented by \(\overset{\rightarrow}{B}_{gt}=\left[x_{gt}\,y_{gt}\,w_{gt}\,h_{gt}\right]\).  
The _IoU_ is used to measure the degree of overlap between the predicted box and the ground truth box and is defined as follows:  
\[L_{IoU}=1-IoU=1-\frac{W_{i}H_{i}}{wh+w_{gt}h_{gt}-W_{i}H_{i}} \tag{1}\]  
The penalty term for constructing the _WIoU_ is defined as the center point distance ratio, expressed by  
\[R_{WIoU}=\exp(\frac{(x-x_{gt})^{2}+(y-y_{gt})^{2}}{W_{g}^{2}+H_{g}^{2}}) \tag{2}\]  
_WIoUv1_ is defined as follows:  
\[L_{WIoUv1}=R_{WIoU}L_{IoU} \tag{3}\]The outlierness, which describes the quality of the anchor boxes, is defined by  
\[\beta=\frac{L_{IoU}}{L_{IoU}}\in[0,+\infty] \tag{4}\]  
where the term \(\overline{L_{IoU}}\) represents the exponentially weighted moving average with momentum \(m\), and it is utilized to construct a non-monotonic focusing coefficient using \(\beta\):  
\[\text{r}=\frac{\beta}{\delta\alpha^{\beta-\delta}} \tag{5}\]  
where \(\alpha,\delta\) represents a manually set parameter, which is experimentally validated to perform well in the YOLOv7 model when its values are set to 1.9 and 3, respectively. Therefore, \(L_{WIoUv3}\) is defined as follows:  
\[L_{WIoUv3}=rL_{WIoUv1} \tag{6}\]  
When the outlierness is relatively small, indicating higher anchor box quality, a smaller gradient gain should be assigned. This is gain is assigned to focus the bounding box regression on regular-quality anchor boxes. Conversely, when the outlierness is larger, indicating lower anchor box quality, a smaller gradient gain should also be assigned. This helps effectively prevent harmful gradients caused by low-quality anchor boxes. As the criteria for dividing anchor box quality dynamically change with \(\overline{L_{IoU}}\), _WIoUv3_ can adapt its gradient gain allocation strategy at any moment to best suit the current situation.