Attention mechanism for lightweight YOLOv5 insulator defect detection
By introducing the SGA attention mechanism in YOLOv5, using group normalization and scaling dot product attention weight calculation, the problem of insufficient feature extraction capability and large calculation volume of YOLOv5 in complex backgrounds is solved, and more efficient and accurate Insulator defect detection.
Patent Information
- Application Number
- CN202411394402.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-08
- Publication Date
- 2025-02-11
AI Technical Summary
YOLOv5 has insufficient ability to extract defective target and small target feature in complex contexts, and because the location information and semantic information are not clear enough, it cannot effectively integrate features, resulting in large amounts of computing and cannot be deployed in drone terminals in real time.
A SGA attention mechanism is adopted to optimize feature normalization through group normalization, and the attention weight is calculated using the scaling dot product method, which avoids batch size dependence and improves feature extraction ability and model performance.
It effectively solves the problems of batch size dependence, insufficient feature extraction, high error detection rate and model performance optimization, and improves the accuracy and efficiency of detection.
Smart Images

Figure CN119417764A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to an attention mechanism, and in particular to an attention mechanism for lightweight YOLOv5 insulator defect detection. Background Art
[0002] YOLOv5 is one of the one-stage detection models with a low training threshold in the You Only Look Once (YOLO) series. Among many models, this model achieves the best balance between detection speed and accuracy. It is currently the most widely used transmission line insulator defect detection model.
[0003] Although YOLOv5 inherits many improvement strategies from previous versions, it still has poor feature extraction capabilities for defective targets and smaller targets in complex backgrounds. In addition, since the location information and semantic information of such targets are not clear enough, the feature fusion method of FPN+PAN alone cannot achieve effective fusion of features. The model has too much computational complexity, making it impossible to effectively deploy it on drone terminals for real-time detection. Summary of the invention
[0004] The purpose of the present invention is to provide an attention mechanism for lightweight YOLOv5 insulator defect detection in view of the shortcomings and defects in the prior art. In the insulator defect detection task, the SGA attention mechanism effectively solves the problems of batch size dependence, insufficient feature extraction, high false positive rate and model performance optimization by optimizing feature normalization and attention calculation method, thereby improving the accuracy and efficiency of detection.
[0005] To achieve the above objectives, the present invention adopts the following technical solution: an attention mechanism for lightweight YOLOv5 insulator defect detection, which includes a feature normalization part and an attention weight calculation part.
[0006] An attention mechanism for lightweight YOLOv5 insulator defect detection, which also includes the following calculation formula: x'=f 1×1 (GN(x)×Softmax(SDA(Q,K,V)));Q=δ(W1x),K=σ(W2x), Among them, W1, W2, and W3 are the weight matrices obtained by x passing through three fully connected layers respectively.
[0007] Furthermore, the feature normalization part is specifically as follows: Different from the batch normalization (BatchNorm) used by the NAM module, the SGA mechanism uses group normalization for feature normalization. BatchNorm requires a larger batch size to maintain the accuracy of the model, but this is not realistic when processing large input images due to memory limitations. GroupNorm avoids dependence on batch size by grouping channels and calculating the mean and variance of each group.
[0008] Furthermore, in the feature normalization stage, the attention mechanism uses GroupNorm instead of BatchNorm used in the original NAM. This is because in actual model training, BatchNorm needs to use a sufficiently large Batch Size. If the Batch Size is too small, the false detection rate of the model will be greatly increased. However, in actual tasks, the input image data is very large, and a larger Batch Size will cause video memory overflow. The normalization method of GroupNorm avoids the influence of Batch size on the model. First, the channels are grouped, and then the mean and variance of each group in the channel direction are calculated. The calculation process is as follows: Among them, μ ng (x) represents the mean of the current group with n dimensions retained; σ ng (x) represents the variance of the current group; ε is a value as small as possible; γ and β are scaling factors and translation factors respectively.
[0009] Furthermore, the attention weight calculation part is specifically as follows: inspired by the single-head self-attention model, the SGA mechanism calculates the attention weight by scaling the dot product method, and the process includes the following steps:
[0010] 1) Extract Query, Key, and Value vectors: The input features are processed through three fully connected (FC) layers to extract Query, Key, and Value vectors;
[0011] 2) Similarity calculation: perform a scaled dot product operation on the query and key vectors to calculate the similarity score matrix (scores) between the query and key vectors;
[0012] 3) Softmax normalization: Use the Softmax function to normalize the score matrix to obtain the weight matrix of the Value vector;
[0013] 4) Weighted features: Multiply the weight matrix by the Value vector to obtain the attention-weighted features.
[0014] Furthermore, in the attention weight calculation stage, the scaled dot product attention mechanism in the single-head self-attention model is used to redesign a branch to calculate the attention weight. First, three fully connected layers are used to extract the Query, Key, and Value vectors from the input x, and then the scaled dot product is performed on the Query and Key vectors, and the similarity between each Query vector and the Key vector is calculated to obtain the score matrix scores, and then the score matrix is normalized by Softmax to obtain the weight matrix of the Value vector, and finally the weight matrix is multiplied by the Value vector to obtain the attention weight.
[0015] After adopting the above technical scheme, the beneficial effect of the present invention is as follows: in the insulator defect detection task, the SGA attention mechanism effectively solves the problems of batch size dependence, insufficient feature extraction, high false detection rate and model performance optimization by optimizing feature normalization and attention calculation method, thereby improving the accuracy and efficiency of detection. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.
[0017] Figure 1 It is a structural schematic diagram of the present invention.
[0018] Figure 2 It is a diagram of the normalization method of BatchNorm.
[0019] Figure 3 It is a diagram of the normalization method of GroupNorm.
[0020] Figure 4 This is the initial YOLOv5 comparison chart.
[0021] Figure 5 This is a comparison chart of YOLOv5 with SGA.
[0022] Figure 6 This is a comparison chart of the initial YOLOv5 model.
[0023] Figure 7 This is a model comparison chart of YOLOv5 with SGA. DETAILED DESCRIPTION
[0024] In previous studies on the attention mechanism, Yichao Liu et al. proposed a lightweight attention mechanism module NAM. The NAM attention mechanism is an improvement on the CBAM attention mechanism. It mainly consists of two parts: feature normalization and attention weight acquisition. In the feature normalization part, the input feature vector is processed by BatchNorm to obtain the normalized feature vector. In the weight acquisition part, the weight is calculated by dividing the scaling factor of each channel by the sum of the scaling factors of each channel. Finally, the calculated weight is multiplied by the normalized feature vector to achieve feature weighting operation.
[0025] See also Figure 1 As shown, the technical solution adopted in this specific implementation is: it includes a feature normalization part and an attention weight calculation part, and it also includes the following calculation formula:
[0026] x'=f 1×1 (GN(x)×Softmax(SDA(Q,K,V))) (1)
[0027]
[0028]
[0029] Among them, W1, W2, and W3 are the weight matrices obtained by x passing through three fully connected layers respectively.
[0030] More specifically, the feature normalization part is as follows: Unlike the batch normalization (BatchNorm) used by the NAM module, the SGA mechanism uses group normalization for feature normalization. BatchNorm requires a larger batch size to maintain the accuracy of the model, but this is not realistic when processing large input images due to memory limitations. GroupNorm avoids dependence on batch size by grouping channels and calculating the mean and variance of each group.
[0031] In the feature normalization stage, the attention mechanism uses GroupNorm instead of BatchNorm used in the original NAM. This is because in actual model training, BatchNorm needs to use a sufficiently large Batch Size. If the Batch Size is too small, the false detection rate of the model will be greatly increased. However, in actual tasks, the input image data is very large, and a larger Batch Size will cause video memory overflow. The GroupNorm normalization method avoids the impact of Batch size on the model. First, the channels are grouped, and then the mean and variance of each group in the channel direction are calculated. The calculation process is shown in formulas (4), (5), and (6). The normalization methods of BatchNorm and GroupNorm are as follows: Figure 2-Figure 3 shown.
[0032]
[0033]
[0034]
[0035] Among them, μ ng (x) represents the mean of the current group with n dimensions retained; σ ng (x) represents the variance of the current group; ε is a value as small as possible, such as 1×10 -8 ; γ and β are scaling factor and translation factor respectively.
[0036] More specifically, the attention weight calculation part is as follows: inspired by the single-head self-attention model, the SGA mechanism calculates the attention weight by scaling the dot product method, and the process includes the following steps:
[0037] 1) Extract Query, Key, and Value vectors: The input features are processed through three fully connected (FC) layers to extract Query, Key, and Value vectors;
[0038] 2) Similarity calculation: perform a scaled dot product operation on the query and key vectors to calculate the similarity score matrix (scores) between the query and key vectors;
[0039] 3) Softmax normalization: Use the Softmax function to normalize the score matrix to obtain the weight matrix of the Value vector;
[0040] 4) Weighted features: Multiply the weight matrix by the Value vector to obtain the attention-weighted features.
[0041] In the attention weight calculation stage, the scaled dot product attention mechanism in the single-head self-attention model is used to redesign a branch to calculate the attention weight, such as Figure 1 shown.
[0042] First, three fully connected layers are used to extract the Query, Key, and Value vectors from the input x. Then, the scaled dot product is performed on the Query and Key vectors, and the similarity between each Query vector and the Key vector is calculated to obtain the score matrix scores. Then, the score matrix is normalized by Softmax to obtain the weight matrix of the Value vector. Finally, the weight matrix is multiplied by the Value vector to obtain the attention weight.
[0043] The following is a comparison of the specific experiments and detection results of the present invention.
[0044] experiment:
[0045] The baseline model is YOLOv5s, and the SGA attention mechanism is introduced into the YOLOv5s feature extraction network as an improved model. Training and testing are performed on the public Chinese power line insulator dataset CPLID, and the average precision mean mAP@50, frames per second FPS, and floating point calculation FLOPs are selected to evaluate the algorithm. Table 1 shows the performance indicators of the baseline model and the improved model.
[0046] Table 1
[0047]
[0048] Visual comparison:
[0049] To verify the effectiveness of SGA, the ninth layer of the initial YOLOv5 network (see Figure 4 ) and the improved YOLOv5 network introduces SGA at the 10th layer for visualization comparison (see Figure 5 ), the results show that the SGA attention mechanism can effectively improve the network feature extraction ability.
[0050] Comparison of detection effects:
[0051] To further verify the effectiveness of the SGA attention mechanism, the improved model is used to compare the detection effect with the initial YOLOv5s model. A picture is randomly selected from the CPLID public dataset as the input of the model detect. The results are as follows. Figure 6-Figure 7 .
[0052] The experimental results show that after adding the SGA attention mechanism to the network, the detection accuracy of each category has been improved, and through the first group of comparisons, it can be clearly seen that the addition of SGA solves the problem of network missed detection.
[0053] The above description is only used to illustrate the technical solution of the present invention rather than to limit it. Other modifications or equivalent substitutions made to the technical solution of the present invention by ordinary technicians in this field should be included in the scope of the claims of the present invention as long as they do not depart from the spirit and scope of the technical solution of the present invention.
Claims
1. An attention mechanism for lightweight YOLOv5 insulator defect detection, characterized by: It consists of a feature normalization part and an attention weight calculation part.
2. An attention mechanism for lightweight YOLOv5 insulator defect detection, characterized by: It also includes the following calculation formula: x'=f 1×1 (GN(x)×Softmax(SDA(Q,K,V))) (1) Where W1, W2, and W3 are the weight matrices obtained by x passing through three fully connected layers respectively.
3. The attention mechanism for lightweight YOLOv5 insulator defect detection according to claim 1, characterized in that: The feature normalization part is specifically as follows: Unlike the batch normalization (BatchNorm) used by the NAM module, the SGA mechanism uses group normalization for feature normalization. BatchNorm requires a larger batch size to maintain the accuracy of the model, but this is not practical due to memory limitations when processing large input images. GroupNorm avoids dependence on batch size by grouping channels and calculating the mean and variance of each group.
4. The attention mechanism for lightweight YOLOv5 insulator defect detection according to claim 1, characterized in that: In the feature normalization stage, the attention mechanism uses GroupNorm instead of BatchNorm used in the original NAM. This is because in actual model training, BatchNorm needs to use a sufficiently large Batch Size. If the Batch Size is too small, the false detection rate of the model will be greatly increased. However, in actual tasks, the input image data is very large, and a larger Batch Size will cause video memory overflow. The normalization method of GroupNorm avoids the influence of Batch size on the model. First, the channels are grouped, and then the mean and variance of each group in the channel direction are calculated. The calculation process is as follows: Among them, μ ng (x) represents the mean of the current group with n dimensions retained; σ ng (x) represents the variance of the current group; ε is a value as small as possible; γ and β are scaling factors and translation factors respectively.
5. The attention mechanism for lightweight YOLOv5 insulator defect detection according to claim 1, characterized in that: The attention weight calculation part is specifically as follows: inspired by the single-head self-attention model, the SGA mechanism calculates the attention weight by scaling the dot product method. The process includes the following steps: 1) Extract Query, Key, and Value vectors: The input features are processed through three fully connected (FC) layers to extract Query, Key, and Value vectors; 2) Similarity calculation: perform a scaled dot product operation on the query and key vectors to calculate the similarity score matrix (scores) between the query and key vectors; 3) Softmax normalization: Use the Softmax function to normalize the score matrix to obtain the weight matrix of the Value vector; 4) Weighted features: Multiply the weight matrix by the Value vector to obtain the attention-weighted features.
6. The attention mechanism for lightweight YOLOv5 insulator defect detection according to claim 1, characterized in that: In the attention weight calculation stage, the scaled dot product attention mechanism in the single-head self-attention model is used to redesign a branch to calculate the attention weight; First, three fully connected layers are used to extract the Query, Key, and Value vectors from the input x. Then, the scaled dot product is performed on the Query and Key vectors, and the similarity between each Query vector and the Key vector is calculated to obtain the score matrix scores. Then, the score matrix is normalized by Softmax to obtain the weight matrix of the Value vector. Finally, the weight matrix is multiplied by the Value vector to obtain the attention weight.