# Research on Automated Fiber Placement Surface Defect Detection Based on Improved YOLOv7
## 3 Model Improvements Based on the YOLOv7 Algorithm
### Enhancement of Neck Layer Feature Fusion with Introduction of AFPN Instead of PAFPN  
For the defect detection task, critical features need to encompass detailed semantic information of objects and be extracted through deep neural networks. However, in the current feature pyramid structure, high-level features propagate through multiple intermediate scales, interact with features at these scales, and eventually fuse with low-level features. However, during the propagation and interaction process, the semantic information of high-level features may be lost or weakened. Meanwhile, the bottom-up path structure of the Progressive Attention Feature Pyramid Network (PAFPN) [37; 38; 39; 40] employed by YOLOv7 may introduce the opposite issue: detailed information of low-level features may be lost or weakened during propagation and interaction. Therefore, here, we introduce the Asymptotic Feature Pyramid Network (AFPN) [41] to enhance feature fusion. The AFPN initializes by fusing adjacent low-level features and gradually incorporates high-level features into the fusion process. This strategy can prevent significant semantic differences between non-adjacent levels and ensure a balanced preservation of detailed and semantic information in object detection tasks [41].  
Similar to other object detection methods based on feature pyramid networks [37], the AFPN extracts features at different pyramid levels from the backbone network to form a set of multi-scale features. In the feature fusion step, low-level features first interact with subsequent levels and are eventually fused with high-level features. Through convolution operations, a set of multi-scale features is generated. For the YOLO model, only a subset of features is input into the feature pyramid network, producing the output set.  
The AFPN architecture is illustrated in Figure 2. By gradually fusing features at different pyramid levels, the AFPN architecture brings the features closer to semantic information. This progressive fusion approach reduces semantic gaps compared to directly fusing non-adjacent hierarchical features. For instance, extracting the final layer feature from each feature level of the backbone network forms a set of features at different scales [37], namely \(\{C_{2},C_{3},C_{4},C_{5}\}\). The fusion of features between \(C_{2}\) and \(C_{3}\) helps reduce the gap between them, and the adjacency of \(C_{3}\) and \(C_{4}\) further reduces the semantic gap between \(C_{2}\) and \(C_{4}\). This progressive fusion approach enhances the effectiveness of feature fusion.  
For size adjustment and feature fusion in the AFPN, \(1\times 1\) convolutions and bilinear interpolation are used for upsampling, while different convolution kernels and strides are used for downsampling. Through four residual units [42], features are learned. Unlike YOLOv7, there is no 8x upsampling and downsampling.  
During the multi-level feature fusion process, the AFPN employs ASFF [43] to allocate different spatial weights to features from different layers, enhancing the importance of key levels while mitigating conflicting information from different objects. Figure 3 illustrates the fusion of features from three levels. Let us assume that \(x_{ij}^{n-l}\) represents the feature vector at position \((i,\ j)\) from the nth layer to the lth layer. Through adaptive spatial fusion of multi-level features, we obtain the fused feature vector, denoted as \(y_{ij}^{l}\). It is defined as a linear combination of feature vectors \(x_{ij}^{1-l}\), \(x_{ij}^{2-l}\), and \(x_{ij}^{3-l}\), as shown in Equation (5).  
\[y_{ij}^{l}=\alpha_{ij}^{l}x_{ij}^{1-l}+\beta_{ij}^{l}x_{ij}^{2-l}+\gamma_{ij}^{ l}x_{ij}^{3-l} \tag{5}\]  
where \(\alpha_{ij}^{l}\), \(\beta_{ij}^{l}\), and \(\gamma_{ij}^{l}\) represent the spatial weights of the three levels of features at level l, and \(\alpha_{ij}^{l}+\beta_{ij}^{l}+\gamma_{ij}^{l}=1\).  
Figure 2: The structure of the AFPN [41] (the black arrows indicate convolution operations, while the aqua arrows indicate adaptive spatial fusion).