Small sample industrial defect detection system based on multi-stage diffusion model

By employing a multi-stage diffusion model and a multi-scale attention mechanism, the robustness and accuracy issues of industrial defect detection under small sample conditions are addressed, generating high-quality defect images and improving the sensitivity and interpretability of the detection model.

CN120953220APending Publication Date: 2025-11-14ZHONGBEI UNIV +1
View PDF 0 Cites 8 Cited by

Patent Information

Application Number
CN202511081444.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-08-04
Publication Date
2025-11-14

AI Technical Summary

Technical Problem

Under small sample conditions, traditional supervised learning methods suffer from overfitting and insufficient generalization ability in industrial defect detection, and existing unsupervised methods are unable to effectively utilize latent spatial information, resulting in insufficient robustness and sensitivity of anomaly detection.

Method used

A multi-stage diffusion model is adopted, which gradually models the global structure and local details of industrial images through a phased controlled diffusion generation process. Combined with multi-scale attention and physical constraints, high-quality defect images are generated, and detection accuracy is improved through a multi-task joint optimization unit.

Benefits of technology

It significantly improves the accuracy and robustness of industrial defect detection under small sample conditions, can generate high-quality defect images under unsupervised conditions, and improves the detection accuracy and interpretability of the detection model in complex backgrounds.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120953220A_ABST
    Figure CN120953220A_ABST
Patent Text Reader

Abstract

The invention discloses a small sample industrial defect detection system based on a multi-stage diffusion model. The small sample industrial defect detection system comprises a multi-stage diffusion generation module and a defect detection module based on multi-scale attention and physical constraint. The multi-stage diffusion generation module divides the diffusion generation process into three stages of global structure reconstruction, local detail refinement and texture feature synthesis through a stage control mechanism, and gradually guides feature evolution and improves the generation effect aiming at the quality and diversity problems of defect image generation under the small sample condition; the defect detection module based on multi-scale attention and physical constraint adopts a coding structure fusing local window attention and global attention, through multi-scale feature extraction and fusion, significant features of a defect area are effectively captured, gradient smoothing loss and edge energy consistency loss are introduced at a decoder end, and the defect detection accuracy is improved. Therefore, high-quality reconstruction of a normal area and effective suppression of an abnormal area are realized.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0002] This invention relates to the fields of computer vision and industrial quality inspection technology, and in particular to a small-sample industrial defect detection system based on a multi-stage diffusion model, specifically applied to anomaly detection and defect localization of surface defects in industrial products under small-sample conditions. Background Technology

[0004] In modern industrial production, intelligent inspection of product surface quality has become a crucial link in ensuring manufacturing standards and production efficiency. Traditional manual visual inspection methods suffer from high labor intensity, strong subjectivity, and poor consistency, making them unsuitable for the large-scale, high-precision industrial quality inspection needs. With the development of computer vision technology, automated defect detection methods based on deep learning have been widely used. Among them, supervised learning methods, by training on a large number of labeled samples, can achieve high-precision defect identification. However, due to the scarcity of defect samples and the imbalanced distribution of categories in real-world industrial scenarios, the detection performance of traditional supervised methods is easily affected under small sample conditions, resulting in problems such as overfitting and insufficient generalization ability.

