# The Bearing Surface Defect Detection Method Combining Magnetic Particle Testing and Deep Learning
## 4 Principles and Optimization of the YOLOv5 Model
### WIoU Loss Function  
The performance of object detection models depends on the design of the loss functions. The bounding box loss function is a crucial component of the loss functions and is used to calculate the error between predicted bounding boxes and actual bounding boxes. In the YOLOv5 network, the bounding box loss function used is GiU [20]. In the object detection dataset, a normal label refers to accurately and correctly bounding the position of the target by the annotator, while low-quality examples refer to cases where the annotator mistakenly selects the target's position or combines multiple targets into one bounding box. The basis of GIoU computation assumes that all examples in the dataset are of high quality, and strengthens the regression on anchor boxes accordingly, whereas the reality of datasets may not align with this assumption. Figure 10 shows normal labeling results, while Figure 11 illustrates low-quality examples. To address this issue, this paper uses WioU [21] instead of the original GIoU in the network. WIoU focuses on regressing on regular-quality anchor boxes, which helps mitigate the impact of low-quality examples, thereby improving detection accuracy. This paper uses WIoU as the bounding box loss function, and the definition of the outlierness \(\beta\) in WIoU is given by Equation (2):  
\[\beta=\frac{\mathrm{L}_{\mathrm{IoU}}^{*}}{\mathrm{L}_{\mathrm{IoU}}}\in[0,+ \infty) \tag{2}\]  
The definition of the outlier degree draws inspiration from the monotonic focusing mechanism of focal loss. \(\mathrm{L}_{\mathrm{IoU}}^{*}\) serves as a monotonic focusing factor, increasing monotonically with the rise in loss values. This effectively reduces the contribution of easy examples to the loss values, allowing the model to focus on challenging examples; \(\overline{\mathrm{L}_{\mathrm{IoU}}}\) is the momentum with a sliding average of m.  
Figure 10: Normal labeling results. Anchor boxes with lower outlierness correspond to high-quality anchor boxes, while those with higher outlierness correspond to low-quality anchor boxes. The gradient gain allocation strategy ensures that anchor boxes with both low and high outlierness receive a small gradient gain, thereby emphasizing the regression for anchor boxes of ordinary quality. The expression for WIoU is shown in Equation (3). \[\mathrm{L_{WIoUv3}}=\mathrm{rR_{WIoU}L_{IoU}} \tag{3}\]  
Regarding the specific parameter expressions in WIoU, as shown in Equations (4) and (5):  
\[\mathrm{r}=\frac{\beta}{\delta\alpha^{\beta-\delta}} \tag{4}\]  
\[\mathrm{R_{WIoU}}=\mathrm{exp}(\frac{\left(x-x_{gt}\right)^{2}+\left(y-y_{gt} \right)^{2}}{\left(W_{g}^{2}+H_{g}^{2}\right)^{*}}) \tag{5}\]  
In the equation, \(\mathrm{r}\) is the non-monotonic focusing coefficient; \(\mathrm{R_{WIoU}}\) is the enlargement coefficient for anchor boxes of ordinary quality; \(\mathrm{L_{IoU}}\) is the loss value of the anchor box; \(\mathrm{W_{g}}\) and \(\mathrm{H_{g}}\) are the width and height of the minimum bounding box; (x,y) is the center point of the predicted box; (xgt,ygt) is the center point of the ground truth box; and * denotes the separation of \(\mathrm{W_{g}}\) and \(\mathrm{H_{g}}\) from the computation graph. According to the description of the outlier degree definition in the previous context, it is evident that anchor boxes with a small outlier degree correspond to high-quality anchor boxes, while anchor boxes with a large outlier degree correspond to low-quality anchor boxes. Because high-quality examples have a relatively large IoU, there is less need for further optimization. On the other hand, low-quality examples have a smaller IoU, making further optimization impractical. Therefore, we formulate a strategy for gradient gain allocation by controlling the hyperparameters \(\alpha\) and \(\delta\), ensuring that both anchor boxes with a small outlier degree and those with a large outlier degree receive a small gradient gain.