A tomato leaf disease and insect pest detection method based on improved YOLOv5s

By improving the YOLOv5s model, combining DCAM attention mechanism, Swin Transformer Block and BiFPN network, the problems of large model size and low detection accuracy in the existing detection methods are solved, and efficient and accurate detection of pests and diseases of tomato leaf are achieved.

CN116994056BActive Publication Date: 2025-08-12ANHUI UNIVERSITY OF TECHNOLOGY
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202311008863.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-08-08
Publication Date
2025-08-12
Estimated Expiration
2043-08-08

AI Technical Summary

Technical Problem

The existing tomato pest detection methods have problems such as large model size, low detection accuracy and poor detection effect, making it difficult to achieve efficient and accurate pest detection.

Method used

The improved YOLOv5s model is adopted, by adding DCAM attention mechanism module to the fourth layer of the original network, the C3STR module integrated with Swin Transformer Block replaces the C3 module of the original network layer six, and the BiFPN network replaces the Neck part of the original YOLOv5s, and the loss function is used to train the model to improve detection accuracy.

Benefits of technology

Under the condition of increasing the number of parameters and model size, the accuracy and detection effect of detection are significantly improved, and efficient and accurate detection of pests and diseases of tomato leaf are achieved.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116994056B_ABST
    Figure CN116994056B_ABST
Patent Text Reader

Abstract

The present invention discloses a tomato leaf disease and insect pest detection method based on improved YOLOv5s, which relates to the field of target detection technology. The method comprises the following steps: S1: using tomato leaf disease and insect pest images in the Plant-Village plant disease public dataset to form a tomato leaf disease and insect pest dataset, which specifically includes three categories: tomato late blight, tomato leaf mold, and tomato yellow leaf curl disease, and using open source software LabelImg to perform anchor frame annotation and category classification on different types of diseases and insect pests; S2: dividing the tomato leaf disease and insect pest dataset into a training set, a validation set, and a test set, which are respectively used for model training and testing; S3: adopting YOLOv5s as a basic model, adding a DCAM attention mechanism module to the fourth layer of the original network, using a C3STR module integrated with a Swin Transformer Block to replace the C3 module in the sixth layer of the original network, and simultaneously using a BiFPN network to replace the Neck part of the original YOLOv5s; the present invention improves detection accuracy and detection effect while increasing a small amount of parameters and model size.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of target detection, and in particular to a tomato leaf disease and insect pest detection method based on improved YOLOv5s. Background Art

[0002] Tomato pest and disease detection refers to the process of automatically identifying and detecting diseases and pests that appear on tomato plant leaves. Tomatoes are an important economic crop and occupy a crucial position in agricultural production. However, tomato plants are susceptible to various diseases and pests, which can lead to reduced yields, decreased quality, and even complete death. Tomato pests and diseases primarily include leaf mold, late blight, and yellow leaf curl. These diseases are primarily caused by fungi and can cause symptoms such as spotting, yellowing, and wilting on tomato leaves. Traditional tomato pest and disease detection typically relies on manual observation and empirical judgment, which is time-consuming, labor-intensive, and susceptible to subjective factors.

[0003] In recent years, with the advancement of computer vision and machine learning technologies, automated pest and disease detection methods based on image processing and deep learning have been gradually applied to the agricultural sector. This tomato pest and disease detection technology allows farmers to promptly detect and diagnose pest and disease problems, implement appropriate control measures, improve tomato yield and quality, reduce pesticide use, and achieve sustainable agricultural development. These methods utilize computers to analyze and identify tomato leaf images, but existing detection methods suffer from issues such as large model size, low detection accuracy, and poor detection results. Summary of the Invention

[0004] The purpose of the present invention is to provide a tomato leaf disease and insect pest detection method based on improved YOLOv5s, which solves the problems of existing detection methods such as large model size, low detection accuracy and poor detection effect, and realizes the detection and classification of various tomato leaf diseases and insect pests. The improved algorithm can achieve efficient and accurate disease and insect pest detection, providing farmers with disease and insect pest monitoring and precise prevention and control measures.

[0005] The present invention solves the above-mentioned technical problems through the following technical solutions, which include the following steps:

[0006] S1: Use tomato leaf pest and disease images from the Plant-Village plant disease public dataset to form a tomato leaf pest and disease dataset, which specifically includes three categories: tomato late blight, tomato leaf mold, and tomato yellow leaf curl. Use the open source software LabelImg to perform anchor frame annotation and category classification for different types of pests and diseases;

