# Integrating Prior Knowledge into Attention for Ship Detection in SAR Images
## 4 Combining Prior Knowledge Map
### Integrating Prior Knowledge Map  
Traditional attention mechanisms (such as SENet) serve as self-attention, which have one input tensor. In our work, there are two input tensors: one is for the resized prior knowledge map; and the other is the feature map of CNNs. The output tensor is considered as a better representation of the feature map of CNNs. The output is fed into latter stages of the CNNs.  
Our BDAM extends the structure of CBAM. The detailed structure of BDAM is shown in Figure 6. Given an intermediate feature map, CBAM (depicted in Figure 6a) successively infers attention maps along two separate dimensions, channel and spatial. After that, the attention maps are multiplied to the input feature map for adaptive feature refinement. Our module (BDAM) uses the CNN feature map for channel attention and the prior knowledge map for spatial attention (Brightness and Density Spatial Attention Mechanism, BDSM), which is shown in Figure 6b.  
The intermediate CNN feature map \(\mathbf{F}\in\mathbb{R}^{C\times H\times W}\)and the prior knowledge map \(\mathbf{P}\in\mathbb{R}^{C\times H^{\prime}\times W^{\prime}}\) are used as the input of BDAM. \(\mathbf{F}\) is passed through the channel attention mechanism to obtain the channel attention weight \(\mathbf{M_{c}}\in\mathbb{R}^{C\times 1\times 1}\), while \(\mathbf{P}\) is passed through the spatial attention mechanism to obtain the spatial attention weight \(\mathbf{M_{s}}\in\mathbb{R}^{1\times H\times W}\). The channel attention weight and spatial attention weight are multiplied to get the final attention weight \(\mathbf{M}\in\mathbb{R}^{C\times H\times W}\). This process can be described as:  
\[\mathbf{M} =\mathbf{M_{c}}(\mathbf{F})\otimes\mathbf{M_{s}}(\mathbf{P}) \tag{5}\] \[\mathbf{F^{\prime}} =\mathbf{M}\otimes\mathbf{F}\]  
Figure 5: The comprehensive architecture of our framework. It consists of backbone, BDAM, and a prior knowledge map.  
where \(\otimes\) represents the element-wise multiplication, and \(\mathbf{F}^{\prime}\) is the feature map after adjusting the weights.  
The channel attention module can be divided into two branches: max-pooling and average-pooling. Max-pooling and average-pooling operations are performed on the CNN  
Figure 6: Comparison of CBAM and BDAM. (**a**) CBAM. (**b**) BDAM.  
Figure 7: The schema of resizing the prior knowledge map. Details include regional division, core point statistics, assigning the initial weight, and weight normalization.  
feature maps to obtain \(\mathbf{F_{max}}\) and \(\mathbf{F_{avg}}\). \(\mathbf{F_{max}}\) and \(\mathbf{F_{avg}}\) are fed into the multilayer perceptron (MLP), and the two results output by the MLP are summed element-wise. The merged tensor is finally passed through the Sigmoid function to obtain the channel weights. This process can be described as:  
\[\mathbf{M_{c}}(\mathbf{F})=Sigmoid(\mathbf{W_{0}}(\mathbf{W_{1}}(\mathbf{F_{ max}}))+\mathbf{W_{0}}(\mathbf{W_{1}}(\mathbf{F_{avg}}))) \tag{6}\]  
where \(\mathbf{W_{0}}\in\mathbb{R}^{C\times C/r}\),\(\mathbf{W_{1}}\in\mathbb{R}^{C/r\times C}\) represent the weights of the MLP, and \(r\) is the reduction ratio.  
In the BDSM, the prior knowledge map is first resized by a multilayer convolutional neural network. The resized prior knowledge map is subjected to maxi-pooling and average-pooling operations in the channel direction. Then, the outputs \(\mathbf{P_{max}}\) and \(\mathbf{P_{avg}}\) of the two branches are concatenated and fed into a convolutional layer with a convolutional kernel of size \(7\times 7\). The spatial attention weights are calculated as:  
\[\mathbf{M_{s}}(\mathbf{P}) =Sigmoid(f([MaxPool(\mathbf{W}(\mathbf{P}));AvgPool(\mathbf{W}( \mathbf{P})]) \tag{7}\] \[=Sigmoid(f([\mathbf{P_{max}};\mathbf{P_{avg}}]))\]  
where \(\mathbf{W}\) represents the weights of the initial layers of convolution and \(f\) represents the last layer of convolution.