Capacitance defect detection method based on YOLOv5 model
By introducing a multi-threshold screening and size measurement mechanism into the YOLOv5 model, combined with a two-stage fine-tuning strategy, the problems of sample imbalance and inaccurate size measurement in capacitor screen printing are solved, improving detection accuracy and reliability, and making it suitable for high-precision detection of capacitor defects.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-10-21
- Publication Date
- 2026-03-27
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 not accurate enough 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, and achieves high-precision defect judgment with low false alarms, meeting the refined detection requirements of industrial sites.
Smart Images

Figure CN120976220B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the field of machine vision and deep learning, and particularly relates to a capacitor defect detection method based on a YOLOv5 model. BACKGROUND
[0002] With the rapid development of electronic manufacturing industry, as one of the important electronic components, capacitors have increasingly high requirements for quality control in the production process. Screen printing is a key process in capacitor manufacturing, and its quality directly affects the performance of the final product. During the printing process, various defects may occur, such as black spots, white spots, bumps, hypertrophy, and edge bleeding. These defects not only affect the appearance quality of the capacitor, but also may affect its electrical performance, leading to a decrease in product reliability. Traditional defect detection methods mainly rely on manual visual inspection, which has problems such as low efficiency, high cost, and strong subjectivity. In recent years, with the development of computer vision and deep learning technology, defect detection methods based on deep learning have gradually become a research hotspot.
[0003] As one of the most popular real-time object detection algorithms in current industrial scenarios, the YOLOv5 model has shown 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:
[0004] Firstly, there are various types of capacitor defects, and the characteristics of different defects differ greatly, making it difficult for a single threshold to meet the detection needs of all types of defects. For example, although black and white spot defects are both point-like defects, their characteristics differ significantly, requiring different detection parameters. Similarly, although bump and hypertrophy defects are both area-type defects, their edge characteristics and internal structures differ greatly, also requiring different detection strategies.
[0005] Secondly, the YOLOv5 model can only roughly estimate the size of defects by detecting the length and width of the bounding box, 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 the defect affects the performance of the capacitor. For example, for hypertrophy defects, if the size exceeds a certain range, it may affect the capacitance and voltage resistance performance of the capacitor; for edge bleeding defects, if the edge bleeding width exceeds a certain value, it may affect the insulation performance of the capacitor.
[0006] Finally, there is a serious problem of sample imbalance. In actual production, some defect types (such as black spots) occur frequently and are easy to obtain a large number of samples, while some defect types (such as special-shaped bumps) occur less frequently and are difficult to obtain enough samples. This sample imbalance problem can lead to good detection results for common defects but poor detection results for rare defects, affecting the overall detection performance. SUMMARY
[0007] In order to overcome the above-mentioned defects and deficiencies of the prior art, the purpose of the present application is to provide a capacitor defect detection method based on a YOLOv5 model.
[0008] The present application effectively improves the accuracy and reliability of defect detection by introducing a multi-threshold setting and a defect size accurate measurement mechanism, and using a two-stage fine-tuning method.
[0009] The purpose of the present application is achieved by the following technical solutions:
[0010] A capacitor defect detection method based on a YOLOv5 model, comprising the following steps:
[0011] Step 1: training the YOLOv5 model using a first data set to obtain a pre-trained model;
[0012] Step 2: training the pre-trained model using a second data set to obtain a capacitor defect detection model;
[0013] Step 3: inputting a capacitor picture into the capacitor defect detection model for defect detection to obtain the position, category and confidence of all capacitor defects in the input capacitor picture;
[0014] Step 4: using a multi-threshold setting mechanism to further screen the defects detected in step 3 to finally determine the defects that do not meet the quality requirements of industrial production.
[0015] Further, the first data set and the second data set are both composed of capacitor defect pictures collected on site in silk screen printing, the sample number and defect type of the two data sets are the same, and the defect type includes black spots, white spots, protrusions, edge bleeding and hypertrophy.
[0016] Further, the first data set has the label of the protrusion defect annotated out, and only includes four defect type labels, and the second data set includes five defect type labels.
[0017] Further, the YOLOv5 model includes a Backbone layer, a Neck layer and a Head layer.
[0018] Further, the pre-trained model is trained using the second data set, specifically, the Backbone layer is frozen, and only the Neck layer and the Head layer are fine-tuned and trained.
[0019] Further, the fine-tuning training uses Adam as the optimizer, and sets the learning rate to 0.001.
[0020] Further, the multi-threshold setting mechanism is used to screen the target detection defects, specifically including a confidence threshold screening stage and a secondary threshold screening stage.
[0021] Further, the confidence threshold screening stage comprises:
[0022] First re-confidence screening: set a general confidence threshold applicable to all categories of defects;
[0023] Second re-confidence screening: set a confidence threshold for each defect separately, and re-screen the first re-confidence screening result.
[0024] Further, the secondary threshold screening adopts a method based on pixel size and variance measurement to determine the type of defect.
[0025] Further, for black and white point defects, the length of pixels satisfying the defect gray value threshold is calculated, and through horizontal and vertical measurement, the size threshold of black and white points in two directions is determined.
[0026] For protrusion, bleeding and hypertrophy defects, the gray value variances of all rows and columns of the three defects in each direction are calculated, and a variance threshold is set.
[0027] Compared with the prior art, the present application has the following advantages and beneficial effects:
[0028] 1) effectively alleviate the sample imbalance problem and improve the detection ability of rare defects:
[0029] The present application adopts a two-stage fine-tuning strategy, the first stage uses a class-balanced first dataset for pre-training, so that the model can preliminarily master the general features of each type of defect; the second stage is fine-tuned in a complete but severely imbalanced second dataset, and the backbone is frozen to avoid high-frequency defects (such as protrusions) dominating the bottom feature learning, thereby preserving the recognition ability for low-frequency defects such as black points, white points, and bleeding edges, and significantly improving the generalization performance of the model in the extremely imbalanced sample scene.
[0030] 2) Introduce a multi-threshold screening mechanism to realize high-precision and low-false defect determination:
[0031] The traditional YOLOv5 model only relies on a single confidence threshold for post-processing, which is difficult to balance the detection sensitivity and accuracy of different defect types. The present application designs a dual confidence threshold mechanism of "general preliminary screening + category customized fine screening", and combines the secondary threshold screening based on pixel size and gray variance, which can set the discrimination standard for small targets such as black and white points and large-scale defects such as protrusions and hypertrophy, effectively reducing the missed detection and false positives, and meeting the fine requirements of defect determination in industrial field. BRIEF DESCRIPTION OF DRAWINGS
[0032] Figure 1 is the workflow diagram of the present application;
[0033] Figure 2 is the distribution of the capacitive defects in the data set of the present application;
[0034] Figure 3 is the flow chart of the detection of the capacitive input image of the present application. DETAILED DESCRIPTION
[0035] The present application will be further described in detail below in conjunction with examples, but the embodiments of the present application are not limited thereto.
[0036] Embodiments
[0037] As shown in the following, Figures 1-3 a capacitive defect detection method based on a YOLOv5 model, comprising the following steps:
[0038] Step one: using a first data set to train the YOLOv5 model to obtain a pre-trained model;
[0039] Step two: using a second data set to train the pre-trained model to obtain a capacitive defect detection model;
[0040] Step one is the pre-training stage in this embodiment, and four most representative defect types are selected according to the characteristics of capacitive defect detection, including black spots, white spots, edge penetration and hypertrophy, to ensure that the number of samples of each type of defect is relatively balanced, avoiding sample deviation in the initial training stage. The above process can be represented by the following formula:
[0041]
[0042] D 1 is the data set in the pre-training, N represents the number of samples, xi represents the capacitive sample, represents only a part of the class label, θ b ,θ n ,θ h represents the parameters of the backbone layer, neck layer and head layer of the YOLOv5 model, θ (0) represents the randomly initialized parameters, T represents the number of model iterations.
[0043] Step two is the fine-tuning stage of the pre-training in this embodiment, and the present application adopts a progressive optimization strategy, and the second data set introduces complete five types of defect data to realize full coverage of the defect types.
[0044] The progressive optimization strategy, in particular, freezes the parameters of the Backbone layer, and only fine-tunes the Neck layer and the Head layer. In this way, the general feature extraction capability of the pre-trained model is retained, and targeted optimization is performed for the capacitance defect detection task.
[0045] In the fine-tuning stage, the adam optimizer is used, and the initial learning rate is reduced from 0.01 in the pre-training stage to 0.001. The advantages of this strategy are: the adam optimizer can adaptively adjust the learning rate of each parameter, which helps the model converge faster; and a lower learning rate can ensure that the model performs more fine-tuned parameter adjustment in the fine-tuning stage, avoiding unstable training caused by a too large learning rate, thereby better retaining the features learned in the pre-training stage while optimizing for the capacitance defect detection task.
[0046] Further explanation, as shown in Figure 2 The first data set and the second data set are both composed of capacitance defect pictures collected on-site in silk screen printing, covering five types of defects: black spots, white spots, protrusions, bleeding edges, and hypertrophy. According to the training scheme of deep learning, the data set is divided into training set, validation set and test set according to the ratio of 7:1:2.
[0047] The sample number of the first data set and the second data set is the same, only the number of defects of each category in the training is inconsistent, the number of defects is determined by the label, and a picture often has multiple defects. In the pre-training stage, only four categories of defect labels are annotated, and the protrusion label is annotated. In the fine-tuning stage, labels for five defect categories are available.
[0048] The total number of samples in the data set is about 1258 pictures, which contains the following number of defects:
[0049] The number of black spot defects is 14370;
[0050] The number of white spot defects is 7329;
[0051] The number of protrusion defects is 139644;
[0052] The number of bleeding edge defects is 5610;
[0053] The number of hypertrophy defects is 1176;
[0054] Features: 1) Multi-scale and small target defects: black and white spots are small in size, occupying less than 20 pixels, and 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 protrusion defects far exceeding that of the other types of defects.
[0055] Further explanation, the specific structure in the YOLOv5 model is as follows:
[0056] Backbone layer: Based on residual C3 module, it gradually down-samples and outputs 8x, 16x, 32x three-grade feature maps to capture complete information from fine-grained edge to semantic concept.
[0057] Neck layer: PANet structure is adopted to first fuse high-level semantics from top to bottom, then supplement location details from bottom to top, and finally strengthen the representation ability of three-grade features.
[0058] Head layer: Three-grade features are used to predict center offset, width and height, and class confidence respectively in parallel; in the post-processing stage, NMS is used to remove duplicates, and in the training stage, CIoU Loss + BCE is used to jointly optimize, to realize accurate positioning and classification of multi-scale targets.
[0059] Further, the model parameters of the capacitance defect detection model are: ,
[0060] Among them,
[0061] θ(T)' n represents θ(T) n In this stage of training, changes have occurred, θ(T) b (frozen) represents that the parameters of this stage are in a frozen state and have not changed and are in an initial state θ(T) b The same.
[0062] In this step, the pre-trained model has mastered the general features of capacitance (such as texture and edge) through the first data set, but the excessive proportion of convex defects (more than 60% of the total number of defects) in the second data set is easy to make the model focus too much on this type of shape; freezing the backbone layer can block the reconstruction of bottom features by convex samples, that is, it can retain the discrimination ability of a small number of classes such as black spots, while greatly compressing the trainable parameter amount.
[0063] The Adam optimizer uses a first-order moment direction and a second-order moment noise reduction adaptive mechanism to automatically suppress the gradient weight of the dominant class and create optimization space for low-frequency defects such as seepage edges; a small step learning rate of 0.001 adapts to the sensitive state of the model and can fine-tune the classification boundaries of the Neck layer and the Head layer.
[0064] Step three: input the image into the capacitance defect detection model to obtain the target detection defect.
[0065] Step four: adopt a multi-threshold setting mechanism to screen the target detection defect to obtain the final target defect type.
[0066] The multi-threshold setting mechanism includes a confidence threshold screening stage and a secondary threshold screening stage.
[0067] Further explanation, the confidence threshold is used to filter and confirm the reliability of the detection result. If the confidence score of the detected target is higher than the set threshold, the related target frame will be retained. This mechanism helps to reduce false positives and only retain the detection results that the model is confident about. By adjusting the confidence threshold, optimization between detection accuracy and detection range can be achieved.
[0068] The confidence threshold filtering stage specifically includes two stages:
[0069] The first reset confidence filtering: set a general confidence threshold, which is 0.1 in this embodiment, applicable to all categories of defects; the purpose of this setting is to preliminarily filter the detection results, while ensuring that no correct detection results are missed. In this step, by filtering out most false detections, extensive attention to potential targets can be maintained.
[0070] The second reset confidence filtering: after preliminary filtering, more refined filtering is performed on the results. 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 protrusions and edge bleeding is higher, set to 0.7, and the threshold for hypertrophy is 0.6. This customized setting is based on the characteristics of each type of defect, thereby optimizing the accuracy and robustness of detection. Through such a hierarchical confidence filtering mechanism, reliable detection can be achieved while minimizing false positives and accurately capturing the characteristics of each type of defect.
[0071] The secondary threshold filtering stage: uses a method based on pixel size and variance measurement to determine the type of defect. After the confidence threshold filtering, the secondary threshold filtering focuses on further evaluating the specific size and morphological characteristics of the detection results to meet the strict identification standards for various defects.
[0072] In this embodiment, for black spot defects, only those with a diameter exceeding 10 pixels are considered defects, and those that do not meet this size are not considered. Similar standards also apply to white spots, protrusions, edge bleeding, and hypertrophy. Since these specific size determination requirements cannot be achieved through simple confidence threshold filtering, the secondary threshold filtering uses a method based on pixel size and variance measurement to achieve more accurate determination. Specific measures include: 1) for black and white spots, calculate the length of pixels that meet the defect gray value threshold (different for black and white spots), measure in both horizontal and vertical directions to determine the size of both in these two directions; 2) for the three defects of protrusion, edge bleeding, and hypertrophy, calculate the gray value variance of all rows and columns in each direction, and set a variance threshold (different for the three types of defects), and determine the specific size of the defect based on this value. Such a detection and determination system combines size and gray difference analysis to ensure higher accuracy in locating and confirming defects.
[0073] In this embodiment, the threshold values of the gray scale values of the black and white points are 100 and 200 respectively; the variance threshold values of the convex, bleeding and hypertrophy defects are set in the interval of 20-50, and the general threshold value is set to 45 in this embodiment.
[0074] The present application adopts two-stage training, first pre-training through balanced data set, ensures that the model has good basic detection ability; then adopts hierarchical freezing and progressive learning rate strategy for fine-tuning, solves the problem of sample imbalance, significantly improves the detection performance and stability of the model in the actual production environment. The proposed algorithm is based on YOLOv5 detection model, under the premise of adopting two-stage self-tuning strategy and freezing the backbone network, compared with the original YOLOv5 model without using the strategy, the overall precision of target detection is significantly improved. Specifically, the average precision mean (intersection over union threshold is 0.5) of the proposed algorithm is improved from 0.689 to 0.746, with an improvement of 8.3%; the average precision mean (intersection over union threshold range is 0.5 to 0.95) is improved from 0.569 to 0.608, with an improvement of 6.9%. The above results show that the proposed algorithm does not change the model architecture, but optimizes the training strategy, effectively enhances the model's recognition ability for different confidence targets, especially in the high intersection over union threshold interval, which is suitable for industrial vision detection scenarios with high requirements for detection accuracy and stability.
[0075] On the basis of the classic YOLOv5 architecture, the present application innovatively introduces a post-processing process, realizes a multi-threshold setting mechanism for different types of defects and a sub-pixel level defect size measurement function. Through the multi-threshold setting mechanism, each defect type can obtain the best detection effect; at the same time, the sub-pixel level size measurement technology is adopted to realize high-precision defect quantification, which significantly improves the detection accuracy and size measurement precision.
[0076] The above embodiments are the preferred embodiments of the present application, but the embodiments of the present application are not limited by the above embodiments, and any changes, modifications, substitutions, combinations and simplifications made without departing from the spirit and principles of the present application shall be equivalent replacement methods and shall be included in the protection scope of the present application.
Claims
1. A capacitive defect detection method based on a 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; 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. The first dataset has the labels for protruding defects commented out, and only includes labels for the four defect types; The second dataset includes labels for five defect types; 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; The secondary threshold screening stage specifically uses pixel size and variance measurement methods to determine the defect type; 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 protrusion, seepage, and thickening defects, calculate the variance of gray values for all rows and columns in each direction for the three types of defects, and set gray value variance thresholds for each. 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.
2. 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.
3. The capacitor defect detection method according to claim 2, characterized in that, The fine-tuning training used Adam as the optimizer with a learning rate of 0.001.
Citation Information
Patent Citations
Target detection and model training method and device, equipment and medium
CN117710756A