Greenhouse tomato pest and disease detection method based on attention mechanism improved YOLOv8

By introducing the SimAM parameterless attention mechanism and hybrid data augmentation strategy into the deep layer of the YOLOv8 backbone network, the problems of complex background interference and small lesion detection in greenhouse tomato pest and disease detection are solved, achieving high-precision pest and disease identification and localization, adapting to complex lighting and shading conditions, and reducing the missed detection rate and false detection rate.

CN122115975APending Publication Date: 2026-05-29YUNNAN NORMAL UNIV

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
YUNNAN NORMAL UNIV
Filing Date
2026-02-26
Publication Date
2026-05-29

AI Technical Summary

Technical Problem

Traditional manual inspection methods are inefficient and susceptible to subjective influence in the detection of pests and diseases in greenhouse tomatoes. The existing YOLOv8 model has a high rate of missed detection and occlusion problems in the detection of complex backgrounds and small lesions, which makes it difficult to meet the needs of modern planting.

Method used

By introducing the SimAM parameterless attention mechanism deep into the YOLOv8 backbone network, combined with a hybrid data augmentation strategy and an optimized loss function, the accuracy of identification and localization of small targets and occluded pests and diseases is improved, and the risk of false positives and false negatives is reduced.

Benefits of technology

Without increasing the number of model parameters, it significantly improves the detection accuracy and location accuracy of pests and diseases in greenhouse environments, adapts to complex lighting and shading conditions, and reduces the missed detection rate and false detection rate.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122115975A_ABST
    Figure CN122115975A_ABST
Patent Text Reader

Abstract

The application relates to a greenhouse tomato disease and pest detection method based on an attention mechanism improved YOLOv8, which comprises the following steps: collecting tomato images in a greenhouse environment, and performing data enhancement preprocessing to construct a tomato disease and pest dataset; a target detection network model based on YOLOv8 is constructed, a SimAM non-parameter attention mechanism module is embedded between the end of the backbone network of the network model and a spatial pyramid pooling module; the SimAM module is used to calculate the attention weight of each neuron in a feature map through an energy function, the feature map is subjected to three-dimensional attention enhancement, and the model is trained and detected by using the dataset. The application aims to solve the problems that the prior art has insufficient micro lesion feature extraction capability under a complex greenhouse background and is prone to missing detection due to interference of branch and leaf background noise, and realizes rapid and accurate identification of tomato diseases and pests without increasing the parameter quantity of the model.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the fields of smart agriculture and computer vision technology, and in particular to a greenhouse tomato pest and disease detection method based on an improved YOLOv8 attention mechanism. Background Technology

[0002] As one of the world's most important economic crops, tomatoes are highly susceptible to pests and diseases (such as early blight, late blight, and leaf miners) during greenhouse cultivation. If these diseases are not detected and controlled in a timely and precise manner, they will spread rapidly, leading to a severe decline in yield and quality.

[0003] Currently, traditional tomato disease monitoring mainly relies on manual inspection. However, manual methods are labor-intensive, inefficient, and easily affected by subjective experience and visual fatigue, leading to missed detections or misjudgments, making them unsuitable for the needs of modern large-scale cultivation.

[0004] With the development of deep learning technology, object detection algorithms represented by the YOLO (YouOnlyLookOnce) series have been widely applied in the agricultural field. Although the original YOLOv8 model performs excellently on general datasets, it still faces serious challenges in the actual detection scenario of greenhouse tomatoes: 1. Significant background interference: The complex greenhouse environment, dense foliage, and drastic changes in lighting conditions (such as shadows and highlights) make it difficult for the model to extract effective disease features from the complex background; 2. Difficulty in detecting small targets: Early tomato lesions usually have a very small pixel ratio and indistinct texture features. After multi-layer convolutional downsampling, key feature information is easily lost, resulting in a high false negative rate; 3. Severe occlusion problem: There is often mutual occlusion between fruits and branches and leaves, and the feature extraction network of the standard model lacks the ability to focus on key areas.

[0005] In summary, there is an urgent need to develop an improved target detection method that can resist background interference and accurately capture the characteristics of tiny lesions in complex greenhouse environments. Summary of the Invention

[0006] To address or partially address the problems existing in related technologies, this application provides an improved YOLOv8 greenhouse tomato pest and disease detection method based on an attention mechanism. By deeply reconstructing the YOLOv8 backbone network and integrating the SimAM parameterless attention mechanism, combined with a hybrid data augmentation strategy and an optimized loss function, it effectively suppresses interference from complex greenhouse backgrounds, improves the identification and localization accuracy of small targets and occluded pests and diseases, and reduces the risk of false positives and false negatives.

[0007] The first aspect of this application provides a method for detecting pests and diseases in greenhouse tomatoes based on an improved YOLOv8 attention mechanism, comprising the following steps: Tomato disease and pest images were acquired in a greenhouse setting, and a tomato disease and pest dataset was constructed after preprocessing. The tomato pest and disease dataset is input into the object detection model, which outputs preliminary prediction results containing multiple prediction boxes. The object detection model includes a YOLOv8 network and a C2f_SimAM feature extraction module that integrates the SimAM parameterless attention mechanism. The object detection model calculates the attention weights of the feature map neurons through an energy function, and simultaneously completes feature extraction. Figure 3 Dimensional attention weighting is applied, and then the feature map is adaptively weighted using the Sigmoid function to improve the sensitivity of feature extraction. Determine the IoU value between the predicted bounding box and the manually labeled ground truth bounding boxes in the dataset. Calculate the weighted IoU value by combining the bounding box regression loss weight and the distribution focus loss weight. Select the predicted bounding box with the highest weighted IoU value as the target predicted bounding box. The system filters and verifies the accuracy of the pest and disease types, recognition probabilities, and location coordinates corresponding to the target prediction box, and outputs the final accurate identification and location results of pests and diseases.

