# Tailhook Recognition for Carrier-Based Aircraft Based on YOLO with Bi-Level Routing Attention
## 3 Methodology
### Bi-Level Routing Attention Mechanism  
The multi-head self-attention (MHSA) [18] mechanism plays a significant role in enhancing the model's ability to process sequences and images, particularly within the Transformer architecture. However, its fixed attention range, high computational complexity, and high memory consumption lead to performance bottlenecks, limiting its ability to handle large-scale data. The bi-level routing attention (BRA) [19; 20] mechanism, proposed by [PERSON] et al., aims to improve computational efficiency, reduce memory consumption, and enhance the ability to model long-range dependencies. This architecture significantly improves the efficiency and effectiveness of the attention mechanism when processing high-resolution input data, such as images. By dynamically selecting and routing attention points at two levels, the model can allocate computational resources more flexibly and efficiently.  
Unlike other sparse attention mechanisms, the BRA dynamically determines which key-value pairs each query should attend to. Figure 6 illustrates the bi-level routing attention mechanism, which first collects key-value pairs in the top k relevant windows to compute the most relevant regions. In these windows, it searches for the keys most relevant to the query and calculates the corresponding attention weights accordingly.  
The two-dimensional input feature is divided into \(\frac{HW}{S^{2}}\) non-overlapping regions, and these regions are transformed into query, key, and value tensors using linear projection. Linear projection maps the original data to a new space through matrix multiplication, generating region-level projected \(Q\), \(K\), and \(V\) tensors for the attention mechanism:  
\[Q =X^{r}W^{q}, \tag{1}\] \[K =X^{r}W^{k},\] (2) \[V =X^{r}W^{v}, \tag{3}\]  
Among them, the \(W^{q}\), \(W^{k}\), and \(W^{v}\) are the projection weight matrices for \(Q\), \(K\), and \(V\), respectively, and \(X^{r}\) is the input feature after reshaping. To determine which subregions each routing region should focus on, calculate the average of \(Q\) and \(K\) features within each  
Figure 5: The overall framework of the model.  
Figure 6: The bi-level routing attention mechanism.  
region to obtain the region-level query and key \(Q^{r}\) and \(K^{r}\), and then derive the region-level adjacency matrix \(A^{r}\) by taking the dot product of these two region-level features.  
\[A^{r}=Q^{r}(K^{r})^{T}, \tag{4}\]  
\[I^{r}=topkIndex(A^{r}), \tag{5}\]  
where \(A^{r}\) represents the degree of correlation between the two regions; \(I^{r}\) is the route index matrix and selects the first \(k\) most relevant connections from the adjacency matrix. Therefore, by preserving only the first \(k\) maximum values in each row of the adjacency matrix, projections with lower correlation degrees are discarded, thus realizing region-to-region routing.  
For each query token, focus only on all key-value pairs within the \(k\) region being routed. This step is carried out efficiently by the gather operation, which uses the index matrix to select elements from the input tensor and create a new reassembled tensor:  
\[K^{g}=gather(K,I^{r}), \tag{6}\]  
\[V^{g}=gather(V,I^{r}), \tag{7}\]  
where \(K^{g}\) and \(V^{g}\) are the gathered keys and values. Finally, the standard attention mechanism is applied to \(K^{g}\) and \(V^{g}\), and the output feature map \(O\) is obtained:  
\[O=Attention(Q,K^{g},V^{g})+Lee(V), \tag{8}\]  
here, \(Lee(V)\) represents the local context enhancement term.