Surface defect detection method based on generative adversarial network

Through methods based on generative adversarial networks and Transformer refinement technology, the problems of inaccurate defect contours and noise interference in unsupervised surface defect detection are solved, and accurate detection and high-quality segmentation of small-area defects are achieved, which is suitable for the number of samples. Less industrial scenes.

CN118864342AActive Publication Date: 2024-10-29CHINA UNIV OF PETROLEUM (EAST CHINA)

Patent Information

Application Number
CN202310484962.8
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-04-28
Publication Date
2024-10-29
Estimated Expiration
2043-04-28

AI Technical Summary

Technical Problem

Existing unsupervised surface defect detection methods have inaccuracies and noise interference when detecting defect contours. In particular, they have poor detection results for defects with small areas and cannot meet the needs of practical applications.

Method used

Using a method based on generative adversarial networks, by training a generative network model with a vector quantizer and a discriminator, and combining it with the Transformer's refinement method, a specialized defect segmentation module is designed to achieve accurate defect contour segmentation of the image to be detected.

Benefits of technology

It significantly improves the accuracy of defect segmentation maps, reduces noise in reconstruction residuals, effectively finds defects with smaller areas, improves defect segmentation quality, and is suitable for scenarios where samples lack manual annotation.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118864342A_ABST
    Figure CN118864342A_ABST
Patent Text Reader

Abstract

The invention relates to the field of image processing, and discloses a surface defect detection method based on a generative adversarial network. The method comprises the following steps: constructing a surface defect detection model based on an unsupervised reconstruction thought, introducing a vector quantization variational automatic encoder into the defect detection model to obtain discrete feature representation of an input image, reconstructing a sample in an adversarial training mode, and using various loss functions according to a preset threshold to enable the model to repair a defect sample. A method based on Transform'refinement 'is provided to carry out masking processing on some unconfidence positions in a defect sample feature index sequence, Transform is utilized to carry out multiple times of prediction on the masked positions, then the masked positions are decoded into images, and averaging operation is carried out to achieve the purpose of'refinement' of the images. Meanwhile, the invention provides a novel defect segmentation method for carrying out adaptive threshold segmentation on the difference feature map by using an OSTU algorithm, so that the purpose of accurate defect segmentation is achieved. Compared with the prior art, the method not only can obtain a more complete reconstructed image, but also has a more excellent defect accurate segmentation effect, avoids manual annotation, solves the problem of high false detection rate and omission rate in the detection process of performing model training by singly using a convolutional neural network in an industrial scene to a certain extent, and improves the detection efficiency. And meanwhile, accurate and rapid segmentation of various defects can be realized, and the method has practical application value.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of computer vision, and in particular relates to a surface defect detection method based on a generative adversarial network. Background Art

[0002] In industrial manufacturing production scenarios, surface defect detection is a key link in the actual production process. The presence of surface defects in products will not only affect the appearance quality of the products, but will also directly affect the functions and performance of the products, and even lead to serious consequences, causing losses to the company to a certain extent. In order to effectively ensure the yield rate of products entering the market and maintain a good corporate image, it is particularly important to conduct surface defect detection on products.

[0003] The traditional manual visual method used to identify surface defects is based on naked eye observation and relies on experience to detect defects. Not only is the recognition effect easily affected by subjective factors, but it is also costly and inefficient. It cannot meet the real-time detection requirements of industrial production, and cannot adapt to the development trend of industrial informatization. With the development of computer vision in industrial detection, defect detection based on convolutional neural networks has also been widely studied, mainly including target detection, semantic segmentation, detection methods based on generative adversarial networks and autoencoders, and methods based on pre-trained models. Among them, target detection models based on standard detection models SSD, YOLO, and Faster R-CNN usually have good accuracy and speed, but can only locate the position of the defect in the form of a rectangular frame, and cannot extract the accurate defect contour. In some specific detections, they cannot meet market demand well.

[0004] In the paper "Automatic defect detection and segmentation of tunnel surface using modified Mask R-CNN", a tunnel defect detection method based on the semantic segmentation model Mask R-CNN is proposed, adding a path enhancement feature pyramid network (PAFPN) and an edge detection branch to improve the accuracy of network detection; in the invention patent "A surface defect segmentation method based on two-stage incremental learning" (patent number: 202211600304.2), the UNet model is used as the basis for semantic segmentation, and the learning rate is improved from the optimization level of model parameters while overcoming the catastrophic forgetting problem, so that it can be better applied to surface defect segmentation. Although the semantic segmentation models Mask R-CNN, UNet, SegNet, etc. can locate defects and segment the actual contours of defects, they are all based on supervised learning methods, and a large number of samples and pixel-level annotations are required in the training stage. In actual industrial scenarios, the number of defect samples is usually small, and the cost of manual annotation is high and easily affected by subjective factors.

