An industrial defect classification method based on contrast learning and feature decoupling

By decoupling defects and background features through a dual-path contrast decoupling network model, the problem of low accuracy caused by feature coupling in industrial defect classification is solved, achieving a classification effect with high accuracy and strong generalization ability.

CN121305245BActive Publication Date: 2026-02-06SHANDONG UNIV OF SCI & TECH
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202511853290.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-12-10
Publication Date
2026-02-06
Estimated Expiration
2045-12-10

AI Technical Summary

Technical Problem

Existing industrial defect classification methods suffer from limited feature representation capabilities, strong dependence on manual features, poor performance under small sample conditions, and low classification accuracy due to the high coupling between defect features and background features.

Method used

A method based on contrastive learning and feature decoupling is adopted. A dual-path contrastive decoupling network model is used, including a defect encoder, a background encoder, a contrastive learning module, and a mutual information decoupling module, to extract and decouple defect features and background features respectively, and a pre-training-fine-tuning approach is used for classification.

Benefits of technology

It improves classification accuracy and generalization ability, especially significantly improving classification accuracy under small sample conditions, and reducing feature confusion and false detection rate.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121305245B_ABST
    Figure CN121305245B_ABST
Patent Text Reader

Abstract

The application provides an industrial defect classification method based on contrast learning and feature decoupling, relates to the field of industrial defect classification, and specifically comprises the following steps: constructing an industrial defect classification dataset; preprocessing the dataset; training a two-path contrast decoupling network model using the dataset, taking out a defect encoder part of the trained two-path contrast decoupling network model as a pre-training backbone network of a classification task, and then combining the defect encoder part with a classification head composed of a full connection network to form a defect classification network; training the defect classification network to obtain a defect classification model; inputting a test image into the defect classification model to test the accuracy of the defect classification model; and applying a model with the best test effect on the test set to an actual defect classification process of a factory. The technical scheme of the application overcomes the problem of low classification accuracy caused by a small amount of data, high similarity between defects, and coupling of defect and background features in the prior art.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the field of industrial defect classification, and particularly relates to an industrial defect classification method based on contrast learning and feature decoupling. BACKGROUND

[0002] Industrial defect classification is a core task in the field of industrial vision, and its necessity stems from the strict requirements of intelligent manufacturing on product quality control. As an important means of production quality control, industrial defect classification not only optimizes the production process, but also provides a basis for process improvement.

[0003] Traditional industrial defect classification methods rely on hand-crafted features and classic machine learning classifiers. Specifically, hand-crafted features include gray-scale features extracted by pixel mean, variance, shape features extracted by area, perimeter, and texture features extracted by LBP, Gabor filter, GLCM, etc. Then, the extracted features are input into a support vector machine (SVM) to train a classifier by maximizing the inter-class interval, and the defect category is output. Traditional methods have the advantages of high flexibility and short development cycle, but have the following limitations: 1. Limited feature expression ability, hand-crafted features rely on prior knowledge and are difficult to capture deep semantic information of defects; 2. Strong dependence on artificial features, the quality of features directly determines the classification performance, and if the design is unreasonable (such as ignoring the directional information of defects), the accuracy will decrease significantly; 3. Poor performance under small sample conditions, SVM needs sufficient samples to train the optimal hyperplane, when the defect samples are scarce, overfitting easily occurs, leading to decreased generalization ability.

[0004] Deep learning methods have become the mainstream approach for industrial defect classification. Their core lies in automatically extracting deep features of defects through neural networks to achieve end-to-end classification. This method eliminates the need for manually designed features, effectively capturing the essential differences in defects, and significantly outperforms traditional methods, especially in large-sample and complex scenarios. The specific process includes: first, preprocessing industrial images (such as normalization, cropping, and flipping) and enhancing sample diversity through data augmentation; then, using a model pre-trained on a large-scale dataset (such as ImageNet) as the backbone network; finally, combining the pre-trained model with a custom classification layer and fine-tuning it using an industrial defect dataset to achieve defect classification. Despite significant progress in deep learning methods, fundamental challenges remain in real-world industrial scenarios: the high coupling between defect features and background features. This coupling makes it difficult for the model to learn pure discriminative features, leading to decreased detection accuracy, increased false positive rates, and insufficient generalization ability. Compared to general scenarios, industrial defect regions are often inconspicuous in the background, making it difficult for traditional single-branch network architectures to effectively separate coupled features. During training, the model encodes background information into defect features or dilutes defect features into the background representation, resulting in feature space redundancy and interference. This not only reduces classification accuracy but also makes the model unstable in new testing environments. Furthermore, under small sample conditions, due to the scarcity of industrial defect samples (e.g., less than 100 images) and the large number of parameters in the pre-trained model (e.g., ResNet-50 contains 25 million parameters), the model is prone to overfitting, and its generalization ability decreases significantly.

