Fruit detection method and system in complex environment based on improved YOLOv8n and application
By improving the YOLOv8n model, replacing the C2f module with the efficient convolution module ME and introducing the Inner-IoU loss function, the problem of low citrus fruit detection accuracy in complex environments is solved, achieving more efficient detection accuracy and computing performance.
Patent Information
- Application Number
- CN202410375546.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-03-29
- Publication Date
- 2024-08-23
AI Technical Summary
The existing technology does not have high detection accuracy for citrus fruits in complex environments, requires a large amount of calculation, and is difficult to handle situations where the fruits and leaves have similar colors, there are many fruits, and the occlusion overlaps are serious.
The improved YOLOv8n target detection model is adopted, by replacing the C2f module in the YOLOv8 model with the efficient convolution module ME, increasing multi-scale feature extraction capabilities, and introducing the Inner-IoU loss function to optimize model training and detection performance.
It significantly improves the accuracy of fruit detection in complex environments, reduces the amount of model parameters and calculations, and improves the accuracy and average precision of detection.
Smart Images

Figure CN118537718A
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of target detection methods, and in particular to the technical field of fruit detection using YOLOv8n. Background Art
[0002] With the aging of the population and the increase in labor costs year by year, it is becoming increasingly necessary to carry out efficient and automated harvesting of fruits such as citrus, which have large planting areas and high labor intensity. The application of target detection algorithms in this process is also becoming more and more extensive.
[0003] For example, the existing patent document CN202310392025.X discloses a citrus fruit recognition method and system based on improved YOLOv7, which realizes accurate recognition and positioning of citrus fruits through the improved YOLOv7 network recognition model; the existing patent document CN202210787701.9 discloses a citrus fruit recognition method based on the improved YOLOv5 model, which realizes intelligent recognition and yield monitoring of green citrus fruits.
[0004] However, the above existing technologies usually require large amounts of computation and can only be applied to relatively single environments. In complex field planting environments, the detection accuracy of fruits is not high enough. Summary of the invention
[0005] In view of the defects of the prior art, the purpose of the present invention is to provide a new fruit detection method and system. The method is established based on an improved YOLOv8n target detection model, which can effectively improve the mean average precision (mAP) of detecting fruits such as citrus fruits with similar color characteristics to leaves, numerous fruits and serious occlusion and overlap in complex environments, and further reduce the number of model parameters and calculation amount on the basis of greatly improved accuracy and detection effect.
[0006] The technical solution of the present invention is as follows:
[0007] The fruit detection method in complex environment based on improved YOLOv8n includes:
[0008] S1 obtains a set of fruit images in a complex environment that have been collected and preprocessed on the spot, and divides them into a training set, a test set, and a validation set;
[0009] S2 inputs the images in the training set into the improved YOLOv8n model for training;
[0010] During training, S3 optimizes and adjusts the improved YOLOv8n model through the loss function to obtain the optimized and adjusted improved YOLOv8n model;
[0011] S4 tests and verifies the optimized and adjusted improved YOLOv8n model through the test set and the verification set, and detects fruits with the model that passes the test and verification;
[0012] The improved YOLOv8n model is to perform the following processing in the YOLOv8 model: replace the third and fourth C2f modules from top to bottom in the YOLOv8 backbone network with C2f-ME modules improved by the efficient convolution module ME, and replace all C2f modules except the C2f module connected to the top-level detection head in the YOLOv8 head network with C2f-ME modules; wherein the C2f-ME module is obtained by replacing the Bottleneck module in the C2f module of the YOLOv8 model with the Bottleneck-ME module improved by the efficient convolution module ME, and the Bottleneck-ME module is obtained by replacing the second convolution module from top to bottom in the Bottleneck module of YOLOv8 with the efficient convolution module ME; the efficient convolution module ME has the following structure:
[0013] An input layer containing four groups of feature channels with equal number of channels;
[0014] The four groups of feature channels of the input layer are subjected to 1×1 convolution, 3×3 convolution, 5×5 convolution and the first convolution layer of 7×7 convolution respectively;
[0015] A concatenation layer for concatenating the four sets of feature values obtained by the first convolutional layer;
[0016] The second convolutional layer performs 1×1 convolution on the concatenated features obtained from the concatenation layer;
[0017] The output layer outputs the feature values obtained by the second convolutional layer.
[0018] According to some preferred embodiments of the present invention, the loss function is an improved loss function obtained by improving the loss function of the YOLOv8 model, and the improvement is to replace the CIoU function in the loss function of the YOLOv8 model with the following Inner-IoU function L inner-CIoU get:
[0019] L inner-CIoU =L CIoU +IoU-IoU inner
[0020] in,
[0021] L CIoU The CIoU function is represented as follows:
[0022]
[0023]
[0024]
[0025] IoU represents the following intersection-over-union function:
[0026]
[0027] IoU inner The loss function is expressed as follows:
[0028]
[0029] in,
[0030] inter=(min(b r gt , b r )-max(b l gt , b l ))·(min(b b gt , b b )-max(b t gt , b t ))
[0031] union=(w gt ·h gt )·(ratio) 2 +(w*h)·(ratio) 2 -inter
[0032]
[0033]
[0034]
[0035]
[0036] Among them, v represents the trade-off parameter, α represents the consistency parameter of the aspect ratio, ρ() represents the Euclidean distance, b represents the center point of the anchor box, and b gt represents the center point of the true bounding box, c represents the diagonal of the minimum bounding box, and h gt represents the height of the ground-truth bounding box, w gt represents the width of the true bounding box, h represents the height of the anchor box, w represents the width of the anchor box, B represents the area of the anchor box, and B gtrepresents the area of the true bounding box, inter represents the intersection area of the auxiliary anchor box and the auxiliary bounding box, union represents the merged area of the auxiliary anchor box and the auxiliary bounding box, and b r gt Indicates the horizontal coordinate of the right boundary of the auxiliary bounding box, b r Indicates the horizontal coordinate of the right boundary of the auxiliary anchor box, b l gt Indicates the horizontal coordinate of the left boundary of the auxiliary bounding box, b l Indicates the horizontal coordinate of the left boundary of the auxiliary anchor box, b b gt Indicates the ordinate of the upper boundary of the auxiliary bounding box, b b Indicates the ordinate of the upper boundary of the auxiliary anchor box, b t gt Indicates the ordinate of the lower boundary of the auxiliary bounding box, b t represents the ordinate of the lower boundary of the auxiliary anchor box, and ratio represents the scale factor. (x c gt ,y c gt ) is the horizontal and vertical coordinates of the center point of the real bounding box, (x c ,y c ) are the horizontal and vertical coordinates of the center point of the anchor box.
[0037] According to some preferred embodiments of the present invention, the training, testing and verification in S2-S4 include:
[0038] (1) using the training set as the input of the improved YOLOv8n model, outputting a fused feature map through the improved YOLOv8n model and obtaining weight parameters, and optimizing and adjusting the model through the loss function in this process;
[0039] (2) using the weight parameters to predict the validation set using the optimized and adjusted improved YOLOv8n model, and obtaining performance indicators of the optimized and adjusted improved YOLOv8n model based on the prediction results;
[0040] (3) adjusting the hyperparameters of the optimized and adjusted improved YOLOv8n model according to the performance indicators;
[0041] (4) Repeat the above steps (1)-(3) until the optimized and adjusted improved YOLOv8n model obtains the optimal weight parameters;
[0042] (5) Using the optimal weight parameters to test the model generalization ability on the test set to determine the effectiveness and feasibility of the model.
[0043] The present invention further provides a detection system for implementing the above-mentioned fruit detection method in a complex environment, which comprises: an image storage and preprocessing module for performing step S1 and a model storage and detection module connected to the image storage and preprocessing module for performing steps S2-S4.
[0044] The present invention further provides an application method of the above-mentioned fruit detection method or detection system under complex environment, which is to apply the detection method or system to an automatic citrus detection device.
[0045] The fruit detection method in a complex environment based on the improved YOLOv8n proposed in the present invention uses a more efficient convolution module ME, which improves the calculation speed of the Bottleneck module in the original YOLOv8 model and reduces the number of parameters, and has the ability to extract features at more scales, further improving the fruit detection accuracy in a complex environment. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] Figure 1 Flow chart of the fruit detection method of the present invention in a specific embodiment.
[0047] Figure 2 It is the overall structure diagram of the YOLOv8 model in the prior art.
[0048] Figure 3 It is a structural diagram of the C2f module in the YOLOv8 model in the prior art.
[0049] Figure 4 It is the overall structure diagram of the improved YOLOv8n model in the specific implementation method.
[0050] Figure 5 It is a structural diagram of the C2f-ME module in the improved YOLOv8n model in a specific implementation manner.
[0051] Figure 6 It is a schematic diagram of the structure of the efficient convolution module ME in a specific implementation method.
[0052] Figure 7 It is a schematic diagram of the calculation meaning of the Inner-IoU loss function in a specific implementation method.
[0053] Figure 8 These are some citrus images collected on site in Example 1. DETAILED DESCRIPTION
[0054] The present invention is described in detail below in conjunction with the embodiments and drawings, but it should be understood that the embodiments and drawings are only used to exemplify the present invention and do not constitute any limitation on the protection scope of the present invention. All reasonable changes and combinations within the scope of the inventive concept of the present invention fall within the protection scope of the present invention.
[0055] See attached Figure 1 In some specific embodiments, the fruit detection method in a complex environment based on YOLOv8n of the present invention comprises the following steps:
[0056] S1 obtains a set of fruit images in complex environments that have been collected and preprocessed on-site, and divides them into a training set, a test set, and a validation set.
[0057] The complex environment includes an environment with obvious fruit overlap, shading by branches and leaves, and an environment with significant changes in lighting conditions.
[0058] The preprocessing may be one or more of data enhancement processing, cropping processing, screening processing, labeling processing, etc.
[0059] In some more specific embodiments, the pretreatment comprises:
[0060] (1) Remove blurry, ghosting, and similar images from fruit images collected in complex environments to obtain filtered fruit images;
[0061] (2) The selected fruit images are annotated in YOLO format using LabelImg annotation software to ensure that the bounding box fits the fruit outline and does not contain excessive invalid information, thus obtaining an annotated image;
[0062] (3) The annotated images are subjected to enhancement processing such as cropping, translation, flipping, adding Gaussian noise, and randomly rotating at any angle to obtain enhanced annotated images. The enhanced annotated images are used to form a fruit image dataset for further division into training sets, test sets, and validation sets.
[0063] S2 inputs the images in the training set into the improved YOLOv8n model for training.
[0064] Among them, refer to the attached Figure 4-5 The improved YOLOv8n model is as shown in the attached Figure 2-3In the existing YOLOv8 model shown in FIG, the following processing is performed: the third and fourth C2f modules from top to bottom in the YOLOv8 backbone network (Backbone) (the YOLOv8 backbone network includes 4 layers of C2f modules from top to bottom) are replaced with C2f-ME modules improved by the efficient convolution module ME, and all C2f modules except the C2f module connected to the top-level detection head (Detect) in the YOLOv8 head network (Head) are replaced with C2f-ME modules; wherein the C2f-ME module is obtained by replacing the Bottleneck module in the C2f module of the YOLOv8 model with the Bottleneck-ME module improved by the efficient convolution module ME, and the Bottleneck-ME module is obtained by replacing the second convolution module (Conv) from top to bottom in the Bottleneck module of the YOLOv8 with the C2f-ME module shown in FIG. Figure 6 The efficient convolution module ME shown is obtained, and the efficient convolution module ME processes the output features of the first convolution module to improve the calculation speed of the original Bottleneck module, reduce its parameter amount, and improve the accuracy of multi-scale feature extraction.
[0065] The structure of the efficient convolution module ME is as shown in the attached figure. Figure 6 As shown, it includes: an input layer containing four groups of feature channels with equal number of channels; a first convolutional layer that performs 1×1 convolution, 3×3 convolution, 5×5 convolution and 7×7 convolution on the four groups of feature channels of the input layer respectively; a splicing layer that splices the four groups of feature values obtained by the first convolutional layer; a second convolutional layer that performs 1×1 convolution on the spliced features obtained by the splicing layer; and an output layer that outputs the feature values obtained by the second convolutional layer.
[0066] According to the above structure, the high-efficiency convolution module ME processes the features input therein as follows:
[0067] The input features are divided into 4 groups of feature channels with a channel number of c through the input layer, and then the feature channels are processed by 1×1, 3×3, 5×5, and 7×7 convolutions through the first convolution layer to obtain 4 groups of features of different scales with a channel number of n;
[0068] Then, the concatenation layer of the Concat function is used to concatenate the four groups of features of different scales with n channels into a concatenated feature with a length of 4n.
[0069] Then, the concatenated features are convolved point by point through the 1×1 convolution of the second convolutional layer to obtain the output features.
[0070] Compared with the ordinary convolution using a convolution kernel of size 3 in the original YOLOv8 model, the computational complexity of the above efficient convolution module of the present invention can be reduced to 7 / 12 of the original computational complexity, and the network's attention to feature information of different scales is enhanced. Finally, through 1×1 point-by-point convolution processing, the parameter amount is significantly reduced and the feature information exchange of each scale is completed.
[0071] According to the above model structure, after the image in the training set is input, the backbone network obtains feature maps of different semantic levels and scales through downsampling, and the head network comprehensively utilizes the feature information of different scales output by the backbone network through upsampling and feature fusion of each layer, and outputs the feature information. Subsequently, the detection head with a decoupled head structure is used to perform target detection on the output feature information, predict the bounding box and its category and confidence.
[0072] During training, S3 optimizes and adjusts the improved YOLOv8n model through an improved loss function.
[0073] Among them, the improved loss function is obtained by replacing the CIoU function in the loss function of the original YOLOv8 model (the loss function of the original YOLOv8 model includes an anchor box loss function and a classification loss function, wherein the anchor box loss function is a CIoU function and a DFL function, and the classification loss function is a BCE function) with the Inner-IoU function.
[0074] Among them, the expression of CIoU function L CIoU as follows:
[0075]
[0076]
[0077]
[0078] Among them, v represents the trade-off parameter, α represents the consistency parameter of the aspect ratio, ρ() represents the Euclidean distance, b represents the center point of the anchor box, and b gt represents the center point of the true bounding box, c represents the diagonal of the minimum bounding box, and h gt represents the height of the ground-truth bounding box, w gt represents the width of the true bounding box, h represents the height of the anchor box, w represents the width of the anchor box, and IoU represents the following intersection-over-union ratio:
[0079]
[0080] Among them, B represents the area of the anchor box, B gt Indicates the area size of the true bounding box.
[0081] The above CIoU loss function only considers the shape loss. The expression of the Inner-IoU function obtained by improving it in this invention is L inner-CIoU as follows:
[0082] L inner-CIoU =L CIoU +IoU-IoU inner
[0083] Among them, IoU inner The loss function is expressed as follows:
[0084]
[0085] in,
[0086] inter=(min(b r gt , b r )-max(b l gt , b l ))·(min(b b gt , b b )-max(b t gt , b t ))
[0087] union=(w gt ·h gt )·(ratio) 2 +(w*h)·(ratio) 2 -inter
[0088]
[0089]
[0090]
[0091]
[0092] Among them, inter represents the intersection area of the auxiliary anchor box and the auxiliary bounding box, union represents the merged area of the auxiliary anchor box and the auxiliary bounding box, and b r gt Indicates the horizontal coordinate of the right boundary of the auxiliary bounding box, b r Indicates the horizontal coordinate of the right boundary of the auxiliary anchor box, b l gt Indicates the horizontal coordinate of the left boundary of the auxiliary bounding box, b l Indicates the horizontal coordinate of the left boundary of the auxiliary anchor box, b b gtIndicates the ordinate of the upper boundary of the auxiliary bounding box, b b Indicates the ordinate of the upper boundary of the auxiliary anchor box, b t gt Indicates the ordinate of the lower boundary of the auxiliary bounding box, b t represents the ordinate of the lower boundary of the auxiliary anchor box, and ratio represents the scale factor. (x c gt ,y c gt ) represents the horizontal and vertical coordinates of the center point of the real bounding box, (x c ,y c ) represents the horizontal and vertical coordinates of the center point of the anchor box.
[0093] The Inner-IoU loss function used in the present invention introduces a scale factor to control the scale of the auxiliary bounding box, which can accelerate the bounding box regression. When the ratio is less than 1 and the auxiliary bounding box size is smaller than the actual bounding box, the effective range of regression is smaller than the IoU loss, but the absolute value of the gradient is greater than the gradient obtained by the IoU loss, which can accelerate the convergence of high IoU samples. On the contrary, when the ratio is greater than 1, the larger scale of the auxiliary bounding box expands the effective range of regression and enhances the effect of low IoU sample regression, as shown in the attached figure. Figure 7 The above method can overcome the limitation of weak generalization ability of existing methods by using auxiliary bounding boxes of different scales for the detector.
[0094] The improvement of the loss function makes up for the problems of weak generalization and slow convergence of the CIoU loss function in detection tasks, can make the prediction of subsequent detection anchor boxes more accurate, and to a certain extent improve the accuracy of the network model.
[0095] S4 tests and verifies the optimized and adjusted improved YOLOv8n model through the test set and the validation set, and detects fruits with the tested and verified model.
[0096] The training, testing and verification in the above process S2-S4 can be specifically as follows:
[0097] (1) The training set is used as the input of the improved YOLOv8n model, and the fused feature map is output by the improved YOLOv8n model to obtain the weight parameters. In this process, the model is optimized and adjusted through the improved loss function;
[0098] (2) The optimized and adjusted improved YOLOv8n model is used to predict the validation set through weight parameters, and the performance index of the optimized and adjusted improved YOLOv8n model is obtained according to the prediction results;
[0099] (3) Adjust the hyperparameters of the optimized and adjusted improved YOLOv8n model according to the performance indicators;
[0100] (4) Repeat the above steps (1)-(3) until the optimized and adjusted improved YOLOv8n model obtains the optimal weight parameters;
[0101] (5) Use the optimal weight parameters to test the model generalization ability on the test set to determine the effectiveness and feasibility of the model.
[0102] Example 1
[0103] Citrus fruit detection is performed through the process of the above specific implementation method, wherein the citrus images collected on site include images in front light, back light, leaf occlusion, fruit overlap, distant front light, and distant back light environments, such as Figure 8 As shown in Figure 1, the citrus image dataset obtained after preprocessing contains 9135 samples in total, which are divided into training set, test set and validation set in a ratio of 7:2:1.
[0104] The test results show that the average precision mAP@0.5 value of the improved YOLOv8n model of the present invention is 96.9%, the recall rate is 91.7%, the average precision mAP@0.5:0.95 value is 85.8%, the model size is 5.8MB, and the parameter volume is 2.87M. Compared with the original model YOLOv8n, its mAP@0.5 value, recall rate, and mAP@0.5:0.95 value are increased by 0.4, 1.0, and 0.6 percentage points respectively.
[0105] The above embodiments are only preferred implementations of the present invention, and the protection scope of the present invention is not limited to the above embodiments. All technical solutions under the concept of the present invention belong to the protection scope of the present invention. It should be pointed out that for ordinary technicians in this technical field, improvements and modifications without departing from the principle of the present invention should also be regarded as the protection scope of the present invention.
Claims
1. A fruit detection method in a complex environment based on improved YOLOv8n, characterized in that: It includes: S1 obtains a set of fruit images in a complex environment that have been collected and preprocessed on the spot, and divides them into a training set, a test set, and a validation set; S2 inputs the images in the training set into the improved YOLOv8n model for training; During training, S3 optimizes and adjusts the improved YOLOv8n model through the loss function to obtain the optimized and adjusted improved YOLOv8n model; S4 tests and verifies the optimized and adjusted improved YOLOv8n model through the test set and the verification set, and detects fruits with the model that passes the test and verification; The improved YOLOv8n model is to perform the following processing in the YOLOv8 model: replace the third and fourth C2f modules from top to bottom in the YOLOv8 backbone network with C2f-ME modules improved by the efficient convolution module ME, and replace all C2f modules except the C2f module connected to the top-level detection head in the YOLOv8 head network with C2f-ME modules; wherein the C2f-ME module is obtained by replacing the Bottleneck module in the C2f module of the YOLOv8 model with the Bottleneck-ME module improved by the efficient convolution module ME, and the Bottleneck-ME module is obtained by replacing the second convolution module from top to bottom in the Bottleneck module of YOLOv8 with the efficient convolution module ME; the efficient convolution module ME has the following structure: An input layer containing four groups of feature channels with equal number of channels; The four groups of feature channels of the input layer are subjected to 1×1 convolution, 3×3 convolution, 5×5 convolution and the first convolution layer of 7×7 convolution respectively; A concatenation layer for concatenating the four sets of feature values obtained by the first convolutional layer; The second convolutional layer performs 1×1 convolution on the concatenated features obtained from the concatenation layer; The output layer outputs the feature values obtained by the second convolutional layer.
2. The method for detecting fruit in a complex environment according to claim 1, characterized in that: The loss function is an improved loss function obtained by improving the loss function of the YOLOv8 model, and the improvement is to replace the CIoU function in the loss function of the YOLOv8 model with the following Inner-IoU function L inner-CIoU get: L inner-CIoU =L CIoU +IoU-IoU inner in, L CIoU The CIoU function is represented as follows: IoU represents the following intersection-over-union function: IoU inner The loss function is expressed as follows: in, inter=(min(b r gt ,b r )-max(b l gt ,b l ))·(min(b b gt ,b b )-max(b t gt ,b t )) union=(w gt ·h gt )·(ratio) 2 +(w*h)·(ratio) 2 -inter Among them, v represents the trade-off parameter, α represents the consistency parameter of the aspect ratio, ρ() represents the Euclidean distance, b represents the center point of the anchor box, and b gt represents the center point of the true bounding box, c represents the diagonal of the minimum bounding box, and h gt represents the height of the ground-truth bounding box, w gt represents the width of the true bounding box, h represents the height of the anchor box, w represents the width of the anchor box, B represents the area of the anchor box, and B gt represents the area of the true bounding box, inter represents the intersection area of the auxiliary anchor box and the auxiliary bounding box, union represents the merged area of the auxiliary anchor box and the auxiliary bounding box, and b r gt Indicates the horizontal coordinate of the right boundary of the auxiliary bounding box, b r Indicates the horizontal coordinate of the right boundary of the auxiliary anchor box, b l gt Indicates the horizontal coordinate of the left boundary of the auxiliary bounding box, b l Indicates the horizontal coordinate of the left boundary of the auxiliary anchor box, b b gt Indicates the ordinate of the upper boundary of the auxiliary bounding box, b b Indicates the ordinate of the upper boundary of the auxiliary anchor box, b t gt Indicates the ordinate of the lower boundary of the auxiliary bounding box, b t represents the ordinate of the lower boundary of the auxiliary anchor box, and ratio represents the scale factor. (x c gt ,y c gt ) is the horizontal and vertical coordinates of the center point of the real bounding box, (x c ,y c ) are the horizontal and vertical coordinates of the center point of the anchor box.
3. The method for detecting fruit in a complex environment according to claim 1, characterized in that: The training, testing and validation described in S2-S4 include: (1) using the training set as the input of the improved YOLOv8n model, outputting a fused feature map through the improved YOLOv8n model and obtaining weight parameters, and optimizing and adjusting the model through the loss function in this process; (2) using the weight parameters to predict the validation set using the optimized and adjusted improved YOLOv8n model, and obtaining performance indicators of the optimized and adjusted improved YOLOv8n model based on the prediction results; (3) adjusting the hyperparameters of the optimized and adjusted improved YOLOv8n model according to the performance indicators; (4) Repeat the above steps (1)-(3) until the optimized and adjusted improved YOLOv8n model obtains the optimal weight parameters; (5) Using the optimal weight parameters to test the model generalization ability on the test set to determine the effectiveness and feasibility of the model.
4. A detection system for implementing the fruit detection method under a complex environment according to any one of claims 1 to 3, comprising: An image storage and preprocessing module for performing step S1 and a model storage and detection module connected to the image storage and preprocessing module for performing steps S2-S4.
5. Application of the fruit detection method under complex environment according to any one of claims 1 to 3 or the detection system according to claim 4 in an automated citrus detection device.
Citation Information
Cited By
Fruit maturity and surface defect detection method and system
CN119205658A
Lightweight kiwi fruit detection method based on improved YOLOv8 model
CN119723563A
A lightweight kiwi detection method based on an improved YOLOv8 model
CN119723563B
Sparse point cloud and GCN fused citrus defect detection method and system
CN119850635A
Strawberry fruit detection system and method based on improved YOLOv8n network
CN120299034A
