Capacitor defect detection method based on YOLOv5 model
By introducing multi-threshold screening and size measurement mechanisms into the YOLOv5 model, combined with a two-stage fine-tuning strategy, the problems of sample imbalance and inaccurate size measurement in capacitive screen printing are solved, improving the accuracy and reliability of defect detection and making it suitable for industrial production.
Patent Information
- Application Number
- CN202511500059.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-10-21
- Publication Date
- 2025-11-18
- Estimated Expiration
- 2045-10-21
AI Technical Summary
The existing YOLOv5 model suffers from sample imbalance in capacitor screen printing defect detection, making it difficult to meet the detection needs of different defect types. Furthermore, the defect size measurement is inaccurate, failing to meet the high precision requirements of industrial production.
By employing a multi-threshold setting and defect size precision measurement mechanism, combined with a two-stage fine-tuning method, and by freezing the backbone network (Backbone layer) and fine-tuning the Neck and Head layers, the Adam optimizer and multi-threshold screening mechanism are used to improve detection accuracy and reliability.
It effectively alleviates the problem of sample imbalance, improves the detection capability of rare defects, achieves high-precision defect judgment with low false alarms, and meets the refined detection requirements of industrial sites.
Smart Images

Figure CN120976220A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the fields of machine vision and deep learning, and in particular to a method for detecting capacitor defects based on the YOLOv5 model. Background Technology
[0002] With the rapid development of the electronics manufacturing industry, capacitors, as one of the most important electronic components, face increasingly stringent quality control requirements during production. Screen printing is a crucial process in capacitor manufacturing, and its quality directly impacts the performance of the final product. During the printing process, various defects may occur, such as black spots, white spots, bumps, enlargements, and edge bleeding. These defects not only affect the appearance of the capacitor but may also affect its electrical performance, leading to decreased product reliability. Traditional defect detection methods mainly rely on manual visual inspection, which suffers from low efficiency, high cost, and strong subjectivity. In recent years, with the development of computer vision and deep learning technologies, deep learning-based defect detection methods have gradually become a research hotspot.
[0003] YOLOv5, as one of the most popular real-time object detection algorithms in current industrial scenarios, has demonstrated excellent performance in multiple fields. However, for the specific application scenario of capacitor screen printing defect detection, the standard YOLOv5 model has the following shortcomings: First, capacitor defects are diverse, with significant differences in characteristics between different types, making it difficult to meet the detection needs of all types with a single threshold. For example, although black spot and white spot defects are both point-like defects, their characteristics differ significantly, requiring different detection parameters. Similarly, although bulges and enlarged defects are both area-type defects, their edge features and internal structures differ considerably, necessitating different detection strategies.
[0004] Secondly, the YOLOv5 model can only roughly estimate defect size using the length and width of the detection frame, which cannot meet the precise requirements for defect size in industrial production. In actual production, accurate measurement of defect size is crucial for determining whether a defect affects capacitor performance. For example, for bulky defects, if the size exceeds a certain range, it may affect the capacitor's capacitance and withstand voltage performance; for edge defects, if the edge width exceeds a certain value, it may affect the capacitor's insulation performance.
[0005] Finally, the sample imbalance problem is severe. In actual production, some defect types (such as black spots) occur frequently and are easy to sample in large quantities; while other defect types (such as protrusions with special shapes) occur less frequently and are difficult to sample sufficiently. This sample imbalance problem causes the model to perform well in detecting common defects but poorly in detecting rare defects, affecting the overall detection performance. Summary of the Invention
[0006] In order to overcome the above-mentioned shortcomings and deficiencies of the prior art, the purpose of this invention is to provide a capacitor defect detection method based on the YOLOv5 model.
[0007] This invention effectively improves the accuracy and reliability of defect detection by introducing multiple threshold settings and a precise defect size measurement mechanism, as well as employing a two-stage fine-tuning method.
[0008] The objective of this invention is achieved through the following technical solution: A capacitor defect detection method based on the YOLOv5 model includes the following steps: Step 1: Train the YOLOv5 model using the first dataset to obtain a pre-trained model; Step 2 uses the second dataset to train the pre-trained model to obtain the capacitor defect detection model; Step 3: Input the capacitor image into the capacitor defect detection model to perform defect detection, and obtain the location, category, and confidence level of all capacitor defects in the input capacitor image; Step four employs a multi-threshold setting mechanism to further screen the defects detected in step three, ultimately identifying defects that do not meet industrial production quality requirements.
[0009] Furthermore, both the first and second datasets consist of images of capacitor defects collected on-site during screen printing. The two datasets have the same number of samples and the same defect types, including black spots, white spots, bumps, bleeding edges, and enlargements.
[0010] Furthermore, the first dataset has its labels for protruding defects commented out, and only includes four types of defect labels, while the second dataset includes five types of defect labels.
[0011] Furthermore, the YOLOv5 model includes a Backbone layer, a Neck layer, and a Head layer.
[0012] Furthermore, the second dataset is used to train the pre-trained model, specifically by freezing the Backbone layer and fine-tuning only the Neck and Head layers.
[0013] Furthermore, the fine-tuning training used Adam as the optimizer, with a learning rate of 0.001.
[0014] Furthermore, a multi-threshold setting mechanism is adopted to screen for defects in target detection, specifically including a confidence threshold screening stage and a secondary threshold screening stage.
[0015] Furthermore, the confidence threshold screening stage includes: First Reset Reliability Screening: Set a general confidence threshold that applies to all categories of defects; Second Reset Reliability Screening: Set a confidence threshold for each defect and screen the results of the first reset reliability screening again.
[0016] Furthermore, the secondary threshold screening uses a method based on pixel size and variance measurement to determine the defect type.
[0017] Furthermore, for black and white spot defects, the pixel length that meets the defect grayscale value threshold is calculated, and the size thresholds of black and white spots in both directions are determined by horizontal and vertical measurements. For protrusions, seepage, and thickening defects, calculate the variance of gray values for all rows and columns in each direction for each type of defect, and set a variance threshold.
[0018] Compared with the prior art, the present invention has the following advantages and beneficial effects: 1) Effectively alleviates sample imbalance and improves the ability to detect rare defects: This invention employs a two-stage fine-tuning strategy. In the first stage, the model is pre-trained using a class-balanced first dataset to enable it to initially grasp the general features of various defects. In the second stage, the model is fine-tuned using a complete but severely imbalanced second dataset, and the backbone network is frozen to prevent high-frequency defects (such as bulges) from dominating the learning of low-level features. This preserves the ability to identify low-frequency defects such as black spots, white spots, and seepage edges, significantly improving the model's generalization performance in scenarios with extremely imbalanced samples.
[0019] 2) Introduce a multi-threshold screening mechanism to achieve high-precision, low-false-positive defect identification: Traditional YOLOv5 models rely solely on a single confidence threshold for post-processing, making it difficult to balance the detection sensitivity and accuracy across different defect types. This invention designs a dual-reset confidence threshold mechanism of "general initial screening + category-customized fine screening," combined with secondary threshold screening based on pixel size and grayscale variance. This allows for setting separate discrimination criteria for small targets such as black and white dots, as well as large-scale defects such as protrusions and bulky areas, effectively reducing missed detections and false alarms, and meeting the refined defect judgment requirements of industrial settings. Attached Figure Description
[0020] Figure 1 This is a flowchart of the process of this invention; Figure 2 This is the distribution of capacitor defects in the dataset of this invention; Figure 3 This is a flowchart of the detection process for capacitive input images according to the present invention. Detailed Implementation
[0021] The present invention will be further described in detail below with reference to the embodiments, but the implementation of the present invention is not limited thereto.
[0022] Example like Figures 1-3 As shown, a capacitor defect detection method based on the YOLOv5 model includes the following steps: Step 1: Train the YOLOv5 model using the first dataset to obtain a pre-trained model; Step 2 uses the second dataset to train the pre-trained model to obtain the capacitor defect detection model; Step one, the pre-training phase in this embodiment, selects four of the most representative defect types—black spots, white spots, edge bleeding, and hypertrophy—to ensure a relatively balanced number of samples for each defect type, thus avoiding sample bias in the initial training phase. The above process can be expressed by the following formula:
[0023] D 1 It is the dataset used in pre-training. N Indicates the number of samples. xi Indicates a capacitor sample. Tags indicating only some categories, { θ b ,θ n ,θ h} represents the parameters of the backbone, neck, and head layers of the YOLOv5 model. θ (0) This indicates parameters that are randomly initialized. T This indicates the number of model iterations.
[0024] Step two is the fine-tuning stage of pre-training in this embodiment. The present invention adopts a progressive optimization strategy. The second dataset introduces complete data on the five types of defects to achieve full coverage of defect types.
[0025] The incremental optimization strategy involves freezing the parameters of the Backbone layer and fine-tuning only the Neck and Head layers. This preserves the general feature extraction capabilities of the pre-trained model while providing targeted optimization for the capacitor defect detection task.
[0026] During the fine-tuning phase, this invention employs the Adam optimizer and reduces the initial learning rate from 0.01 in the pre-training phase to 0.001. The advantage of this strategy is that the Adam optimizer can adaptively adjust the learning rate of each parameter, helping the model converge faster; while the lower learning rate ensures more precise parameter adjustments during the fine-tuning phase, avoiding training instability caused by an excessively high learning rate, thus better preserving the features learned in the pre-training phase, while simultaneously optimizing for the capacitance defect detection task.
[0027] To further explain, such as Figure 2 As shown, both the first and second datasets consist of images of capacitor defects collected on-site during screen printing, covering five types of defects: black spots, white spots, bulges, bleeding edges, and enlargements. According to the deep learning training scheme, the datasets are divided into training, validation, and test sets in a 7:1:2 ratio.
[0028] The first and second datasets have the same number of samples, but the number of defects in each category differs during training. The number of defects is determined by the labels; an image often has multiple defects. In the pre-training stage, only four categories of defect labels are available, with convexity labels being commented out. In the fine-tuning stage, labels for all five defect categories are available.
[0029] The dataset contains approximately 1258 images, and these 1258 images contain the following defects: Number of black spot defects: 14370; Number of white spot defects: 7329; Number of protruding defects: 139,644; Number of edge defects: 5610; Number of hypertrophic defects: 1176; Features: 1) Multi-scale and small target defects: Black and white dots are small in size, occupying less than 20 pixels, while the other three types of defects can reach up to 400 pixels; 2) The number of defect samples is extremely unbalanced, with the number of protruding defects far exceeding the number of defects in the other types.
[0030] To further explain, the specific structure of the YOLOv5 model is as follows: Backbone layer: Based on residual C3 module, it downsamples step by step and outputs three levels of feature maps: 8×, 16×, and 32×, which are used to capture complete information from fine-grained edges to semantic concepts.
[0031] Neck layer: Adopting the PANet structure, it first fuses high-level semantics from top to bottom, then supplements positional details from bottom to top, and finally simultaneously enhances the representation ability of the three levels of features.
[0032] The detection head (Head layer) predicts the center offset, width and height, and class confidence in parallel for three levels of features. In the post-processing stage, NMS is used to remove duplicates, and in the training stage, CIoU Loss + BCE are used for joint optimization to achieve accurate localization and classification of targets at multiple scales.
[0033] Furthermore, the model parameters of the capacitor defect detection model are as follows: , in, θ(T)' n express θ(T) n Changes occurred during this phase of training. θ(T) b (frozen) This indicates that the parameters at this stage are in a frozen state, unchanged, and in their initial state. θ(T) b same.
[0034] In this step, the pre-trained model has mastered the general features of capacitors (such as texture and edge) through the first dataset, but the excessive proportion of convex defects in the second dataset (accounting for more than 60% of the total number of defects) makes the model over-focus on this type of morphology; freezing the backbone layer can prevent the underlying features from being reconstructed by convex samples, that is, it can retain the ability to distinguish minority classes such as black spots, while greatly compressing the number of trainable parameters.
[0035] The Adam optimizer uses an adaptive mechanism of first-moment orientation and second-moment denoising to automatically suppress the dominant class gradient weights and make room for optimization of low-frequency defects such as edge bleeding; the small step learning rate of 0.001 adapts to the sensitive state of the model and can finely reshape the classification boundaries of the Neck layer and Head layer.
[0036] Step 3: Input the image into the capacitor defect detection model to obtain the target defect.
[0037] Step four employs a multi-threshold setting mechanism to filter target defects and obtain the final target defect type.
[0038] The multi-threshold setting mechanism includes a confidence threshold screening stage and a secondary threshold screening stage.
[0039] To further explain, the confidence threshold is used to filter and confirm the reliability of detection results. If the confidence score of a detected target is higher than the set threshold, the relevant target box will be retained. This mechanism helps reduce false alarms by retaining only detection results that the model is confident in. By adjusting the confidence threshold, optimization can be achieved between detection accuracy and detection range.
[0040] The confidence threshold screening stage specifically includes two phases: First Reset Confidence Screening: A general confidence threshold is set, which is 0.1 in this embodiment and applicable to all types of defects. The purpose of this setting is to perform preliminary screening of the detection results while ensuring that no correct detection results are missed. In this step, by filtering out most false positives, a broad focus on potential targets can be maintained.
[0041] The second confidence level screening: After the initial screening, the results undergo a more refined screening. In this stage, we set separate confidence thresholds for each defect category. For example, the threshold for black and white spots is set to 0.1, while the threshold for bumps and seepage edges is higher, set to 0.7, and the threshold for enlargements is 0.6. This customized setting is based on the characteristics of various defects, thereby optimizing the accuracy and robustness of detection. Through this hierarchical confidence level screening mechanism, reliable detection can be achieved while minimizing false alarms and accurately capturing the characteristics of various defects.
[0042] Secondary threshold screening stage: Defect type determination is achieved using a method based on pixel size and variance measurement. After confidence threshold screening, secondary threshold screening focuses on further evaluating the specific size and morphological characteristics of the detection results to meet the stringent identification criteria for various defects.
[0043] In this embodiment, for black spot defects, only those with a diameter exceeding 10 pixels are considered defects; those below this size are disregarded. Similar standards apply to white spots, bumps, seepage, and enlargements. Since these specific size criteria cannot be met through simple confidence threshold screening, a secondary threshold screening method based on pixel size and variance measurement is used for more accurate determination. Specific measures include: 1) For black and white spots, calculating the pixel length that meets the defect grayscale value threshold (different for black and white spots), and determining their dimensions in both horizontal and vertical directions through horizontal and vertical measurements; 2) For bumps, seepage, and enlargements, calculating the grayscale value variance of all rows and columns in each direction, and setting a variance threshold (different for the three types of defects), using this value to determine the specific size of the defect. This detection and determination system combines size and grayscale difference analysis, ensuring higher accuracy in defect location and confirmation.
[0044] In this embodiment, the grayscale value thresholds for black dots and white dots are set to 100 and 200, respectively; the variance thresholds for protrusions, seepage edges, and thick defects are set in the range of 20-50, respectively, and the preferred general threshold value in this embodiment is 45.
[0045] This invention employs a two-stage training approach. First, pre-training is performed using a balanced dataset to ensure the model possesses strong basic detection capabilities. Then, a tiered freezing and progressive learning rate strategy is used for fine-tuning, addressing the sample imbalance problem and significantly improving the model's detection performance and stability in real-world production environments. The proposed algorithm, based on the YOLOv5 detection model, significantly improves the overall accuracy of object detection compared to the original YOLOv5 model without this strategy, provided a two-stage self-fine-tuning strategy and a frozen backbone network. Specifically, the mean accuracy (intersection over union threshold of 0.5) of the proposed algorithm increases from 0.689 to 0.746, an improvement of 8.3%; the mean accuracy (intersection over union threshold range of 0.5 to 0.95) increases from 0.569 to 0.608, an improvement of 6.9%. The above results show that the proposed algorithm, without changing the model architecture, effectively enhances the model's ability to identify targets with different confidence levels by optimizing the training strategy. In particular, it performs better in the high cross-sectional area threshold range and is suitable for industrial vision inspection scenarios with high requirements for detection accuracy and stability.
[0046] Building upon the classic YOLOv5 architecture, this invention innovatively introduces a post-processing procedure, implementing a multi-threshold setting mechanism for different defect types and sub-pixel-level defect size measurement functionality. The multi-threshold setting mechanism ensures optimal detection results for each defect type; simultaneously, the use of sub-pixel-level size measurement technology achieves high-precision defect quantification, significantly improving both detection accuracy and size measurement precision.
[0047] The above embodiments are preferred embodiments of the present invention, but the embodiments of the present invention are not limited to the embodiments described above. Any changes, modifications, substitutions, combinations, or simplifications made without departing from the spirit and principle of the present invention shall be considered equivalent substitutions and shall be included within the protection scope of the present invention.
Claims
1. A capacitor defect detection method based on the YOLOv5 model, characterized in that, Includes the following steps: Step 1: Train the YOLOv5 model using the first dataset to obtain a pre-trained model; Step 2 uses the second dataset to train the pre-trained model to obtain the capacitor defect detection model; Step 3: Input the capacitor image into the capacitor defect detection model to perform defect detection, and obtain the location, category and confidence level of all defects in the input capacitor image; Step four uses a multi-threshold setting mechanism to screen the defects detected in step three, and finally determines the defects that do not meet the industrial production quality requirements. A multi-threshold setting mechanism is used to screen defects, including a confidence threshold screening stage and a secondary threshold screening stage.
2. The capacitor defect detection method according to claim 1, characterized in that, Both the first and second datasets consist of images of capacitor defects collected on-site during screen printing. The two datasets have the same number of samples and the same defect types, including black spots, white spots, bumps, bleeding edges, and enlargements.
3. The capacitor defect detection method according to claim 2, characterized in that, The first dataset has the labels for protruding defects commented out, and only includes four defect type labels; the second dataset includes five defect type labels.
4. The capacitor defect detection method according to claim 1, characterized in that, The YOLOv5 model includes a Backbone layer, a Neck layer, and a Head layer.
5. The capacitor defect detection method according to claim 4, characterized in that, The pre-trained model was trained using the second dataset, specifically by freezing the Backbone layer and fine-tuning only the Neck and Head layers.
6. The capacitor defect detection method according to claim 5, characterized in that, The fine-tuning training used Adam as the optimizer with a learning rate of 0.
001.
7. The capacitor defect detection method according to claim 1, characterized in that, The confidence threshold screening stage includes: First Reset Reliability Screening: Set a general confidence threshold that applies to all categories of defects; Second Reset Reliability Screening: Set a confidence threshold for each defect and screen the results of the first reset reliability screening again.
8. The capacitor defect detection method according to claim 7, characterized in that, The secondary threshold screening stage specifically uses pixel size and variance measurement methods to determine the defect type.
9. The capacitor defect detection method according to claim 8, characterized in that, For black and white spot defects, calculate the pixel length that meets the defect grayscale value threshold, and determine the size threshold of black and white spots in both directions by measuring horizontally and vertically. For protrusions, seepage, and thickening defects, calculate the variance of gray values for all rows and columns in each direction for each type of defect, and set gray value variance thresholds for each.
Citation Information
Patent Citations
Factory tooling detection method and system based on neural network target detection
CN115131339A
Target detection and model training method and device, equipment and medium
CN117710756A
PCB defect detection method and system based on adaptive multiple submodels
CN120707492A
Method and system for surface defect detection based on few-shot learning
US20240362762A1
Cited By
Semiconductor defect identification method, device, equipment, medium and program product
CN121456563A
A semiconductor defect identification method, apparatus, device, medium, and program product.
CN121456563B