# Multiscale Dynamic Attention and Hierarchical Spatial Aggregation for Few-Shot Object Detection
## 3 Methodology
### MultiScale-DynaAttention  
As shown in Figure 3, MultiScale-DynaAttention is a dual-stage attention mechanism module composed of channel attention and spatial attention mechanisms. Building on this foundation, we introduce significant optimizations at each stage. First, in the channel attention module, we divided the input feature map \(X\) along the channel dimension into two subfeatures, each processed by different convolutional layers with varying kernel sizes (5 and 7 in this implementation). Specifically, let the input feature map be represented as \(X\in\mathbb{R}^{\mathbb{C}\times H\times W}\), where \(C\), \(H\), and \(W\) denote the number of channels,height, and width, respectively. The two subfeature maps are \(X_{\text{sub1}}\in\mathbb{R}^{(C/2)\times H\times W}\) and \(X_{\text{sub2}}\in\mathbb{R}^{(C/2)\times H\times W}\). The convolution operations are defined as  
\[X_{1}=\text{conv}_{1}(X_{\text{sub1}}), \tag{6}\]  
\[X_{2}=\text{conv}_{2}(X_{\text{sub2}}), \tag{7}\]  
where \(\text{conv}_{1}\) and \(\text{conv}_{2}\) represent convolution operations with different kernel sizes. Next, each subfeature undergoes global max pooling and global average pooling along the width and height dimensions, resulting in four output features, each with a dimension of \((C/2)\times 1\times 1\). These pooled features are then passed through a two-layer multilayer perceptron (MLP) consisting of two fully connected layers to learn nonlinear mappings, with the output features retaining their dimensions. These features are then fused by element-wise addition and concatenation to obtain the channel attention weights \(A_{c}\), with dimensions \(C\times 1\times 1\). The channel attention weights are calculated as follows:  
\[A_{c1}=\sigma(\text{MLP}(\text{AvgPool}(X_{1}))+\text{MLP}(\text{MaxPool}(X_{1 }))), \tag{8}\]  
\[A_{c2}=\sigma(\text{MLP}(\text{AvgPool}(X_{2}))+\text{MLP}(\text{MaxPool}(X_{2 }))), \tag{9}\]  
\[A_{c}=\text{concat}(A_{c1},A_{c2},\text{dim}=1). \tag{10}\]  
Prior to being processed by the spatial attention module, the input feature map \(X\) is multiplied by the channel attention weights \(A_{c}\), yielding the intermediate feature map \(X_{c}\), which serves as the input to the spatial attention module. Subsequently, \(X_{c}\) undergoes max pooling and average pooling along the channel dimension, resulting in two pooled feature maps with dimensions \(1\times H\times W\). A pooling weight parameter \(\omega\) (ranging from 0 to 1) is introduced to balance the contributions of max pooling and average pooling. The pooled  
Figure 3: Structure of MultiScale-DynaAttention. The first section represents the overall module flow, the second section corresponds to the channel attention mechanism (blue part), and the third section corresponds to the spatial attention mechanism (yellow part). features are then concatenated and passed through a convolution operation to reduce the channel dimension to 1, producing the spatial attention weights \(A_{s}\). The spatial attention weights are computed as follows:  
\[A_{s}=\sigma(\text{conv}^{7\times 7}(\text{concat}[\omega\text{AvgPool}(X),(1- \omega)\text{MaxPool}(X)])). \tag{11}\]  
Finally, the output feature map from MultiScale-DynaAttention is obtained by element-wise multiplication of the input feature map \(X_{c}\) and the spatial attention weight \(A_{s}\), followed by a residual connection [29] that adds the input feature map \(X\) to the final output feature map \(X_{s}\). To enhance the multiscale feature extraction and fusion capability of the backbone network, MultiScale-DynaAttention is incorporated into specific layers of the VGG16 architecture: between layers 2 and 3, layers 5 and 6, and layers 9 and 10. These positions were chosen because the number of channels changes at these layers, allowing MultiScale-DynaAttention to more effectively focus on important features.