PCB surface defect optimization detection method based on improved YOLOv5
By adding a small object detection layer in the YOLOv5 network, integrating CBAM and SE attention mechanisms, and selecting the NWD loss function, the problem of YOLOv5's low detection accuracy in PCB surface defect detection is solved, and efficient and accurate defect detection is achieved, satisfying the Industrial demand.
Patent Information
- Application Number
- CN202411134107.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-19
- Publication Date
- 2024-12-06
AI Technical Summary
The existing YOLOv5 has problems such as low detection accuracy, large parameters and slow detection speed in PCB surface defect detection, which is difficult to meet the needs of modern electronic manufacturing for efficient and high-precision detection.
Add a small object detection network structure layer to the YOLOv5 network structure, delete the large object detection layer, integrate the CBAM module and SE attention mechanism, select the NWD loss function, and replace the C3 module in the backbone network to improve feature extraction and detection performance.
The accuracy and efficiency of PCB surface defect detection have been significantly improved, with the average average accuracy (mAP) reaching 97.3%. The detection accuracy, recall and accuracy of various defects have increased significantly, meeting the needs of industrial production.
Smart Images

Figure CN119090827A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of electronic industry, and in particular provides a PCB surface defect optimization detection method based on improved YOLOv5. Background Art
[0002] With the rapid development of the electronic information industry, the quality of printed circuit boards (PCBs), as the core components of electronic products, has a crucial impact on the performance and reliability of the final products. The PCB manufacturing process is complex and involves multiple processes, which will inevitably produce various surface defects, such as missing holes, mouse bites, open circuits, short circuits, strays, and false copper. These defects not only affect the electrical performance of the product, but may also cause serious problems such as short circuits and open circuits. Therefore, effective detection of PCB surface defects is a key link in ensuring product quality.
[0003] Traditional PCB defect detection methods mainly rely on manual visual inspection or automated detection based on image processing technology, but these methods have problems such as high labor costs, low detection efficiency, and low detection accuracy, which makes it difficult to meet the needs of modern electronic manufacturing for efficient and high-precision detection. In recent years, with the rapid development of computer vision and deep learning technology, in order to reduce the pressure of traditional manual detection and improve detection efficiency, target detection methods based on deep learning have shown high practical value in the field of PCB defect detection.
[0004] Shi Zaoxiong et al. proposed to integrate the coordinate attention mechanism into the backbone network of YOLOv5 to enhance the spatial sensitivity of features, introduced the Transformer Encoder module into its neck network and added a high-resolution detection head, which was optimized specifically for small targets. The E-IoU algorithm was used to replace the traditional IoU calculation method, and the detection performance was significantly improved, which can meet the needs of industrial field detection. Tuo Bing et al. optimized the input model data and used the parameter-free SimAM attention mechanism to enhance the feature extraction and positioning capabilities of the backbone network. The CSPHB module was used to replace the CSPLayer structure in the feature fusion network, and the feature fusion interaction capability of the feature fusion network was strengthened, thereby improving the model detection performance. The experimental results show that the improved mean average precision (mAP) of each module has been improved to varying degrees, and the average precision of each category of defects has been significantly improved. Chen Yifei and others improved the accuracy of small target defect detection, constructed a network connection method based on BiFPN, realized the effective fusion and utilization of multi-scale feature information, and introduced the Coordinate Attention mechanism to further improve the accuracy of the model in target positioning and capture, and the mean average accuracy of detection was improved by 3.2%. The above algorithms have greatly improved in performance, but there are still some problems such as low detection accuracy, large number of parameters and slow detection speed.
[0005] In summary, in view of the above-mentioned problems of YOLOv5 in PCB surface defect detection, it is very meaningful to improve YOLOv5 to improve the detection accuracy of defects and fault points on PCB boards. Summary of the invention
[0006] In view of this, the purpose of the present invention is to provide a PCB surface defect optimization detection method based on improved YOLOv5, so as to effectively improve the accuracy of PCB defect detection.
[0007] The technical solution provided by the present invention is: a PCB surface defect optimization detection method based on improved YOLOv5, comprising the following steps:
[0008] S1: Obtain public pre-processed PCB images and label data, and establish a YOLOv5 network structure model based on the PCB surface defect sample dataset;
[0009] S2: Add a small target detection network structure layer to the anchors in the YOLOv5 network structure, and delete the large target detection layer. The small target detection network structure layer is suitable for detecting small components on PCB boards.
[0010] S3: Integrate CBAM into the C3 module of YOLOv5s, so that the last two C3 modules of the backbone network of the YOLOv5 network are changed to C3CBAM;
[0011] S4: Add the attention mechanism module SE to the backbone network of the YOLOv5 network;
[0012] S5: Select NWD as the loss function; get the improved YOLOv5 network model;
[0013] S6: Using the PCB surface defect sample data set to train the improved YOLOv5 network model, to obtain a trained improved YOLOv5 network model, i.e., a PCB surface defect detection model;
[0014] S7: Input the PCB image to be inspected into the PCB surface defect detection model after image preprocessing, and output the defect position in the PCB image and the corresponding defect category.
[0015] Preferably, in S2, the anchors in the YOLOv5 network structure add a small target detection network structure layer and delete the large target detection layer, including:
[0016] S21: Modify the anchors to include three detection heads: anchors of 40x40 feature maps, anchors of 80x80 feature maps, and smaller anchors of 160x160 feature maps, which are used to detect small targets of 4x4 and above.
[0017] S22: Modify Head and set the detection layer to [22, 25, 28]. In the 18th layer, upsample the 80x80 feature map in Neck to 160x160. In the 21st layer, concat the obtained 160x160 feature map with the 160x160 feature map of the second layer in Backbone to obtain a larger feature map for small target detection. Finally, add a target detection layer in the 29th layer.
[0018] Preferably, the training process after CBAM in S3 is added to the network structure:
[0019] The feature map F is input into the channel attention module, and the corresponding attention map is output. Then the input feature map is multiplied by the attention map, and the output passes through the spatial attention block and performs the same operation. Finally, the output feature map is obtained as follows:
[0020]
[0021]
[0022] Among them, Mc∈R1×1×C is a one-dimensional channel attention map, Ms∈RW×H×1 is a two-dimensional spatial attention map, Represents element-wise multiplication.
[0023] The PCB surface defect optimization detection method based on improved YOLOv5 provided by the present invention can more accurately detect various defects on the PCB board and improve the detection performance of the algorithm for PCB surface defects by optimizing and improving the YOLOv5 network structure; the anchors in the YOLOv5 network add a small target detection layer to enhance the ability to extract small target features. An attention mechanism (SE) module is added to the backbone network to improve the algorithm's attention to the small target area, and the last two layers of C3 modules in the backbone network are replaced by C3-CBAM to further improve the model's perception of defects. The loss function is designed to be NWD, which is beneficial to small targets, so as to fully learn the features of the image.
[0024] The method provided by the present invention effectively improves the accuracy of PCB defect detection, shortens the time of PCB defect detection and improves the detection efficiency, and can meet the needs of industrial production. Tests were conducted on the PCB defect dataset published by the Robotics Laboratory of Peking University. The results show that the optimized YOLOv5 model has a mean average precision (mAP) of 97.3% in the defect detection task, which is a significant improvement of 3.3% compared with the original algorithm model. In addition, the detection accuracy, recall rate and precision rate of various defects have achieved comprehensive growth, which fully verifies the effectiveness and practicality of the proposed improved method. BRIEF DESCRIPTION OF THE DRAWINGS
[0025] The present invention is further described in detail below in conjunction with the accompanying drawings and embodiments:
[0026] Figure 1 A flow chart of the PCB surface defect optimization detection method based on improved YOLOv5 provided by the present invention;
[0027] Figure 2 A schematic diagram of the improved YOLOv5 network structure provided by the present invention;
[0028] Figure 3 This is a schematic diagram of the network structure of the SE attention mechanism involved in the present invention;
[0029] Figure 4 The improved C3CBAM network structure involved in the present invention;
[0030] Figure 5 These are some of the datasets disclosed by Peking University involved in the present invention;
[0031] Figure 6The results of training the model based on the dataset disclosed by Peking University involved in the present invention;
[0032] Figure 7 The confusion matrix of the detection effect of six kinds of PCB defects by the method of the present invention is shown;
[0033] Figure 8 The figure is a defect detection result diagram of a PCB image using the method of the present invention. DETAILED DESCRIPTION
[0034] The present invention will be further explained below in conjunction with specific implementation schemes, but the present invention is not limited thereto.
[0035] like Figures 1 to 3 As shown, the present invention provides a PCB surface defect optimization detection method based on improved YOLOv5, comprising the following steps:
[0036] S1: Obtain public pre-processed PCB images and label data, and establish a YOLOv5 network structure model based on the PCB surface defect sample dataset;
[0037] S2: Add a small target detection network structure layer to the anchors in the YOLOv5 network structure, and delete the large target detection layer. The small target detection network structure layer is suitable for detecting small components on PCB boards.
[0038] The network structure of YOLOv5 consists of four parts: input, backbone, neck and head. The existing YOLOv5 contains three detection heads: 20x20 (for large target detection), 40x40 (for medium target detection) and 80x80 (for small target detection). The present invention adds a 160x160 small target detection layer before the 80x80 layer and deletes the 20x20 large target detection layer.
[0039] In the original YOLOv5s algorithm, the 640×640 input image is downsampled by 8 times, 16 times, and 32 times to obtain feature maps of sizes 80×80, 40×40, and 20×20 for detecting small, medium, and large-scale targets.
[0040] Since there is no 160x160 feature map in the Neck part of the original network structure, it is necessary to upsample the 80x80 feature map to obtain a 160x160 feature map. The newly added detection layer first upsamples the 80x80 feature map in Neck to 160x160, and then concats it with the 160x160 feature map of the second layer in Backbone to generate a new 160x160 feature map. Then, the 80x80 feature map is obtained again by downsampling (combining FPN and TPN methods) and passed to the detection head through the C3 module. Finally, the large target detection layer and some corresponding structures are deleted.
[0041] Specific steps:
[0042] (1) Modify anchors: Add a set of smaller anchors for 160x160 feature maps to detect small objects of 4x4 and above, and delete the original larger anchors for 20X20 feature maps.
[0043] (2) Modify the Head part: add a layer to the network structure. The Head part of YOLOv5 consists of two parts: Neck and Detect. The first two stages are upward concat, and the last two stages are downward concat. Finally, there are three detection layers, located in the 18th, 21st and 24th layers respectively. First, in the 18th layer, the 80x80 feature map in Neck is upsampled to 160x160. Then, in the 21st layer, the obtained 160x160 feature map is concat fused with the 160x160 feature map of the second layer in Backbone to obtain a larger feature map for small target detection. In addition, the original 29, 30, and 31 layers are deleted. Finally, a small target detection layer is added to the 29th layer, and a total of three layers [22, 25, 28] are used for detection.
[0044] Considering that the target scale of PCB defects is generally small, the detection layer P5 responsible for large-scale target detection in the original algorithm was deleted to reduce unnecessary parameters and calculations.
[0045] The original yolov5 is P3, P4, P5, used to detect small, medium and large. This application adds a small target detection layer P2, deletes the original P5, and finally retains P2, P3, P4;
[0046] A 160×160 small target detection layer P2 is added to retain more location information and detail information of small-scale targets, so as to provide stronger location information when performing multi-scale feature fusion at the neck end, so that the predicted feature map has more complete context information, which is conducive to the network capturing and learning the feature information of small-scale targets.
[0047] S3: Integrate CBAM into the C3 module of YOLOv5s, so that the last two C3 modules of the backbone network of the YOLOv5 network are changed to C3CBAM;
[0048] CBAM is an efficient and lightweight attention module that can be integrated into any convolutional neural network architecture and can be trained end-to-end with the basic network. CBAM takes into account the imbalance of weights between different channels and the imbalance of spatial dimensions. First, the feature map F is input into the channel attention module, and the corresponding attention map is output. Then, the input feature map is multiplied by the attention map, and the output passes through the spatial attention block, and the same operation is performed to finally obtain the output feature map. The process is as follows:
[0049]
[0050] Among them, Mc∈R1×1×C is a one-dimensional channel attention map, Ms∈RW×H×1 is a two-dimensional spatial attention map, Represents element-by-element multiplication. CBAM uses MaxPooling and AvgPooling simultaneously in two independent dimensions, so richer high-level features can be extracted. The present invention integrates CBAM into the C3 module of YOLOv5s to respectively learn target features and position features in the channel dimension and the global space dimension, increase the weight of useful features, suppress the weight of invalid features, and pay more attention to the target area containing important information.
[0051] S4: Add the attention mechanism module SE to the backbone network of the YOLOv5 network;
[0052] SE is an attention mechanism module used in convolutional neural networks. Its main purpose is to enable the model to more effectively capture important information of input features by adaptively adjusting the attention weights of channels, thereby improving the performance and generalization ability of the model. The present invention adds the SE attention mechanism to the end of the backbone, so that the attention mechanism can see the feature map of the entire backbone part and have a global perspective.
[0053] Specifically, first, input the feature map, whose dimension is H*W*C. Perform global flat pooling to generate a 1*1*C feature vector, the formula is as follows:
[0054]
[0055] Where: Zc is the output of the c-th layer channel after compression; Fsq() is the squeezing function; Uc is the point feature map of the c-th layer channel with coordinates (i, j).
[0056] The next step is to pass through two fully connected layers. The first FC layer plays a role in dimensionality reduction, then ReLU activation is used, and the last FC layer restores the original dimension. The required weight information is generated through the weight W, where W is obtained through learning. The calculation formula is as follows:
[0057] s=F ex (z,W)=σ(g(z,W))=σ(W2δ(W1z)) (4)
[0058] Where W1 and W2 are the parameters of the fully connected layer; δ represents the ReLU function; σ represents the sigmoid function.
[0059] After the weights are normalized by the Sigmoid activation function, the normalized weights are finally added to the features of each channel to obtain a new feature map X. The calculation formula is as follows:
[0060]
[0061] Where: Xc is the calculated new feature; Fscale() is the feature calibration function; Sc is the weight of the c-th layer channel after the excitation operation; Uc is the feature map of the c-th layer channel.
[0062] S5: Select NWD (Normalized Wasserstein Distance) as the loss function; this invention introduces the NWD loss function to replace the original network GIoU loss function to obtain an improved YOLOv5 network model; the loss function is designed to be NWD, which is beneficial to small targets. NWD is a new measurement method to calculate the similarity between boxes, that is, to model the box as a Gaussian distribution, and then use the Wasserstein distance to measure the similarity between the two distributions to replace IoU. The advantage of this distance is that even if the two boxes do not overlap at all, or overlap very little, the similarity can still be measured. In addition, NWD is not sensitive to the scale of the target and is more stable for the scale of small targets.
[0063] S6: Using the PCB surface defect sample data set to train the improved YOLOv5 network model, to obtain a trained improved YOLOv5 network model, i.e., a PCB surface defect detection model;
[0064] S7: Input the PCB image to be inspected into the PCB surface defect detection model after image preprocessing, and output the defect position in the PCB image and the corresponding defect category.
[0065] Applying the method provided in this implementation plan, the model is trained on the dataset publicly available from Peking University to obtain the optimal model for target detection; the optimal model is used to perform target detection, the target detection results are output, and the detection results are evaluated and compared with the original model.
[0066] In the industrial world, defective products are relatively rare in the production process of PCB boards, which increases the difficulty of acquiring PCB defect images for shooting and collection. In addition, in complex industrial production environments, defect targets are often small and difficult to detect, and lighting conditions are often uneven, which poses challenges to defect detection. In order to ensure the accuracy and reliability of the PCB defect dataset, this experimental data uses the public printed circuit board (PCB) defect dataset of the Peking University Intelligent Robotics Open Laboratory. It is a public synthetic PCB dataset containing 693 images with 6 types of defects (leaks, rat bites, open circuits, short circuits, strays, and miscellaneous copper). We divide the dataset into 80% training set and 20% validation set. Some of the dataset images are shown below. Figure 5 shown.
[0067] The improved model is analyzed, and the evaluation indicators used are mainly: precision (Precision, P), recall (Recall, R), AP and mAP. Specifically, the calculation formulas of precision (Precision, P) and recall (Recall, R) are as follows:
[0068]
[0069] In the formula, TP (True Positives) represents true positive examples, that is, the number of samples correctly predicted by the model as positive; FP (False Positives) represents false positive examples, that is, the number of samples where the model mistakenly predicts the negative class as positive; FN (False Negatives) represents false negative examples, that is, the number of samples where the model mistakenly predicts the positive class as negative.
[0070] The calculation formulas for AP and mAP are:
[0071]
[0072] In the formula, n is the total number of categories of targets to be detected, AP (i) is the AP value of the i-th category target. The higher the AP value, the better the detection performance of the model on this category. The higher the mAP value, the better the overall detection performance of the model on all categories.
[0073] The experimental results are shown in Table 1 below;
[0074] Table 1
[0075]
[0076] The precision (P) of the improved model in this application has been significantly improved from 87.9% to 97.1%, which shows that the improved model is more accurate in identifying target categories and reduces misjudgments and missed detections. The improvement in accuracy directly reflects the improved model's ability to extract target features and enhance classification accuracy. The recall rate (R) increased from 97% to 98%. This shows that the model is more comprehensive when detecting targets, can identify more real targets, and reduces missed detections, which is crucial to improving the integrity and reliability of target detection. In terms of the mAP0.5 indicator, the improved model increased from 94.0% to 97.3%, indicating that the average accuracy of the improved model in each category has been improved, further verifying the comprehensiveness and effectiveness of the model improvement. It also reflects that the stability and robustness of the improved model in dealing with different categories of targets and different difficulty scenarios have been enhanced.
[0077] The above method provided by the present invention can be applied to PCB quality control, component detection, electronic product repair and maintenance, etc. Specifically, the improved YOLOv5 model is applied on the production line to detect surface defects of PCB boards in real time, including open circuits, short circuits, strays, pseudo copper, etc. The automated detection equipment uses the scheme for online detection and sorting, reduces the cost of manual detection, and improves production efficiency and product quality. The improved YOLOv5 model is applied to the SMT (surface mount technology) process to detect the correctness and quality of mounted components, ensure the correct mounting and welding of components, realize automatic identification and positioning of components, detect problems such as offset and missing, and combine with the image detection system to achieve efficient and accurate detection. The YOLOv5 model quickly locates defects and fault points on the PCB board, improving maintenance efficiency and accuracy. Use a handheld detection device or a desktop detection system to scan and detect PCB boards in real time, generate detailed defect reports and images, and help technicians perform accurate repairs. These application fields demonstrate the important role of the detection scheme in electronic manufacturing and repair and maintenance, which can significantly improve detection efficiency and accuracy, and is suitable for electronic product production and maintenance environments of various sizes.
[0078] The detailed description of the present invention is written in a progressive manner, emphasizing the differences between the various implementations, and similar parts thereof can be referred to each other.
[0079] The embodiments of the present invention are described in detail above with reference to the accompanying drawings, but the present invention is not limited to the above embodiments, and various changes can be made within the knowledge scope of ordinary technicians in this field without departing from the purpose of the present invention.
Claims
1. PCB surface defect optimization detection method based on improved YOLOv5, characterized in that: The steps include: S1: Obtain public pre-processed PCB images and label data, and establish a YOLOv5 network structure model based on the PCB surface defect sample dataset; S2: Add a small target detection network structure layer to the anchors in the YOLOv5 network structure, and delete the large target detection layer. The small target detection network structure layer is suitable for detecting small components on PCB boards. S3: Integrate CBAM into the C3 module of YOLOv5s, so that the last two C3 modules of the backbone network of the YOLOv5 network are changed to C3CBAM; S4: Add the attention mechanism module SE to the backbone network of the YOLOv5 network; S5: Select NWD as the loss function; get the improved YOLOv5 network model; S6: Using the PCB surface defect sample data set to train the improved YOLOv5 network model, to obtain a trained improved YOLOv5 network model, i.e., a PCB surface defect detection model; S7: Input the PCB image to be inspected into the PCB surface defect detection model after image preprocessing, and output the defect position in the PCB image and the corresponding defect category.
2. The PCB surface defect optimization detection method based on improved YOLOv5 according to claim 1, characterized in that: In S2, the anchors in the YOLOv5 network structure add a small target detection network structure layer and delete the large target detection layer, including: S21: Modify the anchors to include three detection heads: anchors of 40x40 feature maps, anchors of 80x80 feature maps, and smaller anchors of 160x160 feature maps, which are used to detect small targets of 4x4 and above. S22: Modify Head and set the detection layer to [22, 25, 28]. In the 18th layer, upsample the 80x80 feature map in Neck to 160x160. In the 21st layer, concat the obtained 160x160 feature map with the 160x160 feature map of the second layer in Backbone to obtain a larger feature map for small target detection. Finally, add a target detection layer in the 29th layer.
3. The PCB surface defect optimization detection method based on improved YOLOv5 according to claim 1, characterized in that: The training process after CBAM is added to the network structure in S3: The feature map F is input into the channel attention module, and the corresponding attention map is output. Then the input feature map is multiplied by the attention map, and the output passes through the spatial attention block and performs the same operation. Finally, the output feature map is obtained as follows: Among them, Mc∈R1×1×C is a one-dimensional channel attention map, Ms∈RW×H×1 is a two-dimensional spatial attention map, Represents element-wise multiplication.
Citation Information
Patent Citations
GCN-yov5-based mask wearing detection method
CN117994831A
Ceramic tile surface defect detection method based on Yolov5
CN118071673A
PCB surface defect detection method based on YOLOv5
CN118212192A
PCB defect detection method for improving YOLOv5s
CN118334398A
Cited By
PCB defect detection method, apparatus and device, and storage medium
CN120431096A
PCB defect detection method and system based on dynamic frequency domain attention and adaptive sampling
CN121053078A
