Object prediction model training method and system based on YOLOV5
By obtaining the anchor point and reference frame in the YOLOV5 model, calculating the prediction center point, width and height, and optimizing the loss value, the YOLOV5 model solves the problems of low width and height accuracy and center point drift when predicting objects, and improves the prediction effect.
Patent Information
- Application Number
- CN202111511725.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-06
- Publication Date
- 2024-10-01
- Estimated Expiration
- 2041-12-06
AI Technical Summary
When the ordinary YOLOV5 model predicts objects, the width and height accuracy is not high, and the center point drift leads to inaccurate predictions.
By obtaining the object center point and real frame width and height in the training set, input the YOLOV5 model to obtain the anchor point and reference frame of the output feature map, calculate the predicted center point, width and height, calculate the loss value and optimize the model, and iteratively train to improve prediction accuracy .
It effectively reduces the impact of center point drift and inaccurate reference frames, and improves the accuracy and effect of object prediction.
Smart Images

Figure CN114266883B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of image object prediction, and in particular to a YOLOV5-based object prediction model training method and system. Background Art
[0002] Object detection algorithm is one of the important areas of computer vision and the basis of many computer vision tasks. It is widely used in autonomous driving, intelligent security, military and other fields.
[0003] Object detection based on deep learning is divided into two categories: One-stage and Two-stage. The Two-stage algorithm imitates the idea of human observation before thinking. In the first stage, the RPN layer network extracts the object's region of interest (observation). In the second stage, the network identifies the object category and location in the region of interest (thinking). The One-stage category algorithm directly identifies the location and category, lacking the extraction of the region of interest.
[0004] The Yolov5 algorithm is a one-stage algorithm. Its network structure is divided into three parts: Backbone (extracting low-level semantic information of objects), Neck (enhancing receptive field and global context), and Head (classifying and locating objects). The Backbone of Yolov5 is similar to that of YOLOV4, and mainly uses CSP-DARKNET and CBL structures. Neck uses FPN+PAN structure.
[0005] When the ordinary YOLOV5 model predicts an object, the direct prediction of the width and height of the object is not accurate, and the center point will drift, which often leads to inaccurate prediction of the object.
[0006] The above contents are only used to assist in understanding the technical solution of the present invention and do not constitute an admission that the above contents are prior art. Summary of the invention
[0007] In order to solve the above technical problems, the present invention provides an object prediction model training method based on YOLOV5, comprising:
[0008] S1: Obtain a training set, select a training image from the training set, and obtain the center point p0 and the real frame width and height [W0, H0] of the object in the training image; the training image uses a preset length d as a length unit;
[0009] S2: Input the training image into the object detection model based on YOLOV5, and obtain the training labels corresponding to the three output feature maps, the three output feature maps include: a first output feature map, a second output feature map and a third output feature map, and the information of the output feature map includes: whether there is category information, category information, center point information and width and height information;
[0010] S3: Obtaining the anchor point and reference frame of the object in each output feature map through each of the training labels;
[0011] S4: Obtaining a predicted center point of the object in each of the output feature maps by calculating a mathematical relationship between each of the anchor points and the corresponding output feature map about the center point information of the object;
[0012] S5: Obtain predicted width and height of the object in each output feature map by calculating a mathematical relationship between each reference frame and the width and height information of the object in the corresponding output feature map;
[0013] S6: Obtain a first loss value of the model by calculating the loss between the predicted center point and predicted width and height of the object and the center point and width and height of the object, and add the first loss value to the loss value of the object category to obtain an overall loss value of the model;
[0014] S7, inputting the overall loss value into an optimizer for optimization;
[0015] S8: Perform iterative training on steps S1-S7 for a total of n times to obtain a trained object prediction model based on YOLOV5.
[0016] Preferably, step S2 specifically includes:
[0017] S21: The object detection model based on YOLOV5 includes: a first output layer, a second output layer and a third output layer; the width and height of the first output layer are J1×K1, the width and height of the second output layer are J2×K2, and the width and height of the third output layer are J3×K3; each of the output layers uses a preset length d as a length unit;
[0018] S22: Input the training image into the YOLOV5-based object detection model to obtain the first output feature map, the second output feature map, and the third output feature map, scale the center point of the object and the width and height of the real frame according to the width and height of the first output feature map, the second output feature map, and the third output feature map to obtain training labels corresponding to each output feature map.
[0019] Preferably, in step S3, the anchor point is a point in the output feature map corresponding to the center point of the object after the training image is scaled;
[0020] Each of the output feature maps includes three rectangular reference frames of different widths and heights.
[0021] Preferably, step S4 is specifically:
[0022] S41: Obtain the center point coordinates of object A in the training image as R A (X A ,Y A );
[0023] S42: After the center point of the object A is scaled, the anchor point in the first output feature map, the second output feature map, and the third output feature map is: R A1 (X A1 ,Y A1 ), R A2 (X A2 ,Y A2 ) and R A3 (X A3 ,Y A3 );
[0024] S43: In each of the output feature maps, a square with a preset length d as the side length is a grid, and the intersections between the grids are grid points. In the first output feature map, the grid points around R A1 (X A1 ,Y A1 ) are the center point information of the object A, including: a first reference grid point, a second reference grid point, a third reference grid point and a fourth reference grid point;
[0025] S44: Calculate R A1 (X A1 ,Y A1 ) to each reference grid point, the distance to the first reference grid point is L1, the distance to the second reference grid point is L2, the distance to the third reference grid point is L3, and the distance to the fourth reference grid point is L4; the reference grid point with the smallest distance is used as the predicted center point p1 of the first output feature map;
[0026] S45: The predicted center point p2 of the second output feature map and the predicted center point p3 of the third output feature map are calculated through steps S43-S44.
[0027] Preferably, step S5 is specifically:
[0028] S51: Obtain three reference frames of the first output feature map, including: reference frame Q 1-1 , Q 1-2 and Q 1-3 ; The three reference frames of the second output feature map include: reference frame Q2-1 , Q 2-2 and Q 2-3 ; The three reference frames of the third output feature map include: reference frame Q 3-1 , Q 3-2 and Q 3-3 ;
[0029] S52: Obtain the real width and height of the frame of the object A in the training image, wherein the real width and height of the frame include: a width value and a height value; after scaling the real width and height of the frame, obtain the width and height information of the first output feature map, the second output feature map, and the third output feature map, including: first width and height information, second width and height information, and third width and height information;
[0030] S53: Compare the first width and height information with the reference frame Q of the first output feature map 1-1 , Q 1-2 and Q 1-3 , taking the width and height of the reference frame with the smallest sum of the absolute value of the width difference and the absolute value of the height difference as the predicted width and height [W1, H1] of object A in the first output feature map;
[0031] S54: Calculate the predicted width and height [W2, H2] of object A in the second output feature map and the predicted width and height [W3, H3] of object A in the third output feature map through step S53.
[0032] Preferably, in step S6, the calculation formula of the overall loss value of the model is as follows:
[0033]
[0034] Among them, p i is the predicted center point of the object in each output feature map, W i is the predicted width of the object in each output feature map, H i is the predicted height of the object in each output feature map, i is the number of the output feature map, and ΔV is the loss value of the object category.
[0035] An object prediction model training system based on YOLOV5, comprising:
[0036] An extraction module is used to obtain a training set, select a training image from the training set, and obtain the center point p0 and the real frame width and height [W0, H0] of the object in the training image;
[0037] A training label acquisition module is used to input the training image into an object detection model based on YOLOV5 to obtain training labels corresponding to three output feature maps, where the three output feature maps include: a first output feature map, a second output feature map, and a third output feature map, and the information of the output feature map includes: whether there is category information, category information, center point information, and width and height information;
[0038] An anchor point and reference frame acquisition module, used to acquire the anchor point and reference frame of the object in each output feature map through each training label;
[0039] A predicted center point acquisition module, used to obtain the predicted center point of the object in each of the output feature maps by calculating the mathematical relationship between each of the anchor points and the corresponding output feature map about the center point information of the object;
[0040] A predicted width and height acquisition module is used to obtain the predicted width and height of the object in each output feature map by calculating the mathematical relationship between each reference frame and the width and height information of the object in the corresponding output feature map;
[0041] An overall loss value calculation module, used to obtain a first loss value of the model by calculating the loss between the predicted center point and predicted width and height of the object and the center point and width and height of the object, and add the first loss value to the loss value of the object category to obtain an overall loss value of the model;
[0042] An optimization module, used for inputting the overall loss value into an optimizer for optimization;
[0043] The iteration module is used to iterate the training for n times to obtain a trained object prediction model based on YOLOV5.
[0044] The present invention has the following beneficial effects:
[0045] By selecting anchor points and reference frames, the impact of center point drift and inaccurate reference frames can be effectively reduced, thereby improving the prediction effect of objects. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] Figure 1 This is a flow chart of a method according to an embodiment of the present invention;
[0047] Figure 2 This is a system structure diagram of an embodiment of the present invention;
[0048] The realization of the purpose, functional features and advantages of the present invention will be further explained in conjunction with embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION
[0049] It should be understood that the specific embodiments described herein are only used to explain the present invention, and are not used to limit the present invention.
[0050] Reference Figure 1 The present invention provides an object prediction model training method based on YOLOV5, comprising:
[0051] S1: Obtain a training set, select a training image from the training set, and obtain the center point p0 and the real frame width and height [W0, H0] of the object in the training image; the training image uses a preset length d as a length unit, and the preset length d can be specifically set according to project requirements;
[0052] S2: Input the training image into the object detection model based on YOLOV5, and obtain the training labels corresponding to the three output feature maps, the three output feature maps include: a first output feature map, a second output feature map and a third output feature map, and the information of the output feature map includes: whether there is category information, category information, center point information and width and height information;
[0053] S3: Obtaining the anchor point and reference frame of the object in each output feature map through each of the training labels;
[0054] S4: Obtaining a predicted center point of the object in each of the output feature maps by calculating a mathematical relationship between each of the anchor points and the corresponding output feature map about the center point information of the object;
[0055] S5: Obtain predicted width and height of the object in each output feature map by calculating a mathematical relationship between each reference frame and the width and height information of the object in the corresponding output feature map;
[0056] S6: Obtain a first loss value of the model by calculating the loss between the predicted center point and predicted width and height of the object and the center point and width and height of the object, and add the first loss value to the loss value of the object category to obtain an overall loss value of the model;
[0057] S7, inputting the overall loss value into an optimizer for optimization;
[0058] S8: Perform iterative training on steps S1-S7 for a total of n times to obtain a trained object prediction model based on YOLOV5.
[0059] In this embodiment, step S2 is specifically as follows:
[0060] S21: The object detection model based on YOLOV5 includes: a first output layer, a second output layer and a third output layer; the width and height of the first output layer are J1×K1, the width and height of the second output layer are J2×K2, and the width and height of the third output layer are J3×K3; each of the output layers uses a preset length d as a length unit;
[0061] In a specific implementation, it is preferred that the width and height of the first output layer is 20×20, the width and height of the second output layer is 40×40, and the width and height of the third output layer is 80×80;
[0062] S22: Input the training image into the YOLOV5-based object detection model to obtain the first output feature map, the second output feature map, and the third output feature map, scale the center point of the object and the width and height of the real frame according to the width and height of the first output feature map, the second output feature map, and the third output feature map to obtain training labels corresponding to each output feature map.
[0063] In this embodiment, in step S3, the anchor point is the point in the output feature map corresponding to the center point of the object after the training image is scaled;
[0064] Each of the output feature maps includes three rectangular reference frames of different widths and heights;
[0065] In this embodiment, step S4 is specifically as follows:
[0066] S41: Obtain the center point coordinates of object A in the training image as R A (X A ,Y A );
[0067] S42: After the center point of the object A is scaled, the anchor point in the first output feature map, the second output feature map, and the third output feature map is: R A1 (X A1 ,Y A1 ), R A2 (X A2 ,Y A2 ) and R A3 (X A3 ,Y A3 );
[0068] S43: In each of the output feature maps, a square with a preset length d as the side length is a grid, and the intersections between the grids are grid points. In the first output feature map, the grid points around R A1 (X A1 ,Y A1 ) are the center point information of the object A, including: a first reference grid point, a second reference grid point, a third reference grid point and a fourth reference grid point;
[0069] S44: Calculate R A1 (X A1 ,Y A1) to each reference grid point, the distance to the first reference grid point is L1, the distance to the second reference grid point is L2, the distance to the third reference grid point is L3, and the distance to the fourth reference grid point is L4; the reference grid point with the smallest distance is used as the predicted center point p1 of the first output feature map;
[0070] S45: Calculate the predicted center point p2 of the second output feature map and the predicted center point p3 of the third output feature map through steps S43-S44.
[0071] In this embodiment, step S5 is specifically as follows:
[0072] S51: Obtain three reference frames of the first output feature map, including: reference frame Q 1-1 , Q 1-2 and Q 1-3 ; The three reference frames of the second output feature map include: reference frame Q 2-1 , Q 2-2 and Q 2-3 ; The three reference frames of the third output feature map include: reference frame Q 3-1 , Q 3-2 and Q 3-3 ;
[0073] In the specific implementation, each reference frame uses a preset length d as the length unit, and the reference frame Q 1-1 , Q 1-2 and Q 1-3 The width and height are 10:13, 16:30, 33:23 respectively; the reference frame Q 2-1 , Q 2-2 and Q 2-3 The width and height are 30:61, 62:45, 59:119 respectively; reference frame Q 3-1 , Q 3-2 and Q 3-3 The width and height are 116:90, 156:198, and 373:326 respectively.
[0074] S52: Obtain the real width and height of the frame of the object A in the training image, wherein the real width and height of the frame include: a width value and a height value; after scaling the real width and height of the frame, obtain the width and height information of the first output feature map, the second output feature map, and the third output feature map, including: first width and height information, second width and height information, and third width and height information;
[0075] S53: Compare the first width and height information with the reference frame Q of the first output feature map 1-1 , Q 1-2 and Q 1-3, taking the width and height of the reference frame with the smallest sum of the absolute value of the width difference and the absolute value of the height difference as the predicted width and height [W1, H1] of object A in the first output feature map;
[0076] S54: Calculate the predicted width and height [W2, H2] of object A in the second output feature map and the predicted width and height [W3, H3] of object A in the third output feature map through step S53.
[0077] In this embodiment, in step S6, the calculation formula of the overall loss value of the model is as follows:
[0078]
[0079] Among them, p i is the predicted center point of the object in each output feature map, W i is the predicted width of the object in each output feature map, H i is the predicted height of the object in each output feature map, i is the number of the output feature map, and ΔV is the loss value of the object category;
[0080] The loss value of the object category is the cross quotient, which can be directly obtained through the GHM loss function. Using the GHM loss function, we can pay more attention to samples that are easy to separate and further improve the network detection performance.
[0081] The effects after implementation of the present invention are as follows:
[0082] The trained YOLOV5-based object prediction model provided by the present invention is used to perform object prediction, and the open source VOC2007 dataset is used as a test set. The VOC2007 dataset contains 20 categories, namely aeroplane, bicycle, bird, boat, etc. There are many types of object categories, and the sizes of objects vary greatly. The more types there are, the greater the impact on the non-decoupled YOLOV5.
[0083] The prediction effect of the original YOLOV5 model in VOC map@0.5 is 0.761, and map@0.5:0.9 is 0.498. The results of object prediction using the trained YOLOV5-based object prediction model provided by the present invention are shown in Table 1. The final prediction effect of map@0.5 reaches 0.77, and map@0.5:0.9 reaches 0.52. The overall prediction effect is much better than the original YOLOV5 model.
[0084] Table 1
[0085]
[0086]
[0087] refer to Figure 2 The present invention provides an object prediction model training system based on YOLOV5, comprising:
[0088] An extraction module 10 is used to obtain a training set, select a training image from the training set, and obtain a center point p0 and a real frame width and height [W0, H0] of an object in the training image;
[0089] A training label acquisition module 20 is used to input the training image into an object detection model based on YOLOV5 to obtain training labels corresponding to three output feature maps, where the three output feature maps include: a first output feature map, a second output feature map, and a third output feature map, and the information of the output feature map includes: whether there is category information, category information, center point information, and width and height information;
[0090] An anchor point and reference frame acquisition module 30, used to acquire the anchor point and reference frame of the object in each output feature map through each training label;
[0091] A predicted center point acquisition module 40, configured to obtain a predicted center point of the object in each of the output feature maps by calculating a mathematical relationship between each of the anchor points and the corresponding output feature map about the center point information of the object;
[0092] A predicted width and height acquisition module 50 is used to obtain the predicted width and height of the object in each output feature map by calculating the mathematical relationship between each reference frame and the width and height information of the object in the corresponding output feature map;
[0093] An overall loss value calculation module 60 is used to obtain a first loss value of the model by calculating the loss between the predicted center point and predicted width and height of the object and the center point and width and height of the object, and add the first loss value to the loss value of the object category to obtain an overall loss value of the model;
[0094] An optimization module 70, used for inputting the overall loss value into an optimizer for optimization;
[0095] The iteration module 80 is used for iterative training for n times to obtain a trained object prediction model based on YOLOV5.
[0096] It should be noted that, in this article, the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or system including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or system. In the absence of further restrictions, an element defined by the sentence "comprises a ..." does not exclude the existence of other identical elements in the process, method, article or system including the element.
[0097] The serial numbers of the embodiments of the present invention are for descriptive purposes only and do not represent the superiority or inferiority of the embodiments. In a unit claim that lists several means, several of these means may be embodied by the same hardware item. The use of the words first, second, and third, etc. does not indicate any order and these words may be interpreted as identifiers.
[0098] The above are only preferred embodiments of the present invention, and are not intended to limit the patent scope of the present invention. Any equivalent structure or equivalent process transformation made using the contents of the present invention specification and drawings, or directly or indirectly applied in other related technical fields, are also included in the patent protection scope of the present invention.
Claims
1. A method for training an object prediction model based on YOLOV5, characterized in that: include: S1: Obtain a training set, select a training image from the training set, and obtain the center point p0 and the real frame width and height [W0, H0] of the object in the training image; the training image uses a preset length d as a length unit; S2: Input the training image into the object detection model based on YOLOV5, and obtain the training labels corresponding to the three output feature maps, the three output feature maps include: a first output feature map, a second output feature map and a third output feature map, and the information of the output feature map includes: whether there is category information, category information, center point information and width and height information; S3: Obtaining the anchor point and reference frame of the object in each output feature map through each of the training labels; S4: Obtaining a predicted center point of the object in each of the output feature maps by calculating a mathematical relationship between each of the anchor points and the corresponding output feature map about the center point information of the object; S5: Obtain predicted width and height of the object in each output feature map by calculating a mathematical relationship between each reference frame and the width and height information of the object in the corresponding output feature map; S6: Obtain a first loss value of the model by calculating the loss between the predicted center point and predicted width and height of the object and the center point and width and height of the object, and add the first loss value to the loss value of the object category to obtain an overall loss value of the model; S7, inputting the overall loss value into an optimizer for optimization; S8: Perform iterative training on steps S1-S7 for a total of n times to obtain a trained object prediction model based on YOLOV5.
2. The object prediction model training method based on YOLOV5 according to claim 1, characterized in that: Step S2 is specifically as follows: S21: The object detection model based on YOLOV5 includes: a first output layer, a second output layer and a third output layer; the width and height of the first output layer are J1×K1, the width and height of the second output layer are J2×K2, and the width and height of the third output layer are J3×K3; each of the output layers uses a preset length d as a length unit; S22: Input the training image into the YOLOV5-based object detection model to obtain the first output feature map, the second output feature map, and the third output feature map, scale the center point of the object and the width and height of the real frame according to the width and height of the first output feature map, the second output feature map, and the third output feature map to obtain training labels corresponding to each output feature map.
3. The object prediction model training method based on YOLOV5 according to claim 1, characterized in that: In step S3, the anchor point is the point in the output feature map corresponding to the center point of the object after the training image is scaled; Each of the output feature maps includes three rectangular reference frames of different widths and heights.
4. The object prediction model training method based on YOLOV5 according to claim 1, characterized in that: Step S4 is specifically as follows: S41: Obtain the center point coordinates of object A in the training image as R A (X A ,Y A ); S42: After the center point of the object A is scaled, the anchor point in the first output feature map, the second output feature map, and the third output feature map is: R A1 (X A1 ,Y A1 ), R A2 (X A2 ,Y A2 ) and R A3 (X A3 ,Y A3 ); S43: In each of the output feature maps, a square with a preset length d as the side length is a grid, and the intersections between the grids are grid points. In the first output feature map, the grid points around R A1 (X A1 ,Y A1 ) are the center point information of the object A, including: a first reference grid point, a second reference grid point, a third reference grid point and a fourth reference grid point; S44: Calculate R A1 (X A1 ,Y A1 ) to each reference grid point, the distance to the first reference grid point is L1, the distance to the second reference grid point is L2, the distance to the third reference grid point is L3, and the distance to the fourth reference grid point is L4; the reference grid point with the smallest distance is used as the predicted center point p1 of the first output feature map; S45: Calculate the predicted center point p2 of the second output feature map and the predicted center point p3 of the third output feature map through steps S43-S44.
5. The object prediction model training method based on YOLOV5 according to claim 1, characterized in that: Step S5 is specifically as follows: S51: Obtain three reference frames of the first output feature map, including: reference frame Q 1-1 , Q 1-2 and Q 1-3 ; The three reference frames of the second output feature map include: reference frame Q 2-1 , Q 2-2 and Q 2-3 ; The three reference frames of the third output feature map include: reference frame Q 3-1 , Q 3-2 and Q 3-3 ; S52: Obtain the real width and height of the frame of the object A in the training image, wherein the real width and height of the frame include: a width value and a height value; after scaling the real width and height of the frame, obtain the width and height information of the first output feature map, the second output feature map, and the third output feature map, including: first width and height information, second width and height information, and third width and height information; S53: Compare the first width and height information with the reference frame Q of the first output feature map 1-1 , Q 1-2 and Q 1-3 , taking the width and height of the reference frame with the smallest sum of the absolute value of the width difference and the absolute value of the height difference as the predicted width and height [W1, H1] of the object A in the first output feature map; S54: Calculate the predicted width and height [W2, H2] of object A in the second output feature map and the predicted width and height [W3, H3] of object A in the third output feature map through step S53.
6. The object prediction model training method based on YOLOV5 according to claim 1, characterized in that: In step S6, the calculation formula of the overall loss value of the model is as follows: Among them, p i is the predicted center point of the object in each output feature map, W i is the predicted width of the object in each output feature map, H i is the predicted height of the object in each output feature map, i is the number of the output feature map, and ΔV is the loss value of the object category.
7. An object prediction model training system based on YOLOV5, characterized in that: include: An extraction module is used to obtain a training set, select a training image from the training set, and obtain the center point p0 and the real frame width and height [W0, H0] of the object in the training image; A training label acquisition module is used to input the training image into an object detection model based on YOLOV5 to obtain training labels corresponding to three output feature maps, where the three output feature maps include: a first output feature map, a second output feature map, and a third output feature map, and the information of the output feature map includes: whether there is category information, category information, center point information, and width and height information; An anchor point and reference frame acquisition module, used to acquire the anchor point and reference frame of the object in each output feature map through each training label; A predicted center point acquisition module, used to obtain the predicted center point of the object in each of the output feature maps by calculating the mathematical relationship between each of the anchor points and the corresponding output feature map about the center point information of the object; A predicted width and height acquisition module is used to obtain the predicted width and height of the object in each output feature map by calculating the mathematical relationship between each reference frame and the width and height information of the object in the corresponding output feature map; An overall loss value calculation module, used to obtain a first loss value of the model by calculating the loss between the predicted center point and predicted width and height of the object and the center point and width and height of the object, and add the first loss value to the loss value of the object category to obtain an overall loss value of the model; An optimization module, used for inputting the overall loss value into an optimizer for optimization; The iteration module is used to iterate the training for n times to obtain a trained object prediction model based on YOLOV5.
