A PCB defect detection method for improving YOLOv5
By improving the YOLOv5s model and introducing the SPDConv feature re-extraction module, context attention module, and multi-scale receptive field enhancement module, the problems of low detection accuracy and poor robustness in PCB defect detection are solved, and efficient and accurate detection of small targets is achieved.
Patent Information
- Application Number
- CN202311845947.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-12-28
- Publication Date
- 2026-11-24
- Estimated Expiration
- 2043-12-28
AI Technical Summary
Existing technologies for PCB defect detection suffer from low detection accuracy, poor robustness, and slow detection speed, especially in the detection of small targets and under background noise interference, where it is difficult to effectively distinguish between the target and the background.
The improved YOLOv5s model enhances the robustness of feature representation and multi-scale information by introducing the SPDConv feature re-extraction module, context attention module, and multi-scale receptive field enhancement module, combined with an optimized loss function.
It improves the accuracy and robustness of PCB defect detection, especially in the detection of small targets, reduces background noise interference, and improves detection speed and accuracy.
Smart Images

Figure CN118334398B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of defect detection, and more particularly to an improved PCB defect detection method using YOLOv5s. Background Technology
[0002] Printed circuit boards (PCBs) are fundamental components of electronic products. Surface defects on PCBs have a significant impact on the performance and lifespan of electronic products. Electrical measurement and testing methods are difficult to use for accurate defect detection, while traditional testing methods are mostly based on human visual inspection. Although the detection effect is good, the detection speed is unsatisfactory and cannot meet the requirements of rapid industrial testing.
[0003] Currently, defect detection methods can be broadly categorized into two types: machine vision-based and deep learning-based methods. Machine vision-based detection technology acquires image information through a camera and then uses algorithms to compare the image with a standard image, determining the location and type of defects based on manually set parameters. This method relies heavily on the subjective judgment of the inspector, leading to problems such as weak generalization ability, poor robustness, and low detection accuracy. With the continuous development of deep learning technology, deep learning-based target detection methods have been widely applied to defect detection. Through training with large amounts of data, they can automatically learn features, avoiding manual intervention and significantly improving detection accuracy and speed.
[0004] Deep learning-based defect detection methods face several challenges. First, while the backbone network extracts features from the input image, the continuous compression of the image size and increased network depth during this process leads to significant loss of rich detail. Second, the neck fusion network lacks contextual relationships, resulting in weak correlations between features during feature fusion and an inability to effectively enhance feature representations, which greatly impacts the detection accuracy of small targets. Third, PCB defects are often small in size, and the overall image contains substantial background and noise interference, making the target easily confused with the background and significantly hindering detection. Summary of the Invention
[0005] Purpose of the invention: The purpose of this invention is to provide an improved PCB defect detection method using YOLOv5s.
[0006] Technical solution: The present invention includes the following steps:
[0007] (1) Preprocess the defect surface image and expand the defect image dataset by data augmentation. Divide the dataset into three parts: training set, validation set and test set.
[0008] (2) Improve the model. Based on the YOLOv5s model, a PCB defect detection network is built, which includes a backbone network, a neck network and a head network. A feature re-extraction module based on SPDConv is constructed in the backbone network of the model. A context attention module CSAM is introduced at the junction of the backbone network and the neck network. A multi-scale receptive field enhancement module MREB is added before the detection head to build a new model network and optimize the loss function.
[0009] (3) The model is trained to obtain the optimal detection model by sequentially initializing parameters, setting hyperparameters, loading training set samples, and setting the number of iterations for the new network.
[0010] (4) Test the model on the test set to obtain the final detection results of the model.
[0011] Furthermore, the dataset augmentation process in step (1) includes adjusting image brightness, cutout, rotation, cropping, translation, and mirroring.
[0012] Furthermore, step (2) of constructing the feature re-extraction module based on SPDConv includes:
[0013] (2.1) The input feature map is sampled at intervals according to the scaling factor, and then the spatial information is converted into channel information to achieve the purpose of downsampling;
[0014] (2.2) The feature maps that have undergone depthwise separable convolution and ordinary convolution are concatenated to obtain the initial enhanced features, which are then fed into the bottleneck block;
[0015] (2.3) Max pooling is used to emphasize local information and is added element-wise to the output of the bottleneck block to obtain the re-extracted feature information. Finally, the initial enhanced features are used as supplementary information and fused with the re-extracted features in an element-wise addition manner.
[0016] Furthermore, the context attention module CSAM in step (2) includes a deformable convolutional attention module and a depthwise separable convolutional module.
[0017] Furthermore, the output of the deformable convolutional attention module is used as the input of the depthwise separable convolutional module, and the feature representation is enhanced using a 3×3 depthwise separable convolution:
[0018]
[0019]
[0020] Among them, X input It is the input to the context attention module, X output This is the output of the context attention module. It is based on the output of a deformable convolutional attention module and the input of a depthwise separable convolutional module. GN stands for group normalization.
[0021] Further, step (2) of the multi-scale receptive field enhancement module MREB includes 4 branches. The first branch uses ordinary 3×3 convolution to extract features. The input of the second branch is the feature of the element-wise addition of the output of the previous branch and the input of this branch. After the element-wise addition of the input of the previous branch and the third branch, the feature is extracted by a combination of asymmetric 3×1 convolution and dilated 3×3 convolution with a dilation rate of 3. After the input of the previous branch and the fourth branch are concatenated, the feature processing of the fourth branch is performed, which involves two asymmetric convolutions of 1×3 and 3×1, and then a dilated 3×3 convolution with a dilation rate of 5.
[0022] Furthermore, in step (2), the loss function is optimized by using NWD instead of IoU to evaluate the similarity of the bounding boxes.
[0023] Furthermore, the NWD modeling includes:
[0024] To model the bounding box using a Gaussian distribution, the probability density function of a two-dimensional Gaussian distribution is:
[0025]
[0026] The bounding box is modeled as a two-dimensional Gaussian distribution N(μ, ∑). Where x represents the coordinate, μ represents the mean, and ∑ represents the variance;
[0027] The Wasserstein Distance metric is used to measure the similarity of Gaussian distributions for two bounding boxes A = (cx a Cy a w a h a ), B = (cx b cy b w b h b Modeling is performed, and its Gaussian distribution is N. a and N b Then their Wasserstein Distance is:
[0028]
[0029] Since we need to replace IoU as the similarity measure, we perform a normalization operation on the Wasserstein Distance to obtain NWD.
[0030]
[0031] Where C is a constant related to the defect dataset.
[0032] Furthermore, in step (3), the model training uses the accuracy AP of each type of target in the dataset and the average accuracy mAP of all targets as the experimental metrics.
[0033] Furthermore, in step (4), the test set samples divided in the data processing stage are sent to the optimal network detection model obtained after training in the model training stage for testing. Finally, the test data are classified and regressed by the trained weight parameters to obtain the final detection result.
[0034] Beneficial effects: Compared with the prior art, the present invention has the following significant advantages:
[0035] (1) Replacing the convolution with stride 2 with SPDConv in the backbone network and neck network can achieve the same downsampling effect as before. Constructing a feature re-extraction module in the backbone network can retain more fine-grained information and strengthen feature representation, thereby improving the detection effect of small targets.
[0036] (2) In the deepest layer of the backbone network, a context attention module is added to capture the global long-term dependency of top features. At the same time, the deformable convolution in the module can fully fit the small target for feature extraction, which is beneficial to the detection of small targets.
[0037] (3) The introduction of a multi-scale receptive field enhancement module effectively solves the problem of severe loss of channel information, while strengthening the dependence between feature information at different scales and ranges, and complementing information to improve detection accuracy. Attached Figure Description
[0038] Figure 1 This is a flowchart of the present invention;
[0039] Figure 2 This is a diagram showing the overall structure of the network model of the present invention;
[0040] Figure 3 Here is a structural diagram of the feature re-extraction module;
[0041] Figure 4 Here is a diagram of the SPDConv structure;
[0042] Figure 5 A comparison diagram of the context attention module structure and the deformable convolutional attention module structure;
[0043] Figure 6 This is a structural diagram of the multi-scale receptive field enhancement module. Detailed Implementation
[0044] The technical solution of the present invention will be further described below with reference to the accompanying drawings.
[0045] like Figure 1 As shown, the present invention includes the following steps:
[0046] (1) Preprocess the defect surface image and expand the defect image dataset by data augmentation. Divide the dataset into three parts: training set, validation set and test set.
[0047] (2) Improve the model. Based on the YOLOv5s model, a PCB defect detection network is built, which includes a backbone network, a neck network and a head network. A feature re-extraction module based on SPDConv is constructed in the backbone network of the model. A context attention module CSAM is introduced at the junction of the backbone network and the neck network. A multi-scale receptive field enhancement module MREB is added before the detection head to build a new model network and optimize the loss function.
[0048] (3) The model is trained to obtain the optimal detection model by sequentially initializing parameters, setting hyperparameters, loading training set samples, and setting the number of iterations for the new network.
[0049] (4) Test the model on the test set to obtain the final detection results of the model.
[0050] Because the number of defective images in the acquired dataset is relatively small, which is not conducive to deep learning models acquiring sufficient information, methods such as adjusting image brightness, cutout, rotation, cropping, translation, and mirroring are chosen to expand the dataset. Figure 2 As shown.
[0051] While striding convolutional and pooling layers in the backbone network extracts features, this often leads to the loss of fine-grained information during feature extraction, resulting in feature representations with low learning rates. To improve model performance, a feature re-extraction module based on SPDConv is constructed. For example... Figure 3 As shown, for the input feature map, sampling is performed at intervals according to a scaling factor, and then spatial information is converted into channel information to achieve downsampling. The feature maps processed by depthwise separable convolution and ordinary convolution are then concatenated to obtain initial enhanced features, which are fed into the bottleneck block to promote more efficient information transfer. To further extract more abstract features, max pooling is used to emphasize local information, and it is added element-wise with the bottleneck block output to obtain re-extracted feature information. Finally, the initial enhanced features are used as supplementary information and fused with the re-extracted features in an element-wise addition manner.
[0052] After the input feature map is processed by the backbone network, it gains richer, finer-grained information. However, it is limited by contextual information, resulting in less effective information for detection and a larger proportion of background, which is not conducive to detection. Therefore, to capture the dependencies of global information and achieve a differential representation of foreground and background, a contextual attention module is introduced. This module consists of two parts: a deformable convolutional attention module and a depthwise separable convolutional module. In the deformable convolutional attention module, the input feature map is first normalized and then enters the deformable convolutional attention module (DCA). The feature map is initially processed by a 1×1 convolution and the GELU activation function. Two consecutive 3×3 and 5×5 deformable convolutions are used to distort the sampling network, making the convolutional kernel fit the small target as closely as possible for feature extraction, enabling the attention to operate flexibly within the receptive field. Then, residual connections are added for element-wise multiplication. Next, the number of channels is adjusted and element-wise added to the feature map of the input module. Finally, DropPath regularization is added to enhance the generalization and robustness of the features.
[0053] The output of the deformable convolutional attention module is used as the input of the depthwise separable convolution module. A 3×3 depthwise separable convolution is employed to enhance feature representation, and compared to traditional convolution, it reduces computational cost. Residual connections are added to this module and also introduced throughout the entire contextual attention module to enrich the relationships between pixel information, such as... Figure 5 The image shows a comparison between the context attention module structure and the deformable convolutional attention module structure.
[0054]
[0055]
[0056] Among them, X input It is the input to the context attention module, X output This is the output of the context attention module. It is based on the output of a deformable convolutional attention module and the input of a depthwise separable convolutional module. GN stands for group normalization.
[0057] Increasing network depth can lead to the loss of defect targets, feature confusion, and weak semantic correlation between different scales, which is detrimental to defect target detection. To enhance the robustness between multi-scale information and expand the receptive field to capture broader feature information from different receptive fields, a multi-scale receptive field enhancement module is introduced, such as... Figure 6As shown, this module consists of four branches. First, the input feature map is sliced along the channel dimension. The first branch uses a standard 3×3 convolution to extract features. The second branch's input is the element-wise sum of the output of the previous branch and the input of this branch. It then uses a combination of 1×3 asymmetric convolution and 3×3 dilated convolution with a dilation rate of 3 to extract features from the input image of this branch. The first branch's input is then element-wise summed with the input of the third branch, and features are extracted using a combination of 3×1 asymmetric convolution and 3×3 dilated convolution with a dilation rate of 3. The first branch's input is concatenated with the input of the fourth branch, and then the fourth branch's features are processed using two 1×3 and 3×1 asymmetric convolutions to reduce computation and the risk of overfitting, followed by a 3×3 dilated convolution with a dilation rate of 5 to effectively expand the receptive field and extract sufficient feature information. The outputs of the four branches are then concatenated and added to a bottleneck layer. This is then element-wise summed with the output feature maps of multiple branches to supplement feature pixel information, emphasizing features and weakening background areas.
[0058] Furthermore, the loss function is improved to further optimize the overall model and achieve optimal detection performance. Previously, IoU was commonly used to measure the similarity between bounding boxes, but this is limited to the overlap of the bounding boxes. If there is no overlapping region between the bounding boxes, IoU is highly sensitive to positional deviations, which severely affects the target detection performance. Therefore, Normalized Wasserstein Distance (NWD) is used instead of IoU to evaluate the similarity of bounding boxes to improve detection performance.
[0059] NWD first models the bounding box using a Gaussian distribution. Within the bounding box, the background often occupies a large portion. To better distinguish the proportions of the target and background within the bounding box, a two-dimensional Gaussian distribution is chosen for modeling. At the center of the bounding box, i.e., the foreground pixels, the proportion is highest, and the proportion of pixels within the bounding box decreases from the center outwards. For the bounding box, where represents the center coordinates, represents the width of the bounding box, and represents the height of the bounding box, the probability density function using a two-dimensional Gaussian distribution is:
[0060]
[0061] The bounding box is modeled as a two-dimensional Gaussian distribution N(μ, ∑). Where x represents the coordinate, μ represents the mean, and ∑ represents the variance;
[0062] The Wasserstein Distance metric is used to measure the similarity of Gaussian distributions for two bounding boxes A = (cx a cy a w a h a ), B = (cxb cy b w b h b Modeling is performed, and its Gaussian distribution is N. a and N b Then their Wasserstein Distance is:
[0063]
[0064] Since we need to replace IoU as the similarity measure, we perform a normalization operation on the Wasserstein Distance to obtain NWD.
[0065]
[0066] Where C is a constant related to the defect dataset.
[0067] Example
[0068] This invention is an improved PCB defect detection method using YOLOv5s, comprising:
[0069] The first step was data preprocessing. A PCB surface defect dataset was acquired and analyzed. This dataset came from the Intelligent Robotics Open Laboratory of Peking University and contained six types of defects: missing holes, mouse bites, open circuits, short circuits, spurs, and spurious copper, totaling 693 images. Due to the small sample size, the dataset was expanded by adjusting brightness, cutout, random rotation, cropping, and mirroring, resulting in 4158 images. These images were then divided into training, validation, and test sets in an 8:1:1 ratio. Before training, the image size was adjusted to 800×800 pixels.
[0070] The second step is model training. A PCB defect detection network is built based on the YOLOv5s model, including a backbone network, a neck network, and a head network. There are four main improvements: First, a feature re-extraction module is built in the backbone network, and SPDConv is added in the neck network; second, a context attention module is added at the junction of the backbone network and the neck network; third, a multi-scale receptive field enhancement module is added in the neck network; and fourth, the loss function is optimized.
[0071] In the backbone and neck network, SPDConv is used instead of convolution with stride of 2 for feature extraction. By sampling at intervals and concatenating feature maps according to the number of channels, the feature information is transformed from the spatial dimension to the channel dimension.
[0072] This invention proposes a feature re-extraction module. First, SPDConv is used to retain sufficient shallow feature information. Then, after passing through ordinary convolution and DWConv respectively, a feature map with half the number of channels is obtained. These two maps are then concatenated to obtain the initial enhanced features, which are fed into the bottleneck block to promote information flow and ensure effective transmission, resulting in a feature map with the same number of channels as the re-feature extraction module. To extract more abstract features, max pooling is used to emphasize local information, and element-wise summation is performed with the initial enhanced features to achieve dual extraction of the input image. Finally, the initial enhanced features, in the form of supplementary information, are fused with the re-extracted features through element-wise addition.
[0073] This invention proposes a contextual attention module, divided into a deformable convolutional attention module and a depthwise separable convolutional attention module. First, the input feature map enters the deformable convolutional attention module. The feature map obtained through the backbone network undergoes group normalization and then enters the deformable convolutional attention module (DCA). It undergoes channel transformation processing via 1×1 convolution and the GELU activation function. Then, two consecutive 3×3 and 5×5 deformable convolutions are used to distort the convolution kernel, conforming it to the target shape and extracting more precise features. Element-wise multiplication is then added to refine pixel information. Finally, a 1×1 convolution is used to convert the number of channels for element-wise addition with the input features. After the DCA module outputs, DropPath regularization is applied to make the feature representation more generalizable and robust.
[0074] The output of the deformable convolutional attention module is used as the input of the depthwise separable convolution module, and undergoes group normalization. Then, a 3×3 depthwise separable convolution is used to refine the features, reducing the computational cost of the model. The output features, processed by the GELU activation function, are then element-wise added to the input of this module. Simultaneously, residual connections are added to the overall contextual attention module to enrich the connections between pixel information, supplement edge information, and enhance the semantic representation capability of the features.
[0075] To enhance the robustness of multi-scale information and expand the receptive field, capturing a wider range of feature information from different receptive fields, this invention proposes a multi-scale receptive field enhancement module. First, the input feature map is sliced along the channel dimension. The four sub-maps with the same channel dimension are then fed into four branches. The first branch uses a 3×3 convolution to extract features. Simultaneously, the output of this branch is added element-wise to the input of the second branch, followed by a 1×3 asymmetric convolution to extract features, and then a 3×3 convolution with a dilation rate of 3. The input of the third branch is added element-wise to the output of the previous branch, followed by a 3×1 asymmetric convolution to extract features, and then a 3×3 convolution with a dilation rate of 3, effectively expanding the receptive field. The input of the fourth branch is concatenated with the output of the third branch, and then two consecutive 1×3 and 3×1 asymmetric convolutions are used to extract features, followed by a 3×3 convolution with a dilation rate of 5. The outputs of the four branches are concatenated along the channel dimension, and then transformed into the number of channels through a 1×1 convolution. The input of the entire multi-scale receptive field enhancement module is also transformed into the number of channels through a 1×1 convolution. Then, the two are added element by element, and finally the output of the entire module is produced through the ReLU activation function.
[0076] In the loss function, Normalized Wasserstein Distance (NWD) is chosen to replace IoU to evaluate the similarity of bounding boxes, further improving detection performance.
[0077] Thirdly, when training the model in this invention, the experimental environment was Python 3.8, PyTorch 1.7.0, torchvision 0.7.0, with a batch size of 300 and a learning rate of 0.01. The hardware device was... Core TM The system uses an i9-10900X CPU and an NVIDIA RTX 3080 Ti graphics card. The accuracy (AP) for each target class and the average accuracy (mAP) for all targets in the dataset are used as the experimental metrics.
[0078] The fourth step involves feeding the test set samples, which were divided during the data processing stage, into the optimal network detection model obtained after training during the model training stage for testing. Finally, the trained weight parameters are used to classify the test data and perform position regression to obtain the final detection results.
Claims
1. An improved PCB defect detection method for YOLOv5, characterized in that, Includes the following steps: (1) Preprocess the defect surface image and expand the defect image dataset by data augmentation. Divide the dataset into three parts: training set, validation set and test set. (2) Improve the model. Based on the YOLOv5s model, build a PCB defect detection network, including a backbone network, a neck network and a head network. Construct a feature re-extraction module based on SPDConv in the backbone network of the model. Introduce the context attention module CSAM at the junction of the backbone network and the neck network. Add a multi-scale receptive field enhancement module MREB before the detection head to build a new model network and optimize the loss function. (3) The model is trained to obtain the optimal detection model by sequentially initializing parameters, setting hyperparameters, loading training set samples, and setting the number of iterations for the new network. (4) Test the model on the test set to obtain the final detection results of the model; Step (2) of constructing the feature re-extraction module based on SPDConv includes: (2.1) The input feature map is sampled at intervals according to the scaling factor, and then the spatial information is converted into channel information to achieve the purpose of downsampling; (2.2) The feature maps that have undergone depthwise separable convolution and ordinary convolution are concatenated to obtain the initial enhanced features, which are then fed into the bottleneck block; (2.3) Max pooling is used to emphasize local information and is added element-wise to the output of the bottleneck block to obtain the re-extracted feature information. Finally, the initial enhanced features are used as supplementary information and fused with the re-extracted features in an element-wise addition manner. The context attention module (CSAM) in step (2) includes a deformable convolutional attention module and a depthwise separable convolutional module. The output of the deformable convolutional attention module is used as the input of the depthwise separable convolutional module, and the feature representation is enhanced using a 3×3 depthwise separable convolution: in, It is the input to the context attention module. This is the output of the context attention module. It is based on the output of a deformable convolutional attention module and the input of a depthwise separable convolutional module. GN represents group normalization. The step (2) Multi-scale receptive field enhancement module MREB includes 4 branches. The first branch uses ordinary 3×3 convolution to extract features. The input of the second branch is the element-wise sum of the output of the previous branch and the input of this branch. After the element-wise sum of the input of the previous branch and the third branch, features are extracted by a combination of asymmetric 3×1 convolution and dilated 3×3 convolution with a dilation rate of 3. After the input of the previous branch and the fourth branch are concatenated, the features of the fourth branch are processed by two asymmetric convolutions of 1×3 and 3×1, and then by dilated 3×3 convolution with a dilation rate of 5.
2. The improved YOLOv5s PCB defect detection method according to claim 1, characterized in that, The data set augmentation process in step (1) includes adjusting image brightness, cutout, rotation, cropping, translation, and mirroring.
3. The improved YOLOv5s PCB defect detection method according to claim 1, characterized in that, In step (2), the loss function is optimized by using NWD instead of IoU to evaluate the similarity of bounding boxes.
4. The improved YOLOv5s PCB defect detection method according to claim 3, characterized in that, The NWD includes: To model the bounding box using a Gaussian distribution, the probability density function of a two-dimensional Gaussian distribution is: Model the bounding box as a two-dimensional Gaussian distribution. , , Where x represents the coordinate, This represents the mean. Indicates variance; The similarity of Gaussian distributions is measured using the Wasserstein Distance metric for two bounding boxes. , Modeling is performed, and its Gaussian distribution is as follows: and Their Wasserstein distance is: Since we need to replace IoU as the similarity measure, we perform a normalization operation on the Wasserstein Distance to obtain NWD. Where C is a constant related to the defect dataset.
5. The improved YOLOv5s PCB defect detection method according to claim 1, characterized in that, The model training in step (3) uses the accuracy AP of each type of target in the dataset and the average accuracy mAP of all targets as the experimental metrics.
6. The improved YOLOv5s PCB defect detection method according to claim 1, characterized in that, In step (4), the test set samples divided in the data processing stage are sent to the optimal network detection model obtained after training in the model training stage for testing. Finally, the test data are classified and regressed by the trained weight parameters to obtain the final detection result.