[0007] S2: Divide the tomato leaf disease and insect pest dataset into training set, validation set and test set for model training and testing respectively;

[0008] S3: Using YOLOv5s as the base model, the DCAM attention mechanism module was added to the fourth layer of the original network. The C3STR module integrated with the Swin Transformer Block replaced the C3 module in the sixth layer of the original network. The BiFPN network was used to replace the neck part of the original YOLOv5s. The head part was used to predict the location and category of tomato leaf pests and diseases. The model was trained using a loss function, and the final model weights were obtained after convergence.

[0009] S4: Deploy the optimal model weights obtained through training to the computer, input the test image for detection, and generate the final detection results.

[0010] Preferably, in step S1, the tomato leaf pest and disease dataset is constructed using the following method:

[0011] Images of three disease categories, tomato late blight, tomato leaf mold, and tomato yellow leaf curl, were selected from the Plant-Village plant disease public dataset. These images were fused with related disease images taken in real environments to construct a tomato leaf disease and pest dataset. The open source software LabelImg was used to label different types of diseases and pests, with tomato late blight labeled as "A", tomato leaf mold as "B", and tomato yellow leaf curl as "C".

[0012] Preferably, in step S3, a DCAM attention mechanism module is added to the fourth layer of the original network. The module consists of a channel attention mechanism and a spatial attention mechanism. The processing flow is as follows: first, the feature map is input into the two channels of the channel attention mechanism;

[0013] In channel 1, the feature map is first average-pooled to obtain aggregated features, and then a one-dimensional convolution with an adaptive convolution kernel is applied to generate weights. The adaptive convolution kernel size is calculated as follows:

[0014]

[0015] Among them, k is the convolution kernel size, C is the number of channels, γ and b are the weights for changing the number of channels and the convolution kernel, γ = 2, b = 1, / / odd To find odd operations;

[0016] In channel two, the feature map is first average-pooled to extract aggregate features, and then a fully connected layer (FC) is used to assign different weights to the channels. The weights of the two channels are superimposed and normalized using the sigmoid function to solve the channel attention output weight. Finally, the input feature map is multiplied by the weight to obtain a feature map with channel attention weights. The channel attention mechanism is calculated as follows:

[0017] M c(F mc )=σ(W2δ(W1(AvgPool(F)))+f k×k (AvgPool(F)))F

[0018] Among them, M c is the channel attention mechanism, F mc is a feature map with channel attention weights, σ is the sigmoid function, W1 is the weight of the first fully connected layer FC, W2 is the weight of the second fully connected layer FC, δ is the ReLU activation function, AvgPool is the average pooling, f k×k is a convolution with a convolution kernel of k×k;

[0019] The feature map with channel attention weights is input into the spatial attention mechanism, and is subjected to maximum pooling and average pooling respectively. The Concat algorithm is then used to concatenate the pooling results in the channel dimension to solve the spatial weight of the feature map. A convolution layer with a convolution kernel size of 7 is then used for dimensionality reduction. Finally, the feature map with the input channel attention weights is multiplied to obtain a feature map with both channel attention weights and spatial attention weights. The calculation formula of the spatial attention mechanism is as follows:

[0020] M s (F sc )=σ(f 7×7 ([AvgPool(F mc );MaxPool(F mc )]))F mc

[0021] Among them, M s is the spatial attention mechanism, F sc is a feature map with channel attention and spatial attention weights, MaxPool is the maximum pooling, f 7×7 is a convolution with a kernel size of 7.

[0022] Preferably, in step S3, the C3STR module integrated with the Swin Transformer Block is used to replace the C3 module of the sixth layer of the original network, and the processing flow is as follows: the feature map is processed through two paths: one path uses a single convolution module, and the other path uses a combination of the Swin Transformer Block and the basic convolution module CBS;

[0023] In the Swin Transformer Block, the input features first pass through the LN layer to calculate the mean and variance, and perform the intra-window attention mechanism calculation; the calculation result is multiplied by the input features to obtain the output features, and then the features are added to the output feature data through the LN layer and the MLP multi-layer perceptron; then, the output features are again passed through the LN layer to calculate the mean and variance, and perform the inter-window attention mechanism calculation; the calculation result is multiplied by the input features to obtain the final output features, and then the features are added to the output data through the LN layer and the MLP multi-layer perceptron. The Swin Transformer Block calculation formula is as follows:

[0024]

[0025] in, is the output feature of the (S)W-MSA module of the first block, Z l It is the output feature of the MLP module of the lth block, and LN is the Layer Normalization layer standardization module.

[0026] Preferably, in step S3, the Neck part of the original YOLOv5s is replaced by the BiFPN network, and the processing flow is as follows:

[0027] S31: Adjust the number of feature map channels output by the tenth layer of the network from 512 to 256, which is achieved through the Conv convolution module and the Upsample upsampling module;

[0028] S32: Normalizes the features of the seventh and twelfth layers of the fusion network and outputs them to the C3 module to learn residual features;

[0029] S33: The number of channels is adjusted to 128 through the Conv convolution module and the Upsample upsampling module. The features of the fifth and seventeenth layers of the network are normalized and fused, and the output is sent to the C3 module to learn the residual features, which are then output to the next layer and the head of the network.

[0030] S34: The number of channels is adjusted to 256 through the Conv convolution module, and the features of the seventh, fourteenth, and nineteenth layers of the network are normalized and fused, and output to the C3 module to learn the residual features, which are then output to the next layer and the head of the network;

[0031] S35: The number of channels is adjusted to 256 through the Conv convolution module, and the features of the eleventh and twenty-third layers of the network are normalized and fused, and output to the C3 module to learn the residual features, which are then output to the next layer and the head of the network. Among them, the normalized fusion features continuously update the weights through feature learning, so that the network can obtain more feature information. Its calculation formula is:

[0032]

[0033] Among them, O is the normalized fusion feature, w i is the learning weight of each layer of the network, w j is the learning weight of the jth layer, I i is the input feature, ε=0.0001.

[0034] Preferably, the output calculation formula of each layer of BiFPN is:

[0035]

[0036] in, are the output features of the i-th layer and the i-1-th layer of the bottom-up pathway, is the input feature of the i-th layer, is the intermediate feature of the i-th layer in the top-down approach, w1', w2', and w3' are different learning weights of the i-th layer, Conv is the convolution operation, and resize is the upsampling or downsampling operation with matching resolution. The calculation formula of the intermediate feature of the i-th layer is:

[0037]

[0038] in, is the input feature of the i+1th layer, w1, w2, and w3 are different learning weights of the i-th layer.

[0039] Preferably, in step S3, the location and category of tomato leaf pests and diseases are predicted through the Head part, and the processing flow is: use the ArgumentParser library to define and parse the command line parameters, then use the torch library to load the pre-trained model and initialize the parameters, then preprocess the input image, convert it into a Tensor that conforms to the model input format and input the Tensor into the model for prediction, obtain the target detection result, and finally visualize the detection result and save it in the output image.

[0040] Preferably, in step S3, the model is trained using a loss function, and the final model weight is obtained after convergence, wherein the loss function adopts CIoU Loss, which is as follows:

[0041]

[0042] Among them, LCIoU is the position loss of the predicted box, IoU is the intersection-over-union ratio of the predicted box and the real box, p, p gt are the center point coordinates of the predicted box and the real box respectively, ρ 2is the Euclidean distance, c is the diagonal length of the smallest rectangle containing the predicted box and the true box, α is the weight balance coefficient, and v is the square of the difference in the aspect ratio between the predicted box and the true box; the definitions of v and α are as follows:

[0043]

[0044] Among them, w gt 、h gt The width and height of the real box are respectively, and w and h are the width and height of the predicted box respectively;

[0045] In the model parameter setting, input image size, prior box size, target recognition type and name, initial learning rate, and learning rate adjustment strategy were used. The model was trained using the divided tomato leaf pest and disease training set. During training, the model was verified using the divided tomato leaf pest and disease validation set. After training, the optimal weight file was saved after the model converged.

[0046] Preferably, in step S4, the optimal model weight is loaded, and the divided tomato leaf pest and disease test set is input. After feature extraction and feature aggregation, the Detect layer obtains the bounding box coordinates, confidence level, and category of the tomato leaf pest and disease target that may be contained. Non-maximum suppression is then used to remove redundant detection frames to obtain the final detection result.

[0047] Preferably, in step S4, the divided tomato leaf pest and disease test set images are input into the YOLOv5s-TLPD model deployed on the computer, and the detection results, i.e., the location, category and confidence of the tomato pest and disease, are output using the display terminal.