[0008] The physical meaning of the energy function is: to find a set of transformation parameters that make the target neuron With background neurons Maximizing the difference between them (i.e. minimizing energy) is shown in the following formula: ; in, For the target neuron, For other neurons within the same channel, This represents the total number of neurons in that channel; and These are the weights and biases of the transformation, respectively; This is the regularization coefficient.

[0009] Specifically, the deep feature extraction module of the YOLOv8 backbone network is reconstructed to obtain the C2f_SimAM feature extraction module. Specifically, the C2f module at a specific location in the backbone network that is responsible for extracting high-level semantic features is replaced with the C2f_SimAM module that integrates the SimAM parameterless attention mechanism. The specific location is the P4 level (corresponding to the 6th layer) and the P5 level (corresponding to the 8th layer) of the backbone network.

[0010] The specific process of calculating weights using the SimAM attention mechanism within the C2f_SimAM module includes: Based on neuroscience theory, an energy function is defined for each neuron in the input feature map, and its importance is measured by calculating the linear correlation between the target neuron and surrounding neurons. By minimizing the energy function, an analytical solution for the energy value of each neuron is obtained; The Sigmoid function is used to process the analytical solution of the energy value to obtain the corresponding attention weight. The weight is then multiplied by the feature map to achieve adaptive weighting, which improves the sensitivity of feature extraction without increasing the number of model parameters.

[0011] The preprocessing of tomato disease and pest images includes the following steps: The original image was converted from RGB space to HSV color space, and the hue, saturation and luminance components were randomly perturbed to simulate different light intensities and shadow changes in a greenhouse environment. Random scaling, random translation, and random horizontal flipping operations are performed on the images to increase the diversity of the location and size of pest targets in the samples; The Mosaic data augmentation method was used to randomly select four images, crop and stitch them together to generate training samples with rich background information. At the same time, a random erasure strategy was introduced to randomly select rectangular areas in the images for pixel value masking, simulating the occlusion of tomato fruits or lesions by greenhouse branches and leaves.

[0012] The construction of the tomato pest and disease dataset also includes: manually annotating the pest and disease targets in the preprocessed tomato images using annotation tools to generate XML or TXT format label files containing category labels and bounding box coordinates (i.e., ground truth box coordinates); and randomly dividing the labeled dataset into training set, validation set, and test set according to a preset ratio, which are used for model parameter training and updating, hyperparameter tuning, and final model performance testing, respectively.

[0013] The training strategies for the object detection model include: A composite loss function is constructed to guide the update of model parameters. The composite loss function consists of a weighted sum of classification loss, bounding box regression loss and distribution focus loss. Specifically, the binary cross-entropy loss is used to calculate the classification loss, and the complete intersection-union ratio loss combined with DFL is used to calculate the bounding box regression loss, providing a weight basis for the calculation of the weighted IoU value. We employ stochastic gradient descent as the optimizer and introduce a cosine annealing learning rate scheduling strategy. We perform a warm-up process in the early stages of training, and then dynamically adjust the learning rate according to the cosine function curve to avoid the model training getting stuck in local optima.

[0014] The preset evaluation index used for accuracy verification is the mean accuracy, and its calculation formula is as follows: First, calculate the average precision (AP) for each class: ; in, Represents precision. Recall rate. This is the PR curve; Then calculate the mean precision (mAP) for all categories: ; in, This represents the total number of categories of pests and diseases. For the first The average accuracy of the disease type; the overall detection performance of the model in a greenhouse environment was measured by calculating mAP@0.5 when the IoU threshold was 0.5.

[0015] The second aspect of this application provides a greenhouse tomato pest and disease detection system based on an improved attention mechanism in YOLOv8, comprising: The image acquisition module is used to acquire images of tomato plants in a greenhouse environment in real time and transmit the acquired image data to the processing module. The processing module includes a memory, a processor, and a data interface. The data interface is used to receive image data transmitted by the image acquisition module. The memory stores a computer program and the target detection model trained in the first aspect. The processor is configured to execute the computer program and call the target detection model to implement the steps of the greenhouse tomato pest and disease detection method based on attention mechanism improved YOLOv8 provided in the first aspect of this application. The interaction and alarm module is bidirectionally connected to the processing module and includes a display unit, an alarm unit, and a data storage unit. The display unit is used to visually display the detection results and image annotation information. The alarm unit can preset disease warning thresholds. When a specific high-risk disease category or the disease occurrence area exceeds the threshold, a signal is issued through sound and light alarm, and warning information is pushed to the designated terminal simultaneously. The data storage unit is used to retain the detection results and original image data.

[0016] A computer-readable storage medium storing a computer program thereon is disclosed. The computer-readable storage medium is a non-volatile storage medium, including at least one of a solid-state drive, a hard disk drive, a USB flash drive, and a read-only memory. When the computer program is executed by a processor, it calls the target detection model and dataset parameters stored in the medium to sequentially implement the steps of the greenhouse tomato pest and disease detection method based on attention mechanism improved YOLOv8 provided in the first aspect of this application.