[0005] Therefore, there is a need for an industrial defect classification method based on contrastive learning and feature decoupling, which has high classification accuracy and strong generalization ability. Summary of the Invention

[0006] The main objective of this invention is to provide an industrial defect classification method based on contrastive learning and feature decoupling, so as to solve the problem of low classification accuracy caused by the small amount of data, high similarity between defects, and coupling between defects and background features in the existing classification model.

[0007] To achieve the above objectives, this invention provides an industrial defect classification method based on contrastive learning and feature decoupling, specifically including the following steps:

[0008] S1: Collect industrial defect data and construct an industrial defect classification dataset.

[0009] S2, preprocessing the dataset, including: removing images with abnormal shooting conditions and second confirmation of defect category classification.

[0010] S3, training the dual-path contrast decoupling network model using the data set, the dual-path contrast decoupling network model comprising: a defect encoder, a background encoder, a contrast learning module, a mutual information decoupling module, a defect classifier and a background classifier; the features extracted by the defect encoder and the background encoder are input into the contrast learning module, and then the feature decoupling is performed through the mutual information decoupling module to obtain the decoupled defect features and background features, which are respectively sent into the defect classifier and the background classifier, and the defect classifier and the background classifier learn the defect features and the background features respectively.

[0011] S4, the defect encoder part of the trained dual-path contrast decoupling network model is taken out as a pre-training backbone network of a classification task, and a classification head composed of a fully connected network is combined to form a defect classification network; the defect classification network is trained using the data set in step S2 to obtain a defect classification model.

[0012] S5, inputting a test image into the defect classification model obtained through step S4 to test the accuracy of the defect classification model.

[0013] S6, applying the model with the best test effect on the test set to the actual defect classification process of the factory.

[0014] Further, the contrast learning module comprises: a category supervised contrast learning module and a background supervised contrast learning module, the category supervised contrast learning module narrows the feature distance of samples of the same category and pushes away samples of different categories; and the background supervised contrast learning module makes the background features of different samples and different perspectives consistent.

[0015] Further, step S3 specifically comprises the following steps:

[0016] S3.1, inputting two groups of image data with a number of into the defect encoder and the background encoder respectively to obtain defect features , and background features , .

[0017] S3.2, sending , and the category label into the contrast learning module, and the loss function of the category supervised contrast learning module is as follows:

[0018] ;

[0019] wherein, represents the same category loss, represents the different category loss, represents the weight.

[0020] ​Further, step S3.2 is specifically:

[0021] In the category supervised contrast learning module, the same class samples are first spliced along the sample dimension to form a feature matrix containing 2N samples, then the features are normalized so that the length of the feature vector of each sample is, then the similarity between all sample features is calculated, then the same class sample mask is created, then the logarithmic probability is calculated, only the loss of the same class sample pair is calculated, and finally the negative logarithmic probability average value of the same class sample pair is taken as the same class loss, the same class loss is:

[0022] ;

[0023] wherein, represents the number of same class samples; is the label prediction value of the i-th sample, is the label prediction value of the j-th sample, is the indicator function, 1 when satisfied, and 0 when not satisfied; represents the normalized feature of the i-th sample; represents the anchor sample index; represents the index of the positive sample belonging to the same class as the anchor sample; represents the index of all samples; represents the temperature parameter; represents the exponential function; represents the natural logarithm. For different class samples, the cosine similarity of the two features is directly calculated, and a threshold is set, and no loss will be generated when the cosine similarity is lower than the threshold, and the different class loss is:

[0024]

[0025] ;

[0026] wherein, represents the number of different class samples, represents the boundary threshold, represents the normalized feature of the i-th sample in the first group, represents the feature of the i-th sample in the second group, represents the boundary threshold, represents the cosine similarity function, is the maximum function. Further, step S3 also includes the following steps:

[0027] Further, step S3.2 is specifically: ​​​​

[0028] S3.3, in the background supervision contrast learning module, the background feature of the anchor point is calculated , is merged, and the similarity between each background feature and other background features is calculated, and the background supervision contrast loss function is as follows:

[0029] ;

