Surface defect detection method based on optimized memory module and U-Net++
Through the method based on optimized memory modules and U-Net++, the problem of few abnormal samples and low detection accuracy in surface defect detection in industrial images is solved, and high-precision and efficient surface defect detection and positioning are achieved to meet the real-time needs of industry.
Patent Information
- Application Number
- CN202410325962.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-03-21
- Publication Date
- 2024-10-01
- Estimated Expiration
- 2044-03-21
Smart Images

Figure CN118154561B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of industrial image surface defect detection, and in particular to a surface defect detection method based on an optimized memory module and U-Net++. Background Art
[0002] Image anomaly detection and localization tasks aim to identify abnormal images and locate abnormal sub-regions. Techniques for detecting various anomalies of interest have been widely used in industrial inspection. In industrial scenarios, surface defect detection and localization are particularly difficult because there are few abnormal samples and anomalies can range from subtle changes in thin scratches to large structural defects such as missing parts.
[0003] Existing technologies mainly solve this problem in an unsupervised manner, including reconstruction-based methods, synthesis-based methods and embedding-based methods, that is, only normal samples are used in the training process. Among them, the reconstruction-based method believes that the deep network trained only with normal data cannot accurately reconstruct the abnormal area, so the pixel-by-pixel reconstruction error is used as the abnormality score for abnormality positioning; the integrated structural similarity index SSIM loss function is widely used in training, however, this idea may not always hold true. Sometimes the network can "generalize" well and thus reconstruct abnormal inputs well, resulting in errors; the synthesis-based method estimates the decision boundary between normal and abnormal by training on synthetic anomalies generated on non-abnormal images. However, the synthesized images are not realistic enough and may not be comprehensive enough. Features from synthetic data may deviate far from normal features. Using such negative samples for training may result in a normal feature space with loose boundaries, which means that blurred defects may be included in the normal feature space. At the same time, since defects are diverse and unpredictable, it is impossible to generate an abnormal set containing all abnormal values; the embedding-based method uses a network pre-trained on ImageNet for feature extraction and embeds normal features into a compressed space. In the embedding space, abnormal features are far away from normal clusters, and surface defects are identified and located in this way. However, surface defects in real scenes usually have a different distribution from ImageNet. Directly using pre-trained features may cause mismatch problems. In addition, embedding-based methods severely limit real-time performance, especially for edge devices.
[0004] The existing technology has the following shortcomings: there are few abnormal samples, the abnormalities may vary from subtle changes such as thin scratches to large structural defects such as missing parts, and it is impossible to achieve high-quality pixel-level accurate positioning of abnormalities.
[0005] The above information disclosed in this Background section is only for enhancement of understanding of the background of the present disclosure and therefore it may contain information that does not constitute the prior art that is already known to one of ordinary skill in the art. Summary of the invention
[0006] The purpose of the present invention is to provide a surface defect detection method based on an optimized memory module and U-Net++. The present invention solves the problem of industrial image surface defect detection in the above-mentioned background technology by adopting an optimized memory module of a K-means clustering algorithm, a U-Net++ decoding module and an abnormality simulation method of a data augmentation network.
[0007] In order to achieve the above object, the present invention provides the following technical solution: a surface defect detection method based on an optimized memory module and U-Net++, comprising the following steps:
[0008] S1. Preprocess all normal samples, then use the ResNet18 model to extract features from the normal samples, freeze the network parameters of the ResNet18 model feature extraction, and then use the clustering algorithm for the feature maps of different layers of all extracted normal samples to obtain the optimized memory module;
[0009] S2, input the normal training sample into the data augmentation network and use the abnormal simulation strategy to realize the random enhancement of the input sample, and obtain the simulated abnormal image and the real GT image corresponding to the sample;
[0010] S3, input the simulated abnormal samples and normal samples into the model network based on the optimized memory module and U-Net++, the ResNet18 model gives the predicted surface defect abnormality prediction value, and calculates the loss function Loss;
[0011] When S4 and ResNet18 models start training, the loss function is back-propagated to continuously optimize the network parameters of the ResNet18 model. After multiple rounds of iterations, the results of each iteration are tested using the test data set, and the optimal model is saved until the final defect detection model is obtained after all iterations.
[0012] S5. Obtain the test sample and input it into the trained final defect detection model. The defect detection model gives a predicted GT map, thereby obtaining an abnormality location map.
[0013] Optionally, in step S1, the optimized memory module is used to preprocess all normal samples, and the preprocessing steps are as follows:
[0014] All normal samples are resized to 256*256, and the ResNet18 model is used to extract features from normal samples;
[0015] Freeze the network parameters of the ResNet18 model feature extraction, and obtain feature maps of scales [64, 64, 64], [128, 32, 32], and [256, 16, 16] through layer1, layer2, and layer3 of the ResNet18 model, respectively;
[0016] For the feature maps of different layers of all extracted normal samples, the K-means clustering algorithm is used for different layers respectively, and clustering is performed according to the set number of clusters. The features closest to each cluster center are selected and retained in the memory module, thereby obtaining memory modules of different layers. Among them, the memory module stores the normal memory information and is calibrated as MI. The feature definition formula in the memory model of the jth layer in the memory information MI is Where, MI j is the memory model of the memory information MI at the jth layer, K is the number of features in the set memory model, c j is the number of channels of the j-th layer model, h j is the feature height of the j-th layer model, w j is the feature width of the j-th layer model.
[0017] Optionally, in step S2, the steps of obtaining the simulated abnormal image and the real GT image corresponding to the sample are as follows:
[0018] Construct a data augmentation network and use anomaly simulation strategies to enhance the correlation of input normal samples or introduce local irregular anomalies to construct simulated abnormal samples;
[0019] Acquire simulated abnormal samples and corresponding normal samples, as well as labels and real GT images corresponding to the simulated abnormal samples and corresponding normal samples, for subsequent training;
[0020] Data augmentation includes enhancement processing operations such as rotation, bias, sharpness, daylight, equalization, brightness change, color change, and automatic contrast change. The anomaly simulation strategy is based on Perlin noise to generate simulated anomaly samples, and the definition formula is: Where SA is the simulated abnormal sample, α is the opacity parameter, which is uniformly sampled in the interval [0.15, 1.0], M is the mask map of the generated abnormal area, and I is the original image. is the negation of M, is the element-wise multiplication symbol, Z is the noise image, which consists of two parts. One part comes from the DTD texture dataset, the purpose of which is to simulate texture anomalies, and the other part comes from the input image, the purpose of which is to simulate structural anomalies. In order to simulate structural anomalies, the input original image I needs to be randomly adjusted in terms of mirror symmetry, rotation, brightness, saturation and hue, that is, I is the normal sample of the input.
[0021] Optionally, in step S3, the model network based on the optimized memory module and U-Net++ includes an optimized memory module, an image augmentation module, a feature extraction module, an optimal differential information calculation module, a multi-scale fusion module and a U-Net++ decoding module, wherein:
[0022] Optimize memory modules: Perform preprocessing operations such as sample size adjustment, feature extraction, and clustering calculation on all normal samples to obtain memory modules of different layers. The memory modules store normal memory information MI.
[0023] Image augmentation module: Input normal training samples into the data augmentation network and use the abnormal simulation strategy to realize random enhancement of input samples. The data augmentation network is used to realize the relevant enhancement of input normal samples or introduce local irregular anomalies to construct simulated abnormal samples. The simulated abnormal samples and corresponding normal samples are obtained, as well as the labels and real GT images corresponding to the simulated abnormal samples and the corresponding normal samples, which are used for subsequent training;
[0024] Feature extraction module: Receive the abnormal simulation samples and normal samples obtained by the image augmentation module and perform image preprocessing again, adjust the abnormal simulation samples and normal samples to 256*256 size, use the ResNet18 model to extract features of the input image, and save the feature maps extracted from each layer of the ResNet18 model respectively. The size of the feature map of the layer0 layer is [64, 128, 128], the size of the feature map of the layer1 layer is [64, 64, 64], the size of the feature map of the layer2 layer is [128, 32, 32], the size of the feature map of the layer3 layer is [256, 16, 16], and the size of the feature map of the layer4 layer is [512, 8, 8]; and output the feature maps of the layer1 layer, layer2 layer, and layer3 layers as input NI to the optimal differential information calculation module to refine features for distinguishing defects.
[0025] Best differential information calculation module: receives the feature map input NI of layer1, layer2, and layer3, and calculates the distance between NI and all memory information MI in layers, and finds the memory feature closest to the input feature in each layer The difference information formula is calculated as follows: In the formula, the feature map input NI of layer1, layer2, and layer3 is calibrated as F i,j , i represents the i-th input feature, j represents the j-th layer, D i,j It is represented as the difference information of the jth layer of the i-th input feature, and the best difference information D of each layer is i,j Summarize and organize into the best differential information DI * ;
[0026] Multi-scale fusion module: the best differential information DI * The concatenation operation is completed with the feature map input image NI at each layer according to the high-level features on the channel dimension to obtain concatenation information CI1, CI2 and CI3, and the sizes of the concatenation information CI1, CI2 and CI3 are [128, 64, 64], [256, 32, 32], [512, 16, 16] respectively, the concatenation information CI1 is the concatenation of the input sample layer1 layer feature map and the best difference information of the input sample and the memory information MI at the layer1 layer according to the high-level features on the channel dimension, the concatenation information CI2 is the concatenation of the input sample layer2 layer feature map and the best difference information of the input sample and the memory information MI at the layer2 layer according to the high-level features on the channel dimension, and the concatenation information CI3 is the concatenation of the input sample layer1 layer feature map and the best difference information of the input sample and the memory information MI at the layer3 layer according to the high-level features on the channel dimension;
[0027] Each concatenated information CI1, CI2 and CI3 is input into the same-scale fusion block for fusion. The same-scale fusion block includes a coordinate attention mechanism with SE attention mechanism and two convolution blocks. Considering that CI1, CI2 and CI3 are simple splicing of two kinds of information in the channel dimension, the coordinate attention mechanism is used to capture the information relationship between their channels, that is, CI1, CI2 and CI3 are respectively output through the same-scale fusion block to fuse the feature map CI of the best difference information. 1m , CI 2m and CI 3m , and the characteristic graph CI 1m , CI 2m and CI 3m The sizes are [64, 64, 64], [128, 32, 32], [256, 16, 16] respectively;
[0028] Then, upsampling is used to CI feature maps of different dimensions. 1m , CI 2m and CI 3m Resolution alignment is performed to achieve multi-scale fusion. The upsampling module used contains a bilinear interpolation layer and a basic convolution block. Convolution is then performed to align the number of channels of feature maps of different dimensions, and element-by-element addition is performed to achieve multi-scale feature fusion. The specific steps are to convert the feature map CI 3m Input upsampling module, and then with CI 2m Perform element-by-element addition to obtain CI 2n Similarly, CI 2n Input upsampling module, and then with CI 1m Perform element-by-element addition to obtain CI 1n ;
[0029] Secondly, the spatial attention mechanism is used to obtain the spatial attention map. For the best differential information DI * The features at three different levels in the image are averaged in the channel dimension to obtain three feature maps with sizes of 16×16, 32×32 and 64×64 respectively. The 16×16 feature map is directly used as the spatial attention map M3. After the spatial attention map M3 is upsampled, the 32×32 feature map is element-wise multiplied to obtain the spatial attention map M2. Similarly, after the spatial attention map M2 is upsampled, the 64×64 feature map is used to perform element-wise multiplication to obtain the spatial attention map M1. The calculation formulas of the spatial attention maps M1, M2 and M3 are as follows: In the formula, C3, C2, and C1 are respectively expressed as The number of channels, Respectively represented as DI * The best difference between layer3, layer2 and layer1. Expressed as The feature map of channel i, They are represented as the spatial attention map M3 and the spatial attention map M2 obtained after upsampling, Represented as element-wise multiplication notation;
[0030] The final spatial attention map M n , n=1, 2, 3 respectively for CI 1n , CI 2n and CI 3m Perform weighted calculation to obtain multi-scale fusion feature maps f1, f2 and f3, and the sizes of feature maps f1, f2 and f3 are [64, 64, 64], [128, 32, 32], [256, 16, 16] respectively;
[0031] U-Net++ decoding module: the layer0 feature map and layer4 feature map extracted by the ResNet18 model are taken as f0 and f4 respectively, and the feature maps f1, f2 and f3 processed by multi-scale fusion are obtained at the same time. The sizes of the feature maps f0, f1, f2, f3 and f4 are [64, 128, 128], [64, 64, 64], [128, 32, 32], [256, 16, 16], [512, 8, 8] respectively. The feature maps f0, f1, f2, f3 and f4 are taken as the input of each layer of the decoder in the U-Net++ network to decode the U-Net++ network. The decoding of the U-Net++ decoding module starts from the bottom to the top. The U-Net++ decoding module uses an upsampling layer and a normal convolution layer, wherein the upsampling layer includes a bilinear interpolation layer and a basic convolution block, and the basic convolution block consists of a convolution layer, a batch normalization and a ReLU activation function.
[0032] With f 3_1 For example, feature map f4 passes through the upsampling layer, and then jumps to feature map f3, concatenates them according to the channel dimension, and then inputs the ordinary convolution layer to obtain f 3_1 Similarly, using the same calculation method, we can obtain f 2_2 、f 1_3 and f 0_4 , then the feature map f 3_1 、f 2_2 、f 1_3 and f 0_4 The specific calculation formula is as follows: 3_1 =H(f3,U(f4)),f 2_2 =H(f2,f 2_1 ,U(f 3_1 )),f 1_3 =H(f1,f 1_1 ,f 1_2 ,U(f 2_2 )),f 0_4 =H(f0,f 0_1 ,f 0_2 ,f 0_3 ,U(f 1_3 )), where H() represents the convolution calculation of the input ordinary convolution layer, (f3,U(f4)), (f2,f 2_1 ,U(f 3_1 ))、(f1,f 1_1 ,f 1_2 ,U(f 2_2 ))、(f0,f 0_1 ,f 0_2 ,f 0_3 ,U(f 1_3)) respectively represent concatenating each feature according to the channel dimension, and U() represents the input upsampling layer for calculation;
[0033] The final f obtained in the U-Net++ decoding module 0_4 The feature map of size [48, 256, 256] is convolved to obtain a feature map of size [2, 256, 256]. The feature map of [2, 256, 256] is softmax-calculated according to the channel dimension to obtain the prediction result, and the loss function Loss is calculated for the prediction result.
[0034] Optionally, the calculation formula of the loss function Loss is Loss=λ l1 ×L l1 +λ focal ×L focal ,and And L focal =-α t ×(1-p t ) γ ×log(p t ), where y represents the true value of the model, Represented as the model prediction value, α t , γ represent hyper parameters, λ l1 , focal Respectively represented as L l1 , L focal The coefficient of p t It is expressed as reflecting the closeness to the true value y of the model, that is, p t The larger the value, the closer it is to y, and the more accurate the classification; when the GT value of the corresponding pixel in y is 1, p t Equal to the predicted probability p, when the GT value of the corresponding pixel in y is 0, p t Equal to 1-p.
[0035] In the above technical solution, the technical effects and advantages provided by the present invention are:
[0036] The present invention uses an optimized memory module based on a K-means clustering algorithm, thereby avoiding the problem of excessive memory space consumption and long optimal difference information calculation time caused by storing too many normal sample features in the memory model, and also avoiding the problem of storing limited normal samples in the memory model but being too one-sided to cover all normal feature distributions;
[0037] The present invention uses the U-Net++ network for decoding to complete semantic segmentation. By effectively simplifying unsupervised anomaly detection into an end-to-end semantic segmentation task, the unsupervised image surface defect detection is more flexible and can more accurately complete the detection and positioning of surface defects. The U-Net++ network can bridge the semantic gap between the encoding and decoding feature maps, and can effectively improve the semantic segmentation accuracy, thereby improving the surface defect detection and positioning accuracy.
[0038] The present invention uses a data augmentation network and anomaly simulation strategy to process normal images to obtain simulated abnormal images and their corresponding real GT images to provide training data for the model, and introduces normal mode memory information into the U-net++ network structure to assist model learning to achieve surface defect detection. By effectively simplifying unsupervised anomaly detection into an end-to-end semantic segmentation task, the unsupervised image surface defect detection is made more flexible, and the detection and positioning of surface defects can be completed more accurately, effectively improving the accuracy of surface defect detection and positioning. In addition, the surface defect detection method based on the optimized memory module and U-Net++ has a faster reasoning speed, which can better meet the real-time requirements in actual industrial production environments. BRIEF DESCRIPTION OF THE DRAWINGS
[0039] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments recorded in the present invention. For ordinary technicians in this field, other drawings can also be obtained based on these drawings.
[0040] Figure 1 This is a flow chart of the surface defect detection method based on optimized memory module and U-Net++ of the present invention.
[0041] Figure 2 This is a model network structure module diagram based on the optimized memory module and U-Net++ of the present invention.
[0042] Figure 3 This is an example diagram of the bottle data set of the present invention.
[0043] Figure 4 This is a flow chart of model training of the present invention.
[0044] Figure 5 This is a schematic diagram of the memory module optimized for the present invention.
[0045] Figure 6 This is an abnormal simulation flow chart of the present invention.
[0046] Figure 7 This is a clustering architecture diagram of the present invention.
[0047] Figure 8 This is the U-Net++ decoding module diagram of the present invention.
[0048] Fig. 9 This is a flow chart of the application of the model of the present invention.
[0049] Fig.10 FIG. 2 is an application diagram of an embodiment of the present invention. DETAILED DESCRIPTION
[0050] Example embodiments will now be described more fully with reference to the accompanying drawings. However, example embodiments can be implemented in a variety of forms and should not be construed as limited to the examples set forth herein; rather, these example embodiments are provided so that the description of the present disclosure will be more comprehensive and complete, and the concept of the example embodiments will be fully conveyed to those skilled in the art.
[0051] Example 1
[0052] The present invention provides Figure 1-2 The surface defect detection method based on the optimized memory module and U-Net++ shown includes the following steps:
[0053] S1. Preprocess all normal samples, then use the ResNet18 model to extract features from the normal samples, freeze the network parameters of the ResNet18 model feature extraction, and then use the clustering algorithm for the feature maps of different layers of all extracted normal samples to obtain the optimized memory module;
[0054] S2, input the normal training sample into the data augmentation network and use the abnormal simulation strategy to realize the random enhancement of the input sample, and obtain the simulated abnormal image and the real GT image corresponding to the sample;
[0055] S3, input the simulated abnormal samples and normal samples into the model network based on the optimized memory module and U-Net++, the ResNet18 model gives the predicted surface defect abnormality prediction value, and calculates the loss function Loss;
[0056] When S4 and ResNet18 models start training, the loss function is back-propagated to continuously optimize the network parameters of the ResNet18 model. After multiple rounds of iterations, the results of each iteration are tested using the test data set, and the optimal model is saved until the final defect detection model is obtained after all iterations.
[0057] S5. Obtain the test sample and input it into the trained final defect detection model. The defect detection model gives a predicted GT map, thereby obtaining an abnormality location map.
[0058] Specifically, in step S1, the optimized memory module is used to preprocess all normal samples, and the preprocessing steps are as follows:
[0059] All normal samples are resized to 256*256, and the ResNet18 model is used to extract features from normal samples;
[0060] Freeze the network parameters of the ResNet18 model feature extraction, and obtain feature maps of scales [64, 64, 64], [128, 32, 32], and [256, 16, 16] through layer1, layer2, and layer3 of the ResNet18 model, respectively;
[0061] For the feature maps of different layers of all extracted normal samples, the K-means clustering algorithm is used for different layers respectively, and clustering is performed according to the set number of clusters. The number of cluster centers is set according to the actual distribution of normal samples, and the features closest to each cluster center are selected and retained in the memory module, thereby obtaining memory modules of different layers, where the memory module stores normal memory information and is calibrated as MI. The feature definition formula in the memory model of the jth layer in the memory information MI is Where, MI j is the memory model of the memory information MI at the jth layer, K is the number of features in the set memory model, c j is the number of channels of the j-th layer model, h j is the feature height of the j-th layer model, w j is the feature width of the j-th layer model.
[0062] Specifically, in step S2, the steps of obtaining the simulated abnormal image and the real GT image corresponding to the sample are as follows:
[0063] Construct a data augmentation network and use anomaly simulation strategies to enhance the correlation of input normal samples or introduce local irregular anomalies to construct simulated abnormal samples;
[0064] Acquire simulated abnormal samples and corresponding normal samples, as well as labels and real GT images corresponding to the simulated abnormal samples and corresponding normal samples, for subsequent training;
[0065] Data augmentation includes enhancement processing operations such as rotation, bias, sharpness, daylight, equalization, brightness change, color change, and automatic contrast change. The anomaly simulation strategy is based on Perlin noise to generate simulated anomaly samples, and the definition formula is: Where SA is the simulated abnormal sample, α is the opacity parameter, which is uniformly sampled in the interval [0.15, 1.0], M is the mask map of the generated abnormal area, and I is the original image. is the negation of M, is the element-wise multiplication symbol, Z is the noise image, which consists of two parts. One part comes from the DTD texture dataset, the purpose of which is to simulate texture anomalies, and the other part comes from the input image, the purpose of which is to simulate structural anomalies. In order to simulate structural anomalies, the input original image I needs to be randomly adjusted in terms of mirror symmetry, rotation, brightness, saturation and hue, that is, I is the normal sample of the input.
[0066] Specifically, in step S3, the model network based on the optimized memory module and U-Net++ includes an optimized memory module, an image augmentation module, a feature extraction module, an optimal differential information calculation module, a multi-scale fusion module and a U-Net++ decoding module, wherein:
[0067] Optimize memory modules: Perform preprocessing operations such as sample size adjustment, feature extraction, and clustering calculation on all normal samples to obtain memory modules of different layers. The memory modules store normal memory information MI.
[0068] Image augmentation module: Input normal training samples into the data augmentation network and use the abnormal simulation strategy to realize random enhancement of input samples. The data augmentation network is used to realize the relevant enhancement of input normal samples or introduce local irregular anomalies to construct simulated abnormal samples. The simulated abnormal samples and corresponding normal samples are obtained, as well as the labels and real GT images corresponding to the simulated abnormal samples and the corresponding normal samples, which are used for subsequent training;
[0069] Feature extraction module: Receive the abnormal simulation samples and normal samples obtained by the image augmentation module and perform image preprocessing again, adjust the abnormal simulation samples and normal samples to 256*256 size, use the ResNet18 model to extract features of the input image, and save the feature maps extracted from each layer of the ResNet18 model respectively. The size of the feature map of the layer0 layer is [64, 128, 128], the size of the feature map of the layer1 layer is [64, 64, 64], the size of the feature map of the layer2 layer is [128, 32, 32], the size of the feature map of the layer3 layer is [256, 16, 16], and the size of the feature map of the layer4 layer is [512, 8, 8]; and output the feature maps of the layer1 layer, layer2 layer, and layer3 layers as input NI to the optimal differential information calculation module to refine features for distinguishing defects.
[0070] Best differential information calculation module: receives the feature map input NI of layer1, layer2, and layer3, and calculates the distance between NI and all memory information MI in layers, and finds the memory feature closest to the input feature in each layer The difference information formula is calculated as follows: In the formula, the feature map input NI of layer1, layer2, and layer3 is calibrated as F i,j , i represents the i-th input feature, j represents the j-th layer, D i,j It is represented as the difference information of the jth layer of the i-th input feature, and the best difference information D of each layer is i,j Summarize and organize into the best differential information DI * ;
[0071] Multi-scale fusion module: the best differential information DI * The concatenation operation is completed with the feature map input image NI at each layer according to the high-level features on the channel dimension to obtain concatenated information CI1, CI2 and CI3, and the sizes of the concatenated information CI1, CI2 and CI3 are [128, 64, 64], [256, 32, 32], [512, 16, 16] respectively;
[0072] Specifically, the concatenation information CI1 is the best difference information between the input sample layer 1 feature map and the input sample and the memory information MI at the layer 1 layer concatenated according to the high-level features on the channel dimension, the concatenation information CI2 is the best difference information between the input sample layer 2 feature map and the input sample and the memory information MI at the layer 2 layer concatenated according to the high-level features on the channel dimension, and the concatenation information CI3 is the best difference information between the input sample layer 1 feature map and the input sample and the memory information MI at the layer 3 layer concatenated according to the high-level features on the channel dimension;
[0073] Each concatenated information CI1, CI2 and CI3 is input into the same-scale fusion block for fusion. The same-scale fusion block includes a coordinate attention mechanism with SE attention mechanism and two convolution blocks. Considering that CI1, CI2 and CI3 are simple splicing of two kinds of information in the channel dimension, the coordinate attention mechanism is used to capture the information relationship between their channels, that is, CI1, CI2 and CI3 are respectively output through the same-scale fusion block to fuse the feature map CI of the best difference information. 1m , CI 2m and CI 3m , and the characteristic graph CI 1m , CI 2m and CI 3m The sizes are [64, 64, 64], [128, 32, 32], [256, 16, 16] respectively;
[0074] Then, upsampling is used to CI feature maps of different dimensions. 1m , CI 2m and CI 3mResolution alignment is performed to achieve multi-scale fusion. The upsampling module used contains a bilinear interpolation layer and a basic convolution block. Convolution is then performed to align the number of channels of feature maps of different dimensions, and element-by-element addition is performed to achieve multi-scale feature fusion. The specific steps are to convert the feature map CI 3m Input upsampling module, and then with CI 2m Perform element-by-element addition to obtain CI 2n Similarly, CI 2n Input upsampling module, and then with CI 1m Perform element-by-element addition to obtain CI 1n ;
[0075] Secondly, the spatial attention mechanism is used to obtain the spatial attention map. For the best differential information DI * The features at three different levels in the image are averaged in the channel dimension to obtain three feature maps with sizes of 16×16, 32×32 and 64×64 respectively. The 16×16 feature map is directly used as the spatial attention map M3. After the spatial attention map M3 is upsampled, the 32×32 feature map is element-wise multiplied to obtain the spatial attention map M2. Similarly, after the spatial attention map M2 is upsampled, the 64×64 feature map is used to perform element-wise multiplication to obtain the spatial attention map M1. The calculation formulas of the spatial attention maps M1, M2 and M3 are as follows: In the formula, C3, C2, and C1 are respectively expressed as The number of channels, Respectively represented as DI * The best difference between layer3, layer2 and layer1. Expressed as The feature map of channel i, They are represented as the spatial attention map M3 and the spatial attention map M2 obtained after upsampling, Represented as element-wise multiplication notation;
[0076] The final spatial attention map M n , n=1, 2, 3 respectively for CI 1n , CI 2n and CI 3m Weighted calculation is performed to obtain multi-scale fusion processed feature maps f1, f2 and f3, and the sizes of the feature maps f1, f2 and f3 are [64, 64, 64], [128, 32, 32], [256, 16, 16], respectively.
[0077] U-Net++ decoding module: The layer0 feature map and layer4 feature map extracted by the ResNet18 model are taken as f0 and f4 respectively, and the feature maps f1, f2 and f3 processed by multi-scale fusion are obtained at the same time. The sizes of the feature maps f0, f1, f2, f3 and f4 are [64, 128, 128], [64, 64, 64], [128, 32, 32], [256, 16, 16], [512, 8, 8] respectively. The feature maps f0, f1, f2, f3 and f4 are then used as the input of each layer of the decoder in the U-Net++ network to perform decoding of the U-Net++ network;
[0078] Specifically, the decoding of the U-Net++ decoding module starts from the bottom up. The U-Net++ decoding module uses an upsampling layer and a normal convolution layer. The upsampling layer contains a bilinear interpolation layer and a basic convolution block. The basic convolution block consists of a convolution layer, batch normalization, and a ReLU activation function.
[0079] With f 3_1 For example, feature map f4 passes through the upsampling layer, and then jumps to feature map f3, concatenates them according to the channel dimension, and then inputs the ordinary convolution layer to obtain f 3_1 Similarly, using the same calculation method, we can obtain f 2_2 、f 1_3 and f 0_4 , then the feature map f 3_1 、f 2_2 、f 1_3 and f 0_4 The specific calculation formula is as follows: 3_1 =H(f3,U(f4)),f 2_2 =H(f2,f 2_1 ,U(f 3_1 )),f 1_3 =H(f1,f 1_1 ,f 1_2 ,U(f 2_2 )),f 0_4 =H(f0,f 0_1 ,f 0_2 ,f 0_3 ,U(f 1_3 )), where H() represents the convolution calculation of the input ordinary convolution layer, (f3,U(f4)), (f2,f 2_1 ,U(f 3_1 ))、(f1,f 1_1 ,f 1_2 ,U(f 2_2 ))、(f0,f 0_1 ,f 0_2 ,f 0_3,U(f 1_3 )) respectively represent concatenating each feature according to the channel dimension, and U() represents the input upsampling layer for calculation;
[0080] The final f obtained in the U-Net++ decoding module 0_4 The feature map of size [48, 256, 256] is convolved to obtain a feature map of size [2, 256, 256]. The feature map of [2, 256, 256] is softmax-calculated according to the channel dimension to obtain the prediction result, and the loss function Loss is calculated for the prediction result.
[0081] Specifically, the calculation formula of the loss function Loss is Loss = λ l1 ×L l1 +λ focal ×L focal ,and And L focal =-α t ×(1-p t ) γ ×log(p t ), where y represents the true value of the model, Represented as the model prediction value, α t , γ represent hyper parameters, λ l1 , focal Respectively represented as L l1 , L focal The coefficient of p t It is expressed as reflecting the closeness to the true value y of the model, that is, p t The larger the value, the closer it is to y, and the more accurate the classification; when the GT value of the corresponding pixel in y is 1, p t Equal to the predicted probability p, when the GT value of the corresponding pixel in y is 0, p t Equal to 1-p.
[0082] Example 2
[0083] The present invention specifically provides Figure 3-10 The surface defect detection method based on the optimized memory module and U-Net++ shown includes selecting an industrial data set from a subfolder in the MVTec AD data set, namely, the bottle data set, wherein the MVTec AD data set is a real-world anomaly segmentation data set, and the bottle data set is a bottle data set, with a total of 287 images;
[0084] Among the 287 images, there are 60 defective images, 227 normal images without defects, and 19 normal images are used for testing. The original width of the dataset image is 900 pixels and the height is 900 pixels. Figure 3 As shown, it can be found from the above data that defective samples account for a small part of the total number of samples, and it is difficult to use supervised learning methods, but it is very suitable to use unsupervised learning methods;
[0085] The parameter settings during the surface defect detection model training process include the following:
[0086] The input image size set in the training process is 256×256, the batch size is set to 8, the training process is iterated 3000 times, and the test data set is used to test each iteration result, and the optimal model is saved. After all iterations, the final defect detection model is obtained;
[0087] The experimental settings are as follows: AdamW optimizer is used, the initial learning rate is 0.001, the weight decay is set to 0.0005, the learning rate decay is set to cosine decay, the decay rate is set to 0.1, and the loss function L l1 , L focal The coefficient λ l1 , focal They are set to 0.6 and 0.4 respectively, and the K-means clustering center in the optimized memory model is set to 25.
[0088] Specifically, Figure 4 As shown, the steps of the surface defect detection method based on the optimized memory module and U-Net++ are as follows:
[0089] Step 1: Get all normal samples and adjust them to 256*256 size, and use ResNet18 model to extract features from normal samples;
[0090] Freeze the network parameters of the ResNet18 model feature extraction, and obtain feature maps of scales [64, 64, 64], [128, 32, 32], and [256, 16, 16] through layer1, layer2, and layer3 of the ResNet18 model, respectively;
[0091] For the feature maps of different layers of all extracted normal samples, the K-means clustering algorithm is used for different layers respectively, and clustering is performed according to the set number of clusters. The number of cluster centers is set according to the actual distribution of normal samples. The features closest to each cluster center are selected and retained in the memory module, thereby obtaining memory modules of different layers, where the memory module stores the normal memory information and is calibrated as MI, such as Figure 5As shown, the feature definition formula in the memory model of the jth layer in the memory information MI is Where M j is the memory model of the memory information MI at the jth layer, K is the number of features in the set memory model, c j is the number of channels of the j-th layer model, h j is the feature height of the j-th layer model, w j is the feature width of the j-th layer model.
[0092] Step 2: Obtain sample images from the bottle dataset, use an ordinary CMOS industrial camera to acquire industrial product images, take image surface defects as detection targets, and realize defect detection and positioning.
[0093] Step 3: Preprocess the acquired normal sample image and adjust it to 256*256 size, then input the data into the data augmentation module to realize the relevant enhancement of the input normal sample or introduce local irregular anomalies to construct simulated abnormal samples, so as to obtain simulated abnormal samples and corresponding normal samples, as well as labels and real GT images corresponding to the simulated abnormal samples and corresponding normal samples, which are used for subsequent training;
[0094] Data augmentation includes enhancement processing operations such as rotation, bias, sharpness, daylight, equalization, brightness change, color change, and automatic contrast change. The anomaly simulation strategy is based on Perlin noise to generate simulated anomaly samples, and the definition formula is: Where SA is the simulated abnormal sample, α is the opacity parameter, which is uniformly sampled in the interval [0.15, 1.0], M is the mask map of the generated abnormal area, and I is the original image. is the negation of M, is the element-wise multiplication symbol, Z is the noise image, which consists of two parts. One part comes from the DTD texture dataset, the purpose is to simulate texture anomalies, and the other part comes from the input image, the purpose is to simulate structural anomalies. In order to simulate structural anomalies, the input original image I needs to be randomly adjusted in terms of mirror symmetry, rotation, brightness, saturation and hue, that is, I is the input normal sample. The anomaly simulation process is as follows: Figure 6 shown.
[0095] Step 4: Input the simulated abnormal samples and normal samples into the model network based on the optimized memory module and U-Net++ for model training. The model network based on the optimized memory module and U-Net++ includes an optimized memory module, an image augmentation module, a feature extraction module, an optimal differential information calculation module, a multi-scale fusion module and a U-Net++ decoding module, and the overall framework of the model network is shown in the figure below. Figure 7 As shown,
[0096] First, feature extraction is performed. The abnormal simulation samples and normal samples obtained by the image augmentation module are received and the images are preprocessed again. The abnormal simulation samples and normal samples are adjusted to 256*256 size. The ResNet18 model is used to extract features from the input images. The feature maps extracted from each layer of the ResNet18 model are saved respectively. The size of the feature map of the layer0 layer is [64, 128, 128], the size of the feature map of the layer1 layer is [64, 64, 64], the size of the feature map of the layer2 layer is [128, 32, 32], the size of the feature map of the layer3 layer is [256, 16, 16], and the size of the feature map of the layer4 layer is [512, 8, 8]. The feature maps of the layer1 layer, the layer2 layer, and the layer3 layer are output as input NI to the optimal differential information calculation module to extract features for distinguishing defects.
[0097] Best differential information calculation module: receives the feature map input NI of layer1, layer2, and layer3, and calculates the distance between NI and all memory information MI in layers, and finds the memory feature closest to the input feature in each layer The difference information formula is calculated as follows: In the formula, the feature map input NI of layer1, layer2, and layer3 is calibrated as F i,j , i represents the i-th input feature, j represents the j-th layer, D i,j It is represented as the difference information of the jth layer of the i-th input feature, and the best difference information D of each layer is i,j Summarize and organize into the best differential information DI * .
[0098] Step 5: Then multi-scale fusion is used to obtain the best differential information DI * The concatenation operation is completed with the feature map input image NI at each layer according to the high-level features on the channel dimension to obtain the concatenation information CI1, CI2 and CI3, and the sizes of the concatenation information CI1, CI2 and CI3 are [128, 64, 64], [256, 32, 32], [512, 16, 16] respectively. The concatenation information CI1 is the best difference information of the input sample layer1 layer feature map and the input sample and memory information MI at the layer1 layer concatenated according to the high-level features on the channel dimension. The concatenation information CI2 is the best difference information of the input sample layer2 layer feature map and the input sample and memory information MI at the layer2 layer concatenated according to the high-level features on the channel dimension. The concatenation information CI3 is the best difference information of the input sample layer1 layer feature map and the input sample and memory information MI at the layer3 layer concatenated according to the high-level features on the channel dimension.
[0099] Each concatenated information CI1, CI2 and CI3 is input into the same-scale fusion block for fusion. The same-scale fusion block includes a coordinate attention mechanism with SE attention mechanism and two convolution blocks. Considering that CI1, CI2 and CI3 are simple splicing of two kinds of information in the channel dimension, the coordinate attention mechanism is used to capture the information relationship between their channels, that is, CI1, CI2 and CI3 are respectively output through the same-scale fusion block to fuse the feature map CI of the best difference information. 1m , CI 2m and CI 3m , and the characteristic graph CI 1m , CI 2m and CI 3m The sizes are [64, 64, 64], [128, 32, 32], [256, 16, 16] respectively;
[0100] Then, upsampling is used to CI feature maps of different dimensions. 1m , CI 2m and CI 3m Resolution alignment is performed to achieve multi-scale fusion. The upsampling module used contains a bilinear interpolation layer and a basic convolution block. Convolution is then performed to align the number of channels of feature maps of different dimensions, and element-by-element addition is performed to achieve multi-scale feature fusion. The specific steps are to convert the feature map CI 3m Input upsampling module, and then with CI 2m Perform element-by-element addition to obtain CI 2n Similarly, CI 2n Input upsampling module, and then with CI 1m Perform element-by-element addition to obtain CI 1n ;
[0101] Secondly, the spatial attention mechanism is used to obtain the spatial attention map. For the best differential information DI * The features at three different levels in the image are averaged in the channel dimension to obtain three feature maps with sizes of 16×16, 32×32 and 64×64 respectively. The 16×16 feature map is directly used as the spatial attention map M3. After the spatial attention map M3 is upsampled, the 32×32 feature map is element-wise multiplied to obtain the spatial attention map M2. Similarly, after the spatial attention map M2 is upsampled, the 64×64 feature map is used to perform element-wise multiplication to obtain the spatial attention map M1. The calculation formulas of the spatial attention maps M1, M2 and M3 are as follows: In the formula, C3, C2, and C1 are respectively expressed as The number of channels, Respectively represented as DI * The best difference between layer3, layer2 and layer1. Expressed as The feature map of channel i, They are represented as the spatial attention map M3 and the spatial attention map M2 obtained after upsampling, Represented as element-wise multiplication notation;
[0102] The spatial attention map M obtained later n , n=1, 2, 3 respectively for CI 1n , CI 2n and CI 3m Perform weighted calculation, and the feature maps f1, f2, and f3 after multi-scale fusion processing, and the sizes of the feature maps f1, f2, and f3 are [64, 64, 64], [128, 32, 32], and [256, 16, 16] respectively;
[0103] The layer0 feature map and layer4 feature map extracted by the ResNet18 model are taken as f0 and f4 respectively, and the feature maps f1, f2 and f3 processed by multi-scale fusion are obtained at the same time. The sizes of the feature maps f0, f1, f2, f3 and f4 are [64, 128, 128], [64, 64, 64], [128, 32, 32], [256, 16, 16], [512, 8, 8] respectively. The feature maps f0, f1, f2, f3 and f4 are taken as the input of each layer of the decoder in the U-Net++ network to perform decoding of the U-Net++ network; Figure 8 As shown in the figure, the decoding of the U-Net++ decoding module starts from the bottom to the top. The U-Net++ decoding module uses an upsampling layer and a normal convolution layer. The upsampling layer contains a bilinear interpolation layer and a basic convolution block. The basic convolution block consists of a convolution layer, batch normalization, and a ReLU activation function.
[0104] With f 3_1 For example, feature map f4 passes through the upsampling layer, and then jumps to feature map f3, concatenates them according to the channel dimension, and then inputs the ordinary convolution layer to obtain f 3_1 Similarly, using the same calculation method, we can obtain f 2_2 、f 1_3 and f 0_4 , then the feature map f 3_1 、f 2_2 、f 1_3 and f 0_4 The specific calculation formula is as follows: 3_1 =H(f3,U(f4)),f 2_2 =H(f2,f 2_1 ,U(f 3_1 )),f 1_3 =H(f1,f 1_1 ,f1_2 ,U(f 2_2 )),f 0_4 =H(f0,f 0_1 ,f 0_2 ,f 0_3 ,U(f 1_3 )), where H() represents the convolution calculation of the input ordinary convolution layer, (f3,U(f4)), (f2,f 2_1 ,U(f 3_1 ))、(f1,f 1_1 ,f 1_2 ,U(f 2_2 ))、(f0,f 0_1 ,f 0_2 ,f 0_3 ,U(f 1_3 )) respectively represent concatenating each feature according to the channel dimension, and U() represents the input upsampling layer for calculation;
[0105] The final f obtained in the U-Net++ decoding module 0_4 The feature map of size [48, 256, 256] is convolved to obtain a feature map of size [2, 256, 256]. The feature map of [2, 256, 256] is softmaxed according to the channel dimension to obtain the prediction result, and the loss function Loss is calculated for the prediction result.
[0106] The calculation formula of the loss function Loss is Loss = λ l1 ×L l1 +λ focal ×L focal ,and And L focal =-α t ×(1-p t ) γ ×log(p t ), where y represents the true value of the model, Represented as the model prediction value, α t , γ represent hyper parameters, λ l1 , focal Respectively represented as L l1 , L focal The coefficient of p t It is expressed as reflecting the closeness to the true value y of the model, that is, p t The larger the value, the closer it is to y, and the more accurate the classification; when the GT value of the corresponding pixel in y is 1, p t Equal to the predicted probability p, when the GT value of the corresponding pixel in y is 0, p tEqual to 1-p, the loss function is back-propagated, and the model network parameters are continuously optimized. After 3000 rounds of iterations, the test data set is used to test each iteration result, and the optimal model is saved. After all iterations, the final defect detection model is obtained.
[0107] Step 6: Input the sample to be tested into the model network based on the optimized memory module and U-Net++ for testing, and obtain the test results as follows Fig.10 As shown, the detection process is as follows Fig. 9 shown.
[0108] The surface defect detection method based on optimized memory module and U-Net++ provided in an embodiment of the present invention, and the specific method and process of surface defect detection based on optimized memory module and U-Net++ are detailed in the above-mentioned embodiment of the surface defect detection method based on optimized memory module and U-Net++, which will not be repeated here.
[0109] The above formulas are all dimensionless and numerical calculations. The formula is a formula for the most recent real situation obtained by collecting a large amount of data and performing software simulation. The preset parameters in the formula are set by technicians in this field according to actual conditions.
[0110] The above embodiments can be implemented in whole or in part by software, hardware, firmware or any other combination. When implemented by software, the above embodiments can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions or computer programs. When the computer instructions or computer programs are loaded or executed on a computer, the process or function described in the embodiment of the present application is generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions can be transmitted from one website site, computer, server or data center to another website site, computer, server or data center by wired or wireless (e.g., infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that contains one or more available media sets. The available medium can be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium. The semiconductor medium can be a solid-state hard disk.
[0111] It should be understood that in the various embodiments of the present application, the size of the serial numbers of the above-mentioned processes does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present application.
[0112] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.
[0113] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art who is familiar with the present technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
Claims
1. A surface defect detection method based on optimized memory module and U-Net++, characterized in that: The steps are as follows: S1. All normal samples are preprocessed, and then the ResNet18 model is used to extract features from the normal samples. The network parameters of the ResNet18 model feature extraction are frozen, and then a clustering algorithm is used for the feature maps of different layers of all extracted normal samples to obtain an optimized memory module. In step S1, the optimized memory module is used to preprocess all normal samples, and the preprocessing steps are as follows: All normal samples are resized to 256*256, and the ResNet18 model is used to extract features from normal samples; Freeze the network parameters of the ResNet18 model feature extraction, and obtain feature maps of scales [64, 64, 64], [128, 32, 32], and [256, 16, 16] through layer1, layer2, and layer3 of the ResNet18 model, respectively; For the feature maps of different layers of all extracted normal samples, the K-means clustering algorithm is used for different layers respectively, clustering is performed according to the set number of clusters, and the features closest to each cluster center are selected and retained in the memory module, thereby obtaining memory modules of different layers. Among them, the memory module stores the normal memory information and is calibrated as MI. The feature definition formula in the memory model of the jth layer in the memory information MI is Where, MI j is the memory model of the memory information MI at the jth layer, K is the number of features in the set memory model, c j is the number of model channels in the jth layer, h j is the feature height of the j-th layer model, w j is the feature width of the j-th layer model; S2. Input the normal training sample into the data augmentation network and use the abnormal simulation strategy to implement random enhancement of the input sample to obtain the simulated abnormal image and the real GT image corresponding to the sample; S3, the simulated abnormal samples and normal samples are input into the model network based on the optimized memory module and U-Net++, the ResNet18 model gives the predicted surface defect abnormality prediction value, and calculates the loss function Loss; in step S3, the model network based on the optimized memory module and U-Net++ includes an optimized memory module, an image augmentation module, a feature extraction module, an optimal differential information calculation module, a multi-scale fusion module and a U-Net++ decoding module, wherein, Optimize the memory module: All normal samples are preprocessed by sample size adjustment, feature extraction and clustering calculation to obtain memory modules of different layers. The memory modules store the normal memory information MI. Image augmentation module: Normal training samples are input into the data augmentation network and the abnormal simulation strategy is used to implement random augmentation of the input samples. The data augmentation network implements correlation enhancement of the input normal samples or introduces local irregular anomalies to construct simulated abnormal samples. The simulated abnormal samples and corresponding normal samples are obtained, as well as the labels and real GT images corresponding to the simulated abnormal samples and the corresponding normal samples, which are used for subsequent training. Feature extraction module: Receive the abnormal simulation samples and normal samples obtained by the image augmentation module and perform image preprocessing again, adjust the abnormal simulation samples and normal samples to 256*256 size, use the ResNet18 model to extract features of the input image, and save the feature maps extracted from each layer of the ResNet18 model respectively. The feature map size of layer0 is [64, 128, 128], the feature map size of layer1 is [64, 64, 64], the feature map size of layer2 is [128, 32, 32], the feature map size of layer3 is [256, 16, 16], and the feature map size of layer4 is [512, 8, 8]. The feature maps of layer1, layer2, and layer3 are output as input NI to the optimal differential information calculation module to extract features for distinguishing defects. Best differential information calculation module: receives the feature map input NI of layer1, layer2, and layer3, and calculates the distance between NI and all memory information MI in layers, and finds the memory feature closest to the input feature in each layer The formula for calculating the difference information is: In the formula, the feature map input NI of layer1, layer2 and layer3 is calibrated as F i,j , i represents the i-th input feature, j represents the j-th layer, D i,j It is expressed as the difference information of the jth layer of the i-th input feature, and the best difference information D of each layer is i,j Summarize and organize into the best differential information DI * ; Multi-scale fusion module: the best differential information DI * The concatenation operation is completed with the feature map input image NI at each layer according to the high-level features on the channel dimension to obtain concatenation information CI1, CI2 and CI3, and the sizes of the concatenation information CI1, CI2 and CI3 are [128, 64, 64], [256, 32, 32], [512, 16, 16] respectively. The concatenation information CI1 is the best difference information of the input sample layer1 layer feature map and the input sample and memory information MI at the layer1 layer concatenated according to the high-level features on the channel dimension. The concatenation information CI2 is the best difference information of the input sample layer2 layer feature map and the input sample and memory information MI at the layer2 layer concatenated according to the high-level features on the channel dimension. The concatenation information CI3 is the best difference information of the input sample layer1 layer feature map and the input sample and memory information MI at the layer3 layer concatenated according to the high-level features on the channel dimension. Each concatenated information CI1, CI2 and CI3 is input into the same-scale fusion block for fusion. The same-scale fusion block includes a coordinate attention mechanism with SE attention mechanism and two convolution blocks. Considering that CI1, CI2 and CI3 are simple splicing of two kinds of information in the channel dimension, the coordinate attention mechanism is used to capture the information relationship between their channels, that is, CI1, CI2 and CI3 are output as the feature map CI of the best fusion difference information after passing through the same-scale fusion block. 1m 、CI 2m and CI 3m , and the feature map CI 1m 、CI 2m and CI 3m The sizes are [64, 64, 64], [128, 32, 32], [256, 16, 16] respectively; Then, upsampling is used to CI feature maps of different dimensions 1m 、CI 2m and CI 3m Perform resolution alignment to achieve multi-scale fusion. The upsampling module used contains a bilinear interpolation layer and a basic convolution block. Then convolution is performed to align the number of channels of feature maps of different dimensions, and element-by-element addition is performed to achieve multi-scale feature fusion. The specific steps are to convert the feature map CI 3m Input upsampling module, and then CI 2m Perform element-by-element addition to obtain CI 2n , similarly, CI 2n Input upsampling module, and then CI 1m Perform element-by-element addition to obtain CI 1n ; Secondly, the spatial attention mechanism is used to obtain the spatial attention map. For the best differential information DI * The features at three different levels in the spatial attention map are averaged in the channel dimension to obtain three feature maps with sizes of 16×16, 32×32, and 64×64, respectively. The 16×16 feature map is directly used as the spatial attention map M3. After the spatial attention map M3 is upsampled, the 32×32 feature map is element-wise multiplied to obtain the spatial attention map M2. Similarly, after the spatial attention map M2 is upsampled, the 64×64 feature map is used to perform element-wise multiplication to obtain the spatial attention map M1. The calculation formulas of the spatial attention maps M1, M2, and M3 are as follows: In the formula, C3, C2, and C1 are respectively expressed as The number of channels, Respectively expressed as DI * The best difference between layer3, layer2, and layer1, Expressed as The feature map of channel i, They are respectively represented as the spatial attention map M3 and the spatial attention map M2 obtained after upsampling. Represented as element-wise multiplication notation; The final spatial attention map M n , n=1, 2, 3 respectively for CI 1n 、CI 2n and CI 3m Perform weighted calculation to obtain the multi-scale fusion feature maps f1, f2, and f3, and the sizes of the feature maps f1, f2, and f3 are [64, 64, 64], [128, 32, 32], and [256, 16, 16] respectively; U-Net++ decoding module: The layer0 feature map and layer4 feature map extracted by the ResNet18 model are used as f0 and f4 respectively, and the feature maps f1, f2 and f3 of multi-scale fusion processing are obtained at the same time. The sizes of each feature map f0, f1, f2, f3 and f4 are [64, 128, 128], [64, 64, 64], [128, 32, 32], [256, 16, 16], [512, 8, 8] respectively. The feature maps f0, f1, f2, f3 and f4 are used as the input of each layer of the decoder in the U-Net++ network to perform decoding of the U-Net++ network; the decoding of the U-Net++ decoding module starts from the bottom up, and the U-Net++ decoding module uses an upsampling layer and a normal convolution layer, wherein the upsampling layer contains a bilinear interpolation layer and a basic convolution block, and the basic convolution block consists of a convolution layer, a batch normalization and a ReLU activation function; f 3_1 For example, the feature map f4 passes through the upsampling layer, and then jumps to the feature map f3, splicing them according to the channel dimension, and then inputs the ordinary convolution layer to obtain f 3_1 , similarly, use the same calculation method to obtain f one by one 2_2 、f 1_3 and f 0_4 , then the feature map f 3_1 、f 2_2 、f 1_3 and f 0_4 The specific calculation formula is as follows: 3_1 =H(f3, U(f4)), f 2_2 =H(f2,f 2_1 ,U(f 3_1 )), f 1_3 =H(f1,f 1_1 ,f 1_2 ,U(f 2_2 )), f 0_4 =H(f0,f 0_1 ,f 0_2 ,f 0_3 ,U(f 1_3 )), where H() represents the convolution calculation of the input ordinary convolution layer, (f3, U(f4)), (f2,f 2_1 ,U(f 3_1 ))、(f1,f 1_1 ,f 1_2 ,U(f 2_2 ))、(f0,f 0_1 ,f 0_2 ,f 0_3 ,U(f 1_3 )) respectively represent the concatenation of each feature according to the channel dimension, and U() represents the input upsampling layer for calculation; The final f obtained in the U-Net++ decoding module 0_4 The feature map of size [48, 256, 256] is convolved to obtain a feature map of size [2, 256, 256]. The feature map of [2, 256, 256] is softmaxed according to the channel dimension to obtain the prediction result, and the loss function Loss is calculated for the prediction result. When the S4 and ResNet18 models begin training, the loss function is back-propagated to continuously optimize the network parameters of the ResNet18 model. After multiple rounds of iterations, each iteration result is tested using the test dataset, and the optimal model is saved until the final defect detection model is obtained after all iterations. S5. Obtain the test sample and input it into the trained final defect detection model. The defect detection model gives the predicted GT map, thereby obtaining the abnormality location map.
2. The surface defect detection method based on optimized memory module and U-Net++ according to claim 1, characterized in that: In step S2, the steps for obtaining the simulated abnormal image and the real GT image corresponding to the sample are as follows: Construct a data augmentation network and use anomaly simulation strategies to enhance the correlation of input normal samples or introduce local irregular anomalies to simulate abnormal samples; Obtain simulated abnormal samples and corresponding normal samples, as well as labels and real GT images corresponding to the simulated abnormal samples and corresponding normal samples, for subsequent training; Data augmentation includes enhancement processing operations such as rotation, bias, sharpness, daylight, equalization, brightness change, color change, and automatic contrast change. The anomaly simulation strategy is based on Perlin noise to generate simulated anomaly samples, and the definition formula is Where SA is the simulated abnormal sample, α is the opacity parameter, which is uniformly sampled in the interval [0.15, 1.0], M is the mask map of the generated abnormal area, and I is the original image. is the inverse of M, is the element-wise multiplication symbol, Z is the noise image, which consists of two parts. One part comes from the DTD texture dataset, the purpose of which is to simulate texture anomalies, and the other part comes from the input image, the purpose of which is to simulate structural anomalies. In order to simulate structural anomalies, the input original image I needs to be randomly adjusted in terms of mirror symmetry, rotation, brightness, saturation, and hue, that is, I is the normal sample of the input.
3. The surface defect detection method based on optimized memory module and U-Net++ according to claim 2, characterized in that: The calculation formula of the loss function Loss is Loss = λ l1 ×L l1 +λ focal ×L focal ,and And L focal =-α t ×(1-p t ) γ ×log(p t ), where y represents the true value of the model, Expressed as the model prediction value, α t , γ are hyper parameters, λ l1 ,λ focal Respectively represented as L l1 , L focal The coefficient of p t It is expressed as reflecting the closeness to the true value y of the model, that is, p t The larger the p is, the closer it is to y, and the more accurate the classification is. When the GT value of the corresponding pixel in y is 1, p t Equal to the predicted probability p, when the GT value of the corresponding pixel in y is 0, p t Equal to 1-p.
