## 2 Materials and Methods
### YOLOv8 Network Improvement
#### 2.3.1 Improvement of Loss Function  
IoU (Intersection over Union) is a simple function used to compute the localization loss by evaluating the degree of overlap between two bounding boxes [42,43]. The YOLOv8n network employs the CIoU (Complete IoU) as its loss function, which primarily focuses  
Figure 5: Improved YOLOv8n network architecture. Note: Figure 5 illustrates the network architecture of YOLOv8n-WSE-pest in this study. The upper part depicts the hierarchical structure of the network, while the right side elucidates the working principle of the original components within the network. Specifically, the Binary Cross-Entropy (BCE) loss function is employed for binary classification tasks, quantifying the discrepancy between the predicted and actual class labels. The Distribution Focal Loss (DFL) transforms the bounding box regression problem in object detection into a sequence prediction problem, thereby enhancing the detection accuracy in scenarios where the targets exhibit boundary ambiguity or occlusion. Additionally, the shortcut operation within the Bottleneck component facilitates skip connections in feature maps, contributing to feature fusion and the stability of network training. on the IoU between predicted and ground truth boxes. However, it fails to fully capture the relative position and size differences between objects and is not suitable for insect images with missing body parts. Moreover, when dealing with low-quality data sources, the original loss function cannot accurately measure the performance of object detection. To address these issues, this study improves the YOLOv8n's default loss function using the dynamic non-monotonic focal mechanism, WIoU-v3 [44]. This enhanced loss function aims to overcome the limitations of the original loss function by considering the relative position and size differences between objects. The schematic diagram of this loss function is depicted in Figure 6.  
The WIoU-v3 formulation is defined by Equations (1)-(3), where its core idea is to replace IoU with outliers to assess the quality of anchor boxes. The degree of outliers in anchor boxes is represented by the ratio of \(L_{IoU}^{*}\) to \(\overline{L_{IoU}}\). Here, \(L_{IoU}^{*}\) denotes the monotonically increasing focus coefficient of the original \(L_{WIoU-v1}\), while \(\overline{L_{IoU}}\) represents the sliding average with momentum \(m\). The non-monotonic focus coefficient \(r\) is constructed using the degree of outliers, denoted by \(\beta\). Additionally, \(\alpha\) and \(\delta\) are hyperparameters involved in the formulation. \[\beta=\frac{L_{IoU}^{*}}{\overline{L_{IoU}}}\in[0,+\infty) \tag{1}\]  
\[m=1-\sqrt[m]{0.05} \tag{2}\]  
\[L_{WIoUv3}=rL_{WIoUv1},r=\frac{\beta}{\delta\alpha^{\beta-\delta}} \tag{3}\]  
Applying the coefficient \(r\) to \(L_{WIoU-v1}\), which is defined by Equations (4) and (5), \(W_{g}\) and \(H_{g}\) represent the width and height of the bounding. \(R_{WIoU}\in[1,e)\) denotes the regular anchor box \(L_{IoU}\) with enhanced quality, while \(L_{IoU}\in[0,1]\) represents the high-quality anchor box \(R_{WIoU}\) with reduced quality. Due to the dynamic nature of \(\overline{L_{IoU}}\), \(L_{WIoU-v3}\) is capable of a real-time gradient allocation strategy. When the value of \(\beta\) is small, the anchor box has a high quality, and the loss function assigns it a smaller gradient gain, focusing the bounding box regression on anchor boxes of regular quality. When the value of \(\beta\) is large, the anchor box has a low quality, and the loss function assigns it a smaller gradient gain  
Figure 6: WIoU-v3 schematic diagram. Note: On the **left** side of the figure are anchor boxes of three different qualities. On the **right** side, the method is demonstrated to more accurately assess the quality of anchor boxes by predicting the relative position and size differences between the predicted boxes and the ground truth boxes. to avoid harmful gradients caused by low-quality anchor boxes while still maintaining attention on anchor boxes of regular quality.