# Research on Gangue Detection Algorithm Based on Cross-Scale Feature Fusion and Dynamic Pruning
## 2 Optimized Detection Algorithm for Coal Gangue Based on YOLOv5s
### Optimization of the Loss Function  
The foreground of coal gangue images on the conveyor belt bears a resemblance to the background, resulting in increased difficulty of detection. The quality of anchor boxes significantly affects the detection performance. The image prediction within the head layer, the IoU (Intersection over Union), in the bounding box localization loss function represents the ratio of the intersection area to the union area between the predicted box and the ground truth box. The loss function in YOLOv5s is based on the CIoU (Complete IoU) loss function, which takes into account three aspects: the loss of overlap between the predicted box and the ground truth box, the loss of distance between the centers of the predicted box and ground truth box, and the loss of aspect ratio between the predicted box and ground truth box. The loss function is formulated as shown in Equations (6)-(8). \[L_{CIoU}=1-IOU+\frac{\rho^{2}(b,b^{st})}{c^{2}}+\alpha\
u \tag{6}\]  
\[\
u=\frac{4}{\pi^{2}}(\arctan\frac{\omega^{st}}{h^{st}}-\arctan\frac{\omega}{ h})^{2} \tag{7}\]  
\[\alpha=\frac{\
u}{(1-IoU)+\
u} \tag{8}\]  
where \(b\) and \(b^{st}\) represent the center points of the prediction box and the ground truth box, respectively. \(c\) is the length of the minimum diagonal of the external rectangle, \(\rho^{2}\) represents the Euclidean distance between the centroids of the prediction box and the ground truth box, \(\alpha\) is the loss balance factor, \(\
u\) is the normalization of the difference between the predicted and ground truth box aspect ratios, and \(w\), \(h\), \(w^{st}\), \(h^{st}\) are the widths and heights of the predicted and ground truth boxes, respectively. The CIoU Loss does not account for the angular loss between the predicted and ground truth boxes, leading to slow convergence in anchor box regression. To enhance detection accuracy and accelerate convergence speed, this paper builds upon this by introducing the ESIoU (Enhanced Structured IoU) loss function [20; 21], the schematic diagram of the loss function is illustrated in Figure 3.  
The ESIoU loss function takes into account the IoU loss, angle loss, distance loss, shape loss, width loss, and height loss between the predicted and ground truth boxes. The ESIoU loss function is formulated as shown in Equation (9). \[L_{ESIoU}=1-IoU+\frac{\Delta+\Omega}{2}+\frac{\rho^{2}\big{(}w,w^{gt}\big{)}}{ \big{(}D^{w}\big{)}^{2}}+\frac{\rho^{2}\big{(}h,hs^{gt}\big{)}}{\big{(}D^{h} \big{)}^{2}} \tag{9}\]  
The distance loss \(\Delta\) is formulated as shown in Equation (10). \[\Delta=\sum_{t=x,y}\Big{(}1-e^{-(2-\Lambda)\rho_{t}}\Big{)} \tag{10}\]  
where \(\rho_{x}=\left(\frac{B^{gt}-B}{C_{w}}\right)^{2}\), \(\rho_{y}=\left(\frac{B^{gt}-B}{C_{h}}\right)^{2}\), \(C_{w}\), and \(C_{h}\), respectively, denote the width and height of the minimum bounding rectangle of the centroids for the predicted box and the ground truth box. The angle loss \(\Lambda\) is formulated as shown in Equation (11). \[\Lambda=\cos(2(\arcsin(\frac{C_{h}}{\sigma})-\frac{\pi}{4})) \tag{11}\]  
where \(c_{h}\) represents the difference in height between the predicted box and the ground truth box centroids, and \(\sigma\) denotes the separation between the centroids of the predicted box and ground truth box. The shape loss \(\Omega\) is expressed as shown in Equation (12). \[\Omega=\sum_{t=w,h}\big{(}1-e^{-\omega_{t}}\big{)}^{\theta} \tag{12}\]  
where \(\omega_{w}=\frac{|w-w^{gt}|}{max(w,w^{gt})}\), \(\omega_{h}=\frac{|h-bs^{gt}|}{max(h,hs^{gt})}\), \((w,w^{gt})\), and \((h,hs^{gt})\) represent the width and height of the predicted box and the ground truth box, respectively. \(\theta\) represent the attention level on the shape difference between the predicted and ground truth boxes; \(\theta\) is set to 4. In the width and height losses, \(D^{w}\) and \(D^{h}\), respectively, denote the width and height of the minimum bounding rectangle for the predicted and ground truth boxes.