[0017] The technical solution provided in this application may include the following beneficial effects: This application provides an improved YOLOv8 greenhouse tomato pest and disease detection method based on an attention mechanism. By introducing SimAM deep into the YOLOv8 backbone network, the method automatically identifies key features of lesions using an energy function, effectively solving the problem that small lesions are easily obscured by the background of branches and leaves in greenhouse environments. The method uses SimAM as a parameter-free attention mechanism, significantly improving detection accuracy without increasing the number of model parameters or computational burden. Compared to traditional attention mechanisms such as SE and CBAM, it is more suitable for running on agricultural embedded devices. By combining HSV transform, Mosaic enhancement, and CIoU+DFL loss function, the model can adapt to the complex lighting and shading conditions in greenhouses, exhibiting high robustness and localization accuracy, and significantly improving the regression accuracy of the detection boxes.

[0018] It should be understood that the above general description and the following detailed description are exemplary and explanatory only, and do not limit this application. Attached Figure Description

[0019] The above and other objects, features and advantages of this application will become more apparent from the more detailed description of exemplary embodiments thereof in conjunction with the accompanying drawings, wherein the same reference numerals generally represent the same components in the exemplary embodiments thereof.

[0020] Figure 1 This is a flowchart illustrating a method for detecting pests and diseases in greenhouse tomatoes based on an improved YOLOv8 attention mechanism, as proposed in this application. Figure 2 A schematic diagram of the overall architecture of the greenhouse tomato pest and disease detection network based on the improved YOLOv8 attention mechanism provided in this application embodiment; Figure 3 This is a schematic diagram of the internal structure and processing flow of the C2f_SimAM module in the embodiments of this application; Figure 4 This is a schematic diagram of the hybrid data augmentation strategy process used in the embodiments of this application; Figure 5 This diagram illustrates a comparison of the detection results of the model in this application embodiment and the original YOLOv8 model for bacterial spot disease in tomatoes; from left to right, the diagram shows the original image to be detected, the detection result of the original YOLOv8 model, and the detection result of the improved model of this invention.

[0021] Figure 6 A schematic diagram of the overall implementation process of the greenhouse tomato pest and disease detection method based on the improved YOLOv8 attention mechanism provided in the embodiments of this application.

[0022] Figure 7This is a scatter plot comparing the efficiency of the proposed model and the baseline model in terms of parameter quantity and detection accuracy; it demonstrates that the improved model achieves a significant leap in detection accuracy at the cost of only a tiny increase in the number of parameters. Detailed Implementation

[0023] Embodiments of this application will now be described in more detail with reference to the accompanying drawings. While embodiments of this application are shown in the drawings, it should be understood that this application may be implemented in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided to make this application more thorough and complete, and to fully convey the scope of this application to those skilled in the art.

[0024] Unless otherwise expressly specified and limited, the terms "installation," "connection," "linking," and "fixing," etc., should be interpreted broadly. For example, they can refer to a fixed connection, a detachable connection, or an integral part; they can refer to a mechanical connection or an electrical connection; they can refer to a direct connection or an indirect connection through an intermediate medium; they can refer to the internal communication of two components or the interaction between two components. Those skilled in the art can understand the specific meaning of the above terms in this application according to the specific circumstances.

[0025] like Figure 1 The method for detecting pests and diseases in greenhouse tomatoes based on an improved YOLOv8 attention mechanism, as shown, includes the following steps: S1. Obtain images of tomato diseases and pests in a greenhouse setting, and construct a tomato disease and pest dataset after preprocessing.

[0026] Preprocessing mainly involves augmenting the data before labeling it, and specifically includes the following steps: S1-1: Enhanced Light Adaptability. The original image is converted from RGB color space to HSV color space, and the hue, saturation, and value components are randomly perturbed to simulate different light intensities and shadow variations in a greenhouse environment; S1-2: Geometric transformation. Random scaling, translation, and horizontal flipping are performed on the image to increase the diversity of the location and size of pest targets in the sample; S1-3: Mosaic Augmentation and Random Erasing. The Mosaic data augmentation method was used, randomly selecting four images for cropping and stitching to regenerate training samples with rich background information. Simultaneously, a random erasing strategy was introduced, randomly selecting rectangular regions in the images for pixel-level masking to simulate the occlusion of tomato fruits or lesions by greenhouse branches and leaves.

[0027] S1-4: Use annotation tools (such as LabelImg or Labelme) to manually annotate the pest and disease targets in the enhanced tomato images, and generate XML or TXT format label files containing category labels and bounding box coordinates; randomly divide the annotated dataset into training set, validation set and test set according to a preset ratio (e.g. 8:1:1), which are used for training and updating model parameters, tuning hyperparameters and testing the final model performance, respectively.

[0028] S2. Input the tomato pest and disease dataset into the target detection model and output preliminary prediction results containing multiple prediction boxes.

[0029] The object detection model includes a YOLOv8 network and a C2f_SimAM feature extraction module that integrates the SimAM parameterless attention mechanism. The object detection model calculates the attention weights of neurons in the feature map through an energy function, and simultaneously completes feature extraction. Figure 3 The feature map is then weighted by attention and then adaptively weighted by the sigmoid function to improve the sensitivity of feature extraction.