[0048] Compared with the prior art, the beneficial effects of the present invention are:

[0049] (1) A DCAM attention mechanism module is added to the fourth layer of the original network. This module combines channel attention and spatial attention. Channel attention weights different channels of the feature map, allowing the network to better focus on useful channel features and suppress useless channels, thereby enhancing the network's representation ability. The channel attention mechanism weights different spatial positions of the feature map, allowing the network to pay more attention to important local areas while reducing attention to noise or irrelevant areas, which helps to improve the network's receptive field, improve the discriminability and robustness of features, and effectively improve the model's feature extraction and classification capabilities.

[0050] (2) The C3STR module integrated with the Swin Transformer Block is used to replace the C3 module in the sixth layer of the original network. By dividing the input feature map into blocks and applying a local window attention mechanism on each block, the local and global image information is captured, so that the model can better handle multi-scale features, enhance the model's ability to extract and learn small-size feature targets, and improve the model's detection effect.

[0051] (3) The BiFPN network is used to replace the Neck part of the original YOLOv5s. The network's multi-scale feature fusion, cross-layer connection and learnable weights are used to improve the network's receptive field and feature expression capabilities, thereby improving the model's accuracy in detecting various targets.

[0052] (4) After being deployed on a computer, the present invention can use a display terminal to output the location, category, and confidence of tomato leaf pest and disease images. Compared with the original YOLOv5s model, the detection accuracy and detection effect are significantly improved, and the detection rate is faster, while only a small amount of parameters and model size are increased. BRIEF DESCRIPTION OF THE DRAWINGS

[0053] Figure 1 is a flow chart of the present invention;

[0054] Figure 2 The DCAM attention mechanism module structure used in the YOLOv5s-TLPD network of the present invention;

[0055] Figure 3 It is the C3STR module structure used in the YOLOv5s-TLPD network of the present invention;

[0056] Figure 4 It is the BiFPN network structure used in the YOLOv5s-TLPD network of the present invention;

[0057] Figure 5 This is the overall structure diagram of the YOLOv5s-TLPD network of the present invention;

[0058] Figure 6 This is a diagram of the terminal display effect of an example of the present invention. DETAILED DESCRIPTION

[0059] The above and other technical features and advantages of the present invention are described in more detail below with reference to the accompanying drawings.

[0060] The present invention provides a technical solution: a tomato leaf disease and insect pest detection method based on improved YOLOv5s, referring to Figures 1 to 6 , the specific steps include:

[0061] (1) Use the tomato late blight, tomato leaf mold, and tomato yellow leaf curl disease images in the Plant-Village plant disease public dataset to create a tomato leaf disease and pest dataset.

[0062] (2) The open source software LabelImg was used to label different types of pests and diseases, generating anchor frames of different numbers and sizes. Tomato late blight was labeled as “A”, tomato leaf mold was labeled as “B”, and tomato yellow leaf curl was labeled as “C”.

[0063] (3) Build an improved YOLOv5s-TLPD model, propose a DCAM attention mechanism module, and add it to the fourth layer of the original YOLOv5s network; use the C3STR module integrated with Swin Transformer to replace the C3 module in the sixth layer of the original network; use the BiFPN (Bidirectional Feature Pyramid Network) network to replace the Neck part of the original YOLOv5s to achieve high-precision detection of the model.

[0064] (4) Train the improved YOLOv5s-TLPD model.

[0065] (5) The trained model is deployed to the computer, and after inputting the tomato leaf pest and disease picture, the prediction results are output, namely the location, category and confidence of the tomato leaf pest and disease.

[0066] To create the tomato leaf disease and pest dataset, we specifically selected 1,292 tomato late blight images, 1,141 tomato leaf mold images, and 1,413 tomato yellow leaf curl images from the Plant-Village plant disease public dataset.

[0067] To build the improved YOLOv5s-TLPD model, the following steps are included:

[0068] (1) Add the DCAM attention mechanism module to the fourth layer of the original network, refer to Figure 2 ,This module consists of a channel attention mechanism and a spatial attention mechanism. Its processing flow is as follows: first, the feature map is input into the two channels of the channel attention mechanism. In channel one, the feature map is first average pooled to obtain aggregated features, and then a one-dimensional convolution of the adaptive convolution kernel is used to generate weights. The adaptive convolution kernel size is calculated as follows:

