## 3 Improved Algorithm Based on YOLOv5s
### Improvements Based on YOLOv5s and Swin Transformer  
The Transformer has been widely used in the field of NLP and has recently migrated to the field of computer vision, where it can be used for target detection, object classification, semantic segmentation, instance segmentation, and other computer vision tasks. The detection targets of this animal dataset are mostly medium-sized targets, so we apply Swin Transformer to this animal dataset, and the experimental results confirm that the application of the Transformer to this animal image block improves the detection accuracy and detection speed of the algorithm. Transformer was originally used to solve the natural language translation problem, and Vision Transformer [36] was suggested by [PERSON] et al. to use Transformer in the field of computer vision by segmenting an image into multiple image blocks and flattening each image block into a one-dimensional vector to be used as the input of Transformer to train the classification model in a supervised manner. The pre-training results of large data sets show that the accuracy exceeds the highest results of CNN [37; 38]. Swin Transformer [39] is a self-attentive mechanism for deep learning models, whose primary goal is to effectively interpret high-resolution images by breaking them up into smaller blocks and using hierarchical attention processes to gather both local and global contextual information. The following equation serves as a summary of the Swin Transformer's structure:  
\[SwinTR=T+S+P+FFN \tag{13}\]  
where \(T\) denotes the tokenization layer, \(S\) denotes the shifted window mechanism, \(P\) denotes the patch merging layer, \(M\) denotes the multi-scale architecture, and \(FFN\) stands for the feed-forward network. The tokenization layer segments the input image into a series of non-overlapping patches, which are then processed by a shift window mechanism to capture the contextual information of different regions of the image. The patch merging layer combines the features of different patches to create a uniform representation of the image, while the multi-scale architecture helps the model to detect objects of different sizes. Finally, the feedforward network performs the final classification and regression tasks to identify and localize objects in the image. Overall, Swin Transformer uses a sliding window approach to achieve cross-window connectivity in a hierarchical feature map for feature interaction, which can effectively capture local and global contextual information. Although the C3 module has the global perceptual field of the feature map, it is computationally intensive, which seriously affects the training and detection speed of the model, especially when the input image resolution is high. In this paper, we replace some C3 modules of the backbone and head with Swin Transformer modules, which can improve the computational speed and efficiency at the cost of minimizing the loss of perceptual field by using two window partitioning mechanisms alternately, and this structure has the flexibility of multi-scale modeling and takes into account the efficiency and characterization ability. Since most of the detection objects in this paper are large targets with relatively obvious features, other lightweight networks such as CBAM [40] need to focus on both channel and spatial dimensions to obtain more detailed attention information, which instead increases the number of parameters and computational effort to some extent. The improved Swin Transformer module in this paper has a multiscale architecture that allows flexible switching in the detection of small and large targets, while its unique sliding window mechanism enables cross-window connections in the hierarchical feature map, which can also effectively capture more extensive regional information without affecting the computational speed and efficiency. Figure 5 shows two consecutive Swin Transformer modules. While W-MSA is a conventional multi-headed self-attentive module, SW-MSA adds a moving window scheme to W-MSA.