# Research on Gangue Detection Algorithm Based on Cross-Scale Feature Fusion and Dynamic Pruning
## 2 Optimized Detection Algorithm for Coal Gangue Based on YOLOv5s
### Cross-Scale Feature Fusion Based on Lightweighted Weighted Feature Pyramids  
In the process of feature fusion, the significance of different features varies. YOLOv5s employs a multi-scale feature fusion approach with Feature Pyramid Network (FPN [17]) and Path Aggregation Network (PAN [18]). Constructing a bottom-to-top Path Aggregation Network allows the network to fuse semantic information from both low-level and high-level features. However, during the feature concatenation process, the consideration of feature weight is not taken into account. In the Weighted Bidirectional Feature Pyramid Network (BiFPN [19]), cross-scale connections and weighted feature fusion are employed, assigning appropriate weight values to features at different levels. Figure 2a illustrates the structure of the BiFPN.  
This paper further optimizes the BiFPN network as follows. Firstly, YOLOv5s has only three distinct scale output nodes, whereas BiFPN has five output nodes. To integrate the BiFPN network into YOLOv5s, it is imperative to reduce the number of BiFPN network output nodes to match the three nodes of YOLOv5s. Secondly, during the process of feature fusion, nodes that do not contribute to the fused features have a negligible impact on the output results. By reducing the number of nodes, the parameter count is decreased. Therefore, nodes between \(P_{3}\) and \(P_{5}\) were excluded. Finally, by adding connections between input nodes and output nodes within the same size dimension, the network achieves more comprehensive feature fusion without significantly increasing computational load. Therefore, the cross-scale connection for the \(P_{4}\) layer is retained.  
During the feature fusion process, the resolution of each layer's input features varies, and they correspond to different weights. Moreover, the magnitude of weight values is unbounded, which may lead to gradient explosions, preventing model convergence. To address this, Softmax normalization is employed to ensure weight values lie within the range of [0; 1; 1]. This enhances computational efficiency and enables rapid normalization fusion for bidirectional cross-scale connections. The weight normalization formula is represented as Equation (1).  
\[O=\sum_{l}\frac{w_{i}}{\epsilon+\sum\limits_{j}w_{j}}\cdot I_{i}, \tag{1}\]  
where \(\epsilon=0.001\), the ReLU activation function, is employed to ensure that \(w_{i}\geq 0\), \(I_{i}\) denotes the input features.  
The improved Weighted Bidirectional Feature Pyramid Network is illustrated in Figure 2b.  
Figure 2: BiFPN network diagram before and after improvement. (**a**) Structure of BiFPN. (**b**) Improved BiFPN-tiny.  
In Figure 2b, the formulas for each output node are shown in Equations (2)-(5).  
\[P_{3}^{out}=Conv\left(\frac{w_{1}\cdot P_{3}^{in}+w_{6}\cdot Resize\left(P_{6}^{ td}\right)}{w_{1}+w_{6}+\epsilon}\right) \tag{2}\]  
\[P_{4}^{td}=Conv\left(\frac{w_{2}\cdot P_{4}^{in}+w_{5}\cdot Resize\left(P_{5}^{ in}\right)}{w_{1}+w_{2}+\epsilon}\right) \tag{3}\]  
\[P_{4}^{out}=Conv\left(\frac{w_{7}\cdot P_{4}^{in}+w_{3}\cdot P_{4}^{td}+w_{8} \cdot Resize\left(P_{3}^{out}\right)}{w_{7}+w_{3}+w_{8}+\epsilon}\right) \tag{4}\]  
\[P_{5}^{out}=Conv\left(\frac{w_{4}\cdot P_{5}^{in}+w_{9}\cdot Resize\left(P_{4} ^{out}\right)}{w_{4}+w_{9}+\epsilon}\right) \tag{5}\]  
where \(P_{i}^{in}\) is the input feature of the layer, \(P_{i}^{td}\) is the middle feature of the \(i\) layer, \(P_{i}^{out}\) is the output feature of the \(i\) layer after feature fusion, Resize represents either upsampling or downsampling operations, and Conv denotes a convolution operation.