[0030] The object detection model is an improved YOLOv8 model, and its specific construction structure is as follows: The YOLOv8 model's backbone consists of sequentially connected convolutional modules, C2f modules, and spatial pyramid pooling (SPPF) modules. The C2f modules responsible for extracting high-level semantic features at specific locations within the backbone are replaced with an improved module (denoted as the C2f_SimAM module) integrating the SimAM attention mechanism. These specific locations include layers P4 (corresponding to layer 6) and P5 (corresponding to layer 8) of the backbone. By introducing the C2f_SimAM module at these layers, complex background noise from greenhouse environments is suppressed during the multi-scale feature extraction stage, enhancing the expressive power of disease features.

[0031] The working principle of the SimAM attention mechanism includes: based on neuroscience theory, defining an energy function for each neuron in the feature map, which measures the importance of the target neuron by calculating the linear correlation between the target neuron and its surrounding neurons; obtaining an analytical solution by minimizing the energy function, and using the Sigmoid function to process and obtain attention weights, adaptively weighting the feature map, and this process does not add any additional network parameters.

[0032] The specific process of calculating weights using the SimAM attention mechanism within the C2f_SimAM module includes: Based on neuroscience theory, an energy function is defined for each neuron in the input feature map. This energy function measures the importance of the target neuron by calculating the linear correlation between the target neuron and its surrounding neurons. By minimizing the energy function, an analytical solution for the energy value of each neuron is obtained; The energy value analytical solution is processed using the Sigmoid function to obtain the corresponding attention weight, and this weight is multiplied by the feature map to achieve adaptive weighting of the feature map; the C2f_SimAM module improves the sensitivity of feature extraction without increasing the number of model parameters through the above process.

[0033] Specific strategies for training object detection models include: A composite loss function is constructed to guide model parameter updates. The composite loss function is composed of a weighted average of classification loss, bounding box regression loss, and distribution focal loss (DFL). Specifically, binary cross-entropy loss (BCELoss) is used to calculate the classification loss, and complete intersection-union ratio loss (CIoULoss) combined with DFL is used to calculate the bounding box regression loss, so as to improve the positioning accuracy of overlapping and shading pest targets in greenhouse environments. Stochastic gradient descent (SGD) is used as the optimizer, and a cosine annealing learning rate scheduling strategy is introduced. A warmup process is performed at the beginning of training, and then the learning rate is dynamically adjusted according to the cosine function curve to avoid the model training getting stuck in local optima.

[0034] In object detection models, the mean accuracy (mAP) is used as the core indicator to evaluate the model's detection performance. Its calculation formula is as follows: First, calculate the average precision (AP) for each class: in, Represents precision. Recall rate. This is the PR curve; Then calculate the mean precision (mAP) for all categories: in, This represents the total number of categories of pests and diseases. For the first The average accuracy of the disease type; the overall detection performance of the model in a greenhouse environment was measured by calculating mAP@0.5 when the IoU threshold was 0.5.

[0035] S3. Determine the IoU value between the predicted bounding box and the manually labeled ground truth bounding boxes in the dataset. Calculate the weighted IoU value by combining the bounding box regression loss weight and the distribution focus loss weight. Select the predicted bounding box with the highest weighted IoU value as the target predicted bounding box. S4. Filter and verify the pest and disease type, recognition probability and positioning coordinates corresponding to the target prediction box, and output the final accurate identification and positioning results of pests and diseases.

[0036] Example 1: Construction of a YOLOv8 network model based on SimAM improvement like Figure 2 The image shows an improved YOLOv8 model for detecting pests and diseases in greenhouse tomatoes. The original YOLOv8 model consists of three parts: a backbone network, a neck network, and a head. The backbone network is mainly responsible for extracting features from the input image, the neck network is responsible for multi-scale feature fusion, and the head network is responsible for the final class prediction and bounding box regression.

[0037] In greenhouse environments, tomato plants have lush foliage, resulting in significant background interference. Furthermore, early blight and late blight lesions occupy a very small pixel area in the early stages, making them easily obscured by complex background noise. To address this issue, this embodiment performs targeted structural reconstruction of the YOLOv8 backbone network. Specifically, this embodiment introduces the SimAM (SimpleAttentionModule) parameter-free attention mechanism in the deep feature extraction stage of the backbone network. Specifically, the original C2f modules in layers 6 (corresponding to the P4 feature layer, 16x downsampling) and 8 (corresponding to the P5 feature layer, 32x downsampling) of the backbone network are replaced with an improved module integrating SimAM, denoted as the C2f_SimAM module.

[0038] The reasons for choosing to replace layers P4 and P5 are as follows: Layer P4 (Mid-to-high-level features): This layer's feature map contains relatively rich semantic information while preserving a certain spatial resolution. Introducing SimAM here helps the model pay more attention to the texture and shape features of diseases when distinguishing between diseases and background (such as leaves and stems), reducing false background detections.

[0039] Layer P5 (highest feature layer): This layer's feature maps have the strongest semantic information but the lowest spatial resolution. Introducing SimAM here further strengthens the weights of key features through a global energy function, ensuring that the input features are "purified" high-quality features before final feature fusion via the SPPF (Spatial Pyramid Pooling) module, thereby improving the ability to detect and reduce the false negatives of small lesions.

[0040] like Figure 3 As shown, the internal processing flow of the C2f_SimAM module is as follows: Feature splitting and extraction: The input feature map first passes through a convolutional layer for channel adjustment and is then split into two parts according to the Split operation; one part is processed by multiple Bottleneck stacked modules for deep feature extraction, and the other part is directly connected through residuals.