[0005] Unsupervised defect detection methods do not require manual labeling of samples during the training phase and have attracted attention in recent years. In the paper "Medical glass bottle mouth defect detection method based on convolutional autoencoder", a convolutional autoencoder network defect detection model with an encoder-decoder structure is established, and the convolutional attention modules of spatial and channel attention are embedded in the encoder to enhance the network feature extraction capability. The multi-scale structural similarity MS-SSIM and L1 loss are combined to improve the image reconstruction effect, thereby achieving accurate and efficient automated product quality inspection. In the paper "Automatic fabric defect detection with amulti-scale convolutional denoising autoencoder network model", a convolutional denoising autoencoder network is used to reconstruct image blocks in multiple Gaussian pyramid layers, and the detection results from each resolution channel are integrated. The reconstructed residual of each image patch is used as an indicator for direct pixel-level prediction. The reconstructed residual map of each resolution level is segmented and integrated to generate the final detection result. In the patent "An unsupervised defect detection method for photovoltaic modules based on an improved GAN algorithm" (patent number: 202010135948.3), an SSIM-GAN model is constructed, and the encoder network generates a latent space corresponding to the input normal image. The structural similarity is used to describe the difference between images to determine whether the image has defects.

[0006] The above-mentioned unsupervised defect detection method is mainly based on the structure of the autoencoder model and uses the reconstruction error to determine whether the image to be detected has defects. When the structure of the target to be detected is complex, the reconstruction residual of the model on the image to be detected is usually large, especially in areas without defects. There are also large image reconstruction residuals. In addition, directly constructing the defect contour segmentation map based on the reconstruction residual is usually interfered by the noise residual, making it difficult to accurately detect defects with a small area, and the defect contour segmentation map is inaccurate and cannot meet the actual application needs. Summary of the invention

[0007] The purpose of the present invention is to overcome the above shortcomings and provide a defect detection method based on a generative adversarial network. A generative network model with a vector quantizer and a discriminator is trained based on defect-free samples, and a special defect segmentation module and a refinement method based on a Transformer are designed to obtain a defect segmentation map with accurate defect contours of the image to be detected. The step flow chart of the detection method can be found in the attached Figure 1 , which solves the problems of inaccurate defect segmentation maps and high missed detection rate of defects with small areas in existing unsupervised surface defect detection methods.

[0008] The disclosed defect detection method comprises the following steps:

[0009] S1. Use normal, defect-free samples to train a generative adversarial network with the following structure:

[0010] (1) The network consists of an encoder, a vector quantizer, a decoder, and a discriminator in sequence;

[0011] (2) The encoder and decoder contain SE-ResNet modules and SAGAN-based self-attention modules (AttnBlock). The SE-ResNet module adds the results of the input feature map x after the residual module and ShortCut operation to obtain a new feature map, and then performs global pooling. Next, two convolution operations with a convolution kernel size of 1 are performed, and the Sigmoid activation function is used to obtain the importance of each feature channel. The features before pooling are combined based on the channel importance. The self-attention module (Attn Block) takes the convolution feature map x as the input feature vector, and uses a convolution layer with a convolution kernel size of 1 for linear transformation. The linear transformation outputs g(x), f(x), and h(x) correspond to Q, K, and V of the Transformer model, and outputs the self-attention result.

[0012] (3) There is a vector quantizer between the encoder and the decoder. The vector quantizer uses a specific embedding space to match and replace the output features of the encoder. In a dictionary learning manner, each dimension of the encoder output feature is sampled and replaced from the embedding space according to the probability size, thereby obtaining a discrete feature map of the input image.

[0013] (4) After the decoder, the discriminator is connected. The discriminator uses the discriminator of the PatchGAN model, which outputs a matrix. Each element in the matrix is ​​an evaluation of a local area (receptive field) in the image.

[0014] S2, preprocess the image to be tested according to the model's requirements for the input image;

[0015] S3. Define a Transformer-based image refinement method to resample the feature map index after vector quantization;

[0016] S4, using the trained generative adversarial network in step S1 combined with the refinement method invented in step S3 to reconstruct the image to be detected after being processed in step S2;

[0017] S5. Input the reconstructed image obtained in step S4 and the image to be detected in step S2 into the defect segmentation device of the invention to obtain a defect segmentation map with accurate defect contours.

[0018] As a preferred embodiment, the training process of the generative adversarial network described in step S1 can be found in the attached Figure 2 , specifically:

[0019] S11. Prepare normal and defect-free pictures;

[0020] S12: Build a training data set. This step is implemented through the following sub-steps:

[0021] S121, resize the images in the training set: first grayscale the original images and resize them to 256×256, and then randomly crop the images to a size of 224×224 that is suitable for the model;

[0022] S122, according to the size of the training data set, select random horizontal flipping, Mix-up, Mosaic and other image enhancement methods to perform data expansion on the resized image;

[0023] S13, build a generative adversarial network model, this step is implemented by the following sub-steps:

[0024] S131, constructing a generator network structure; the generator is constructed based on a vector quantization variational autoencoder (VQ-VAE), and is composed of an encoder, a vector quantizer, and a decoder connected in sequence. This step is implemented by the following sub-steps:

[0025] S1311, constructing the encoder network structure; the encoder includes four modules, namely SE-ResNet (Squeeze-and-Excitation-Residual Network), three downsampling modules (Downsample), a self-attention module (Attn Block) based on the SAGAN network, and an intermediate layer module (Mid Block) connected in sequence; the structure of the downsampling module is two SE-ResNet modules, between which a downsampling convolution layer with a convolution kernel size of 3 and a step length of 2 is used; the intermediate layer module is two SE-ResNet modules, between which a self-attention module is used; the SE-ResNet module combines the SE module and the Residual module, the SE module can suppress unimportant channels, and the Residual module allows the establishment of a deeper network without gradient disappearance; the self-attention module solves the problem of limited convolution receptive field and also makes calculation easier; the activation function of the encoder is the Swish activation function, and the calculation formula is:

[0026] f(x) = x·sigmoid(x) (1)

[0027] S1312, constructing a vector quantizer: mapping the continuous features after picture encoding to a discrete feature space;

[0028] (1) The input image is output by the encoder as z e(x), the number of channels is converted to the embedding space through convolution with a convolution kernel size of 1. The number of vectors in K is denoted by z e ′(x), then for z e The channel dimension of ′(x) performs GumbelSoftmax operation, specifically:

[0029]

[0030] Among them, g i =-log(-log(u i )),u i ~U(0,1) is called Gumbel noise, which aims to add randomness to increase exploration; π i represents the probability of the i-th vector being selected in the embedding space, z i represents the sampling probability of the i-th vector in the embedding space. When τ is smaller (τ→0), the entire softmax function is closer to argmax, that is, the z vector is closer to the One-hot distribution;

[0031] (2) Select the subscript k with the largest component in the z vector, and then look up the table to convert e k The vector z is used as the decoder input q (x), that is:

[0032] z q (x) = e k , where k = argmax j z (3)

[0033] S1313, construct a decoder structure: the decoder includes a SE-ResNet, a middle layer module (MidBlock), a self-attention module (Attn Block) based on the SAGAN network, three upsampling modules (UPsample), and a convolution layer with a 3×3 convolution kernel connected in sequence; the upsampling module uses two SE-ResNet modules, and a combination of nearest neighbor interpolation and convolution is adopted between them; the activation function of the decoder is a Swish function;

[0034] S132, constructing a discriminator network: the discriminator includes five sublayers, the first four sublayers are used to extract features of the input image, each sublayer is composed of a convolutional layer, a normalization layer, and an activation layer, and the last sublayer is composed of a convolutional layer and an activation layer, where the convolutional layer is used to change the number of channels of the feature map to 1; the output of the discriminator is an N×N matrix, and each value in the matrix represents the evaluation of an area (receptive field) in the image; the activation function of the discriminator is LeakyReLU, and the calculation formula is:

[0035] f(x)=max(αx,x) (4)

[0036] In formula (4), α is a relatively small positive value, and in the present invention, it is set to 0.2;

[0037] The discriminator normalization layer uses a group normalization function, which is calculated as:

[0038]

[0039] In the above formula (5), x i represents the i-th group of data, μ G represents the mean of the i-th group of data, Represents the variance of the i-th group of data, ∈ is a small positive value, usually 10 -6 , γ and β represent learnable parameters;

[0040] S133, the generator and the discriminator constitute a generative adversarial network module;

[0041] S14, training the generative adversarial network model, this step is implemented by the following sub-steps:

[0042] S141, input the image pre-processed in step S12 into the encoder;

[0043] S142, the encoder outputs a feature map of size 28×28×C (C represents the number of feature channels);

[0044] S143, replacing the above feature map with a new feature map composed of vectors in the embedding space through a vector quantizer;

[0045] S144, inputting the new feature map generated in step S143 into the decoder to reconstruct the input image;

[0046] S145. Input the preprocessed input image and the reconstructed image into the discriminator, and calculate the generator loss and the discriminator loss respectively. The generator loss is:

[0047] L G =λ1 L rec +λ2L ssim +λ3L KL +λ4L adv (6) In the above formula (6), L rec is the reconstruction loss, defined as:

[0048] L rec =E x~p(x) [||xG(x)||1] (7)

[0049] In the above formula (6), L ssimIt is a structural loss. Considering that the SSIM index measures the similarity between two images from the brightness, contrast and texture structure of the image, the SSIM-related loss is introduced to improve the quality of the generated image and speed up the convergence of the network. The loss is defined as:

[0050]

[0051] In the above formula (8), μ x represents the mean of x, σ x represents the standard deviation of x, p x represents the distribution of real data, μ x ,μ G(x) are the average grayscale of each pixel of the input image and the reconstructed image calculated by sliding window, σ xG(x) is the covariance of the contrast parameters of the original input image x and the reconstructed image G(x);

[0052] In the above formula (6), L adv The adversarial loss introduced to further improve the quality of the generated images is defined as:

[0053] L adv =E x~p(x) (D(G(x))-1) 2 (9)

[0054] In the above formula (6), L KL is the KL divergence loss, which is a constraint on the posterior distribution and prior distribution in the variational autoencoder and is defined as:

[0055] L KL =D KL (q φ (z|x)||p θ (z)) (10)

[0056] where q φ (z|x) represents z e ′(x) is the probability distribution normalized in the channel dimension, and pθ(z) represents the sampling distribution of the vectors in the assumed embedding space that follows a uniform distribution.

[0057] The discriminator loss is:

[0058] L D =E x~p(x) (D(x)-1) 2 +E x~p(x) (D(G(x))-0) 2 (11)

[0059] S146. According to the generator and discriminator loss functions, use Adam as the optimizer of the generator and the discriminator to update the weights of the generative adversarial network;

[0060] S15. Save the parameters and trained weights in the adversarial network generated in step S14.

[0061] Preferably, the specific process of preprocessing the test set images in step S2 is as follows:

[0062] S21, resize the test set image: first grayscale the original image and resize it to 256×256;

[0063] S22, cropping the center of the image obtained in step S21 to a size of 224×224;