[0069]

[0070] Among them, k is the convolution kernel size, C is the number of channels, γ and b are the weights for changing the number of channels and the convolution kernel, γ = 2, b = 1, / / odd To find odd operations.

[0071] In channel two, the feature map is first average-pooled to extract aggregate features, and then a fully connected layer (FC) is used to assign different weights to the channels. The weights of the two channels are superimposed and normalized using a sigmoid function to obtain the channel attention output weight. Finally, the input feature map is multiplied by the weight to obtain a feature map with channel attention weights. The channel attention mechanism is calculated as follows:

[0072] M c (F mc )=σ(W2δ(W1(AvgPool(F)))+f k×k (AvgPool(F)))F

[0073] Among them, M c is the channel attention mechanism, F mc is a feature map with channel attention weights, σ is the sigmoid function, W1 is the weight of the first fully connected layer FC, W2 is the weight of the second fully connected layer FC, δ is the ReLU activation function, AvgPool is the average pooling, f k×k is a convolution with a kernel of k×k.

[0074] The feature map with channel attention weights is input into the spatial attention mechanism, and is subjected to maximum pooling and average pooling respectively. The Concat algorithm is then used to concatenate the pooling results in the channel dimension to solve the spatial weight of the feature map. A convolution layer with a convolution kernel size of 7 is then used for dimensionality reduction. Finally, the feature map with the input channel attention weights is multiplied to obtain a feature map with both channel attention weights and spatial attention weights. The calculation formula of the spatial attention mechanism is as follows:

[0075] M s (F sc )=σ(f 7×7 ([AvgPool(F mc );MaxPool(F mc )]))F mc

[0076] Among them, M s is the spatial attention mechanism, F sc is a feature map with channel attention and spatial attention weights, MaxPool is the maximum pooling, f 7×7 is a convolution with a kernel size of 7.

[0077] (2) The C3STR module integrated with the Swin Transformer Block is used to replace the C3 module in the sixth layer of the original network. The processing flow is as follows: the feature map is processed through two paths: one path uses a single convolution module, and the other path uses a combination of the Swin Transformer Block and the basic convolution module CBS. In the Swin TransformerBlock, the input feature first passes through the LN layer to calculate the mean and variance, and performs the window-in-attention mechanism calculation (W-MSA). The calculation result is multiplied with the input feature to obtain the output feature, and then the feature is added to the output feature data through the LN layer and the MLP multi-layer perceptron. Then, the output feature is again calculated through the LN layer for mean and variance, and the window-in-attention mechanism calculation (SW-MSA) is performed. The calculation result is multiplied with the input feature to obtain the final output feature, and then the feature is added to the output data through the LN layer and the MLP multi-layer perceptron. The calculation formula of the Swin Transformer Block is as follows:

[0078]

[0079] in, is the output feature of the (S)W-MSA module of the lth block, Z l It is the output feature of the MLP module of the lth block, and LN is the Layer Normalization layer standardization module.

[0080] (3) The BiFPN network is used to replace the Neck part of the original YOLOv5s. The processing flow is as follows: the number of channels of the feature map output by the tenth layer of the network is adjusted from 512 to 256, which is achieved through the Conv convolution module and the Upsample upsampling module. Then, the features of the seventh and twelfth layers of the network are normalized and fused, and output to the C3 module to learn the residual features. Then, the number of channels is adjusted to 128 through the Conv convolution module and the Upsample upsampling module, and the features of the fifth and seventeenth layers of the network are normalized and fused, and output to the C3 module to learn the residual features, and output them to the next layer and the head of the network. Then, the number of channels is adjusted to 256 through the Conv convolution module, and the features of the seventh, fourteenth and nineteenth layers of the network are normalized and fused, and output to the C3 module to learn the residual features, and output them to the next layer and the head of the network. Finally, the number of channels is adjusted to 256 through the Conv convolution module, and the features of the 11th and 23rd layers of the network are normalized and fused, output to the C3 module to learn the residual features, and output to the next layer and the head of the network. Among them, the normalized fusion features continuously update the weights through feature learning, so that the network can obtain more feature information. Its calculation formula is:

[0081]

[0082] Among them, O is the normalized fusion feature, w i is the learning weight of each layer of the network, w j is the learning weight of the jth layer, I i is the input feature, ε=0.0001.

[0083] The output calculation formula of each layer of BiFPN is:

[0084]

[0085] in, are the output features of the i-th layer and the i-1-th layer of the bottom-up pathway, is the input feature of the i-th layer, is the intermediate feature of the i-th layer from the top-down approach, w'1, w'2, and w'3 are different learning weights of the i-th layer, Conv is the convolution operation, and resize is the upsampling or downsampling operation with matching resolution. The calculation formula for the intermediate feature of the i-th layer is:

[0086]

[0087] in, is the input feature of the i+1th layer, w1, w2, and w3 are different learning weights of the i-th layer.

[0088] Training the improved YOLOv5s-TLPD model involves the following steps:

[0089] The model is trained using the loss function, and the final model weight is obtained after convergence. The loss function uses CIoULoss, as shown below:

[0090]

[0091] Among them, LCIoU is the position loss of the predicted box, IoU is the intersection-over-union ratio of the predicted box and the real box, p, p gt are the center point coordinates of the predicted box and the real box respectively, ρ 2 is the Euclidean distance, c is the diagonal length of the smallest rectangle containing the predicted box and the true box, α is the weight balance coefficient, and v is the square of the difference in the aspect ratio between the predicted box and the true box. The definitions of v and α are as follows:

[0092]

[0093] Among them, w gt 、h gtThe width and height of the ground-truth box are respectively, while w and h are the width and height of the predicted box. When setting the network model parameters, input the image size, prior box size, target type and name, initial learning rate, and learning rate adjustment strategy. The model was trained using the partitioned tomato leaf pest and disease training set.

[0094] The improved network training hyperparameters are shown in Table 1:

[0095] Table 1

[0096]

[0097]

[0098] During training, the model is verified using the divided tomato leaf disease and insect pest validation set, and the optimal weight file is saved after training until the model converges.

[0099] Deploying the trained model to a computer, inputting tomato leaf pest and disease images and outputting prediction results specifically includes the following steps: using the computer to load the optimal model weights, inputting the divided tomato leaf pest and disease test set, extracting and aggregating features, and obtaining the bounding box coordinates, confidence level, and category of targets that may contain tomato leaf pests and disease through the Detect layer. Non-maximum suppression is then used to remove redundant detection frames to obtain the final detection results.

[0100] According to the final detection results of the model, the detection accuracy and speed of the improved YOLOv5s-TLPD model are evaluated from the average precision mAP (mean Average Precision), recall rate (Recall), number of parameters (Parameters), detection rate (FPS) and model size indicators.

[0101] The comparison results with the original YOLOv5s model are shown in Table 2 below:

[0102] Table 2

[0103]

[0104] As shown in Table 2, the improved YOLOv5s-TLPD model significantly improves both mean precision and recall while maintaining similar parameters, detection rate, and model size to the original YOLOv5s. Without significantly increasing deployment complexity, the model effectively improves the accuracy of object detection and localization for different categories, reduces missed detections, and achieves a faster detection rate.

[0105] The classified tomato leaf pest and disease test set images are input into the YOLOv5s-TLPD model deployed on the computer, and the detection results, namely the location, category, and confidence level of the tomato pest and disease, are output using the display terminal.

[0106] The terminal display effect is as follows Figure 6 shown.

[0107] The above description is merely a preferred embodiment of the present invention and is intended to be illustrative rather than restrictive of the present invention. Those skilled in the art will appreciate that many changes, modifications, and even equivalents may be made to the present invention within the spirit and scope of the claims, all of which fall within the scope of protection of the present invention.

Claims

