# Research on Automated Fiber Placement Surface Defect Detection Based on Improved YOLOv7
## 3 Model Improvements Based on the YOLOv7 Algorithm  
### Enhancement of Backbone Network with BiFormer Attention Mechanism Based on Transformer  
The Transformer, as a widely utilized network model in the recent field of computer vision, relies on self-attention mechanisms and possesses several advantages. Firstly, it is capable of effectively capturing long-range dependencies in data [30], which is crucial for handling various complex data tasks. Secondly, the model design of the Transformer is flexible and free from inductive bias, enabling it to adapt to various types of data [31]. Additionally, the Transformer exhibits high parallelism, which accelerates the training and inference processes of large-scale models, thus improving computational efficiency [32; 33; 34]. Traditional object detection algorithms like YOLOv7 extract image features using operations such as convolution and pooling. However, this approach has limitations; to address this issue, this study introduces a Transformer-based BiFormer attention mechanism module [35]. In the BiFormer model, the two-dimensional input feature map \(X\in\mathbb{R}^{H\times W\times C}\) is first divided into \(S\times S\) non-overlapping regions to ensure each region contains \(\frac{\text{HW}}{S^{2}}\) feature vectors. Subsequently, X undergoes reshaping to obtain \(X^{r}\in\mathbb{R}^{5^{2}\times\frac{\text{HW}}{5^{2}}\times C}\). Following this, linear projection is applied to \(X^{r}\) using fully connected layers, resulting in a new \(Q,K,V\in\mathbb{R}^{5^{2}\times\frac{\text{HW}}{5^{2}}\times C}\). Through this projection operation, BiFormer can capture correlations between different positions and channels, providing more comprehensive input information for the subsequent attention mechanism. \[Q=X^{r}W^{q},K=X^{r}W^{k},V=X^{r}W^{v} \tag{1}\]  
where \(W^{q}\), \(W^{k}\), and \(W^{v}\) are the projection weights for query, key, and value, with dimensions of \(\mathbb{R}^{C\times C}\). To facilitate region-to-region routing, BiFormer adopts a directed graph approach. Firstly, BiFormer performs region-wise average pooling on \(Q\) and \(K\) to obtain \(Q^{r},K^{r}\in\mathbb{R}^{5^{2}\times C}\). Through this step, BiFormer can calculate the similarity between queries and keys at the region level. Subsequently, BiFormer conducts matrix multiplication between the transpose of \(Q^{r}\) and \(K^{r}\) to generate an adjacency matrix of the region-to-region affinity graph, denoted as \(A^{r}\in\mathbb{R}^{5^{2}\times S^{2}}\). \[A^{r}=Q^{r}(K^{r})^{T} \tag{2}\]  
In order to further optimize the affinity graph, BiFormer retains the top k connections of each region with other regions. BiFormer introduces a routing index matrix \(I_{r}\in\mathbb{N}^{5^{2}\times S^{2}}\), where each row of \(I_{r}\) contains the indices of the top k most relevant regions to the i-th region. Through the routing index matrix \(I_{r}\), Equation (3) [35] filters and collects K and V to obtain \(K^{s}\) and \(V^{s}\). Finally, a fine-grained token-to-token attention mechanism is applied to Q, \(K^{s}\), and \(V^{s}\) collectively, as shown in Equation (4), where LCE [36] represents depth-wise separable convolution with a kernel size of 5 and a stride of 1. \[K^{g}=\text{gather}(K,I^{r}),V^{g}=\text{gather}(V,I^{r}) \tag{3}\]  
\[O=\text{Attention}(Q,K^{g},V^{g})+\text{LCE}(V) \tag{4}\]  
In summary, the BiFormer attention mechanism guides YOLOv7 to focus more on surface defects in AFP, disregarding irrelevant regions. When calculating attention weights, it considers relationships between the same type of surface defects as well as different types of surface defects. This allows it to capture more comprehensive feature relationships and effectively detect surface defects in AFP. Furthermore, the BiFormer attention mechanism retains certain positional information, enabling better understanding and representation of sequential data through relative position encoding.