# YOLOv7-3D: A Monocular 3D Traffic Object Detection Method from a Roadside Perspective
## 3 Proposed Method
### Model Structure
#### 3.2.2 Multi-Scale Spatial Attention Mechanism  
\[Y=K\cdot[R|T]\cdot X \tag{3}\]  
FPN (Feature Pyramid Network) is a feature pyramid network used for multi-scale object detection. It constructs multi-scale feature maps by adding extra lateral connections in the Backbone network. These lateral connections extract features from different levels of the Backbone feature maps and merge them into the feature maps of the previous layer. This way, FPN can obtain rich semantic information at different scales and provide feature maps with different resolutions. This is crucial for detecting objects of different scales, as objects may have varying sizes and proportions. In this paper, the original 3-layer FPN  
Figure 2: Schematic diagram of the seven degrees of freedom of the target. structure in the YOLOv7 algorithm is modified to a 5-layer structure to accommodate more scale variations of the targets. As shown in the diagram, the upsampling structure of the FPN feature pyramid consists of CBS, UpSample, Concat, CatConv, and the multi-scale spatial attention mechanism. Attention mechanisms are widely used to improve the performance of deep learning models by selectively focusing on relevant information and suppressing irrelevant or noisy information [38; 39; 40]. However, these attention mechanisms overlook the scale information of feature maps. This paper proposes a Multi-Scale Spatial Attention mechanism (MSSA), which adaptively adjusts the weights of features from P2 to P6. It increases the weights of scale features that are more beneficial for the recognition task while suppressing the weights of other scale features. Spatially, the model can focus on image textures and contextual information that are advantageous for the recognition task. The structure of MSSA is shown in the Figure 3. MSSA performs average pooling on the input 2D feature map \(G\in\mathbb{R}^{C\times(H\times W)}\), with pooling kernel sizes of \(K^{h}\) and \(K^{w}\), and strides of \(S^{h}\) and \(S^{w}\) along the horizontal and vertical directions, respectively. This process generates condensed features \(W^{c}_{avg}\) and \(H^{c}_{avg}\). Subsequently, the features from these two condensed layers are aggregated:  
(1) First, concatenate the compacted features. Since the dimensions of features \(W^{c}_{avg}\) and \(H^{c}_{avg}\) do not match, the width and height dimensions of feature \(H^{c}_{avg}\) need to be transposed before concatenating it with \(H^{c}_{avg}\) to obtain the feature map \(M\). (2) Second, set a hyperparameter \(r\) such that \(M\) is passed through a 2D convolution to obtain the feature map \(M^{1}\), where the number of channels changes from \(c\) to \(\frac{r}{r}\) In this paper, \(r\) is set, and the number of channels in \(M^{1}\) should not be less than 8. Then, insert a BN layer and a SiLU activation function to obtain the feature map \(M^{2}\). At this stage, \(M^{2}\) incorporates both the feature compaction of the input feature \(G\) along the \(x\)-axis and \(y\)-axis, allowing spatial information of the input feature \(G\) to interact. The mixed spatial information in \(M^{2}\) is then divided, transposed, and passed through another 2D convolution to restore the channel number to \(c\), resulting in \(W^{\prime}\) and \(H^{\prime}\). These two feature maps represent the spatial weights. Finally, element-wise multiplication is performed between \(W^{\prime}\), \(H^{\prime}\), and the corresponding elements of matrix \(G\) to obtain \(G^{\prime}\). This process combines the spatial weights in the input feature map, where the spatial weights beneficial for the recognition task are increased. In the diagram, \(\odot\) represents element-wise multiplication between matrices. Thus, the spatial information \(S\) and channel information \(C\) are adaptively adjusted. This process can be represented as:  
\[ \begin{split} G^{\prime}&=\sigma(f\otimes([AvgPoolW( \mathbf{G});AvgPoolH(\mathbf{G})]))\\ &=\sigma\Big{(}f\otimes\Big{(}\Big{[}W^{c}_{avg};H^{c}_{avg} \Big{]}\Big{)}\Big{)}\end{split}