Pedestrian detection method based on improved YOLOv5 model
By improving the YOLOv5 model, introducing CBAM, VIT transformer and SPD-Conv modules, and using the EIoU loss function, the occlusion and multi-pose problems in pedestrian detection are solved, and the detection accuracy and model performance are improved.
Patent Information
- Application Number
- CN202410645038.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-23
- Publication Date
- 2024-09-06
AI Technical Summary
Existing pedestrian detection methods are difficult to accurately identify small target pedestrians under the conditions of occlusion, interference and multiple postures of pedestrians, and are affected by illumination changes and low resolution, resulting in low detection accuracy.
Improve the YOLOv5 model, enhance the feature extraction capabilities by introducing the CBAM module, VIT transformer and SPD-Conv module, and use the EIoU loss function to improve the bounding box regression and improve the aspect ratio calculation accuracy.
It significantly improves the accuracy of pedestrian detection under occlusion, interference and multi-pose situations, enhances the detection accuracy of small target pedestrians, and improves the convergence speed and regression accuracy of the model.
Smart Images

Figure CN118609159A
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of pedestrian detection and recognition, and in particular to a pedestrian detection method based on an improved YOLOv5 model. Background Art
[0002] Pedestrian detection technology has been widely used in the fields of intelligent transportation, autonomous driving, and security monitoring. It is also a prerequisite for visual tasks such as human posture estimation, pedestrian re-identification, and personnel search. In actual scene applications, pedestrian detection systems often need to have high real-time performance and strong robustness. This is the difficulty and pain point of pedestrian detection technology, and it has also made it a research hotspot and focus in the field of target detection.
[0003] With the continuous development of deep learning, deep learning is more suitable for larger data sets, and target detection algorithms based on deep learning have gradually become the focus of research. However, the current pedestrian detection method can only handle local occlusion problems, and large-area and serious occlusion problems still need further research; illumination changes in the real background and interference from objects similar to pedestrian outlines will cause confusion between pedestrian targets and backgrounds, making them difficult to distinguish; for pedestrians imaged at a distance, the targets are small, the resolution is low, and there is a lack of sufficient feature information, which is easily affected by noise, so it is difficult for the detection algorithm to accurately locate small target pedestrians. In addition, the uncertainty caused by the multi-posture of pedestrians will also affect the accuracy of the pedestrian detection algorithm. Summary of the invention
[0004] The purpose of the present invention is to overcome the defects of the above-mentioned prior art and to provide a pedestrian detection method based on an improved YOLOv5 model, which can effectively improve the accuracy of pedestrian detection and recognition under occlusion, interference, and multi-posture conditions.
[0005] The purpose of the present invention can be achieved by the following technical solution: A pedestrian detection method based on an improved YOLOv5 model, comprising the following steps:
[0006] S1. Improve the backbone feature extraction network and loss function in the YOLOv5 model to enhance the ability to extract image features and accurately calculate the aspect ratio;
[0007] S2. Use the image dataset to train and test the improved YOLOv5 model to obtain a pedestrian detection model;
[0008] S3. Input the image to be detected into the pedestrian detection model, and output the pedestrian detection result.
[0009] Furthermore, the specific process of improving the backbone feature extraction network in step S1 is as follows:
[0010] Introducing CBAM (Convolutional Block Attention Module) to combine channel attention and spatial attention;
[0011] Introducing VIT transformer (Vision Transformer) and using self-attention mechanism for information fusion;
[0012] The SPD-Conv (Small Proposal Detection Convolution) module is introduced to solve the problem of performance degradation of Convolutional Neural Networks (CNN) in detection tasks with low image resolution and small objects.
[0013] Furthermore, the CBAM includes a channel attention module and a spatial attention module, and the output end of the channel attention module is connected to the input end of the spatial attention module.
[0014] Furthermore, the channel attention module performs global maximum pooling and global average pooling on the input image according to the width and height to obtain two feature maps, which are applied to the fully connected layer, and the output features are added element by element, and then activated through the activation function to generate channel attention weights, and element-by-element multiplication operations are performed between the channel attention weights and the input image to obtain the output of the channel attention module.
[0015] Furthermore, the input of the spatial attention module is the output of the channel attention module. The spatial attention module is based on the global maximum pooling and global average pooling of the channel, and then combines the two results. After the convolution layer, the dimension is reduced to a single channel, and then the spatial attention weight is generated through the activation function. Finally, the spatial attention weight is multiplied by the input feature to obtain the final generated feature.
[0016] Furthermore, the VIT transformer uses the Encoder part of the original Transformer and uses the Multi-Head Attention (MSA) mechanism in the Encoder Block.
[0017] Furthermore, the SPD-Conv module includes an SPD (space to depth) layer and a non-strided convolution layer to downsample the feature map without losing learnable information.
[0018] Furthermore, the improvement of the loss function in step S1 is specifically to use EIoU (Efficient Intersection over Union) as the bounding box regression loss function, which is used to split the influencing factors of the aspect ratio, and calculate the width and height values of the predicted box and the real box respectively with the width and height values of the minimum bounding box.
[0019] Furthermore, the EIoU loss function includes three parts: overlap loss, center point distance loss and width and height loss.
[0020] Furthermore, the EIoU loss function is specifically:
[0021]
[0022] Among them, ρ 2 (b,b gt ) is the Euclidean distance between the center point of the predicted box and the real box, c is the diagonal distance between the predicted box and the minimum bounding box of the real box, α is the weight function, v is the consistency of the aspect ratio, w and w gt are the widths of the predicted box and the true box, h and h respectively. gt are the heights of the predicted box and the true box, respectively, w and c h They are the width and height of the minimum bounding box of the predicted box and the true box, respectively.
[0023] Compared with the prior art, the present invention has the following advantages:
[0024] The present invention improves the backbone feature extraction network in the YOLOv5 model to enhance the ability to extract image features, and introduces an SPD-Conv module and a Transformer encoder block module into the feature extraction network to strengthen feature extraction, thereby solving the problem of performance degradation of a convolutional neural network (CNN) in detection tasks with low image resolution and small objects; a new detection head is added to the model to improve the network's detection accuracy for small-target pedestrians; at the same time, a Transformer encoder block is introduced into the head to form a TPH prediction head, and an attention mechanism CBAM module is introduced into the feature fusion stage to improve the utilization rate of feature information in the feature fusion stage. This can effectively improve the accuracy of pedestrian detection and recognition under occlusion, interference, and multi-posture conditions.
[0025] The present invention improves the loss function in the YOLOv5 model to solve the problem of fuzzy definition of aspect ratio. By introducing EIoU as the bounding box regression loss function, the influencing factors of the aspect ratio are split, and the width and height values of the predicted box and the real box are calculated respectively with the width and height values of the minimum circumscribed box, which can improve the convergence speed and regression accuracy of the model, thereby improving the pedestrian detection accuracy. BRIEF DESCRIPTION OF THE DRAWINGS
[0026] Figure 1 It is a schematic diagram of the method flow of the present invention;
[0027] Figure 2 It is the classic YOLOv5 network structure;
[0028] Figure 3 It is a schematic diagram of the process of improving YOLOv5 in the embodiment;
[0029] Figure 4 It is the improved YOLOv5 network structure;
[0030] Figure 5 It is the schematic diagram of CBAM module;
[0031] Figure 6 Schematic diagram of the channel attention module;
[0032] Figure 7 Schematic diagram of the spatial attention module;
[0033] Figure 8 This is a schematic diagram of the Transformer Encoder Block module;
[0034] Fig. 9 It is a schematic diagram of the multi-layer perceptron module;
[0035] Fig.10 This is the SPD-Conv structure diagram;
[0036] Fig.11 This is the traditional YOLOv5 detection effect;
[0037] Fig.12 To test the effect of this program. DETAILED DESCRIPTION
[0038] The present invention is described in detail below with reference to the accompanying drawings and specific embodiments.
[0039] Example
[0040] like Figure 1 As shown, a pedestrian detection method based on an improved YOLOv5 model includes the following steps:
[0041] S1. Improve the backbone feature extraction network and loss function in the YOLOv5 model to enhance the ability to extract image features and accurately calculate the aspect ratio;
[0042] S2. Use the image dataset to train and test the improved YOLOv5 model to obtain a pedestrian detection model;
[0043] S3. Input the image to be detected into the pedestrian detection model, and output the pedestrian detection result.
[0044] This solution improves the YOLOv5 model, where the YOLOv5 model results are as follows: Figure 2 As shown in the figure, compared with the YOLO series algorithms, the YOLOv5 algorithm uses adaptive anchor boxes at the input of the network, eliminating the traditional anchor box clustering step based on the K-Means algorithm; in addition, the spatial pyramid pooling module is used in the backbone network to integrate the extracted features, and the pooling kernels of different sizes in the SPP are used to perform pooling operations on the feature map, which can improve the receptive field while maintaining the speed, so as to better extract features; YOLOv5 adds the Focus module and the Bottlenneck CSP module to the Backbone, and realizes the detection requirements of different scenes according to the number of model convolutions and the stacking of Bottlenneck CSP modules. At the neck of the network, PANet is used to add a bottom-up feature fusion path, and the feature map output at each level is directly connected to the path information at the same level, which strengthens the joint use of high-level and low-level information. Under the action of adaptive feature pooling, the damaged information path between regions and features can also be quickly restored, and the candidate regions are aggregated at each feature layer to avoid random allocation, thereby obtaining better feature expression capabilities and richer semantic information.
[0045] Experiments were conducted on five model specifications of YOLOv5, namely n, s, m, l, and x versions from small to large. The network structures are the same, but the difference lies in the depth and width of the network models. The experimental results are shown in Table 1.
[0046] Table 1
[0047]
[0048]
[0049] In Table 1, Params indicates the number of model parameters, Depth is used to control the depth of the model, Width is used to control the width of the model, GFLOPs indicates the number of floating-point operations per second of the model, and mAP indicates the average detection accuracy of the target category when the threshold is 0.5. FPS indicates the number of images that the model can process per second, which is used to measure the detection speed of the model. Through experimental data analysis, it can be found that with the increase of network depth and width, the mAP of the model is improved, but the number of model parameters and GFLOPs also gradually increase, causing the FPS to gradually decrease. It can be seen that the increase in network scale will increase the complexity of the network, resulting in the deterioration of the real-time performance of the model. From the detection results, YOLOv5n is the network model with the fastest detection speed, the smallest depth and width, but the worst mAP. YOLOv5x has the highest mAP, but the slowest detection speed. Compared with other network models, YOLOv5s has a better mAP and a faster detection speed, which can better balance accuracy and speed.
[0050] Therefore, this solution selects the lightweight YOLOv5s model as the basic network model for improvement. The main improvements are: by introducing the SPD-CONV module and Transformer encoder block in the backbone feature extraction network to strengthen feature extraction; on the basis of the three-scale detection layer, a new detection head is added to improve the network's detection performance for small target pedestrians; the Transformer encoder block is introduced in the head part to form the Transformer Prediction Head (TPH), and the Convolutional Block Attention Module (CBAM) is introduced in the feature fusion stage to improve the utilization of feature information in the feature fusion stage. The improvement process is as follows: Figure 3 As shown in Figure 2, the improved network structure is as follows: Figure 4 shown.
[0051] Specifically, in order to make the model pay more attention to the target area in the image, the CBAM convolutional attention module is introduced. CBAM is a lightweight convolutional attention module that combines channel attention and spatial attention. The structure is as follows Figure 5 shown.
[0052] Among them, the structure of the channel attention module is as follows Figure 6 As shown, in the channel attention module, the input feature map F∈R c*H*W Perform global maximum pooling and global average pooling according to width and height respectively, and get two F 1 ∈R C*1*1The feature map is then applied to the fully connected layer, and the output features are added element by element, and then activated by the activation function to generate the channel attention weight. The channel attention weight is multiplied element by element with the input feature map to obtain the output of the channel attention module, which is the input required by the spatial attention module.
[0053] The channel attention module compresses the input feature map in the spatial dimension to obtain a one-dimensional vector. When compressing in the spatial dimension, not only average pooling but also maximum pooling should be considered. Average pooling and maximum pooling can be used to aggregate the spatial information of the feature map sent to the shared network, compress the spatial dimension of the input feature map, and sum and merge them element by element to generate the channel attention map.
[0054] The structure of the Spatial Attention Module is as follows Figure 7 As shown in the figure, in the spatial attention module, the feature map of the channel attention module is used as input, and the channel-based global maximum pooling and global average pooling are first performed, and then the two results are combined. After the convolution layer, the dimension is reduced to a single channel, and then the spatial attention weight is generated through the activation function, and finally the spatial attention weight is multiplied by the input feature to obtain the final generated feature.
[0055] The spatial attention mechanism is channel compression, which performs average pooling and maximum pooling in the channel dimension. Maximum pooling is the operation of extracting the maximum value of the channel, while average pooling is the operation of extracting the average value of the channel. Then the previously extracted feature maps (the number of channels is 1) are merged to obtain a dual-channel feature map.
[0056] Secondly, the VIT transformer is introduced, and the self-attention mechanism is used for information fusion. Unlike the traditional convolutional neural network and recurrent neural network architecture models, the Vision Transformer uses the self-attention mechanism for information fusion. Its model has the advantages of small number of parameters and low complexity. The Vision Transformer uses the Encoder part of the original Transformer, and uses the Multi-Head Attention (MSA) mechanism in the Encoder Block. The Transformer Encoder Block module is as follows: Figure 8 As shown in Figure 2, the multi-layer perceptron (MLP) submodule in the Transformer Encoder Block module is as follows: Fig. 9As shown in the figure, the function of the entire MLP module is to perform linear transformation, fuse information by first increasing the dimension and then reducing the dimension, use GeLU as the activation function between the two linear changes, and add the Drop operation to increase the robustness of the model.
[0057] Furthermore, considering that in detection tasks with low image resolution and small objects, the performance of convolutional neural networks in target classification and target detection will drop rapidly. This is because a flawed but common design in the CNN architecture, namely the use of convolution strides and pooling layers, leads to the loss of fine-grained information and less efficient feature representation learning. To this end, this scheme introduces the SPD-Conv module to solve the problem of degraded performance of convolutional neural networks (CNNs) in detection tasks with low image resolution and small objects. SPD-Conv consists of a space-to-depth (SPD) layer and a non-strided convolution layer. SPD-Conv downsamples the feature map without losing learnable information, completely abandoning the currently widely used cross-row convolution and pooling operations. The specific structure is as follows: Fig.10 As shown. Space-to-depth is a space-to-depth convolution layer, which generalizes the image conversion technology to down-sampling the feature maps within and throughout the CNN while retaining all the information in the channel dimension, so there is no information loss.
[0058] Finally, EIoU is introduced as the bounding box regression loss function, the influencing factors of aspect ratio are split, and the width and height values of the predicted box and the real box are calculated with the width and height values of the minimum bounding box. When detecting objects in an image, the algorithm will generate multiple predicted boxes because there are multiple objects in the field of view. In YOLOv5, CIoU is used as the bounding box regression loss function, and the calculation formula is shown in formula (1).
[0059]
[0060] In the formula, ρ 2 (b,b gt ) represents the Euclidean distance between the center point of the predicted box and the real box, c represents the diagonal distance between the predicted box and the minimum bounding box of the real box, α represents the weight function, v represents the consistency of the aspect ratio, w and w gt Represent the width of the predicted box and the real box, h and h respectively gt Represent the height of the predicted box and the true box respectively.
[0061] However, the aspect ratio in CIoU only considers the width-to-height ratio of the real box and the predicted box, rather than the width-to-height value, which is somewhat ambiguous. Therefore, this solution introduces EIoU as the bounding box regression loss function. The core idea is to split the influencing factors of the aspect ratio based on CIoU, and calculate the width and height values of the predicted box and the real box respectively with the width and height values of the minimum bounding box.
[0062] The EIoU loss function consists of three parts: overlap loss, center point distance loss, and width and height loss. The EIoU calculation formula is shown in formula (2).
[0063]
[0064] In the formula, ρ 2 (b,b gt ) represents the Euclidean distance between the center point of the predicted box and the real box, c represents the diagonal distance between the predicted box and the minimum bounding box of the real box, α represents the weight function, v represents the consistency of the aspect ratio, w and w gt Represent the width of the predicted box and the real box, h and h respectively gt Represents the height of the predicted box and the real box respectively. w and c h Represent the width and height of the minimum bounding box of the predicted box and the true box respectively.
[0065] Using EIoU as the bounding box regression loss function solves the ambiguous definition of aspect ratio in CIoU and can improve the convergence speed and regression accuracy of the model during training.
[0066] To verify the effectiveness of this solution, this embodiment uses the improved YOLOv5 model for training and testing, and the experiment is carried out under Windows 11. The GPU is NVIDIA GeForce RTX3080, the memory is 16G; the programming language used is Python, and the deep learning framework is Pytorch.
[0067] The dataset used in this embodiment is City Persons, a dataset commonly used in pedestrian detection research. The CityPersons dataset is a subset of the Cityscapes dataset used for semantic segmentation tasks, which only annotates pedestrians. The dataset was collected in 18 cities in 3 countries, covering three different seasons. Each image contains an average of 7 pedestrians, and each pedestrian annotation includes a visible area annotation box and a full body annotation box. This embodiment selects 2169 images from the City Persons dataset as the training set of the model, performs certain screening on the pedestrian annotations of the images, uses 331 images as the verification set, and uses 200 images as the test set of the model.
[0068] In this embodiment, the input image resolution is 640×640, Mosaic is used for data enhancement, 50 epochs are trained, the initial learning rate is 0.01, the optimizer is SGD, and mAP@0.5, Precision, Recall, etc. are used as evaluation indicators of model performance. Precision is used to evaluate the accuracy of prediction, and the calculation formula is shown in formula (3). Recall is used to evaluate whether the prediction is complete, and the calculation formula is shown in formula (4).
[0069]
[0070]
[0071] Where T P Indicates the number of positive samples predicted as positive samples, F P Indicates the number of negative samples predicted as positive samples, F N Represents the number of positive samples predicted as negative samples. The area where the PR curve intersects the coordinate axis is the average precision, A P The calculation formula is shown in formula (5): The calculation formula of is shown in formula (6).
[0072]
[0073]
[0074] In order to verify the improvement and optimization effect of this scheme on each module of YOLOv5, a set of ablation experiments was set up on the City persons dataset. The ablation experiment includes the comparison of 4 improved modules. On the basis of the original YOLOv5, the first module is to replace the Conv module of the backbone network; the second module is to add a new detection head to become four detection heads; the third module is to add the CBAM module; the fourth module is to add the VIT transformer module. The experimental results are shown in Table 2, where SPD-CONV means replacing the Conv module of the original YOLOv5, Head means adding a detection head, CBAM means adding the CBAM attention module to the neck, VIT means adding the VIT transformer module, where √ means adding this module, and × means not adding this module.
[0075] Table 2
[0076]
[0077]
[0078] It can be seen from Table 2 that each module introduced in this scheme improves the detection effect of the model. Among them, the effect of adding a detection head to the model is the most obvious. Adding a detection head can improve the model's detection accuracy for small target pedestrians and improve the detection accuracy of the algorithm.
[0079] Fig.11 This is a schematic diagram of the traditional YOLOv5 detection effect. Fig.12 This is an illustration of the improved YOLOv5 detection effect. By comparison, it can be seen that the detection effect of this scheme on small target pedestrians is improved.
[0080] In summary, in order to improve the detection accuracy of multi-occluded and multi-scale pedestrians, this scheme designs an improvement on the YOLOv5 model. First, the SPD-CONV module and the Transformer encoder block module are introduced into the feature extraction network to strengthen feature extraction; then a new detection head is added to improve the network's detection accuracy for small-target pedestrians. Similarly, the Transformer encoder block is introduced into the head to form a TPH prediction head, and the attention mechanism CBAM module is introduced in the feature fusion stage to improve the utilization rate of feature information in the feature fusion stage. Experiments have shown that compared with the YOLOv5 detection model, the newly proposed detection model has better detection effect, can improve the network's detection performance for small-target pedestrians, and improve the utilization rate of feature information in the feature fusion stage, which is conducive to accurate pedestrian detection and recognition under occlusion, interference, and multi-posture conditions.
Claims
1. A pedestrian detection method based on an improved YOLOv5 model, characterized in that: The following steps are involved: S1. Improve the backbone feature extraction network and loss function in the YOLOv5 model to enhance the ability to extract image features and accurately calculate the aspect ratio; S2. Use the image dataset to train and test the improved YOLOv5 model to obtain a pedestrian detection model; S3. Input the image to be detected into the pedestrian detection model, and output the pedestrian detection result.
2. The pedestrian detection method based on the improved YOLOv5 model according to claim 1, characterized in that: The specific process of improving the backbone feature extraction network in step S1 is: Introducing CBAM to combine channel attention and spatial attention; Introducing VIT transformer, using self-attention mechanism for information fusion; The SPD-Conv module is introduced to solve the problem of degraded performance of convolutional neural networks in detection tasks with low image resolution and small objects.
3. A pedestrian detection method based on an improved YOLOv5 model according to claim 2, characterized in that: The CBAM includes a channel attention module and a spatial attention module, and the output end of the channel attention module is connected to the input end of the spatial attention module.
4. The pedestrian detection method based on the improved YOLOv5 model according to claim 3, characterized in that: The channel attention module performs global maximum pooling and global average pooling on the input image according to the width and height to obtain two feature maps, which are applied to the fully connected layer, and the output features are added element by element. Then, the features are activated through the activation function to generate channel attention weights, and element-by-element multiplication operations are performed between the channel attention weights and the input image to obtain the output of the channel attention module.
5. The pedestrian detection method based on the improved YOLOv5 model according to claim 4, characterized in that: The input of the spatial attention module is the output of the channel attention module. The spatial attention module is based on the global maximum pooling and global average pooling of the channel, and then combines the two results. After the convolution layer, the dimension is reduced to a single channel, and then the spatial attention weight is generated through the activation function. Finally, the spatial attention weight is multiplied by the input feature to obtain the final generated feature.
6. The pedestrian detection method based on the improved YOLOv5 model according to claim 2, characterized in that: The VIT transformer uses the Encoder part of the original Transformer and uses a multi-head attention mechanism in the Encoder Block.
7. The pedestrian detection method based on the improved YOLOv5 model according to claim 2, characterized in that: The SPD-Conv module consists of an SPD layer and a non-strided convolutional layer to downsample the feature map without losing learnable information.
8. A pedestrian detection method based on an improved YOLOv5 model according to any one of claims 1 to 7, characterized in that: The improvement of the loss function in step S1 is specifically to use EIoU as the bounding box regression loss function, which is used to split the influencing factors of the aspect ratio, and calculate the width and height values of the predicted box and the real box respectively with the width and height values of the minimum circumscribed box.
9. The pedestrian detection method based on the improved YOLOv5 model according to claim 8, characterized in that: The EIoU loss function consists of three parts: overlap loss, center point distance loss, and width and height loss.
10. The pedestrian detection method based on the improved YOLOv5 model according to claim 9, characterized in that: The EIoU loss function is specifically: Among them, ρ 2 (b,b gt ) is the Euclidean distance between the center point of the predicted box and the real box, c is the diagonal distance between the predicted box and the minimum bounding box of the real box, α is the weight function, v is the consistency of the aspect ratio, w and w gt are the widths of the predicted box and the true box, h and h respectively. gt are the heights of the predicted box and the true box, respectively, w and c h They are the width and height of the minimum bounding box of the predicted box and the true box, respectively.
Citation Information
Cited By
Infrared and visible light fusion pedestrian detection method based on weight autonomous optimization
CN119723615A
Pedestrian multi-target tracking method based on improved YOLOv5 and feature extraction
CN119991744A