[0030] wherein, represents the total number of positive samples, i.e. all non-self sample pairs, ; represents the background feature of the anchor point , represents the background feature of the anchor point of the same kind as the anchor point , and represents all background features.

[0031] S3.4, the variance regularization loss function is used to prevent the collapse of the background feature:

[0032] ;

[0033] wherein, represents the variance threshold, represents the feature dimension, represents the variance of the first dimension.

[0034] Further, the mutual information decoupling module includes a sample generator, a discriminator composed of three fully connected layers, and a gradient reversal layer, which uses an adversarial training method to realize mutual information minimization of defect encoding and background encoding, and achieves feature decoupling effect; the generator concatenates the defect feature and the background feature from the same sample as a positive sample, and randomly concatenates the defect feature and the background feature from different samples as a negative sample; the discriminator discriminates whether the input is a positive sample through the mutual information of the defect feature and the background feature; the gradient reversal layer guides the gradient in the opposite direction, so that the discriminator gradually cannot distinguish between positive and negative samples, and at the same time forces the feature encoder to find deeper and more general features.

[0035] Further, the mutual information decoupling loss function is composed of positive sample loss and negative sample loss , and the formula is as follows:

[0036] ;

[0037] ;

[0038] ​​ ;

[0039] wherein, represents the defect feature of the first sample of the first group, represents the defect feature of the first sample of the second group; represents the background feature of the first sample of the first group, represents the background feature of the first sample of the second group; represents the discriminator output; , represents the random permutation index, and respectively represent the background feature of the first sample after the order of the first group and the second group is shuffled.

[0040] Further, the defect classifier adopts a full connection layer, and the defect encoder learns the defect feature by constraint of a cross-entropy loss function; the background classifier comprises a gradient reversal layer and a full connection layer; and the classification loss function is as follows:

[0041] ;

[0042] ;

[0043] ;

[0044] ;

[0045] wherein, , , respectively represent a defect classification loss, a regular classification loss and a cross-entropy loss function; and respectively represent the class prediction probability of the defect classifier of the first group and the second group data; and respectively represent the defect class label vector corresponding to and ; and respectively represent the class prediction probability of the background classifier of the first group and the second group data; and respectively represent the defect class label vector corresponding to and ; represents the label vector of the sample belongs to the class ; representative model prediction sample belongs to a class probability of belonging to a class representative class total number.

[0046] Further, the total loss function of the dual-path contrastive decoupling network model is:

[0047] .

[0048] The present application has the following beneficial effects:

[0049] A dual-path contrastive decoupling network model is designed, in which the defect encoder is specifically responsible for extracting defect-related discriminative features, and the background encoder focuses on learning the standard representation of the background, avoiding feature confusion from the source.

[0050] The method of supervised contrastive learning is used to increase the difference between different categories of defects and improve the classification accuracy of similar defect categories.

[0051] A mutual information decoupling module is designed to decouple defect features and background features, solving the problem of low classification accuracy caused by the similarity between some defects and backgrounds.

[0052] Using the pre-training-fine-tuning method, the classification accuracy is greatly improved under small sample conditions. BRIEF DESCRIPTION OF DRAWINGS

[0053] In order to more clearly illustrate the technical solutions in the specific embodiments or prior art of the present application, the drawings needed in the specific embodiments or prior art description will be briefly introduced below. Obviously, the drawings in the following description are some embodiments of the present application, and those skilled in the art can also obtain other drawings according to these drawings without creative labor. In the drawings:

[0054] Figure 1 A flowchart of an industrial defect classification method based on contrastive learning and feature decoupling is shown.

[0055] Figure 2 A t-SNE visualization diagram for decoupling defect features and background features using the method provided by the present application is shown.

[0056] Figure 3 A feature clustering t-SNE visualization diagram using the ResNet50 model output is shown.

[0057] Figure 4 A feature clustering t-SNE visualization diagram using the defect encoder provided by the present application is shown. DETAILED DESCRIPTION

[0058] The technical solutions of the present application will be described clearly and completely in conjunction with the drawings. Obviously, the described embodiments are part of the embodiments of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative labor fall within the scope of protection of the present application.

[0059] As shown in a kind of industrial defect classification method based on contrast learning and feature decoupling, specifically includes the following steps: Figure 1

[0060] S1, industrial defect data is collected, and industrial defect classification dataset is constructed.

[0061] S2, the data set is preprocessed, including: eliminating the image of abnormal shooting, defect category classification second confirmation.

