# Weed Detection Method Based on Lightweight and Contextual Information Fusion
## 2 Materials and Methods
### Enhanced YOLOv5 Model
#### 2.2.5 Soft-NMS-EloU  
Traditional Non-Maximum Suppression (NMS) [36] is an algorithm used to obtain local maxima and suppress non-maximum values. It begins by sorting all candidate bounding boxes based on their category scores and selects the one with the highest score. It then iterates through the remaining candidate boxes. When the Intersection over Union (IoU) value between the remaining candidate box and the one with the highest score exceeds the predetermined IoU threshold, the remaining box is removed. This process is repeated until all boxes have been processed, resulting in the retained candidate boxes as the final detection results. However, when there are numerous densely packed objects in an image, NMS can lead to overlapping between the preselected boxes, resulting in the direct  
Figure 5: CARAFE module diagram. discard of many boxes and a decrease in detection accuracy. Since our dataset contains many densely packed weed targets, this algorithm is not suitable. To address this issue, the NMS algorithm is replaced with the Soft-NMS algorithm. In Soft-NMS, when the IoU between a detection box and the highest-scoring detection box exceeds the set threshold, it does not immediately set the score of the detection box to zero. Instead, it replaces the original score with a slightly lower score. The following are the processing steps for NMS and Soft-NMS: (1) represents NMS and (2) represents Soft-NMS. Here, \(s_{i}\) denotes the current score of the detection box being processed, \(N_{t}\) is the set IoU threshold, and \(M\) is the highest-scoring detection box. \[s_{i}\bigg{\{} \begin{array}{ll}s_{i},&IoU(M,b_{i})<N_{t}\\ 0,&IoU(M,b_{i})\geq N_{t}\end{array}  \tag{1}\]  
\[s_{i}\bigg{\{} \begin{array}{ll}s_{i},&IoU(M,b_{i})<N_{t}\\ s_{i}(1-IoU(M,b_{i})),&IoU(M,b_{i})\geq N_{t}\end{array}  \tag{2}\]  
Soft-NMS involves further calculations related to IoU, and the choice of IoU directly affects the suppressive effect of Soft-NMS. The original Soft-NMS used IoU for non-maximum suppression. However, IoU has a limitation: when two detection boxes do not overlap, the IoU value is 0, indicating no re flection of the distance between the two objects. In such cases, the gradient is 0, making further optimization and training impossible. CIoU is a loss function used in YOLOv5, which takes into account the overlap area of bounding box regression, center point distance, and aspect ratio. The penalty term for CIoU is defined as follows:  
\[R_{CIoU}=\frac{\rho^{2}\big{(}b,b^{gt}\big{)}}{c^{2}}+\alpha v \tag{3}\]  
Here, \(\alpha\) is a weight function, and \(v\) is used to measure the similarity of aspect ratios, defined as follows:  
\[v=\frac{4}{\pi^{2}}(arctan\frac{w^{gt}}{h^{gt}}-arctan\frac{w}{h})^{2} \tag{4}\]  
The CIoU loss is defined as follows:  
\[L_{CIoU}=1-IOU+\frac{\rho^{2}\big{(}b,b^{gt}\big{)}}{c^{2}}+\alpha v \tag{5}\]  
In the above equations, \(C_{uv}\), \(C_{h}\), \(\rho\) represent the width and height of the minimum enclosing box covering both boxes, and the Euclidean distance between \(b\) and \(b^{gt}\), \(w\), \(h\), \(w^{gt}\), \(h^{gt}\) are the width and height of the predicted and ground truth boxes. DIoU and CIoU overcome the issue of calculating loss when prediction and ground truth boxes do not intersect or contain each other. They accelerate convergence by computing the center point distance. The \(\alpha v\) factor in the CIoU calculation incorporates aspect ratio considerations, improving regression accuracy. However, CIoU has some limitations. It has ambiguity regarding aspect ratios and cannot reflect the differences in width and height confidence effectively, which can hinder effective similarity optimization. To address these shortcomings, this paper proposes a method called Soft-NMS-EloU, which combines EIoU with Soft-NMS to achieve faster and better convergence and improve training accuracy. The EIoU loss calculation is shown in Equation (6):  
\[