[0064] Preferably, the Transformer-based image refinement network structure in step S3 has an activation function of GeLU activation function. The module flow can be found in the attached Figure 4 , the specific steps are as follows:

[0065] S31. Train the Transformer-based image refinement network module. This step is implemented through the following sub-steps:

[0066] S311, serializing the embedded spatial index feature map obtained after vector quantization, randomly selecting 25% of the positions in the index sequence, and at the same time, 80% of the probability of masking the selected index, 10% of the probability of randomly replacing the current position index, and 10% of the probability of keeping it unchanged;

[0067] S312, using the index sequence processed in step S311 as the input of the Transformer;

[0068] S313, using Transformer to predict a new index of the selected position;

[0069] S314. Calculate the cross entropy loss function between the index of the selected position before masking and the new index after Transformer prediction, using the AdamW optimizer, where β1 = 0.9, β2 = 0.95, the learning rate is cosine annealed from 0.0003 to 0.00005 after 100 epochs, and the maximum training epoch is 200;

[0070] S32, saving the parameters and trained weights in the Transformer-based refined network module obtained in step S31;

[0071] S33, using the training result of step S31, the image to be detected preprocessed in step S2 is encoded and vector quantized to obtain a sampling probability map, and the index values ​​corresponding to the positions with probabilities less than 0.5 are masked;

[0072] S34, inputting the masked index sequence in step S33 into the Transformer, and using the Transformer to predict the new index at the masked position;

[0073] S35, the new index sequence is input to the decoder and decoded into a reconstructed new picture, and the masked index sequence is resampled 8 times, and steps S33 and S34 are performed in sequence to obtain 8 new pictures;

[0074] S36: Perform an arithmetic average operation on the eight images obtained in step S35, and finally obtain a refined image as a reconstructed image of the original image to be detected.

[0075] Preferably, the step S4 reconstructs the input image to be detected, and the specific steps are as follows:

[0076] S41, read the trained generative adversarial network trained in step S1;

[0077] S42, reading the Transformer-based image retouching module trained in step S3;

[0078] S43, inputting the image to be detected after preprocessing in step S2 into the improved network structure to obtain a reconstructed image to be detected.

[0079] Preferably, the novel defect segmentation device proposed by the present invention in step S5, and the defect segmentation process diagram can be found in the attached Figure 5 , specifically including the following steps:

[0080] S51, constructing an SSIM similarity calculation formula to calculate a difference feature map between the reconstructed image and the image to be detected after preprocessing in step S2;

[0081] S511, construct a local SSIM index in a sliding window manner, fix the SSIM window size to 5, traverse the entire image pixel by pixel, and set a Gaussian kernel with a standard deviation of 1.0 for weighted averaging;

[0082] S512, set μ x ,μ G(x) The average grayscale of each pixel of the input image and the reconstructed image obtained by sliding window is used as the brightness measure to calculate the brightness similarity L. (x,G(x)) , the calculation formula is:

[0083]

[0084] S513, calculating contrast similarity C (x,G(x)) , the calculation formula is:

[0085]

[0086] In the above formula (13) It is based on the average brightness of each pixel in the window μ x The calculated standard deviation of each pixel in the window is called the contrast parameter;

[0087] S514, calculating structural similarity s (x,G(x)) , the calculation formula is:

[0088]

[0089] In the above formula (14), σ xG(x) is the covariance of the contrast parameters of the original input image x and the reconstructed image G(x) within the window;

[0090] S515: According to step S512, step S513 and step S514, construct SSIM to measure the similarity between the input image and the reconstructed image, and the expression is:

[0091] SSIM=L (x,G(x)) C (x,g(x)) S (x,G(x)) (15)

[0092] At the same time, the constants C2 and C3 in the above formula (15) of the present invention satisfy the numerical relationship The final local SSIM index expression is obtained:

[0093]

[0094] S515, performing reflect padding with a padding of 2 on the image obtained in step S2 and the reconstructed image in step S4, and then calculating the local structural similarity of the two images in a sliding manner using the SSIM window, and finally obtaining a difference feature map of the same size as the test image;

[0095] S52, using the OSTU algorithm to perform adaptive threshold segmentation on the feature map obtained in step S51 to obtain a preliminary residual map, the specific steps are as follows:

[0096] S521, finding the segmentation threshold t of the foreground and background. This step can be implemented by the following sub-steps:

[0097] S5211, counting the number of each pixel in the reconstructed image in the entire image;

[0098] S5212, calculating the probability distribution of each pixel in the entire image;

[0099] S5213, traverse each gray level, and calculate the probability between foreground and background classes under the current gray value;

[0100] S5214. Establish the objective function: when the segmentation threshold is t, the inter-class variance g(t) is expressed as:

[0101] g(t)=w0×(u0-u) 2 +w1×(u1-u) 2 =w0×w1×(u1-u0) 2 (17)

[0102] In formula (17), w0 refers to the number of foreground pixels, w1 refers to the number of background pixels, u0 refers to the weighted average of foreground, u1 refers to the weighted average of background, and u = u0 × w0 + u1 × w1 refers to the weighted average of the entire image;

[0103] S5215, find out t which makes g(t) in step S5214 the largest as the segmentation threshold of foreground and background;

[0104] S522, a value less than the threshold value t obtained in step S521 will be classified as background, and a value greater than or equal to the threshold value t will be classified as a candidate defect, thereby obtaining a residual image with partial noise;