[0062] S3, the dual-path contrast decoupling network model is trained using the data set, the dual-path contrast decoupling network model includes: defect encoder, background encoder, contrast learning module, mutual information decoupling module, defect classifier and background classifier;The features extracted by the defect encoder and the background encoder are input into the contrast learning module, and then the feature decoupling is carried out through the mutual information decoupling module, to obtain the decoupled defect feature and background feature, which are respectively sent into the defect classifier and the background classifier, and the defect classifier and the background classifier learn the defect feature and the background feature respectively. Background encoder and defect encoder use two independent ResNet50 networks in parallel;The defect classifier includes a global pooling layer and a fully connected layer;The background classifier includes a gradient inversion layer, a global pooling layer and a fully connected layer;The contrast learning module includes a cosine similarity function;The mutual information decoupling module includes a discriminator composed of three fully connected layers and a gradient inversion layer.

[0063] S4, model fine-tuning: the defect encoder part of the trained dual-path contrast decoupling network model is taken out as a pre-training backbone network for classification task, and a classification head composed of a fully connected network is formed to form a defect classification network;The data set in step S2 is used to train and fine-tune the defect classification network, to obtain a defect classification model.

[0064] S5, the test image is input into the defect classification model obtained by step S4, and the precision of the defect classification model is tested.

[0065] S6, defect classification: the model with the best test effect on the test set is applied to the actual defect classification process of the factory, so as to realize real-time accurate classification of the defect category, reduce the labor cost, and further promote the informatization construction in the field of industrial defect classification.

[0066] ​The present application firstly collects defect image samples and divides them into training data and verification data. Then, the two-way contrast decoupling network is trained using industrial defect data to obtain a pre-trained model of feature decoupling. Then, the pre-trained model is combined with a classifier to obtain a defect classification model, and a small amount of data samples are used to fine-tune the defect classification model to obtain a fine-tuned defect classification model. Finally, the fine-tuned defect classification model is used for defect classification.

[0067] Specifically, in the embodiment, the data selected in step S1 is derived from a certain powder metallurgy manufacturing factory, and an industrial camera with a resolution of 5 million pixels is used to automatically collect images at different workstations in combination with a sensor. The defect images are classified by manual screening to lay the foundation for model training. The entire data set contains 4 defect categories and 5100 images. Specifically, the defect categories include pits, material adhesion, material deficiency, and cracking, each with 1275 images.

[0068] Specifically, in the embodiment, since image loss caused by sensor failure and human classification errors may occur during data collection, the collected data is strictly preprocessed. The preprocessing step in step S2 specifically includes removing abnormal images and confirming the defect category for the second time. Abnormal images include missing targets, overexposed photos, and blurred defects, which are considered invalid images and are deleted. Then, the preprocessed data is constructed into a complete data set. After data preprocessing, there are 5100 images, 4000 of which are used for pre-training of the decoupling network; then 25 images of each defect category are used for model fine-tuning to simulate a small sample scenario; finally, 250 images of each defect category are used for model precision testing, totaling 1000 images. Data preprocessing can effectively improve data quality and improve the training accuracy of the model.

[0069] Specifically, the contrast learning module includes a category supervised contrast learning module and a background supervised contrast learning module. The category supervised contrast learning module narrows the feature distance of similar samples and pushes away different samples to improve classification accuracy. The background supervised contrast learning module makes the background features consistent under different samples and different perspectives. The category supervised contrast learning module includes learning same-class loss and learning different-class loss.

[0070] Specifically, step S3 specifically includes the following steps:

[0071] S3.1, two groups of image data with a quantity of are input into the defect encoder and the background encoder, respectively, to obtain defect features , and background features , with a dimension of 2048.

[0072] S3.2, the , And the category labels are fed into the contrastive learning module, and the loss function of the category-supervised contrastive learning module is... as follows:

[0073] ;

[0074] in, Representing similar losses, Represents outlier loss. The value represents the weight, which is set to 0.7 in this embodiment.

[0075] Specifically, step S3.2 is as follows:

[0076] In the category-supervised contrastive learning module, similar samples are first concatenated along the sample dimension to form a feature matrix containing 2N samples. Then, the features are normalized so that the feature vector of each sample has a length of . Next, the similarity between all sample features is calculated. Then, a similar sample mask (of the same type but not itself) is created. The log probability is then calculated, and the loss is calculated only for pairs of similar samples. Finally, the average of the negative log probabilities of pairs of similar samples is taken as the similarity loss. for:

[0077] ;

