Wheat ear detection method based on improved YOLOv8
By introducing the Rep5Conv structural reparameterization convolution module, the multi-scale attention EMA module, and the Focaler-IoU loss function into the YOLOv8 model, the problems of low detection accuracy and environmental interference in wheat ear detection are solved, achieving higher detection accuracy and robustness.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-05
- Publication Date
- 2026-04-03
AI Technical Summary
The existing YOLOv8 algorithm has problems such as low detection accuracy, susceptibility to environmental interference, and high rates of missed and false detections in wheat ear detection, making it difficult to meet the needs of practical applications.
In the YOLOv8 model, the Rep5Conv structural reparameterized convolution module, the multi-scale attention EMA module, and the Focaler-IoU loss function are introduced. By improving the structure of the backbone network and the Neck layer, the bounding box regression process is optimized, thereby improving the feature extraction and detection accuracy.
It significantly improves the accuracy and robustness of wheat ear detection, reduces environmental interference, and enhances the model's detection performance, especially in the case of small targets and occlusion.
Smart Images

Figure CN121789036A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of computer vision and agricultural detection technology, specifically relating to a wheat ear detection method based on an improved YOLOv8. Background Technology
[0002] Wheat is not only one of the most widely cultivated food crops globally, but its production and trade dynamics have a profound impact on the world food market. As a primary ingredient in bread, pastries, and many other foods, the stability of wheat supply directly affects global food prices and economic fluctuations. In China, wheat is not only an important component of the traditional diet but also a key element of the national food security strategy. The government's emphasis on wheat production is reflected in various agricultural support policies and technological investments, aiming to ensure stable domestic wheat production and reduce dependence on the international market. Wheat yield is one of the important indicators for assessing agricultural productivity, but its statistical analysis has always been a challenge. Wheat yield statistics are affected by various factors, including natural conditions, planting techniques, and post-harvest processing. Traditional statistical methods often fail to reflect these dynamic changes in real time, leading to data lag or bias. By using satellite imagery and ground data, deep learning models can more accurately predict wheat yield and growth status, effectively overcoming the limitations of traditional methods. The subjectivity and limitations of traditional methods have been overcome. The introduction of deep learning technology has not only improved the efficiency of wheat yield assessment but also promoted the modernization of agricultural production methods, improving the overall quality and economic benefits of crops.
[0003] Deep learning-based object detection techniques can be broadly categorized into two types: multi-stage detection algorithms based on candidate regions and single-stage detection algorithms based on regression. Commonly used multi-stage algorithms include R-CNN, Fast R-CNN, Faster R-CNN, and Unet. Single-stage object detection does not generate candidate regions; instead, it directly locates and classifies objects using deep convolutional neural networks (DCNN), significantly reducing time and space overhead and improving speed. However, its accuracy is lower compared to two-stage algorithms. Currently, commonly used algorithms for wheat detection include the YOLO series, SSD, and RetinaNet. Among the popular single-stage YOLO algorithms, the latest version released in 2023, YOLOv8, is currently the fastest-growing and best-performing. YOLOv8 is based on YOLOv5 and has been improved, resulting in enhanced accuracy, better performance, and a more lightweight design. However, the original model still faces many challenges in wheat ear detection: the targets are densely distributed, crowded together, and severely occluded, making it difficult to effectively detect small wheat ears; it also lacks utilization of contextual semantic information; in addition, the complex field background environment (such as changes in light intensity and interference from branches and leaves) further exacerbates the detection difficulty. In summary, the existing model suffers from low detection accuracy, susceptibility to environmental interference, and high rates of missed and false detections, making it difficult to meet the needs of practical applications. Summary of the Invention
[0004] The purpose of this invention is to provide a wheat ear detection method based on an improved YOLOv8, which significantly improves detection accuracy, solves the problem of missed detection and false detection under wheat shading and crowding conditions, and reduces environmental interference.
[0005] To achieve the above objectives, the present invention provides the following technical solution: A wheat ear detection method based on an improved YOLOv8 includes the following steps: Step 1: Build an improved YOLOv8 network model. The improved YOLOv8 network model is based on the YOLOv8 architecture and includes a structure-reparameterized convolutional Rep5Conv module set in the backbone network, an efficient multi-scale attention EMA module set in the Neck layer, and a Focaler-IoU loss function for model training. The Rep5Conv module with reparameterized convolutions adopts a parallel structure, including iso-mapping, BatchNorm, average pooling layers, 1×1 convolutions and 3×3 convolutions. It merges multiple branches into a single 3×3 convolution structure through convolution reparameterization. The efficient multi-scale attention EMA module can handle any given feature map. Different semantic learning is achieved by dividing the data into G sub-features according to the channel dimension. C represents the number of channels in the input feature map, and H and W represent the height and width of the feature map, respectively. The IoU loss is reconstructed using a linear interval mapping method, as shown in the following formula: in This is the reconstructed focaler IoU, where IoU is the original IoU value, [d, u] ∈ [0,1]; where IoU represents the intersection-union ratio, i.e., the ratio of the area of the overlapping portion of two bounding boxes to the area of their union portion, d is the lower threshold, and u is the higher threshold; by adjusting the values of d and u, it can be made Different regression samples have different focuses, and the bounding box regression loss is different. The definition is as follows: ; Step 2: Construct an experimental dataset based on the global wheat dataset, divide it into training, testing, and validation sets, and perform image preprocessing; Step 3: Train the improved YOLOv8 network model; Step 3.1: Through the improved backbone network and Neck layer, multi-scale visual features and enhanced semantic representations are extracted from the input image, and finally the optimized multi-level feature map is output. Step 3.2: The optimized multi-level feature map is fed into the detection head, and through the anchor box mechanism and the classification and regression convolutional layer, candidate prediction boxes and their category confidence scores are generated. Step 3.3: Optimize the bounding box regression process using the Focaler-IoU loss function. By dynamically adjusting the attention given to samples of different difficulty, the localization accuracy and convergence speed can be improved. Step 3.4: Using a label assignment strategy, match the real wheat ear targets with the preset anchor boxes to determine the positive and negative training samples; Step 3.5: Calculate the total loss Collaborative optimization of classification and regression tasks; Step 3.6: Filter out redundant detection boxes through non-maximum suppression post-processing; Step 3.7: Update and improve the parameters of the YOLOv8 network model; Step 4: Use the validation set to evaluate the performance of the improved YOLOv8 network model and optimize the parameters; Step 5: Input the image of the target wheat ear region to be detected into the trained improved YOLOv8 network model and output the detection results.
[0006] Furthermore, the reparameterization method for the Rep5Conv module described in step 1 includes the following: (1) A 3×3 convolution plus a BatchNorm can be equivalent to a 3×3 convolution with reconstructed parameters, as shown in the following formula: By replacing the independent variables in the BN layer equation with the convolutional layer formula, the following results are obtained: in , , and These represent the cumulative mean, standard deviation, scaling factor, and bias of the new convolutional layer after fusion, respectively. This represents the set of weights for the convolution kernel. This represents the mean value in the original batch normalized layer. This represents the bias in the original batch normalization layer; (2) Assuming that both the input and output channels are 2, a 1×1 convolution can be regarded as a special 3×3 convolution, that is, zeros are padded around the 1×1 convolution kernel, and a 3×3 convolution plus a BatchNorm can be equivalent to a 3×3 convolution with reconstructed parameters. (3) Assuming that both the input and output channels are 2, the parameters of the 3×3 convolution are four 3×3 matrices, and an identity mapping can be regarded as a special 1×1 convolution, that is, the convolution kernel is the identity matrix; (4) An average pooling layer with C channels and a kernel size of 3 can be equivalently transformed into a special 3×3 convolutional layer with the same stride, as shown in the following formula: in This represents the weight tensor of the special convolutional kernel in the average pooling layer. The kernel size; Average pooling layers are applied to each channel of the input. For a given channel, let the kernel size be K. The padding of the kernel is the reciprocal of the square of its kernel size, i.e. The remaining channels are set to 0, as shown in the formula: in Represents the special convolution kernel weight tensor A specific two-dimensional slice, Indicates the index of the output channel; Due to the additivity of convolutions, the five branches of each Rep5conv module can be merged into a single 3×3 convolution. For input, For output, Indicates the first Convolution kernel with multiple branches, , , and These represent the cumulative mean, standard deviation, scaling factor, and bias of the BN layer, respectively. When the input channel equals the output channel and the input height and width equal the output height and width, the following formula applies: This indicates a batch normalization operation. They represent the first The cumulative mean, standard deviation, scale factor, and bias of the batch-normalized BN layer of the branch. Indicates the first The convolution kernel weight matrix of the branch.
[0007] Furthermore, the implementation mechanism of the efficient multi-scale attention EMA module described in step 1 includes the following: The efficient multi-scale attention EMA module extracts attention weights from grouped feature maps through three parallel branches, namely two 1×1 branches and one 3×3 branch. (1) Introduce global average pooling operation on the 1×1 branch, and use 1D horizontal and 1D vertical global average pooling to encode the channel direction so that the channel descriptor can accurately describe the global position information. (2) After connecting the two spatial feature vectors, perform a 1×1 convolution operation to decompose the two feature vectors again. Use Sigmoid to limit the output value between 0 and 1, and reweight the weights of the original feature map channels to balance the difference between easy and difficult samples and categories. (3) In the 3×3 branch, Conv3×3 is used to capture multi-scale features, omitting GN normalization and average pooling operations, in order to extract multi-scale feature representations; (4) Encode the global spatial information of each branch output using 2D global average pooling, and then convert the output channel features of these two branches into their corresponding dimensional shapes, i.e. , This is the global feature vector after channel grouping. This represents the high-dimensional features after spatial compression; the specific pooling formula is: ; in, This represents the global spatial information encoding result of the c-th channel, where c represents the channel index. This represents the feature value of the input feature map at the c-th channel and spatial position (i, j); (5) Use the SoftMax function and Matmul matrix dot product operation to perform fitting and local cross-channel interaction, establish pixel-level pairwise relationships and realize bidirectional information connection; (6) The value output by the Sigmoid function is reweighted with the original feature value so that the final output has the same dimension as the input feature map.
[0008] Furthermore, in step 3.2, the detection head applies 1×1 convolution to predict anchor boxes for each scale of feature map: multiple anchor boxes of different sizes and proportions are preset for each spatial location, and two types of information are output for each anchor box through convolution operation: bounding box regression parameters and class confidence.
[0009] Furthermore, the total loss described in step 3.5 The weighted average of the bounding box regression loss and the classification loss is calculated as follows: in, , and To balance the weighting coefficients of the two losses, For classification loss, it is responsible for optimizing the prediction confidence of wheat ear categories; The target confidence loss is used to determine whether the predicted bounding box contains the target object.
[0010] Furthermore, the evaluation metrics for the improved YOLOv8 network model include mean accuracy. Recall rate and accuracy ; Mean Precision The mean AP (Accuracy) of detection for each class in the dataset. The larger the value, the higher the average accuracy of the model. The calculation formula is as follows: in, Indicates the number of target categories. Indicates the number of detection categories. This is the PR curve. denoted as the area under the PR curve for a certain category; where mAP@50 represents the average precision when the IoU threshold between the predicted box and the ground truth box is 0.5, and mAP@0.5:0.95 represents the average mAP over different IoU thresholds from 0.5 to 0.95 with a step size of 0.05; Recall rate This refers to the proportion of target objects detected by the model out of all actual target objects. The calculation formula is as follows: To predict the number of positive samples in the undetected category, To predict the number of positive samples of the correct class; The accuracy rate of target detection reflects the effectiveness of the algorithm. The calculation formula is: FP is the number of negative samples that correctly predict the class.
[0011] Compared with the prior art, the present invention has the following beneficial effects: This invention improves upon the YOLOv8 model. First, some ordinary convolutional layers in the backbone network are replaced with the Rep5Conv structurally reparameterized convolutional module. By fusing multi-branch, multi-scale features during training and effectively merging them into a single convolutional structure during inference, the feature representation capability and fusion efficiency are significantly enhanced without increasing computational overhead or model parameters, thereby improving the overall recognition accuracy and accelerating inference speed. Second, an efficient multi-scale attention module (EMA) is introduced into the Neck layer, which adaptively focuses on key regions of the wheat ears within a larger receptive field. This effectively strengthens the feature extraction capability for small, dense, and mutually occluded targets, improves the detection rate and accuracy of mutually occluded small targets, reduces interference from irrelevant background features in wheat detection, and solves the problem of missed and false detections in crowded wheat occlusion situations. Finally, the original loss function is replaced with the Focaler-IoU loss function to optimize the bounding box regression process, further accelerating model convergence and improving the accuracy of localization and classification. The aforementioned improvements work synergistically to significantly enhance the accuracy, robustness, and environmental adaptability of wheat ear detection while maintaining the model's lightweight nature. Experimental results show that on the Global Wheat Dataset (GWHD), after 300 training epochs, this method significantly improves detection accuracy compared to the baseline algorithm: mAP@0.5 is improved by 2.2%, and mAP@0.5:0.95 is improved by 3.0%, effectively enhancing the performance of wheat ear detection. Attached Figure Description
[0012] Figure 1 This is a schematic diagram of the structure of the improved YOLOv8 network of the present invention; Figure 2 This is a schematic diagram of the Rep5Conv module structure of the present invention, which features reparameterized convolution. Figure 3 This is a schematic diagram of the structure for transforming a 1×1 convolution into a 3×3 convolution; Figure 4 This is a schematic diagram of the identity mapping structure; Figure 5This is a schematic diagram of the high-efficiency multi-scale attention EMA module structure of the present invention; Figure 6 is a comparison of the detection results of the cropped target region, the original YOLOv8 algorithm, and the improved algorithm of this invention. Figure 6(a) shows the target region cropped from the original image and the detection results of the original YOLOv8 algorithm on the cropped region. Figure 6(b) shows the detection results of the improved YOLOv8 algorithm of this invention on the same cropped region. Figure 7 shows the detection performance of the YOLOv8 algorithm and the improved YOLOv8 algorithm on the global wheat dataset. Figure 7(a) shows the test results of the original YOLOv8 algorithm, and Figure 7(b) shows the test results of the improved YOLOv8 algorithm. Detailed Implementation
[0013] To make the above-mentioned objects, features and advantages of the present invention more apparent and understandable, the present invention will be described in detail below with reference to the accompanying drawings and specific embodiments.
[0014] The wheat ear detection method based on the improved YOLOv8 described in this embodiment includes the following steps: Step 1: Build an improved YOLOv8 network model like Figure 1 As shown, the improved YOLOv8 network model is based on the YOLOv8 architecture and includes a structure-reparameterized convolutional Rep5Conv module in the backbone network, an efficient multi-scale attention EMA module in the Neck layer, and a Focaler-IoU loss function for model training.
[0015] Multi-branch structures can extract multi-scale features better, resulting in superior detection performance compared to single-branch structures. However, due to their use of multi-branch parallel structures, they involve higher computational and parameter counts, leading to slower speeds. In complex field environments, wheat identification hinges on finding a balance between improving model accuracy and preventing excessive parameter expansion. To address this issue, this invention introduces the Rep5onv module into the original YOLOv8 neck network. This improves model accuracy and maintains good multi-scale feature fusion capabilities while minimizing the increase in parameter count and memory consumption, thus meeting the deployment requirements of low computational cost and high accuracy.
[0016] The RepConv module is the basic module of the RepVGG network. It adopts the convolution reparameterization method. In the Train stage, it uses three parallel structures to enrich multi-scale information. In the Val stage, it uses the convolution reparameterization method to merge multiple branches into a single branch structure.
[0017] The Rep5Conv module is an improvement upon RepConv. During the training phase, it employs five parallel structures—identity mapping, BatchNorm, average pooling layers, 1×1 convolutions, and 3×3 convolutions—to enrich multi-scale information, better achieve feature extraction, and obtain better weight parameters. Then, during the inference phase, a convolution reparameterization method is used to fuse the multiple branches into a single 3×3 convolution structure. The structure of the Rep5Conv module is as follows: Figure 2 As shown, the specific method for reparameterization is as follows: (1) A 3×3 convolution plus a BatchNorm can be equivalent to a 3×3 convolution with reconstructed parameters, as shown in the following formula: By replacing the independent variables in the BN layer equation with the convolutional layer formula, the following results are obtained: in , , and These represent the cumulative mean, standard deviation, scaling factor, and bias of the new convolutional layer after fusion, respectively. This represents the set of weights for the convolution kernel. This represents the mean value in the original batch normalized layer. This represents the bias in the original batch normalization layer.
[0018] (2) Assuming both input and output channels are 2, a 1×1 convolution can be considered a special 3×3 convolution, i.e., zeros are padded around the 1×1 convolution kernel. A 3×3 convolution with a BatchNorm can be equivalent to a 3×3 convolution with reconstructed parameters, such as... Figure 3 As shown; (3) Assuming that both the input and output channels are 2, the parameters of a 3×3 convolution are four 3×3 matrices. An identity mapping can also be viewed as a special 1×1 convolution, where the kernel is the identity matrix, such as... Figure 4 As shown; (4) An average pooling layer with C channels and a kernel size of 3 can be equivalently transformed into a special 3×3 convolutional layer with the same stride, as shown in the following formula: in This represents the weight tensor of the special convolutional kernel in the average pooling layer. This represents the kernel size.
[0019] Average pooling layers are applied to each channel of the input. For a given channel, let the kernel size be K. The padding of the kernel is the reciprocal of the square of its kernel size, i.e. The remaining channels are set to 0, as shown in the formula: in Represents the special convolution kernel weight tensor A specific two-dimensional slice, Indicates the index of the output channel.
[0020] Due to the additivity of convolutions, the five branches of each Rep5conv module can be merged into a single 3×3 convolution. Let... For input, For output, Indicates the first Convolution kernel with multiple branches, , , and These represent the cumulative mean, standard deviation, scaling factor, and bias of the Batch Normalization (BN) layer, respectively. When the input channel equals the output channel and the input height and width equal the output height and width, the following formula applies: This indicates a batch normalization operation. They represent the first The cumulative mean, standard deviation, scale factor, and bias of the batch normalized (BN) stratum of the branches. Indicates the first The convolution kernel weight matrix of the branch.
[0021] Wheat images often suffer from crowding and occlusion, and the background is quite complex. Therefore, to improve the performance of the output detector head and increase the amount of small target features captured, an efficient multi-scale attention mechanism (EMA) is incorporated. This mechanism groups some channel dimensions into batch dimensions, avoiding channel dimensionality reduction and preserving information in each channel. This results in a uniform distribution of spatial semantic features in each feature map, effectively capturing pixel-level attention feature values of small targets. The feature grouping and multi-scale structure of EMA effectively establish inter-dimensional dependencies, improving the detector's processing efficiency and generating accurate target localization information.
[0022] Efficient multi-scale attention mechanism (EMA) for any given feature map Different semantic learning is achieved by dividing the data into G sub-features according to the channel dimension. C represents the number of channels in the input feature map, and H and W represent the height and width of the feature map, respectively. The working principle is as follows: Figure 5 As shown.
[0023] EMA extracts attention weights from grouped feature maps through three parallel branches: two 1×1 branches and one 3×3 branch.
[0024] (1) A global average pooling operation is introduced on the 1×1 branch. 1D horizontal and 1D vertical global average pooling are used to encode the channel direction so that the channel descriptor can accurately describe the global position information. (2) After connecting the two spatial feature vectors, perform a 1×1 convolution operation to decompose the two feature vectors again. Use Sigmoid to limit the output value between 0 and 1, and reweight the weights of the original feature map channels to balance the difference between easy and difficult samples and categories. (3) In the 3×3 branch, Conv3×3 is used to capture multi-scale features, eliminating the need for GN normalization and average pooling operations, in order to extract multi-scale feature representations; (4) Use 2D global average pooling to encode the global spatial information of each branch output, and then convert the output channel features of these two branches into their corresponding dimensional shapes. , This is the global feature vector after channel grouping. (This represents a high-dimensional feature representation after spatial compression). The specific pooling formula is: in, This represents the global spatial information encoding result of the c-th channel, where c represents the channel index. This represents the feature value (activation value) of the input feature map at the c-th channel and spatial position (i, j).
[0025] (5) In order to capture pixel-level pairwise relationships and connect information in two directions, the SoftMax function and Matmul matrix dot product operation were used for fitting and local cross-channel interaction. (6) The value output by the Sigmoid function is reweighted with the original feature values so that the final output has the same dimension as the input feature map. ).
[0026] This method incorporates the EMA attention mechanism, combining channel information and contextual information, which helps to distinguish target wheat images under occlusion and crowding conditions, reduces environmental interference, and improves the model's detection capability.
[0027] Bounding box regression plays a crucial role in object detection, and the localization accuracy of object detection largely depends on the loss function of bounding box regression. Existing research improves regression performance by utilizing the geometric relationships between bounding boxes, while neglecting the impact of sample difficulty distribution on bounding box regression. To address this issue, this invention uses the Focaler-IoU loss function, which, by focusing on regression samples of varying difficulty, can improve the detector's performance in different detection tasks.
[0028] To focus on different regression samples and thus different detection tasks, this invention uses a linear interval mapping method to reconstruct the IoU loss. The formula is as follows: in This is the reconstructed focaler IoU, where IoU is the original IoU value, [d, u] ∈ [0,1]. IoU represents the intersection-union ratio, which is the ratio of the area of the overlapping portion of two bounding boxes to the area of their union, where d is the lower threshold and u is the higher threshold. By adjusting the values of d and u, it is possible to... The focus is different on the regression samples. This design allows the loss function to be sensitive to a certain range of IoU values, enabling it to pay more attention to samples with moderate overlap between the predicted and ground truth bounding boxes.
[0029] Bounding box regression loss The definition is as follows: Step 2: Construct an experimental dataset based on the global wheat dataset, dividing it into training, testing, and validation sets, and perform image preprocessing. The experimental dataset used in this invention comes from the Global Wheat Dataset, which contains over 3,000 training images from Europe (France, UK, Switzerland) and North America (Canada) and approximately 1,000 test images from Australia, Japan, and China. The images are outdoor field images, capturing the natural changes in the appearance of wheat heads. Data augmentation was performed on the dataset, including random brightness adjustments and exposure adjustments ranging from 15% to 15%. A total of 23,557 images were used: 20,400 for training, 1,579 for validation, and 1,578 for testing.
[0030] Step 3: Train the improved YOLOv8 network model; Step 3.1: Through the improved backbone network and Neck layer, multi-scale visual features and enhanced semantic representations are extracted from the input image, and finally, the optimized multi-level feature map is output. The Rep5Conv module, a structure-reparameterized convolutional layer, employs five parallel structures—identity mapping, BatchNorm, average pooling layers, 1×1 convolutions, and 3×3 convolutions—to extract visual features from the input image that fuse multi-scale local information. These features are then fed into the Neck layer, where an efficient multi-scale attention module (EMA) adaptively calibrates channel weights through grouped channels and parallel branching structures, resulting in a larger receptive field. This effectively suppresses interference from complex field backgrounds and enhances the response to key features of mutually occluded wheat ears. Finally, the network outputs optimized feature maps from the shallow, medium, and deep layers of the backbone network, providing high-quality features with both detail and semantics for subsequent detection heads.
[0031] Step 3.2: The optimized multi-level feature map is fed into the detection head, and through the anchor box mechanism and classification / regression convolutional layer, candidate prediction boxes and their class confidence scores are generated. The multi-scale feature maps, fused and enhanced by the Neck layer, are fed into the detection head. For each scale of feature map, the detection head applies a 1×1 convolution to predict anchor boxes. Specifically, multiple anchor boxes of different sizes and proportions are pre-defined for each spatial location, and the convolution operation outputs two types of information for each anchor box: first, bounding box regression parameters, used to fine-tune the position and size of the anchor box to better fit the real wheat ear target; second, class confidence, representing the probability that a wheat ear target exists within the anchor box. This step realizes the transformation from features to preliminary detection results, generating a large number of candidate predictions covering different scales and locations.
[0032] Step 3.3: Optimize the bounding box regression process using the Focaler-IoU loss function. By dynamically adjusting the attention given to samples of different difficulty levels, the localization accuracy and convergence speed are improved. Step 3.4: Using a label assignment strategy, match the real wheat ear targets with the preset anchor boxes to determine the positive and negative training samples. This process calculates the intersection-union ratio (IU) between predicted and ground truth boxes and combines strategies such as cross-grid matching to assign appropriate anchor boxes as positive samples for subsequent bounding box regression and classification loss calculation.
[0033] Step 3.5: Calculate the total loss and collaboratively optimize the classification and regression tasks. Total loss The weighted average of the bounding box regression loss and the classification loss is calculated as follows: in, , and The weighting coefficients are used to balance the two losses. Focus on accurate regression of bounding boxes; For classification loss, a binary cross-entropy loss function is used to optimize the prediction confidence of wheat ear category; For the target confidence loss, a binary cross-entropy function is also used to determine whether the predicted bounding box contains the target object. Through joint optimization of the total loss function, the model is ensured to improve synergistically in both localization and classification tasks.
[0034] Step 3.6: Filter out redundant detection boxes through non-maximum suppression post-processing. The detection head outputs numerous predicted bounding boxes containing a large number of overlapping redundant boxes. To address this, a weighted non-maximum suppression post-processing algorithm is employed. This algorithm first sorts all predicted boxes according to class confidence, then selects the box with the highest confidence and calculates its Intersection over Union (IoU) with other overlapping boxes. For adjacent boxes with an IoU exceeding a set threshold, instead of direct suppression, a weighted fusion is performed based on their confidence, ultimately retaining an optimal box that combines location and confidence. This method effectively alleviates the problem of missed detections in densely packed wheat ears and generates cleaner, more accurate final detection results.
[0035] Step 3.7: Update and improve the parameters of the YOLOv8 network model. The gradient of the model parameters with respect to the loss is calculated using the backpropagation algorithm. The AdamW optimizer, leveraging its adaptive learning rate and weight decay advantages, is employed to update all parameters of the improved YOLOv8 network based on the gradient information. This includes the Rep5Conv module in the backbone, the EMA attention module in the Neck layer, and the convolutional weights of the detection head. This iterative process is repeated until the model converges, ultimately yielding a high-performance, robust wheat ear detection model.
[0036] Step 4: Use the validation set to evaluate the performance of the improved YOLOv8 network model and optimize the parameters. Step 5: Input the image of the target wheat ear region to be detected into the trained improved YOLOv8 network model, and output the detection results. This invention uses the average accuracy (mean value) ), recall rate ( ) and accuracy ( The performance of the model is measured using evaluation metrics such as [list of metrics]. Detailed evaluation metrics are as follows: Mean precision ( ): refers to the detection accuracy of each category in the dataset ( The mean of ). The larger the value, the higher the average accuracy of the model. The calculation formula is as follows: in, Indicates the number of target categories. Indicates the number of detection categories. This is the PR curve. The area under the PR curve for a certain category.
[0037] Where mAP@50 represents the average accuracy when the IoU threshold between the predicted box and the ground truth box is 0.5, and mAP@0.5:0.95 represents the average mAP at different IoU thresholds from 0.5 to 0.95 with a step size of 0.05.
[0038] Recall rate ( ): This refers to the proportion of target objects detected by the model out of all actual target objects. The calculation formula is as follows: To predict the number of positive samples in the undetected category, To predict the number of positive samples of the correct class.
[0039] The accuracy rate of target detection reflects the effectiveness of the algorithm. The calculation formula is: FP is the number of negative samples that correctly predict the class.
[0040] Experimental Section: PyTorch was used as the deep learning model framework, conducted on an Ubuntu 20.04 system with CUDA version 11.3. The GPU was an RTX 4090 24GB, the CPU was a Xeon(R) Platinum 8352V, 90GB of memory, the number of iterations was 300, the batch size was set to 16, the AdamW optimizer was used, the initial learning rate was 0.01, the momentum parameter was set to 0.937, and the image input size was 640×640.
[0041] To verify the effectiveness of the proposed invention, it was compared with different detection algorithms on a global wheat dataset, including YOLOv5, YOLOv6, YOLOv7, YOLOv8n and YOLOv8s from the YOLO series.
[0042] To further verify the performance of the improved model, the model of this invention was compared with the YOLO series algorithms, as shown in Table 1.
[0043] Table 1 Performance comparison between the model of this invention and the YOLO series algorithms As shown in Table 1, compared with the YOLO series algorithms, the model of this invention significantly outperforms YOLOv5, YOLOv6, YOLOv7, YOLOv8n, and YOLOv8 in various metrics. Specifically, in terms of mAP@0.5, this invention improves upon YOLOv5, YOLOv6, YOLOv7, YOLOv8n, and YOLOv8 by 9.133%, 10.216%, 5.175%, 4.822%, and 2.238%, respectively. In the more stringent mAP@0.5:0.95 metric, the corresponding improvements are 11.090%, 9.557%, 8.746%, 5.443%, and 3.018%, respectively. Furthermore, in terms of precision, the present invention outperforms the comparison model by 9.471%, 8.790%, 3.913%, 3.030%, and 1.222%, respectively; and in terms of recall, it also improves by 14.532%, 8.512%, 5.681%, 5.704%, and 3.238%, respectively.
[0044] To demonstrate the superiority of the various improved modules proposed in this invention, an ablation experiment was designed and conducted, as shown in Table 2.
[0045] Table 2 Ablation Experiment Results As shown in Table 2, regarding the feature network, replacing the convolutional module with the Rep5CONV module improved mAP@0.5 and mAP@0.5-0.95 by 1.083% and 0.927% respectively compared to the original baseline, and also improved accuracy and recall by 0.467% and 0.985% respectively. If the EMA attention mechanism is added, compared to adding only the Rep5conv module, mAP@0.5 and MAP@0.5-0.95 improved by 0.632% and 0.234% respectively, recall also improved by 0.034%, and precision improved by 0.51%. Furthermore, replacing the loss function with the Focaler-IoU loss function improved mAP@0.5 and MAP@0.5-0.95 by 0.523% and 1.745% respectively; accuracy and recall also improved by 0.245% and 2.223% respectively. Integrating all modules improves precision by 1.222%, recall by 3.242%, and mAP@0.5 and MAP@0.5-0.95 by 2.238% and 3.018%, respectively, compared to the original model. This demonstrates that the algorithm constructed in this invention can significantly improve the average precision of object detection while maintaining model precision and recall.
[0046] To further reveal the feature extraction mechanism and performance advantages of the improved YOLOv8 algorithm of this invention, the feature response of the target region is visualized and analyzed. The feature heatmap can clearly show the degree of interest of the model in the target region. The subsequent gated image detection all show the target region cropped from the detection result image, as shown in Figure 6.
[0047] As can be seen from the feature heatmap in Figure 6(a), the basic algorithm exhibits some missed detections, and a small portion of the target area appears red. With the algorithm improvement, the red area in Figure 6(b) becomes larger and more pronounced, and the missed targets are detected. Comparing Figures 6(a) and 6(b), it can be seen that the feature heatmap of the improved YOLOv8 algorithm shows clearer targets, stronger shape regularity, and better adaptation to the shape features of the object itself. It can extract better target features, extract more target features for small targets with poor resolution to support accurate prediction, and capture richer target information for occluded targets, ensuring the stability of detection performance. The improved YOLOv8 algorithm extracts more features, can better predict targets, and can meet the detection needs of small targets and occluded targets in complex scenes.
[0048] Figure 7 shows the detection performance of the YOLOv8 algorithm and the improved YOLOv8 algorithm on the global wheat dataset. In Figure 7(a), the original YOLOv8 algorithm detected 24 wheat targets in the global wheat dataset image; in Figure 7(b), the improved YOLOv8 algorithm detected 27 wheat targets. Comparing Figure 7(a) and Figure 7(b), it can be seen that the original YOLOv8 algorithm has insufficient feature extraction when dealing with small targets or targets that are mutually occluded, resulting in missed detections. Under the same conditions, a heavily parameterized convolutional module is first added to the YOLOv8 backbone network to enhance the multi-scale feature fusion capability. By fusing the feature pyramid and the large kernel separable convolutional module, the network's sensitivity to occluded targets is improved. An EMA attention mechanism is introduced to improve the performance of the output detection head and increase the amount of small target features captured. The improved YOLOv8 algorithm enables the better detection of small targets and occluded targets.
Claims
1. A method for detecting wheat ears based on an improved YOLOv8, characterized in that, Includes the following steps: Step 1: Build an improved YOLOv8 network model. The improved YOLOv8 network model is based on the YOLOv8 architecture and includes a structure-reparameterized convolutional Rep5Conv module set in the backbone network, an efficient multi-scale attention EMA module set in the Neck layer, and a Focaler-IoU loss function for model training. The Rep5Conv module with reparameterized convolutions adopts a parallel structure, including iso-mapping, BatchNorm, average pooling layers, 1×1 convolutions and 3×3 convolutions. It merges multiple branches into a single 3×3 convolution structure through convolution reparameterization. The efficient multi-scale attention EMA module can handle any given feature map. Different semantic learning is achieved by dividing the data into G sub-features according to the channel dimension. C represents the number of channels in the input feature map, and H and W represent the height and width of the feature map, respectively. The IoU loss is reconstructed using a linear interval mapping method, as shown in the following formula: in It is the reconstructed focaler-IoU, where IoU is the original IoU value, [d, u] ∈ [0, 1]; Where IoU represents the intersection-union ratio, which is the ratio of the area of the overlapping portion of two bounding boxes to the area of their union portion. d is a lower threshold, and u is a higher threshold; by adjusting the values of d and u, one can... Different regression samples have different focuses, and the bounding box regression loss is different. The definition is as follows: ; Step 2: Construct an experimental dataset based on the global wheat dataset, divide it into training, testing, and validation sets, and perform image preprocessing; Step 3: Train the improved YOLOv8 network model; Step 3.1: Through the improved backbone network and Neck layer, multi-scale visual features and enhanced semantic representations are extracted from the input image, and finally the optimized multi-level feature map is output. Step 3.2: The optimized multi-level feature map enters the detection head and generates candidate prediction boxes and their category confidence scores through the anchor box mechanism and classification and regression convolutional layer. Step 3.3: Optimize the bounding box regression process using the Focaler-IoU loss function. By dynamically adjusting the attention given to samples of different difficulty, the localization accuracy and convergence speed can be improved. Step 3.4: Using a label assignment strategy, match the real wheat ear targets with the preset anchor boxes to determine the positive and negative training samples; Step 3.5: Calculate the total loss Collaborative optimization of classification and regression tasks; Step 3.6: Filter out redundant detection boxes through non-maximum suppression post-processing; Step 3.7: Update and improve the parameters of the YOLOv8 network model; Step 4: Use the validation set to evaluate the performance of the improved YOLOv8 network model and optimize the parameters; Step 5: Input the image of the target wheat ear region to be detected into the trained improved YOLOv8 network model and output the detection results.
2. The wheat ear detection method based on improved YOLOv8 according to claim 1, characterized in that, The reparameterization method for the Rep5Conv module described in step 1 includes the following: (1) A 3×3 convolution plus a BatchNorm can be equivalent to a 3×3 convolution with reconstructed parameters, as shown in the following formula: By replacing the independent variable in the BN layer equation with the convolutional layer formula, the following result is obtained: in , , and These represent the cumulative mean, standard deviation, scaling factor, and bias of the new convolutional layer after fusion, respectively. This represents the set of weights for the convolution kernel. This represents the mean value in the original batch normalized layer. This represents the bias in the original batch normalization layer; (2) Assuming that both the input and output channels are 2, a 1×1 convolution can be regarded as a special 3×3 convolution, that is, zeros are padded around the 1×1 convolution kernel, and a 3×3 convolution plus a BatchNorm can be equivalent to a 3×3 convolution with reconstructed parameters. (3) Assuming that both the input and output channels are 2, the parameters of the 3×3 convolution are four 3×3 matrices, and an identity mapping can be regarded as a special 1×1 convolution, that is, the convolution kernel is the identity matrix; (4) An average pooling layer with C channels and a kernel size of 3 can be equivalently transformed into a special 3×3 convolutional layer with the same stride, as shown in the following formula: in This represents the weight tensor of the special convolutional kernel in the average pooling layer. The kernel size; Average pooling layers are applied to each channel of the input. For a given channel, let the kernel size be K. The padding of the kernel is the reciprocal of the square of its kernel size, i.e. The remaining channels are set to 0, as shown in the formula: in Represents the special convolution kernel weight tensor A specific two-dimensional slice, Indicates the index of the output channel; Due to the additivity of convolutions, the five branches of each Rep5conv module can be merged into a single 3×3 convolution. For input, For output, Indicates the first Convolution kernel with multiple branches, , , and These represent the cumulative mean, standard deviation, scaling factor, and bias of the BN layer, respectively. When the input channel equals the output channel and the input height and width equal the output height and width, the following formula applies: This indicates a batch normalization operation. They represent the first The cumulative mean, standard deviation, scale factor, and bias of the batch-normalized BN layer of the branch. Indicates the first The convolution kernel weight matrix of the branch.
3. The wheat ear detection method based on improved YOLOv8 according to claim 2, characterized in that, The implementation mechanism of the efficient multi-scale attention EMA module described in step 1 includes the following: The efficient multi-scale attention EMA module extracts attention weights from grouped feature maps through three parallel branches, namely two 1×1 branches and one 3×3 branch. (1) Introduce global average pooling operation on the 1×1 branch, and use 1D horizontal and 1D vertical global average pooling to encode the channel direction so that the channel descriptor can accurately describe the global position information. (2) After connecting the two spatial feature vectors, perform a 1×1 convolution operation to decompose the two feature vectors again. Use Sigmoid to limit the output value between 0 and 1, and reweight the weights of the original feature map channels to balance the difference between easy and difficult samples and categories. (3) In the 3×3 branch, Conv3×3 is used to capture multi-scale features, omitting GN normalization and average pooling operations, in order to extract multi-scale feature representations; (4) Encode the global spatial information of each branch output using 2D global average pooling, and then convert the output channel features of these two branches into their corresponding dimensional shapes, i.e. , This is the global feature vector after channel grouping. This represents a high-dimensional feature representation after spatial compression. The specific pooling formula is as follows: ; in, This represents the global spatial information encoding result of the c-th channel, where c represents the channel index. This represents the feature value of the input feature map at the c-th channel and spatial position (i, j); (5) Use the SoftMax function and Matmul matrix dot product operation to perform fitting and local cross-channel interaction, establish pixel-level pairwise relationships and realize bidirectional information connection; (6) The value output by the Sigmoid function is reweighted with the original feature value so that the final output has the same dimension as the input feature map.
4. The wheat ear detection method based on improved YOLOv8 according to claim 3, characterized in that, Step 3.2 The detection head applies 1×1 convolution to predict anchor boxes for each feature map at each scale: multiple anchor boxes of different sizes and proportions are preset for each spatial location, and two types of information are output for each anchor box through convolution operation: bounding box regression parameters and class confidence.
5. The wheat ear detection method based on improved YOLOv8 according to claim 4, characterized in that, The total loss described in step 3.5 The weighted average of the bounding box regression loss and the classification loss is calculated as follows: in, , and To balance the weighting coefficients of the two losses, For classification loss, it is responsible for optimizing the prediction confidence of wheat ear categories; The target confidence loss is used to determine whether the predicted bounding box contains the target object.
6. A wheat ear detection method based on an improved YOLOv8 according to any one of claims 1-5, characterized in that, The evaluation metrics for the improved YOLOv8 network model include mean accuracy. Recall rate and accuracy ; Mean accuracy The mean AP (Accuracy) of detection for each class in the dataset. The larger the value, the higher the average accuracy of the model. The calculation formula is as follows: in, Indicates the number of target categories. Indicates the number of detection categories. This is the PR curve. denoted as the area under the PR curve for a certain category; where mAP@50 represents the average precision when the IoU threshold between the predicted box and the ground truth box is 0.5, and mAP@0.5:0.95 represents the average mAP over different IoU thresholds from 0.5 to 0.95 with a step size of 0.05; Recall rate This refers to the proportion of target objects detected by the model out of all actual target objects. The calculation formula is as follows: To predict the number of positive samples in the undetected category, To predict the number of positive samples of the correct class; The accuracy rate of target detection reflects the effectiveness of the algorithm. The calculation formula is: FP is the number of negative samples that correctly predict the class.