## 3 Materials and Methods
### Improved YOLOv8 Model
#### 3.4.1 Swin Transformer  
In object detection tasks, targets in images often have different scales, and this scale difference may be due to the distance of the object in the image, mutual occlusion between objects, or the size difference of the object itself. Due to the different scale sizes of targets, effective object detection becomes more challenging. Therefore, we introduced the Swin Transformer module in the backbone part of the network to enhance the network's ability to extract features of objects of different scales. Swin Transformer [42] is a novel visual transformer architecture that solves the computational and memory bottleneck problems of traditional transformers in high-resolution image processing by introducing a local window self-attention mechanism and a shift operation. It divides the image into multiple fixed-size windows and computes self-attention within each window to reduce computational complexity, while shifting the windows allows features between different windows to communicate with each other, effectively  
Figure 3: The model structure of improved YOLOv8. modeling long-range dependencies. Swin Transformer also adopts a hierarchical design to gradually reduce the resolution of the feature map and capture features at different scales, and this design can improve both computational efficiency and memory bottleneck in traditional transformers. This design improves computational efficiency and retains the ability to model global information, which is widely used in image classification, target detection, semantic segmentation, and other visual tasks, and shows excellent performance and efficiency. The structure of Swin Transformer is shown in Figure 4a. It is divided into four stages. Before entering Stage 1, the input image is split into non-overlapping, equal-sized patches by Patch Partition, and each patch is regarded as a \"token\", and then a feature sequence is obtained. This feature sequence is then sent to the linear embedding layer for projection to obtain a new feature dimension, and after two Swin Transformer blocks for self-attention computation, it enters Stage 2. In Stage 2, the feature dimension is first reduced by patch fusion, and then the feature is transferred to a deeper layer after two Swin Transformer Blocks to compute the attention. Then go to Stage 3 and Stage 4 sequentially to get the final features. In the latter two stages, the features are fused by patch fusion, and then the Swin Transformer Block calculates the self-attention to transform the features to deeper layers. The difference is that in Stage 3, six Swin Transformer Blocks (two of them in a group) are passed through. Each stage changes the dimensionality of the sequence, resulting in a hierarchical representation. Through this hierarchical multi-scale feature representation, Swin Transformer is able to effectively capture the information at different scales in the image and improve the performance of the model. The C2f module in YOLOv8 excels in local feature extraction but lacks in capturing global context information and is less effective when dealing with complex backgrounds or lighting changes. The Swin Transformer module has hierarchical feature extraction capabilities and can effectively extract and integrate information on multiple scales, which is particularly important for processing tomatoes at various maturity stages. Moreover, Swin Transformer shows good stability and robustness when handling complex backgrounds  
Figure 4: (**a**) The architecture of a Swin Transformer (Swin-T) [42]; (**b**) two successive Swin. W-MSA and SW-MSA are multi-head self attention modules with regular and shifted windowing configurations, respectively, in Transformer Blocks. or lighting conditions. Therefore, this study introduces a Swin Transformer module in the high-level feature processing stage of the backbone section and retains the C2f module in the low-level feature extraction stage.