1. A tomato leaf disease and insect pest detection method based on improved YOLOv5s, characterized in that: The steps include: S1: Use tomato leaf pest and disease images from the Plant-Village plant disease public dataset to form a tomato leaf pest and disease dataset, which specifically includes three categories: tomato late blight, tomato leaf mold, and tomato yellow leaf curl. Use the open source software LabelImg to perform anchor frame annotation and category classification for different types of pests and diseases; S2: Divide the tomato leaf disease and insect pest dataset into training set, validation set and test set for model training and testing respectively; S3: Using YOLOv5s as the base model, the DCAM attention mechanism module was added to the fourth layer of the original network. The C3STR module integrated with the Swin Transformer Block replaced the C3 module in the sixth layer of the original network. The BiFPN network was used to replace the neck part of the original YOLOv5s. The head part was used to predict the location and category of tomato leaf pests and diseases. The model was trained using a loss function, and the final model weights were obtained after convergence. S4: Deploy the optimal model weights obtained through training to the computer, input the test image for detection, and generate the final detection results; In step S3, a DCAM attention mechanism module is added to the fourth layer of the original network. The module consists of a channel attention mechanism and a spatial attention mechanism. Its processing flow is as follows: first, the feature map is input into the two channels of the channel attention mechanism; In channel 1, the feature map is first average-pooled to obtain aggregated features, and then a one-dimensional convolution with an adaptive convolution kernel is applied to generate weights. The adaptive convolution kernel size is calculated as follows: Among them, k is the convolution kernel size, C is the number of channels, γ and b are the weights for changing the number of channels and the convolution kernel, γ = 2, b = 1, ‖ odd To find odd operations; In channel two, the feature map is first average-pooled to extract aggregate features, and then a fully connected layer (FC) is used to assign different weights to the channels. The weights of the two channels are superimposed and normalized using the sigmoid function to solve the channel attention output weight. Finally, the input feature map is multiplied by the weight to obtain a feature map with channel attention weights. The channel attention mechanism is calculated as follows: M c (F mc )=σ(W2δ(W1(AvgPool(F)))+f k×k (AvgPool(F)))F Among them, M c is the channel attention mechanism, F mc is a feature map with channel attention weights, σ is the sigmoid function, W1 is the weight of the first fully connected layer FC, W2 is the weight of the second fully connected layer FC, δ is the ReLU activation function, AvgPool is the average pooling, f k×k is a convolution with a convolution kernel of k×k; The feature map with channel attention weights is input into the spatial attention mechanism, and is subjected to maximum pooling and average pooling respectively. The Concat algorithm is then used to concatenate the pooling results in the channel dimension to solve the spatial weight of the feature map. A convolution layer with a convolution kernel size of 7 is then used for dimensionality reduction. Finally, the feature map with the input channel attention weights is multiplied to obtain a feature map with both channel attention weights and spatial attention weights. The calculation formula of the spatial attention mechanism is as follows: M s (F sc )=σ(f 7×7 ([AvgPool(F mc );MaxPool(F mc )]))F mc Among them, M s is the spatial attention mechanism, F sc is a feature map with channel attention and spatial attention weights, MaxPool is the maximum pooling, f 7×7 is a convolution with a kernel size of 7.

2. The tomato leaf disease and insect pest detection method based on improved YOLOv5s according to claim 1, characterized in that: In step S1, the tomato leaf disease and insect pest dataset is constructed using the following method: We selected images of three disease categories, tomato late blight, tomato leaf mold, and tomato yellow leaf curl, from the Plant-Village plant disease public dataset. We fused these images with relevant disease images taken in real environments to construct a tomato leaf disease and pest dataset. We used the open source software LabelImg to label different types of diseases and pests, with tomato late blight labeled "A", tomato leaf mold labeled "B", and tomato yellow leaf curl labeled "C".

3. The tomato leaf disease and insect pest detection method based on improved YOLOv5s according to claim 1, characterized in that: In step S3, the C3STR module integrated with the Swin Transformer Block is used to replace the C3 module in the sixth layer of the original network. The processing flow is as follows: the feature map is processed through two paths: one path uses a single convolution module, and the other path uses a combination of the Swin Transformer Block and the basic convolution module CBS; In the Swin Transformer Block, the input features first pass through the LN layer to calculate the mean and variance, and perform the intra-window attention mechanism calculation; the calculation result is multiplied by the input features to obtain the output features, and then the features are added to the output feature data through the LN layer and the MLP multi-layer perceptron; then, the output features are again passed through the LN layer to calculate the mean and variance, and perform the inter-window attention mechanism calculation; the calculation result is multiplied by the input features to obtain the final output features, and then the features are added to the output data through the LN layer and the MLP multi-layer perceptron. The Swin Transformer Block calculation formula is as follows: in, is the output feature of the (S)W-MSA module of the lth block, Z l It is the output feature of the MLP module of the lth block, and LN is the Layer Normalization layer standardization module.

