Target detection method and system based on improved Yolov5
By introducing the DFIoU loss function into the Yolov5 model and adjusting the regression loss of the anchor box, the impact of low-quality image samples on the generalization performance of the model is solved, and the detection accuracy and the model's processing ability of low-quality image samples are improved.
Patent Information
- Application Number
- CN202311730711.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-12-15
- Publication Date
- 2024-08-23
AI Technical Summary
When the existing Yolov5 model processes low-quality image samples, the bounding box loss function penalizes low-quality image samples too heavily, resulting in a decline in model generalization performance.
The DFIoU loss function is introduced in the Yolov5 model, and the regression loss of the anchor box is adjusted through the penalty term RDFIoU, increasing the IOU loss of ordinary quality anchor boxes and reducing the IOU loss of high-quality anchor boxes, thereby making the bounding box loss function more focused on ordinary quality anchors. box.
It improves the accuracy of target detection and improves the model's ability to process low-quality image samples without affecting the inference time.
Smart Images

Figure CN118537526A
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer vision technology, and in particular to a target detection method and system based on improved Yolov5. Background Art
[0002] Object detection is a core problem in computer vision, and its detection performance depends on the design of loss functions. As an important part of the object detection loss function, a good definition of the bounding box loss function will bring significant performance improvements to the object detection model. Yolov5 is a widely used object detection model, and its bounding box loss functions are: 1) GIoU; 2) DIoU; 3) CIoU; 4) SIoU; 5) EIoU.
[0003] However, when performing object detection based on the existing Yolov5 model, it is assumed that the images in the training data are of high quality, and the model is committed to strengthening the fitting ability of the bounding box loss. In fact, however, the training data inevitably contains low-quality image samples, and geometric factors such as distance and aspect ratio will increase the penalty for low-quality image samples. At this time, if the bounding box regression of low-quality image samples is blindly strengthened, the generalization performance of the model will be reduced. Summary of the invention
[0004] The purpose of the present invention is to provide a target detection method and system based on improved Yolov5, which adds a DFIoU loss function to the existing Yolov5 model, improves the existing Yolov5 model, and can greatly improve the detection accuracy.
[0005] To achieve the above object, the present invention provides the following technical solutions:
[0006] On the one hand, a target detection method based on improved Yolov5 is provided, which comprises the following steps:
[0007] Add the loss function DFIoU to the network of the target detection model Yolov5 to obtain the improved Yolov5 model;
[0008] Obtain image data containing the target to be detected, and perform annotation and data enhancement to obtain a training set;
[0009] Using the training set to train the improved Yolov5 model to obtain the final target detection model;
[0010] And the image to be detected is detected according to the final target detection model to determine whether the image to be detected contains the target to be detected.
[0011] Preferably, the loss function DFIoU is defined as shown in formula (1):
[0012] L DFIoU =R DFIoU L IoU (1)
[0013] Among them, L DFIoU That is the loss function DFIoU; R DFIoU For L DFIoU The penalty term; L IoU is the regression loss of the anchor box.
[0014] Preferably, the penalty term R DFIoU Obtained by formula (2):
[0015]
[0016] Among them, W g , H g are the width and height of the minimum bounding box respectively; x and y are the coordinates of the center point of the predicted anchor box respectively; x gt ,y gt are the coordinates of the center points of the anchor boxes that have been marked; “*” indicates W g , H g The calculation of is separated from the graphics calculation.
[0017] Preferably, the loss function DFIoU is added to the network of the target detection model Yolov5, comprising the following steps:
[0018] Add the loss function DFIoU to the utils / metrics.py module of the target detection model Yolov5;
[0019] And add the loss function DFIoU to the utils / loss.py module of the target detection model Yolov5, and set DFIoU = True.
[0020] Preferably, obtaining image data containing the target to be detected, and performing annotation and data enhancement to obtain a training set comprises the following steps:
[0021] Obtain image data containing the target to be detected, and perform annotation and data enhancement;
[0022] The image data containing the target to be detected is divided into a training set and a validation set according to a preset ratio.
[0023] Preferably, image data containing the target to be detected is obtained by imaging equipment or web crawling; the data enhancement includes one or more of single-sample data enhancement, multi-sample data enhancement and data enhancement based on deep learning.
[0024] Preferably, the improved Yolov5 model is trained using the training set to obtain a final target detection model, comprising the following steps:
[0025] Set training hyperparameters for the improved Yolov5 model;
[0026] Using the training set to train the improved Yolov5 model;
[0027] And the improved Yolov5 model that has completed training is verified using the verification set to obtain the target detection accuracy index.
[0028] Preferably, the hyperparameters include one or more of epoch, batch_size, imgsz, depth_multiple, width_multiple and nc.
[0029] Preferably, the target detection accuracy index includes one or more of Precision Recall, mAP50, and Map50-95.
[0030] A target detection system based on improved Yolov5 is also provided, which is used to implement the above target detection method, and includes:
[0031] Function definition module, which is used to define the loss function DFIoU;
[0032] A function adding module, which is used to add the loss function DFIoU to the network of the target detection model Yolov5;
[0033] The model training module is used to train the improved Yolov5 model using the training set to obtain the final target detection model;
[0034] And a target detection module, which is used to detect the image to be detected according to the final target detection model to determine whether the image to be detected contains the target to be detected.
[0035] In summary, the present invention has the following beneficial effects compared with the prior art:
[0036] The present invention adds a custom DFIoU loss function to the existing Yolov5 model, thereby improving the existing Yolov5 model to increase the IOU loss of ordinary quality anchor boxes and reduce the IOU loss of high-quality anchor boxes, so that the bounding box loss function can focus on the anchor boxes of ordinary quality and increase their weight, thereby greatly improving the detection accuracy. BRIEF DESCRIPTION OF THE DRAWINGS
[0037] Figure 1It is a flowchart of the steps of the target detection method based on the improved Yolov5 in the present invention.
[0038] Figure 2 It is a structural schematic diagram of the target detection system based on improved Yolov5 in the present invention.
[0039] Figure 3 It is a schematic diagram of the structure of an electronic device for realizing the target detection method based on improved Yolov5 in the present invention. DETAILED DESCRIPTION
[0040] The technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention.
[0041] Example 1
[0042] like Figure 1 As shown, this embodiment provides a target detection method based on improved Yolov5, which includes the following steps:
[0043] S1. Add the loss function DFIoU to the network of the target detection model Yolov5 to obtain the improved Yolov5 model;
[0044] The definition of the loss function DFIoU is shown in formula (1):
[0045] L DFIoU =R DFIoU L IoU (1)
[0046] Among them, L DFIoU That is the loss function DFIoU; R DFIoU For L DFIoU The penalty term; L IoU is the regression loss of the anchor box;
[0047] Furthermore, the penalty term R DFIoU Obtained by formula (2):
[0048]
[0049] Among them, W g , H g are the width and height of the minimum bounding box respectively; x and y are the coordinates of the center point of the predicted anchor box respectively; x gt ,y gtare the coordinates of the center points of the anchor boxes that have been marked; “*” indicates W g , H g The calculation of is separated from the graphics calculation.
[0050] Therefore, when R DFIoU ∈[1, e), it can significantly amplify the regression loss L of the normal quality anchor box. IoU ; When L IoU ∈[0, 1], the penalty term R of the high-quality anchor box will be reduced DFIoU , and reduce its attention to the distance between the center points; wherein the ordinary quality anchor frame refers to an anchor frame that does not accurately select the target in a complex scene, and the high-quality anchor frame refers to an anchor frame that can accurately select the target in a complex scene.
[0051] Furthermore, the loss function DFIoU is added to the network of the target detection model Yolov5, including the following steps:
[0052] Add the loss function DFIoU to the utils / metrics.py module of the target detection model Yolov5;
[0053] And add the loss function DFIoU to the utils / loss.py module of the target detection model Yolov5, and set DFIoU = True to use the loss function DFIoU;
[0054] S2. Obtain image data containing the target to be detected, and perform annotation and data enhancement to obtain a training set.
[0055] S3. Use the training set to train the improved Yolov5 model to obtain the final target detection model.
[0056] S4. Detect the image to be detected according to the final target detection model to determine whether the image to be detected contains the target to be detected.
[0057] Furthermore, step S2 includes the following steps:
[0058] Acquire image data containing the target to be detected by imaging devices such as vehicle-mounted cameras or web crawling, and perform annotation and data enhancement, wherein the data enhancement includes one or more of single-sample data enhancement, multi-sample data enhancement, and data enhancement based on deep learning;
[0059] Use a custom data file to divide the image data containing the target to be detected into a training set and a validation set according to a preset ratio. For example, in this embodiment, the number of images in the training set is 3536, and the number of images in the validation set is 921;
[0060] The step S3 comprises the following steps:
[0061] The improved Yolov5 model is trained with hyperparameters set; in this embodiment, the hyperparameters include one or more of epoch, batch_size, imgsz, depth_multiple, width_multiple and nc, and the values of the hyperparameters are shown in Table 1:
[0062] Table 1 Training hyperparameter settings
[0063] Hyperparameters value epoch 300 batch_size 20 imgsz 640 depth_multiple 0.33 width_multiple 0.50 nc 10
[0064] Using the training set to train the improved Yolov5 model;
[0065] And using the verification set to verify the improved Yolov5 model that has completed training to obtain the target detection accuracy index, wherein the target detection accuracy index includes one or more of PrecisionRecall, mAP50, and Map50-95.
[0066] like Figure 2 As shown, compared with Yolov5 in the prior art, the improved Yolov5 model in this embodiment has a lower bounding box loss function;
[0067] At the same time, when the verification set is used for verification, the model weight with the best result saved in training is used for verification, and the detection accuracy of the prior art (ie, "yolov5") and the improved Yolov5 model (ie, "yolov5-DFIoU") in this embodiment is compared, and the results are shown in Table 2.
[0068] Table 2 Comparison of detection accuracy
[0069]
[0070] As can be seen from Table 2, in this embodiment, by adding the DFIoU loss function to the existing Yolov5 model, various accuracy indicators (including PrecisionRecall, mAP50, and Map50-95) can be improved to a certain extent when the network structure is the same, and the reasoning time is not lost. At the same time, the bounding box loss is calculated by IOU=Intersection / Union, where Intersection represents the area of the intersection of the detection box and the true box, and Union represents the area of the union of the detection box and the true box, which is used to evaluate the accuracy of the target detection algorithm.
[0071] It can be seen that in this embodiment, by adding the DFIoU loss function to the existing Yolov5 model, the IOU loss of the anchor box of ordinary quality can be increased, and the IOU loss of the anchor box of high quality can be reduced, so that the bounding box loss function can focus on the anchor box of ordinary quality and increase its weight, thereby greatly improving the detection accuracy.
[0072] Embodiment 2:
[0073] This embodiment provides a target detection system based on improved Yolov5, which can implement the target detection method described in Example 1, such as Figure 2 As shown, the target detection system includes:
[0074] Function definition module 1, which is used to define the loss function DFIoU;
[0075] Function adding module 2, which is used to add the loss function DFIoU to the network of the target detection model Yolov5, and the specific steps are the same as those in Example 1;
[0076] Model training module 3, which is used to train the improved Yolov5 model using the training set to obtain the final target detection model;
[0077] And a target detection module 4 is used to detect the image to be detected according to the final target detection model to determine whether the image to be detected contains the target to be detected.
[0078] Embodiment 3:
[0079] like Figure 3 As shown, this embodiment provides an electronic device for implementing the target detection method based on the improved Yolov5 described in Example 1, and the electronic device includes a processor 10, a memory 11, a communication bus 12, a communication interface 13, and a computer program stored in the memory 11 and executable on the processor 10, such as a program that can implement the target detection method based on the improved Yolov5 described in Example 1.
[0080] Wherein, the processor 10 may be composed of an integrated circuit in some embodiments, for example, a single packaged integrated circuit, or a plurality of integrated circuits with the same or different functions, including one or more central processing units (CPUs), microprocessors, digital processing chips, graphics processors, and combinations of various control chips, etc. The processor 10 is the control core (ControlUnit) of the electronic device, and uses various interfaces and lines to connect various components of the entire electronic device, and executes or executes programs or modules stored in the memory 11 (for example, executing the program of the target detection method based on the improved Yolov5 described in Example 1, etc.), and calls the data stored in the memory 11 to execute various functions of the electronic device and process data.
[0081] The memory 11 includes at least one type of readable storage medium, and the readable storage medium includes a flash memory, a mobile hard disk, a multimedia card, a card-type memory (for example, SD or DX memory, etc.), a magnetic memory, a disk, an optical disk, etc. The memory 11 may be an internal storage unit of an electronic device in some embodiments, such as a mobile hard disk of the electronic device. The memory 11 may also be an external storage device of an electronic device in other embodiments, such as a plug-in mobile hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (Secure Digital, SD) card, a flash card (Flash Card), etc. equipped on the electronic device. Further, the memory 11 may also include both an internal storage unit of the electronic device and an external storage device. The memory 11 may not only be used to store application software and various types of data installed in the electronic device, such as the program code of the target detection method based on the improved Yolov5 described in Example 1, but may also be used to temporarily store data that has been output or is to be output.
[0082] The communication bus 12 may be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The bus may be divided into an address bus, a data bus, a control bus, etc. The bus is configured to realize connection and communication between the memory 11 and at least one processor 10, etc.
[0083] The communication interface 13 is used for communication between the above-mentioned electronic device and other devices, including a network interface and a user interface. Optionally, the network interface may include a wired interface and / or a wireless interface (such as a WI-FI interface, a Bluetooth interface, etc.), which is generally used to establish a communication connection between the electronic device and other electronic devices. The user interface may be a display (Display), an input unit (such as a keyboard (Keyboard)), and optionally, the user interface may also be a standard wired interface, a wireless interface. Optionally, in some embodiments, the display may be an LED display, a liquid crystal display, a touch-sensitive liquid crystal display, and an OLED (Organic Light-Emitting Diode, organic light-emitting diode) touch device, etc. Among them, the display may also be appropriately referred to as a display screen or a display unit, which is used to display information processed in the electronic device and to display a visual user interface.
[0084] Figure 3 Only an electronic device with components is shown, and those skilled in the art will understand that Figure 3 The structure shown does not constitute a limitation on the electronic device, and may include fewer or more components than shown in the figure, or combine certain components, or arrange the components differently.
[0085] Embodiment 4:
[0086] This embodiment provides a computer-readable storage medium, which stores a computer program. When the computer program is executed by a processor of an electronic device, it can implement the target detection method based on the improved Yolov5 described in Example 1.
[0087] In summary, this application improves the existing Yolov5 model by defining the DFIoU loss function and adding the DFIoU loss function to the existing Yolov5 model, so as to increase the IOU loss of ordinary quality anchor boxes and reduce the IOU loss of high-quality anchor boxes, so that the bounding box loss function can focus on the anchor boxes of ordinary quality and increase their weight, thereby greatly improving the detection accuracy.
[0088] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principle of the present invention should be included in the protection scope of the present invention.
Claims
1. A target detection method based on improved Yolov5, characterized in that: The steps include: Add the loss function DFIoU to the network of the target detection model Yolov5 to obtain the improved Yolov5 model; Obtain image data containing the target to be detected, and perform annotation and data enhancement to obtain a training set; Using the training set to train the improved Yolov5 model to obtain the final target detection model; And the image to be detected is detected according to the final target detection model to determine whether the image to be detected contains the target to be detected.
2. The target detection method according to claim 1, characterized in that: The definition of the loss function DFIoU is shown in formula (1): L DFIoU =R DFIoU L IoU (1) Among them, L DFIoU That is the loss function DFIoU; R DFIoU For L DFIoU The penalty term; L IoU is the regression loss of the anchor box.
3. The target detection method according to claim 2, characterized in that: Penalty term R DFIoU Obtained by formula (2): Among them, W g , H g are the width and height of the minimum bounding box respectively; x and y are the coordinates of the center point of the predicted anchor box respectively; x gt ,y gt are the coordinates of the center points of the anchor boxes that have been marked; "*" indicates W g , H g The calculation of is separated from the graphics calculation.
4. The target detection method according to claim 1, characterized in that: Adding the loss function DFIoU to the network of the target detection model Yolov5 includes the following steps: Add the loss function DFIoU to the utils / metrics.py module of the target detection model Yolov5; And add the loss function DFIoU to the utils / loss.py module of the target detection model Yolov5, and set DFIoU = True.
5. The target detection method according to claim 1, characterized in that: Obtain image data containing the target to be detected, and perform annotation and data enhancement to obtain a training set, including the following steps: Obtain image data containing the target to be detected, and perform annotation and data enhancement; The image data containing the target to be detected is divided into a training set and a validation set according to a preset ratio.
6. The target detection method according to claim 5, characterized in that: Image data containing the target to be detected is obtained by imaging equipment or web crawling; the data enhancement includes one or more of single sample data enhancement, multi-sample data enhancement and data enhancement based on deep learning.
7. The target detection method according to claim 1, characterized in that: The improved Yolov5 model is trained using the training set to obtain the final target detection model, including the following steps: Set training hyperparameters for the improved Yolov5 model; Using the training set to train the improved Yolov5 model; And use the verification set to verify the improved Yolov5 model that has completed training.
8. The target detection method according to claim 7, characterized in that: The hyper parameters include one or more of epoch, batch_size, imgsz, depth_multiple, width_multiple and nc.
9. The target detection method according to claim 7, characterized in that: The target detection accuracy index includes one or more of PrecisionRecall, mAP50, and Map50-95.
10. A target detection system based on improved Yolov5, which is used to implement the target detection method according to any one of claims 1 to 9, characterized in that: The target detection system comprises: Function definition module, which is used to define the loss function DFIoU; A function adding module, which is used to add the loss function DFIoU to the network of the target detection model Yolov5; The model training module is used to train the improved Yolov5 model using the training set to obtain the final target detection model; And a target detection module, which is used to detect the image to be detected according to the final target detection model to determine whether the image to be detected contains the target to be detected.
