PCB surface defect detection method and system based on improved YOLOv8
By improving the YOLOv8 model and introducing the C2f_DCNv2 module, SPPF_LSKA module, and C2f_SCConv module, the geometric deformation adaptability, multi-scale feature fusion, and feature redundancy problems of YOLOv8 in PCB defect detection are solved, achieving higher accuracy and faster detection results.
Patent Information
- Application Number
- CN202510833889.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-20
- Publication Date
- 2025-09-26
AI Technical Summary
The existing YOLOv8 algorithm has problems in PCB defect detection, such as weak geometric deformation adaptability, insufficient multi-scale feature fusion, and feature redundancy. This results in insufficient detection accuracy and speed, making it difficult to meet industrial real-time detection needs.
By improving the YOLOv8 model, introducing the C2f_DCNv2 module to enhance geometric deformation adaptability, the SPPF_LSKA module to expand the multi-scale receptive field, and combining the C2f_SCConv module to eliminate feature redundancy, the model lightweight design is optimized.
It improves the detection accuracy and speed, and enhances the ability to identify PCB defects, especially the detection accuracy of tiny defects and the ability to identify them in complex backgrounds.
Smart Images

Figure CN120707531A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of computer vision and industrial target detection, and in particular to a PCB surface defect detection method and system based on an improved YOLOv8, which is suitable for automated quality inspection of printed circuit boards (PCBs) in electronics manufacturing. Background Art
[0002] In mass PCB production, due to the complex manufacturing process and numerous processing steps, each process (such as etching, drilling, and soldering) has the potential to introduce surface defects such as leaks, rat bites, and short circuits. These defects are often tiny and often obscured by background textures. Traditional manual visual inspection is inefficient, resulting in a high rate of missed detection, seriously impacting the reliability and yield of PCB products. Existing PCB defect detection technologies primarily rely on manual analysis combined with traditional image processing algorithms (such as edge detection and template matching). This approach is not only time-consuming and labor-intensive, but also poorly adaptable to variations in defect morphology. In recent years, deep learning-based object detection algorithms have become increasingly mainstream. They can be categorized into two types: two-stage algorithms (such as Faster R-CNN) use a region proposal network (RPN) to generate candidate bounding boxes, followed by classification and regression. While they offer high accuracy, they are computationally complex and struggle to meet the real-time demands of production lines. Single-stage algorithms (such as the YOLO series) directly predict object location and category end-to-end, offering fast detection speeds but limited sensitivity for small objects.
[0003] YOLOv8, as a currently advanced single-stage algorithm, has demonstrated a high speed advantage in PCB defect detection, but its default structure has significant limitations: weak adaptability to geometric deformations, with fixed convolution kernels struggling to capture the diverse deformations of PCB defects; insufficient multi-scale feature fusion, with the existing SPPF module failing to adequately extract contextual information for small defects (such as micropore cracks); and feature redundancy issues, with standard convolutions prone to generating noisy responses in complex backgrounds, leading to false detections. Summary of the Invention
[0004] This invention aims to provide a PCB surface defect detection method and system based on an improved YOLOv8 algorithm. This method achieves higher detection accuracy and faster inference speed in industrial PCB defect detection equipment. It also optimizes the model's lightweight design, making it easier to deploy in intelligent quality inspection systems. Production line inspectors can quickly locate defects (such as short circuits, open circuits, and copper residue in holes) based on the test results, thereby improving quality control efficiency in PCB manufacturing.
[0005] Technical solution:
[0006] A PCB surface defect detection method based on improved YOLOv8 includes the following steps:
[0007] Step 1: Build a dedicated dataset for PCB surface defects, covering all types of defect samples required for target detection;
[0008] Step 2: Divide the dataset into training set, validation set, and test set in proportion;
[0009] Step 3: Improve the YOLOv8 model: In the backbone framework, C2f and DCNv2 are fused to form the C2f_DCNv2 module, and the BAM attention mechanism module is introduced at the end of the backbone; the LSKA attention mechanism module is introduced into the SPPF module and fused into the SPPF_LSKA module; in the neck framework, C2f and SCConv are fused into the C2f_SCConv module;
[0010] Step 4: Use the training set and validation set to train and verify the improved YOLOv8 model and generate the optimal weight file
[0011] Step 5: Load the optimal weight file and use the test set to evaluate the model performance.
[0012] Preferably, in step 1, PCB images with a resolution of ≥1920×1080 pixels are collected by an industrial camera or optical inspection equipment, covering different lighting, angles and PCB models; data augmentation operations such as random rotation of ±30° and brightness adjustment are performed on the original data; defect bounding boxes are annotated using the YOLO format, and blurred, overexposed or incorrectly annotated images are eliminated.
[0013] Preferably, the implementation of the C2f_DCNv2 module includes: replacing the standard convolution of Bottleneck in the C2f module with the DCNv2 deformable convolution; learning the spatial offset Δpk through the dynamic deformable convolution DCNv2 to adapt to the defect geometric deformation; for the input feature x and the convolution kernel weight W, the value of the DCNv2 output feature map position p is:
[0014]
[0015] Among them, K is the sampling point of the convolution kernel, p k is the fixed offset predefined by the convolution kernel, Δpk is the dynamic offset of each sampling point learned, and Δm k is the learned modulation factor for each sampling point.
[0016] Preferably, the feature extraction process of the C2f_DCNv2 module is as follows: the feature map is input into C2f_DCNv2, first passes through a standard convolution Conv, and then the number of channels is divided into two through the split module. The first channel passes through n Bottleneck_DCNv2 modules in turn and outputs, and the second channel is directly spliced with the output of the first channel based on the Concat module, and finally passes through a standard convolution Conv to obtain the output feature map.
[0017] Preferably, the implementation of the SPPF_LSKA module includes: adding an LSKA module after the Concat layer of SPPF; the generation of the LSKA module weight is: decomposing the convolution kernel into depth-separable convolutions in the horizontal and vertical directions, first sequentially passing through the horizontal 1×3 and vertical 3×1 convolution kernels for separation convolution to generate a primary feature map; then sequentially passing through the horizontal 1×5 and vertical 5×1 hole convolution kernels for large receptive field convolution to generate the final feature map Y.
[0018] Preferably, the feature extraction process of the SPPF_LSKA module is as follows: the feature map is input into SPPF_LSKA, first passes through a standard convolution Conv, and then passes through a three-layer maximum pooling module Maxpool. The size of each pooling window is 5*5. Due to the stacking effect, the receptive fields of the three-layer pooling modules are 5*5, 9*9, and 13*13, respectively, to obtain the features of each scale of each input feature map; then the output of the first convolution and the output of the three-layer pooling are spliced together through the Concat module, and the spliced result is input to the LSKA module, and finally the feature map is output through a standard convolution Conv.
[0019] Preferably, the implementation of the C2f_SCConv module includes: replacing the standard convolution of Bottleneck with the SCConv spatial channel reorganization convolution, and SCConv performs the following steps in sequence:
[0020] (a) Spatial Reorganization Unit (SRU): Suppresses spatial redundancy through group normalization, gated threshold segmentation, and cross-reconstruction.
[0021] (b) Channel Reorganization Unit CRU: feature splitting by proportion α, group convolution, and channel attention weighted fusion.
[0022] The present invention also discloses a PCB surface defect detection system based on improved YOLOv8, comprising: a hardware module: including an industrial camera, used to collect PCB video streams and transmit them to a PC via a USB interface; a software module: a PyQt-based graphical interface, used to load images / video streams or control real-time acquisition; calling the best.pt weight file generated by the improved YOLOv8 model to visually display the defect type, confidence level and location coordinates.
[0023] The beneficial effects of the present invention are:
[0024] The invention solves the problems of weak geometric deformation adaptability, which makes it difficult for fixed convolution kernels to capture the diverse deformations of PCB defects; insufficient multi-scale feature fusion, and insufficient extraction of contextual information of small defects (such as microporous cracks) by the existing SPPF module; feature redundancy problem, in which standard convolution is prone to produce noise response in complex backgrounds, leading to false detection; by fusing deformable convolution with the C2f_DCNv2 module, the geometric deformation adaptability is enhanced, solving the problem of detecting diverse deformations of PCB defects; by using the void convolution of the SPPF_LSKA module to expand the multi-scale receptive field, significantly enhancing the capture of contextual information of small defects such as microporous cracks; and by combining the spatial compression mechanism of the C2f_SCConv module to eliminate feature redundancy and effectively suppress complex background noise, it can be better applied to the task of PCB defect detection. BRIEF DESCRIPTION OF THE DRAWINGS
[0025] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0026] Figure 1 Flowchart of the PCB surface defect detection method based on improved YOLOv8 provided by an embodiment of the present invention;
[0027] Figure 2 An improved YOLOv8 network structure diagram provided by an embodiment of the present invention;
[0028] Figure 3 This is a structural diagram of the C2f_DCNv2 and Bottleneck_DCNv2 modules in an embodiment of the present invention;
[0029] Figure 4 This is a structural diagram of the SPPF_LSKA module in an embodiment of the present invention;
[0030] Figure 5 This is a structural diagram of the C2f_SCConv and Bottleneck_SCConv modules in an embodiment of the present invention;
[0031] Figure 6 A schematic diagram of the structure of a PCB surface defect detection system based on improved YOLOv8 provided in an embodiment of the present invention. DETAILED DESCRIPTION
[0032] To make the above-mentioned objects, features, and advantages of the present invention more apparent and understandable, specific embodiments of the present invention are described in detail below with reference to the accompanying drawings. The following description sets forth many specific details to facilitate a full understanding of the present invention. However, the present invention can be implemented in many different ways than those described herein, and those skilled in the art can make similar modifications without violating the scope of the invention. Therefore, the present invention is not limited to the specific embodiments disclosed below. The following embodiments of the present invention are further described in detail with reference to the accompanying drawings.
[0033] Example 1
[0034] This embodiment discloses a PCB surface defect detection method based on improved YOLOv8, such as Figures 1 to 6 As shown, the following steps are included:
[0035] Step 1: Build a dedicated dataset for PCB surface defects, covering all types of defect samples required for target detection. Specifically,
[0036] Step 1.1, obtain image samples: In this embodiment, the dataset is PCB defect data published by a laboratory. The original dataset contains a total of 554 samples, with an image resolution of 3034*1586 and a jpg format. The defects are divided into six categories: missing hole, mouse bite, open circuit, short circuit, spur, and spurious copper.
[0037] Step 1.2, Data Augmentation: Due to the small number of original image samples, image augmentation is necessary to expand the dataset to improve the model's generalization performance and enhance its ability to detect defects in different scenarios. The augmentation techniques used are 90° rotation, 180° rotation, 270° rotation, brightness variation (random factor 0.6-1.2), and random rotation + brightness variation (rotation angles of 90° / 180° / 270°, brightness variation factor 0.6-1.2). The resulting augmented dataset contains 554 + 554 × 5 = 3324 samples.
[0038] Step 1.3, image annotation: Use the labelimg tool to annotate the expanded data samples. Select the defects in each image using the minimum bounding rectangle and label the defect category. The label of missing_hole is 0, the label of mouse_bite is 1, the label of open circuit is 2, the label of short circuit is 3, the label of spur is 4, and the label of spurious_copper is 5. Save the annotation results of each image in txt format.
[0039] Step 1.4, data cleaning: remove blurred, overexposed, or incorrectly labeled images to ensure the quality of the dataset.
[0040] Step 2, data set division: Divide the image samples and label samples in the data set into training set (train), validation set (valid) and test set (test) in a ratio of 7:2:1.
[0041] Step 3: Build and improve the YOLOv8 network model.
[0042] Configure the environment: Download the YOLOv8 source code from Github, use Anaconda to install a virtual environment to run the source code, and use the Ultrilytics command to download the necessary software packages.
[0043] Input data: Input the constructed training set and validation set into the improved YOLOv8 network model, including image and label samples, and the network model configuration file.
[0044] Debug hyperparameters: Set the input image size to 640*640, the number of training epochs to 300, the batch size to 32, the initial learning rate to 0.01, the final learning rate to 0.001, the number of input image channels to 3, and the optimizer to Adam.
[0045] like Figure 2 As shown in the figure, the improved YOLOv8 target detection model adopts a three-stage architecture as a whole: Backbone network for feature extraction, Neck network for multi-scale feature enhancement, and Detect for defect localization and classification.
[0046] The Backbone network includes the basic convolution layer Conv, the feature extraction layer C2f_DCNv2 (replacing the original C2f), the multi-scale enhancement layer SPPF_LSKA (replacing the original SPPF) and the attention mechanism layer BAMBlock.
[0047] The basic convolutional layer Conv module includes Conv2d→BatchNorm2d→SiLU connected in sequence.
[0048] The C2f_DCNv2 module, SPPF_LSKA module, and BAMBlock module are core improvements to the Backbone network:
[0049] (1) The C2f_DCNv2 module is a fusion of C2f and DCNv2. Specifically, the standard convolution cv2 in BottleNeck in the C2f module is replaced with the deformable convolution DCNv2. Since PCB defects (such as "rat bites" and "burrs") usually have irregular geometric shapes, the fixed sampling method of standard convolution is difficult to adapt to such deformations, resulting in inaccurate detection frames. After adding the deformable convolution (DCNv2) module to this application, the model's detection accuracy for deformation defects can be effectively improved, reducing missed detections.
[0050] like Figure 3 As shown in the figure, the specific structure of the C2f_DCNv2 module is: Input→Conv→split→n×Bottleneck_DCNv2→Concat→Conv.
[0051] The specific structure of Bottleneck_DCNv2 is: Input→Conv→DCNv2, and the low-level features are retained by short-circuiting shortcut=true.
[0052] The DCNv2 convolution kernel's sampling points can be dynamically offset based on the input content, breaking through the fixed grid limitations of standard convolution and better adapting to object deformation and posture changes. Furthermore, the DCNv2 convolution kernel introduces a learnable modulation factor mask for each sampling point, ranging from [0, 1]. This factor dynamically adjusts the weight of the offset position, allowing the model to more precisely control which areas are more important.
[0053] The C2f_DCNv2 module learns offsets through dynamic deformable convolution DCNv2 to adapt to defect geometry deformation.
[0054] Feature extraction process: The feature map is input into C2f_DCNv2, first passes through a standard convolution Conv, and then passes through the split module to split the number of channels into two. One part continues to pass through n Bottleneck_DCNv2 modules and output. This output is spliced with the other part that did not pass through Bottleneck_DCNv2 based on the Concat module, and finally passes through a standard convolution Conv to obtain the output feature map.
[0055] For the deformable convolution DCNv2 in the Bottleneck_DCNv2 module, the offset of the convolution kernel acquisition point and the learning of the modulation factor mask are added on the basis of the standard convolution. For the input feature x and the convolution kernel weight W, the value of the output feature map position p is:
[0056] Among them, the number of sampling points of the convolution kernel is set to K, pk is the fixed offset predefined by the convolution kernel, Δp k is the learned dynamic offset of each sampling point, Δm k is the modulation factor of each sampling point learned. In the formula, the coordinate of the kth actual sampling point is p+pk+Δpk. Since this is a fractional coordinate, the weighted value must be obtained by the bilinear interpolation method and then multiplied by the modulation factor Δm of the sampling point. k , and finally the weighted sum of all sampling points is used to obtain the value of position p.
[0057] The learning of dynamic offset and modulation factor is calculated through an independent convolution. kernel_size is the convolution kernel size, the number of output channels is set to 3*kernel_size*kernel_size, the first 2*kernel_size*kernel_size value is the dynamic offset (offset), and the last kernel_size*kernel_size is the modulation factor (mask).
[0058] The obtained dynamic offset and modulation factor values are passed through the MPCA module, and the generation of the offset and modulation factor is further optimized through the spatial and channel attention mechanisms; they are respectively input into the deformable convolution deform_conv2d provided by torchvision to obtain the output feature map, and then a complete DCNv2 module is obtained through BatchNorm2d batch normalization and ReLU activation function.
[0059] (2) The SPPF_LSKA module is a fusion of SPPF and LSKA. Specifically, an LSKA module is added after the Concat module in the SPPF network to enhance the fusion capability of multi-scale features and dynamically adjust the weights of important spatial regions while maintaining computational efficiency. Due to the large size differences of PCB defects (such as "mixed copper" may cover a large area, while "burrs" are only a few pixels), the original SPPF module has insufficient multi-scale perception capabilities, resulting in unstable detection of small or large defects. This application introduces the LSKA module into the SPPF module, which expands the receptive field through depth-separable convolution in the horizontal and vertical directions, captures defect features of different scales, and effectively improves the model's detection accuracy for defects of different sizes.
[0060] like Figure 4 As shown in Figure 1, the specific structure of the SPPF_LSKA module is: Input→Conv→3×Maxpool→Concat (multi-pooling concatenation)→LSKA→Conv. LSKA horizontally-vertically separable convolution can enhance key features.
[0061] Feature extraction process: After the feature map is input into the SPPF_LSKA network, it first passes through a standard convolution Conv, and then passes through a three-layer maximum pooling module Maxpool. The size of each pooling window is 5*5, but due to the stacking effect, the receptive fields of the three-layer pooling modules are 5*5, 9*9, and 13*13 respectively, to obtain the features of each scale of each input feature map; then the output of the first convolution and the output of the three-layer pooling are spliced together through the Concat module, and the spliced result is input to the LSKA module, and finally the feature map is output through a standard convolution Conv.
[0062] In the LSKA module, the convolution kernel is decomposed into depth-separable convolutions in the horizontal and vertical directions, generating spatial attention weights for the feature map and enhancing the feature response of important areas. When the feature map passes through the LSKA module, the following operations are performed in sequence:
[0063] (a) Separate the convolution processing feature map with 1×3 convolution kernel in the horizontal direction to obtain f0 1*3 (X);
[0064] (b) Separate the convolution process step (a) with a 3×1 convolution kernel in the vertical direction to generate the primary feature map f1 3*1 (f0 1*3 (X));
[0065] (c) Output of the horizontal 1×5 hole convolution kernel with large receptive field convolution step (b), and f2 is obtained 1*5 (f1 3*1 (f0 1 *3 (X)));
[0066] (d) The output of the convolution process step (c) with a 5×1 hole convolution kernel in the vertical direction and a large receptive field is obtained to obtain f3 5*1 (f2 1*5 (f1 3 *1 (f0 1*3 (X))));
[0067] (e) The output of step (d) is integrated through a 1×1 standard convolution to generate the final feature map Y, which is expressed as: Y = f4 1*1 (f3 5*1 (f2 1*5 (f1 3*1 (f0 1*3 (X))))).
[0068] (3) The BAM attention mechanism module is located at the end of the Backbone framework. By dynamically adjusting the importance of the feature map through channel and spatial dual-path attention, the network can focus on key information more efficiently and suppress irrelevant background noise. Since tiny defects on PCBs (such as "leak holes" and "short circuits") only occupy a very small number of pixels in the image, they are easily overwhelmed by background information, resulting in missed detection. After adding the channel-space dual-path attention mechanism (BAM) module, this application can effectively improve the recall rate of small targets and reduce missed detections.
[0069] Feature extraction process: For the channel attention branch, the input feature map is first passed through a global average pooling (GAP) to obtain a channel statistic, and then the channel weight M is obtained through two layers of fully connected layers that first reduce the dimension and then increase the dimension and the nonlinear activation function ReLU. c , the formula is expressed as:
[0070] M c (X) = LN(MLP(AvgPool(X))),
[0071] Where AvgPool represents the global pooling operation, and MLP is a two-layer linear layer structure. Each linear layer contains a fully connected layer (Linear), batch normalization (BN), and activation function (ReLU), and finally outputs the channel weight M through a single fully connected layer (LN). c .
[0072] For the spatial attention branch, the input feature map is first compressed by a 1×1 convolution to reduce the number of channels. Then, a 3×3 dilated convolution is performed on the 3 layers to expand the receptive field and effectively aggregate the context information. Finally, a 1×1 convolution is performed on the output channel to compress it to 1 to generate the spatial weight M. s , which can be expressed as:
[0073] M s =f4 1*1 (f3 3*3 (f2 3*3 (f1 3*3 (f0 1*1 (X)))))
[0074] In the formula, except f4 1*1 Except for the single Conv2d convolution, the rest of the convolutions are combined structures of Conv2d, BN and ReLU.
[0075] Finally, the expanded channel attention and spatial attention are element-wise added and the weight M is obtained through the Sigmoid activation function. Finally, the feature map Y is output. The calculation formula is:
[0076] The overall feature extraction path of the Backbone network is a serial structure of Conv→C2f_DCNv2→BAM, and the output of the BAMBlock module is directly connected to Neck.
[0077] The Neck network includes feature recombination (C2f_SCConv) and feature fusion (Upsample, which expands the feature map) and concatenation (Concat, which combines the backbone's mid-level features). The C2f_SCConv module outputs to the Detect branch, which is a three-level detection branch.
[0078] (1) The C2f_SCConv module is a fusion of C2f and SCConv. Specifically, the standard convolution cv2 in the Bottleneck in the C2f network is replaced with the spatial channel reorganization convolution SCConv to form a new Bottleneck_SCConv network. Since the PCB surface usually has complex background textures (such as copper wires, pads, silk screens, etc.), these areas are prone to generate a large amount of redundant calculations during the feature extraction process, resulting in model misdetection or missed detection. After adding the spatial channel reorganization convolution (SCConv) module, this application can reduce the amount of calculation, improve the inference speed, and reduce false detections caused by background interference.
[0079] like Figure 5 As shown in Figure 1, the structure of the C2f_SCConv module is: Input→Conv→split→n×Bottleneck_SCConv→Concat (concatenating the split branches that have not passed through Bottleneck_SCConv and the output of each level of Bottleneck_SCConv)→Conv.
[0080] The structure of the Bottleneck_SCConv module is: Input→Conv→SCConv, and the short-circuit connection shortcut=true retains low-level features.
[0081] SCConv convolution can greatly reduce the amount of computation and is mainly composed of two units: spatial reorganization unit (SRU) and channel reorganization unit (CRU). SRU uses a separation and reconstruction method to suppress spatial redundancy, and CRU uses a split-transform-fusion strategy to reduce channel redundancy.
[0082] Feature extraction process: After the feature map is input into the C2f_SCConv network, it first passes through a standard convolution Conv, then the output channel is divided into two through the split module, one part passes through n Bottleneck_SCConv networks continuously, and then the output of the first convolution Conv and the output of each Bottleneck_SCConv are spliced together through the Concat module, and finally passed through a standard convolution Conv to obtain the output feature map.
[0083] For spatial channel reorganization convolution SCConv, the amount of calculation can be greatly reduced. First, separation-reconstruction is performed to reduce spatial redundancy, and then segmentation-transformation-fusion is used to reduce channel redundancy. For spatial reorganization SRU, the input feature map group is first normalized by GN operation:
[0084]
[0085] Among them, μ and σ are the mean and standard deviation of X, ε is a small positive constant added for division stability, β is a trainable affine transformation, and γ is a trainable scaling parameter of GN, which is used to measure the spatial information richness of each channel; then the normalized weight W is calculated γ , binary weights W1 (important part) and W2 (redundant part) are generated by sigmoid function and threshold gating (the threshold is set to 0.5), which are expressed as:
[0086] W=Gate(Sigmoid(W γ GN(x))),
[0087] Where Wγ is the normalized correlation weight.
[0088] The input feature X is multiplied by the important weight and redundant weight to obtain two parts, and then the output feature map is obtained by cross reconstruction and splicing:
[0089]
[0090] X w1 ∪X w2 =X w
[0091] X w1 and X w2 is the cross-reconstruction feature, is the element-wise summation, and ∪ is the concatenation. After applying SRU to the intermediate input feature X, the features of the important part are separated from the features of the redundant part, and they are reconstructed to enhance the representative features and suppress the redundant features in the spatial dimension.
[0092] For the channel reorganization unit CRU, the features output by SRU are divided into two parts according to the ratio α, and then the two parts are compressed using 1×1 convolution channels. up Group convolution and point convolution weights are used to extract features. The lower part X low Use point convolution weights and X low Splicing to extract features:
[0093]
[0094] Where W G and W P1 is the learnable weight matrix of GWC and PWC, X up and Y1 are the upper layer input and output feature maps, W P2 is the learnable weight matrix of PWC, ∪ is the concatenation operation, X low and Y2 are the lower layer input and output feature maps respectively.
[0095] Finally, the average pooling S1 and S2 of the global channel are calculated, and the attention weights β1 and β2 are generated by the Sigmoid function to obtain the final output feature map:
[0096] Y=β1Y1+β2Y2,
[0097] Where β1 and β2 are feature importance vectors.
[0098] Detect uses three detection branches for defects of varying scales. Each branch follows the structure: Conv → Conv2d → Cls_loss / Bbox_loss. Conv is used for feature refinement, Conv2d adjusts the output channels, Cls_loss is used for defect classification (including six categories, such as leaks and rat bites), and Bbox_loss is used for bounding box regression (locating defect locations). Detect directly receives feature maps of varying resolutions from Neck. The large target branch detects large defects such as burrs and copper scraping; the medium target branch detects medium-sized defects such as opens and shorts; and the small target branch detects small defects such as leaks and rat bites.
[0099] This application adds the C2f_DCNv2 module to the deep network (P5 layer) of the backbone to process deformation features, which meets the needs of locating irregular defects; adds the SPPF_LSKA module at the end of the backbone to aggregate multi-scale features and expand the receptive field, which is suitable for extracting contextual information of defects of different sizes; adds the BAM attention module to the last layer of the backbone to further filter redundant information and enhance the response to small targets; replaces all C2f modules with C2f_SCConv modules in the head to reduce computational redundancy and improve inference speed.
[0100] Step 4: Input the training set and validation set into the original and improved YOLOv8 models respectively to generate the optimal weight file best.pt.
[0101] The hardware configuration used in the present invention is: CPU: Intel(R) Xeon(R) Platinum 8352V, CPU main frequency: 2.1GHz, GPU: virtual GPU (vGPU), memory: 90GB, video memory: 32GB; software configuration is: PyTorch=>1.11.0, Python=>3.8 (Ubuntu 20.04), CUDA=>11.3; the set hyperparameters are: image size is 640*640, number of training epochs is 300, initial learning rate is 0.01, final learning rate is 0.001, number of batches is 32, and optimizer is Adam.
[0102] Load the training set, validation set, and model configuration .yaml file, set model to train, wait for 300 training cycles to complete, and save the training results in the detect folder, which contains the last.pt and best.pt files, confusion matrix, P (precision) curve, R (recall) curve, F1 curve, etc.
[0103] Step 5: Test and evaluate the improved YOLOv8 model. This example discloses a comparative experiment. A test set and the trained optimal weight file, best.pt, are loaded. The test results of the improved PCB surface defect detection model on the test set are obtained, including defect location, defect category, and confidence. The results are then compared with the test results of the original YOLOv8 model in terms of precision (P), recall (R), and mean average precision (mAP), and the improvement percentage is calculated.
[0104] As shown in Table 1, this is a comparison table of the original and improved YOLOv8 model experiments:
[0105] Table 1
[0106] Comparison of experimental results of original and improved YOLOv8 models
[0107] Model Precision (P) Recall (R) mAP@0.5 Original YOLOv8 0.945 0.921 0.943 Improving YOLOv8 0.985 0.967 0.986
[0108] According to the calculation results in Table 1, the improved algorithm improves precision by 4.2%, recall by 5.0%, and mean average precision (mAP@0.5) by 4.6%. The improved algorithm not only performs better in overall detection accuracy, but also significantly improves its ability to identify subtle defects in complex backgrounds.
[0109] In this embodiment, an ablation experiment is added to verify the technical effects of each innovative module. Table 2 shows a comparison table of ablation experiment data.
[0110] Table 2
[0111] Model Precision (P) Recall (R) mAP@0.5 YOLOv8 0.945 0.921 0.943 YOLOv8+C2f_SCConv 0.952 0.927 0.948 YOLOv8+C2f_DCNv2 0.957 0.931 0.954 YOLOv8+BAM 0.968 0.942 0.962 YOLOv8+SPPF_LSKA 0.965 0.943 0959 Improving YOLOv8 0.985 0.967 0.986
[0112] The above modules are the original YOLOv8 model, the experimental data of each innovative module superimposed on the original model, and the experimental data of each innovative module superimposed on the original model. The results show that after replacing the neck with the C2f_SCConv module, the precision rate is improved by 0.74%, the recall rate is improved by 0.65%, and the mAP is improved by 0.53% compared with the original model; after replacing the backbone with the C2f_DCNv2 module, the precision rate is improved by 1.3%, the recall rate is improved by 1.1%, and the mAP is improved by 1.2% compared with the original model; introducing B at the end of the backbone After implementing the AM attention mechanism, the precision increased by 2.4%, the recall increased by 2.3%, and the mAP increased by 2.0% compared to the original model. By introducing the LSKA attention mechanism module into the SPPF module in the backbone, merging it into the SPPF_LSKA module, the precision increased by 2.1%, the recall increased by 2.4%, and the mAP increased by 1.7% compared to the original model. Finally, by combining all the modules into the original model, the improved YOLOv8 model achieved a 4.2% improvement in precision, a 5.0% improvement in recall, and a 4.6% improvement in mAP compared to the original model. As can be seen, each innovative module slightly improves the detection capability of the original model. When combined with the original model, the detection precision, recall, and mAP all increase significantly, demonstrating the synergistic effect of the modules.
[0113] Example 2
[0114] This embodiment discloses a PCB surface defect detection system based on improved YOLOv8. Figure 6 Shown, including:
[0115] Hardware part: mainly uses industrial cameras to collect video streams of the PCB to be inspected and inputs them to the PC through the USB interface.
[0116] Software part: Design a pyQT-based graphical user interface, which can load pictures or video streams and control the camera to collect data in real time. Then call the best.pt weight model generated by the trained improved YOLOv8 model to visualize the detection results on the graphical user interface, including defect type, confidence level, and coordinate position.
[0117] It should be noted that Figure 6The system structure shown is only an exemplary architecture related to the technical solution of this patent, and its purpose is not to limit the specific computing device type or configuration method to which the present invention is applicable. In actual implementation, relevant technical personnel can add or subtract the components shown, adopt different combinations, or adjust the layout design according to the specific application scenario. For those skilled in the art, it should be understood that there are many feasible variations of the architecture and it should not be limited to the specific embodiments listed in the specification.
[0118] The foregoing is merely a detailed description of specific embodiments of the present invention, intended to enable those skilled in the art to understand and implement the present invention. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present application. Therefore, the present invention is not limited to the embodiments shown herein, but is intended to be construed in the widest manner consistent with the principles and novel features of the present application.
Claims
1. A PCB surface defect detection method based on improved YOLOv8, characterized in that: The steps include: Step 1: Build a dedicated dataset for PCB surface defects, covering all types of defect samples required for target detection; Step 2: Divide the dataset into training set, validation set, and test set in proportion; Step 3: Improve the YOLOv8 model: In the backbone framework, C2f and DCNv2 are fused to form the C2f_DCNv2 module, and the BAM attention mechanism module is introduced at the end of the backbone; the LSKA attention mechanism module is introduced into the SPPF module and fused into the SPPF_LSKA module; in the neck framework, C2f and SCConv are fused into the C2f_SCConv module; Step 4: Use the training set and validation set to train and validate the improved YOLOv8 model to generate an optimal weight file; Step 5: Load the optimal weight file and use the test set to evaluate the model performance.
2. The detection method according to claim 1, wherein In step 1, PCB images with a resolution of ≥1920×1080 pixels are collected using an industrial camera or optical inspection equipment, covering different lighting, angles, and PCB models. Data augmentation operations such as random rotation of ±30° and brightness adjustment are performed on the original data. The defect bounding boxes are annotated using the YOLO format, and blurred, overexposed, or incorrectly annotated images are eliminated.
3. The detection method according to claim 1, wherein The implementation of the C2f_DCNv2 module includes: replacing the standard convolution of Bottleneck in the C2f module with the DCNv2 deformable convolution; learning the spatial offset Δpk through the dynamic deformable convolution DCNv2 to adapt to the defect geometry deformation; for the input feature x and the convolution kernel weight W, the value of the DCNv2 output feature map position p is: Among them, K is the sampling point of the convolution kernel, pk is the predefined fixed offset of the convolution kernel, Δp k is the learned dynamic offset of each sampling point, Δm k is the learned modulation factor for each sampling point.
4. The detection method according to any one of claims 1 to 3, characterized in that The feature extraction process of the C2f_DCNv2 module is as follows: the feature map is input into C2f_DCNv2, first passes through a standard convolution Conv, and then the number of channels is divided into two through the split module. The first channel passes through n Bottleneck_DCNv2 modules in sequence and outputs, and the second channel is directly spliced with the output of the first channel based on the Concat module. Finally, a standard convolution Conv is passed to obtain the output feature map.
5. The detection method according to claim 4, characterized in that The implementation of the SPPF_LSKA module includes: adding an LSKA module after the Concat layer of SPPF; generating the weight of the LSKA module as follows: decomposing the convolution kernel into depth-separable convolutions in the horizontal and vertical directions, first performing separation convolutions with 1×3 convolution kernels in the horizontal direction and 3×1 convolution kernels in the vertical direction to generate a primary feature map; then performing large receptive field convolutions with 1×5 convolution kernels in the horizontal direction and 5×1 convolution kernels in the vertical direction to generate the final feature map Y.
6. The detection method according to claim 5, characterized in that The feature extraction process of the SPPF_LSKA module is as follows: the feature map is input into SPPF_LSKA, first passes through a standard convolution Conv, and then passes through a three-layer maximum pooling module Maxpool. The size of each pooling window is 5*5. Due to the stacking effect, the receptive fields of the three-layer pooling modules are 5*5, 9*9, and 13*13 respectively, to obtain the features of each scale of each input feature map; then the output of the first convolution and the output of the three-layer pooling are spliced together through the Concat module, and the spliced result is input to the LSKA module, and finally the feature map is output through a standard convolution Conv.
7. The detection method according to claim 1, characterized in that The implementation of the C2f_SCConv module includes replacing Bottleneck's standard convolution with SCConv spatial channel reorganization convolution, which performs the following steps in sequence: (a) Spatial Reorganization Unit (SRU): Suppresses spatial redundancy through group normalization, gated threshold segmentation, and cross-reconstruction. (b) Channel Reorganization Unit CRU: feature splitting by proportion α, group convolution, and channel attention weighted fusion.
8. A PCB surface defect detection system based on improved YOLOv8, characterized in that: The detection method according to any one of claims 1 to 7, wherein the system comprises: Hardware module: includes an industrial camera for capturing PCB video streams and transmitting them to a PC via a USB interface; Software module: A PyQt-based graphical interface for loading image / video streams or controlling real-time acquisition; calling the best.pt weight file generated by the improved YOLOv8 model to visualize the defect type, confidence level, and location coordinates.
Citation Information
Cited By
Laptop appearance defect detection method and device based on DL, medium and equipment
CN120931640A
Dl-based notebook computer appearance defect detection method, device, medium and equipment
CN120931640B