## 3 Establishment of YOLOv5-Based Optimized Detection Model and Evaluation Metrics
### Improvement of the Loss Function  
The default CIoU loss function in YOLOv5, while considering the distance between the center points of the ground truth and predicted boxes as well as the aspect ratio of the bounding boxes, fails to penalize the width and height mismatch when the aspect ratios of the predicted and ground truth boxes are the same. This results in the aspect ratio penalty term having no effect, thereby limiting further optimization of the model. In practical  
Figure 5: Network structure for ECA module.  
-held defect detection, the CIoU loss function often has limitations when dealing with small targets, complex-shaped defects, and background noise. Considering that the WIoU loss function uses a dynamic non-monotonic focusing mechanism, it enables the model to flexibly focus on relevant anchor boxes by dynamically adjusting attention across different anchor boxes. This mechanism not only improves the model's convergence accuracy and overall performance but also enhances its adaptability to variations in defect target scales and the presence of background noise. Therefore, the WIoU loss function is used as a replacement for the CIoU loss function. The WIoU loss function uses the outlier degree \(\beta\) instead of IoU to assess the quality of the predicted boxes. The WIoU loss function has three versions: v1, v2, and v3. Given the prevalence of low-quality samples in pipeline weld defect images, version v3 is adopted in this study due to its adaptive training strategy, which effectively balances the learning of hard and easy samples, resulting in the best overall performance. The expression of the WIoU-v3 loss function and the parameter \(\beta\) is as follows:  
\[L_{WIoU}=rR_{WIoU}L_{IoU} \tag{3}\]  
\[r=\frac{\beta}{\delta\alpha^{\beta-\delta}} \tag{4}\]  
\[R_{WIoU}=\exp(\frac{(x-x_{gt})^{2}+(y-y_{gt})^{2}}{(W_{\beta}^{2}+H_{\beta}^{ 2})^{*}}) \tag{5}\]  
\[L_{IoU}=1-IoU=1-\frac{W_{i}H_{i}}{wh+w_{gt}h_{gt}-W_{i}H_{i}} \tag{6}\]  
\[\beta=\frac{L_{IoU}^{*}}{L_{IoU}^{-}}\in[0,+\infty) \tag{7}\]  
where \(L_{WIoU}\) represents the WIoU loss function; \(r\) is the dynamic non-monotonic focusing factor; \(R_{WIoU}\in[0,1)\) is the penalty term of WIoU, representing distance attention; \(L_{IoU}\)\(\in[0,1]\) is the IoU loss function, representing the ratio of the intersection between the ground truth box and the predicted box; \(\delta\) and \(\alpha\) are hyperparameters set to 3 and 1.9, respectively [23]; \(x\) and \(y\) are the horizontal and vertical coordinates of the top-left corner of the predicted box, respectively; \(x_{gt}\) and \(y_{gt}\) are the horizontal and vertical coordinates of the top-left corner of the ground truth box, respectively; \(W_{g}\) and \(H_{\beta}\) represent the width and height of the minimum enclosing box, respectively; \(w\) and \(h\) represent the width and height of the predicted box, respectively; \(w_{gt}\) and \(h_{gt}\) represent the width and height of the ground truth box, respectively; \(L_{IoU}^{-}\) is the mean of \(L_{IoU}\) across the batch; \(L_{IoU}^{*}\) is the result obtained after separation.  
The smaller \(\beta\) indicates a higher quality of the anchor box. Since \(L_{IoU}^{-}\) is dynamically changing, the criteria for classifying anchor box quality are also dynamic. Therefore, the WIoU loss function can adaptively adjust the gradient gain distribution strategy, enhancing overall detection performance.