[0078] in, Represents the number of samples of the same type; For the first The predicted label value for each sample. For the first The predicted label value for each sample. For indicator functions, The value is 1 when the condition is met, and 0 when the condition is not met. Representing the Normalized features of each sample; Represents the anchor point sample index; The index represents the positive sample that belongs to the same class as the anchor sample; An index representing all samples; This represents the temperature parameter; the smaller the value, the more sensitive it is to gradients. In this embodiment, the default value is set to 0.1. Represents an exponential function; It represents the natural logarithm.

[0079] For outlier samples, the cosine similarity between the two features is calculated directly, and a threshold is set. When the cosine similarity is below the threshold, no loss occurs; this is the outlier loss. for:

[0080] ;

[0081] wherein, represents the number of different samples, represents the boundary threshold, represents the normalized feature of the first sample of the first group, represents the feature of the second sample of the second group, represents the feature of the second sample of the second group, represents the boundary threshold, when the cosine similarity is lower than , no loss will be generated, in the embodiment, the value is set to 0.5, represents the cosine similarity function, is a max function. Since the background is usually regarded as an "uninformative" or "negligible" region, the model expects the background features of different samples and different perspectives to be highly consistent. The background supervision contrast loss function is designed about the background, all background samples are regarded as the same class, the same class background similarity (the same class background should be similar) is preserved, reasonable differences between the background features are allowed, and the commonality of the background is naturally learned by the model through the relative probability mechanism. The purpose is to let the background not carry discriminative information and avoid interference with foreground learning.

[0082] Specifically, step S3 further comprises the following steps:

[0083] S3.3, in the background supervision contrast learning module, using the ,

[0084] background features obtained in step S3.1, a background supervision contrast loss function is calculated, and , are merged, then the similarity between each background feature and other background features is calculated, and the background loss function makes the similarity between the encodings higher. The background supervision contrast loss function is as follows:

[0085] ;

[0086] wherein, represents the total number of positive samples, i.e. all non-self sample pairs, ; represents a temperature parameter, the smaller the parameter, the more sensitive to the gradient, in the present application, the default value is set to 0.5; represents the background feature of the anchor point , represents the specific background feature of the anchor point of the same class as the anchor point , represents all background features.

[0087] ​​S3.4, in order to prevent the collapse of background features, the feature space degenerates, the present application also designs a variance regularization loss function, by forcing the average variance of background features not less than the threshold margin, to ensure that the feature space is enough to prevent the collapse of background features: variance regularization loss function Prevent the collapse of background features:

[0088] ;

[0089] Wherein, represents the variance threshold, the default value in the present application is set to 0.1; represents the feature dimension, the present application 2048; represents the variance of the first dimension.

[0090] Specifically, the mutual information decoupling module includes a sample generator, a discriminator composed of three fully connected layers and a gradient reversal layer, which realizes the mutual information minimization of defect encoding and background encoding by using the way of adversarial training, and achieves the effect of feature decoupling; The generator splices the defect features and background features from the same sample together as positive samples, and splices the defect features and background features from different samples randomly as negative samples; The discriminator distinguishes whether the input is a positive sample through the mutual information of the defect features and the background features, when the discriminator cannot distinguish the positive and negative samples, it means that the mutual information of the defect features and the background features is close to zero, and the feature decoupling is completed; The gradient reversal layer guides the gradient in the opposite direction, so that the discriminator gradually cannot distinguish the positive and negative samples, and at the same time forces the feature encoder to find deeper and more general features.

[0091] Specifically, the mutual information decoupling loss function is composed of positive sample loss and negative sample loss , the formula is as follows:

[0092] ;

[0093] ;

[0094] ;

[0095] Wherein, represents the defect feature of the first sample of the first group, represents the defect feature of the first sample of the second group; represents the background feature of the first sample of the first group, represents the background feature of the first background features of the sample; representing the discriminator output, i.e., a scalar similarity score; , representing the background features of the first and second groups of samples after shuffling, respectively; and representing the background features of the first and second groups of samples after shuffling, respectively.