[0105] S53, traversing the maximum value of the area of ​​the candidate defect region in each residual image, and calculating the area value with the highest F1 index as the optimal segmentation threshold K;

[0106] S531, finding the candidate defect with the largest area from the residual image in step S52, and counting the maximum defect contour area of ​​each residual image;

[0107] S532, taking the area value that makes the F1 score the highest in step S531 as the optimal threshold K for image segmentation, where the expression of the F1 score is:

[0108]

[0109] In the above formula (18), precision is the accuracy in the confusion matrix, and recall is the recall rate in the confusion matrix;

[0110] S54, using the optimal segmentation threshold K obtained in step S53 to perform binarization processing on the residual image in step S52, to obtain a more accurate defect segmentation image with most of the noise removed;

[0111] S541, if the areas of all candidate regions in the image in step S52 are smaller than the optimal segmentation threshold K obtained in step S53, the image will be judged as defect-free; if the area of ​​the defect contour in the image is larger than the optimal segmentation threshold K in step S53, the image will be judged as a defective image;

[0112] S542. If the detected image in step S541 is determined to be a defective image, the grayscale values ​​within the contours of the image that are less than the optimal segmentation threshold are all set to 0, so as to obtain a defect segmentation map with most of the noise removed and accurate defect locations.

[0113] In summary, the distinguishing technical features of the technology disclosed in the present invention include at least:

[0114] 1. The present invention uses the OSTU algorithm to perform adaptive threshold segmentation on the difference feature map, finds the area value with the highest F1 score as the optimal threshold K for determining whether there is a defect, and then binarizes the image determined to be defective to obtain a defect segmentation map with most of the noise removed and an accurate defect outline, providing a novel defect segmentation method that can effectively remove noise in the reconstruction residual and effectively discover defects with smaller areas;

[0115] 2. The model disclosed in the present invention contains a vector quantizer, which can encode the input image through a sparse coding space to obtain a discrete feature space, which can play a regularization role, reduce the occurrence of overfitting in the training stage, improve the quality of reconstructed images, and lay the foundation for the image refinement module;

[0116] 3. The present invention provides a Transformer-based image refinement method, which serializes the index feature map, masks the untrusted indexes in the index sequence, and uses Transformer to predict the new index at the masked position, thereby solving the problem of large reconstruction residuals caused by the defect position in the test phase not being trained in the training phase.

[0117] Compared with the prior art, the present invention has the following beneficial effects:

[0118] 1. A new defect segmentation method provided by the present invention enables the defect segmentation map to have a more accurate defect contour, thereby effectively removing the noise in the reconstruction residual and significantly improving the defect segmentation quality;

[0119] 2. On this basis, the Transformer-based image refinement method proposed improves the image quality reconstructed by the vector quantization variational autoencoder, effectively reduces the missed detection of defective areas, and makes up for the low accuracy of existing defect detection methods for specific area defects;

[0120] 3. The technology disclosed in the present invention is an unsupervised defect detection method based on a generation method, which only requires normal samples without defects and is suitable for scenarios where samples lack manual annotation and the number of defective samples is small. BRIEF DESCRIPTION OF THE DRAWINGS

[0121] Figure 1 A flow chart of the steps of a surface defect detection method based on a generative adversarial network provided by the present invention

[0122] Figure 2 Flowchart for training a generative adversarial network

[0123] Figure 3 Unsupervised defect detection model for the testing phase

[0124] Figure 4 Flowchart of the Transformer-based image refinement network module

[0125] Figure 5 Schematic diagram of the defect segmentation process of the present invention

[0126] Figure 6 Example of using a data set for an embodiment of the present invention

[0127] Figure 7 Defect segmentation results for experimental category 6 dataset

[0128] Figure 8 Defect segmentation results for experimental category 7 dataset

[0129] Fig. 9 An example of the segmentation results of the supervised and unsupervised algorithms for the comparison test category 6

[0130] Fig.10 An example of the segmentation results of the supervised and unsupervised algorithms for the comparison test category 6

[0131] Fig.11 This is an example of missed detection.

[0132] Fig.12 The defect segmentation effect after adding the "fine-tuning" module DETAILED DESCRIPTION

[0133] In order to make the purpose, technical scheme and advantages of the present invention clearer, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention. Unless otherwise defined, all technical and scientific terms used herein have the same meaning as those commonly understood by those skilled in the art of the present invention.

[0134] The present invention is further described below in conjunction with the accompanying drawings and embodiments.

[0135] Embodiment 1:

[0136] A dataset is selected to test the effectiveness of the disclosed method. In order to simulate the scarcity of annotated data in real scenes, an industrial defect detection dataset DAGM2007 is selected, which contains ten categories of artificially generated texture defects. Each of the first six categories has 1,000 defect-free images and 150 defective images, and each of the last four categories has 2,000 defect-free images and 300 defective images. In order to better restore the actual scenes in the field of industrial inspection, two representative categories, Class 6 and Class 7, are selected from the dataset for experiments. The surface texture of Class 6 is relatively messy and the defect area is large, while Class 7 has small defects. See the image examples. Figure 6 , the column where Class6 is located represents the defect samples and defect annotations of category 6, and the column where Class7 is located represents the defect samples and defect annotations of category 7. The two categories of data sets are divided into test set, training set, and validation set, where "N" represents "Normal" normal defect-free images, and "P" represents "Paranormal" defective images. The following Table 1 lists the specific number of images and image categories.