[0041] Feature concatenation: The features processed by Botteleneck are concatenated with the residual branch features to fuse information from different receptive fields.

[0042] Attention weighting (core step): The concatenated feature map is not directly output, but is instead input into the SimAM module. The SimAM module calculates the importance weight of neurons at each spatial location in the feature map based on the energy function, generating a 3D attention map (3DA attentionMap).

[0043] Feature reconstruction: The generated 3D attention map is multiplied by the original feature map to obtain an enhanced feature map, which is then output to the next layer of the network through the final convolutional layer.

[0044] Through the above structural improvements, this embodiment significantly enhances Backbone's ability to express greenhouse tomato disease characteristics without changing the network layer depth or increasing the number of parameters (SimAM is a parameterless module).

[0045] Example 2: Specific calculation process of SimAM parameterless attention mechanism In this embodiment, the C2f_SimAM module introduced in layers P4 and P5 of the backbone network has SimAM (SimpleAttentionModule) as its core component. Unlike existing attention mechanisms such as SE (Squeeze-and-Excitation) or CBAM (ConvolutionalBlockAttentionModule), which require global pooling or fully connected layers to generate channel weights (which introduces additional parameters), SimAM is a completely parameter-free, energy function-based 3D attention mechanism.

[0046] This embodiment is based on the "spatial inhibition effect" theory in neuroscience, which states that active neurons inhibit the activity of surrounding neurons, thus exhibiting a different firing pattern. Based on this theory, the SimAM module identifies important neurons exhibiting "high linear correlation" or "high discriminative power" in the feature map by defining an energy function. The specific calculation steps are as follows: (1) Define the energy function.

[0047] For the input feature map For each channel in the SimAM, the target neuron is measured. With other neurons in this channel The linear separability between them. The defined energy function. As shown in the formula below: in, For the target neuron, For other neurons within the same channel, This represents the total number of neurons in that channel; and These are the weights and biases of the transformation, respectively; This represents the regularization coefficient. The physical meaning of this formula is: to find a set of transformation parameters that make the target neuron... With background neurons Maximize the difference between them (i.e., minimize energy).

[0048] (2) Calculate the analytical solution of the energy function.

[0049] To avoid solving the above formula through iterative training (which consumes a lot of computational resources), this embodiment directly calculates the minimum energy value using the analytical solution. Through mathematical derivation, the formula for calculating the minimum energy value of each neuron is as follows: in, This is the mean value of all neurons within that channel. This represents the variance of all neurons within that channel.

[0050] From this formula, we can see that neurons With channel mean The greater the difference, the higher its energy value. The lower the value, the more important the information contained in the neuron (i.e., it may be a lesion feature rather than the background).

[0051] (3) Generate attention weights and reconstruct features.

[0052] Due to energy value A lower value indicates higher importance, therefore it needs to be inverted to obtain the attention weights. This embodiment uses the sigmoid function to scale the energy values, resulting in the final three-dimensional attention weight matrix. : Finally, the calculated weight matrix Compared with the original input feature map Perform element-wise multiplication to obtain the enhanced feature map. : Through the above steps, the C2f_SimAM module can adaptively infer the disease-related regions in the feature map without requiring any additional network parameters (Parameters-Free), thus achieving feature enhancement.

[0053] Example 3: Dataset Construction, Preprocessing, and Model Training Configuration To verify the effectiveness of the improved YOLOv8 model, this embodiment constructs a dedicated greenhouse tomato pest and disease dataset and designs specific data augmentation and training strategies, such as... Figure 6 As shown, it includes: 1. Dataset construction and annotation.

[0054] First, images of tomato plants were collected in the greenhouse, covering various categories such as early blight, late blight, leaf miners, and healthy leaves.

[0055] After collection, the pest and disease targets in the images are manually labeled using a labeling tool (such as LabelImg). When labeling, draw a rectangle that fits as closely as possible to the edge of the lesion, and generate a TXT format label file containing the class label (ClassID) and normalized center coordinates and width and height information.

[0056] To ensure the objectivity of model evaluation, the dataset was randomly divided into training, validation, and test sets in an 8:1:1 ratio. The training set was used for iterative updates of model parameters, the validation set was used for hyperparameter monitoring and optimal model saving during training, and the test set was used for final performance evaluation.

[0057] 2. Hybrid data augmentation strategy.