[0005] To address the challenge of small-sample detection, existing research has proposed unsupervised or self-supervised learning strategies, especially methods based on reconstruction models (such as variational autoencoders (VAEs) and generative adversarial networks (GANs). These methods learn the distribution characteristics of normal samples to detect anomalous samples that deviate from the normal distribution. While these methods alleviate the problem of insufficient defective samples to some extent, they still have several limitations. On the one hand, traditional reconstruction models struggle to fully model the complex and varied structural details in industrial images under small-sample conditions, leading to a decline in the quality of generated images and difficulty in clearly distinguishing anomalous regions. On the other hand, existing methods generally lack explicit modeling of the physical properties of industrial images (such as continuity and edge energy distribution), which can easily result in the accurate reconstruction of anomalous regions, leading to insufficient robustness and sensitivity in anomaly detection. Furthermore, in the anomaly determination stage, traditional detection methods typically rely solely on the pixel-level reconstruction error between the input image and the reconstructed image as the basis for anomaly scoring, failing to effectively utilize the uncertainty information in the latent space, thus limiting the reliability and interpretability of the detection results.

[0006] Diffusion models offer significant advantages in generating defects from small-sample industrial images. A type of generative model based on a stochastic process, diffusion models progressively inject noise into the original data through a forward diffusion process, then recover samples with data distribution characteristics from the noise through a reverse generation process. Under small-sample conditions, it can deeply mine latent features within a limited number of samples. Specifically, during the generation process, diffusion models can learn the overall structure and detailed texture information of samples from a small number of normal and defective industrial samples. Through a multi-stage denoising process, different levels of feature generation are focused at different stages. For example, the approximate shape of the defect is first constructed, and then the local morphology and texture are gradually refined, thereby generating diverse and high-quality defect images. These generated images contain rich details, and their defect edges and textures are more realistic, providing high-quality and diverse training data for subsequent anomaly detection models. This enables the detection models to better capture different types of defect features, improving the accuracy and reliability of anomaly detection in industrial images under small-sample scenarios. Summary of the Invention

[0008] Based on the above ideas, this invention proposes a small-sample industrial defect detection system based on a multi-stage diffusion model. This system gradually models the global structure and local details of industrial images through a staged diffusion generation process, effectively alleviating the problem of insufficient modeling of industrial defect images under small sample conditions, thereby improving the accuracy and robustness of industrial anomaly detection.

[0009] To achieve the above objectives, the following technical solution is adopted:

[0010] A small-sample industrial defect detection system based on a multi-stage diffusion model includes a multi-stage diffusion generation module and a defect detection module based on multi-scale attention and physical constraints.

[0011] Multi-stage diffusion generation module: used to gradually generate high-quality industrial defect images under small sample conditions. By dividing the diffusion process into a global structure reconstruction stage, a local refinement stage and a texture synthesis stage, it guides the evolution and refinement of image features layer by layer, thereby improving the overall structural rationality, detail richness and texture naturalness of the generated image.

[0012] The multi-stage diffusion generation module of the model includes a diffusion model. First, the extracted normal industrial image and the defect mask are input into the diffusion model for noise addition. After a multi-stage denoising process, an industrial image with defects at the original normal image mask position is obtained. This solves the problem of insufficient quantity and richness of industrial defect images, enabling the industrial defect detection model to obtain a large dataset for training and improving the detection accuracy.

[0013] The multi-stage diffusion generation module includes:

[0014] Stage Control Unit: By setting specific thresholds for the diffusion time step, the diffusion process is scientifically divided into three stages: global structure construction, local detail refinement, and texture synthesis. This unit dynamically controls the diffusion path to achieve a progressive and orderly transition between stages in feature generation.

[0015] Gated attention enhancement unit: Within each diffusion stage, channel attention and spatial attention mechanisms are integrated, and features at different scales are filtered through weight allocation, focusing on key feature channels and important spatial regions, thereby enhancing the accuracy of feature expression and the coherence of contextual logic at each stage.

[0016] Multi-task joint optimization unit: Integrates diffusion reconstruction loss, perceptual consistency loss, and feature fidelity loss to provide optimization directions for feature learning at different stages of the generative model. Simultaneously, it strengthens information transmission from low-frequency to high-frequency features in the diffusion denoising stage, further ensuring the structural integrity and detail richness of the generated defective image.

[0017] The aforementioned small-sample industrial defect detection system based on a multi-stage diffusion model includes a multi-stage diffusion generation module that divides the diffusion process into a global structure reconstruction stage, a local refinement stage, and a texture synthesis stage by setting a fixed diffusion time step threshold. Within each stage, a gating attention mechanism is introduced to enhance features, guiding the features to evolve layer by layer from the overall structure to fine-grained texture, ultimately generating an industrial defect image with complete structure, rich details, and natural texture.

[0018] The aforementioned small-sample industrial defect detection system based on a multi-stage diffusion model includes a stage control unit that dynamically divides the generation stages according to different time steps of the diffusion process and guides the gradual refinement of the feature evolution path. Specifically, multi-scale features are first captured from different downsampling layers in the U-Net encoder, with the downsampling layers corresponding to resolutions of 256×256, 128×128, 64×64, and 32×32, respectively. To unify the scale for subsequent processing, pooling operations are used to uniformly compress the features at each scale to a resolution of 32×32, and then concatenating them along the channel dimension into a unified feature tensor, thus fusing spatial structural information from different scales.

[0019]

[0020] After feature fusion, in order to incorporate temporal evolution information, the time step t in the diffusion process is sinusoidally and cosinely encoded to generate a temporal position information vector PE(t). The specific encoding method is shown in the following formula:

[0021]

[0022] In the above formula, i represents the vector dimension index, and PE represents position encoding, which is a way to convert position information into a vector representation.

[0023] The fused features are obtained by adding the above multi-scale feature tensor to the step-by-step encoding vector element by element. This serves as the foundation for subsequent feature evolution and stage judgment. To further enhance feature representation capabilities, an innovative residual attention feature path is introduced, comprising a main path and an attention bypass:

[0024] In the main path, fused features Passing in sequence Convolutional operations extract local features, which are then processed by LayerNorm to stabilize the feature distribution. Subsequently, the GELU activation function is used to introduce a non-linear mapping to enhance the model's expressive power. The calculation process is shown in the following formula:

[0025]

[0026] In the above formula, For input features, express Convolution operation, LayerNorm represents layer normalization operation, and GELU is the activation function.

[0027] In the attention bypass, the fused features are first subjected to global average pooling to extract global feature descriptions along the channel dimension. These features are then processed through two fully connected network layers, and the channel attention weights are generated using the Sigmoid activation function. The calculation process is shown in the following formula:

[0028]

[0029] In the above formula, Indicates global average pooling. and These are two fully connected layers, For activation function, This is the Sigmoid function.

[0030] Following channel attention, a spatial attention mechanism is further applied. The channel-weighted features are subjected to max pooling and average pooling along the horizontal and vertical directions respectively to extract spatial structure features. Then, spatial attention weights are generated through convolution and sigmoid activation. The overall weighted fusion yields the attention bypass output features. The calculation process is shown in the following formula:

[0031]

[0032] In the above formula, This is for element-wise multiplication.

[0033] Subsequently, the main path features With attention bypass features The data is concatenated along the channel dimension and input into a discriminative module consisting of a three-layer fully connected network. The stage switching probability P is calculated to measure the sufficiency of the feature representation in the current stage. The calculation process is shown in the following formula:

[0034]

[0035] In the above formula, , , These are the weight matrices for a three-layer fully connected network. This represents concatenating two feature vectors in sequence. GELU is the activation function, and LayerNorm is the layer normalization operation. The output P is the phase switching probability, which is the Sigmoid function.

[0036] Ultimately, stage switching is decided based on a fixed threshold. When P is greater than the fixed threshold, it is determined that the current stage's feature expression is sufficient, triggering the diffusion process to switch to the next stage; otherwise, the feature evolution state of the current stage is maintained. Through the above dynamic control mechanism, the stage control unit can achieve stable and controllable stage refinement switching during the diffusion generation process, effectively improving the feature modeling capability and generated image quality of the diffusion model at different stages.

[0037] The small-sample industrial defect detection system based on a multi-stage diffusion model includes a gated attention enhancement unit that dynamically enhances the flow of key feature information and improves the efficiency of context feature interaction at different stages of the diffusion process. This module connects different encoder and decoder layers in a U-Net structure to form a cross-layer information bridge structure. Unlike the static skip connections of traditional U-Net, this invention dynamically activates corresponding cross-layer connection paths according to the current stage of the diffusion process, enhancing the model's ability to perceive stages of information at different scales.

[0038] After the connection is established, the feature map output by the encoder is first upsampled or downsampled, and its spatial resolution and semantic level are adjusted by combining 1×1 convolution to ensure that the encoder features and decoder features are consistent in spatial dimension and feature distribution. Subsequently, a cross-attention mechanism is used to fuse the connection features. Specifically, the current layer features of the decoder are used... As the query vector (Query, Q), the encoder transmits features across layers. The attention relevance score matrix is ​​calculated using keys (Key, K) and values ​​(Value, V). The calculation process is as follows:

[0039]

[0040]

[0041] In the above formula, It is the dimension of the key. This represents matrix multiplication.

[0042] The score matrix S is then normalized using Softmax to obtain the attention weight matrix P. Subsequently, V is weighted and summed based on P to obtain the cross-attention output features. The calculation method is as follows:

[0043]

[0044] Then output features based on cross-attention Features of the original decoder The fusion process involves introducing a gating coefficient. The weighting ratio between the cross-attention features and the original features is controlled, and the fusion calculation process is shown in the following formula:

[0045]

[0046] In the above formula, the gating coefficient This is a learnable parameter, and its value ranges from 0 to 1.

[0047] The small-sample industrial defect detection system based on a multi-stage diffusion model includes a multi-task joint optimization unit that guides feature generation and reconstruction during the diffusion process. This unit combines three different types of optimization objectives—stage identification loss, quality difference constraint loss, and feature consistency loss—and uses a weighted summation method to form a joint optimization objective function, achieving a balanced improvement in multiple performance aspects. Specifically, the joint optimization objective function defined in this invention is as follows:

[0048]

[0049] In the above formula, , , , which is a weighting coefficient used to adjust the relative importance of each loss term in the total loss. To identify losses in stages, To constrain quality differences, This is the feature consistency loss. The three factors are weighted and summed to form the final total loss L.

[0050] Stage identification loss Whether a phase switch occurs at the current time step t of the diffusion process is used as a pseudo-label. The probability p of phase switching predicted by the model at time t is used to calculate the binary cross-entropy loss, which measures the accuracy of the model's prediction of the timing of diffusion phase switching. Its definition is as follows:

[0051]

[0052] In the above formula, This represents the total number of steps during the training process. Let it be a binary variable. At time [time]... When a phase switch occurs, ,otherwise .

[0053] Quality difference constraint loss Based on the characteristics of different stages in the diffusion process, different image reconstruction loss functions are employed to improve the restoration effect of image quality at each stage. Specifically, these include:

[0054] In the global stage, mean squared error (MSE) loss is used to enhance the accurate reconstruction of image structural contours; in the thinning stage, edge loss based on image gradient differences is used to highlight the boundary details of defective regions; in the texture stage, structural similarity (SSIM) index is used as the loss function to enhance the perceptual similarity of local image textures. The above quality difference constraint loss is defined as follows:

[0055]

[0056] In the above formula, Indicates the input image. Indicates the reconstructed image. This indicates the image gradient calculation operation, and s represents the current denoising stage.

[0057] Feature consistency loss To ensure the temporal stability of features generated during the diffusion process, the feature consistency loss calculates the L2 norm difference between feature vectors extracted by each layer of U-Net at different input times, thus promoting consistency in feature extraction over time. Its loss function is defined as follows:

[0058]

[0059] In the above formula, This represents the hierarchical index of the U-Net network. Indicates the first The feature extraction function of a layer is used to extract features of the corresponding layer from the input image. This represents the input image at time t. Indicates the process The input image after the step. The square of the L2 norm of a vector is used to measure the difference between two eigenvectors.

[0060] Through the aforementioned multi-task joint optimization mechanism, this invention can simultaneously consider the accuracy of stage identification, the quality of image reconstruction, and the stability of features during the diffusion modeling process, significantly improving the accuracy and robustness of small-sample industrial defect detection.

[0061] The aforementioned small-sample industrial defect detection system based on a multi-stage diffusion model includes a defect detection module based on multi-scale attention and physical constraints. The detection module comprises a local window attention unit, a global attention unit, a decoder unit based on physical constraints, and an entropy-guided anomaly judgment unit, which are respectively used to extract local fine-grained features, model global structural information, constrain the reconstruction capability of abnormal regions, and evaluate the uncertainty of the potential space, and finally output image-level anomaly judgment and pixel-level defect localization results.

[0062] The aforementioned small-sample industrial defect detection system based on a multi-stage diffusion model includes a local window attention unit in its defect detection module based on multi-scale attention and physical constraints. This unit enhances the model's ability to perceive local fine-grained features and subtle anomalies. Specifically, this unit divides the input feature map into several fixed-size, non-overlapping local windows. Each local window contains a set of features from adjacent spatial locations for local region feature modeling. This design effectively reduces the spatial complexity of attention computation and highlights anomalies in local regions.

[0063] Within each local window, self-attention is calculated separately. First, the features within the local window are mapped to the query vector Q, key vector K, and value vector V respectively through linear transformations. Then, for the features within each local window, the local self-attention score matrix S is calculated according to the following formula:

[0064]

[0065] in, Let K be the feature dimension of the key vector. This represents the similarity score between the query and the key.

[0066] The score matrix S is normalized using Softmax to generate a local attention weight matrix P, which measures the importance relationship between local features. Then, the value vector V is weighted and summed according to the weight matrix P to generate the attention feature output within the local window.

[0067] After completing the local attention calculation, the features output by all local windows are reassembled according to their spatial positions in the original feature map, restoring a feature map of the same size as the original. The assembled feature map serves as the input to the subsequent encoder module, continuing to be used in the feature extraction and anomaly detection process.

[0068] Through the aforementioned local window division and in-window self-attention calculation mechanism, the local window attention unit can accurately capture local spatial anomalies, effectively enhancing the model's ability to perceive minute defects and local anomalies, and improving the robustness and detection accuracy of the overall detection system in complex backgrounds.

[0069] The aforementioned small-sample industrial defect detection system based on a multi-stage diffusion model includes a global attention unit in its defect detection module based on multi-scale attention and physical constraints. This unit enhances the model's ability to model the overall structure of an image, making it particularly suitable for detecting abnormal regions with large-scale structural changes or spatially dispersed locations. Unlike local window attention units, which calculate self-attention only within a limited area, this unit performs global self-attention calculation directly across the entire input feature map without spatial region division. This ensures that the model can acquire information from other regions at any location, enabling long-distance dependency modeling.

[0070] Specifically, the input feature map is first transformed into three vector sequences: query, key, and value, using a linear mapping. Then, the attention score matrix S is calculated across the entire feature map.

[0071]

[0072] Where Q and K represent the global query and the key vector, respectively. The key vector dimension is used. The score matrix S, after Softmax normalization, yields the attention weight matrix P, which is then used to weight the value vector to generate the final attention output features.

[0073]

[0074] This module is not limited by local windows and can acquire long-range interaction information between image regions across the entire spatial dimension, enabling the model to model global structural dependencies and abnormal distribution patterns.

[0075] By introducing a global attention mechanism, the model can significantly enhance its perception range when dealing with anomalies that are highly structured, spatially dispersed, or far-reachingly correlated. It accurately captures contextual information between distant regions, improving the ability to identify complex, large-scale, or discontinuous defects. This module complements the local window attention mechanism, forming a multi-scale collaborative perception strategy that provides a more comprehensive and robust feature foundation for subsequent detection results.

[0076] The aforementioned small-sample industrial defect detection system based on a multi-stage diffusion model, and its physically constrained decoder unit, aim to improve the model's ability to reconstruct normal structures while suppressing erroneous reconstructions of abnormal regions. This amplifies the response characteristics of defect regions in reconstruction errors, enhancing the discriminative power of anomaly detection. This module employs a mirror-image decoder architecture symmetrical to the encoder structure, consisting of multiple upsampling and convolutional modules stacked alternately. Each upsampling module uses bilinear interpolation to progressively enlarge the spatial size of the feature map and extracts local contextual features through subsequent 3×3 convolution operations, improving the spatial structural continuity and detail restoration capabilities of the reconstructed image. Compared to traditional transposed convolution, this design offers better stability and structure preservation in avoiding artifacts.

[0077] Beyond structural design, this decoder further introduces a constraint mechanism based on image physical properties. It employs a differentiable regularization term to normalize the decoder output, guiding the network to learn feature representations that better conform to the physical laws of normal images. This includes two key types of physical constraints:

[0078] First, an edge gradient smoothing constraint is introduced to characterize the continuity and smoothness that a normal image should possess in its spatial distribution. This constraint takes the reconstructed image output from the decoder as input, calculates the first-order gradients of the image in the horizontal and vertical directions respectively, and uses their total norm as a regularization term to guide the network to avoid generating drastic abrupt changes in structure in space. This process can be expressed by the following formula:

[0079]

[0080] In the above formula, To reconstruct the image for the decoder, and These represent the gradients of the image in the horizontal and vertical directions, respectively. Minimizing this regularization term encourages the model output to maintain a smooth gradient in local regions, suppressing high-frequency abrupt changes that do not conform to physical laws.

[0081] Secondly, an edge energy consistency constraint is introduced to enhance the model's ability to model the energy distribution of image edges. Specifically, the Sobel operator is used to extract the gradient magnitude map of the image, which serves as a representation of the local energy distribution. A regularization term is then designed based on these gradient maps to encourage the model to generate structurally stable and edge-uniform image responses during reconstruction, thereby exhibiting characteristics of missing information or blurred edges in anomalous regions. This constraint is defined as follows:

[0082]

[0083] By jointly introducing the two regularization terms mentioned above, the model is forced to learn the edge structure and energy distribution characteristics of normal images during the training phase. However, for unseen abnormal regions, due to their local mutations, irregularities, or high-energy edge characteristics, the model will be unable to effectively restore them, resulting in reconstruction failures, texture damage, and other phenomena in these regions, thereby further improving the accuracy and robustness of anomaly detection.

[0084] Overall, this physical constraint decoder works together at both the structural design and loss guidance levels, enabling the model to have a strong ability to reconstruct normal structures and a natural ability to reject abnormal structures. This greatly improves the performance of unsupervised anomaly detection based on reconstruction error, and is particularly suitable for industrial vision inspection tasks with regular surface structures and significant defect abrupt changes.

[0085] The aforementioned small-sample industrial defect detection system based on a multi-stage diffusion model includes an entropy-guided anomaly scoring unit, which enhances the discrimination capability and robustness of the anomaly judgment mechanism. Its core idea is to introduce potential spatial uncertainty as an auxiliary signal on the basis of traditional pixel-level reconstruction error, and construct a joint scoring function to more comprehensively measure the degree of anomaly of the sample.

[0086] This unit is built on a variational autoencoder (VAE) structure. First, the encoder encodes the features of the input samples and outputs the corresponding latent mean vector. Sum of logarithmic variance vector Thus, the probability distribution of the latent variable z is defined. This latent variable follows a multidimensional Gaussian distribution, and its mathematical form is as follows:

[0087]

[0088] in, The standard deviation of each dimension of the latent variable is obtained by taking the exponent of the logarithmic variance vector.

[0089] To measure the stability and uncertainty of the encoder in representing the sample, the system further performs information entropy analysis on the Gaussian distribution to obtain the uncertainty score of each sample in the latent space. The formula for calculating the entropy value is as follows:

[0090]

[0091] Where d is the dimension of the potential space. Let be the standard deviation of the i-th latent variable. This entropy value reflects the complexity of the sample's latent distribution: a larger value indicates a more dispersed distribution, meaning that the feature representation of the sample is unstable and more likely to correspond to samples with structural or image anomalies.

[0092] To achieve anomaly detection, the unit jointly constructs the final anomaly scoring function S(x) by combining the traditional pixel-level reconstruction error with the potential entropy value, as follows:

[0093]

[0094] in, It is an adjustment coefficient used to balance the contribution of reconstruction error and potential entropy value. Indicates the pixel-level reconstruction error MSE. This represents the potential spatial information entropy.

[0095] This joint scoring strategy possesses dual discriminative capabilities: on the one hand, it captures the reconstruction difficulty of the input image in terms of spatial structure through reconstruction errors; on the other hand, it perceives the stability and reliability of the image in the coding space through latent entropy values, thereby improving the overall recognition effect for samples that are difficult to reconstruct or have abnormal structures. By introducing an entropy-guided mechanism, the scoring unit effectively compensates for the insufficient discriminative ability that relies solely on reconstruction errors, significantly improving detection performance in complex backgrounds or scenes with minor defects, and providing a reliable foundation for subsequent image-level judgment and pixel-level visualization.

[0096] The small-sample industrial defect detection system based on a multi-stage diffusion model includes an image-level anomaly detection unit used to identify anomalies and locate spatial defects in the input image during the inference stage based on a comprehensive anomaly scoring function, taking into account both overall accuracy and fine-grained visualization capabilities.

[0097] In the overall anomaly detection process, each input sample is first quantified and scored based on an entropy-guided anomaly scoring function S(x). This scoring function comprehensively considers pixel-level reconstruction errors of the input image and uncertainties in the latent coding space, effectively distinguishing between normal and abnormal samples. The system sets an anomaly detection threshold and compares the scoring result with that threshold. When S(x) is greater than the threshold, the sample is determined to be an anomaly; otherwise, it is determined to be a normal sample. The anomaly detection threshold is initially determined by fitting an anomaly scoring distribution on the validation set. Typically, the confidence interval boundary is selected as the initial reference value, and it can be flexibly fine-tuned according to actual application needs to achieve the optimal balance between precision and recall in different task scenarios.

[0098] After completing the image-level determination, in order to further refine the location perception of abnormal areas, the unit simultaneously calculates the pixel-by-pixel reconstruction error between the input image and its corresponding reconstructed image, generating a pixel-level abnormal defect map. The calculation method is as follows:

[0099]

[0100] Where (i,j) represents the pixel position in the i-th row and j-th column of the image, and A(i,j) reflects the local reconstruction error information at that position. The larger the error value, the higher the probability that there is an anomaly in the pixel region.

[0101] To achieve a more intuitive visualization of abnormal areas, the abnormal defect map is further processed using heatmap mapping. Specifically, after normalizing the abnormal defect map to the range of [0,1], error information is encoded using a color gradient method. Areas with larger error values ​​are represented by darker or warmer colors in the heatmap, thus clearly indicating the location and shape of potential defect areas. This visualization not only facilitates the intuitive evaluation of inspection results but also provides an important reference for subsequent manual re-inspection, defect repair, and quality control processes.

[0102] Through the combined design of image-level anomaly judgment and pixel-level anomaly localization, the unit realizes a closed loop of anomaly detection from the whole to the part, effectively improving the discrimination accuracy, reliability and interpretability of the industrial defect detection system, and is suitable for high-precision anomaly detection tasks in diverse and complex backgrounds.

[0103] The beneficial effects of this invention are:

[0104] This invention proposes a small-sample industrial defect detection system based on a multi-stage diffusion model, constructing an integrated solution for high-quality defect image generation and anomaly detection suitable for small-sample conditions in industrial scenarios. The system comprehensively incorporates a multi-stage diffusion modeling strategy, a multi-scale attention mechanism, and a physically constrained reconstruction network, significantly enhancing the response sensitivity to local anomalies and complex defects while ensuring structural reconstruction capabilities. By synergistically integrating image generation and anomaly detection, this invention effectively solves practical problems such as the scarcity of industrial image data samples and the difficulty in obtaining high-quality defect samples, enabling automatic defect identification, localization, and visualization analysis under unsupervised conditions. This method is applicable to quality inspection tasks in various industrial scenarios, featuring flexible deployment, strong versatility, and high detection accuracy. It can be widely applied to quality control, equipment maintenance, and production monitoring in multiple industries such as manufacturing, electronics, metallurgy, and aerospace, providing technical support and performance assurance for the stable operation and lean production of industrial intelligent inspection systems. Attached Figure Description

[0106] Figure 1 Here is a structural block diagram of the multi-stage diffusion generation module;

[0107] Figure 2 This is a schematic diagram of the structure of the stage control module;

[0108] Figure 3 This is a schematic diagram of the gating attention enhancement module.

[0109] Figure 4 This is an overall architecture diagram of the defect detection module of the present invention;

[0110] Figure 5 This is a flowchart illustrating the local window attention mechanism in the anomaly detection and classification module of this invention.

[0111] Figure 6 This is a schematic diagram of the global attention in the anomaly detection and classification module.

[0112] Figure 7 A schematic diagram of the heat map visualization of the abnormal area; Detailed Implementation

[0114] The present invention will be described in detail below with reference to specific embodiments.

[0115] A small-sample industrial defect detection system based on a multi-stage diffusion model includes a multi-stage diffusion generation module and a defect detection module based on multi-scale attention and physical constraints. The multi-stage diffusion generation module is used to generate high-quality industrial defect images under small-sample conditions. It guides the diffusion process from global structure to local details through a stage division strategy, so as to improve the overall structural rationality, detail richness and texture naturalness of the generated image.

[0116] The multi-stage diffusion generation module includes a stage control unit, a gated attention enhancement unit, and a multi-task joint optimization unit.

[0117] Stage control unit: Based on the diffusion time step, a fixed threshold is set to divide the diffusion process into a global structure reconstruction stage, a local refinement stage, and a texture synthesis stage. By dynamically controlling the diffusion evolution path, the feature generation is refined layer by layer and the stage is switched.

[0118] Gated attention enhancement unit: Introduces channel attention mechanism and spatial attention mechanism within each diffusion stage. By weighted filtering of features at different scales, it highlights important feature channels and key spatial regions, thereby improving the relevance and contextual coherence of feature expression at each stage.

[0119] Multi-task joint optimization unit: Combining diffusion reconstruction loss, perceptual consistency loss and feature fidelity loss, it guides the generative model to optimize the feature learning process at different stages. At the same time, it enhances the information transmission of low-frequency to high-frequency features during diffusion denoising, thereby improving the structural integrity and detail richness of the final generated defective image.

[0120] The defect detection module based on multi-scale attention and physical constraints is used to detect anomalies and locate defects in industrial images under unsupervised conditions. It improves detection accuracy and robustness by synergistic enhancement of local and global feature perception and by combining physical property constraints.

[0121] The defect detection module based on multi-scale attention and physical constraints includes a local window attention unit, a global attention unit, a physically constrained decoder unit, an entropy-guided anomaly scoring unit, and an image-level anomaly determination unit.

[0122] Local window attention unit: In the encoder structure, image features are divided into local windows. The self-attention mechanism within the window is used to extract local texture features and minor defect information, thereby improving the model's ability to perceive local anomalies.

[0123] Global Attention Unit: Parallel to the local window attention path, it introduces a global self-attention mechanism to capture long-distance dependencies and overall structural changes across windows, enhancing the ability to detect defects over a wide range.

[0124] The decoder module based on physical constraints adopts a symmetrical structure design of a mirror encoder, consisting of an alternating stack of upsampling and convolution modules. It uses bilinear interpolation combined with convolution to extract local context information. It also introduces edge gradient smoothing constraints and edge energy consistency constraints, and uses regularization to suppress over-reconstruction of abnormal regions and enhance the response differences of abnormal regions.

[0125] Entropy-guided anomaly scoring unit: Based on the encoder output of variational autoencoder, the distribution of latent variables is determined using the mean vector and log-variance vector. The potential uncertainty of the sample is measured by calculating the information entropy of the latent Gaussian distribution, and a joint anomaly score is constructed by weighting the reconstruction error and the latent entropy value.

[0126] Image-level anomaly detection unit: Based on the anomaly scoring function S(x) and a set threshold, image-level anomaly detection is performed. At the same time, the pixel-by-pixel reconstruction error is used to generate an anomaly defect map, and the defect map is converted into a heat map form to intuitively realize the spatial visualization and positioning of the defect area.

[0127] The multi-stage diffusion generation module, such as Figure 1 As shown, it includes a stage control unit, a gated attention enhancement unit, and a multi-task joint optimization unit;

[0128] The specific steps are as follows:

[0129] The A1 model utilizes industrial images provided during industrial production and manufacturing, inputting both normal industrial images and defect masks into the model. Specifically, the model focuses on generating defect images at the locations corresponding to the defect masks in the normal images.

[0130] The A2 stage control unit dynamically divides the diffusion time step into three stages: global structure reconstruction, local refinement, and texture synthesis; step A2 integrates the stage control unit of the diffusion model, such as... Figure 2 As shown, this unit sets a fixed threshold by using a diffusion time step, and divides the diffusion process into a global structure reconstruction stage, a local refinement stage, and a texture synthesis stage. By dynamically controlling the diffusion evolution path, it achieves the layer-by-layer refinement and stage switching of feature generation.

[0131] The specific steps of step A2 are as follows:

[0132] A21. Feature splicing

[0133] like Figure 2 As shown, multi-scale features are first captured from different downsampling layers of U-Net, which have resolutions of 256×256, 128×128, 64×64, and 32×32, respectively. Pooling operations are then used to uniformly compress the features at each scale to a resolution of 32×32, and the resulting tensors are concatenated along the channel dimension to form a unified feature tensor. .

[0134]

[0135] A22. Perform sine and cosine position encoding on time step t during the diffusion process.

[0136] Determining whether to proceed to the next stage also requires combining timing information, and the diffusion step number t provides this timing information. The step number t is encoded using sine and cosine positions... The encoding process for step t can be referenced in the following formula. i represents the vector dimension index, and PE represents position encoding, which is a way to convert position information into a vector representation.

[0137]

[0138] The multi-scale features are added element-wise to the step-count encoding vector to obtain the fused features. It introduces an innovative residual attention feature path, consisting of a main path and an attention bypass. The main path integrates features... The local features are extracted sequentially through convolution, followed by layer normalization (LayerNorm) and GELU activation function processing. The calculation process is shown in the following formula:

[0139]

[0140] In the above formula, For input features, express Convolution operation, LayerNorm represents layer normalization operation, and GELU is the activation function.

[0141] A23. Attention bypass

[0142] In the attention bypass, channel attention is first used to generate channel weights to capture important channels in defective regions. The input features are then... Global average pooling is performed to compress the space to one dimension, yielding a global feature description in the channel dimension. Then, two fully connected layers are applied: the first fully connected layer compresses the number of channels, and the second fully connected layer restores the number of channels to their original value. Finally, the channel weights are output using the sigmoid function. The calculation process can be represented by the following formula.

[0143]

[0144] in, Indicates global average pooling. and These are two fully connected layers, For activation function, This is the Sigmoid function.

[0145] Spatial attention follows channel attention, focusing primarily on global defect areas. The features output from channel attention are subjected to max pooling in the horizontal direction and average pooling in the vertical direction, resulting in two feature maps with different spatial information. These are then processed using convolution and a sigmoid activation function to generate spatial weights. The features weighted by channel attention and spatial attention are compared with the main path features. The summation yields the final output of the attention bypass. The calculation process is shown in the following formula.

[0146]

[0147] in This is for element-wise multiplication.

[0148] To determine whether to proceed to the next stage, the extracted features are used. The decision is then made regarding whether to proceed to the next stage. In the probability calculation section, a three-layer fully connected network is constructed to compute the probability. and As input, the calculation process is shown in the following formula.

[0149]

[0150] in, , , These are the weight matrices for a three-layer fully connected network. This represents concatenating two feature vectors in sequence. GELU is the activation function, and LayerNorm is the layer normalization operation. The output P is the phase switching probability, which is the Sigmoid function.

[0151] A3 Gated Attention Enhancement

[0152] The gated attention enhancement unit in step A3 connects the various codecs of U-Net across layers, as shown in the appendix for the connection method. Figure 3 As shown, different connections are dynamically activated according to the stage of the diffusion process. After connection, upsampling or downsampling operations are performed, and convolution is combined to adjust the feature size and semantic level to ensure the consistency of encoder features and decoder features in spatial resolution and feature distribution.

[0153] The specific steps of step A3 are as follows:

[0154] A31. Phase-based connection strategy

[0155] The gated attention enhancement units activate different units depending on the stage of denoising. Specifically, in the global stage, the main goal is to generate the overall shape of the image. At this stage, only the gated attention enhancement unit between E4 and D1 needs to be activated, because E4 contains high-level semantic information of the image, providing information about the global structure and defect masks. D1 is at the beginning layer of the decoder, and introducing information from E4 into D1 helps to initially grasp the global defect distribution in the early stages of the generation process. In the refinement stage, to accurately refine the initially identified defect areas, the gated attention enhancement units between E4 and D1, and between E3 and D2, are activated. E3 contains more mesoscale detail information than E4; connecting it with D2, combined with the information from E4 and D1, allows for the refinement of the defect area shape and boundaries while considering both global structure and mesoscale detail. In the texture stage, to generate high-quality detailed textures, all gated attention enhancement units are activated.

[0156] A32. Feature Alignment

[0157] Since the U-Net encoder and decoder connected by the gated attention enhancement unit are cross-layer connections, they differ significantly in spatial dimension and semantic level, and direct fusion may lead to feature mismatch. Therefore, the gated attention enhancement unit first performs feature alignment, using upsampling or downsampling and 1×1 convolution to ensure that features from different stages and levels have consistent spatial and semantic scales.

[0158] A33. Cross-attention calculation

[0159] Because the original skip connections statically fuse the encoder and decoder directly through concatenation and convolution, they introduce many redundant features. To filter out irrelevant features, cross-attention is applied to the features of the encoder and decoder. The cross-attention mechanism achieves weighted aggregation of features by calculating the relationship between the query (Q), key (K), and value (V), allowing the decoder to focus more on feature regions more relevant to the current task. In the calculation process, let the decoder's features be Query, and the encoder's features be Key and Value. Their similarity score matrix is ​​calculated as shown in the following formula.

[0160]

[0161]

[0162] In the above formula, It is the dimension of the key. This represents matrix multiplication. Then, the attention weight matrix P is obtained by normalization using the Softmax function. Subsequently, a weighted sum of Values ​​based on P is performed to obtain the cross-attention output features. The calculation process is shown in the following formula.

[0163]

[0164] A34 Gating Fusion

[0165] To improve information transmission quality by selectively retaining or suppressing certain features at different stages, a gated fusion unit is introduced. This unit introduces a gating coefficient... Dynamic control of cross-attention features Features of the original decoder Fusion is performed. Gating coefficient. It is calculated by a learnable function, and the value of the coefficient ranges from 0 to 1. The calculation process is shown in the following formula.

[0166]

[0167] A4 Multi-task Joint Optimization Unit

[0168] Multi-task joint optimization unit: includes stage identification loss, quality difference constraint and feature consistency loss. The three losses are weighted and summed to form the joint optimization objective function.

[0169] The specific details of step A34 are as follows:

[0170] The main purpose of stage recognition loss is to enable the model to accurately predict the timing of stage transitions, ensuring a smooth transition between different stages. During training, the decision of whether a stage transition occurs at the current time step t is used as the pseudo-label for the stage. Combined with the stage switching probability predicted by the model at time t The loss is calculated based on the binary cross-entropy loss function, and the calculation process is shown in the following formula.

[0171]

[0172] in: This represents the total number of steps during the training process. Let it be a binary variable. At time [time]... When a phase switch occurs, ,otherwise The purpose of this loss function is to enable the control module during the training phase to accurately determine whether to proceed to the next phase, thereby improving the stability of phase switching and avoiding unstable generation quality caused by premature or delayed switching.

[0173] To adapt to the objectives of different stages, this paper further subdivides the quality difference constraint into three loss functions. In the global stage, details should not be emphasized too early; therefore, mean squared error is used to guide the model to reconstruct the image structure with the minimum pixel difference, highlighting the approximate outline and distribution range of defect regions. In the refinement stage, a gradient-based edge loss function is chosen. This function uses the difference in image gradients as a supervision signal, which can enhance the model's ability to characterize defect boundaries, and is particularly suitable for edge extraction and region segmentation in complex backgrounds. In the texture stage, the structural similarity index (SSIM) is used as the loss function to measure the perceptual similarity of brightness, contrast, and structure within a local window, guiding the model to reconstruct detailed structures that more closely resemble the real texture distribution. Therefore, the formula for the quality difference constraint is shown below.

[0174]

[0175] In the above formula, Indicates the input image. Indicates the reconstructed image. This indicates the image gradient calculation operation, and s represents the current denoising stage.

[0176] Feature consistency loss is used to ensure that the features generated by the model at different times have a certain degree of stability and consistency, avoiding drastic fluctuations in features. The specific calculation formula is as follows:

[0177]

[0178] in: This represents the hierarchical index of the U-Net network. Indicates the first The feature extraction function of a layer is used to extract features of the corresponding layer from the input image. Represents the input image at time t. Indicates the process The input image after the step. The L2 norm of a vector is squared, used to measure the difference between two feature vectors. By minimizing this loss function, the model can learn to maintain the relative stability of features at each step, thus resulting in better consistency of the generated image across different stages.

[0179] A5 Anomaly Detection and Classification Module

[0180] The overall architecture of the anomaly detection and classification module is as follows: Figure 4 As shown, it includes a local window attention unit, a global attention unit, a physically constrained decoder unit, an entropy-guided anomaly scoring unit, and an image-level anomaly determination unit.

[0181] The specific steps of step A5 are as follows:

[0182] A51. Local Window Attention Unit

[0183] The Local Window Attention module aims to enhance the model's ability to model local detail features, making it particularly suitable for capturing regions with complex textures and frequent detail changes in images. Unlike traditional convolution, this module combines multi-scale receptive fields with channel attention mechanisms to achieve efficient modeling of local region information. Its structure is as follows: Figure 5 As shown.

[0184] Specifically, local window attention divides the input features into non-overlapping segments. Small window, window feature map First, the processing proceeds through two parallel convolutional sub-branches with different convolutional layer stack depths to simulate local extraction capabilities across varying receptive field ranges. The feature maps output by each sub-branch are denoted as follows: and Subsequently, channel attention mechanisms are applied independently to the two branches to calculate the importance response of each channel. Channel attention employs a squeeze-and-excitation (SE) operation. First, global average pooling is performed on the input feature map to generate channel description vectors. These vectors are then passed through two fully connected layers and activated using a sigmoid activation function to generate channel weights. Then, the channel dimension of the original feature map is multiplied to achieve channel weighting. Finally, the outputs of the two sub-branches are weighted and fused to obtain the output feature map of the local window attention module.

[0185] A52. Global Attention Unit

[0186] To enhance the model's ability to perceive global information and capture long-distance dependencies between different regions in an image, this patent introduces a global attention mechanism that integrates input features. Average pooling is performed, where H, W, and C represent the height, width, and number of channels of the feature map, respectively. This yields the downsampled average pooled feature matrix. This is used for subsequent attention calculations, and its structure is as follows: Figure 6 As shown.

[0187] Next, a linear transformation maps Z to Key and Value vectors. To fully preserve the original spatial information, the Query vector still uses the unpooled original feature map and maintains the same dimension as the local branches. The Query and Key are then processed through a standard self-attention mechanism to obtain the output features of the global attention. The calculation process is shown below.

[0188]

[0189] A53 Physically Constrained Decoder Unit

[0190] This model's decoder employs a symmetrical structure, mirroring the encoder's structure. It consists of multiple alternating upsampling and convolutional modules, progressively recovering the image structural information contained in the latent spatial code Z. To ensure the stability and continuity of spatial structure reconstruction, the decoder's upsampling modules utilize bilinear interpolation combined with convolutional operations. In each upsampling stage, this structure first enlarges the feature map using bilinear interpolation, then extracts local contextual information through a 3×3 convolutional layer, further enhancing feature representation capabilities. Compared to traditional transposed convolution, this method offers stronger controllability and reconstruction stability, effectively avoiding artifacts easily generated by deconvolution, and contributing to improved applicability and generalization ability of the model in industrial images.

[0191] A gradient smoothing constraint loss is introduced. Taking the image reconstructed by the decoder as input, its first-order gradients in the horizontal and vertical directions are calculated. The total norm of the first-order gradient is used as a regularization constraint term to suppress abrupt changes in the image. The calculation process is shown in the following formula:

[0192]

[0193] In the above formula, To reconstruct the image for the decoder, and These represent the gradients of the image in the horizontal and vertical directions, respectively, measuring the intensity of local changes between pixels.

[0194] Then, an edge energy consistency constraint loss is introduced, and the gradient magnitude image of the reconstructed image is extracted using the Sobel operator. A regularization term is designed based on the gradient energy distribution to encourage the generated image to have a stable and regular edge response pattern. The calculation process is shown in the following formula:

[0195]

[0196] Guided by the aforementioned structural constraints, the decoder focuses on learning the physical consistency features in normal images during the training phase. When faced with abnormal images during the testing phase, it naturally forms incorrect reconstructions because it cannot reconstruct these defects, thereby significantly enhancing the response of abnormal regions in reconstruction errors.

[0197] A54 Entropy-Guided Anomaly Scoring Unit

[0198] In traditional reconstruction-based anomaly detection methods, models typically rely solely on the pixel-level error between the input image and its reconstructed image as the basis for anomaly scoring. However, in practical applications, a single reconstruction error is often insufficient to fully reveal the nature of the anomaly. To enhance the robustness and discriminative power of the anomaly scoring mechanism, this patent introduces the information entropy of the latent coding space as an auxiliary basis for anomaly discrimination.

[0199] Specifically, in the structure of a variational autoencoder (VAE), the distribution of the latent variable z is the mean vector output by the encoder. Log-variance vector The formula is determined jointly as shown below.

[0200]

[0201] However, in actual detection tasks, the latent distributions corresponding to different input samples vary significantly, especially in the encoding of anomalous samples, where the distribution of latent variables is often more unstable and uncertain. Therefore, the uncertainty of the latent space itself may be an effective criterion. Based on this, this paper proposes a latent encoder analysis method based on entropy values ​​to calculate the information entropy of the latent distribution, which measures the complexity of the encoder in sample representation and provides an auxiliary indicator for subsequent defect judgment. This method calculates the Gaussian distribution entropy value of each sample in the latent space from an information theory perspective, and the calculation formula is shown below.

[0202]

[0203] in The dimension of the latent vector. For the first The entropy value represents the standard deviation of each dimension. A larger entropy value indicates higher uncertainty in the latent space, suggesting that the sample may have structural instability defects and may correspond to an anomalous input sample.

[0204] To further improve detection performance, this patent designs an anomaly scoring mechanism based on entropy. In the calculation of reconstruction error, the potential entropy value corresponding to the sample is used as an adjustment factor to adaptively adjust the detection weights of different samples. Specifically, a final anomaly score is defined for each sample. As shown in the following formula.

[0205]

[0206] in It is an adjustment coefficient used to balance the contribution of reconstruction error and potential entropy value. Indicates the pixel-level reconstruction error MSE. This represents the potential spatial information entropy.

[0207] This strategy enables the model not only to identify areas that are difficult to reconstruct, but also to assist in judging whether there are anomalies in the image in the subsequent process, thereby improving the overall robustness of defect recognition.

[0208] A55 Image-Level Anomaly Detection Unit

[0209] Anomaly identification is performed based on the anomaly scoring function S(x). By setting an anomaly judgment threshold, the input image is judged as an anomaly sample. The threshold is fitted based on the scoring distribution on the validation set and the confidence interval boundary is selected as the initial value. It can be flexibly adjusted according to actual detection needs.

[0210] Then, pixel-level defect visualization is used to calculate the pixel-by-pixel reconstruction error between the input image and its corresponding reconstructed image, generating an anomaly defect map to describe the reconstruction error information of each pixel. The calculation definition is shown in the following formula:

[0211]

[0212] Finally, to visualize the abnormal areas, the abnormal defect diagram A was converted into a heatmap. Areas with larger error values ​​were represented by darker colors on the heatmap, such as... Figure 7 As shown in the figure, the pixel-level anomaly localization is quite accurate, and various anomalies and defects at various scales can be identified relatively accurately.

[0213] Figure 7 This is the qualitative anomaly localization result of the small-sample industrial defect detection system based on a multi-stage diffusion model of the present invention. The result image mainly includes the following elements:

[0214] Original Defect Images: Images of industrial products containing real industrial defects, covering various scenarios such as textures and objects.

[0215] Generated image: The model reconstructs the input image. Normal areas are clearly reconstructed, while defective areas appear blurry or distorted due to model-suppressed reconstruction (physical constraints).

[0216] Heatmap: A visualization of defect location results through pixel-level reconstruction errors. Darker colors (e.g., red, orange) indicate a higher probability that the area is a defect, while lighter colors (e.g., blue, green) indicate a higher probability that it is a normal area. The map shows relatively accurate pixel-level anomaly localization and can accurately identify various abnormal defects at different scales.

[0217] As can be clearly seen from the figure, the model can accurately identify defects of different scales and shapes. Whether it is a minor scratch, a large area of ​​stain, or an edge defect in a complex background, the heat map can be highly consistent with the real defect area, with clear boundaries and no obvious false positives.

[0218] The excellent performance of qualitative anomaly localization results relies on the multi-scale attention mechanism to enhance defect perception capabilities. This is because the model encoder introduces local window attention and global attention in parallel. The two attention mechanisms work together to enable the model to capture millimeter-level scratches and identify large-area defects across regions, significantly improving the integrity and accuracy of defect areas in the heat map.

[0219] It should be understood that those skilled in the art can make improvements or modifications based on the above description, and all such improvements and modifications should fall within the protection scope of the appended claims.

Claims

1. A small-sample industrial defect detection system based on a multi-stage diffusion model, characterized in that, The system includes a multi-stage diffusion generation module and a defect detection module based on multi-scale attention and physical constraints. The multi-stage diffusion generation module generates high-quality industrial defect images under small sample conditions. It guides the diffusion process from global structure to local details through a stage-based strategy, improving the overall structural rationality, detail richness, and texture naturalness of the generated image. The defect detection module based on multi-scale attention and physical constraints performs anomaly detection and defect localization of industrial images under unsupervised conditions. It enhances detection accuracy and robustness through the synergistic enhancement of local and global feature perception, combined with physical attribute constraints. The multi-stage diffusion generation module includes a stage control unit, a gated attention enhancement unit, and a multi-task joint optimization unit: Stage Control Unit: Root... A fixed threshold is set based on the diffusion time step, and the diffusion process is divided into a global structure reconstruction stage, a local refinement stage, and a texture synthesis stage. By dynamically controlling the diffusion evolution path, the feature generation is refined layer by layer and the stage switching is achieved. Gated attention enhancement unit: Channel attention mechanism and spatial attention mechanism are introduced within each diffusion stage. By weighted filtering of features at different scales, important feature channels and key spatial regions are highlighted, improving the relevance and contextual coherence of feature expression at each stage. Multi-task joint optimization unit: Combining diffusion reconstruction loss, perceptual consistency loss, and feature fidelity loss, it guides the generative model to optimize the feature learning process at different stages. At the same time, it enhances the information transmission of low-frequency to high-frequency features during diffusion denoising, improving the structural integrity and detail richness of the final generated defective image.

2. The small-sample industrial defect detection system based on a multi-stage diffusion model according to claim 1, characterized in that, The defect detection module based on multi-scale attention and physical constraints includes a local window attention unit, a global attention unit, a physically constrained decoder unit, an entropy-guided anomaly scoring unit, and an image-level anomaly determination unit. The local window attention unit divides image features into local windows within the encoder structure, utilizing an in-window self-attention mechanism to extract local texture features and minute defect information, enhancing the model's ability to perceive local anomalies. The global attention unit runs parallel to the local window attention path, introducing a global self-attention mechanism to capture long-distance dependencies across windows and overall structural changes, enhancing the detection capability for large-scale defects. The physically constrained decoder module adopts a symmetrical structure design based on a mirror encoder, consisting of alternating stacks of upsampling and convolution modules, using bilinear interpolation combined with convolution to extract local contextual information. Furthermore, edge gradient smoothing constraints and edge energy consistency constraints are introduced to suppress excessive reconstruction of abnormal regions and enhance the response differences of abnormal regions through regularization. The entropy-guided anomaly scoring unit uses the encoder output of the variational autoencoder to determine the distribution of latent variables using the mean vector and log-variance vector. It calculates the potential uncertainty of the sample by the information entropy of the latent Gaussian distribution and constructs a joint anomaly score by weighting the reconstruction error and the latent entropy value. The image-level anomaly determination unit performs image-level anomaly determination based on the anomaly scoring function S(x) and a set threshold. At the same time, it uses the pixel-by-pixel reconstruction error to generate anomaly defect maps and converts the defect maps into heatmaps to intuitively realize the spatial visualization and localization of defect areas.

3. The small-sample industrial defect detection system based on a multi-stage diffusion model according to claim 1, characterized in that, The stage control unit first captures multi-scale features from different downsampling layers of the U-Net. These downsampling layers have resolutions of 256×256, 128×128, 64×64, and 32×32, respectively. Pooling operations are then used to uniformly compress the features at each scale to a resolution of 32×32, and the features are then concatenated along the channel dimension into a unified feature tensor. Then, by performing sine and cosine position encoding on the time step t during the diffusion process, it is used for subsequent stage control judgment; the multi-scale features are added element-wise to the step-by-element encoding vector to obtain the fused features. It introduces a residual attention feature path, consisting of a main path and an attention bypass; in the main path, features are fused. Local features are extracted sequentially through convolution, followed by LayerNorm activation and GELU activation. In the attention bypass, global average pooling is first used to extract channel-wide features, which are then processed through two fully connected layers and activated by Sigmoid to generate channel attention weights. Subsequently, a spatial attention mechanism is applied, performing max pooling and average pooling in the horizontal and vertical directions respectively, followed by convolution and Sigmoid activation to generate spatial attention weights. After overall weighted fusion, the attention bypass output features are obtained. Finally, the main path features and attention bypass features are concatenated, and the stage switching probability P is calculated through a three-layer fully connected neural network. The output P value is used as a continuous score index for the sufficiency of feature expression. The stage switching is based on a fixed threshold. After comparing with the threshold, the selection is made to maintain the current stage's generation state or switch to the next diffusion stage, thereby achieving stable and controllable dynamic stage switching.

4. The small-sample industrial defect detection system based on a multi-stage diffusion model according to claim 1, characterized in that, The gated attention enhancement unit connects the encoders and decoders of U-Net across layers, dynamically activating different connections based on the stage of the diffusion process. After connection, upsampling or downsampling operations are performed, combined with convolution to adjust feature size and semantic level, ensuring consistency between encoder and decoder features in spatial resolution and feature distribution. Then, using decoder features as the query (Q) and encoder features as the key (K) and value (V), a cross-attention relevance score matrix is ​​calculated between the query and key. Finally, the value is weighted and summed according to P to obtain the cross-attention output feature. Then, output features based on cross-attention. Features of the original decoder The fusion process involves introducing a gating coefficient. Control the weighting ratio of cross-attention features to the original features.

5. The small-sample industrial defect detection system based on a multi-stage diffusion model according to claim 1, characterized in that, The multi-task joint optimization unit includes stage identification loss, quality difference constraint, and feature consistency loss. These three losses are weighted and summed to form the joint optimization objective function. The final loss function is constructed by weighted summation of the three loss functions. To balance different optimization objectives; among them, the stage identification loss uses whether a stage switch occurs at the current time step t as a pseudo-label of the stage. The model combines the stage switching probability p predicted at time t and calculates it based on the binary cross-entropy loss function to measure the accuracy of the switching timing. The quality difference constraint loss adopts different image reconstruction loss functions for different diffusion stages. In the global stage, the mean squared error (MSE) loss is used to enhance the reconstruction of structural contours. In the refinement stage, the edge loss based on image gradient difference is used to highlight the details of defect boundaries. In the texture stage, the structural similarity (SSIM) index is used as the loss to restore the perceptual similarity of local textures in the image. The feature consistency loss constrains the stability of generated features over time by calculating the L2 norm difference between the feature vectors extracted by each layer of U-Net under different input times.

6. The small-sample industrial defect detection system based on a multi-stage diffusion model according to claim 1, characterized in that, The local window attention unit divides the input feature map into multiple non-overlapping local windows of fixed size using local window partitioning. Each local window contains several adjacent spatial location features for local feature modeling. Self-attention calculation is performed within each window to calculate self-attention weights for the features within that local window. The attention calculation process follows these steps: first, the local features are mapped to query, key, and value vectors, respectively; then, the attention score matrix S is calculated; finally, feature fusion is performed to reassemble the attention features output from each local window back to the original feature map shape, which is then used as input to the subsequent encoder feature stream, thereby achieving accurate capture and enhanced perception of abnormal changes in local regions.

7. The small-sample industrial defect detection system based on a multi-stage diffusion model according to claim 2, characterized in that, The global attention unit described above performs self-attention calculations directly on the input feature map across the entire spatial range without local partitioning, ensuring that the model can perceive long-distance dependencies between different regions of the image. Its calculation process is similar to that of local window attention. Through global feature modeling and long-range dependency capture, the global attention unit can effectively enhance the model's ability to model large-scale structural changes and long-distance related defect regions, and improve the detection module's performance in recognizing complex and dispersed abnormal patterns.

8. The small-sample industrial defect detection system based on a multi-stage diffusion model according to claim 2, characterized in that, The physical constraint-based decoder unit adopts a symmetrical structure of a mirror encoder, consisting of alternating stacks of upsampling and convolution modules. Each upsampling module enlarges the feature map size through bilinear interpolation, and then extracts local context information through convolution to improve the stability and continuity of spatial feature recovery and avoid the artifact problem that is easily generated by traditional transposed convolution. Furthermore, a marginal gradient smoothing constraint loss is introduced. The first-order gradients in the horizontal and vertical directions are calculated using the image reconstructed by the decoder as input, and the total norm of the first-order gradient is used as a regularization constraint term to suppress drastic abrupt changes in the image. Furthermore, an edge energy consistency constraint loss is introduced, and the gradient magnitude image of the reconstructed image is extracted using the Sobel operator. A regularization term is designed based on the gradient energy distribution to encourage the generated image to have a stable and regular edge response pattern. Guided by the above structural constraints, the decoder focuses on learning the physical consistency features in normal images during the training phase. When faced with abnormal images during the testing phase, it naturally forms incorrect reconstructions because it cannot reconstruct these defects, thereby significantly enhancing the response of abnormal regions in reconstruction errors.

9. The small-sample industrial defect detection system based on a multi-stage diffusion model according to claim 2, characterized in that, The entropy-guided anomaly scoring unit is based on the encoder output of a variational autoencoder and uses a mean vector. With log-variance vector The distribution of the latent variable z is determined; by calculating the information entropy of the latent Gaussian distribution, the uncertainty index of each sample in the latent space is obtained; the anomaly scoring module introduces the latent space entropy value as an auxiliary factor on the basis of traditional pixel-level reconstruction error, and constructs a joint anomaly scoring function. Specifically, the final anomaly score S(x) of each sample is defined as a weighted combination of the reconstruction error MSE and the latent entropy value H; through the above entropy-guided anomaly scoring mechanism, the discrimination performance and robustness in the anomaly detection task can be improved by further combining the uncertainty characteristics of the latent space while retaining the reconstruction error information, so as to achieve more accurate judgment of image-level anomalies and spatially located anomalies.

10. The small-sample industrial defect detection system based on a multi-stage diffusion model according to claim 2, characterized in that, The image-level anomaly detection unit identifies anomalies based on an anomaly scoring function S(x). An anomaly threshold is set to classify the input image as an anomalous sample. The threshold is fitted to the scoring distribution on the validation set and uses the confidence interval boundary as the initial value, which can be flexibly adjusted according to actual detection needs. Then, pixel-level defect visualization is used to calculate the pixel-by-pixel reconstruction error between the input image and its corresponding reconstructed image, generating an anomaly defect map to describe the reconstruction error information of each pixel. Finally, to visualize the anomaly areas, the anomaly defect map A is converted into a heatmap. Areas with larger error values ​​are represented by darker colors on the heatmap, thus intuitively displaying the spatial distribution of potential defects and providing a reference for subsequent manual inspection and defect repair.

Citation Information

Cited By

  • Power system data generation method based on outlier detection and depth generation model

    CN121167313A

  • Industrial defect detection method based on multi-scale Transform

    CN121329966A

  • Fruit disease detection method and system based on global memory and local comparison

    CN121354093A

  • A fruit disease detection method and system based on global memory and local contrast

    CN121354093B

  • Lightweight multi-task context controlled image processing system, method and device

    CN121527446A