PCB surface defect detection method based on improved YOLOv5 algorithm
By improving the YOLOv5 algorithm, adding CA modules and bidirectional feature pyramid networks, the accuracy and robustness of existing PCB defect detection technology in complex defect recognition are solved, and more efficient and accurate defect detection is achieved.
Patent Information
- Application Number
- CN202411839800.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-13
- Publication Date
- 2025-04-11
AI Technical Summary
When existing PCB defect detection technology faces complex and changing defects, its accuracy and robustness need to be improved. In addition, traditional manual detection is low efficiency and poor accuracy, which cannot meet the production needs of large batches and high precision.
Using the improved YOLOv5 algorithm, by adding CA modules to the feature extraction network and improving feature fusion network as a bidirectional feature pyramid network, the network's attention to the target and the accuracy of feature fusion are improved.
The model's accuracy of identifying PCB defects is improved, the error detection rate is reduced, and more efficient defect detection is achieved, meeting the needs of large-scale and high-precision production.
Smart Images

Figure CN119810035A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of PCB detection, and in particular to a PCB surface defect detection method based on an improved YOLOv5 algorithm. Background Art
[0002] The quality of printed circuit boards (PCBs) is one of the key issues in the electronic information industry. In the field of PCB manufacturing, defects are inevitable. Once a solder joint has a problem, it will affect the function of the entire circuit board.
[0003] There are six common defects in PCB: leaking holes, excess copper, rat bites, open circuits, short circuits and burrs. Defect detection relies on traditional manual visual comparison, which is inefficient and inaccurate, and cannot meet the needs of large-scale and high-precision production. Therefore, PCB defect detection technology needs to develop in the direction of automation and intelligence, but the accuracy and robustness of existing detection technology need to be improved when facing complex and changeable PCB defects.
[0004] PCB defect detection based on machine vision has become an important part of quality control in the automated production process of modern PCB manufacturing systems. PCB defect detection technology based on machine vision does not need to rely on manual feature extraction. Intelligent defect detection algorithms can accurately extract, identify and classify PCB defect features. Summary of the invention
[0005] The purpose of the present invention is to provide a PCB surface defect detection method based on an improved YOLOv5 algorithm to solve the problems in the background technology.
[0006] In order to solve the above technical problems, the present invention provides a PCB surface defect detection method based on an improved YOLOv5 algorithm, comprising:
[0007] Step S1: Obtain a public PCB defect data set and divide it into a test set and a training set in proportion;
[0008] Step S2: improving the YOLOv 5 PCB defect recognition model, including improving the feature extraction network and improving the feature fusion network;
[0009] Step S3: using the improved recognition model to train the training set of PCB defect images;
[0010] Step S4: Pass the defect image test set into the trained model, record the test results and evaluate the model performance.
[0011] In one embodiment, the improved feature extraction network in step S2 includes:
[0012] (1) Add to the feature extraction network of YOLOv5 CA (Coordinate Attention, Attention Mechanism )mold piece By assigning different weights, key information can be accurately screened from the input features. Because the target position is small, repeated downsampling operations are performed in the process of obtaining low-resolution feature maps to avoid information loss. The last C3 module in the feature extraction network of YOLOv5 adds an attention mechanism. CA The module ensures the integrity of the target location information; the C3 module is a module in the YOLOv5 feature extraction network, which consists of three convolutional layers and multiple Bottleneck modules;
[0013] (2) The specific operation process of the attention mechanism module is divided into two steps, namely, coordinate information embedding and coordinate attention generation. The coordinate information embedding part performs average pooling in the horizontal and vertical coordinate directions of each channel and converts it into a pair of one-dimensional feature encodings to obtain a pair of feature maps with direction-aware features. The height h is the output of the cth channel and is expressed as The mathematical expression is shown in formula (1), and the output of the cth channel with width w is written as The mathematical expression is shown in formula (2):
[0014]
[0015] In formulas (1) and (2), W is Input feature map width , H is Input feature map height , x c It means encoding each channel along the horizontal coordinate and the vertical coordinate. The feature map obtained by the coordinate information embedding process is concatenated, convolved and activated to obtain a feature map with both horizontal and vertical spatial information. The function expression is shown in formula (3):
[0016] f=δ(F1([z h ,z w ])) (3)
[0017] In formula (3), [z h ,z w ] is the Concat operation along the spatial dimension, δ is the nonlinear activation function, and F1 represents the intermediate feature map that encodes the spatial information in the horizontal and vertical directions;
[0018] Then the intermediate features Split into two directions along the space and Where C is the Cth channel, r is the compression ratio in the control channel attention block, and f h and f wIndicates that the convolution and activation function operations are performed on the tensors in the two directions respectively, and the number of channels is equal to the original input features. Figure 1 The result is expanded as the attention weight g in the horizontal and vertical coordinate directions h and g w , as shown in equations (4) and (5):
[0019] g h =σ(F h (f h )) (4)
[0020] g w =σ(F w (f w )) 5)
[0021] In the above formula, σ is the sigmoid activation function, F h for Horizontal convolution function , F w for Vertical convolution function number , multiply the obtained weight by the input feature map to obtain a feature map with weights. The output of the coordinate attention module is shown in formula (6):
[0022]
[0023] x c ( i , j ) is the input feature, is the attention weight in the horizontal direction, For the vertical coordinate direction Attention Weight .
[0024] In one embodiment, improving the feature fusion network in step S2 includes:
[0025] The bidirectional feature pyramid network structure realizes the weighted fusion of features of different scales. The bidirectional feature pyramid network draws on the top-to-bottom and bottom-to-top connections of PANet (Path Aggregation Network). It is different from the PANet network in two aspects:
[0026] (1) The bidirectional feature pyramid network uses a weighted addition method to replace the channel splicing of the PANet network. The bidirectional feature pyramid network sets a weight for each input feature map. The network learns the importance of each input feature layer to determine the weight parameter, so that features of different scales and resolutions contribute different results to their output results, thereby obtaining a more accurate prediction box; the obtained weight is used according to the fast normalization fusion method, and the expression is shown in formula (7):
[0027]
[0028] Among them, I i represents the input feature, O represents the output feature, and W i represents the feature map weight, ∑ j W j is the sum of the weights of various features, ε is a small value of 0.0001, in each W i Then apply an activation function ReLU (Rectified Linear Unit, linear rectification function) to ensure ω i ≥0, ensuring the stability of weight updates;
[0029] (2) The bidirectional feature pyramid network increases Neck middle The lateral connection from the input node to the output node of the layer fuses the complete feature information before fusion with the predicted output layer, fusing more features with less computation.
[0030] In one embodiment, the training of the improved recognition model in step S3 includes: setting the input image size to 1024×1024, setting epochs (total number of training cycles) is 300 ,Preheating training is performed in the first three epochs, the defect image test set is passed into the trained model, the detection results are recorded and the model performance is evaluated.
[0031] In one embodiment, step S4 includes:
[0032] Based on the basic indicators, the precision Precision, recall Recall and average precision AP are calculated. The expressions are as follows:
[0033]
[0034] In the formula, TP is true positive, FP is false positive, FN is false negative, and P(r) represents the function image surrounded by recall and precision. area , the recall and precision obtained from each test are combined according to the relationship, and then the average precision is calculated by interpolation;
[0035] In addition, the detection time DetectionTime is also considered an important indicator for evaluating the real-time performance of the model. The detection time refers to the time it takes for the model to detect an image. The expression is as follows:
[0036]
[0037] Where T is the total test time and N is the total number of test images.
[0038] The present invention provides a PCB surface defect detection method based on an improved YOLOv5 algorithm. Firstly, a CA module is added to the YOLOv5 feature extraction network. By learning the relationship between different areas in a feature map, the CA module better captures position information, improves the network's attention to the target, reduces the weight of irrelevant features, and further reduces the false detection rate of the model. Secondly, a BiFPN structure is used instead of a PANet structure to perform feature fusion of different scales, and a weighted addition method is used instead of channel splicing, so that features of different scales and resolutions make different contributions to the output result, thereby improving the accuracy of the target recognition result. BRIEF DESCRIPTION OF THE DRAWINGS
[0039] Figure 1 It is a flow chart of a PCB surface defect detection method based on an improved YOLOv5 algorithm of the present invention;
[0040] Figure 2 This is a network structure diagram of a PCB surface defect recognition model based on an improved YOLOv5 algorithm of the present invention;
[0041] Figure 3 is the experimental detection effect diagram (a) hole; (b) open circuit; (c) burr; (d) short circuit; (e) excess copper; (f) rat bite DETAILED DESCRIPTION
[0042] The following is a further detailed description of a PCB surface defect detection method based on an improved YOLOv5 algorithm proposed by the present invention in conjunction with the accompanying drawings and specific embodiments. According to the following description, the advantages and features of the present invention will become clearer. It should be noted that the accompanying drawings are all in a very simplified form and are not in precise proportions, which are only used to conveniently and clearly assist in explaining the purpose of the embodiments of the present invention.
[0043] The present invention provides a PCB surface defect detection method based on an improved YOLOv5 algorithm, and the process is as follows: Figure 1 As shown, the following steps are included:
[0044] Step S1: Obtain a public PCB defect dataset and divide it into a test set and a training set in a ratio of 8:2.
[0045] Step S2: improving the YOLOv5 PCB defect recognition model, including improving the feature extraction network and improving the feature fusion network.
[0046] The improved feature extraction network in step S2 is specifically as follows:
[0047] (1) Generally, PCB integration accuracy is high. In order to solve the problem that the recognition model cannot effectively distinguish the target and background information, the present invention adds an attention mechanism (Coordinate Attention, CA) module to the feature extraction network of YOLOv5. By assigning different weights, key information is accurately screened from the input features. Because the target position is small, repeated downsampling operations are performed in the process of obtaining low-resolution feature maps to cause information loss. The last C3 module in the feature extraction network of YOLOv5 is added with a coordinate attention mechanism to ensure the integrity of the target position information. The C3 module is a module in the feature extraction network of YOLOv5, which consists of three convolutional layers and multiple Bottleneck modules.
[0048] (2) The specific operation process of the CA module is divided into two steps, namely, coordinate information embedding and coordinate attention generation. In order to enable the attention mechanism module to capture spatial long-distance relationships with precise location information, the coordinate information embedding part performs average pooling in the horizontal and vertical coordinate directions of each channel and converts it into a pair of one-dimensional feature encodings. A pair of feature maps with direction-aware features can be obtained. The height h is the output of the cth channel and can be expressed as The mathematical expression is shown in formula (1). The output of the cth channel with a width of w can be written as The mathematical expression is shown in formula (2):
[0049]
[0050] In formulas (1) and (2), W is Input feature map width , H is Input feature map height , x c It means encoding each channel along the horizontal coordinate and the vertical coordinate. The feature map obtained by the coordinate information embedding process is concatenated, convolved and activated to obtain a feature map with both horizontal and vertical spatial information. The function expression is shown in formula (3):
[0051] f=δ(F1([z h ,z w ])) (3)
[0052] In formula (3), [z h ,z w ] is the Concat operation along the spatial dimension, δ is the nonlinear activation function, and F1 represents the intermediate feature map that encodes the spatial information in the horizontal and vertical directions.
[0053] Then the intermediate features Split into two directions along the space and Where C is the Cth channel, r is the compression ratio in the control channel attention block, and f h and f w Indicates that the convolution and activation function operations are performed on the tensors in the two directions respectively, and the number of channels is equal to the original input features. Figure 1 The result is expanded as the attention weight g in the horizontal and vertical coordinate directions h and g w , as shown in equations (4) and (5):
[0054] g h =σ(F h (f h )) (4)
[0055] g w =σ(F w (f w )) (5)
[0056] In the above formula, σ is the sigmoid activation function, F h for Horizontal convolution function , F w for Vertical convolution function number The obtained weight is multiplied by the input feature map to obtain a feature map with weights. The output formula of the coordinate attention module is shown in formula (6):
[0057]
[0058] x c ( i, j) is the input feature, is the attention weight in the horizontal direction, For the vertical coordinate direction Attention Weight
[0059] The improved feature fusion network in step S2 is specifically: the bidirectional feature pyramid network (BiFPN) structure realizes the weighted fusion of features of different scales. BiFPN draws on the top-to-bottom and bottom-to-top connections of PANet (Path Aggregation Network), and is different from PANet in two aspects:
[0060] The first point is that BiFPN uses weighted addition to replace the channel splicing of PANet. BiFPN sets a weight for each input feature map, and determines the weight parameter by learning the importance of each input feature layer through the network, so that features of different scales and resolutions contribute different results to their output results, thereby obtaining a more accurate prediction frame; the obtained weight is used according to the fast normalized fusion method (Fast Normalized Fusion), and the expression is shown in formula (7):
[0061]
[0062] Among them, I i represents the input feature, O represents the output feature, and W i represents the feature map weight, ∑ j W j is the sum of the weights of various features, ε is a small value of 0.0001, in each W i Then apply an activation function ReLU (Rectified Linear Unit, linear rectification function) to ensure ω i ≥0, ensuring the stability of weight updates.
[0063] The second point is to add a lateral connection from the input node to the output node in the Neck intermediate layer, which fuses the complete feature information before fusion with the prediction output layer, and fuses more features with less computational effort.
[0064] Step S3: Figure 2 As shown, the improved recognition model is used to train the training set of PCB defect images. The performance of the running device of the present invention is as follows: GPU is RTX3090 (24GB) * 1, CPU is 15Vcpu Intel (R) Xeon (R) Platinum 8362 CPU @ 2.80GHz. The improved network model is trained: the input image size is set to 1024 × 1024, and the epoch is set s (total number of training cycles) The size of the image is 300, and warm-up training is performed in the first three epochs. The defect image test set is passed into the trained model, the detection results are recorded, and the model performance is evaluated.
[0065] Step S4: Pass the defect image test set into the trained model, record the test results and evaluate the model performance. In order to more intuitively reflect the target recognition performance, the accuracy (Precision), recall (Recall) and average precision (AP) can be calculated based on the basic indicators. The expressions are as follows:
[0066]
[0067] Where TP is true positive, FP is false positive, FN is false negative, P(r) represents the area of the function image enclosed by the recall and precision. The recall and precision obtained from each detection are combined according to the relationship, and then the average precision is calculated by interpolation.
[0068] In actual engineering applications, in addition to model recognition accuracy, detection time is also considered an important indicator for evaluating the real-time performance of the model. Detection time refers to the time it takes for the model to detect an image. The expression is as follows:
[0069]
[0070] After testing and comparison, this method has higher accuracy in detecting PCB defects. The detection results are shown in Figure 3, where the defect in Figure 3 (a) is a hole, the defect in Figure 3 (b) is an open circuit, the defect in Figure 3 (c) is a burr, the defect in Figure 3 (d) is a short circuit, the defect in Figure 3 (e) is excess copper, and the defect in Figure 3 (f) is a rat bite. The detection accuracy of this method is shown in Table 1, and the comparison with the existing method is shown in Table 2.
[0071]
[0072] Table 1 Detection accuracy of this method
[0073] From the above results, it can be seen that the average accuracy of the proposed method at the IOU (degree of overlap between the target box and the prediction box) threshold of 0.5 is higher than 94%, and the average accuracy at the IOU 0.5-0.95 threshold is higher than 45%, indicating that the proposed method can achieve accurate extraction, recognition and classification of PCB defect features.
[0074]
[0075] Table 2. Comparison with YOLO V Comparison results of 5
[0076] The comparison shows that the average accuracy of the proposed method is better than that of YOLO. V 5 is 0.6% higher, and with the same recall rate, the accuracy is higher than YOLO V 52.6%, indicating that the proposed method has higher detection accuracy, lower false positive rate and better effect.
[0077] The present invention provides a PCB surface defect detection method based on an improved YOLOv5 algorithm. By preparing an appropriate image data set and training a model, real-time defect detection can be achieved, thereby improving production efficiency and product quality. At the same time, only the training data, model architecture and parameters need to be adjusted to be applicable to different types of defect detection tasks, and the method has strong scalability. The average accuracy of the method proposed by the present invention is 0.6% higher than that of YOLOv5, and under the same recall rate, the accuracy is 2.6% higher than that of YOLOv5, indicating that the proposed method not only realizes the accurate extraction, recognition and classification of PCB defect features, but also has higher detection accuracy and lower false detection rate, providing a powerful technical solution for subsequent correction and improvement of PCB.
[0078] The above description is only a description of the preferred embodiments of the present invention, and is not intended to limit the scope of the present invention. Any changes or modifications made by a person skilled in the art in the field of the present invention based on the above disclosure shall fall within the scope of protection of the claims.
Claims
1. A PCB surface defect detection method based on an improved YOLOv5 algorithm, characterized in that: include: Step S1: Obtain a public PCB defect data set and divide it into a test set and a training set in proportion; Step S2: improving the YOLOv5 PCB defect recognition model, including improving the feature extraction network and improving the feature fusion network; Step S3: using the improved recognition model to train the training set of PCB defect images; Step S4: Pass the defect image test set into the trained model, record the test results and evaluate the model performance.
2. The PCB surface defect detection method based on the improved YOLOv5 algorithm as claimed in claim 1, characterized in that: The improved feature extraction network in step S2 includes: (1) Add to the feature extraction network of YOLOv5 CA (Coordinate Attention, Attention Mechanism ) module By assigning different weights, key information can be accurately screened from the input features. Because the target position is small, repeated downsampling operations are performed in the process of obtaining low-resolution feature maps to avoid information loss. The last C3 module in the feature extraction network of YOLOv5 adds an attention mechanism. CA The module ensures the integrity of the target location information; the C3 module is a module in the YOLOv5 feature extraction network, which consists of three convolutional layers and multiple Bottleneck modules; (2) The specific operation process of the attention mechanism module is divided into two steps, namely, coordinate information embedding and coordinate attention generation. The coordinate information embedding part performs average pooling in the horizontal and vertical coordinate directions of each channel and converts it into a pair of one-dimensional feature encodings to obtain a pair of feature maps with direction-aware features. The height h is the output of the cth channel and is expressed as The mathematical expression is shown in formula (1), and the output of the cth channel with width w is written as The mathematical expression is shown in formula (2): In formulas (1) and (2), W is Input feature map width , H is Input feature map height , x c It means encoding each channel along the horizontal coordinate and the vertical coordinate. The feature map obtained by the coordinate information embedding process is concatenated, convolved and activated to obtain a feature map with both horizontal and vertical spatial information. The function expression is shown in formula (3): f=δ(F1([z h ,z w ])) (3) In formula (3), [z h ,z w ] is the Concat operation along the spatial dimension, δ is the nonlinear activation function, and F1 represents the intermediate feature map that encodes the spatial information in the horizontal and vertical directions; Then the intermediate features Split into two directions along the space and Where C is the Cth channel, r is the compression ratio in the control channel attention block, and f h and f w It means to perform convolution and activation function operations on the tensors in two directions respectively, make the number of its channels consistent with the original input feature map, and expand the result as the attention weight g in the horizontal and vertical coordinate directions h and g w , as shown in equations (4) and (5): g h =σ(F h (f h )) (4) g w =σ(F w (f w )) (5) In the above formula, σ is the sigmoid activation function, F h for Horizontal convolution function , F w for Vertical convolution function , multiply the obtained weight by the input feature map to obtain a feature map with weights. The output of the coordinate attention module is shown in formula (6): x c (i , j) for Input features, is the attention weight in the horizontal direction, is the attention in the vertical coordinate direction Weight .
3. The PCB surface defect detection method based on the improved YOLOv5 algorithm as claimed in claim 1, characterized in that: The improved feature fusion network in step S2 includes: The bidirectional feature pyramid network structure realizes the weighted fusion of features of different scales. The bidirectional feature pyramid network draws on the top-to-bottom and bottom-to-top connections of PANet (Path Aggregation Network). It is different from the PANet network in two aspects: (1) The bidirectional feature pyramid network uses a weighted addition method to replace the channel splicing of the PANet network. The bidirectional feature pyramid network sets a weight for each input feature map. The network learns the importance of each input feature layer to determine the weight parameter, so that features of different scales and resolutions contribute different results to their output results, thereby obtaining a more accurate prediction box; the obtained weight is used according to the fast normalization fusion method, and the expression is shown in formula (7): Among them, I i represents the input feature, O represents the output feature, and W i represents the feature map weight, ∑ j W j is the sum of the weights of various features, ε is a small value of 0.0001, in each W i Then apply an activation function ReLU (Rectified Linear Unit, linear rectification function) to ensure ω i ≥0, ensuring the stability of weight updates; (2) The bidirectional feature pyramid network increases Neck middle The lateral connection from the input node to the output node of the layer fuses the complete feature information before fusion with the predicted output layer, fusing more features with less computation.
4. The PCB surface defect detection method based on the improved YOLOv5 algorithm as claimed in claim 1, characterized in that: The training of the improved recognition model in step S3 includes: setting the input image size to 1024×1024, setting epochs (total number of training cycles) is 300 ,Preheating training is performed in the first three epochs, the defect image test set is passed into the trained model, the detection results are recorded and the model performance is evaluated.
5. The PCB surface defect detection method based on the improved YOLOv5 algorithm as claimed in claim 1, characterized in that: The step S4 comprises: Based on the basic indicators, the precision Precision, recall Recall and average precision AP are calculated. The expressions are as follows: In the formula, TP is true positive, FP is false positive, FN is false negative, and P(r) represents the function image surrounded by recall and precision. area , the recall and precision obtained from each test are combined according to the relationship, and then the average precision is calculated by interpolation; In addition, the detection time DetectionTime is also considered an important indicator for evaluating the real-time performance of the model. The detection time refers to the time it takes for the model to detect an image. The expression is as follows: Where T is the total test time and N is the total number of test images.
Citation Information
Cited By
Bridge surface disease detection method based on improved Yolov8 target detection
CN121236606A
Bridge surface disease detection method based on improved yolov8 target detection
CN121236606B
PCB defect detection method and system based on improved YOLOv11s
CN121305205A