[0058] Considering the characteristics of greenhouse environment, such as large changes in light intensity and severe shading by branches and leaves, this embodiment adopts a combined strategy of "geometric transformation + photometric transformation + hybrid enhancement" during the training phase (e.g., Figure 4 (as shown) Photometric Transformation (HSV): The RGB image is converted to the HSV color space by randomly perturbing the hue (H), saturation (S), and lightness (V) components. In this embodiment, the perturbation gain coefficients for hue, saturation, and lightness are set to 0.015, 0.7, and 0.4, respectively, to simulate the complex lighting environment in a greenhouse.

[0059] Geometric transformations: Randomly scale the image (set the scaling factor to 0.5, i.e., the image size fluctuates within ±50%), randomly translate it (set the translation factor to 0.1), and randomly flip it horizontally (set the flip probability to 0.5) to increase the spatial diversity of the samples.

[0060] Mosaic data augmentation involves randomly selecting four training images, randomly cropping, scaling, and rearranging them, and then stitching them together to create a new training image. This method not only enriches the image background but also increases the number of samples for small targets (micro-lesions) through scaling, which helps improve the model's detection performance for small targets.

[0061] Random erasing: A rectangular region is randomly selected in the image with a trigger probability of 0.4 and masked with random pixel values. This strategy simulates a scenario where tomato fruits or leaves are partially obscured by branches, forcing the model to identify based on residual local features, thereby preventing overfitting.

[0062] Phased enhancement strategy: Furthermore, in order to adapt the model to the natural distribution of images in the later stages of training, this embodiment automatically turns off Mosaic enhancement in the last 10 epochs before the end of training to further fine-tune the model accuracy.

[0063] 3. Loss function and optimization strategy.

[0064] This embodiment constructs a composite loss function during the training process. It is composed of a weighted average of classification loss, bounding box regression loss, and distribution focus loss: Classification loss (ClsLoss): The binary cross-entropy loss (BCELoss) is used with a weight of 0.5 to measure the accuracy of class prediction.

[0065] Bounding box regression loss (BoxLoss): The Cross-Union Loss (CIoULoss) is used with a weight of 7.5. CIoULoss comprehensively considers the overlap area between the predicted box and the ground truth box, the distance between the center points, and the aspect ratio, effectively solving the regression bias problem caused by the irregular shape of the lesions.

[0066] Distributed Focus Loss (DFL): With a weight of 1.5, it is used to solve the problem of blurred edges of the detection box and improve positioning accuracy.

[0067] In terms of optimizer configuration, this embodiment uses a stochastic gradient descent (SGD) optimizer with an initial learning rate of 0.01, momentum of 0.937, and weight decay coefficient of 0.0005.

[0068] Simultaneously, a cosine annealing learning rate scheduling strategy is introduced. During the initial three epochs of training, a warmup is performed, with the learning rate linearly increasing from 0 to its initial value; subsequently, the learning rate gradually decreases according to a cosine function curve. This strategy enables the model to converge quickly in the early stages of training and to finely search for the optimal solution in the later stages, avoiding getting trapped in local minima.

[0069] Example 4: Experimental Results Verification and Comparative Analysis 1. Experimental environment and evaluation indicators.

[0070] To rigorously verify the beneficial effects of the technical solution of this invention, all experiments were conducted in the same hardware and software environment. The hardware platform configuration was as follows: CPU: AMD Ryzen 98949HX, GPU: NVIDIA GeForce RTX 5060 (8GB), RAM: 32GB; the software environment was Windows 11 operating system, deep learning framework: PyTorch 2.9.1, CUDA version: 12.8.

[0071] Evaluation metrics: This embodiment uses the mean accuracy (mAP@0.5) as the core metric for measuring detection accuracy, while also focusing on the number of model parameters and frames per second (FPS) to evaluate the model's lightweight nature and real-time performance.

[0072] The calculation of mAP depends on precision (P) and recall (R). P represents the proportion of samples predicted as positive that are actually positive, and R represents the proportion of samples that are actually positive that were correctly predicted. mAP@0.5 is the average precision of all disease categories when the Intersection over Union (IoU) threshold is set to 0.5.

[0073] 2. Attention mechanism ablation experiment (used to demonstrate the superiority of SimAM) To verify the rationality of choosing the SimAM parameterless attention mechanism in this invention, while keeping the backbone network and other training parameters consistent, the mainstream SE module and CBAM module were introduced at the same positions (P4 and P5 layers) of the YOLOv8 model for comparison with the SimAM module of this invention. The experimental results are shown in Table 1.

[0074] Table 1 Comparison of ablation experiment results with different attention mechanisms As shown in Table 1, while introducing SE and CBAM improves detection accuracy to some extent, both introduce a considerable number of additional parameters. In contrast, the SimAM module introduced in this invention, although theoretically parameterless, only brings a very small increase in parameters in actual engineering integration (from 3.01M to 3.05M, an increase of only 1.3%). With almost the same number of parameters, mAP@0.5 is significantly improved by 2.3 percentage points. This proves that SimAM can achieve more efficient feature focusing while maintaining the advantage of lightweight model.

[0075] 3. Comparison experiment of mainstream algorithms.

[0076] To further verify the advantages of the model in terms of overall performance, it was compared with current mainstream object detection algorithms (Faster R-CNN, YOLOv5s, YOLOv7-Tiny) on the same tomato dataset. The experimental results are shown in Table 2.

[0077] Table 2 Performance Comparison of Different Detection Algorithms As shown in Table 2, the method of this invention significantly outperforms the two-stage algorithm Faster R-CNN and earlier YOLO series algorithms in terms of detection accuracy. Compared with the original YOLOv8n, although the inference speed (FPS) decreases only slightly (mainly due to the computational overhead of SimAM, but still meets the real-time requirements), the detection accuracy is significantly improved. This indicates that the method of this invention effectively solves the problems of missed detections and false detections while ensuring the real-time performance of greenhouse operations.

[0078] Model performance ratio analysis.

[0079] To further evaluate the overall performance of the model, this embodiment presents a comparison chart of "parameter quantity - accuracy" (Efficiency vs. Accuracy Comparison), as shown below. Figure 7 As shown.

[0080] The horizontal axis (X-axis) represents the number of model parameters, measured in millions (M). The further to the left, the lighter the model.

[0081] Vertical axis (Y-axis): Represents the average accuracy under high stringency (mAP@0.5:0.95). This metric reflects the model's ability to achieve high-precision positioning better than mAP@0.5.

[0082] Data Analysis: As shown in the figure, the baseline model (Baseline_600) is located in the lower left corner, with approximately 3.01M parameters and an mAP of approximately 0.845. The improved model of this invention (SimAM_600_pro) is located in the upper right corner, with approximately 3.05M parameters, and its mAP jumps to around 0.870.

[0083] Conclusion: As clearly shown in the graphs, the displacement (parameter increase) of the model in this invention on the horizontal axis is extremely small, almost negligible; however, the displacement (accuracy improvement) on the vertical axis is very significant. This fully demonstrates that the SimAM module, as a parameterless or extremely low-parameter attention mechanism, successfully breaks the constraint of traditional models that "accuracy improvement must sacrifice a large amount of computational resources," and achieves a significant improvement in the detection performance of greenhouse tomato pests and diseases while maintaining the advantage of lightweight design.

[0084] 4. Visualization effect analysis.

[0085] To visually verify the effectiveness of the improved model in extracting disease features, a typical tomato bacterial spot disease sample was selected for comparative testing, such as... Figure 5 As shown. Figure 5 From left to right: the original image to be detected, the detection result of the benchmark model, and the detection result of the improved model of this invention.

[0086] Left image (Original Image): Shows a tomato leaf sample containing typical bacterial spot lesions.

[0087] The middle image (Baseline_600): Using the original YOLOv8 model (Baseline_600) for detection, although the model can locate the diseased area, its classification confidence score is 0.85. This means that there is still some uncertainty in the model's judgment of this disease feature.

[0088] Right figure (SimAM (2021) _600_pro): After introducing the SimAM parameterless attention mechanism, the detection confidence of the improved model of this invention for the same image is increased to 0.87.

[0089] Analysis Conclusion: Although the visual differences in the detection box positions are not significant, the increased confidence indicates that the C2f_SimAM module effectively enhances the network's response to key features such as lesion texture and edges. In the feature space, the improved model can extract more discriminative features, enabling it to provide more reliable judgments when faced with lesions that appear similar, thereby reducing the risk of false positives and false negatives.

[0090] Example 5 A greenhouse tomato pest and disease detection system based on an improved attention mechanism in YOLOv8 includes: The image acquisition module is used to acquire images of tomato plants in a greenhouse environment in real time and transmit the acquired image data to the processing module. The processing module includes a memory, a processor, and a data interface. The data interface is used to receive image data transmitted by the image acquisition module. The memory stores a computer program and a trained target detection model. The processor is configured to execute the computer program and call the target detection model to implement the steps of the greenhouse tomato pest and disease detection method based on the attention mechanism improved by YOLOv8 as described above. The interaction and alarm module is bidirectionally connected to the processing module and includes a display unit, an alarm unit, and a data storage unit. The display unit is used to visually display the detection results and image annotation information. The alarm unit can preset disease warning thresholds. When a specific high-risk disease category or the disease occurrence area exceeds the threshold, a signal is issued through sound and light alarm, and warning information is pushed to the designated terminal simultaneously. The data storage unit is used to retain the detection results and original image data.

[0091] A computer-readable storage medium storing a computer program thereon, wherein the computer-readable storage medium is a non-volatile storage medium, including at least one of a solid-state drive, a hard disk drive, a USB flash drive, and a read-only memory; when the computer program is executed by a processor, it calls the target detection model and dataset related parameters stored in the medium to sequentially implement the steps of improving the YOLOv8 greenhouse tomato pest and disease detection method based on the attention mechanism.

[0092] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.

[0093] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.

[0094] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application.

[0095] The various embodiments of this application have been described above. These descriptions are exemplary and not exhaustive, nor are they limited to the disclosed embodiments. Many modifications and variations will be apparent to those skilled in the art without departing from the scope and spirit of the described embodiments. The terminology used herein is chosen to best explain the principles, practical application, or improvement of the technology in the market, or to enable others skilled in the art to understand the embodiments disclosed herein.

Claims

1. A method for detecting pests and diseases in greenhouse tomatoes based on an improved YOLOv8 attention mechanism, characterized in that, Includes the following steps: Tomato disease and pest images were acquired in a greenhouse setting, and a tomato disease and pest dataset was constructed after preprocessing. The tomato pest and disease dataset is input into the target detection model, which outputs preliminary prediction results containing multiple prediction boxes. The target detection model includes a YOLOv8 network and a C2f_SimAM feature extraction module that integrates the SimAM parameterless attention mechanism. The target detection model calculates the attention weights of the feature map neurons through the energy function, simultaneously completes the three-dimensional attention weighting of the feature map, and then adaptively weights the feature map through the Sigmoid function to improve the sensitivity of feature extraction. Determine the IoU value between the predicted bounding box and the manually labeled ground truth bounding boxes in the dataset. Calculate the weighted IoU value by combining the bounding box regression loss weight and the distribution focus loss weight. Select the predicted bounding box with the highest weighted IoU value as the target predicted bounding box. The pest and disease types, recognition probabilities, and location coordinates corresponding to the target prediction boxes are filtered and their accuracy verified, and the final accurate identification and location results of pests and diseases are output.

2. The greenhouse tomato pest and disease detection method based on attention mechanism-improved YOLOv8 according to claim 1, characterized in that, The physical meaning of the energy function is: to find a set of transformation parameters that make the target neuron With background neurons The difference between them is maximized (i.e., energy is minimized), as shown in the following formula: ; in, For the target neuron, For other neurons within the same channel, This represents the total number of neurons in that channel; and These are the weights and biases of the transformation, respectively; This is the regularization coefficient.

3. The greenhouse tomato pest and disease detection method based on attention mechanism-improved YOLOv8 according to claim 1, characterized in that, The deep feature extraction module of the backbone network of the YOLOv8 network is reconstructed to obtain the C2f_SimAM feature extraction module. Specifically, the C2f module at a specific location in the backbone network that is responsible for extracting high-level semantic features is replaced with the C2f_SimAM module that integrates the SimAM parameterless attention mechanism. The specific location is the P4 layer (corresponding to the 6th layer) and the P5 layer (corresponding to the 8th layer) of the backbone network.

4. The greenhouse tomato pest and disease detection method based on the improved YOLOv8 attention mechanism according to claim 1, characterized in that, The specific process of calculating weights using the SimAM attention mechanism within the C2f_SimAM module includes: Based on neuroscience theory, an energy function is defined for each neuron in the input feature map, and its importance is measured by calculating the linear correlation between the target neuron and surrounding neurons. By minimizing the energy function, an analytical solution for the energy value of each neuron is obtained; The energy value analytical solution is processed using the Sigmoid function to obtain the corresponding attention weight. This weight is then multiplied by the feature map to achieve adaptive weighting, thereby improving the sensitivity of feature extraction without increasing the number of model parameters.

5. The greenhouse tomato pest and disease detection method based on attention mechanism-improved YOLOv8 according to claim 1, characterized in that, The preprocessing of the tomato disease and pest images includes the following steps: The original image was converted from RGB space to HSV color space, and the hue, saturation and luminance components were randomly perturbed to simulate different light intensities and shadow changes in a greenhouse environment. Random scaling, random translation, and random horizontal flipping operations are performed on the images to increase the diversity of the location and size of pest targets in the samples; The Mosaic data augmentation method was used to randomly select four images, crop and stitch them together to generate training samples with rich background information. At the same time, a random erasure strategy was introduced to randomly select rectangular areas in the images for pixel value masking, simulating the occlusion of tomato fruits or lesions by greenhouse branches and leaves.

6. The greenhouse tomato pest and disease detection method based on attention mechanism-improved YOLOv8 according to claim 1, characterized in that, The construction of the tomato pest and disease dataset also includes: manually annotating the pest and disease targets in the preprocessed tomato images using annotation tools to generate XML or TXT format label files containing category labels and bounding box coordinates (i.e., ground truth box coordinates); and randomly dividing the annotated dataset into training set, validation set, and test set according to a preset ratio, which are used for model parameter training and updating, hyperparameter tuning, and final model performance testing, respectively.

7. The greenhouse tomato pest and disease detection method based on attention mechanism-improved YOLOv8 according to claim 1, characterized in that, The training strategy for the object detection model includes: A composite loss function is constructed to guide the update of model parameters. The composite loss function is composed of a weighted sum of classification loss, bounding box regression loss and distribution focus loss. Specifically, the binary cross-entropy loss is used to calculate the classification loss, and the complete intersection-union ratio loss combined with DFL is used to calculate the bounding box regression loss, providing a weight basis for the calculation of the weighted IoU value. We employ stochastic gradient descent as the optimizer and introduce a cosine annealing learning rate scheduling strategy. We perform a warm-up process in the early stages of training, and then dynamically adjust the learning rate according to the cosine function curve to avoid the model training getting stuck in local optima.

8. The method for detecting pests and diseases in greenhouse tomatoes based on the improved YOLOv8 attention mechanism according to claim 1, characterized in that, The preset evaluation index used for the accuracy verification is the average accuracy, and its calculation formula is as follows: First, calculate the average precision (AP) for each class: ; in, Represents precision. Recall rate. This is the PR curve; Then calculate the mean precision (mAP) for all categories: ; in, This represents the total number of categories of pests and diseases. For the first The average accuracy of the disease type; the overall detection performance of the model in a greenhouse environment was measured by calculating mAP@0.5 when the IoU threshold was 0.

5.

9. A greenhouse tomato pest and disease detection system based on an improved YOLOv8 attention mechanism, characterized in that, include: The image acquisition module is used to acquire images of tomato plants in a greenhouse environment in real time and transmit the acquired image data to the processing module. The processing module includes a memory, a processor, and a data interface. The data interface is used to receive image data transmitted by the image acquisition module. The memory stores a computer program and the trained target detection model as described in claim 1. The processor is configured to execute the computer program and call the target detection model to implement the steps of the greenhouse tomato pest and disease detection method based on attention mechanism improved YOLOv8 as described in any one of claims 1 to 8. The interaction and alarm module is bidirectionally connected to the processing module and includes a display unit, an alarm unit, and a data storage unit. The display unit is used to visually display the detection results and image annotation information. The alarm unit can preset a disease warning threshold. When a specific high-risk disease category or the disease occurrence area exceeds the threshold, a signal is issued through an audible and visual alarm, and a warning message is simultaneously pushed to a designated terminal. The data storage unit is used to retain the detection results and original image data.

10. A computer-readable storage medium having a computer program stored thereon, characterized in that, The computer-readable storage medium is a non-volatile storage medium, including at least one of solid-state drives, hard disk drives, USB flash drives, and read-only memory; when the computer program is executed by the processor, it calls the target detection model and dataset related parameters stored in the medium to sequentially implement the steps of the greenhouse tomato pest and disease detection method based on attention mechanism improved by YOLOv8 as described in any one of claims 1 to 8.