PCB defect detection method and device based on improved YOLOv8
By improving the YOLOv8 network model, combining deformable convolution and feature pyramid networks, adding the Triblet Attention mechanism, and using the Focal-EIOU Loss function, the error detection and missed detection problems in complex background and intensive defect detection are solved, achieving high accuracy and efficient PCB defect detection.
Patent Information
- Application Number
- CN202411269228.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-11
- Publication Date
- 2024-12-27
AI Technical Summary
When existing PCB defect detection methods based on deep learning face small defects with complex backgrounds and densely distributed, their feature extraction accuracy is not high, they are prone to false detection and missed detection, and it is difficult to effectively identify defects of different types and sizes.
Using the improved YOLOv8 network model, the feature extraction and detection accuracy are optimized by introducing deformable convolution DCNv2 and RepGFPN, and the Triblet Attention mechanism is added, and Focal-EIOU Loss is used in the loss function.
It significantly improves the ability to identify defects of different types and sizes, enhances the robustness and accuracy of feature extraction, improves detection accuracy and efficiency, and reduces the misjudgment rate and missed detection rate.
Smart Images

Figure CN119205673A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to PCB defect detection, and in particular to a PCB defect detection method and device based on improved YOLOv8. Background Art
[0002] Printed Circuit Board (PCB) has a complex structure. During large-scale industrial production, inherent defects will occur under the influence of various factors. In order to ensure the stable and reliable operation of electronic products, the defect detection link before PCB leaves the factory is particularly important. Traditional manual detection methods are easily affected by environmental interference and subjective factors, and the detection efficiency and accuracy are low.
[0003] The target detection method based on deep learning has powerful feature extraction and pattern recognition capabilities, and has been applied to PCB defect detection, significantly improving detection accuracy and efficiency. CN117351288A discloses a PCB defect detection method based on an improved YOLOv5 algorithm, which uses the EfficientViT feature network to extract features from images and improve the detection capability of PCB defects. However, the following problems still exist:
[0004] (1) When faced with a large number of tiny defect targets with various types and dense distribution on PCB boards, the feature extraction accuracy of the model is not high, and it is easy to have problems of false detection and missed detection;
[0005] (2) Due to the intricate circuits on the PCB board, an extremely complex background environment is formed. This complexity causes the model to easily lose key information during the feature extraction process, which in turn has an adverse impact on the overall detection effect. Summary of the invention
[0006] Object of the invention: The first object of the present invention is to provide a PCB defect detection method based on improved YOLOv8 with high detection accuracy; the second object of the present invention is to provide a PCB defect detection device based on improved YOLOv8.
[0007] Technical solution: The PCB defect detection method based on improved YOLOv8 described in the present invention includes:
[0008] (1) Obtain a public dataset consisting of PCB defect images, perform Mosaic data augmentation on the images in the public dataset to expand the dataset, and divide the expanded dataset into a training set, a test set, and a validation set;
[0009] (2) Build an improved YOLOv8 network model, including:
[0010] Introduce the deformable convolution DCNv2 into the Bottleneck in the C2f module of the P5 feature map extraction layer in the YOLOv8 backbone network, replace all convolutions in the original Bottleneck with the deformable convolution DCNv2, construct Bottleneck_dcnv2, and obtain the C2f_DCNv2 module;
[0011] RepGFPN is used to optimize the feature pyramid network, the TribletAttention mechanism is introduced in the output part of the feature pyramid network, and an additional 160×160 detection head is added to the original 80×80, 40×40, and 20×20 detection heads;
[0012] (3) The improved YOLOv8 network model is trained using the training set, and the model’s PCB defect detection performance is verified using the test set and validation set.
[0013] Furthermore, the public dataset uses a synthetic PCB dataset released by the Peking University Intelligent Robotics Open Laboratory. The images in the dataset have six types of defects: leaky holes, rat bites, open circuits, short circuits, strays, and miscellaneous copper.
[0014] Furthermore, the DCNv2 feature matrix is: Where y(p) represents the value of the output feature map at position p, K represents the number of sampling positions of the convolution kernel, and w k represents the weight of the convolution kernel at the kth position, x(p+p k +Δp k ) indicates that the input feature map is at p+p k +Δp k The eigenvalue at position, p k Represents the standard offset of the kth position of the convolution kernel, Δp k represents the learnable offset of the kth position, Δm k is the normalized adjustment term.
[0015] Furthermore, in the C2f_DCNv2 module, the input tensor (h, w, c) first undergoes a preliminary transformation of the feature map through a 1×1 convolutional layer, where h represents the feature tensor height, w represents the feature tensor width, and c represents the number of channels of the feature tensor; then the Split operation is used to evenly split the feature tensor (h, w, c) output by the convolutional layer into two equal-sized sub-tensors (h, w, 0.5c) along the channel dimension. The two equal-sized sub-tensors are respectively input into two parallel paths, one directly leading to the Concat module, and the other going deep into the Bottleneck_dcnv2 calculation sequence, which consists of n Bottleneck_dc The nv2 modules are connected in series. A shortcut operation is performed before each Bottleneck_dcnv2 module to ensure the continuity of the information flow, and the result is output to the Concat module. At the same time, the output of each Bottleneck_dcnv2 module is also output to the Concat module; the Concat module concatenates (n+2) (h,w,0.5c) tensors from different paths and levels to form a feature tensor with a dimension of (h,w,0.5(n+2)c), and then adjusts the channel dimension through an additional 1×1 convolutional layer to ensure that the number of output channels c_out is consistent with the number of input channels c_in.
[0016] Furthermore, the internal design of the Bottleneck_dcnv2 module follows a dual-path design: one path directly transmits features through a shortcut, and the other path performs deep feature extraction and transformation through two serially connected DCNv2 convolutional modules.
[0017] Furthermore, RepGFPN abandons the additional upsampling operation required in the queen-fusion stage of GFPN; and, RepGFPN adopts the CSPStage structure to replace the traditional 3×3 convolution method in GFPN. The CSPStage structure inputs feature maps from two or three different levels, and is divided into two parallel branches after the concat operation. One branch performs channel dimensionality reduction through 1×1 convolution to reduce the amount of calculation, and the other branch introduces the ELAN feature aggregation module, which is composed of multiple Rep3×3 convolutions and standard 3×3 convolutions alternatingly; the outputs of the two branches are fused again through the concat operation to form the final output feature map.
[0018] Furthermore, the Triblet Attention mechanism has a three-parallel branch architecture. The first two branches focus on analyzing the interaction between the spatial dimensions H, W and the channel dimension C. In this process, the input tensor X first undergoes a transformation of the spatial dimension:
[0019] The first branch rotates the tensor 90° counterclockwise along the height axis H and reshapes it into the form of W×H×C; the second branch performs a similar operation along the width axis W to obtain a H×C×W tensor;
[0020] At the same time, the third branch keeps the original shape of the input tensor X unchanged;
[0021] Subsequently, all branches are subjected to feature compression and fusion by introducing the Z-pool layer, which combines the maximum pooling and average pooling operations in the dimension to reduce the depth dimension of each branch tensor to two dimensions;
[0022] Z-pool is defined as: Z-Pool(x) = [MaxPool(x), AvgPool(X)]
[0023] The reduced-dimensional tensor is then passed through a convolution layer with a convolution kernel of K, and finally the corresponding attention weight is generated by the Sigmond activation function and added to the rotated tensor. At the final output, the first two branches first rotate the output tensor to be consistent with the shape of the input tensor, and then average and aggregate it with the output tensor of the third branch as the final output.
[0024] The expression of the output tensor Y is:
[0025] Among them, X represents the original input tensor, X1 and X2 represent the tensors of the first two branches after rotation, and Represent the attention weights generated by the Sigmond activation function of the three branches. Indicates that the tensor needs to be rotated.
[0026] Furthermore, the improved YOLOv8 network model adopts the Focal-EIOU Loss function.
[0027] Further,
[0028]
[0029] Among them, L EIOU represents the enhanced intersection-over-union loss function; L IOU represents the standard intersection-over-union loss; L dis Indicates the center point distance loss; L asp represents the aspect ratio loss; IOU represents the intersection-over-union ratio; b represents the center coordinate of the detection box, b gt represents the center coordinate of the real box; ρ represents b and b gt The Euclidean distance between c and h cRespectively represent the width and height of the bounding box; w represents the width of the detection box, w gt represents the width of the real frame; h represents the height of the detection frame, h gt Indicates the height of the real box.
[0030] The original Yolov8 uses CIOU as the coordinate loss function. The generated boundary constraint prediction label has insufficient constraints on the false target prediction box, which is easy to cause misjudgment and has a great impact on the robustness and accuracy indicators of detection. In the PCB defect detection scenario, the Focal-EIOU Loss function significantly improves the detection accuracy of small-sized and densely distributed defects by focusing on high-quality bounding box predictions. It combines the advantages of CIOU, optimizes the matching degree between the bounding box and the real defect, and solves the problem of detection error caused by low annotation quality. At the same time, the smooth loss curve ensures more effective information dissemination, reduces the influence of geometric factors, makes the model more robust in complex backgrounds and multi-defect target scenarios, and accelerates the convergence speed of model training, improving detection efficiency and accuracy.
[0031] The PCB defect detection device based on improved YOLOv8 described in the present invention includes a memory and a processor, wherein a computer program is stored in the memory. When the computer program is executed by the processor, the steps of the PCB defect detection method based on improved YOLOv8 are implemented.
[0032] Beneficial effects: Compared with the prior art, the present invention has the following significant advantages:
[0033] (1) The present invention uses deformable convolution (DCNv2) combined with C2f convolution layer. Through this optimization strategy, the model can focus on key defect features more accurately when processing PCB board images, improve the ability to identify defects of different types and sizes, and achieve accurate prediction. It has achieved the significant effect of enhancing the robustness and accuracy of feature extraction and improving the accuracy and efficiency of defect detection.
[0034] (2) The present invention uses RepGFPN to replace the original model structure, adds an extra 160×160 detection layer to the detection head, and adds Triblet Attention to the output part. Through this optimization strategy, the model can focus more accurately on key defect features when processing PCB board images, ignore unnecessary background information, and achieve accurate prediction. It can also further shorten the model's reasoning time, thereby achieving a good balance between efficiency and accuracy. BRIEF DESCRIPTION OF THE DRAWINGS
[0035] Figure 1It is a flowchart of a PCB defect detection method based on improved YOLOv8 provided by an embodiment of the present invention;
[0036] Figure 2 is a schematic diagram of the structure of a C2f_DCNv2 module in an embodiment of the present invention;
[0037] Figure 3 is a schematic diagram of the GFPN structure in an embodiment of the present invention;
[0038] Figure 4 is a schematic diagram of the structure of RepGFPN in an embodiment of the present invention;
[0039] Figure 5 is a schematic diagram of the Triplet Attention mechanism in an embodiment of the present invention;
[0040] Figure 6 It is a schematic diagram of the improved YOLOv8 network structure in an embodiment of the present invention;
[0041] Figure 7 It is an indicator diagram of average precision mAP, accuracy Precision, and recall Recall in the embodiment of the present invention. DETAILED DESCRIPTION
[0042] The present invention will be further described below in conjunction with the accompanying drawings.
[0043] like Figure 1 As shown, an embodiment of the present invention provides a PCB defect detection method based on improved YOLOv8, comprising the following steps:
[0044] (1) Obtain a public dataset consisting of PCB defect images, perform Mosaic data augmentation on the images in the public dataset to expand the dataset, and divide the expanded dataset into a training set, a test set, and a validation set;
[0045] The public data set of this embodiment uses a synthetic PCB data set released by the Intelligent Robot Open Laboratory of Peking University, which contains 1,386 images. The images have six types of defects: leaky holes, rat bites, open circuits, short circuits, strays, and miscellaneous copper.
[0046] Four images were randomly scaled, cropped, and arranged and then spliced together to expand the data set, resulting in a total of 3070 images. Data augmentation can increase the diversity of training samples, enrich the defect information of the data set, and improve the robustness, generalization ability, detection accuracy and other performance of the improved YOLOv8 network model described later.
[0047] (2) Build and improve the YOLOv8 network model;
[0048] (2-1) Introduce the deformable convolution DCNv2 into the Bottleneck in the C2f module of the P5 feature map extraction layer in the YOLOv8 backbone network, replace all convolutions in the original Bottleneck with the deformable convolution DCNv2, construct Bottleneck_dcnv2, and obtain the C2f_DCNv2 module, as shown in Figure 2 As shown;
[0049] Since some defects on PCB are too small and of many types, they are easily confused, resulting in poor feature information extracted by the YOLOv8 network model. In order to strengthen the feature fusion between different scales and improve the detection accuracy, the present invention uses deformable convolution DCNv2 combined with C2f convolution layer in the backbone network for optimization.
[0050] In the deep learning architecture, the deformable convolution DCNv2 can significantly enhance the model's adaptive learning ability for geometric transformations. DCNv2 not only retains the basic functions of traditional convolution in feature extraction, but also introduces a flexible sampling offset mechanism to accurately capture and predict non-rigid deformation targets and complex background changes in images. In each convolution sampling process, DCNv2 not only allows the sampling points to be dynamically adjusted according to the learned offset to adapt to the shape changes of the target, but also introduces an additional modulation mechanism, that is, the sampling values are weighted and adjusted using the learned weights. This dual adjustment strategy greatly enriches the flexibility of the convolution operation, enabling it to perform more complex and sophisticated geometric transformation learning. In contrast, the traditional convolution operation is limited by its fixed receptive field size and weight distribution, and it is difficult to effectively deal with the non-rigid deformation and complex and changing background environment that are prevalent in images. This limitation is particularly evident when dealing with challenging visual tasks, which may lead to limited model performance and inability to fully mine key information in the image.
[0051] The feature matrix of standard convolution is shown in formula (1):
[0052]
[0053] Among them, y(p) represents the value of the output feature map at position p, K represents the number of sampling positions of the convolution kernel, and w k represents the weight of the convolution kernel at the kth position, x(·) represents the eigenvalue of the input feature map at the corresponding position; p k Represents the standard offset of the k-th position of the convolution kernel.
[0054] Adding the learnable offset to formula (1) yields the DCNv1 feature matrix as shown in formula (2):
[0055]
[0056] Among them, Δpk represents the learnable offset at the k-th position.
[0057] When the target range is smaller than the DCNv2 range, the non-target area will be misidentified. Therefore, a weight term is added to each sampling point in formula (2) to punish it and eliminate the influence of irrelevant areas. The DCNv2 feature matrix is obtained as shown in formula (3):
[0058]
[0059] Among them, Δm k is the normalized adjustment term.
[0060] Compared with DCNv1, DCNv2 has significant improvements in object detection and instance segmentation. By introducing deformable convolution, comprehensive convolution kernel weights and offset values, the network module is given the ability to change the spatial distribution and relative influence of its samples, and the output is associated with more target areas.
[0061] Combination Figure 2 In the C2f_DCNv2 module, the input tensor (h,w,c) first undergoes a preliminary transformation of feature mapping through a 1×1 convolutional layer (Conv), where h represents the feature tensor height, w represents the feature tensor width, and c represents the number of channels of the feature tensor (i.e., the depth of the feature tensor); then the Split operation is used to evenly split the feature tensor (h,w,c) output by the convolutional layer into two equal-sized sub-tensors (h,w,0.5c) along the channel dimension. This splitting strategy is designed to process features in parallel to improve the flexibility and efficiency of the network. Next, the two sub-tensors of equal size are input into two parallel paths, one of which goes directly to the Concat module, which is a direct path to retain the original features; the other goes deep into the Bottleneck_dcnv2 calculation sequence, which is composed of n (the specific number n is determined by the network architecture design, and n is set to 1 at the feature map P5 layer) Bottleneck_dcnv2 modules in series. Each Bottleneck_dcnv2 module integrates the deformable convolution DCNv2 to enhance the modeling ability of geometric transformation. A shortcut operation is performed before each Bottleneck_dcnv2 module to ensure the continuity of the information flow, and the result is output to the Concat module. At the same time, the output of each Bottleneck_dcnv2 module is also output to the Concat module, thereby realizing multi-level and multi-scale feature fusion.
[0062] As a residual module with residual connections, the Bottleneck_dcnv2 module also follows a dual-path design: one path directly transmits features through shortcuts, and the other path performs deep feature extraction and transformation through two serially connected DCNv2 convolutional modules. This design not only improves the adaptability of the network to complex deformable targets, but also alleviates the gradient vanishing problem in deep networks through residual connections.
[0063] Finally, the Concat module concatenates the (n+2) (h,w,0.5c) tensors from different paths and levels to form a feature tensor with a dimension of (h,w,0.5(n+2)c), and then adjusts the channel dimension through an additional 1×1 convolutional layer to ensure that the number of output channels c_out is consistent with the number of input channels c_in, thereby achieving efficient integration and reuse of features.
[0064] The present invention uses deformable convolution DCNv2 combined with C2f convolution layer in the backbone network part, so that the model breaks through the original convolution limitation and increases variability. The model can better distinguish the types of defects on the PCB board and reduce the negative impact of the board-irrelevant background content on feature extraction, thereby improving the detection accuracy.
[0065] (2-2) RepGFPN is used to optimize the feature pyramid network, the Triblet Attention mechanism is introduced in the output part of the feature pyramid network, and an additional 160×160 detection head is added to the original 80×80, 40×40, and 20×20 detection heads;
[0066] GFPN contains skip layers and cross-scale connections. In the GFPN network, log2n skip layers are used for connections at the same level, while cross-scale connections are made between blocks at different levels. Skip layer connections are an important connection mechanism in GFPN, which establishes direct connections between different levels of the network, including dense-link and log2n-link. Cross-scale connections in GFPN are a mechanism for connecting feature maps of different scales. Figure 3 Shown is the GFPN structure.
[0067] At most log2l-link layers are cascaded in log2n-link. In level k, the lth layer receives feature maps from at most log2l+1 previous layers, and these input layers are expanded with base 2 and depth i as exponential, as shown in formula (4):
[0068]
[0069] Among them, P represents the feature map, l (lowercase letter 1) represents the scale or level of the feature map, and k represents different channels or convolution kernels; Represents feature maps extracted from different layers or different scales.
[0070] Compared with GFPN, RepGFPN has optimized and improved the feature fusion strategy, which is mainly reflected in the following aspects:
[0071] First, RepGFPN introduces a more sophisticated design in the feature fusion process. By configuring feature maps of different scales under different channel dimensions, it achieves the flexibility to adjust the expression capabilities of high-level and low-level features while maintaining lightweight calculations. This innovative design enables RepGFPN to more effectively capture multi-level information in images and improve the model's ability to understand complex scenes.
[0072] Secondly, in order to further improve the real-time performance of the model, RepGFPN abandons the additional upsampling operation required in the queen-fusion stage of GFPN. This change not only simplifies the feature fusion process, but also significantly reduces the computational complexity, allowing RepGFPN to respond to processing tasks faster while maintaining high accuracy and meet the needs of real-time applications.
[0073] Finally, in the specific implementation of the feature fusion module, RepGFPN uses the CSPStage structure to replace the traditional 3×3 convolution method in GFPN. The design of the CSPStage structure fully considers feature reuse and computational efficiency. Its input comes from feature maps of two or three different levels. After the concat operation, it is divided into two parallel branches. One branch performs channel dimension reduction through 1×1 convolution to reduce the amount of calculation, and the other branch introduces the ELAN feature aggregation module. The ELAN feature aggregation module is composed of multiple Rep3×3 convolutions and standard 3×3 convolutions alternately to enhance the nonlinear expression ability of the features. The outputs of the two branches are fused again through the concat operation to form the final output feature map. This design achieves a significant improvement in the feature fusion effect without introducing additional computational burden, so that RepGFPN achieves a good balance between accuracy and efficiency. Figure 4 A schematic diagram of the RepGFPN structure is shown, intuitively presenting its optimized feature fusion mechanism.
[0074] The Triblet Attention mechanism is added to the feature pyramid output part of the YOLOv8 network to reduce the impact of background interference on detection, while effectively integrating multi-scale information and enhancing the ability to capture features between different dimensions in the tensor, thereby effectively improving detection accuracy. By introducing the TA attention mechanism, the network can better understand the information relationship between different channels, further optimize feature representation, and improve target detection performance.
[0075] The working principle of TA attention mechanism is based on its unique three-parallel branch architecture, which aims to fully capture and integrate the complex dependencies between spatial dimensions and channel dimensions. Specifically, the first two branches focus on parsing the interaction characteristics between spatial dimensions H, W and channel dimension C. In this process, the input tensor X first undergoes a transformation of the spatial dimension:
[0076] The first branch rotates the tensor 90° counterclockwise along the height axis H and reshapes it into the form of W×H×C; the second branch performs a similar operation along the width axis W to obtain a H×C×W tensor; this transformation strategy aims to reveal the potential connection between space and channels from different perspectives.
[0077] Meanwhile, the third branch keeps the original shape of the input tensor X unchanged and directly focuses on extracting dependencies within the spatial features.
[0078] Subsequently, all branches are compressed and fused by introducing the Z-pool layer, which cleverly combines the maximum pooling and average pooling operations in the dimension, reducing the depth dimension of each branch tensor to two dimensions, thereby effectively retaining the rich information representation of the tensor and significantly reducing the computational complexity of subsequent processing. This design not only ensures that the TA attention mechanism can operate efficiently, but also promotes the deep interaction and fusion of information between different dimensions, providing the model with a more comprehensive and sophisticated feature representation capability.
[0079] Z-pool is defined as formula (5):
[0080] Z-Pool(X)=[MaxPool(X),AvgPool(X)](5)
[0081] The reduced tensor is then passed through a convolution layer with a convolution kernel of K, and finally the Sigmond activation function generates the corresponding attention weight, which is recorded as and And add it to the rotation tensor; at the final output, the first two branches first rotate the output tensor to be consistent with the shape of the input tensor, and then average and aggregate it with the output tensor of the third branch as the final output;
[0082] The expression of the output tensor Y is shown in formula (6):
[0083]
[0084] Among them, X represents the original input tensor, X1 and X2 represent the tensors of the first two branches after rotation, and Represent the attention weights generated by the Sigmond activation function of the three branches. Indicates that the tensor needs to be rotated. Figure 5 shown.
[0085] The Triblet Attention mechanism establishes dependencies between different dimensions in the network through rotation operations and residual transformations, eliminating the indirect correspondence between channels and weights, and achieving improved accuracy with less computational overhead.
[0086] In addition, the present invention adds a prediction head enhancement model to detect objects with richer scales. PCB solder joints usually contain many small targets, such as leaks, open circuits, mouse bites, etc. After YOLOv8 performs feature fusion, the detection layer outputs feature maps of different sizes for detecting targets of different sizes. The feature map with large size has a small receptive field, contains more target positions and local feature details, and is suitable for detecting small targets; the feature map with small scale has a large receptive field, rich semantic information but unclear local details, and is suitable for detecting large targets. In order to improve the detection accuracy of small defects on PCB boards, while avoiding excessive loss of detail features caused by downsampling. The present invention solves the problem of missing detection when the detection target of YOLOv8 is too small, and the small target feature information of the deeper feature map is insufficient by adding a small target detection layer. The main body of the head part of the YOLOv8 network is three Detect detectors, which detect feature maps of different sizes. When the input image size is 640×640, the sizes of the three feature maps are 80×80, 40×40, and 20×20, respectively. The present invention adds a 160×160 detection layer on the original basis, which retains more feature information than the original three detection layers, making the model more accurate in detecting small targets. Figure 6 As shown, this is the improved YOLOv8 network structure diagram of the present invention.
[0087] (2-3) The improved YOLOv8 network model calculates the loss of the output result of the feature fusion stage and the label image. The loss adopts the Focal-EIOU Loss function. The loss calculation method of the loss function is shown in formula (7):
[0088]
[0089] Among them, L EIOU represents the enhanced intersection-over-union loss function, which is the overall loss function; L IOU represents the standard intersection-over-union loss, which calculates the overlap between the detection box and the true box; L dis Represents the center point distance loss, which is used to measure the Euclidean distance between the center point of the predicted box and the true box; L asprepresents the aspect ratio loss, which is used to measure the difference in aspect ratio between the predicted box and the real box; IOU represents the intersection-over-union ratio, which is the ratio of the overlapping area between the detection box and the real box to the joint area, measuring the degree of their overlap; b represents the center coordinate of the detection box, b gt represents the center coordinate of the real box; ρ represents b and b gt The Euclidean distance between c and h c Respectively represent the width and height of the bounding box; w represents the width of the detection box, w gt represents the width of the real frame; h represents the height of the detection frame, h gt Indicates the height of the real box.
[0090] While increasing the aspect ratio similarity, the EIOU loss also takes into account how the model effectively reduces (w,h) and (w gt ,h gt ). Compared with the original loss function, the loss function used enhances the robustness in complex scenes and multi-target scenes, and is also more beneficial to improving the accuracy of model detection.
[0091] (3) The improved YOLOv8 network model is trained using the training set, and the model’s PCB defect detection performance is verified using the test set and validation set.
[0092] The expanded data set is used to verify the generalization and accuracy of the improved YOLOv8 network model of the present invention. The average precision mAP, accuracy Precision, and recall rate Recall reflect the superiority of the algorithm. The calculation formulas of each evaluation index are as follows:
[0093]
[0094] Among them, K represents the number of categories contained in the data set, AP i represents the average precision of the i-th class; TP represents true positive examples, FN represents false negative examples, and FP represents false positive examples.
[0095] Table 1 shows the mAP comparison results, and Table 2 shows the mAP of the present invention for detecting various defects. Figure 7 This is an indicator diagram of mAP, accuracy, and recall in the present invention.
[0096] Table 1 mAP comparison results
[0097]
[0098]
[0099] Table 2 mAP of the present invention for detecting various defects
[0100]
[0101] From Table 1, the data set is verified, which effectively proves that the improved algorithm of the present invention has significantly improved the detection accuracy of the PCB data set. From Table 2, the mAP and mAP0.5:0.95 values show that the present invention has a high average accuracy for each defect and can complete the PCB defect task well. Figure 7 This is an indicator chart of the average precision mAP, accuracy Precision, and recall Recall of the present invention, which can reflect the model performance from the line graph level.
[0102] An embodiment of the present invention also provides a PCB defect detection device based on improved YOLOv8, including a memory and a processor, wherein a computer program is stored in the memory. When the computer program is executed by the processor, the steps of the PCB defect detection method based on improved YOLOv8 described in the embodiment of the present invention are implemented.
Claims
1. A PCB defect detection method based on improved YOLOv8, characterized in that: include: (1) Obtain a public dataset consisting of PCB defect images, perform Mosaic data augmentation on the images in the public dataset to expand the dataset, and divide the expanded dataset into a training set, a test set, and a validation set; (2) Build an improved YOLOv8 network model, including: Introduce the deformable convolution DCNv2 into the Bottleneck in the C2f module of the P5 feature map extraction layer in the YOLOv8 backbone network, replace all convolutions in the original Bottleneck with the deformable convolution DCNv2, construct Bottleneck_dcnv2, and obtain the C2f_DCNv2 module; RepGFPN is used to optimize the feature pyramid network, the TribletAttention mechanism is introduced in the output part of the feature pyramid network, and an additional 160×160 detection head is added to the original 80×80, 40×40, and 20×20 detection heads; (3) The improved YOLOv8 network model is trained using the training set, and the model’s PCB defect detection performance is verified using the test set and validation set.
2. The PCB defect detection method based on improved YOLOv8 according to claim 1, characterized in that: The public dataset uses a synthetic PCB dataset released by the Peking University Intelligent Robotics Open Laboratory. The images in the dataset have six types of defects: leaky holes, rat bites, open circuits, short circuits, strays, and miscellaneous copper.
3. The PCB defect detection method based on improved YOLOv8 according to claim 1, characterized in that: The DCNv2 feature matrix is: Where y(p) represents the value of the output feature map at position p, K represents the number of sampling positions of the convolution kernel, and w k represents the weight of the convolution kernel at the kth position, x(p+p k +Δp k ) indicates that the input feature map is at p+p k +Δp k The eigenvalue at position, p k Represents the standard offset of the kth position of the convolution kernel, Δp k represents the learnable offset of the kth position, Δm k is the normalized adjustment term.
4. The PCB defect detection method based on improved YOLOv8 according to claim 3, characterized in that: In the C2f_DCNv2 module, the input tensor (h,w,c) first undergoes a preliminary transformation of the feature map through a 1×1 convolutional layer, where h represents the feature tensor height, w represents the feature tensor width, and c represents the number of channels of the feature tensor; then the Split operation is used to evenly split the feature tensor (h,w,c) output by the convolutional layer into two equal-sized sub-tensors (h,w,0.5c) along the channel dimension. The two equal-sized sub-tensors are input into two parallel paths, one directly leading to the Concat module, and the other going deep into the Bottleneck_dcnv2 calculation sequence, which consists of n Bottleneck_dcnv 2 modules are connected in series. A shortcut operation is performed before each Bottleneck_dcnv2 module to ensure the continuity of the information flow, and the result is output to the Concat module. At the same time, the output of each Bottleneck_dcnv2 module is also output to the Concat module; the Concat module concatenates (n+2) (h,w,0.5c) tensors from different paths and levels to form a feature tensor with a dimension of (h,w,0.5(n+2)c), and then adjusts the channel dimension through an additional 1×1 convolutional layer to ensure that the number of output channels c_out is consistent with the number of input channels c_in.
5. The PCB defect detection method based on improved YOLOv8 according to claim 4, characterized in that: The Bottleneck_dcnv2 module follows a dual-path design: one path directly transmits features through shortcuts, and the other path performs deep feature extraction and transformation through two serially connected DCNv2 convolutional modules.
6. The PCB defect detection method based on improved YOLOv8 according to claim 1, characterized in that: RepGFPN abandons the additional upsampling operation required in the queen-fusion stage of GFPN; in addition, RepGFPN adopts the CSPStage structure to replace the traditional 3×3 convolution method in GFPN. The CSPStage structure inputs feature maps from two or three different levels, and is divided into two parallel branches after the concat operation. One branch performs channel dimensionality reduction through 1×1 convolution to reduce the amount of calculation, and the other branch introduces the ELAN feature aggregation module, which is composed of multiple Rep3×3 convolutions and standard 3×3 convolutions alternatingly; the outputs of the two branches are fused again through the concat operation to form the final output feature map.
7. The PCB defect detection method based on improved YOLOv8 according to claim 1, characterized in that: The TribletAttention mechanism has a three-parallel branch architecture. The first two branches focus on analyzing the interaction characteristics between the spatial dimensions H, W and the channel dimension C. In this process, the input tensor X first undergoes a transformation of the spatial dimension: The first branch rotates the tensor 90° counterclockwise along the height axis H and reshapes it into the form of W×H×C; the second branch performs a similar operation along the width axis W to obtain a H×C×W tensor; At the same time, the third branch keeps the original shape of the input tensor X unchanged; Subsequently, all branches are subjected to feature compression and fusion by introducing the Z-pool layer, which combines the maximum pooling and average pooling operations in the dimension to reduce the depth dimension of each branch tensor to two dimensions; Z-pool is defined as: Z-Pool(X) = [MaxPool(X), AvgPool(X)] The reduced-dimensional tensor is then passed through a convolution layer with a convolution kernel of K, and finally the corresponding attention weight is generated by the Sigmond activation function and added to the rotated tensor. At the final output, the first two branches first rotate the output tensor to be consistent with the shape of the input tensor, and then average and aggregate it with the output tensor of the third branch as the final output. The expression of the output tensor Y is: Among them, X represents the original input tensor, X1 and X2 represent the tensors of the first two branches after rotation, and Represent the attention weights generated by the three branches through the Sigmond activation function. Indicates that the tensor needs to be rotated.
8. The PCB defect detection method based on improved YOLOv8 according to any one of claims 1 to 7, characterized in that: The improved YOLOv8 network model adopts the Focal-EIOU Loss function.
9. The PCB defect detection method based on improved YOLOv8 according to claim 8, characterized in that: Among them, L EIOU represents the enhanced intersection-over-union loss function; L IOU represents the standard intersection-over-union loss; L dis Indicates the center point distance loss; L asp represents the aspect ratio loss; IOU represents the intersection-over-union ratio; b represents the center coordinate of the detection box, b gt represents the center coordinate of the real box; ρ represents b and b gt The Euclidean distance between c and h c Respectively represent the width and height of the bounding box; w represents the width of the detection box, w gt represents the width of the real frame; h represents the height of the detection frame, h gt Indicates the height of the real box.
10. A PCB defect detection device based on improved YOLOv8, comprising a memory and a processor, wherein a computer program is stored in the memory, characterized in that: When the computer program is executed by a processor, the steps of the PCB defect detection method based on improved YOLOv8 described in any one of claims 1 to 9 are implemented.
Citation Information
Patent Citations
PCB defect detection method based on improved YOLO v5 algorithm
CN117351288A
Cited By
PCB small target defect detection method based on improved YOLOv8x
CN119722669A
Improved YOLOv11 power transmission line defect detection method
CN120032175A
Rice leaf folder imago image detection and counting method and system oriented to complex environment
CN121095871A
