# Optimization Algorithm for Surface Defect Detection of Aircraft Engine Components Based on YOLOv5
## 3 Improvement Methods and Improved Models  
### Introducing BiFormer's Bi-Level Routing Attention Mechanism  
The attention mechanism is a mechanism that focuses on important information, adjusts weights, and improves model performance. It is commonly used in various object detection tasks to enhance detection accuracy and robustness by selecting and focusing on regions or features related to the target. The general attention mechanism typically refers to self-attention, which is a key technique used in the Transformer [24] model. It assigns weights by calculating the interdependencies between different positions of the input sequence, thereby adjusting the attention given to each position in the sequence. In the original Transformer network, this type of structure requires computing the correlations between any two elements in the input sequence. When the sequence length is large, the computational complexity grows exponentially, resulting in high computational resource consumption for models incorporating such techniques. Based on this, [PERSON] et al. [25] proposed a visual Transformer model called BiFormer, which introduces a Bi-level routing attention mechanism into the visual Transformer. It enhances feature representation by enabling information interaction between the global attention and local attention levels. The global attention level is used to capture the overall structure and global semantic information of the image, while the local attention level is used to capture the details and local features of the image. The introduction of this Bi-level routing attention mechanism enables a better handling of global and local relationships in images, effectively capturing both the structural and detailed features of the image. It significantly improves the performance of image classification tasks. The structure is shown in Figure 2.  
In Figure 2, \(Q\) is the query used to compute the weighted relevance to the keywords. \(K\) represents the keywords or identifiers used to provide information or for matching purposes. \(V\) is the value associated with the query results and keyword information. \(C\) is a scalar factor used to adjust the allocation of attention and control the focus of attention. \(A\) is the adjacency matrix used to represent the semantic relevance between two regions. \(O\) is the output of the attention mechanism. During the training process, the Biformer model first extracts features from the input image using a convolutional neural network, resulting in a feature map of size \(H\times W\times C\). This feature map is then split into \(r\) sub-feature maps of size \(H\times W\times C/r\) along the channel dimension \(C\). In the global attention part, the Biformer model maps each sub-feature map to a vector of dimension \(d\) through two fully connected layers. It calculates the similarity between this vector and all other vectors, obtaining a weight vector. The sub-feature maps are then weighted and summed according to this weight vector, resulting in an output vector that represents the global relationship. In the local attention part, the Biformer model applies two convolutional layers to each sub-feature map to obtain a local feature map. It then calculates the similarity between this feature map and all other feature maps, obtaining another weight vector. The feature maps are weighted and summed according to this weight vector, resulting in an output vector that represents the local features. In summary, by utilizing the global routing attention mechanism, each sub-feature map is transformed into a semantic vector representing the global relationship. Simultaneously, through the local routing attention mechanism, it is transformed into a semantic vector representing the local features. Finally, the Biformer model concatenates the output vectors from global attention and local attention. Through fully connected layers, these vectors are mapped to the class and position information of the target object.