# Research on Automated Fiber Placement Surface Defect Detection Based on Improved YOLOv7
## 3 Model Improvements Based on the YOLOv7 Algorithm
### Improvement in Bounding Box Regression Loss Function with Introduction of WIoU Instead of CloUI  
Since YOLOv1, the YOLO series models have widely adopted various loss functions, including the bounding box regression loss function, classification loss function, and confidence loss function, to optimize the model's predictions and improve the accuracy and performance of object detection. The BBR loss function guides target localization, the classification loss function determines the target's category, and the confidence loss function measures the accuracy and confidence of the predicted box. The combination of these loss functions has contributed to the outstanding performance of the YOLO series models in object detection tasks. In YOLOv7, the CIoU loss function, used in YOLOv5, has been adopted to calculate the regression loss, as shown in Equations (6)-(9) [44]:  
\[v=\frac{4}{\pi^{2}}\left(\arctan\frac{w^{st}}{h^{st}}-\arctan\frac{w}{h}\right) ^{2} \tag{6}\]  
\[\alpha=\left(\frac{v}{\left(1-IoU\right)+v}\right) \tag{7}\]  
\[\mathcal{R}_{CIoU}=\left(\frac{\rho^{2}\left(b,b^{st}\right)}{c^{2}}+\alpha v\right) \tag{8}\]  
\[\mathcal{L}_{CIoU}=1-IoU+\left(\frac{\rho^{2}\left(b,b^{st}\right)}{c^{2}}+ \alpha v\right) \tag{9}\]  
\[\mathcal{L}_{CIoU}=1-IoU+\left(\frac{\rho^{2}\left(b,b^{st}\right)}{c^{2}}+ \alpha v\right) \tag{10}\]  
where:  
\(\alpha\) is the weight parameter;  
\(v\) represents the similarity between the aspect ratios of the predicted box and the ground truth box;  
\(c\) is the diagonal distance of the minimum enclosing box containing the predicted box and the ground truth box;  
\(\rho^{2}\left(b,b^{st}\right)\) represents the Euclidean distance between the centers of the predicted box and the ground truth box;  
\(\mathcal{R}_{CIoU}\) is the difference penalty term. To address the issue of low-quality examples in the training data and enhance the model's generalization ability, Wise-IoU (WIoU) [45] was proposed as a distance attention mechanism and the WIoUv1 model was constructed using a two-layer attention mechanism. Figure 3: Adaptive spatial feature fusion [41]. By introducing a parameter \(\mathcal{R}_{WIoU}\) with a range of [1, e), it significantly enhances the \(\mathcal{L}_{IoU}\) for normal-quality anchors. Additionally, another parameter, \(\mathcal{L}_{IoU}\), with a range of [0, 1], is introduced to notably decrease the \(\mathcal{R}_{WIoU}\) for high-quality anchor boxes. When there is good overlap between the anchor box and the target box, this parameter places more importance on the distance between their center points. The incorporation of these two attention mechanisms in designing the loss function allows for reducing the penalty on low-quality examples due to geometric factors and for minimizing interference with the model. Specifically, the introduction of \(\mathcal{R}_{WIoU}\) directs more attention toward anchors of normal quality, thereby improving the model's ability to handle such anchors. On the other hand, the introduction of \(\mathcal{L}_{IoU}\) primarily adjusts high-quality anchor boxes by reducing \(\mathcal{R}_{WIoU}\), prompting the model to focus more on the distance between the center points of the target box and the anchor box. This enhancement contributes to the performance of object detection algorithms and enhances the model's accuracy in capturing the shape of targets. Next, the dynamic non-monotonic anchor box fine-tuning model is introduced, which evaluates the abnormality of anchor boxes by comparing \(\mathcal{L}_{IoU}\) and \(\overline{\mathcal{L}_{IoU}}\). A lower abnormality indicates higher quality of the anchor box, and thus, smaller gradient gains are assigned to focus the fine-tuning efforts on anchors of normal quality. Additionally, assigning smaller gradient gains to anchor boxes with higher abnormality effectively prevents significant harmful gradients caused by low-quality examples. A non-monotonic focus coefficient is built using \(\beta\) and integrated into WIoUv1.