[0096] , and , are input into the mutual information decoupling module, which includes a sample generator, a discriminator composed of three fully connected layers, and a gradient reversal layer. The mutual information minimization of defect encoding and background encoding is achieved by using the way of adversarial training, and the effect of feature decoupling is achieved. The generator splices the defect encoding and the background encoding from the same sample together as a positive sample, and splices the defect encoding and the background encoding from different samples randomly as a negative sample. Before feature decoupling, the mutual information of the positive sample is large because the defect encoding and the background encoding come from the same sample, while the mutual information of the negative sample is close to zero because the defect encoding and the background encoding come from different samples. The discriminator can distinguish whether the input is a positive sample through the mutual information of the defect encoding and the background encoding. When the discriminator cannot distinguish the positive and negative samples, it means that the mutual information of the defect encoding and the background encoding is close to zero, and the feature decoupling is completed. The feature decoupling process is as follows: the generator generates positive and negative samples, and then sends them to the discriminator for positive and negative sample discrimination. During training, the defect and background encoders strive to extract feature information, and the discriminator strives to distinguish positive and negative samples, but in the back propagation process, the gradient reversal layer guides the gradient in the opposite direction, causing the discriminator to gradually fail to distinguish positive and negative samples, while forcing the feature encoder to find deeper and more general features. Finally, with the convergence of the model, the defect and background encoders learn effective features, and the discriminator cannot distinguish positive and negative samples, i.e., feature decoupling is completed.

[0097] Specifically, the defect classifier adopts a fully connected layer, and a cross-entropy loss function is used for constraint, so that the defect encoder learns defect features; the background classifier includes a gradient reversal layer and a fully connected layer; and a classification loss function is as follows:

[0098] ;

[0099] ;

[0100] ;

[0101] ;

[0102] wherein,​​ respectively represent the defect classification loss, the regular classification loss and the cross-entropy loss function; respectively represent the class prediction probability of the first group and the second group of data defect classifiers; respectively represent the corresponding defect class label vectors; respectively represent the class prediction probability of the first group and the second group of data background classifiers; respectively represent the class prediction probability of the first group and the second group of data background classifiers; respectively represent the corresponding defect class label vectors; respectively represent the corresponding defect class label vectors; represents the label vector of the sample belonging to the class ; represents the probability of the model predicting that the sample belongs to the class ; represents the total number of classes.

[0103] The decoupled defect features and background features obtained through the mutual information decoupling module are respectively sent into the defect classifier and the background classifier. The defect classifier adopts a conventional full connection layer design, and is constrained by a cross-entropy loss function, so that the defect encoder learns the defect features. Since the background features are actually irrelevant to the class, the background classifier adds a gradient reversal layer before the full connection layer, and the background encoder reverses the gradient during back propagation, so that the training target of the background encoder is opposite to that of the background classifier. The background encoder is forced to learn pure background features irrelevant to the class, thereby realizing background information learning.

[0104] Specifically, the total loss function of the dual-path contrast decoupling network model is:

[0105] .

[0106] The following steps are performed in the dual-path contrast decoupling network:

[0107] (1) Two groups of images are respectively input into the defect encoder and the background encoder to obtain defect features , and background features , .

[0108] (2) The , ​​​​​​​​The design category supervised contrast learning module loss function constrains the model, so that the defect codes of the same sample are more similar, and the defect codes of different samples are less similar.

[0109] (3) The , is sent to the contrast learning module. The background features are all considered as the same category, and the supervised contrast loss function is used to constrain the background to make the background more similar. At the same time, in order to prevent the background code feature from collapsing, a variance regularization loss function is designed.

[0110] (4) The , , , is sent to the mutual information decoupling module. The mutual information decoupling module includes a generator and a discriminator. The generator will shuffle the order of the input and splice it to get positive samples from the same picture and negative samples from different pictures. The discriminator contains a gradient reversal layer inside, which will multiply the gradient from the discriminator by a negative constant factor , that is . This design makes the gradient signals received by the background and defect encoders opposite to the discriminator. Therefore, the background and defect encoders are encouraged to generate features that can deceive the discriminator and make the discriminator unable to distinguish positive and negative samples. Through the above adversarial training mechanism, the complex mutual information calculation problem is converted into a game process between the discriminator and the encoder. With the gradual optimization of the generator, the discriminator cannot distinguish positive and negative samples, which promotes the minimization of the mutual information of the defect and background features, and realizes the decoupling of the defect features and the background features.

[0111] (5) The decoupled defect features and background features obtained in step (4) are sent to the defect classifier and the background classifier respectively. The defect classifier adopts a conventional fully connected layer design, and is constrained by a cross-entropy loss function, so that the defect encoder learns the defect features. Since the background features are actually unrelated to the category, the background classifier adds a gradient reversal layer before the fully connected layer, and the background encoder reverses the gradient during backpropagation, so that the training target of the background encoder is opposite to the background classifier. Force the background encoder to learn the pure background features unrelated to the category, thereby realizing the background information learning.

