## 3 Methods
### Swin Transformer
In contrast, when M is fixed, W-MSA is set to 7 by default and has linear complexity (with h \(\times\) w patch tokens, each patch token computes \(M^{2}\) times within its respective local window). The immense h \(\times\) w is unbearable for global self-attention computation, while window-based self-attention (W-MSA) has good scalability. These stages progressively process the image to extract and refine features. Patch merging layer: This layer merges each 2 \(\times\) 2 pixel block into a new patch and concatenates in the depth direction to downsample the feature map, halving its height and width while doubling the feature depth. Then, LayerNorm and a fully connected layer are used to adjust the feature depth further, optimizing the feature representation. The Swin Transformer block: This block includes W-MSA (window multi-head self-attention) and SW-MSA (shifted window multi-head self-attention) structures. The W-MSA module divides the feature map into windows of size MxM and performs self-attention processing within each window independently. However, W-MSA only processes self-attention within each window, meaning there is no information exchange between windows. To address this limitation, the SW-MSA module is introduced, allowing information to be exchanged between windows and enhancing the overall information flow in the model. The model can effectively reduce the computational load through this structural design while ensuring effective information transfer between different stages, enhancing feature expression capabilities and model performance. The patch merging layer, W-MSA, and SW-MSA modules are shown in Figure 5:  
Figure 4: Structure of the Swin Transformer block. By replacing the traditional ResNet50 with Swin Transformer as the network backbone, the model's ability to handle complex image scenes has been significantly enhanced, particularly demonstrating outstanding performance in processing dense and stacked tomato images within the dataset. Swin Transformer adopts a hierarchical Transformer structure, enabling it to capture richer global dependencies and contextual information, which is crucial for understanding and analyzing densely stacked object scenes. Furthermore, the structure of the Swin Transformer allows for feature processing at multiple resolutions, achieved through intra-window self-attention mechanisms and inter-window connections. This flexibility supports multi-scale feature extraction, essential for handling objects of different sizes in object detection tasks. Compared to traditional convolutional networks based on fixed convolutional kernel structures, Swin Transformer is not constrained by convolution operations, thus allowing for more flexible learning of underlying patterns and complex geometric structures within the data. This replacement of the backbone network significantly improves the model's feature extraction capability, leading to a substantial enhancement in the model's accuracy and efficiency in practical applications. This improvement strengthens the model's ability to handle complex scenes and enhances its adaptability and accuracy across various application scenarios, particularly excelling in challenging object detection and image recognition tasks.