An automatic fruit recognition method based on the attention YOLOv5 model
By introducing the attention mechanism and CIOU border regression loss function into the fruit recognition model, the problem of the model ignoring local information and frame overlap is solved, and the accuracy and efficiency of fruit recognition are improved. It is suitable for automatic fruit weighing and pricing systems.
Patent Information
- Application Number
- CN202210138123.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-02-15
- Publication Date
- 2024-09-17
- Estimated Expiration
- 2042-02-15
AI Technical Summary
Existing fruit recognition models tend to focus on the global information of the object, easily ignoring key local information of the fruit, and do not consider the relationship between the target frame and the prediction frame when they overlap, resulting in inaccurate prediction results.
The attention-based YOLOv5 model is used to enhance the feature extraction of the backbone network through the SENet attention module. The CIOU border regression loss function is used to consider the aspect ratio and center point relationship between the target box and the prediction box, combined with Mosaic data enhancement and adaptive grayscale filling. processing to improve the accuracy of fruit recognition.
It improves the accuracy of fruit recognition, makes the prediction frame closer to the real frame, improves the detection speed and efficiency, and is suitable for automatic fruit weighing and pricing systems in supermarkets and wet markets.
Smart Images

Figure CN114494710B
Abstract
Description
Technical Field
[0001] The invention belongs to the field of fruit recognition, and in particular relates to an automatic fruit recognition method based on an attention YOLOv5 model. Background Art
[0002] In daily life, supermarkets and vegetable markets need to manually weigh and calculate prices, which is not only time-consuming and time-consuming, but also reduces the efficiency of customers' purchases. Therefore, it is very necessary for supermarkets and vegetable markets to combine fruit weighing, automatically calculate the price of fruits, and improve the efficiency of customers' purchases.
[0003] The current mainstream methods have the following main problems: 1. The model tends to focus on the global information of the object and easily ignores some key and important local information of the fruit; 2. When the target box and the prediction box overlap, the relationship between them is not considered, which easily leads to inaccurate prediction results. Summary of the invention
[0004] The purpose of the present invention is to provide an automatic fruit recognition method based on the attention YOLOv5 model to solve the technical problems that the existing model tends to focus on the global information of the object, easily ignores some key and important local information of the fruit, and does not consider the relationship between the existing target frame and the prediction frame when they overlap, which easily leads to inaccurate prediction results.
[0005] In order to solve the above technical problems, the specific technical solutions of the present invention are as follows:
[0006] An automatic fruit recognition method based on the attention YOLOv5 model includes the following steps:
[0007] Step 1: preprocess the original image of the data set;
[0008] Step 2: Input the backbone network to extract features, and use the SENet attention module to obtain a one-dimensional vector corresponding to the channel as the evaluation score;
[0009] Step 3: Apply the evaluation score to the corresponding channel of the feature map through multiplication operation to obtain effective features for fruit recognition;
[0010] Step 4: Fusion of features through the Feature Pyramid Networks and Path Aggregation Network structures;
[0011] Step 5: Predict the image features and use CIOU to consider the relationship between the aspect ratio and the center point of the target box and the prediction box to improve the prediction accuracy, and output the recognition results according to large, medium and small targets.
[0012] Furthermore, in step 1, image stitching is performed using a Mosaic data enhancement method.
[0013] Furthermore, the size of the data set is normalized in step 1, and the original image is adaptively filled with the least gray value, which specifically includes the following steps:
[0014] Step 1.1, set the image scaling ratio; let the original image be A×B, scale it to a×a, where A is the width of the original image, B is the height of the original image, a is the width of the scaled image, the width and height of the scaled image are equal, divide the width and height of the scaled image by the corresponding width and height of the original image, and get two coefficients, and take the smaller coefficient;
[0015] Step 1.2, set the scaled size; multiply the width and height of the original image by the smallest coefficient, then the width is C and the height is D;
[0016] Step 1.3, set the gray edge fill value; first subtract D from C, and use the remainder method to get the pixel value to be filled (CD)% E = F, where E = 2 n , n is the number of downsampling times the network has passed, and each end pixels; gray filling is used during testing, and the original resize operation is used during training to improve object detection and calculation speed.
[0017] Furthermore, the backbone network is CSPDarknet-53, which can enhance the learning ability of the convolutional network and reduce memory consumption.
[0018] Furthermore, the operation process of the SENet attention module includes the following steps:
[0019] Step 2.1, use global average pooling as the Squeeze operation;
[0020] Step 2.2: Use two fully connected layers to obtain the correlation between channels while reducing parameters and computation.
[0021] Step 2.3, normalize the weights by Sigmoid;
[0022] Step 2.4: Apply the normalized weights to the features of the original channel through the Scale operation.
[0023] Furthermore, in step 5, CIOU takes into account the distance, overlap rate, border scale and penalty factor between the real border and the predicted border, making the target border regression more stable.
[0024] CIOU formula:
[0025]
[0026] Among them, ρ 2 (b,b gt ) is the Euclidean distance d between the center points of the predicted box and the true box, and c represents the diagonal distance of the minimum enclosing rectangular box that contains both the true box and the predicted box.
[0027] Formula (2) is the formula for α in the penalty term αv:
[0028]
[0029] Formula (3) is the formula for v in the penalty term αv:
[0030]
[0031] Among them, w gt Indicates the width of the real box, h gt represents the height of the real box, w represents the width of the predicted box, and h represents the height of the predicted box.
[0032] Formula (4) is the calculation formula of CIOU loss during regression:
[0033]
[0034] When the target box and the prediction box overlap, the CIOU values are different; when the c values are the same, the positional relationship between the target box and the prediction box is measured by the ratio d of the Euclidean distance between the center point and the diagonal line, so that the loss function can converge.
[0035] The automatic fruit recognition method based on the attention YOLOv5 model of the present invention has the following advantages:
[0036] 1. The present invention adds an attention mechanism after the backbone network to increase the important features of the fruit, weaken the unimportant features, and improve the accuracy of fruit recognition.
[0037] 2. The present invention uses the CIOU border regression loss function to take into account the relationship between the aspect ratio and the center point of the target box and the test box, so that the fruit prediction box is closer to the real box. BRIEF DESCRIPTION OF THE DRAWINGS
[0038] Figure 1 It is a flow chart of the automatic fruit recognition method based on the attention YOLOv5 model of the present invention;
[0039] Figure 2 It is a schematic diagram of the structure of CSP1_X of the present invention;
[0040] Figure 3 It is a schematic diagram of the structure of CSP2_X of the present invention;
[0041] Figure 4 It is a schematic diagram of the SENet structure of the present invention;
[0042] Figure 5 A schematic diagram of the network structure of the present invention
[0043] Figure 6 It is a schematic diagram of CIOU of the present invention. DETAILED DESCRIPTION
[0044] In order to better understand the purpose, structure and function of the present invention, the following is a further detailed description of an automatic fruit recognition method based on the attention YOLOv5 model of the present invention in conjunction with the accompanying drawings.
[0045] The process of the present invention is as follows Figure 1 As shown, the specific steps include:
[0046] Step 1: Preprocess the data set;
[0047] The image stitching is performed using the Mosaic data enhancement method, which refers to the idea of CutMix data enhancement. CutMix data enhancement stitches two images together, while Mosaic stitches four images together, which increases the amount of data and enriches the background of the detected object.
[0048] In the YOLO series of algorithms, the initial length and width of the anchor frame are usually set for different data sets. In YOLOv3 and YOLOv4, the initial anchor frame is obtained by a separate algorithm, and the commonly used one is the k-means algorithm. The present invention embeds this function into the code, and realizes that the best initial anchor frame in different training sets can be adaptively calculated for each training. The initial anchor frames of the present invention are [10, 13, 16, 30, 33, 23], [30, 61, 62, 45, 59, 119], [116, 90, 156, 198, 373, 326], and the best initial anchor frames of the present invention are calculated to be [111, 114, 141, 121, 127, 141], [150, 149, 159, 169, 195, 212], [256, 173, 173, 292, 326, 298].
[0049] The sizes of data sets are often different, and their sizes need to be normalized. However, many pictures in actual projects have inconsistent aspect ratios. After scaling and padding, there are more padding parts at both ends, and there is a lot of redundant information, which affects the speed and effect of the model. The method of the present invention adaptively fills the original image with the least grayscale value, so that the grayscale values at both ends of the image height or width are the least, the amount of calculation will also be reduced, and the speed will also be improved. The specific steps are as follows:
[0050] (1) Image scaling ratio. Assume that the original image is 1000x800 and is scaled to 416x416. Divide 416x416 by the width and height of the original image to obtain coefficients of 0.416 and 0.52, respectively. Take the smaller value of 0.416.
[0051] (2) Scaled size: Multiply the width and height of the original image by a smaller coefficient of 0.416, so the width is 416 and the height is 332.
[0052] (3) Gray edge filling value. First, 416-332=84, and then take the remainder to get the pixel value to be filled 84%32=20 (32 is because the network has been downsampled 5 times, and 2 to the fifth power is 32), 10 pixels at each end. Gray filling is used during testing, and the original resize operation is still used during training to improve object detection and calculation speed.
[0053] Step 2: Input the backbone network to extract features, and use the SENet attention module to obtain a one-dimensional vector corresponding to the channel as the evaluation score;
[0054] In order to extract rich features from fruit images, inspired by YOLOv5, this paper uses CSPDarknet-53 as the backbone network. CSPDarknet-53 can enhance the learning ability of convolutional networks and reduce memory consumption.
[0055] The CSPDarknet-53 backbone network includes Focus, Mosaic, convolutional layer, and residual components, where CSP1_X is used to adjust the number of residual components, such as Figure 2 shown.
[0056] CSP2_X in Neck (bottleneck layer) is used to adjust the number of convolutional layers, such as Figure 3 As shown in Figure 2, the features extracted by CSPDarknet-53 are subsequently used to obtain channel attention.
[0057] In order to obtain the weights of different feature channels, strengthen important channels and weaken minor channels, the present invention uses the SENet attention mechanism to learn channel weights. SENet can learn the correlation between channels and generate channel attention. Although the amount of calculation increases, the extracted features are more effective. Figure 4 This is a schematic diagram of the SENet model. First, global average pooling is used as the Squeeze operation; second, two fully connected layers are used to obtain the correlation between channels while reducing parameters and calculations; then, the weights are normalized through Sigmoid; finally, the normalized weights are applied to the features of the original channels through the Scale operation.
[0058] The present invention is to put SENet into the SPP (spatial pyramid pooling) module, such as Figure 5 As shown in the figure, SPP, as an Inception structure, embeds multi-scale information of fruits and aggregates features in different receptive fields. Therefore, using SELayer can re-weight the convolution feature channels, enhance the interdependence between important features, and learn the importance of features in different channels, thereby producing better results and improving recognition performance.
[0059] For fruits with little difference in global features (size, shape, color, etc.) and some local features that are different, the attention mechanism SENet can enhance the important features of the fruit and weaken the unimportant features, so that the extracted fruit features are more representative and retain local important information. In this embodiment, the first 16 feature maps are selected. The size, shape, color and other global features of green apples and guava are similar, while the color, surface texture and root pedicle of some areas are different. Before the SENet operation, the feature information of the two is similar. After the SENet operation, on the one hand, the unimportant information around is weakened, and on the other hand, the important features such as the local texture and shape of the two fruits are highlighted, which is conducive to accurately identifying green apples and guava.
[0060] Step 3: Apply the evaluation score to the corresponding channel of the feature map through multiplication operation to obtain effective features for fruit recognition;
[0061] Step 4: The features are fused through the Feature Pyramid Networks (FPN) and Path Aggregation Network (PAN) structures;
[0062] Step 5: Predict the image features and output the recognition results according to large, medium and small targets respectively.
[0063] The present invention uses CIOU_Loss to replace GIOU_Loss. GIOU_Loss solves the problem of non-overlapping frames, while CIOU_Loss not only considers the problem of overlapping frames, but also takes into account information such as the frame aspect ratio and the position relationship of the center, so that the regression speed and accuracy of the prediction frame are higher.
[0064] CIOU takes into account the distance, overlap rate, border scale and penalty factor between the real box and the predicted box, making the target border regression more stable and effectively solving the problem of IOU divergence during training, such as Figure 6 shown.
[0065] Formula (1) is the CIOU formula:
[0066]
[0067] Among them, ρ 2 (b,b gt ) is the Euclidean distance d between the center points of the predicted box and the true box, and c represents the diagonal distance of the minimum enclosing rectangular box that contains both the true box and the predicted box.
[0068] Formula (2) is the formula for α in the penalty term αv:
[0069]
[0070] Formula (3) is the formula for v in the penalty term αv:
[0071]
[0072] Among them, w gt Indicates the width of the real box, h gt represents the height of the real box, w represents the width of the predicted box, and h represents the height of the predicted box.
[0073] Formula (4) is the calculation formula of CIOU loss during regression:
[0074]
[0075] When the target box and the prediction box overlap, the CIOU value is also different. When the c value is the same, the ratio d of the Euclidean distance between the center point of the target box and the prediction box and the diagonal line can effectively measure the positional relationship between the two, so that the loss function can converge effectively.
[0076] The fruit data set of the present invention is partly from the public data set on the Internet, and partly from the data taken by the mobile phone. The data used are all pictures of fruits taken from overhead during simulated weighing, and different business scenarios are considered. The importance of each feature channel is automatically learned by the attention mechanism, and then the features useful for the fruit recognition task are strengthened and the useless features are suppressed according to the importance; secondly, when the prediction frame of fruit recognition overlaps with the target frame, CIOU considers the relationship between the aspect ratio and the center point of the target frame and the prediction frame, so that the fruit prediction frame is closer to the real frame and the prediction accuracy is improved. There are 15 fruit categories, a total of 13676 pictures, and the ratio of training set, verification set and test set is 8:1:1 (10940 pictures of training set, 1368 pictures of verification set and test set), and the specific categories and quantities are shown in Table 1.
[0077] Table 1 Dataset table
[0078]
[0079]
[0080] In order to solve the problem that manual weighing in supermarkets and vegetable markets is time-consuming and the pricing process is too long, the present invention proposes an automatic fruit recognition algorithm based on the attention YOLOv5 model. Supermarkets and vegetable markets can combine with fruit weighing to automatically calculate the price of fruits and improve the efficiency of customer purchases.
[0081] It is to be understood that the present invention is described by some embodiments, and it is known to those skilled in the art that various changes or equivalent substitutions may be made to these features and embodiments without departing from the spirit and scope of the present invention. In addition, under the teachings of the present invention, these features and embodiments may be modified to adapt to specific circumstances and materials without departing from the spirit and scope of the present invention. Therefore, the present invention is not limited by the specific embodiments disclosed herein, and all embodiments falling within the scope of the claims of this application are within the scope of protection of the present invention.
Claims
1. An automatic fruit recognition method based on the attention YOLOv5 model, characterized in that: The following steps are involved: Step 1: preprocess the original image of the data set; Step 2: Input the backbone network to extract features, and use the SENet attention module to obtain a one-dimensional vector corresponding to the channel as the evaluation score; Step 3: Apply the evaluation score to the corresponding channel of the feature map through multiplication operation to obtain effective features for fruit recognition; Step 4: The features are fused through the feature pyramid and path aggregation network structure; Step 5: Predict the image features and use CIOU to consider the relationship between the aspect ratio and the center point of the target box and the prediction box to improve the prediction accuracy, and output the recognition results according to large, medium and small targets.
2. The automatic fruit recognition method based on the attention YOLOv5 model according to claim 1, characterized in that, In step 1, image stitching is performed using Mosaic data enhancement.
3. The automatic fruit recognition method based on the attention YOLOv5 model according to claim 2, characterized in that, In step 1, the size of the data set is normalized, and the original image is adaptively filled with the minimum grayscale value, which specifically includes the following steps: Step 1.1, set the image scaling ratio; let the original image be A×B, scale it to a×a, where A is the width of the original image, B is the height of the original image, a is the width of the scaled image, the width and height of the scaled image are equal, divide the width and height of the scaled image by the corresponding width and height of the original image, and get two coefficients, and take the smaller coefficient; Step 1.2, set the scaled size; multiply the width and height of the original image by the smallest coefficient, then the width is C and the height is D; Step 1.3, set the gray edge fill value; first subtract D from C, and use the remainder method to get the pixel value to be filled (CD)% E = F, where E = 2 n , n is the number of downsampling times the network has passed, and pixels; gray filling is used during testing, and the original resize operation is used during training to improve object detection and calculation speed.
4. The automatic fruit recognition method based on the attention YOLOv5 model according to claim 1, characterized in that, The backbone network is CSPDarknet-53, which can enhance the learning ability of the convolutional network and reduce memory consumption.
5. The automatic fruit recognition method based on the attention YOLOv5 model according to claim 1, characterized in that, The operation process of the SENet attention module described in step 2 includes the following steps: Step 2.1, use global average pooling as the Squeeze operation; Step 2.2: Use two fully connected layers to obtain the correlation between channels while reducing parameters and computation. Step 2.3, normalize the weights by Sigmoid; Step 2.4: Apply the normalized weights to the features of the original channel through the Scale operation.
6. The automatic fruit recognition method based on the attention YOLOv5 model according to claim 1, characterized in that, In step 5, CIOU takes into account the distance, overlap rate, border scale and penalty factor between the real frame and the predicted frame, making the target border regression more stable. CIOU formula: Among them, ρ 2 (b,b gt ) is the Euclidean distance d between the center points of the predicted box and the true box, and c represents the diagonal distance of the minimum enclosing rectangular box that contains both the true box and the predicted box; The formula for α in the penalty term αv is as follows: The formula for v in the penalty term αv is as follows: Among them, w gt Indicates the width of the real box, h gt represents the height of the real box, w represents the width of the predicted box, and h represents the height of the predicted box; The calculation formula of CIOU loss during regression is as follows: When the target box and the prediction box overlap, the CIOU values are different; when the c values are the same, the positional relationship between the target box and the prediction box is measured by the ratio d of the Euclidean distance between the center point and the diagonal line to make the loss function converge.
Citation Information
Patent Citations
Fish feeding behavior identification method based on YOLOv5
CN113537106A
Insulator defect detection method based on improved yolov5
CN114022432A