[0112] (6) The dual-path contrastive decoupling network is trained to model convergence by jointly optimizing multiple loss functions. In terms of deep learning model hyperparameter settings, the best-performing hyperparameters are selected: the learning rate is 0.0001; the optimizer is the Adam algorithm; the batch size is 48; and the training rounds are 100. To prevent overfitting, an early stopping mechanism is introduced, and if the validation set loss does not decrease for 5 consecutive rounds, the training is terminated in advance. After the training of the model is completed, the model is tested using the test set constructed in step (2). The predicted value of the model and the actual category are used to calculate the accuracy rate to measure the performance of the model.

[0113] To verify the effectiveness and adaptability of the method provided by the application, experiments are performed on the collected powder metallurgy data set, as well as the publicly available hot-rolled strip steel data set NEU-CLS, the textile data set AITEX_FABRIC, and the magnetic tile data set MT. To simulate a small sample scenario, the data ratio of the public data set training and testing is divided as 1:9. To ensure fairness, a classification network ResNet50 with the same network structure as the method of the application is used as a baseline model. The test accuracy is shown in Table 1:

[0114] Table 1 Comparison of test accuracy

[0115]

[0116] As can be seen from Table 1, the classification accuracy of the method provided by the application remains at a high level on each data set.

[0117] Figure 2 Each point represents a sample, and the coordinates are the low-dimensional space coordinates after dimension reduction. The red area represents the defect feature, and the blue area represents the background feature. As shown in Figure 2 , the defect feature and the background feature are obviously separated in the two-dimensional space, indicating that the features output by the defect encoder and the background encoder have successfully achieved decoupling. It is worth noting that part of the blue area representing the background feature is scattered, while the red area of the defect feature appears four obvious clusters, indicating that different defect types have good distinguishability in the feature space.

[0118] To further evaluate the class distinguishability of the defect feature, the t-SNE visualization of the defect feature is labeled by class, and is compared with the feature representation of the commonly used ResNet50 pre-trained model. The visualization results are shown in Figure 3 and Figure 4 , in which the blue, red, orange and green areas correspond to the "pit", "lack of material", "crack" and "adhesive material" respectively. As shown in Figure 4 , the features extracted by the defect encoder provided by the application are clustered, and the same class samples show obvious clustering characteristics; asFigure 3 As shown, the features extracted by the ResNet50 pre-training model are blurred at the boundaries between classes, and there is significant confusion. This phenomenon shows that the coupling between different class features is effectively reduced by introducing the contrast learning mechanism.

[0119] Of course, the above description is not a limitation on the present application, and the present application is not limited to the above examples. Changes, modifications, additions or substitutions made by those skilled in the art within the scope of the present application should also be within the scope of the present application.

Claims

1. An industrial defect classification method based on contrastive learning and feature decoupling, characterized in that, Specifically comprising the following steps: S1, collecting industrial defect data, and constructing an industrial defect classification data set; S2, preprocessing the data set, including: eliminating images with abnormal shooting, and secondly confirming the defect category classification; S3, training a dual-path contrast decoupling network model using the data set, the dual-path contrast decoupling network model comprising: a defect encoder, a background encoder, a contrast learning module, a mutual information decoupling module, a defect classifier, and a background classifier; the features extracted by the defect encoder and the background encoder are input into the contrast learning module, and then the features are decoupled by the mutual information decoupling module to obtain decoupled defect features and background features, which are respectively input into the defect classifier and the background classifier, and the defect classifier and the background classifier learn the defect features and the background features respectively; S4, taking out the defect encoder part of the trained dual-path contrast decoupling network model as a pre-training backbone network for a classification task, and combining the pre-training backbone network with a classification head composed of a fully connected network to form a defect classification network; using the data set in step S2, the defect classification network is trained to obtain a defect classification model; S5, inputting a test image into the defect classification model obtained by step S4 to test the accuracy of the defect classification model; S6, applying the model with the best test effect on the test set to the actual defect classification process of the factory; The contrast learning module comprises: a category supervised contrast learning module and a background supervised contrast learning module, the category supervised contrast learning module narrows the feature distance of samples of the same category and pushes away samples of different categories; and the background supervised contrast learning module makes the background features of different samples and different perspectives consistent; The mutual information decoupling module comprises: a sample generator, a discriminator composed of three fully connected layers, and a gradient reversal layer, and uses an adversarial training method to realize mutual information minimization of defect encoding and background encoding, and achieves feature decoupling; the generator splices the defect features and the background features from the same sample together as a positive sample, and splices the defect features and the background features from different samples randomly as a negative sample; the discriminator discriminates whether the input is a positive sample through the mutual information of the defect features and the background features; and the gradient reversal layer guides the gradient in the opposite direction, so that the discriminator gradually cannot distinguish the positive sample and the negative sample, and forces the feature encoder to find deeper and more general features.

