# Real-Time Monitoring Method for Traffic Surveillance Scenarios Based on Enhanced YOLOv7
## 3 ETS-YOLOv7 Algorithm
### Loss Function Optimization  
By default, YOLOv7 uses CIoU as the loss function, which comprehensively considers the Intersection over Union (IoU), the distance between the center points, and the consistency of the aspect ratio. However, it does not consider the balance between hard and easy samples, which can introduce significant aspect ratio penalties in traffic monitoring systems. Therefore, we introduce the WIoUv3 loss function with a dynamic non-monotonic FM. This loss function employs the outlier degree, rather than IoU, to assess the quality of anchor boxes and introduces a non-monotonic gradient gain allocation strategy. This reduces the impact of both hard and easy samples on the results, giving more attention to samples of moderate difficulty. Intersection over union (IoU) is employed to quantify the degree of overlap between the anchor box and the ground truth box in object detection tasks. It eliminates the influence of the bounding box size in a proportional way, allowing the model to effectively balance the learning of large and small objects when using IoU as the bounding box regression loss. The \(\mathcal{L}_{\text{IoU}}\) formula is as follows:  
\[\mathcal{L}_{\text{IoU}}=1-\text{IoU}. \tag{3}\]  
In order to reduce the penalty introduced by the geometric parameter metrics when the anchor box and the ground truth box overlap significantly, [PERSON] et al. (2020) constructed a distance attention mechanism based on the distance between the center coordinates, forming WIoUv1.  
\[\mathcal{L}_{\text{WIoUv1}}=\text{R}_{\text{WIoU}}\mathcal{L}_{\text{IoU}}, \tag{4}\]  
\[\text{R}_{\text{WIoU}}=\exp(\frac{\left(\text{x}-\text{x}_{\text{gt}}\right)^ {2}+\left(\text{y}-\text{y}_{\text{gt}}\right)^{2}}{\left(\text{W}_{\text{g} }^{2}+\text{H}_{\text{g}}^{2}\right)^{*}}). \tag{5}\]  
where \(\text{W}_{\text{g}}\) and \(\text{H}_{\text{g}}\) represent the maximum width and height of the combined area of the anchor box and the ground truth box. To prevent \(\text{W}_{\text{g}}\) and \(\text{H}_{\text{g}}\) in the denominator from producing negative gradients and hindering convergence during gradient computation,  
Figure 2: Convolutional lock attention module (CBAM). (**a**) Channel attention module; (**b**) spatial attention module.  
they are treated as constants and do not participate in backpropagation (indicated by the superscript * for this operation), x and y denote the center coordinates of the anchor box, while xgt and ygt represent the center coordinates of the ground truth box, and exp(\(\cdot\)) denotes the exponential operation.  
The outlier degree \(\beta\) of the anchor box is given by the ratio of \(\mathcal{L}_{\text{IoU}}\) to \(\overline{\mathcal{L}_{\text{IoU}}}\). An excessively large or small outlier degree indicates low-quality or high-quality anchor boxes, respectively. Smaller gradient gains are assigned to these anchor boxes to prevent them from having a significant negative effect on the gradients.  
\[\beta=\frac{\mathcal{L}_{\text{IoU}}^{*}}{\mathcal{L}_{\text{IoU}}}, \tag{6}\]  
\[\text{r}=\frac{\beta}{\delta\alpha^{\beta-\delta}}. \tag{7}\]  
where \(\overline{\mathcal{L}_{\text{IoU}}}\) is the moving average with momentum, r is the non-monotonic focusing coefficient, and \(\alpha\) and \(\delta\) are hyperparameters. Applying this to WIoUv1 yields WIoUv3:  
\[\mathcal{L}_{\text{WIoUv3}}=r\mathcal{L}_{\text{WIoUv1}}. \tag{8}\]  
As a non-monotonic focusing coefficient, r can adaptively adjust the gradient gain strategy while \(\overline{\mathcal{L}_{\text{IoU}}}\) changes dynamically. Therefore, the criterion for judging the quality of the anchor boxes also changes dynamically. This allows WIoUv3 to select the most appropriate gradient gain strategy in different situations.