[0137] Table 1 DAGM2007 dataset division

[0138]

[0139] The commonly used recall rate, precision rate, accuracy rate, and F1 score in classification problems are used as measurement indicators. The true positive examples (TP) are the number of defective images that are correctly classified, the false negative examples (FN) are the number of defective images that are classified as defect-free images, the false positive examples (FP) are the number of defect-free images that are classified as defective images, and the true negative examples (TN) are the number of defect-free images that are correctly classified. The following Table 2 lists the evaluation indicators of this embodiment.

[0140] Table 2 Evaluation indexes of Example 1

[0141]

[0142] See also Figure 1The flow chart of the surface defect detection method is provided. The input image is preprocessed first: the grayscale image of 512×512 provided by the dataset is resized to 256×256. To improve the robustness of the model, the image is further randomly cropped to 224×224 and randomly flipped horizontally for image enhancement. Adam is used as the optimizer for the generator and discriminator with β1=0.5 and β2=0.9. The maximum epoch of training is set to 200, and early stopping is performed according to the reconstruction loss of the validation set. The embedding space contains 64 vectors, each with a dimension of 64. The initial learning rate of the generator is set to 0.0002, and the learning rate is reduced to 0.00001 through cosine annealing for 100 epochs. The initial learning rate of the discriminator is set to 0.0001. In formula (3), λ1=1.0, λ2=0.5, and λ4 is increased from 0 to 0.0005 after 50 epochs of cosine annealing. λ3 follows the calculation formula:

[0143]

[0144] In the above formula (16), G L represents the weight parameter of the last layer of the decoder, and the numerator represents the reconstruction loss (including SSIM loss) relative to G L The second norm of the gradient of G, the denominator represents the adversarial loss relative to G L The second norm of the gradient of , δ is set to 0.0001 to prevent the denominator from being zero.

[0145] See also Figure 3 The schematic diagram of the unsupervised surface defect detection method provided is an experiment on the preprocessed dataset image, presenting the defect detection experiment results of the surface defect detection method proposed by the present invention on the DAGM2007 dataset category 6 and category 7. Table 3 below lists the experimental results of various indicators of this embodiment.

[0146] Table 3 Model performance index results

[0147]

[0148] For some defect samples of category 6, see the defect segmentation effect. Figure 7 , for some defect samples of category 7, see the defect segmentation effect Figure 8 , column (a) is the original image, column (b) is the reconstructed image, column (c) is the original label, column (d) is the pixel binarization image of the test image and the reconstructed image extracted using the OSTU algorithm, and column (e) is the defect segmentation image after thresholding based on the maximum difference contour area. Figure 6 and Figure 7 The segmentation effect shown shows that the defect detection method disclosed in the present invention can effectively segment defects with larger areas for images with complex backgrounds; Figure 8The segmentation results shown indicate that the defect detection method disclosed in the present invention can effectively segment defects with smaller areas.

[0149] Embodiment 2:

[0150] The disclosed method is compared with several other mainstream defect detection methods, including f-AnoGan, PatchCore, CycleGan and Unet, among which f-AnoGan and PatchCore are unsupervised models, CycleGan is a semi-supervised model, and Unet is a supervised model. In addition, the defect segmentation maps of f-AnoGan and CycleGan are the residual maps between the test image and the reconstructed image.

[0151] f-AnoGan and PacthCore use the same data set for training. In the f-AnoGan model, the training samples are divided into 56*56 images. In the test phase, the 224*224 images are divided into 16 56*56 images. A 100-dimensional random vector is used to generate images. The reconstructed images are then sequentially spliced ​​into 224*224 images. At the same time, the largest defect score among the 16 images is selected as the defect score of the test image. In the CycleGan and Unet models, due to the imbalance between the number of defective samples and the number of non-defective samples, the training set and test set originally divided by DAGM2007 in Example 1 are used for training and reasoning. The specific sample distribution is as follows: the Class6 training set includes 492 non-defective samples and 83 defective samples, and its test set includes 508 non-defective samples and 67 defective samples; the Class7 training set and test set each include 1000 non-defective samples and 150 defective samples.

[0152] The evaluation index is the AUC value, that is, the area under the ROC curve, which is used to measure the performance of the classifier. The closer the AUC value is to 1, the better the classifier performance is; conversely, the closer the AUC value is to 0, the worse the classifier performance is. Table 4 below lists the AUC index comparison experimental results of each supervised and unsupervised algorithm.

[0153] Table 4 Comparison of experimental results of AUC indicators of each model

[0154]

[0155] From the comparative experimental results in Table 4 above, it can be seen that the performance of the surface defect detection model provided by the present invention in the defect classification task is significantly better than the unsupervised model f-AnoGan and the semi-supervised model CycleGan, and the AUC indicators are improved by 0.2409 and 0.1112 respectively; the performance is comparable to that of Unet based on supervised learning and the current unsupervised defect detection SOTA model PatchCore.

