A method for establishing a fast locust detection model based on a Transformer architecture
By using a locust detection model based on the Transformer architecture and employing an adaptive attention mechanism and dynamic head structure, the problems of misjudgment and small target detection in locust identification are solved, achieving efficient and accurate locust identification and real-time monitoring.
Patent Information
- Application Number
- CN202410608615.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-16
- Publication Date
- 2026-11-27
- Estimated Expiration
- 2044-05-16
AI Technical Summary
Existing locust identification technologies are prone to misjudgment or missed detection in complex farmland environments, especially in capturing the characteristic information of small locusts, and the problem of sample imbalance affects the identification effect.
A fast locust detection model based on the Transformer architecture is adopted. Through adaptive attention mechanism, dynamic head structure and advanced loss function, combined with data augmentation and model optimization, the locusts can be effectively detected.
It improves the recognition accuracy in complex backgrounds and small object detection, alleviates the sample imbalance problem, and enables real-time monitoring and prevention of locust plagues.
Smart Images

Figure CN118411737B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of image recognition technology, and in particular to a method for establishing a fast locust detection model based on the Transformer architecture. Background Technology
[0002] In the field of agricultural pest monitoring and control, locusts, as a serious pest, pose a significant threat to crop growth. To effectively combat locust plagues, researchers have been actively exploring efficient and accurate locust identification technologies. In recent years, the rapid development of deep learning technology, especially breakthroughs in image recognition, has provided new solutions for locust monitoring.
[0003] However, existing locust identification technologies still face many challenges in practical applications. First, in complex farmland environments, locusts often closely resemble the background in terms of color and texture, leading to frequent misidentification or missed detections by traditional algorithms. Second, for smaller locusts, due to their relatively limited feature information, existing technologies often struggle to capture them accurately, thus affecting identification accuracy. Furthermore, sample imbalance is also a significant factor limiting locust identification performance. In practical applications, the large differences in the number of samples from different categories result in the model's weaker ability to identify a few categories, consequently impacting the overall identification performance.
[0004] To overcome these challenges, researchers have begun to focus on the application of the Transformer architecture in image recognition. Through its unique self-attention mechanism, the Transformer model can comprehensively capture global contextual information in an image, offering significant advantages in handling complex backgrounds and small object detection tasks. However, when directly applying the Transformer architecture to locust recognition tasks, corresponding optimizations and improvements are still needed to fully leverage its performance advantages, taking into account the specific characteristics of locust recognition. Summary of the Invention
[0005] The purpose of this invention is to provide a method for establishing a fast locust detection model based on the Transformer architecture, so as to solve the problems mentioned in the background art.
[0006] To achieve the above objectives, this invention provides a method for establishing a fast locust detection model based on the Transformer architecture, comprising the following steps:
[0007] S1. Data collection: Images of locusts are collected using various devices in different natural environments;
[0008] S2. Data annotation: The collected images are annotated with data. The data annotation process includes preliminary annotation and annotation review.
[0009] S3. Data preprocessing: Normalization, denoising and data augmentation are used to preprocess the image. Denoising includes filtering, and data augmentation includes image rotation, image scaling and image flipping.
[0010] S4. The structure design of the locust detection model based on the Transformer architecture includes an adaptive attention mechanism, a dynamic head structure, and an advanced loss function. The Transformer architecture is used to extract features from the image, and the adaptive attention mechanism and dynamic head structure are used to further process the features. The improved attention advanced loss function is then used to train the locust detection model.
[0011] S5, mobile platform deployment, including pruning the locust detection model, quantizing the locust detection model, distilling the locust detection model, selecting TensorFlow Lite as the deployment framework for the locust detection model, and developing mobile applications.
[0012] Preferably, the various devices in S1 include high-resolution digital cameras, drones, and mobile phones, and the images they capture all have a resolution of 1280*720 or higher, and the different natural environments include grasslands, deserts, and farmlands.
[0013] Preferably, the latest version of the open-source annotation tool LabelImg is used in S2.
[0014] Preferably, the image scaling in S3 uses a bicubic interpolation function. For an image I with dimensions H*W*C, the scaling mathematical expression is as follows:
[0015]
[0016] Where h and W are the interpolation functions in the height and width directions, respectively, i and j are the coordinates on the scaled image, k is the channel exponent, and I' represents the pixel value at position (i,j) on the k-th channel of the output image after convolution.
[0017] For an image I with pixel value P, the normalized mathematical representation is:
[0018]
[0019] Data augmentation employs three methods: Cutout, CutMix, and Mixup. For an image I and a selected region R, CutOut is represented as follows:
[0020]
[0021] Let the two images be I1 and I2, and the selected region be R. CutMix is represented as follows:
[0022]
[0023] Mixup performs the following mixing operation on two images, I1 and I2, and their labels y1 and y2, by selecting a mixing coefficient λ;
[0024] I′=λ×I1+(1-λ)×I2
[0025] y'=λ×y1+(1-λ)×y2.
[0026] Preferably, S4 specifically includes:
[0027] a) Divide the input image into several small blocks, reduce the dimensionality of each block to a one-dimensional vector, and concatenate all the one-dimensional vectors into a sequence and input it into the Transformer. Suppose the image is divided into P*P blocks, then the length of the sequence input into the Transformer is P. 2 Add a token-like identifier to the beginning of the sequence, with the following formula:
[0028]
[0029] Where I i Let represent the i-th small segmented region, and Flatten denotes the flattening operation;
[0030] b. Adaptive resolution Transformer is used. At the bottom of the locust detection model, a large patch size and a small sequence length are used to capture contextual information. At the top of the locust detection model, a small patch and a large sequence length are used to generate detailed feature maps.
[0031] c. A self-attention mechanism based on the Transformer structure is used to handle the target of multiple sequence segmentations, as expressed by the following formula:
[0032]
[0033] Where Q, K, and V represent query, key, and value, respectively, d represents dimension, and Attention represents attention function;
[0034] Based on the self-attention mechanism, an additional attention weight is introduced for each patch, which is learned through a single-layer neural network. The formula is as follows:
[0035] A=σ(W a ·X+b a )
[0036] Where X is the original feature, W a and b aThese are the weights and biases of the neural network, respectively, where σ is the Sigmoid function and A is the obtained attention weights.
[0037] d. Multiply the attention weights by the original features to obtain the final feature representation, as follows:
[0038] F′=A·F
[0039] Where F represents the original feature and F' represents the final feature representation;
[0040] e. Dynamic header structure: The structure of the feature extraction method is dynamically adjusted according to the size of the target. A set of convolutional kernels of different sizes are pre-set, and each convolutional kernel has a separate convolutional branch. In the forward propagation process, the convolutional branch corresponding to the target size is selected for calculation. The specific selection method is as follows:
[0041]
[0042] Among them, h i S represents the output feature at position i. i To select weights, which determine the influence of the j-th convolutional branch at position i, Conv j (X) represents the output of the j-th convolutional branch; operator S ij The selection weights are generated by the gating mechanism, and their mathematical representation is as follows:
[0043]
[0044] Among them, G ij and G ik All scores are calculated by a gating network, and the features at position i and the metadata of the convolutional branch j are received as input.
[0045] f. The advanced loss function adds sample difficulty to the original Focal loss function, as shown in the following expression:
[0046] AFL(p t ,d)=-(1-p t ) γ log(p t )d α
[0047] Where, p t denoted as the prediction probability, γ represents the tuning parameter used to adjust the weights of positive and negative samples in the loss, d represents the sample difficulty, and α represents the tuning parameter used to adjust the weights of different sample difficulties in the loss.
[0048] g. The locust detection model was trained using the experimental platform. The learning rate hyperparameter was set to 0.001, and a learning rate decay strategy was introduced.
[0049] Preferably, quantizing the locust detection model in S5 involves reducing the parameters of the locust detection model from 32-bit floating-point numbers to 8-bit integers.
[0050] Therefore, the present invention employs the above-described method for establishing a fast locust detection model based on the Transformer architecture, which has the following beneficial effects:
[0051] (1) Fully utilize the self-attention mechanism of the Transformer architecture to effectively model the global context of locusts, thereby capturing the feature information of locusts more accurately. By designing an adaptive attention mechanism and a dynamic head, the model can automatically adjust the region of interest according to the characteristics of the input image, further improving its performance in detecting complex backgrounds and small objects;
[0052] (2) An advanced focus loss function is introduced. By weighting samples of different categories, the model’s ability to identify samples of different categories is balanced, thereby effectively mitigating the impact of sample imbalance on model performance.
[0053] (3) Not only does it improve recognition performance at the algorithm level, but it also takes into account the needs of practical applications. By verifying the effectiveness of the model in large-scale experiments and realizing real-time monitoring on mobile computing platforms, this invention provides a new technical means for locust early warning and prevention, which helps to detect and control locust disasters in a timely manner.
[0054] The technical solution of the present invention will be further described in detail below with reference to the accompanying drawings and embodiments. Attached Figure Description
[0055] Figure 1 This is a dataset sample from an embodiment of the present invention;
[0056] Figure 2 This is a schematic diagram of the annotation of the dataset in an embodiment of the present invention;
[0057] Figure 3 This is a schematic diagram of image preprocessing according to an embodiment of the present invention;
[0058] Figure 4 This is a schematic diagram of image enhancement according to an embodiment of the present invention;
[0059] Figure 5 This is a schematic diagram of the Transformer model according to an embodiment of the present invention;
[0060] Figure 6 This is a schematic diagram of the framework of a locust detection model using a Transformer structure with a self-attention mechanism, as described in an embodiment of the present invention.
[0061] Figure 7These are two architecture diagrams illustrating the application of Transformer in target detection according to embodiments of the present invention;
[0062] Figure 8 This is a visualization of the locust detection results in an embodiment of the present invention;
[0063] Figure 9 These are different agricultural pest datasets for embodiments of the present invention. Detailed Implementation
[0064] Example
[0065] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the scope of protection of the present invention.
[0066] This invention discloses a method for establishing a fast locust detection model based on the Transformer architecture, comprising the following steps:
[0067] S1. Data collection: Images of locusts are collected using various devices in different natural environments, such as... Figure 1 .
[0068] Data collection included a large number of locust images collected from the wild. To ensure the richness and diversity of the data, various devices were used for data acquisition, including but not limited to high-resolution digital cameras, drones, and mobile phones. To ensure image clarity, the resolution of all acquired images was guaranteed to be above 1280*720. To collect locust images with species diversity and coverage of a wide geographical distribution, the images were collected from different natural environments, including grasslands, deserts, and farmlands.
[0069] S2. Data annotation: This involves annotating the collected images. The data annotation process includes initial annotation and annotation review, such as... Figure 2 .
[0070] Data annotation is a crucial step in training locust detection models. By accurately annotating locusts in images, the model receives the supervisory information it needs to learn.
[0071] Data annotation utilizes the latest version of the open-source annotation tool LabelImg. As a graphical image annotation tool, LabelImg features a user-friendly interface and simple operation, allowing for easy drawing of bounding boxes and provision of corresponding labels on images, thus achieving accurate annotation of target objects within the image. Furthermore, the latest version of LabelImg has been optimized in terms of functionality, performance, and stability, better meeting image annotation requirements.
[0072] The annotation process includes preliminary annotation and annotation review. Preliminary annotation is performed by an experienced agricultural pest expert who marks target objects (such as locusts) and draws corresponding bounding boxes. Annotation review is conducted by another expert who reviews the annotation results to ensure the accuracy and completeness of the annotations. Annotations with problems are corrected or re-annotated.
[0073] S3. Data preprocessing: Image preprocessing includes normalization, denoising, and data augmentation. Denoising includes filtering, and data augmentation includes image rotation, scaling, and flipping. Figure 3 , Figure 4 .
[0074] Data preprocessing is a necessary step before model training, aiming to improve the model's generalization ability and training efficiency. This embodiment employs techniques such as normalization, denoising, and enhancement to preprocess the images. Normalization maps the pixel values of an image to the same range, eliminating differences in brightness, contrast, etc., between different images; denoising removes noise interference from the image through filtering and other methods, improving image quality; enhancement increases image diversity through rotation, scaling, and flipping, improving the model's robustness.
[0075] Image scaling is performed using a bicubic interpolation function. For an image I of size H*W*C, the scaling mathematical expression is as follows:
[0076]
[0077] Where h and W are the interpolation functions in the height and width directions, respectively, i and j are the coordinates on the scaled image, and k is the channel index.
[0078] Image color normalization helps prevent the model from being affected by large pixel values. For an image I with pixel values P, the normalization mathematical expression is:
[0079]
[0080] Because locusts have diverse distributions in their natural environment, in addition to basic data preprocessing, this embodiment employs three advanced data augmentation methods: Cutout, CutMix, and Mixup. These methods generate new training samples through random operations, further enhancing data diversity and the model's ability to learn from a wider range of data, thereby improving the model's generalization ability.
[0081] CutOut is a simple and effective data augmentation method designed to encourage models to focus more on local features of an image rather than relying on the entire image, thus preventing overfitting and enhancing the model's generalization ability to some extent. Its core idea is to randomly select a region in the image and set all its pixel values to 0, effectively removing that region. For an image I and a selected region R, CutOut can be represented as follows:
[0082]
[0083] CutMix is an innovative data augmentation method based on CutOut. It first randomly selects a region from one image and then replaces that region with a corresponding region from another image. Assuming the two images are I1 and I2, and the selected region is R, this method can be represented as follows:
[0084]
[0085] Mixup is an advanced data augmentation method that generates new training samples that do not exist in the original data distribution. Its main idea is to linearly mix two images and their labels. For two images I1 and I2 and their labels y1 and y2, a mixing coefficient λ is chosen, and then the following mixing operation is performed:
[0086] I′=λ×I1+(1-λ)×I2
[0087] y'=λ×y1+(1-λ)×y2
[0088] S4, such as Figure 5-7 The locust detection model is designed based on the Transformer architecture, including an adaptive attention mechanism, a dynamic head structure, and a high-level loss function. The Transformer architecture is used to extract features from the image, and the adaptive attention mechanism and dynamic head structure are used to further process the features. Finally, the improved attention-based high-level loss function is used to train the locust detection model.
[0089] Specifically, the model first extracts features from the image using a Transformer structure, then further processes these features using an adaptive attention mechanism and a dynamic head structure, and finally trains the model using an improved Focal loss function. This model structure design effectively addresses complex backgrounds, small targets, and imbalanced behavior in locust images.
[0090] The Transformer architecture is designed and improved in detail based on the characteristics of the locust target detection task. Firstly, regarding how to segment and transmit the image sequence, the structure divides the input image into several small blocks, each block is reduced to a one-dimensional vector, and then these one-dimensional vectors are concatenated into a sequence and input into the Transformer. Specifically, assuming the image is segmented into P*P blocks, the length of the sequence input to the Transformer is P. 2 In addition, a special class token is added at the beginning of the sequence for the final classification task. This process can be expressed by the following formula:
[0091]
[0092] In the formula I i Let represent the i-th small segmentation region, and Flatten denote the flattening operation. To address the problem of small targets such as locusts, an adaptive resolution Transformer is employed. Specifically, at the bottom of the model, a larger patch size and a smaller sequence length are used to capture extensive contextual information; at the top of the model, a smaller patch size and a larger sequence length are used to generate more detailed feature maps. This allows the model to simultaneously consider global contextual information and local details, effectively solving the small target problem. Finally, a self-attention mechanism based on the Transformer structure is used to handle targets segmented from multiple sequences. The self-attention mechanism can capture the dependencies between any two positions in a sequence. Therefore, even if a target is segmented into multiple sequences, the model can integrate the information from these sequences through the self-attention mechanism, thereby correctly detecting the target. This process can be represented as follows:
[0093]
[0094] Where Q, K, and V represent query, key, and value, respectively, d represents dimension, and Attention represents attention function.
[0095] To better address the locust detection problem in complex backgrounds, this embodiment designs an adaptive attention mechanism, or AAM. The main goal of the adaptive attention mechanism is to guide the model to pay more attention to important information regions (such as the location of the locusts), thereby reducing interference from complex backgrounds. Its design details are as follows:
[0096] Based on the self-attention mechanism, an additional attention weight is introduced for each patch, which is learned through a single-layer neural network. The formula is as follows:
[0097] A=σ(W a ·X+b a )
[0098] Where X is the original feature, W a and b a Here, σ represents the weights and biases of the neural network, σ is the sigmoid function, and A is the obtained attention weights. Then, the attention weights are multiplied by the original features to obtain the final feature representation, as follows:
[0099] F′=A·F
[0100] Where F represents the original feature and F' represents the final feature representation. As can be seen from the formula, this process essentially adjusts the weights of the original features, enabling the model to automatically learn the importance of each position and adjust the features accordingly.
[0101] To address the common problem of small target detection in locust detection, this embodiment introduces a dynamic head structure. This structure dynamically adjusts the feature extraction method's structure based on the target size, enabling the extraction of more distinct features regardless of their size. The structural design details are as follows: A set of convolutional kernels of different sizes is pre-defined, each with a separate convolutional branch. During the forward propagation process, the convolutional branch corresponding to the target size is selected for calculation. The specific selection method is as follows:
[0102]
[0103] In the formula, h i S represents the output feature at position i. i To select weights, the influence of the j-th convolutional branch at position i is determined, Conv j (X) represents the output of the j-th convolutional branch; operator S ij The selection weights are generated due to the gating mechanism, and their mathematical representation is as follows:
[0104]
[0105] Among them, G ij It is calculated by a gating network, receiving the features at position i and the metadata of the convolutional branch j as input.
[0106] The improved Focal loss function (AFL) addresses the shortcomings of the original Focal loss function in handling class imbalance. This loss function is built upon the original Focal Loss Function by incorporating the concept of sample difficulty to enhance the model's ability to learn from difficult samples. The original Focal Loss Function is defined as follows:
[0107] FL(p i ,d)=-(1-p t ) γ log(p i )
[0108] Where pi represents the predicted probability, and γ represents the tuning parameter used to adjust the weights of positive and negative samples in the loss function. As γ increases, the loss function tends to focus on samples that are difficult to classify. However, the original Focal Loss Function only considers the sample category and ignores the impact of sample difficulty on model training. Therefore, this embodiment introduces the concept of sample difficulty to design the Advanced Focal Loss Function:
[0109] AFL(p i ,d)=-(1-p t ) γ log(p i )d α
[0110] In the formula, d represents the sample difficulty, and α represents the tuning parameter used in the loss function to adjust the weights of different sample difficulties. As α increases, the loss function tends to focus on samples with greater difficulty. This is to better handle the problems caused by classification imbalance and different sample difficulties.
[0111] Model training, through careful selection and tuning of hyperparameters and combined with an efficient experimental platform, aims to maximize model performance, ensure the stability of the training process, and obtain high-quality training results within a limited time.
[0112] In this embodiment, we first set a key learning rate hyperparameter, which, after multiple experimental verifications, was set to 0.001. This value effectively avoids oscillations during training without slowing down the model's learning process, ensuring the model's efficiency and stability during training. Furthermore, this embodiment introduces a learning rate decay strategy: when the validation loss does not show a significant decrease over several consecutive training epochs, the learning rate is automatically halved. This strategy helps to better control the model's training process and effectively prevents overfitting.
[0113] In this embodiment, the Adam optimizer is used. The Adam optimizer combines the advantages of RMSProp and Momentum, adaptively adjusting the learning rate while considering both historical and current gradient information, thus better adapting to the model's learning process. In this embodiment, the parameters beta1 and beta2 of the Adam optimizer are set to 0.9 and 0.999, respectively. These are the standard settings for the Adam optimizer and the optimal parameters obtained through multiple experiments, ensuring better training results while maintaining model training efficiency.
[0114] Regarding the batch size selection, this embodiment sets the batch size to 32 based on GPU memory size and model complexity. This setting ensures sufficient memory during training while also improving the model's generalization ability to some extent. Through comparative experiments, we found that the model performs optimally when the batch size is 32.
[0115] To support efficient model training, this embodiment uses an NVIDIA Tesla V100 GPU as the experimental platform. This GPU boasts high performance, large memory, and powerful computing capabilities, fully meeting the requirements of deep learning model training. Furthermore, this embodiment also utilizes the Python deep learning framework PyTorch for model building and training. Leveraging PyTorch's rich API and flexible model building methods, it achieves high efficiency and convenience in model training. In addition, this embodiment utilizes the CUDA and cuDNN libraries to further accelerate the model computation process on the GPU.
[0116] S5, mobile platform deployment, including pruning the locust detection model, quantizing the locust detection model, distilling the locust detection model, selecting TensorFlow Lite as the deployment framework for the locust detection model, and developing mobile applications.
[0117] A series of lightweight processing strategies were employed to ensure the model's efficient operation on resource-constrained mobile devices. First, the model was pruned by removing unimportant parameters or layers to reduce its size and computational cost. Second, the model was quantized, reducing the model parameters from 32-bit floating-point numbers to 8-bit integers, further minimizing storage and computational requirements. Finally, knowledge distillation was used to train a compact student model to simulate the behavior of the original model, achieving further model compression.
[0118] In the lightweighting process, this embodiment fully considers the preservation of model performance. After each lightweighting step, the lightweight model is fully trained and validated to ensure that the model maintains a small size and low computational complexity without a significant performance degradation.
[0119] For the specific implementation of model deployment, this embodiment selects TensorFlow Lite as the deployment framework. TensorFlow Lite is a lightweight deep learning framework designed for mobile and embedded devices, supporting various hardware accelerations to enable models to run more efficiently on mobile devices.
[0120] This embodiment describes the development of an Android-based mobile application. The application utilizes the smartphone's camera to monitor farmland in real time. By invoking a locust detection model deployed on the mobile device, the application can process video streams and pinpoint potential pest locations. Farmers can then easily view their farmland in real time via their smartphones, enabling them to promptly detect and address pest problems.
[0121] Through lightweighting and deployment on mobile devices, the model in this embodiment performs excellently on smartphones. It processes one frame of image in less than 300 milliseconds, meeting the requirements of real-time processing. Although lightweighting may lead to a slight decrease in model accuracy, this embodiment, through careful design and optimization, ensures that the model's precision, recall, and mAP remain at 0.88, 0.86, and 0.87 respectively, meeting the needs of practical applications.
[0122] To compare the performance of the models, this embodiment proposes seven specific implementations, including dataset collection, visualization of detection results and small target locust detection test, comparative study of data augmentation methods, verification of adaptive attention mechanism, verification of dynamic head design, verification of the effect of advanced focus loss function, and verification of model generalization ability (comparison of different datasets).
[0123] A1. Data Collection
[0124] This embodiment focuses on constructing a model training dataset. The images in the dataset are primarily sourced from field photography. Considering the diversity and wide distribution of locust species, images were collected from various natural environments, including desert regions of Inner Mongolia, farmland in North China, and grasslands in Southwest China, based on field investigations and photography across multiple regions nationwide. The aim is to ensure the dataset covers as many environmental conditions as possible where locusts may appear. Furthermore, the image capture was conducted 24 hours a day, including morning, noon, and evening, taking into account different weather conditions such as sunny, cloudy, and rainy days. The collected image samples are as follows: Figure 1 As shown.
[0125] A2. Visualization of detection results and detection test of small target locusts:
[0126] The main purpose of this embodiment is to verify the effectiveness and superiority of the proposed transformer-based fast locust identification model. This model demonstrates high accuracy, recall, and average precision in handling agricultural pest detection problems, particularly in recognizing complex backgrounds and small targets. By comparing the performance of different models, we hope to clearly demonstrate the advantages of the proposed model over other models, thereby laying the foundation for further improvement of agricultural pest identification and detection technology. The experimental results are shown in the table below:
[0127] Table 1 Comparison of detection performance of different models on the test set
[0128]
[0129] It can be observed that the proposed model outperforms other models in terms of accuracy, recall, and mean precision. Particularly noteworthy is its high mean precision of 0.90, significantly surpassing other models. This is primarily due to its superior handling of small targets and complex background recognition. The model incorporates an adaptive attention mechanism and a dynamic head design, enabling it to better focus on "hard-to-identify" parts of the image, effectively extracting features and recognizing targets. Furthermore, an improved FocalLoss function is employed, which makes the model pay more attention to "hard-to-classify" samples during training, thereby enhancing its recognition ability. These design elements contribute to the model's excellent performance in experiments.
[0130] To more intuitively demonstrate and understand the performance advantages of the proposed model, the detection images of all models are compared, such as... Figure 8 As shown in the figure. Analysis and comparison of the visualization results show that the model has significant advantages in handling small-target locusts and scenes where the locusts are very similar to the background. In the figure, A is the visualization result of our model, B is the visualization result of the EfficientDet model, C is the visualization result of YOLOv5, E is the visualization result of YOLOv3, and F is the visualization result of the SSD model.
[0131] A3. Comparative Study of Data Augmentation Methods:
[0132] Data augmentation, as a key technique for improving model generalization performance and robustness, plays a crucial role in deep learning. In this study, we employed various data augmentation methods, including random cropping, rotation, flipping, scaling, and adjustments to brightness and contrast, to enrich the model's training samples and enhance its recognition capabilities.
[0133] To delve into the specific impact of each data augmentation method on model performance, this embodiment employs an ablation study approach. By removing each data augmentation strategy one by one and comparing the changes in model performance, we can precisely determine which strategies contribute the most to improving model performance.
[0134] Table 2 Comparative Study of Different Data Augmentation Methods
[0135]
[0136] As shown in Table 3, we have recorded the performance of the model under different data augmentation strategies. Without any data augmentation methods, the model's Precision, Recall, and mAP are all at low levels, at 0.85, 0.82, and 0.83, respectively. This is mainly attributed to the fact that the model learns features from only a limited number of training samples, lacking sufficient diversity, which easily leads to overfitting and reduces generalization ability.
[0137] With the introduction of basic preprocessing operations, such as image cropping, flipping, and scaling, the model's performance was significantly improved. Its Precision, Recall, and mAP reached 0.89, 0.87, and 0.88, respectively. These operations effectively increased the diversity of images encountered by the model during training, helping it learn more meaningful features.
[0138] When more advanced data augmentation strategies, such as Cutout, Cutmix, and Mixup, are employed, the model's performance is significantly improved again. These strategies generate new training samples by randomly blurring parts of an image or mixing different images, enabling the model to cope with more complex situations and thus enhancing its robustness.
[0139] Ultimately, when all data augmentation strategies were applied simultaneously, the model achieved the highest levels of Precision, Recall, and mAP, at 0.92, 0.91, and 0.92, respectively. This result strongly demonstrates the effectiveness of combining data augmentation strategies, which together provide more diverse training samples and further improve the model's generalization performance.
[0140] Given that this study focuses on small target recognition, particularly when locusts occupy a small proportion of the image against a complex background, the application of data augmentation strategies is especially important. These strategies can help the model better cope with the challenges of complex backgrounds and small targets, improving the accuracy and stability of recognition.
[0141] A4. Verification of adaptive attention mechanism:
[0142] The adaptive attention mechanism is a key design feature of this model, enabling it to dynamically change the attention distribution based on image content. The impact of the adaptive attention mechanism on model performance is evaluated by ignoring this design and comparing performance metrics. The purpose of this embodiment is to verify whether the adaptive attention mechanism effectively improves model performance. A significant performance improvement using this design would confirm its effectiveness; otherwise, it would suggest the need for further optimization.
[0143] Table 3 Ablation studies of adaptive attention mechanisms
[0144]
[0145] Table 4 illustrates the impact of implementing the adaptive attention mechanism on detection performance. The results show that using the adaptive attention mechanism improves the model's performance in Precision, Recall, and mAP, increasing them from 0.88 to 0.88, respectively.
[0146] The values ranged from 0.86 to 0.92, 0.91, and 0.92. In a given locust detection task, locusts are small and their color blends in with the background, resulting in them occupying only a small portion of the image in most cases. This necessitates that the model pay attention to these important local features. Furthermore, considering the random distribution of locusts, they can appear anywhere in the image, requiring the model to process global information. The adaptive attention mechanism enables the model to adaptively allocate weights when processing local features and global information, thereby improving pest detection capabilities. This embodiment verifies the effectiveness of the adaptive attention mechanism in agricultural pest detection tasks; all three metrics showed improvement after applying the mechanism, highlighting its importance.
[0147] A5. Dynamic header design verification:
[0148] Dynamic head design is another important aspect of this model, allowing the network structure to dynamically change according to the size of the input image. To verify the impact of dynamic head design on model performance, this embodiment omits it and compares performance metrics. The motivation for this embodiment is to determine whether dynamic head design can effectively improve model performance. A significant performance improvement using this design would confirm its effectiveness; otherwise, it would indicate the need for further optimization.
[0149] Table 4 Ablation study of dynamic head
[0150]
[0151] Table 4 shows the impact of implementing a dynamic header design on the model. As can be seen from the table, the implementation of a dynamic header significantly improves the model's Precision, Recall, and mAP. Without a dynamic header, the model's Precision, Recall, and mAP are 0.89, 0.87, and 0.88, respectively. When a dynamic header structure is introduced, these metrics improve to 0.92, 0.91, and 0.92. This indicates that the dynamic header structure effectively improves model performance. First, the dynamic header enables the model to adaptively focus on small targets, which is beneficial for better detection of small targets. Second, the dynamic header provides an effective method for target detection in complex backgrounds, which is crucial for locust detection tasks in complex backgrounds. Therefore, considering the task and recognition challenges (small target detection and complex background processing), the effect of the dynamic header is as expected, effectively enhancing the model's recognition ability.
[0152] A6. Validation of the effects of the advanced focus loss function:
[0153] The loss function is a crucial component in training deep learning models because it defines the optimization objective. In this model, the FocalLoss function is used, a loss function specifically designed to handle class imbalance problems. In this embodiment, to verify the impact of the FocalLoss function on model performance, Cross EntropyLoss and Mean SquaredErrorLoss are used instead of FocalLoss, and the model performance is compared. The motivation behind this embodiment is to determine whether the FocalLoss function can effectively improve model performance and to understand the impact of different loss functions on model performance. This allows us to understand the importance of loss function selection and the impact of different loss functions on model performance.
[0154] Table 5 Ablation Study of Loss Function
[0155]
[0156] Table 6 shows the effects of using different loss functions in the model. It can be observed that when using Adaptive Focal Loss (AFL), Precision, Recall, and mAP perform best, with values of 0.92, 0.91, and 0.92, respectively. Cross-entropy loss and mean squared error loss are commonly used loss functions, but they are relatively weak in handling class imbalance. During training, the model may favor classes with more instances, thus ignoring instances in classes with fewer instances, such as small objects (locusts). Therefore, the effectiveness of these two loss functions is relatively low. The Focal Loss function aims to address class imbalance by increasing the weight of hard-to-classify instances and decreasing the weight of easy-to-classify instances, allowing the model to focus more on "hard-to-classify" instances. Therefore, the model using the Focal Loss function performs better than the model using cross-entropy loss and mean squared error loss in terms of precision, recall, and mAP. AFL is an improvement on Focal Loss, taking into account the target size factor, making it more suitable for small target detection problems. Therefore, the model using the AFL function has the best performance across all metrics.
[0157] A7. Validation of Model Generalization Ability (Comparison of Different Datasets):
[0158] In this embodiment, we apply the proposed model to other datasets for testing to verify its generalization ability. Typically, deep learning models perform well on the training set, but their performance may degrade on the test set and in real-world applications due to overfitting. Therefore, testing the model's performance on other datasets is essential, as it helps understand the model's performance when handling unknown data, i.e., its generalization ability. Furthermore, the aim of this study is to propose an effective method for detecting agricultural pests, which are diverse and widely distributed. Therefore, it is necessary to verify whether the proposed model can handle various types of agricultural pests, not just locusts. The model was tested on two different agricultural pest datasets: the European corn borer dataset and the cotton bollworm dataset, such as... Figure 9 As shown in the figure. Images A and B are from the European Corn Borer dataset, while C and D are from the Cotton Bollworm dataset.
[0159] The European corn borer is a pest that causes severe damage to crops. It is relatively large and brightly colored, quite different from locusts. On the other hand, the cotton bollworm is a common pest of cotton crops; it is small, light-colored, and more difficult to detect. For the European corn borer dataset, 1000 annotated images were used, with 800 used for training and 200 for testing. For the cotton bollworm dataset, 1500 annotated images were used, with 1200 used for training and 300 for testing. The test results are shown in Table 6.
[0160] Table 6 Test results for other datasets
[0161]
[0162] As shown in Table 6, the model's performance on the two test datasets demonstrates its good generalization ability and its ability to effectively handle various agricultural pests. Despite significant differences in appearance between the European corn borer and locust, the model still achieved a precision of 0.90, a recall of 0.88, and a mPA of 0.89. This is mainly due to the model's adaptive attention mechanism and dynamic head design, which allows it to adaptively handle various types of agricultural pests. For the cotton bollworm, although the pest's small size and light color make detection more difficult, the model still achieved a precision of 0.88, a recall of 0.86, and a mPA of 0.87. This is because the model's design considers the problem of small target detection, thereby improving detection accuracy through the adaptive attention mechanism. The model's performance on the European corn borer and cotton bollworm test datasets proves its generalization ability and effectiveness in handling various agricultural pests. This provides an effective method for intelligent monitoring of agricultural pests and has high application value.
[0163] Therefore, this invention adopts the above-mentioned method for establishing a fast locust detection model based on the Transformer architecture, which solves the shortcomings of existing locust identification technology in detecting complex backgrounds and small objects, as well as the impact of sample imbalance on model recognition performance, thereby improving the accuracy and efficiency of locust identification and providing strong technical support for agricultural pest control.
[0164] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention and not to limit them. Although the present invention has been described in detail with reference to preferred embodiments, those skilled in the art should understand that modifications or equivalent substitutions can still be made to the technical solutions of the present invention, and these modifications or equivalent substitutions cannot cause the modified technical solutions to deviate from the spirit and scope of the technical solutions of the present invention.
Claims
1. A method for establishing a fast locust detection model based on the Transformer architecture, characterized in that, Includes the following steps: S1. Data collection: Images of locusts are collected using various devices in different natural environments; S2. Data annotation: The collected images are annotated with data. The data annotation process includes preliminary annotation and annotation review. S3. Data preprocessing: Normalization, denoising and data augmentation are used to preprocess the image. Denoising includes filtering, and data augmentation includes image rotation, image scaling and image flipping. S4. The locust detection model is designed based on the Transformer architecture, including an adaptive attention mechanism, a dynamic head structure, and a high-level loss function. Features are extracted from the image using the Transformer architecture, further processed using the adaptive attention mechanism and dynamic head structure, and then the improved attention-based high-level loss function is used to train the locust detection model. The high-level loss function adds sample difficulty to the original Focal loss function, and its expression is as follows: in, Indicates the predicted probability. This represents the tuning parameters used to adjust the weights of positive and negative samples in the loss. Indicates the difficulty of the sample. This represents the tuning parameters used in the loss to adjust the weights of different sample difficulty levels; S5, mobile platform deployment, including pruning the locust detection model, quantizing the locust detection model, distilling the locust detection model, selecting TensorFlow Lite as the deployment framework for the locust detection model, and developing mobile applications.
2. The method for establishing a fast locust detection model based on the Transformer architecture according to claim 1, characterized in that: The S1 contains a variety of devices, including high-resolution digital cameras, drones, and mobile phones, all of which capture images with a resolution of 1280*720 or higher, depicting different natural environments such as grasslands, deserts, and farmlands.
3. The method for establishing a fast locust detection model based on the Transformer architecture according to claim 1, characterized in that: S2 uses the latest version of the open-source annotation tool LabelImg.
4. The method for establishing a fast locust detection model based on the Transformer architecture according to claim 1, characterized in that: Image scaling in S3 uses a bicubic interpolation function. For an image I of size H*W*C, the scaling mathematical expression is as follows: in, and These are the interpolation functions for the height and width directions, respectively. and The coordinates on the scaled image. For channel index, This indicates the position of the output image in the k-th channel after convolution processing. , The pixel value of ) For an image I with pixel value P, the normalized mathematical representation is: ; Data augmentation employs three methods: Cutout, CutMix, and Mixup. For an image I and a selected region R, CutOut is represented as follows: ; Let the two images be respectively and The selected region is R, and CutMix is represented as follows: ; Mixup for and Two images and their labels and Choose the mixing coefficient λ and perform the following mixing operation; 。 5. The method for establishing a fast locust detection model based on the Transformer architecture according to claim 1, characterized in that, S4 specifically includes: a) Divide the input image into several small blocks, reduce the dimensionality of each block to a one-dimensional vector, and concatenate all one-dimensional vectors into a sequence, which is then input into the Transformer. Assuming the image is divided into P*P blocks, the length of the sequence input into the Transformer is... Add a token-like element to the beginning of the sequence, as shown in the formula below: ; in I i Let represent the i-th small segmentation region, and Flatten denotes the flattening operation; b. Adaptive resolution Transformer is used. At the bottom of the locust detection model, a large patch size and a small sequence length are used to capture contextual information. At the top of the locust detection model, a small patch and a large sequence length are used to generate detailed feature maps. c. An adaptive attention mechanism based on the Transformer structure is used to handle the target of multiple sequence segmentation, as expressed by the following formula: in, , , These represent the query, key, and value, respectively. Dimensions are represented by dimensionality, and Attention represents the attention function. Based on the self-attention mechanism, an additional attention weight is introduced for each patch, which is learned through a single-layer neural network. The formula is as follows: in It is a primitive feature. and These are the weights and biases of the neural network, respectively. for Sigmoid function, The attention weights obtained; d. Multiply the attention weights by the original features to obtain the final feature representation, as follows: in Indicates original features, This represents the final feature representation; e. Dynamic Header Structure: The structure of the feature extraction method is dynamically adjusted according to the size of the target. A set of convolutional kernels of different sizes is pre-set, and each kernel has a separate convolutional branch. During forward propagation, the convolutional branch corresponding to the target size is selected for calculation. The specific selection method is as follows: in, Indicates the first The output features of the location, To select weights, the influence of the j-th convolutional branch at position i is determined. Represents the output of the j-th convolution branch; operator The selection weights are generated due to the gating mechanism, and their mathematical representation is as follows: in, and All scores are calculated by a gating network, and the features at position i and the metadata of the convolutional branch j are received as input. g. The locust detection model was trained using the experimental platform. The learning rate hyperparameter was set to 0.001, and a learning rate decay strategy was introduced.
6. The method for establishing a fast locust detection model based on the Transformer architecture according to claim 5, characterized in that: Quantization of the locust detection model in S5 involves reducing the parameters of the locust detection model from 32-bit floating-point numbers to 8-bit integers.
Citation Information
Patent Citations
Target detection method based on meta-learning combination attention mechanism network model
CN117576379A
Invasive pest sticky trap image recognition method based on deep learning
CN117635911A