4. The tomato leaf disease and insect pest detection method based on improved YOLOv5s according to claim 1, characterized in that: In step S3, the BiFPN network is used to replace the Neck part of the original YOLOv5s. The processing flow is as follows: S31: Adjust the number of feature map channels output by the tenth layer of the network from 512 to 256, which is achieved through the Conv convolution module and the Upsample upsampling module; S32: Normalizes the features of the seventh and twelfth layers of the fusion network and outputs them to the C3 module to learn residual features; S33: The number of channels is adjusted to 128 through the Conv convolution module and the Upsample upsampling module. The features of the fifth and seventeenth layers of the network are normalized and fused, and the output is sent to the C3 module to learn the residual features, which are then output to the next layer and the head of the network. S34: The number of channels is adjusted to 256 through the Conv convolution module, and the features of the seventh, fourteenth, and nineteenth layers of the network are normalized and fused, and output to the C3 module to learn the residual features, which are then output to the next layer and the head of the network; S35: The number of channels is adjusted to 256 through the Conv convolution module, and the features of the eleventh and twenty-third layers of the network are normalized and fused, and output to the C3 module to learn the residual features, which are then output to the next layer and the head of the network. Among them, the normalized fusion features continuously update the weights through feature learning, so that the network can obtain more feature information. Its calculation formula is: Among them, O is the normalized fusion feature, w i is the learning weight of each layer of the network, w j is the learning weight of the jth layer, I i is the input feature, ε=0.0001.

5. The tomato leaf disease and insect pest detection method based on improved YOLOv5s according to claim 4, characterized in that: The output calculation formula of each layer of BiFPN is: in, are the output features of the i-th layer and the i-1-th layer of the bottom-up pathway, is the input feature of the i-th layer, is the intermediate feature of the i-th layer in the top-down approach, w'1, w'2, and w'3 are different learning weights of the i-th layer, Conv is the convolution operation, and resize is the upsampling or downsampling operation with matching resolution. The calculation formula for the intermediate feature of the i-th layer is: in, is the input feature of the i+1th layer, w1, w2, and w3 are different learning weights of the i-th layer.

6. The tomato leaf disease and insect pest detection method based on improved YOLOv5s according to claim 1, characterized in that: In step S3, the location and category of tomato leaf pests and diseases are predicted through the Head part. The processing flow is as follows: use the ArgumentParser library to define and parse the command line parameters, then use the torch library to load the pre-trained model and initialize the parameters, then preprocess the input image, convert it into a Tensor that conforms to the model input format, and input the Tensor into the model for prediction to obtain the target detection results. Finally, the detection results are visualized and saved in the output image.

7. The tomato leaf disease and insect pest detection method based on improved YOLOv5s according to claim 1 is characterized in that: In step S3, the model is trained using the loss function, and the final model weight is obtained after convergence. The loss function uses CIoU Loss, which is as follows: Among them, LCIoU is the position loss of the predicted box, IoU is the intersection-over-union ratio of the predicted box and the real box, p, p gt are the center point coordinates of the predicted box and the real box respectively, ρ 2 is the Euclidean distance, c is the diagonal length of the smallest rectangle containing the predicted box and the true box, α is the weight balance coefficient, and v is the square of the difference in the aspect ratio between the predicted box and the true box; the definitions of v and α are as follows: Among them, w gt 、h gt The width and height of the real box are respectively, and w and h are the width and height of the predicted box respectively; In the model parameter setting, input image size, prior box size, target recognition type and name, initial learning rate, and learning rate adjustment strategy were used. The model was trained using the divided tomato leaf pest and disease training set. During training, the model was verified using the divided tomato leaf pest and disease validation set. After training, the optimal weight file was saved after the model converged.

8. The tomato leaf disease and insect pest detection method based on improved YOLOv5s according to claim 1, characterized in that: In step S4, the optimal model weights are loaded and the divided tomato leaf pest and disease test set is input. After feature extraction and feature aggregation, the Detect layer obtains the bounding box coordinates, confidence level, and category of the tomato leaf pest and disease targets. Non-maximum suppression is then used to remove redundant detection boxes to obtain the final detection results.

9. The tomato leaf disease and insect pest detection method based on improved YOLOv5s according to claim 1, characterized in that: In step S4, the divided tomato leaf pest and disease test set images are input into the YOLOv5s-TLPD model deployed on the computer, and the detection results, namely the location, category and confidence of the tomato pest and disease, are output using the display terminal.

Citation Information

Patent Citations

  • Methods for detecting leaf lesions in greenhouse tomatoes and targeted spraying devices

    CN114937009A

  • Tomato maturity detection method based on lightweight improved YOLO

    CN114943697A