[0156] The effects of unsupervised and supervised algorithms on defect segmentation on Class6 and Class7 datasets are respectively shown in Fig. 9 and Fig.10 , where Figure (1) shows an example of the segmentation effect of the unsupervised algorithm on the data set, where (a) represents the original defect image, (b) represents the label of the defect image, (c) represents the segmentation result of f-AnoGan, (d) represents the segmentation result of PatchCore, and (e) represents the segmentation result of the present invention; Figure (2) shows an example of the segmentation effect of the supervised algorithm on the data set. Where (a) represents the original defect image, (b) represents the label of the defect image, (c) represents the segmentation result of CycleGan, (d) represents the segmentation result of Unet, and (e) represents the segmentation result of the present invention. From the defect segmentation effect, the segmentation results of the present invention and PatchCore are the best. The f-AnoGan and CycleGan models perform poorly on the Class7 data set with a smaller defect area, while Unet segments an area as large as possible. In addition, although PatchCore performs well in both classification and segmentation, it uses a pre-trained model based on ImageNet for inference, which limits the size of the input image. The samples collected in the actual production process do not necessarily meet the input size of the pre-trained model, and the input image needs to be cut, which prolongs the inference time in the testing phase.

[0157] Embodiment three:

[0158] Considering that the defect detection performance based on the image reconstruction concept strongly depends on the repair effect of the test image, the better the repair effect, the simpler and more effective the defect segmentation. For the Class 6 images with large-area defects involved in the first and second embodiments, when the method disclosed in the present invention does not use the refinement operation, there is a certain degree of missed detection. For example, see Fig.11 , where (a) represents the defect image, (b) represents the reconstructed image, (c) represents the defect annotation, (d) represents the SSIM residual image of (a) and (b), and (e) represents the defect segmentation result.

[0159] The Transformer-based image retouching method provided by the present invention can effectively improve the restoration effect and obtain a better reconstructed image. Figure 4In the training phase, 25% of the positions (784*0.25=196) in the index sequence of the training set are randomly selected, and the selected index is masked with 80% probability, the current position index is randomly replaced with 10% probability, and the current position index is kept unchanged with 10% probability. The processed index sequence is used as the input of the Transformer, and its target is the original unprocessed index sequence. The loss function is the cross entropy loss function at the selected position. The optimizer uses AdamW, where β1=0.9, β2=0.95, and the learning rate is cosine annealed from 0.0003 to 0.00005 after 100 epochs. The maximum training epoch is 200.

[0160] During the test phase, based on the sampling probability feature map obtained by vector quantization, the index values ​​corresponding to the positions with probabilities less than 0.5 are masked, and the masked index sequence is input into the Transformer. The 64-dimensional vector output at the masked position is annealed at a temperature τ of 0.8 and then normalized. The first 16 output values ​​with larger probabilities are retained, and then eight samples are taken according to the probability values ​​to obtain eight new feature index sequences. These eight feature sequences are decoded into images respectively, and finally the eight images are arithmetic averaged to obtain a refined image.

[0161] The evaluation index uses the dice index commonly used in semantic segmentation to evaluate the performance of model defect detection. The calculation method is: Where TP is the number of defective pixels correctly classified, FN is the number of defective pixels classified as non-defective pixels, and FP is the number of non-defective pixels classified as defective pixels. The closer the dice index is to 1, the better the segmentation effect. Table 5 below lists the comparison of experimental results.

[0162] Table 5 Comparison of segmentation results before and after adding the refinement module

[0163]

[0164] As can be seen from Table 5 above, the defect segmentation effect has been significantly improved after Transformer refinement, the missed detection area has been significantly reduced, the defect segmentation dice index has increased by about 0.3 percentage points, the defect classification index AUC has increased by 0.4 percentage points, and the recall rate has reached 100%, indicating that the Transformer model refinement method can effectively improve the defect detection effect of the model.

[0165] Experimental results refer to Fig.12 , where (a) is the defect image, (b) is its defect annotation, (c) is the segmentation result of the model disclosed in the present invention without using Transformer refinement, and (d) is the defect segmentation result after using Transformer refinement.

[0166] The above description describes the specific implementation of the present invention in combination with the diagrams, but does not limit the scope of protection of the present invention. Based on the technical solution of the present invention, various modifications or variations that can be made by relevant technicians without creative work are still within the scope of protection of the present invention.

Claims

1. A surface defect detection method based on generative adversarial network, characterized in that: The following steps are involved: S1. Use normal, defect-free samples to train a generative adversarial network with the following structure: (1) The network consists of an encoder, a vector quantizer, a decoder, and a discriminator in sequence; (2) The encoder and decoder contain SE-ResNet modules and SAGAN-based self-attention modules (AttnBlock). The SE-ResNet module adds the results of the input feature map x after the residual module and ShortCut operation to obtain a new feature map, and then performs global pooling. Next, two convolution operations with a convolution kernel size of 1 are performed, and the Sigmoid activation function is used to obtain the importance of each feature channel. The features before pooling are combined based on the channel importance. The self-attention module (Attn Block) takes the convolution feature map x as the input feature vector, and uses a convolution layer with a convolution kernel size of 1 for linear transformation. The linear transformation outputs g(x), f(x), and h(x) correspond to Q, K, and V of the Transformer model, and outputs the self-attention result. (3) There is a vector quantizer between the encoder and the decoder. The vector quantizer uses a specific embedding space to match and replace the output features of the encoder. In a dictionary learning manner, each dimension of the encoder output feature is sampled and replaced from the embedding space according to the probability size, thereby obtaining a discrete feature map of the input image. (4) After the decoder, the discriminator is connected. The discriminator uses the discriminator of the PatchGAN model, which outputs a matrix. Each element in the matrix is ​​an evaluation of a local area (receptive field) in the image. S2, preprocess the image to be tested according to the model's requirements for the input image; S3. Define a Transformer-based refinement method to resample the feature map index after vector quantization; S4, using the generative adversarial network trained in step S1 combined with the refinement method in step S3 to reconstruct the image to be detected after being processed in step S2; S5. Input the reconstructed image obtained in step S4 and the image to be detected in step S2 into a defect segmentor to obtain a defect segmentation map of the image to be detected.

