PCB surface defect detection method based on improved YOLOv8n model
By improving the YOLOv8n model, introducing the LSK-Attention mechanism and the GELAN high-efficiency layer aggregation network, and replacing the loss function, the problems of low detection accuracy and slow speed in PCB surface defect detection are solved, and an efficient and low-complexity detection effect is achieved. .
Patent Information
- Application Number
- CN202410846371.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-27
- Publication Date
- 2024-10-01
AI Technical Summary
The existing technology has problems such as low detection accuracy, slow speed, and high consumption of computing resources in PCB surface defect detection, making it difficult to meet the performance requirements of industrial real-time detection.
Improve the YOLOv8n model, introduce the LSK-Attention attention mechanism, GELAN efficient layer aggregation network and WIoU loss function, and optimize the backbone network and loss function of the model to improve detection accuracy and efficiency.
The accuracy and efficiency of PCB surface defect detection are significantly improved, and the amount of parameters and calculation complexity are reduced, making it suitable for industrial real-time detection.
Smart Images

Figure CN118735883A_ABST
Abstract
Description
Technical Field
[0001] The invention relates to a PCB surface defect detection method based on an improved YOLOv8n model. Background Art
[0002] At present, with the rapid development of science and technology, people's demand for smart electronic products is also growing. Printed circuit boards (PCBs) have become indispensable as the basic components of electronic products. PCBs are widely used in many fields, such as computers, intelligent robots, and aerospace. However, during the production process, PCBs may be affected by many factors such as manufacturing equipment, improper handling by operators, processing technology, and production environment, which may cause some defects on the surface of PCBs, resulting in quality problems in electronic products. Poor quality electronic products may not only cause significant economic losses, but also endanger personal safety and cause major accidents. Therefore, it is of great significance to detect PCB defects.
[0003] Initially, most companies produced PCBs whose surface defects were mainly detected by manual visual inspection or electrical inspection. Although manual visual inspection can be relatively accurate, the process is too monotonous and repetitive, which can easily cause fatigue. When the PCB production speed is very high, the staff cannot perform accurate inspection. Due to the high cost and low efficiency of manual inspection, it is not applicable in actual industrial environments. The electrical inspection method has limitations and can only detect certain specific defects. In addition, when professional instruments come into contact with the PCB, there is a risk of scratching the PCB.
[0004] Later, with the continuous development of machine vision, technology based on machine vision gradually became an effective method to solve the problem of PCB surface defect detection. Early defect detection methods mainly extracted features manually and then classified defects. However, these traditional methods generally have problems such as slow detection speed, manual feature extraction, and low accuracy, making it difficult to improve detection efficiency in the actual complex PCB production industrial environment.
[0005] With the rapid development of artificial intelligence and deep learning, computer vision technology is increasingly used in many fields such as image processing, human-computer interaction, and target detection. Deep learning has occupied an important position in the field of computer vision with its excellent feature learning ability. At present, target detection algorithms based on deep learning are mainly divided into two categories: one is a single-stage target detection algorithm represented by the YOLO series of algorithms. The single-stage target detection algorithm can directly predict the category and position of the target through the model of the input image. The other is a two-stage target detection algorithm represented by the R-CNN series of algorithms. The two-stage target detection algorithm extracts features from the input image, generates candidate regions, and continuously adjusts the candidate region information for classification and positioning recognition.
[0006] Although the target detection algorithm based on deep learning has made some optimization and improvement to PCB defect detection, there are still some shortcomings. For example, the model is large in size, the detection accuracy is low, the detection speed is too slow, and it is easy to consume more computing resources, which cannot meet the performance requirements of industrial real-time detection. Summary of the invention
[0007] The technical problem to be solved by the present invention is to overcome the defects of the prior art and provide a PCB surface defect detection method based on an improved YOLOv8n model, which improves the detection accuracy and significantly improves the detection efficiency.
[0008] In order to solve the above technical problems, the technical solution of the present invention is: a PCB surface defect detection method based on an improved YOLOv8n model, the steps of the method include:
[0009] S01: Obtain PCB surface defect dataset;
[0010] S02: preprocessing the acquired PCB surface defect data set;
[0011] S03: Improve the YOLOv8n model to obtain an improved YOLOv8n model;
[0012] S04: Use the preprocessed PCB surface defect dataset to train the improved YOLOv8n model, and evaluate the performance of the trained improved YOLOv8n model through evaluation indicators;
[0013] Improvements to the YOLOv8n model include:
[0014] Introduce the LSK-Attention mechanism in the last layer of the backbone network of the YOLOv8n model;
[0015] Introduce the efficient layer aggregation network GELAN into the YOLOv8n model, that is, replace the C2f module in the YOLOv8n model with the RepNCSPELAN4 module, and replace the SPPF module with the SPPELAN module;
[0016] Replace the CIoU loss function in the YOLOv8n model with the WIoU loss function.
[0017] Further, in step S01, the PCB surface defects include six types, namely: leak holes, mouse bites, open circuits, short circuits, burrs and excess copper.
[0018] Further, in step S02, preprocessing includes: converting the label file into a txt format and dividing the training set and the validation set according to a ratio.
[0019] Furthermore, in step S02, the preprocessing further includes: performing data enhancement processing on the PCB surface defect dataset before training.
[0020] Furthermore, data enhancement processing includes cropping, rotation, HSV data enhancement, Mosaic data enhancement and MixUp data enhancement.
[0021] Furthermore, the WIoU loss function includes three modes: Wise-IoU v1, Wise-IoU v2 and Wise-IoU v3; wherein,
[0022] The WIoU v1 loss function is defined as follows:
[0023] L WIoUv1 =R WIoU L IoU ;
[0024]
[0025] Among them, the anchor box is The target frame is W i H i is the intersection of the target box and the anchor box, W g and H g Represent the width and height of the minimum bounding box, S u is the union of the target box and the anchor box, and IoU is the intersection over union ratio; R WIoU is the penalty term, R WIoU ∈[1,e), which enlarges the L of the normal quality anchor box IoU , while L IoU ∈[0,1], which can reduce the R of high-quality anchor boxes WIoU , in order to prevent R WIoU Produce gradients that hinder convergence, W g ,H g Separation from the computational graph;
[0026] The Wise-IoU v2 loss function is defined as follows:
[0027]
[0028] is the sliding average, which is used as a normalization factor. The normalization factor is dynamically updated to make the gradient gain The overall level is kept high. γ is an index used to control the focusing effect of bounding box regression. The quality of the anchor box can be measured by the outlier degree, which is defined as: The Wise-IoU v3 loss function is defined as follows:
[0029]
[0030] Among them, α and δ are two hyperparameters used to control the gradient gain r. Setting appropriate α and δ values can enable the model to better adjust the gradient at different stages of training, thereby improving the performance of the model.
[0031] Further, in step S04, the evaluation indicators are precision, recall, average precision of each category, parameter quantity and billion floating point operations per second.
[0032] After adopting the above technical solution, the present invention has the following beneficial effects:
[0033] 1. The present invention introduces a dynamic large convolution kernel attention mechanism LSK-Attention module (i.e., LSK-Attention mechanism) into the backbone network of the improved YOLOv8n model to expand the receptive field of the model and improve the model's feature extraction capability for PCB surface defects, thereby improving the model's detection accuracy.
[0034] 2. The present invention introduces the general efficient layer aggregation network GELAN, replaces the C2f module of the original YOLOv8n model with the RepNCSPELAN4 module, and replaces the SPPF module with the SPPELAN module. By effectively aggregating features of different layers, the feature learning ability of the model is enhanced, which helps to capture information of different scales, thereby improving the accuracy of target detection.
[0035] 3. The present invention replaces the CIoU loss function in the original YOLOv8n model with the WIoU loss function. Through its unique dynamic non-monotonic focusing mechanism, it effectively balances the model's learning of anchor frames of different qualities, reduces the negative impact of low-quality samples, and thus improves the overall performance of the model. BRIEF DESCRIPTION OF THE DRAWINGS
[0036] Figure 1 The network structure diagram of the improved YOLOv8n model of the present invention;
[0037] Figure 2 This is the structure diagram of the LSK-Attention module of the present invention;
[0038] Figure 3 It is a GELAN network structure diagram of the present invention;
[0039] Figure 4 Comparison chart of the improved YOLOv8n model of the present invention and other common models in terms of accuracy, recall, average detection precision mAP_0.5 and mAP_0.5:0.95;
[0040] Figure 5(a) is a PR curve of each category of defects of the original YOLOv8n model on the PCB dataset;
[0041] Figure 5(b) shows the PR curve of each category of defects of the improved YOLOv8n model on the PCB dataset. DETAILED DESCRIPTION
[0042] In order to make the contents of the present invention more clearly understood, the present invention is further described in detail below based on specific embodiments in conjunction with the accompanying drawings.
[0043] like Figure 1 As shown in Figure 5, a PCB surface defect detection method based on an improved YOLOv8n model includes the following steps:
[0044] S01: Obtain PCB surface defect dataset;
[0045] S02: preprocessing the acquired PCB surface defect data set;
[0046] S03: Improve the YOLOv8n model to obtain an improved YOLOv8n model;
[0047] S04: Use the preprocessed PCB surface defect dataset to train the improved YOLOv8n model, and evaluate the performance of the trained improved YOLOv8n model through evaluation indicators;
[0048] Improvements to the YOLOv8n model include:
[0049] Introduce the LSK-Attention mechanism in the last layer of the backbone network of the YOLOv8n model;
[0050] Introduce the efficient layer aggregation network GELAN into the YOLOv8n model, that is, replace the C2f module in the YOLOv8n model with the RepNCSPELAN4 module, and replace the SPPF module with the SPPELAN module;
[0051] Replace the CIoU loss function in the YOLOv8n model with the WIoU loss function.
[0052] Further, in step S01, the PCB surface defect dataset comes from the PCB defect dataset published by the Intelligent Robotics Open Laboratory of Peking University. There are six types of PCB surface defects: leaking holes, rat bites, open circuits, short circuits, burrs, and excess copper.
[0053] Further, in step S02, preprocessing includes: since the source file label is in xml format, using the improved YOLOv8n model training requires converting the label file into txt format and dividing the training set and the validation set according to the ratio; the ratio of the training set to the validation set can be 8:2.
[0054] Furthermore, in step S02, the preprocessing further includes: performing data enhancement processing on the PCB surface defect dataset before training.
[0055] Furthermore, data enhancement processing includes cropping, rotation, HSV data enhancement, Mosaic data enhancement and MixUp data enhancement.
[0056] Specifically, in step S03, the specific model structure of the improved YOLOv8n model is as follows: Figure 1 As shown. The YOLOv8n model includes an input, a backbone network, a neck network, and an output. A dynamic large convolution kernel attention mechanism LSK-Attention module (i.e., LSK-Attention mechanism) is introduced into the backbone network of the original YOLOv8n model to expand the receptive field of the model. This improvement helps to improve the model's ability to extract features of PCB surface defects, thereby improving the model's detection accuracy. The general efficient layer aggregation network GELAN is introduced, such as Figure 1 As shown in the figure, the C2f module in the original YOLOv8n model is replaced with the RepNCSPELAN4 module, and the SPPF module is replaced with the SPPELAN module. The feature learning ability of the model is enhanced by effectively aggregating features of different layers, which helps to capture information of different scales and thus improve the accuracy of target detection. The loss function of the original YOLOv8n model is improved, and the CIoU loss function is replaced with the WIoU loss function. Through its unique dynamic non-monotonic focusing mechanism, the model's learning of anchor boxes of different qualities is effectively balanced, reducing the negative impact of low-quality samples, thereby improving the overall performance of the model.
[0057] Improvement 1 of the improved YOLOv8n model: Introduce the LSK-Attention mechanism in the last layer of the backbone network of the YOLOv8n model; Among them, there are defects of various shapes in the PCB surface defect dataset, which usually only occupy a small part of the image and are similar to background features, making them difficult to accurately identify and easily leading to false detection and missed detection. Therefore, this embodiment introduces a dynamic large convolution kernel attention mechanism LSK-Attention module (i.e., LSK-Attention mechanism) at the end of the backbone network of the YOLOv8n model. The key advantage of the LSK-Attention module is that it uses multiple large convolution kernels to generate features with a wide receptive field. In addition, the LSK-Attention module dynamically selects appropriate convolution kernels by considering the local information of the input feature map to adapt to the contextual information of various target types. It also dynamically adjusts its receptive field, which is an adaptive weight allocation method by dynamically selecting the most relevant feature maps from large convolution kernels and combining them spatially. This allows for higher attention to the most relevant spatial regions of the detected targets, thereby improving the accuracy of target detection. The overall module structure of the LSK-Attention module is as follows Figure 2 shown.
[0058] like Figure 2 As shown in the figure, the specific operation of the LSK-Attention mechanism is as follows:
[0059] In order to dynamically select an appropriate spatial kernel, the input feature X is divided into multiple sub-feature maps; next, convolution kernels of different sizes are applied to each sub-feature map to generate multiple output feature maps; subsequently, these sub-output feature maps are concatenated together, which can be expressed as:
[0060]
[0061] Subsequently, the concatenated feature maps are average pooled and max pooled in the channel dimension, namely, SA avg and SA max , to extract the spatial relationship descriptor, which can be expressed as:
[0062]
[0063] Subsequently, in the SA avg and SA max After concatenation, the convolutional layer Convert 2-channel spatial pooling features into spatial attention maps to ensure that they have the same number of depthwise convolutions N. The mathematical formula for this process is:
[0064]
[0065] By applying the sigmoid activation function to each spatial attention map, the spatial selection weight of each depthwise convolution can be obtained The weighted depth convolution feature map is obtained by performing element-wise multiplication by weight and corresponding depth convolution. Finally, the convolution layer is used To fuse these feature maps and produce the final attention feature S. The process is as follows:
[0066]
[0067] The final output Y of the LSK-Attention module is the element-wise product between the input features X and S, which can be expressed as:
[0068] Y = X·S;
[0069] Improvement 2 of the YOLOv8n model: Introduce the general efficient layer aggregation network GELAN, replace the C2f module of the original YOLOv8n with the RepNCSPELAN4 module, and replace the SPPF module with the SPPELAN module. The structures of the RepNCSPELAN4 and SPPELAN modules are as follows: Figure 1 As shown in , both modules come from the efficient layer aggregation network.
[0070] When the input image is extracted and transformed layer by layer by the YOLOv8 network, some information will be lost, and the features of small target defects cannot be fully obtained, thus affecting the detection accuracy. The architecture of GELAN enhances the effective aggregation of features of different scales. Through the improved feature fusion method, GELAN can capture richer target information, thereby improving the detection accuracy. Its network structure is as follows: Figure 3 shown.
[0071] GELAN borrows the idea of SCPNet and expands on the YOLOv7ELAN network architecture, generalizing the functionality of ELAN, which originally only used convolutional layer stacks, to a new architecture that can use any computing blocks.
[0072] Improvement 3 of the YOLOv8n model: Replace the CIoU loss function in the original YOLOv8n model with the WIoU loss function;
[0073] This embodiment uses the Wise-IoU loss function to replace the CIoU function in the original model, which includes three modes: Wise-IoU v1, Wise-IoU v2 and Wise-IoU v3. Through experiments, this embodiment finds that the effect of WIoU v1 on the PCB dataset is poor, while WIoU v2 and WIoU v3 have better effects.
[0074] The WIoU v1 loss function is defined as follows:
[0075] L WIoUv1 =R WIoU L IoU ;
[0076]
[0077] Among them, the anchor box is The target frame is W i H i is the intersection of the target box and the anchor box, W g and H g Represent the width and height of the minimum bounding box, S u is the union of the target box and the anchor box, and IoU is the intersection over union ratio. WIoU is the penalty term, R WIoU ∈[1,e), which can enlarge the L of the normal quality anchor box IoU , while L IoU ∈[0,1], which can reduce the R of high-quality anchor boxes WIoU , in order to prevent R WIoU Produce gradients that hinder convergence, W g ,H g Separate from the computation graph (* indicates this operation). L WIoUv2 By L WIoUv1 The improved calculation method is as follows:
[0078]
[0079] in, YesL WIoU v1 The monotonic focusing coefficient can enable the model to focus on difficult samples, thereby improving the detection performance. During the model training process, the gradient gain With L IoU The decrease of L leads to a slower convergence speed in the later stage of training. IoU The mean of is used as the normalization factor:
[0080]
[0081] in is a sliding average, dynamically updating the normalization factor to make the gradient gain The overall level is maintained at a high level, solving the problem of slow convergence in the late stage of training. The quality of the anchor box can be measured by the outlier degree, which is defined as:
[0082] A small outlier means that the anchor box has high quality, and a small gradient gain can be assigned to it so that the bounding box regression focuses on the anchor box of normal quality. Assigning a smaller gradient gain to an anchor box with a large outlier will effectively prevent low-quality samples from generating large harmful gradients. We can construct a non-monotonic focusing coefficient and apply it to WIoU v1:
[0083]
[0084] Among them, when β = δ, r = 1. It is dynamic, and the quality division criteria of the anchor boxes are also dynamic, which enables WIoU v3 to make the gradient gain allocation strategy that best suits the current situation at every moment.
[0085] Specifically, in step S04:
[0086] The preprocessed PCB dataset is used to train the improved YOLOv8n model, including adjusting the size of the input image to 640×640, setting the batch size to 16, the number of epochs to 600, selecting AdamW as the optimizer, and setting the learning rate to 0.001.
[0087] Specifically, in step S04, the evaluation indicators are precision (precision, P), recall (recall, R), mean average precision (mAP), parameters, and billion floating point operations per second (GFLOPS), which are used to measure the detection performance of the model. Among them, mAP is calculated by precision (precision, P) and recall (recall, R). Precision represents the proportion of correctly detected targets, and recall represents the proportion of actually detected targets in the data set. The calculation formula is as follows:
[0088]
[0089] Among them, TP is the number of correctly predicted positive samples, FN is the number of incorrectly predicted negative samples, and FP is the number of incorrectly predicted positive samples.
[0090] The hardware platform of the simulation experiment in this embodiment is: a 12-core Intel(R) Xeon(R) Platinum8255C CPU and an NVIDIA GeForce RTX 2080Ti GPU with 11GB memory; the Ubuntu18.04 operating system is used, and the configured virtual environment includes Python3.8, Pytorch1.8.1, cuda11.1, etc.
[0091] Table 1 Comparison of detection effects of six networks under PCB dataset
[0092]
[0093] Experiments show that the method proposed in this embodiment is more powerful than the current advanced methods and can more accurately detect the types and locations of PCB surface defects. The comparison results of different methods on PCB data sets are as follows: Figure 4 As shown in Table 1, the detection accuracy of the original YOLOv8n model used in this embodiment on the PCB surface defect dataset is higher than that of YOLOv3-tiny, YOLOv6s and YOLOv7-tiny. It has the same detection accuracy as YOLOv5s but the number of parameters and computational complexity are reduced by 57% and 66% respectively. On the PCB dataset, the mAP of the improved YOLOv8n model reached 91.2%, which is 3% higher than that of the original YOLOv8n model. In addition, compared with the other four methods, the improved YOLOv8n model not only has high detection accuracy but also has fewer parameters and lower computational complexity, enabling it to be quickly deployed in industrial inspection equipment.
[0094] It can be clearly seen from Figure 5(a) and Figure 5(b) that compared with the original model, the improved YOLOv8n model shows higher accuracy in all defects except short circuit defects. The detection accuracy of the improved YOLOv8n model in the categories of mouse bite, open circuit, burr, and excess copper defects is improved by 6.5%, 5%, 4%, and 4.9% respectively compared with the original model. In summary, this embodiment introduces a dynamic large convolution kernel attention mechanism LSK-Attention module in the backbone network of the improved YOLOv8n model to expand the receptive field of the model, improve the model's feature extraction ability for PCB surface defects, and thus improve the model's detection accuracy. The general efficient layer aggregation network GELAN is introduced, the C2f module of the original YOLOv8n is replaced by the RepNCSPELAN4 module, and the SPPF module is replaced by the SPPELAN module. The feature learning ability of the model is enhanced by effectively aggregating features of different layers, which helps to capture information of different scales, thereby improving the accuracy of target detection. The CIoU loss function in the original YOLOv8n model is replaced by the WIoU loss function. Through its unique dynamic non-monotonic focusing mechanism, the model's learning of anchor boxes of different qualities is effectively balanced, reducing the negative impact of low-quality samples, thereby improving the overall performance of the model.
[0095] The specific embodiments described above further illustrate the technical problems, technical solutions and beneficial effects solved by the present invention. It should be understood that the above are only specific embodiments of the present invention and are not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the protection scope of the present invention.
Claims
1. A PCB surface defect detection method based on an improved YOLOv8n model, characterized in that The steps of the method include: S01: Obtain PCB surface defect dataset; S02: preprocessing the acquired PCB surface defect data set; S03: Improve the YOLOv8n model to obtain an improved YOLOv8n model; S04: Use the preprocessed PCB surface defect dataset to train the improved YOLOv8n model, and evaluate the performance of the trained improved YOLOv8n model through evaluation indicators; Improvements to the YOLOv8n model include: Introduce the LSK-Attention mechanism in the last layer of the backbone network of the YOLOv8n model; Introduce the efficient layer aggregation network GELAN into the YOLOv8n model, that is, replace the C2f module in the YOLOv8n model with the RepNCSPELAN4 module, and replace the SPPF module with the SPPELAN module; Replace the CIoU loss function in the YOLOv8n model with the WIoU loss function.
2. The PCB surface defect detection method according to claim 1, characterized in that: In step S01, there are six types of PCB surface defects, namely: leak holes, mouse bites, open circuits, short circuits, burrs and excess copper.
3. The PCB surface defect detection method according to claim 1, characterized in that: In step S02, preprocessing includes: converting the label file into txt format and dividing the training set and the validation set according to the ratio.
4. The PCB surface defect detection method according to claim 3, characterized in that: In step S02, the preprocessing further includes: performing data enhancement processing on the PCB surface defect dataset before training.
5. The PCB surface defect detection method according to claim 4, characterized in that: Data enhancement processing includes cropping, rotation, HSV data enhancement, Mosaic data enhancement, and MixUp data enhancement.
6. The PCB surface defect detection method according to claim 1, characterized in that: The WIoU loss function includes three modes: Wise-IoU v1, Wise-IoU v2 and Wise-IoUv3; The WIoU v1 loss function is defined as follows: L WIoUv1 =R WIoU L IoU ; Among them, the anchor box is The target frame is W i H i is the intersection of the target box and the anchor box, W g and H g Represent the width and height of the minimum bounding box, S u is the union of the target box and the anchor box, and IoU is the intersection over union ratio; R WIoU is the penalty term, R WIoU ∈[1,e), which enlarges the L of the normal quality anchor box IoU , while L IoU ∈[0,1], reducing the R of high-quality anchor boxes WIoU , in order to prevent R WIoU Produce gradients that hinder convergence, W g ,H g Separation from the computational graph; The Wise-IoU v2 loss function is defined as follows: is the sliding average, which is used as a normalization factor. The normalization factor is dynamically updated to make the gradient gain The overall level is kept high. γ is an index used to control the focusing effect of bounding box regression. The quality of the anchor box can be measured by the outlier degree, which is defined as: The Wise-IoU v3 loss function is defined as follows: Among them, α and δ are two hyperparameters used to control the gradient gain r. Setting appropriate α and δ values enables the model to better adjust the gradient at different stages of training, thereby improving the performance of the model.
7. The PCB surface defect detection method according to claim 1, characterized in that: In step S04, the evaluation indicators are precision, recall, average precision of each category, parameter quantity and billion floating point operations per second.
Citation Information
Patent Citations
PCB bare board surface defect detection method based on YOLOv8
CN116823738A
Complex road scene target detection method based on YOLOv7 improvement
CN116844126A
Lightweight apple target detection method
CN117095391A
Road defect detection method and device based on improved YOLOv8 model and medium
CN117576073A
PCB surface defect detection method based on improved YOLOv7-tiny
CN117593277A
Cited By
New energy lithium battery surface defect visual detection method based on YOLOv8 model
CN120235848A