2. The industrial defect classification method based on contrastive learning and feature decoupling according to claim 1, characterized in that, Step S3 specifically comprises the following steps: S3.1, the number of two groups is The image data is input into the defect encoder and the background encoder respectively, and the defect feature , and the background feature , are obtained; S3.2, to , and category labels into a contrastive learning module, which supervises the loss function of the contrastive learning module as follows: ; wherein, represents a same class loss, represents a different class loss, represents a weight.

3. The industrial defect classification method based on contrastive learning and feature decoupling according to claim 2, characterized in that, Step S3.2 specifically comprises: In the category supervised contrast learning module, the same category samples are first spliced along the sample dimension to form a feature matrix containing 2N samples, then the features are normalized so that the length of the feature vector of each sample is, then the similarity between all sample features is calculated, then the same category sample mask is created, then the logarithmic probability is calculated, only the loss of the same category sample pair is calculated, and finally the negative logarithmic probability average value of the same category sample pair is taken as the same category loss, and the same category loss is: ; in, Represents the number of samples of the same type; For the first The predicted label value for each sample. For the first The predicted label value for each sample. For indicator functions, The value is 1 when the condition is met, and 0 when the condition is not met. Representing the Normalized features of each sample; Represents the anchor point sample index; The index represents the positive sample that belongs to the same class as the anchor sample; An index representing all samples; Represents temperature parameter; Represents an exponential function; Represents the natural logarithm; For heterogeneous samples, the cosine similarity of two features is directly calculated, and a threshold is set. When the cosine similarity is lower than the threshold, no loss will be generated, and the heterogeneous loss is: ; wherein, represents the number of heterogeneous samples, represents a boundary threshold, represents a normalized feature of a first sample of the first group, represents a normalized feature of a first sample of the first group, represents a feature of a second sample of the second group, represents a feature of a second sample of the second group, represents a boundary threshold, represents a cosine similarity function, is a max function.

4. The industrial defect classification method based on contrastive learning and feature decoupling according to claim 3, characterized in that, Step S3 further comprises the following steps: S3.3, in the background supervision contrast learning module, will , merge, and then calculate the similarity of each background feature with other background features, the background supervision contrast loss function is as follows: ; wherein, represents the total number of positive samples, i.e. all non-self sample pairs, ; represents the background feature of the anchor point , represents the anchor point of the same kind as the anchor point , the specific background feature, represents all background features; S3.4, using a variance regularized loss function Prevent background features from collapsing: ; wherein, a representative variance threshold, a representative feature dimension, a representative first dimensional variance.

5. The industrial defect classification method based on contrastive learning and feature decoupling according to claim 1, characterized in that, Mutual information decoupling loss function consisting of a positive sample loss and a negative sample loss , formulated as follows: ; ; ; in, The first group's number Defect characteristics of each sample The second group's number Defect characteristics of each sample; The first group's number Background features of each sample The second group's number Background features of each sample; Represents the discriminator output; , Represents a randomly arranged index. and These represent the first and second groups after being shuffled. Background features of each sample.

6. The industrial defect classification method based on contrastive learning and feature decoupling according to claim 1, characterized in that, The defect classifier adopts a fully connected layer, and is constrained by a cross-entropy loss function, so that the defect encoder learns defect features. The background classifier comprises a gradient reversal layer and a fully connected layer; and a classification loss function The formula is as follows: ; ; ; ; in, , , These represent the defect classification loss, regularization classification loss, and cross-entropy loss functions, respectively. and These represent the category prediction probabilities of the defect classifiers for the first and second groups of data, respectively. and Respectively represent and and The corresponding defect category label vector; and These represent the category prediction probabilities of the background classifiers for the first and second groups of data, respectively. and Respectively represent and and The corresponding defect category label vector; Representative sample Category The label vector; Representative model predicts samples Category The probability of; Represents the total number of categories.

7. The industrial defect classification method based on contrastive learning and feature decoupling according to claim 1, characterized in that, Total loss function of the dual-path contrastive decoupled network model is: 。

Citation Information

Patent Citations

  • Surface defect identification method based on semi-supervised learning

    CN118333992A

  • Testing system and method for in chip decoupling capacitor circuits

    US11131711B1