2. The surface defect detection method based on generative adversarial network according to claim 1, characterized in that: In step S1, there is a vector quantizer with the following structure between the encoder and the decoder of the generative adversarial network: (1) The vector quantizer stores and updates the embedding space It contains K D-dimensional vectors, Let the output of the encoder be z. e (x), the vector quantizer first changes its channel number to K through a convolution operation with a convolution kernel size of 1. The output of the convolution operation is denoted as z e ′(x), then z e The channel dimension of ′(x) performs a GumbelSoftmax operation. Specifically, the sampling probability of the i-th vector in the embedding space E is Among them, g i =-log(-log(u i )),u i ~U(0,1) is Gumbel noise, π i represents the probability of the i-th vector being selected in the embedding space; τ>0 is a parameter. The smaller τ is, the closer the softmax function is to argmax, that is, the z vector is closer to the One-hot distribution; (2) Select the subscript k with the largest component in the z vector and convert e k As the input of the decoder z q (x), that is, z q (x) = e k , where k = argmax j z; (3) z q (x) is used as the input of the decoder to obtain the discrete feature space of normal samples.

3. The surface defect detection method based on generative adversarial network according to claim 1 is characterized in that: In step S3, the feature index after vector quantization is resampled based on the Transformer method, specifically: S31. Use normal samples to train the Transformer-based image refinement network module. This step is implemented through the following sub-steps: S311, serializing the embedded spatial index feature map obtained after vector quantization, randomly selecting 25% of the positions in the index sequence, and at the same time, 80% of the probability of masking the selected index, 10% of the probability of randomly replacing the current position index, and 10% of the probability of keeping it unchanged; S312, using the index sequence processed in step S311 as the input of the Transformer; S313, using Transformer to predict a new index of the selected position; S314, calculating the cross entropy loss function between the index of the selected position before masking and the new index predicted by Transformer; S32, saving the parameters and trained weights of the Transformer-based refined network module obtained in step S31; S33, using the training result of step S31, the image to be detected pre-processed in step S2 is processed by an encoder and a vector quantizer to obtain a sampling probability map, and the index values ​​corresponding to the positions where the probability is less than 0.5 are masked; S34, inputting the masked index sequence in step S33 into the Transformer, and using the Transformer to predict the new index at the masked position; S35, the new index sequence is input to the decoder and decoded into a reconstructed new picture; S36, performing steps S33, S34 and S35 on the masked index sequence in sequence, repeating the steps 8 times, performing 8 resamplings, and outputting 8 pictures; S37: Perform an arithmetic average operation on the eight images obtained in step S36 to obtain a refined image as a reconstructed image of the original image to be detected.

4. The surface defect detection method based on generative adversarial network according to claim 1, characterized in that: The processing steps of the defect segmentation device described in step S5 are specifically as follows: S51, construct an SSIM similarity calculation formula to calculate the difference feature map between the reconstructed image and the image to be detected after preprocessing in step S2, where the SSIM similarity is calculated using the local SSIM index, and the calculation formula is: μ x With μ G(x) are the average grayscale of each pixel of the input image and the reconstructed image calculated by sliding window, σ xG(x) is the covariance of the contrast parameters of the original input image x and the reconstructed image G(x); S52, using the OSTU algorithm to perform adaptive threshold segmentation on the feature map obtained in step S51, the steps include: (1) Let the segmentation threshold be t, and establish the objective function g(t) = w0×(u0-u) through the variance between foreground and background classes. 2 +w1×(u1-u) 2 =w0×w1×(u1-u0) 2 ; (2) Find the maximum value of the function g(t) as the threshold for distinguishing foreground from background and construct a residual map; S53, traversing the maximum value of the area of ​​the candidate defect region in each residual image, and calculating the area value that makes the defect classification F1 index the highest as the optimal segmentation threshold K; S54, using the optimal segmentation threshold K obtained in step S53 to perform binarization processing on the residual image in step S52, to obtain a defect segmentation image with most of the noise removed and an accurate defect outline.

Citation Information

Patent Citations

  • Defect target area image positioning segmentation method for rolling contact fatigue defect

    CN115239666A

  • Unsupervised defect detection method based on quantization auto-encoder

    CN115375604A

  • Workpiece CT image defect detection and segmentation method and system

    CN115587989A

  • Integrated circuit metal packaging deep learning defect detection method

    CN115713480A

Cited By

  • Product defect unsupervised semantic segmentation method and device based on pre-training model

    CN119600296A

  • Unsupervised semantic segmentation method and device for product defects based on pre-training model

    CN119600296B

  • Underwater pipeline defect detection method based on improved UNet model

    CN119693318A

  • Wafer defect detection method and device

    CN120431063A

  • Rapid detection method and system for textile defects

    CN120747121A