A method and apparatus for detecting defects on a PCB surface
By employing a semantic background separation mechanism and a small defect perception method guided by intersection boxes, the problems of unstable model training and low accuracy of small defect detection in PCB defect detection are solved, achieving efficient and accurate defect detection. By utilizing feature information from defect-free samples, the cost is reduced.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- HUAZHONG UNIV OF SCI & TECH
- Filing Date
- 2026-03-16
- Publication Date
- 2026-06-19
AI Technical Summary
Existing PCB defect detection technologies rely on large-scale labeled defect samples, which is costly. Furthermore, the high proportion of defect-free samples leads to unstable model training, making it difficult to focus on defect features. The detection accuracy of minor defects is low, and defect-free samples are not effectively utilized.
A semantic background separation mechanism is adopted to accurately separate defect features and non-defect features. Defect features are separated by CLS and IoU dual scoring. Combined with the intersection box-guided micro-defect perception method, data augmentation is performed using non-defect samples to construct a high proportion of non-defect sample training set, and iterative optimization is performed in the decoder network layer.
It improves the detection accuracy and stability of the defect detection model, makes full use of the feature information of defect-free samples, enhances the ability to perceive minute defects, reduces data annotation costs, and improves detection efficiency.
Smart Images

Figure CN122243916A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the technical field of small sample defect detection, and more specifically, relates to a method and device for detecting defects on PCB surfaces. Background Technology
[0002] With the rapid development of information technology, printed circuit boards (PCBs) have become a key component of core hardware in fields such as communication systems, intelligent transportation, industrial automation, and aerospace. Their quality directly determines the reliability and safety of end products. In safety-critical scenarios such as medical implants and automotive control units, PCB defects can cause system malfunctions and even endanger personal safety. Furthermore, the multi-stage nature of PCB manufacturing means that early defects can easily propagate exponentially in subsequent processes, severely impacting product quality. Therefore, high-precision and efficient PCB defect detection is a core element in ensuring production quality and reducing costs, and is of great significance to industrial manufacturing.
[0003] Traditional PCB defect detection mainly relies on manual visual inspection and template matching algorithms. Limited by the small size and complex shape of defects, it suffers from low detection accuracy, poor generalization ability, and low efficiency. In recent years, deep learning methods, such as the YOLO series, DETR series, and Fast R-CNN models, have become mainstream, significantly improving detection speed and accuracy. Some studies have further optimized performance by incorporating PCB-specific features, but existing technologies still face three major problems that urgently need to be addressed: First, they heavily rely on large-scale labeled defect samples, which are scarce in industrial settings, and manual labeling is time-consuming, labor-intensive, and extremely costly. Second, defect-free samples account for over 90% of the total, and these easily obtainable, low-cost samples are not effectively utilized. Furthermore, the extreme imbalance in data leads to unstable training of the defect detection model, making it difficult to focus on defect features. Third, there is insufficient perception of minute defects, resulting in low detection accuracy for a large number of tiny defects on PCBs. In addition, while some semi-supervised methods attempt to utilize unlabeled data, their reliance on the initialization of pre-trained weights limits their adaptive learning to the characteristics of the PCB industry. Commonly used detection models, such as the DETR series, cannot even converge in scenarios with a high proportion of defect-free samples, while the YOLO series models, although stable in performance, have limited detection capabilities. Summary of the Invention
[0004] In view of the above-mentioned defects or improvement needs of the existing technology, the present invention provides a PCB surface defect detection method and equipment, which aims to solve the problem of low detection accuracy caused by unstable training in the existing detection methods.
[0005] To achieve the above objectives, according to one aspect of the present invention, a method for detecting surface defects on a PCB is provided, comprising the following steps: inputting an image of the PCB to be detected into a defect detection model, wherein the defect detection model performs defect detection based on the received image; wherein the decoder of the defect detection model is equipped with a semantic background separation mechanism, wherein the semantic background separation mechanism filters and separates defect features and non-defect features through CLS and IoU dual scoring, the separated defect features enter the next layer of filtering, and regularization loss is used to suppress non-defect features.
[0006] Furthermore, the training dataset corresponding to the defect detection model is composed of a combination of defective samples and non-defective samples.
[0007] Furthermore, an overlapping image segmentation method is used to pre-segment the PCB image to obtain segmented small images. The segmented small images are then randomly sampled or resampled to obtain defect-free samples. The defect-free samples are combined with defective samples to form a training dataset.
[0008] Furthermore, the defect detection model also includes a backbone network and an encoder. A semantic background separation mechanism is deployed at the input and output of each layer of the decoder network to perform feature separation on the Query vector output by the encoder and pass the defect Query into the subsequent decoder layer.
[0009] Furthermore, the defect detection model uses the DETR model as the basic defect detection model, and deploys a semantic background separation mechanism in the decoder. The mixed features output by the backbone network and encoder are transformed into query vectors containing semantic information, i.e., Query vectors, through linear projection. Each Query vector corresponds one-to-one with the final output defect prediction bounding box. For the extracted Query vectors, the IoU score and CLS score corresponding to each Query are calculated, and the Query vectors are sorted in descending order of CLS score to obtain:
[0010] Sort by IoU score in descending order:
[0011] In the formula, This is the query vector sorted according to CLS scores; This is the query vector before sorting. These represent the CLS score ranking and the IoU score ranking, respectively. and These are the query vectors for CLS scores and IoU scores, respectively; set the filtering threshold: This represents the number of true rectangular bounding boxes in a single image. =10, which is the optimal tolerance range for IoU selection. =20, which is the tolerance range for non-optimal IoU screening, and a two-layer screening is performed.
[0012] Furthermore, the formula used by the semantic context separation mechanism for two-layer filtering is as follows:
[0013]
[0014] in A collection of defect query vectors. This is a collection of non-defect query vectors.
[0015] Furthermore, a small defect perception method guided by intersection boxes is used to enhance the defect features. The loss function used in the intersection box-guided small defect perception method is:
[0016] In the formula, A is the true bounding box; B is the predicted bounding box. Let A represent the diagonal length, width, and height of the intersection box of A and B, respectively. The exponential enhancement coefficient, To prevent the calculation of abnormal constants, Represents the squared Euclidean distance; These are the center points of A and B, respectively; These represent the widths of the predicted rectangle A and the actual rectangle B, respectively. These represent the heights of the predicted rectangle A and the actual rectangle B, respectively.
[0017] Furthermore, the total loss function of the defect inspection model is:
[0018] in, Varifocal loss, To predict the L1 loss between the bounding box and the true bounding box, the formula is:
[0019] in, The x and y coordinates of the center point of the rectangle are: The absolute width and height of the rectangle, superscript. This indicates that the variable with this subscript is the predicted output value of the defect detection model, and the corresponding rectangle is the predicted rectangle. The superscript... This indicates that the variable with the superscript is the true value manually labeled in the training set, and the corresponding rectangle is the true rectangle. and These are the loss terms for the semantic background separation mechanism and the intersection box-guided minor defect perception method, respectively.
[0020] The present invention also provides a PCB surface defect detection system based on defect-free sample feature enhancement. The system includes a memory and a processor. The memory stores a computer program, and the processor executes the computer program to perform the PCB surface defect detection method described above.
[0021] The present invention also provides a computer-readable storage medium storing machine-executable instructions, which, when invoked and executed by a processor, cause the processor to implement the PCB surface defect detection method as described above.
[0022] In summary, compared with the prior art, the PCB surface defect detection method and equipment provided by this invention have the following advantages: 1. This invention employs a semantic background separation mechanism to accurately separate defect features and non-defect features, solving the problems of unstable training of defect detection models and easy confusion between background and defects under a high proportion of non-defect samples. This ensures that the defect detection model focuses on learning core defect features, thereby improving detection accuracy.
[0023] 2. This invention proposes a method for perceiving minute defects. By using the intersection of the predicted bounding box and the actual bounding box as guidance, it further enhances the defect detection model's ability to learn features of insignificant defects, significantly improves the sensitivity of the defect detection model to perceiving minute defects, and solves the problems of insufficient gradient feedback and low localization accuracy of existing loss function IoU calculation methods for minute defects.
[0024] 3. This invention makes full use of the pattern information of abundant and low-cost defect-free samples in industrial scenarios. By image segmentation and data augmentation, a high proportion of defect-free sample training set is constructed. The data scale can be expanded without additional annotation costs, solving the pain points of existing technologies that rely on scarce labeled defect samples and have high data costs.
[0025] 4. Defect features enter the lower network and are filtered again. L2 regularization loss is applied to non-defect features to guide the defect detection model to propagate smaller gradients. Based on this, the weights of non-defect features are reduced through regularization penalty to suppress background interference.
[0026] 5. Deploy the semantic background separation mechanism at the input or output of the decoder network layer, pass the defect query into the subsequent decoder layer for iterative optimization, realize feature purification in the multi-layer decoder, improve the training efficiency and stability of the defect detection model, and ultimately ensure that the defect detection model can focus on learning defect features in a high proportion of defect-free sample datasets and can obtain gains from defect-free features. Attached Figure Description
[0027] Figure 1 This is a schematic diagram of the feature enhancement of defect-free samples provided in an embodiment of the present invention; Figure 2 This is a schematic diagram of the semantic background separation mechanism provided in an embodiment of the present invention; Figure 3 yes Figure 2 A schematic diagram of feature separation for semantic context separation in [the context of the text]. Figure 4 This is a schematic diagram illustrating the principle of intersection box-guided micro-defect perception provided in an embodiment of the present invention; Figure 5 This is a block diagram of the PCB defect detection model with enhanced features of defect-free samples provided in this embodiment of the invention. Detailed Implementation
[0028] To make the objectives, technical solutions, and advantages of this invention clearer, the invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the invention. Furthermore, the technical features involved in the various embodiments of this invention described below can be combined with each other as long as they do not conflict with each other.
[0029] This invention provides a PCB surface defect detection method. The defect detection method addresses the problem of wasted value of defect-free samples by providing a complete optimization scheme from training to detection. Combined with the characteristics of PCB defects, it forms a defect detection model with enhanced features of defect-free samples. Experiments have verified that this defect inspection method can effectively utilize the feature information of defect-free samples and improve the detection accuracy of the defect detection model for PCB surface defects.
[0030] Please see Figure 1The defect detection method constructs an overlapping image segmentation method in the data preprocessing stage, making full use of the high proportion of defect-free samples in industrial scenarios. In the feature learning stage, a semantic background separation mechanism is introduced to solve the training instability under the high proportion of defect-free samples. In the defect detection stage, an intersection box-guided micro-defect perception method is designed to improve the detection accuracy of micro-defects. Finally, a real-time detection framework integrating dual core mechanisms is built to balance detection speed and accuracy, solving the pain points of existing technologies such as reliance on labeled defect samples, missed detection of micro-defects, and unstable training.
[0031] Please see Figure 5 The defect detection method mainly includes the following steps: inputting an image of the PCB to be detected into a defect detection model, which performs defect detection based on the received image; wherein, the decoder of the defect detection model is equipped with a semantic background separation mechanism, which filters and separates defect features and non-defect features through CLS and IoU dual scoring, the separated defect features enter the next layer of filtering, and regularization loss is used to suppress non-defect features.
[0032] The training dataset corresponding to the defect detection model is composed of a combination of defective samples and defect-free samples. Specifically, an overlapping image segmentation method is used to pre-segment the PCB image to obtain segmented small images. The segmented small images are then randomly sampled or resampled to obtain defect-free samples. The defect-free samples are combined with the defective samples to form the training dataset.
[0033] In this invention, addressing the problem of existing technologies relying on scarce and expensive labeled defect samples and incurring high data costs, a high-resolution PCB image is segmented into sizes suitable for the defect detection model through image segmentation preprocessing. A large number of defect-free samples are obtained by random sampling or resampling of the segmented images and mixed with defective samples to construct a training dataset with a high proportion of defect-free samples. This expands the scale of the training data without increasing labeling costs. The proportion of defect-free samples in the training dataset is 60%-80%. Data augmentation processing, including random color distortion, flipping, scaling, cropping, and boundary expansion, is then applied to the segmented images.
[0034] This invention uses the PKU-PCB public dataset as both training and validation datasets, focusing on detecting six common PCB defects: missing vias, rodent bites, open circuits, short circuits, burrs, and fake copper. These six defect types cover the main quality problems in PCB production, and the defect-free sample rate is higher than 70%, meeting the conditions for general defect detection scenarios and possessing strong industrial representativeness, thus ensuring the practical application value of this embodiment.
[0035] The PKU-PCB dataset contains 693 high-resolution PCB images, covering the six types of defects mentioned above. Since the original images have varying resolutions, this embodiment first scales all original images to 3072×2400 pixels to ensure image detail integrity. Then, this embodiment uses an overlapping image segmentation method for preprocessing: the size of the segmented small images is set to 640×640 pixels, with an overlap rate of 20%. Segmentation is performed using a sliding window method from left to right and top to bottom, with a window step size of 512 pixels, ensuring that each defect is completely contained in at least one small image, avoiding defects being cut off by segmentation edges. After segmentation, the small images need to be filtered, retaining those containing defects as "defect samples" and randomly selecting those without any defects as "defect-free samples," ensuring that the proportion of defect-free samples meets the requirements of industrial scenarios.
[0036] This embodiment constructs two core training sets: a 10-shot scenario, which includes 10 labeled samples for each type of defect, comprising 454 defective samples and 1292 defect-free samples, with defect-free samples accounting for 74% (1292 / (454+1292)); and a 30-shot scenario, comprising 1310 defective samples and 3323 defect-free samples, with defect-free samples accounting for 72%. Simultaneously, to verify the adaptability of the defect detection model to different proportions of defect-free samples, auxiliary training sets with defect-free sample proportions of 60%, 70%, and 80% are constructed through random sampling or resampling. The validation set for all training sets is fixed at 15239 samples.
[0037] To enhance the feature diversity of the training set, this embodiment performs data augmentation on all segmented small images, including random color distortion (brightness adjustment range ±15%, contrast adjustment range ±20%, saturation adjustment range ±15%), 50% probability of horizontal / vertical random flipping, 0.8-1.2x random scaling, random cropping, and boundary expansion. All augmented images are normalized to the [0,1] interval, with pixel values divided by 255 to ensure consistency of the input data for the defect detection model.
[0038] To address the issues of defect detection models easily confusing defect features with background features and unstable training when dealing with a high proportion of defect-free samples, and to obtain feature enhancement effects from defect-free samples, such as... Figure 2 As shown, by using the IoU-CLS dual scoring and dual-layer screening mechanism, defect features and non-defect features are separated and different learning strategies are adopted for each, giving the defect detection model the ability to enhance the features of non-defect samples.
[0039] The defect detection model includes a backbone network, an encoder, and a decoder. The semantic background separation mechanism is deployed at the input and output of each layer of the RTDETR model decoder. After feature separation and suppression of the query vector output by the encoder, the defect query is passed to the subsequent decoder layer.
[0040] To address the issue of defect detection models easily confusing defect features with similar background features and leading to training instability when there is a high proportion of defect-free samples, the semantic background separation mechanism uses CLS and IoU dual scoring to accurately distinguish defect features that are highly correlated with real PCB defects and beneficial to the performance of the defect detection model, and non-defect features that are unrelated to real PCB defects and harmful to the performance of the defect detection model. Different learning strategies are adopted for defect features and non-defect features. Defect features enter the next layer of screening process, while non-defect features are suppressed through regularization loss, ensuring training stability under a high proportion of defect-free samples and reducing the misclassification rate of defect-free regions.
[0041] Please see Figure 3 The defect detection model uses the Detection Transformer (DETR) model as the basic defect detection model, and deploys a semantic background separation mechanism in the decoder. For the mixed features output by the DETR backbone network and encoder, these mixed features are transformed into query vectors containing semantic information, i.e., Query vectors, through linear projection. Each Query vector corresponds one-to-one with the final output defect prediction bounding box. For the extracted Query vectors, an IoU-CLS dual-scoring filtering mechanism is designed: the IoU score and CLS score corresponding to each Query are calculated. The IoU score is defined as the intersection-union ratio (IoU) between the defect prediction bounding box and the ground truth bounding box corresponding to the Query, reflecting the objective positional matching degree. The CLS score is defined as the confidence score of the category prediction output by the defect detection model's classification head for the Query vector, reflecting the subjective probability of the defect. The Query vectors are sorted in descending order of CLS score to obtain:
[0042] Sort by IoU score in descending order:
[0043] In the formula, This is the query vector sorted according to CLS scores; This is the query vector before sorting. represents the CLS score ranking and the IoU score ranking, respectively, where smaller values for i and j indicate a stronger correlation with the defect. and These are the query vectors for CLS scores and IoU scores, respectively; set the filtering threshold: This represents the number of true rectangular bounding boxes in a single image. =10, which is the optimal tolerance range for IoU selection. =20, which is the tolerance range for non-optimal IoU screening. A two-layer screening is performed, and the corresponding formula is:
[0044]
[0045] in A collection of defect query vectors. It is a collection of non-defect query vectors, which can accurately distinguish between real defect features and easily confused non-defect features, thus solving the problem of misjudgment caused by single-rating screening.
[0046] Different learning strategies are adopted for the separated defect queries and non-defect queries. Defect queries are fed into the lower-level network and their features are filtered again. L2 regularization loss is applied to non-defect queries to guide the defect detection model to propagate smaller gradients. The L2 regularization loss is included in the total loss function, and its formula is as follows:
[0047] In the formula, λ = 0.02, and n is the total number of non-defect queries. Based on this, regularization penalties are applied to attenuate the weights of non-defect features and suppress background interference. The semantic-background separation mechanism is deployed at the input or output of the decoder network layer. Defect queries are fed into subsequent decoder layers for iterative optimization. Feature purification is achieved in multi-layer decoders, improving the training efficiency and stability of the defect detection model. Ultimately, this ensures that the defect detection model can focus on learning defect features in a high-proportion dataset of non-defect samples and can gain benefits from non-defect features.
[0048] In one implementation, the training set images are input into the RTDETR backbone network CSPDarknet and encoder to extract multi-scale feature maps. The encoder outputs 300 query vectors, each corresponding to a predicted bounding box in the final output. The queries are transformed into Q, K, and V through a linear projection operation, each with dimensions of 300×256, calculated as follows:
[0049]
[0050] In the formula, For the input image, The function is used to filter the top-300 feature vectors from the encoder output. It is a linear projection layer with an input dimension of 256 and an output dimension of 256.
[0051] The IoU score and CLS score are calculated. The CLS score is the confidence of the defect detection model in predicting the category of each query. It is output by the classification head of the decoder and ranges from [0,1]. The higher the score, the greater the probability that the defect detection model believes that the region corresponding to the query is a defect. The IoU score is the intersection-union ratio between the predicted rectangle and the real rectangle corresponding to the query. It is calculated by the small defect perception method and ranges from [0,1]. The higher the score, the higher the objective position matching degree between the predicted rectangle and the real rectangle.
[0052] Subsequently, a two-layer feature filtering mechanism for semantic context separation is used to separate features. First, the 300 queries are sorted in descending order by CLS score and then by IoU score:
[0053]
[0054] in This represents the query sequence based on CLS scores. This represents the query sequence sorted by IoU score. , The smaller the value, the higher the defect score.
[0055] The results were obtained through double-layer intersection filtering:
[0056]
[0057] in For the defect query collection, This is a non-defect query collection. This represents the number of true rectangular bounding boxes in a single image. The optimal tolerance range for IoU is set to 10. The tolerance range for suboptimal IoU is set to 20. Combined with... Figure 3 The query set is filtered by CLS score and IoU score to identify queries with a high probability of defects, and then... To refine the boundary, defect queries and easily confused queries (non-defect queries) are identified. Among them, defect queries are the features that the defect detection model needs to focus on learning and are the main features that the model learns. Easily confused queries are suppressed from affecting the defect detection model and are used for auxiliary feature enhancement learning.
[0058] Different learning strategies are used for defective and non-defective features. For defective queries, they are fed into the next layer decoder to continue mixing and filtering with non-defective queries. For non-defective queries, L2 regularization loss is applied to suppress their feature weights. The semantic context separation loss is λ=0.02, where n is the number of non-defect queries. By incorporating the total loss function of the defect detection model, the weights of non-defect features are attenuated through backpropagation, thereby reducing background interference.
[0059] Please see Figure 4 Based on EIoU, the size of the intersection of the predicted and true bounding boxes is used as the denominator, and α-IoU is introduced to amplify the gradient during backpropagation, resulting in a small defect perception loss function guided by the intersection box:
[0060] In the formula, A is the true bounding box; B is the predicted bounding box. Let A represent the diagonal length, width, and height of the intersection box of A and B, respectively. The exponential enhancement coefficient, To prevent the calculation of abnormal constants, Represents the squared Euclidean distance; These are the center points of A and B, respectively; These represent the widths of the predicted rectangle A and the actual rectangle B, respectively. These represent the heights of the predicted rectangle A and the actual rectangle B, respectively.
[0061] Specifically, firstly, we analyze the limitations of traditional EIoU loss, which is expressed as:
[0062] Where A and B represent the predicted bounding box and the actual bounding box of the defect, respectively. and Let A and B represent the center points respectively. These represent the width and height of the rectangle, respectively. Represents the square of the Euclidean distance. This represents the length of the diagonal of the smallest enclosing rectangle of A and B. and Let A and B represent the width and height of the minimum enclosing rectangles of A and B, respectively. These represent the widths of the predicted rectangle A and the actual rectangle B, respectively. These represent the heights of the predicted rectangle A and the actual rectangle B, respectively.
[0063] In the case of minor defects, when the predicted bounding box A completely encloses the ground truth bounding box B, the minimum bounding box of A and B coincides with the predicted bounding box A. This makes the center distance loss term in the loss function insensitive, failing to effectively guide the optimization of the defect detection model. This invention replaces the minimum bounding box size of A and B with the intersection size of the predicted bounding box A and the ground truth bounding box B as the denominator in the loss function calculation, and introduces... -IoU is used as the exponential coefficient of each term in the loss function to enhance the perception of minute defects, resulting in:
[0064] In the formula, Let A represent the diagonal length, width, and height of the intersection box of A and B, respectively. =1 is used to avoid calculation anomalies when the number of pixels in the intersection box is less than 1, and α=3 is the exponential enhancement coefficient of each loss term. The technical effect of this step is that when the predicted rectangular box surrounds a small defect, the denominator is adapted to the defect size, which significantly amplifies the loss value and enhances the sensitivity of the defect detection model to the positional deviation of small defects.
[0065] The defect perception guided by the intersection box is essentially an IoU calculation method, which also participates in the IoU score calculation in the semantic background separation mechanism. This achieves synergistic optimization between the semantic background separation mechanism and the micro-defect perception method, making feature selection more aligned with the needs of micro-defect detection and further improving overall detection performance.
[0066] In one specific implementation, to address the problem that defect detection models struggle to accurately learn insignificant defect features in datasets with a high proportion of defect-free samples, the sensitivity of the defect detection model to insignificant features is enhanced by improving the calculation method of the loss function. Specifically, this embodiment improves upon the EIoU loss function and employs the Intersection Box Guided Minimal Defect Perception (IBGP) method; it uses the open-source architecture of RTDETR, with 8 heads in the multi-head self-attention mechanism and a hidden layer dimension of 256; the decoder is a 3-layer Transformer decoder, outputting 300 predicted bounding boxes, with each layer containing a multi-head cross-attention mechanism and a feedforward network.
[0067] The collaborative workflow of semantic background separation and intersection box-guided minor defect perception loss is as follows: 1. The input image passes through the entire defect detection model, the encoder outputs a query, and the detection head outputs a predicted bounding box; 2. The IoU score and CLS score between the predicted bounding box and the real bounding box are calculated through the semantic background separation mechanism. The Query output by the encoder is then double-scored and double-filtered to suppress non-defective features. 3. The minute defect perception method uses the intersection box to guide the calculation of the intersection box size between the predicted rectangle and the real rectangle, and obtains the IBGP loss; 4. Backpropagate the total loss function to optimize the parameters of the entire network.
[0068] This invention addresses the shortcomings of existing frameworks that fail to balance real-time performance and high accuracy, and cannot effectively integrate features from defect-free samples. It integrates a semantic background separation mechanism with a bounding box-guided method for detecting minute defects, achieving effective utilization of defect-free sample features. While maintaining a real-time detection speed of FPS ≥ 98, it improves detection accuracy and achieves stable convergence even when defect-free samples account for 80%, fully adapting to the needs of industrial production lines. Specifically, firstly, the RTDETR model is used as the basic defect detection model to utilize its real-time detection characteristics. Secondly, high-resolution images are segmented to obtain smaller images adapted to the model's input size. These smaller images contain a large number of defect-free samples and a small number of defective samples, which are input into the backbone network and encoder of the defect detection model, outputting multi-scale features. Subsequently, the semantic background separation mechanism separates and suppresses the mixed features output by the encoder. On the one hand, L2 regularization loss is calculated to suppress non-defective features; on the other hand, defective features are retained and proceed to the next layer of feature selection. The semantic background separation mechanism performs feature filtering at both the input and output of each decoder layer. This process, involving three decoder layers and four layers of semantic background separation, outputs clean defect features, accurately determining the defect category, bounding box coordinates, and confidence score. Finally, the minor defect perception method calculates the loss based on the intersection-union ratio (IUU) of the predicted and ground truth bounding boxes, and backpropagates to optimize the entire defect detection model, strengthening the learning of minor defect features and improving localization accuracy.
[0069] The total loss function of the defect inspection model is:
[0070] in, The variable loss is used to enhance the learning of high-quality positive samples and suppress simple negative samples through dynamic weight allocation. The bounding box L1 loss is calculated using the following formula:
[0071] in, The x and y coordinates of the center point of the rectangle are: The absolute width and height of the rectangle, superscript. This indicates that the variable with this subscript is the predicted output value of the defect detection model, and the corresponding rectangle is the predicted rectangle. The superscript... This indicates that the variable with this index is the actual value manually labeled within the training set, and the corresponding bounding box is the actual bounding box.
[0072] Finally, the accuracy of the predicted rectangle's coordinates and dimensions is optimized; and The loss terms for the semantic context separation mechanism and the minor defect perception method are respectively adjusted by λ=0.02. The magnitude is controlled to ensure the balance of each loss component. Through training, the defect detection model acquires the ability to adapt to both defective and non-defective features. It can stably learn defective samples in a training set with a high proportion of non-defective samples, and can further enhance the detection accuracy of the defect detection model using non-defective samples, thus achieving the dual goals of stable training and high-precision detection.
[0073] The defect detection model directly utilizes the adaptive capabilities of defect and non-defect features acquired during the training process to accurately detect defective and non-defective samples. At this time, it does not perform loss calculations for semantic background separation or intersection box-guided loss calculations for minor defects. It does not increase computation time during deployment and can meet the actual real-time detection requirements.
[0074] The present invention also provides a PCB surface defect detection system based on defect-free sample feature enhancement. The system includes a memory and a processor. The memory stores a computer program, and the processor executes the computer program to perform the PCB surface defect detection method described above.
[0075] The present invention also provides a computer-readable storage medium storing machine-executable instructions, which, when invoked and executed by a processor, cause the processor to implement the PCB surface defect detection method as described above.
[0076] Those skilled in the art will readily understand that the above description is merely a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.
Claims
1. A method for detecting defects on the surface of a PCB, characterized in that, The steps are as follows: An image of the PCB to be inspected is input into the defect detection model, which performs defect detection based on the received image. The decoder of the defect detection model is equipped with a semantic background separation mechanism. The semantic background separation mechanism filters and separates defect features and non-defect features through CLS and IoU dual scoring. The separated defect features enter the next layer of filtering, and regularization loss is used to suppress non-defect features.
2. The PCB surface defect detection method as described in claim 1, characterized in that: The training dataset corresponding to the defect detection model is composed of a combination of defective samples and non-defective samples.
3. The PCB surface defect detection method as described in claim 2, characterized in that: An overlapping image segmentation method is used to pre-segment the PCB image to obtain segmented small images. The segmented small images are then randomly sampled or resampled to obtain defect-free samples. The defect-free samples are combined with defective samples to form a training dataset.
4. The PCB surface defect detection method as described in claim 1, characterized in that: The defect detection model also includes a backbone network and an encoder. A semantic background separation mechanism is deployed at the input and output of each layer of the decoder network to perform feature separation on the Query vector output by the encoder and pass the defect Query into the subsequent decoder layer.
5. The PCB surface defect detection method as described in claim 4, characterized in that: The defect detection model uses the DETR model as the basic defect detection model, and deploys a semantic background separation mechanism in the decoder. The mixed features output by the backbone network and encoder are transformed into query vectors containing semantic information, i.e., Query vectors, through linear projection. Each Query vector corresponds one-to-one with the final output defect prediction bounding box. For each extracted Query vector, the IoU score and CLS score are calculated, and the Query vectors are sorted in descending order of CLS score to obtain: Sort by IoU score in descending order: In the formula, This is the query vector sorted according to CLS scores; This is the query vector before sorting. These represent the CLS score ranking and the IoU score ranking, respectively. and These are the query vectors for CLS scores and IoU scores, respectively; set the filtering threshold: This represents the number of true rectangular bounding boxes in a single image. =10, which is the optimal tolerance range for IoU selection. =20, which is the tolerance range for non-optimal IoU screening, and a two-layer screening is performed.
6. The PCB surface defect detection method as described in claim 5, characterized in that: The formula used by the semantic context separation mechanism for two-layer filtering is: in A collection of defect query vectors. This is a collection of non-defect query vectors.
7. The PCB surface defect detection method according to any one of claims 1-6, characterized in that: A small defect perception method guided by intersection boxes is used to enhance defect features. The loss function used in this method is: In the formula, A is the true bounding box; B is the predicted bounding box. Let A represent the diagonal length, width, and height of the intersection box of A and B, respectively. The exponential enhancement coefficient, To prevent the calculation of abnormal constants, Represents the squared Euclidean distance; These are the center points of A and B, respectively; These represent the widths of the predicted rectangle A and the actual rectangle B, respectively. These represent the heights of the predicted rectangle A and the actual rectangle B, respectively.
8. The PCB surface defect detection method as described in claim 7, characterized in that: The total loss function of the defect inspection model is: in, Varifocal loss, To predict the L1 loss between the bounding box and the true bounding box, the formula is: in, The x and y coordinates of the center point of the rectangle are: The absolute width and height of the rectangle, superscript. This indicates that the variable with this subscript is the predicted output value of the defect detection model, and the corresponding rectangle is the predicted rectangle. The superscript... This indicates that the variable with the superscript is the true value manually labeled in the training set, and the corresponding rectangle is the true rectangle. and These are the loss terms for the semantic background separation mechanism and the intersection box-guided minor defect perception method, respectively.
9. A PCB surface defect detection system based on defect-free sample feature enhancement, characterized in that: The system includes a memory and a processor. The memory stores a computer program, and when the processor executes the computer program, it performs the PCB surface defect detection method according to any one of claims 1-8.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores machine-executable instructions, which, when invoked and executed by a processor, cause the processor to implement the PCB surface defect detection method according to any one of claims 1-8.