###### Abstract  
Agriculture is pivotal in national economies, with pest classification significantly influencing food quality and quantity. In recent years, pest classification methods based on deep learning have made progress. However, there are two problems with these methods. One is that there are few multi-scale pest detection algorithms, and they often lack effective global information integration and discriminative feature representation. The other is the lack of high-quality agricultural pest datasets, leading to insufficient training samples. To overcome these two limitations, we propose two methods called RS Transformer (a two-stage region proposal using Swin Transformer) and the Randomly Generated Stable Diffusion Dataset (RGSDD). Firstly, we found that the diffusion model can generate high-resolution images, so we developed a training strategy called the RGSDD, which was used to generate agricultural pest images and was mixed with real datasets for training. Secondly, RS Transformer uses Swin Transformer as the backbone to enhance the ability to extract global features, while reducing the computational burden of the previous Transformer. Finally, we added a region proposal network and ROI Align to form a two-stage training mode. The experimental results on the datasets show that RS Transformer has a better performance than the other models do. The RGSDD helps to improve the training accuracy of the model. Compared with methods of the same type, RS Transformer achieves up to 4.62% of improvement. S 2023 13 12206 12 October 202  
example of intelligent diagnosis. Research in respect of the classification and detection of agricultural pests is crucial to help farmers effectively manage crops and take timely measures to reduce the harm caused by pests. Object detection models, which come in one-stage and two-stage varieties, are frequently employed in pest classification and detection. One-stage models like YOLO [4; 5; 6] and SSD [7] are renowned for their rapid detection capabilities. In contrast, two-stage models like Fast R-CNN [8] and Faster R-CNN [9] excel in achieving high accuracy, albeit at a slower processing speed compared to their one-stage counterparts. The Transformer model [10] has many potential applications in AI. Based on its effectiveness in natural language processing (NLP) [11], recent research has extended the Transformer to the field of computer vision (CV) [12]. In 2021, Swin Transformer [13] was proposed as a universal backbone for CV, achieving the latest SOTA on multiple dense prediction benchmarks. The differences between language and vision make the transition from language to vision difficult, such as the vast range of visual entity scales. However, Swin Transformer can solve this problem well. In this paper, we use a Vision Transformer with a shift window to detect pests. Currently, two dataset-related issues affect pest detection. The first is the scarcity of high-quality datasets. There are only approximately 600 photos in eight pest datasets, reflecting the lack of agricultural pest datasets [14]. The second issue is the challenges involved in detecting pests at multiple scales. The size difference between large and microscopic pests is large, up to 30 times in some cases. For example, the relative size of the largest pest in the LMPD2020 dataset is 0.9%, while the relative size of the smallest pest is only 0.03%. When the size difference of the test object is large, it is difficult for the test results at multiple scales to achieve high accuracy simultaneously, and the problem of missing detection often occurs. Moreover, the Transformer also requires a large dataset for training. In agriculture, there are few high-quality pest datasets available, and some datasets from the internet have poor clarity and different sizes. In recent years, with the development of AI-generated content technology, increasing numbers of large models of image generation based on a text description have been developed.