# Multiscale Dynamic Attention and Hierarchical Spatial Aggregation for Few-Shot Object Detection
## 3 Methodology
### Preliminaries and Training Strategy  
In the context of FSOD, we divided the dataset into base classes and novel classes. The base class dataset \(\mathcal{D}_{\text{base}}\) contains abundant samples for each class, used for initial training to allow the model to learn general feature representations. The novel class dataset \(\mathcal{D}_{\text{novel}}\) contains only a few samples for each class, used for fine-tuning the model to adapt to new object categories. The base class dataset \(\mathcal{D}_{\text{base}}\) is defined as  
\[\mathcal{D}_{\text{base}}=\{(x_{i},y_{i})\mid i=1,\dots,N_{\text{base}}\}, \tag{1}\]  
Figure 2: Overall architecture of the model. The upper section represents the base training phase, while the lower section represents the novel fine-tuning phase. The lock icon indicates frozen parameters, and the unlock icon represents adjustable parameters. where \(x_{i}\) represents the \(i\)-th input image, and \(y_{i}=(c_{i},b_{i})\) represents the corresponding target class label \(c_{i}\) and bounding box coordinates \(b_{i}\). Here, \(c_{i}\in\mathcal{C}_{\text{base}}\), and \(\mathcal{C}_{\text{base}}\) is the set of base classes. The novel class dataset \(\mathcal{D}_{\text{novel}}\) is defined as  
\[\mathcal{D}_{\text{novel}}=\{(x_{j},y_{j})\mid j=1,\dots,N_{\text{novel}}\}, \tag{2}\]  
where \(x_{j}\) represents the \(j\)-th input image, and \(y_{j}=(c_{j},b_{j})\) represents the corresponding target class label \(c_{j}\) and bounding box coordinates \(b_{j}\). Here, \(c_{j}\in\mathcal{C}_{\text{novel}}\), and \(\mathcal{C}_{\text{novel}}\) is the set of novel classes. Based on the above settings, our model follows a transfer-learning-based training strategy adapted from the TFA algorithm. First, we pretrain the TFA model using the base class dataset \(\mathcal{D}_{\text{base}}\), which contains abundant samples \(\{(x_{i},y_{i})\}_{i=1}^{N_{\text{base}}}\), to learn general feature representations. The loss function at this stage is defined as  
\[\mathcal{L}_{\text{base}}=\sum_{i=1}^{N_{\text{base}}}\big{(}\mathcal{L}_{ \text{RPN}}(x_{i},y_{i})+\mathcal{L}_{\text{cls}}(x_{i},y_{i})+\mathcal{L}_{ \text{reg}}(x_{i},y_{i})\big{)}, \tag{3}\]  
where \(\mathcal{L}_{\text{RPN}}\) represents the region proposal network (RPN) loss, \(\mathcal{L}_{\text{cls}}\) denotes the classification loss, and \(\mathcal{L}_{\text{reg}}\) refers to the bounding box regression loss. Next, we freeze the parameters \(\theta_{\text{feat}}\) of the feature extraction network to ensure they remain unchanged during subsequent training. The MultiScale-DynaAttention module is then incorporated into the feature extraction network, resulting in updated feature extraction network parameters \(\theta_{\text{feat}}^{*}\). The detection network further extracts intermediate features and integrates the hierarchical adaptive-pyramid pooling module to construct a spatially adaptive multiscale detection branch, forming the complete detection model \(\mathcal{M}\). The updated model parameters are now  
\[\theta_{\mathcal{M}}=(\theta_{\text{feat}}^{*},\theta_{\text{head}}), \tag{4}\]  
where \(\theta_{\text{head}}\) are the parameters of the detection heads. We then fine-tune the detection head parameters \(\theta_{\text{head}}\) using the novel class dataset \(\mathcal{D}_{\text{novel}}\), which contains few samples \(\{(x_{j},y_{j})\}_{j=1}^{N_{\text{novel}}}\). Each detection head has the same loss function, defined as  
\[\mathcal{L}_{\text{novel}}=\sum_{j=1}^{N_{\text{novel}}}\big{(}\mathcal{L}_{ \text{RPN}}(x_{j},y_{j})+\mathcal{L}_{\text{cls}}(x_{j},y_{j})+\mathcal{L}_{ \text{reg}}(x_{j},y_{j})\big{)}. \tag{5}\]  
After fine-tuning, the resulting model \(\mathcal{M}^{*}\) is capable of effectively detecting novel class samples. This training strategy leverages the rich samples of the base class dataset for initial pretraining, integrates the MultiScale-DynaAttention and hierarchical adaptive-pyramid pooling modules, and fine-tunes with the novel class dataset, thereby achieving efficient detection of few-shot objects.