# Residual Transformer YOLO for Detecting Multi-Scale Crowded Pedestrian
## 3 The Proposed Method
### Bottleneck Transformer Encoder Layer and Detect Head  
Transformer [22] signifies a pivotal advancement in deep learning designed to enhance NLP performance. Its remarkable capabilities inspire DETR [23] to integrate it into object detection. Transformer exhibits superior performance and potential in various vision tasks compared to traditional convolutional networks. However, Transformer comes with high computational and spatial complexity due to its self-attentive computation of global features, which directly relates to the number of feature pixels. To address this issue, combining Transformer with ResNet [24] reduces computational and spatial complexity, resulting in a more potent visual model compared to conventional convolutional network structures. BOTrans uses the global Multi-Head Self-Attention (MHSA) of the Transformer Encoder to replace the Conv layer of ResNet, reducing parameters and lowering latency. The structure of BOTrans is shown in Figure 4b. Resnet typically has 4 stages with strides (4, 8, 16, 3), and BOTrans is derived by replacing 3 groups of 3 \(\times\) 3 Conv layers in stage4 with MHSA, as detailed in Table 1. This substitution reduces BOTrans parameter count by 18% when compared to ResNet using a 1024 \(\times\) 1024 input size. BOTrans maintains an identical structure to ResNet, allowing seamless integration into object detection models. This integration enhances their feature extraction capabilities. In RT-YOLO, BOTrans focuses on various regions within the image, acquiring relationships and contexts between these regions. This mechanism helps capture the global context and semantic information of objects, leading to a more accurate differentiation of occluded object boundaries and precise localization of small-scale objects. Figure 4: The structure of Bottleneck layer. Figure 5 shows the structure of MHSA, which differs from the canonical MHSA in three main aspects. Firstly, the number of heads is reduced from 8 to 4. Secondly, the content-position encoding \(r\) is two-dimensional instead of one-dimensional, with \(R_{h}\) and \(R_{w}\) representing relative information in the vertical and horizontal directions. Thirdly, position encoding is embedded after the MHSA layer. MHSA multiplies the input image \(x\) with learnable parameters \(W_{Q}\), \(W_{K}\), and \(W_{V}\) matrices to obtain corresponding query matrix \(q\), matching matrix \(k\), and information matrix \(v\). In addition, relative position encoding \(\mathbf{r}\) is used in Self-Attention. By multiplying \(r^{T}\) with \(q\), position-related information is obtained, thereby integrating position sense into the attention calculation. This enables the model to consider spatial relationships between features and their positions, ultimately enhancing feature extraction efficiency and robustness. Subsequently, \(q\) is multiplied with \(k^{T}\) and then summed with position encoding \(r\). After softmax, it obtains weights for \(v\). These weights are used to calculate weighted relevance scores, which are the results of the self-attention computation. MHSA takes parallel computation between multi-heads and exchanges information by sharing parameters \(W\) and the features are learnt from different attention heads are combined to obtain the results. The multi-head computation can be calculated as follows:  
\[MultiHead(Q,K,V)=Concat(head_{1}\ldots,head_{n})W \tag{1}\]  
\[Where\ head_{i}=Attention\left(QW_{i}^{Q},KW_{i}^{K},VW_{i}^{V}\right)= softmax\left(\frac{Q_{i}K_{i}^{T}}{\sqrt{d_{k}}}\right)V_{i} \tag{2}\]  
Here, Equation (1) represents the result after multi-head self-attention computation, while Equation (2) represents the result of a single head's computation, where \"\(i\)\" represents the head number. \(Q\), \(K\), \(V\) are the query, matching, and information matrices generated based on the input image features, which is the set of \(q\), \(k\), and \(v\) mentioned above. \(d\) represents the length of vector \(k\).