# Hybrid Multi-Granularity Approach for Few-Shot Image Retrieval with Weak Features
## 3 Model Architecture
### Bi-Level Routing Attention Mechanism  
Attention mechanisms play a pivotal role in deep learning, particularly for small-target, weak-feature, and few-shot data. Inspired by human vision, they enable neural networks to focus on critical regions while suppressing irrelevant information. For small targets and weak features, attention acts as an intelligent filter, allocating computational resources efficiently to capture essential details and improve feature extraction. Standard self-attention mechanism faces challenges with small targets and weak features due to its global dependency computation, which incurs high computational overhead and disperses attention weights. Multi-Head Self-Attention (MHSA) [40] enhances feature modeling through parallel attention heads, but its fixed attention range struggles with sparse features in small-target and weak-feature data. Additionally, MHSA's equivalent computation wastes resources on irrelevant regions, reducing efficiency. Figure 3: The ODConv implementation process. [PERSON] et al. [41] proposed the Bi-Level Routing Attention (BRA) mechanism, which employs dynamic query-aware sparse attention to address these challenges. For small targets and weak features, BRA first performs coarse-grained region screening to locate relevant key-value pairs, reducing computational complexity. It then conducts fine-grained attention calculations within these regions to precisely extract local details. As shown in Figure 4, BRA selects the top candidate windows for attention computation, balancing efficiency and precision. Figure 4 illustrates a hierarchical attention mechanism framework that was used for visual feature processing, and it shows significant advantages in sparse feature scenarios. The input image is divided into multiple candidate windows, with a window size of \(\frac{W}{S}\times\frac{H}{S}\), where \(S\) is an adjustable step size parameter. By selecting the key-value pairs (\(K^{S}\) and \(V^{S}\)) with the highest relevance within these regions, the mechanism achieves a balance between computational efficiency and feature precision. The query vector \(Q\) (with dimensions \(\frac{HW}{S^{2}}\times C\)) is matrix-multiplied with the key-value matrix (with dimensions \(\frac{kHW}{S^{2}}\times C\)) to generate the attention matrix \(A\), whose elements represent the correlation strength between the query and key-value features. After normalization processing, the attention matrix is combined with the value matrix to ultimately produce the feature matrix \(O\) (with dimensions \(\frac{HW}{S^{2}}\times C\)). Compared to traditional methods, this hierarchical strategy only performs in-depth calculations on highly relevant regions, thereby avoiding redundant computations in the sparse feature background and significantly improving model performance. The figure clearly demonstrates the complete process from image segmentation, feature extraction, and attention weighting to the final feature fusion, reflecting the delicate balance of computational efficiency and feature precision achieved by this mechanism. The 2D input feature map \(X\in R^{H\times W\times C}\) was divided into \(S\times S\) non-overlapping regions, each containing \(\frac{HW}{S^{2}}\) feature vectors. Reshaping \(X\) into \(X^{\prime}\), linear projections generated query, key, and value tensors \(Q\), \(K\), and \(V\), respectively:  
\[Q =X^{\prime}W^{q}, \tag{5}\] \[K =X^{\prime}W^{k},\] (6) \[V =X^{\prime}W^{v}. \tag{7}\]  
where \(W^{q}\), \(W^{k}\), and \(W^{v}\) are projection weights for queries, keys, and values, respectively. A directed graph determines the inter-region attention relationships. Region-level queries and the keys \(Q^{\prime},K^{\prime}\in R^{S^{2}\times c}\) are obtained by averaging, and the adjacent matrix \(A^{\prime}\) is computed as follows:  
\[A^{\prime}=Q^{\prime}(K^{\prime})^{T}. \tag{8}\]  
Figure 4: The Bi-Level Routing Attention mechanism.