# Laboratory Flame Smoke Detection Based on an Improved YOLOX Algorithm
## 2 Materials and Methods
### Swin Transformer
After the backbone network process, the output of each component is expressed as shown in Equations (1)-(4). \[\hat{X}^{l}=\text{W }-\text{ MSA}(\text{LN}(\text{X}^{l-1}))+X^{-1} \tag{1}\]  
\[X^{l}=\text{MLP}(\text{LN}(\hat{X}^{l}))+\hat{X}^{l} \tag{2}\]  
\[\hat{X}^{l+1}=\text{SW-MSA}(\text{LN}(\text{X}^{l}))+X^{l} \tag{3}\]  
\[X^{l+1}=\text{MLP}(\text{LN}(\hat{X}^{l+1}))+\hat{X}^{l+1} \tag{4}\]  
where \(\hat{X}^{l}\) and \(X^{l}\) denote the output features of the (S)W-MSA module and the MLP module for block \(l\), respectively; W-MSA and SW-MSA denote window-based multi-head self-attention using regular and shifted window partitioning configurations, respectively. Figure 4 depicts the total network architecture of the Swin Transformer. The input feature map is passed through a PatchPartition layer as the network develops, changing the smallest unit of the image from a pixel to a patch, with each patch being treated as a token, and the result is a three-dimensional matrix of H/4 \(\times\) W/4. The downsampling, resolution, channel number, and hierarchy are all controlled by the patch merging layer. Each set of 2 \(\times\) 2 patches' features is joined together by the first patch merging layer, which also connects a linear layer to the spliced features. These features are then processed by the Swin Transformer backbone structure. The output dimension is set to C and the number of tokens is maintained at H/4 \(\times\) W/4 in the process of \"stage1\". Then, in \"stage2\", the output dimension is set to 2C and the number of tokens is decreased by a factor of 4 times to H/8 \(\times\) W/8. After changing the output dimension to 2C, feature processing is carried out. Each stage, which is repeated six times and twice, denotes a level. \"stage2\" and \"stage1\" are repeated twice; \"stage3\" and \"stage4\" are similar to the previous stages, and the output is H/16 \(\times\) W/16 and H/32 \(\times\) W/32, respectively, and the output dimension is 4C and 8C, repeated 6 times and 2 times, respectively. Each stage represents a level. Similar to common convolutional neural networks like ResNet [18], this is a hierarchical structure. This makes it simple to adapt Swin Transformer's architecture to use the backbone network for various vision tasks. Figure 4: Swin Transformer network structure. The self-attentive mechanism is the key module of Transformer, which is calculated as shown in Equation (5). Where \(Q\), \(K\), and \(V\) are query, key, and value, respectively, and d is the number of query dimensions. \[\text{A}=\text{Attention}(Q,K,V)=\text{SoftMax}(QK^{T}/\sqrt{d})V \tag{5}\]  
Traditional Transformer structures typically use global self-attention to compute the relationship between one token and all other tokens, but because of the complexity increase caused by the global computation, they are unsuitable for vision problems that call for intensive prediction or the representation of high-resolution images with numerous token sets. Swin Transformer suggests computing self-attention within a local window for effective modeling. The windows are set up so that the photos are consistently segmented and without overlap. The window based on hxw patches has a much lower computational complexity than the window based on global attention.