Weakly supervised defect detection method and system for industrial devices based on double space consistency anchor matrix

CN123016116APending Publication Date: 2026-10-30CHONGQING UNIV OF POSTS & TELECOMM
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611218924.8
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-08-12
Publication Date
2026-10-30

AI Technical Summary

Technical Problem

其一,表征空间单一,现有方法仅依赖局部特征分布或全局语义特征的单一侧面,难以全面刻画缺陷的多粒度特性,对同时涉及纹理变异与结构失真的复合缺陷判别力不足;

Benefits of technology

(1)本发明采用弱监督学习范式,训练阶段仅需少量图像级异常标签(无需像素级精细标注),以大量正常样本为基础,大幅减少了人工标注工作量与时间成本,使发明能够较好的适配于标注资源受限的工业现场。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123016116A_ABST
    Figure CN123016116A_ABST
Patent Text Reader

Abstract

The application relates to an industrial device weak supervision defect detection method and system based on a double-space consistency anchor matrix, and belongs to the technical field of industrial visual detection. The application extracts structure perception features through a structure perception feature encoder, and obtains local distribution features and global semantic features after decoupling; in the training stage, a hierarchical sparse memory bank guided by anchoring is used to perform semantic-geometric joint sparsification and self-purification on the local features, and a discriminative global prototype is generated through learnable prototype contrast learning to realize double-space alignment through a cross-space consistency anchor matrix and KL divergence constraint; in the inference stage, a local anomaly score is obtained through a local branch, a global anomaly score is obtained through a global branch, and then evidence learning dynamic fusion and cross-space consistency residual correction are performed to output unified anomaly scores and pixel-level defect positioning results. The application only needs a small amount of image-level labels for training, and both detection accuracy and real-time inference efficiency are considered.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of industrial visual inspection technology, and relates to a method and system for weakly supervised defect detection of industrial devices based on a dual-space consistency anchoring matrix. Background Technology

[0002] With the deepening development of intelligent manufacturing and the Industrial Internet, the manufacturing industry is accelerating its transformation and upgrading towards digitalization, networking, and intelligence. The integrated application of emerging technologies such as artificial intelligence, deep learning, and cloud computing is driving the transformation of quality control methods in industrial production from traditional manual inspection to intelligent visual inspection. As core components maintaining the integrity of equipment and structures, the defect detection of industrial devices is directly related to product reliability and production safety. Therefore, researching efficient and accurate industrial defect detection methods has significant engineering application value.

[0003] In real-world industrial scenarios, the number of anomalous samples is typically far fewer than that of normal samples, and the morphology of defects is complex and diverse, potentially manifesting as microscopic texture variations or macroscopic structural deviations. Furthermore, pixel-level annotation is prohibitively expensive, severely limiting the practical deployment of traditional strongly supervised learning methods. While existing anomaly detection methods based on unsupervised or weakly supervised learning can utilize a large number of normal samples for modeling, they still suffer from the following shortcomings under complex backgrounds and multi-scale anomaly conditions: First, the representation space is singular. Existing methods rely only on a single aspect of local feature distribution or global semantic features, which makes it difficult to fully characterize the multi-granularity characteristics of defects and is insufficient in discriminating against complex defects that involve both texture variation and structural distortion. Secondly, the lack of explicit modeling of cross-spatial structural consistency means that when local feature responses and global semantic responses contradict each other, there is a lack of effective joint inference mechanisms, which leads to a significant decrease in the robustness of the discrimination against fuzzy anomalies or structural confusion defects. Third, while memory-based feature matching methods have achieved certain results on public datasets, they face problems such as severe feature redundancy, high computational overhead, and insufficient protection of low-density normal patterns in actual deployments, making it difficult to balance detection accuracy and real-time requirements.

[0004] In conclusion, how to improve anomaly detection accuracy while reducing annotation costs, and how to balance inference efficiency and engineering practicality, remains a key issue that urgently needs to be addressed in the field of industrial anomaly detection.

[0005] To address the aforementioned issues, this solution proposes a method. Although the problem of scarce abnormal samples cannot be completely solved, this solution can still construct an effective and stable industrial product image anomaly detection system even under conditions of scarce abnormal samples. It also provides reliable technical support for quality traceability, predictive maintenance, and industrial data cloud migration in intelligent manufacturing environments. Summary of the Invention

[0006] In view of this, the purpose of this invention is to provide a method and system for weakly supervised defect detection of industrial devices based on a dual-space consistent anchoring matrix, which can improve the accuracy of anomaly detection while reducing annotation costs, and take into account inference efficiency and engineering practicality.

[0007] This invention employs a weakly supervised learning paradigm, using a large number of normal samples as the foundation and a small number of abnormal samples as supplementary data. Abnormal samples only require image-level labels (assigning normal / abnormal category information to the entire image, without pixel-level annotation), thus significantly reducing data annotation costs. The main aspects of this invention are: first, designing a structure-aware feature encoder to extract structured features that possess both geometric adaptability and multi-scale contextual relevance; then, mapping these features to a compact embedding space and decoupling them into local distributed features and global semantic features; constructing a hierarchical sparse memory optimization mechanism and a learnable prototype contrastive learning mechanism from a dual-space perspective; introducing a learnable cross-space consistency anchoring matrix to achieve semantic-geometric joint sparsity and self-cleaning of memory entries; and establishing an inverted index from prototype vectors to subsets of the memory to improve retrieval efficiency; in the inference stage, fusing the dual-branch outputs and quantifying cognitive uncertainty, ultimately outputting a unified anomaly score and refined defect localization results.

[0008] To achieve the above objectives, the present invention provides a method for detecting weakly supervised defects in industrial devices based on a dual-space consistency anchoring matrix, which includes a training phase and an inference phase.

[0009] The training phase includes: First, construct an image dataset of industrial devices, denoted as: , and These represent the image height and width, respectively, and 3 represents the number of RGB channels. The dataset consists of images of normal samples, providing only image-level labels (normal / abnormal).

[0010] Then, based on the constructed dataset, the following is executed: 1. Extract structure-aware feature maps with geometric adaptability and multi-scale context from the input image.

[0011] (1) Basic feature extraction For the input image First, an initial feature map is obtained through a basic feature extraction network consisting of three consecutive 3×3 convolutional layers (with a stride of 2 for the first two layers and a stride of 1 for the third layer, used for downsampling) and a ReLU activation function. (1) In the formula, This is the initial feature map. Basic convolutional networks, This refers to the number of output channels of the basic convolutional network.

[0012] (2) Spatial sampling offset prediction Initial feature map The data is fed into a deformable convolutional module to predict the spatial sampling offset map. : (2) In the formula, It is a lightweight convolutional layer. The total number of sampling points for each convolutional kernel. The number of channels in the spatial sampling offset map is... , respectively corresponding Spatial sampling offset in the x and y directions of each sampling point.

[0013] (3) Offset-driven cross-scale attention weights Spatial sampling offset map As a conditional signal, it is sent to the offset-driven attention weight generator (i.e., a small network consisting of global average pooling (GAP) and global max pooling (GMP) and two layers of MLP+Sigmoid) to generate cross-scale attention weights.

[0014] First, global average pooling (GAP) and global max pooling (GMP) operations are independently performed on each channel of the spatial sampling offset map to extract deformation features in different dimensions: (3) In the formula, This is a spatial sampling offset map. For global average pooling, For global max pooling, Offset feature vector after global average pooling. This is the offset feature vector after global max pooling.

[0015] Offset feature vector after global average pooling and the offset feature vector after global max pooling After feature concatenation, a cross-scale attention weight vector is generated through a two-layer fully connected network (MLP) and a sigmoid activation function. : (4) In the formula, For feature splicing operations, It is a two-layer fully connected neural network. This is the Sigmoid activation function.

[0016] This mechanism enables the network to dynamically adjust the response weights of features at different scales based on the degree of deformation of the defect.

[0017] (4) Multi-scale feature extraction and attention-weighted fusion Initial feature map The input is fed into the multi-branch dilated convolution module. This module contains... Parallel dilated convolution branches, dilation rate Set separately This is used to capture contextual information at different scales. The output feature maps of each branch are as follows: (5) in , This is the single-scale feature map output by the i-th dilated convolution branch. The void ratio is A 3×3 hollow convolutional layer.

[0018] Finally, using the attention weight vector The features of each branch are dynamically weighted and fused to obtain the final structure-aware feature map. : (6) In the formula, This is a structure-aware feature map.

[0019] 2. The structure-aware feature map is split into local distribution features and global semantic features through a decoupling mechanism.

[0020] Structure-aware feature maps It contains rich spatial information. To support both local distribution modeling and global semantic modeling, the structure-aware feature map is mapped to a unified, compact embedding space, and then decomposed into two complementary feature representations through a decoupling mechanism: local distribution features and global semantic features.

[0021] Local distribution characteristics: for Perform bottleneck projection, and project each spatial location The feature vectors are projected into a low-dimensional embedding space to form a local embedding graph Z that preserves fine spatial location information. The local distribution features are used for subsequent anchoring of the hierarchical sparse memory module to calculate the local distribution distance score.

[0022] Global semantic features: Global average pooling is performed in the spatial dimension to obtain aggregated global feature vectors. These vectors are then mapped to the same low-dimensional embedding space through another independent bottleneck projection layer, forming a global embedding vector that compresses global semantic information. Global semantic features are used in subsequent learnable prototype comparison learning modules to calculate global semantic similarity scores.

[0023] The specific splitting process is as follows: (1) Bottleneck projection Through a two-layer fully connected network (bottleneck projector) The feature vector at each spatial location is projected into a low-dimensional embedding space: (7) In the formula: For bottleneck projectors, This represents the number of channels in the structure-aware feature map. For the dimension of the low-dimensional embedding space, For unnormalized embedded feature maps, The height of the structure-aware feature map. The width of the electrical field in the structure-aware feature map.

[0024] (2) Decoupling is a local distribution feature By directly preserving the spatial dimension, we obtain a locally embedded graph: (8) In the formula, This is a partially embedded image.

[0025] For each spatial location of the local embedding graph Define local embedding vectors Then L2 normalization is performed: (9) Normalized local embedding vectors of all normal samples Composing the complete set That is, local distribution characteristics, the size of which is In the formula, For spatial location The original local embedding vector at that location, It is an L2 norm. It is a very small constant to prevent the denominator from being 0.

[0026] (3) Decoupling into global semantic features

[0027] right Global average pooling is performed along the spatial dimension, and then the global embedding vector is obtained through another bottleneck projector: (10) In the formula, The global bottleneck projector has the same structure as the local bottleneck projector but with independent parameters. The first layer will... The first layer maps the 128-dimensional dimension to a 128-dimensional dimension and then connects to ReLU. The second layer maps the 128-dimensional dimension to... =64 dimensions. Structure-aware feature maps In spatial location The complete channel feature vector at that location. This is an unnormalized global embedding vector.

[0028] For vectors Perform L2 normalization: Normalized global embedding vectors of all normal samples Form a set That is, global semantic features. Let be the normalized global embedding vector of the j-th normal sample. This represents the total number of normal samples in the training set.

[0029] 3. Construct a prototype contrastive loss and generate a normalized prototype vector set through contrastive learning based on global semantic features.

[0030] A set of discriminative prototype vectors is learned from global semantic features. These prototype vectors highly summarize the typical semantic patterns of normal industrial devices and serve as a reference benchmark for global semantic anomaly discrimination during the inference phase. The learnable prototype contrastive learning module requires training with normal samples and a small number of image-labeled anomalous samples, where the anomalous samples act as negative examples to help form discriminative boundaries for the prototypes. The images of anomalous industrial devices here are labeled at the image level.

[0031] (1) Input data Receive the normalized global embedding vector set of all normal samples For each training sample (including normal and abnormal samples), its global embedding vector is denoted as... And L2 normalization has been completed, that is ,in The global embedding vector of normal samples is used to construct the cluster center of the prototype, while abnormal samples are used to increase the distance from the normal prototype.

[0032] (2) Prototype initialization Set the number of prototype vectors Randomly initialize all prototype vectors, each prototype vector having dimension . Dimension, and perform L2 normalization on each prototype vector to make the magnitude length 1: (11) In the formula, For the first One prototype vector.

[0033] These prototype vectors serve as trainable parameters and participate in subsequent gradient updates. Initially, the prototype vectors are randomly distributed on the unit hypersphere, and gradually converge to prototype centers that can represent different normal semantic patterns as training progresses.

[0034] (3) Compare learning objectives The core objective of contrastive learning is to bring the global embedding vector of normal samples closer to its best-matching prototype vector, while keeping outliers away from all prototype vectors and promoting separation among the prototype vectors. To achieve this, the loss function consists of two parts: an attraction term and a repulsion term.

[0035] 1) Attraction term: For each normalized global embedding vector Calculate its cosine similarity with all prototype vectors: (12) Due to the normalization of the global embedding vector and the prototype vectors All values ​​have been normalized, and the cosine similarity is equivalent to the inner product, with a value range of [-1, 1]. In the formula, Let be the cosine similarity.

[0036] Temperature-scaling-based cross-entropy loss is used as the attraction term. For normal samples, this loss encourages them to be assigned to a specific prototype vector (i.e., the prototype vector most similar to them), while suppressing similarity with other prototype vectors. The attraction term is defined as: (13) In the formula, As an attraction, For the first The global embedding vector of each sample. For the winning prototype index, This is a temperature parameter (which can be set to 0.1). The total number of training samples, To and The best matching prototype vector.

[0037] 2) Exclusion Term: To prevent redundancy caused by multiple prototype vectors pointing to the same semantic center, a prototype separation regularization term is introduced as a loss. Here, the same semantic center means that in the high-dimensional global semantic feature space, multiple prototype vectors converge to the same feature cluster center position of the normal pattern after training, and their semantic connotations highly overlap, all corresponding to the semantic pattern of the same normal device.

[0038] Prototype separation regularization loss penalizes any two prototype vectors whose cosine similarity exceeds a preset upper bound. Situation: (14) In the formula, This is an exclusion term. Indicates the first One prototype vector, By setting Exclusion and The same prototype vector (i.e., the similarity between the prototype vector and itself is not calculated).

[0039] This regularization term penalizes any two prototype vectors whose cosine similarity exceeds a preset upper bound. This situation causes the prototype vectors to be mutually orthogonal, covering different normal mode subspaces.

[0040] Total prototype contrast loss As a weighted sum of attracting and repulsing terms, the prototype contrastive loss forces the global semantic features of normal samples to form a compact, separated cluster structure in the prototype space.

[0041] 3) The weighted sum of the attraction and repulsion terms yields the prototype contrast loss: (15) in, The weighting coefficients for the exclusion term are used to balance cluster compactness and prototype separability.

[0042] 4) In the learnable prototype contrastive learning module, normal and abnormal industrial device images are used for training, and the prototype contrastive loss is used during the training process. Abnormal samples serve as negative examples to help form discriminative boundaries for the prototype vectors.

[0043] After training, the module outputs a normalized set of discriminative prototype vectors. .

[0044] 4. Use local distribution features as initial candidate memories; generate cross-space consistency anchoring matrix; divide the initial candidate memories into anchoring core layer and distribution filling layer according to cosine similarity; perform consistency residual feedback elimination on the distribution filling layer; generate inverted association index between prototype vector and corresponding memory subset based on cross-space consistency anchoring matrix.

[0045] (1) Initialize candidate memory Construct the entire set from the normalized local embedding vectors. All vectors are used as the initial candidate memory. The initial candidate memory contains a large number of samples, which need to be sparsified.

[0046] (2) Generation and iterative update of cross-space consistent anchoring matrix The cross-space consistency anchoring matrix is ​​the core hub for achieving dual-space collaborative modeling. As a learnable, dynamic structural prior, it establishes an explicit statistical correlation between local distributed features and global semantic prototypes. This matrix simultaneously serves as both the "calculation basis within the memory module" and the "input parameter for joint optimization of the two spaces." During training, it iteratively optimizes through a closed-loop mechanism of forward generation and backward update, ultimately achieving deep alignment between local geometric information and global semantic information.

[0047] The cross-space consistent anchoring matrix is ​​a learnable dynamic parameter matrix, and its generation and update process is as follows: Initial generation (first forward propagation): During the first round of forward propagation in training, the anchoring matrix is ​​generated for the first time within the anchor-guided hierarchical sparse memory module. The input to the anchoring matrix is ​​the output of two external modules: 1) Normalized set of locally embedded vectors , Let be the normalized local embedding vector of the i-th training sample; 2) Prototype vector set , This is the k-th normalized prototype vector.

[0048] make This represents the total number of entries in the initial candidate memory. The height and width of the locally embedded image. This represents the number of normal samples in the training set. Because... and All have been L2 normalized, and the anchor matrix elements are obtained by cosine similarity calculation: , Let be the cosine similarity; a larger value indicates a stronger semantic association between the i-th local feature and the k-th prototype vector. The element in the i-th row and k-th column of the cross-space consistency anchoring matrix forms the cross-space consistency anchoring matrix. The initially generated anchoring matrix serves as an internal parameter of the module and is directly used in subsequent memory hierarchical partitioning operations.

[0049] Starting from the second round of training, the anchoring matrix has both internal input and external output attributes, forming a complete iterative closed loop: the anchoring matrix serves as both an internal input, participating in cosine similarity hierarchical layering, consistency residual filtering, and inverted index construction, and an external output, with the anchoring matrix calculated in the current round serving as the core output of the memory module, used for subsequent calculations of consistency constraint loss.

[0050] (3) Hierarchical stratification The entries in the memory bank are divided into two layers based on cosine similarity: 1) Anchoring the core layer For each prototype vector Choose the one with the highest similarity to the cosine. Local features (Recommended) =50). These features are forcibly preserved and assigned to the anchored core layer to maintain basic consistency across space.

[0051] 2) Distributed filling layer The remaining features are assigned to the distribution-filling layer, where sparsity and cleanup are required. This is a set of candidate memory banks.

[0052] 3) Adaptive sparsity reduction of the distributed filling layer For each feature in the distribution-filled layer First, calculate its local density (kernel density estimation): (16) In the formula, Indicates and Euclidean distance nearest A neighbor, acceptable . Let be the Gaussian kernel bandwidth, and let be the median of the distances between all feature pairs. The kernel density estimate of the target feature represents the local density of the normalized local embedding vector in the normal feature space; a larger value indicates that the feature belongs to a common normal pattern, and a smaller value indicates that it belongs to a rare normal pattern. To normalize the local embedding vector for the target, for A k-nearest neighbor feature, right Sum the features of all K nearest neighbors.

[0053] Then construct the spatial gradient accumulation curve: (17) That is, the density is less than or equal to the density threshold. The number of features. By finding the curve The inflection point (where the second derivative is at its maximum) determines the adaptive sampling rate. Finally, random sampling is performed in the distributed filling layer. Features, and the sampling probability is Proportional (rare patterns are preferred). Where, This is the cumulative value of the spatial gradient. Density threshold A larger value indicates that the feature is in a high-density area (common in normal patterns), while a smaller value indicates that it is rare. Count the features that meet the conditions. Local features The estimated kernel density.

[0054] (4) Consistency residual feedback elimination For each feature retained in the distributed filling layer Calculate the inconsistency residual between it and the corresponding maximum associated prototype vector. : (18) In the formula, For the i-th local feature The cross-spatial consistency residual. When a feature in the distribution-filled layer... Cross-space consistent residuals Greater than the preset threshold If so, then remove that feature, where can be taken as . .

[0055] This step enables semantic-geometric joint self-cleaning.

[0056] (5) Final memory and inverted index After the above screening, the final memory bank is: ,in , The distribution fill layer is used to filter and remove features.

[0057] To accelerate reasoning retrieval, an inverted index is established based on a cross-space consistent anchoring matrix: for the k-th prototype vector Define its corresponding subset of the memory bank as: (19) For each local feature Find the prototype vector with the highest cosine similarity; if the prototype vector is numbered k, then... Subset Each local feature belongs to one and only one prototype subset. Essentially, it establishes an inverted index of "prototype → corresponding local feature set": each local embedding vector in the memory is assigned to the prototype vector with the highest cosine similarity, ultimately resulting in each prototype vector corresponding to a unique local feature subset. This is a subset of the memory bank corresponding to the k-th prototype vector. For the i-th normalized local embedding vector, The value in the i-th row and k'-th column of the anchoring matrix A represents the cosine similarity between the i-th local feature and the k'-th prototype vector. The larger the value, the stronger the correlation. This represents the total number of global prototype vectors. For the i-th local feature, after traversing all P prototype vectors, return the number of the prototype vector with the highest similarity to the cosine.

[0058] Each memory bank subset Assign it to the prototype vector that maximizes the cosine similarity. Save the inverted index. .

[0059] 5. Calculate the KL divergence distribution alignment loss based on local distribution features, and construct the global average feature alignment loss based on local distribution features and global semantic features; combine the KL divergence distribution alignment loss, the global average feature alignment loss and the prototype comparison loss to perform joint optimization of local and global spaces, and generate a consistent prior benchmark.

[0060] By utilizing the cross-space consistency anchoring matrix and the prototype vector set, two statistical consistency constraints (KL divergence distribution alignment and global average feature alignment) are used to force the local distributed feature space and the global semantic feature space to maintain structural alignment, and finally output a consistent prior benchmark for cross-space constraint correction during the inference stage.

[0061] (1) Input data includes cross-space consistent anchoring matrix A Prototype vector set The local embedding map corresponding to the j-th training image The normalized global embedding vector corresponding to the j-th training image and prototype contrast loss .

[0062] (2) KL divergence distribution alignment loss KL divergence alignment loss ensures that each local embedding vector is consistent with its most relevant prototype vector in terms of probability distribution, thereby inversely passing global semantic information to the learning of local features.

[0063] 1) For each local embedding vector Find the most relevant prototype vector index based on the cross-space consistency anchoring matrix: (20) In the formula, For anchoring matrix elements, scalar, range is . The integer is the index of the prototype vector that best matches the locally embedded vector.

[0064] 2) Introduce two learnable linear mapping matrices. and ,in The dimension is the probability space. The local embedding vector... and the corresponding prototype vector Mapped to The dimensional space is transformed into a probability distribution using Softmax: (twenty one) The Softmax operation is defined as follows: (twenty two) 3) Calculate the KL divergence between the two probability distributions: (twenty three) in (twenty four) (3) Global average feature alignment loss To ensure that the bispace does not produce systematic biases in global statistical properties, the average features of the entire local embedding graph should be close to the values ​​of the global embedding vector.

[0065] 1) For the j-th training image, calculate its local embedding map. Spatial average characteristics:

[0066] (25) In the formula, For spatial location The local embedding vector at that location.

[0067] 2) Calculate the average feature and the global embedding vector of the same image. The squared Euclidean distance between them is averaged over all training images: (26) In the formula, This represents the total number of samples in the training set.

[0068] (4) Overall loss and training strategy The two consistency losses and the prototype comparison loss are weighted and summed to form the total loss function for the entire training phase: (27) In the formula, the weighting coefficients are set empirically. , .

[0069] Total losses during training The following parameters are updated simultaneously via backpropagation: The parameters of the convolutional layers in the structure-aware feature encoder, the parameters of the bottleneck projector used for feature decoupling, and the learnable linear mapping matrix. and Learnable prototype vector set (pass The gradient of the cross-space consistency anchoring matrix is ​​not directly optimized, but rather through... and The update indirectly changes things.

[0070] (5) Generation of consistent prior benchmarks After training is complete (with all parameters converged and fixed), the normal range of the bispace response differences is statistically determined using all normal samples (containing no outliers). A consistent prior benchmark is used for cross-space constraint correction during the inference phase.

[0071] The calculation steps for the consistent prior benchmark are as follows: 1) For each normal image j, calculate its local distribution distance: (28) In the formula, It is a subset of the semantically relevant activation memory corresponding to the normal image j. Its generation logic is completely consistent with the inference stage (i.e., matching the first three prototype vectors + taking the union of the corresponding subsets by the inverted index). For the local distribution anomaly score (unnormalized) of normal image j, Linearly normalize to [0,1]. For a single local embedding vector in a subset of the memory bank, Let cosine similarity be the similarity between two vectors. For the normal image j in spatial location The normalized local embedding vector at that location.

[0072] 2) Calculate global semantic similarity. (29) In the formula, The global semantic similarity score for normal image j. Let k be the normalized discriminative prototype vector. This is the set of discriminative prototype vectors after training. Let be the normalized global embedding vector of the normal image j.

[0073] 3) Define inconsistency measures: (30) In the formula, This is a measure of cross-spatial inconsistency for a normal image j. When the two spaces are consistent, the local distribution distance is small (close to 0) and the global similarity is large (close to 1). Approaching 0, therefore Approaching 0; if a contradiction arises (e.g., large local distance but still large global similarity), then... Significantly greater than 0.

[0074] 4) Calculate for all normal samples mean and standard deviation : (31) In the formula, This is the mean of the inconsistency measure across all normal samples. The standard deviation of the inconsistency measure for all normal samples. This represents the total number of normal samples in the training set.

[0075] Will It is stored as a consistent prior benchmark for use in the inference phase.

[0076] The inference phase is the practical application phase of the method of this invention. Its core objective is to quickly and accurately complete defect detection and localization of industrial device images using the fixed model parameters obtained in the training phase. Unlike the training phase, the inference phase does not update any network parameters. All modules (structure-aware encoder, dual-space decoupling unit, hierarchical sparse memory, discriminative prototype set, cross-space consistency anchoring matrix, and prototype-memory inverted index) are in a fixed state after training convergence. The inference phase adopts a technical approach of "dual-branch independent detection + evidence learning dynamic fusion + cross-space consistency constraint correction".

[0077] The reasoning stage includes: 6. Extract structure-aware feature maps from the image to be tested, and then split them into local embedding maps and global embedding vectors.

[0078] Image to be tested Input the structure-aware feature encoder to obtain the structure-aware feature map. Subsequently, the structure-aware feature map was split. Output respectively: (1) Locally embedded graph Its dimensions are Preserving detailed spatial location information. Each spatial location in the local embedding map. The normalized local embedding vector on is denoted as Local embedding vectors are used to characterize the local distribution features of the image under test, focusing on representing the geometric shape, edge texture and spatial distribution information of defects.

[0079] (2) Global embedding vector , dimension To compress a single vector of global information, L2 normalization is performed to obtain... This vector is used to characterize the global semantic features of the image under test, focusing on representing the category attributes of defects and the overall contextual semantic information.

[0080] 7. Select multiple prototype vectors similar to the normalized global embedding vector, and use the inverted index generated during the training phase to retrieve the memory subset corresponding to the prototype vector; calculate the cosine distance between the normalized local embedding vector at each spatial location on the local embedding graph and the memory entry vector in the memory subset to obtain the local distribution anomaly score; calculate the cosine similarity between the normalized global embedding vector and all prototype vectors to obtain the global semantic anomaly score.

[0081] Traditional memory retrieval methods require traversing all entries in the memory to calculate similarity, resulting in a time complexity of O(n log n). ( The total number of entries in the memory is insufficient to meet the real-time detection needs of industry. This invention utilizes the prototype-memory inverted index established during the training phase to achieve rapid matching of locally distributed features.

[0082] (1) Quickly activate a subset of semantically relevant memory. Normalize the global embedding vector Match the prototype vectors obtained during the training phase to find the three prototypes with the highest similarity. The corresponding subset of the single prototype memory is retrieved using the inverted index and merged into a set. .

[0083] (2) Calculate the distribution distance score Only in the active subset of the memory bank Within the local embedding graph, the cosine distance between the normalized local embedding vector at each spatial location and the vector of each memory entry in the active memory subset (the memory entry vector refers to each independently stored, normalized local embedding vector in the sparse memory constructed during the training phase) is calculated, and the maximum value is taken as the anomaly score of the local distribution branch. (32) (33) In the formula, Spatial location in a localized embedded graph Pixel-level anomaly scores at the location, To score the image-level local distribution anomalies of the image under test. For the semantically relevant activation memory subset corresponding to the current image to be tested, To activate a single normalized local embedding vector in the memory subset (the memory entry vectors (local embedding vectors) in the activation subset are all vectors constructed and saved during the training phase), The cosine similarity function for two normalized vectors takes values ​​in the range [-1, 1]. Spatial location of the local embedding map of the image to be tested The normalized local embedding vector at that location, These represent the height and width of the local embedded image, respectively.

[0084] (3) Normalize the global embedding vector Cosine similarity is calculated with all discriminative prototype vectors, and the maximum value is taken as the normal pattern matching score, which is then converted into a global semantic anomaly score. (34) In the formula, This represents the image-level global semantic anomaly score, ranging from [0,2]. A larger value indicates a more severe deviation from the normal semantic pattern. For the set of discriminative prototype vectors after training, For the first A normalized prototype vector, is the normalized global embedding vector of the image to be tested.

[0085] 8. Based on evidence learning theory, the cognitive uncertainty of global and local branches is quantified, and the local distribution anomaly score and the global semantic anomaly score are adaptively weighted and fused according to the uncertainty.

[0086] Different types of defects have varying degrees of dependence on global and local branches: for defects with obvious morphological features (such as cracks and scratches), local branches are more reliable; for defects with obvious semantic features (such as discoloration and corrosion), global semantic branches are more reliable. Fixed-weight fusion methods cannot adapt to this difference. This invention introduces evidence learning theory to quantify the cognitive uncertainty of the outputs of the two branches, and adaptively generates dynamic fusion weights based on the uncertainty to achieve weighted fusion dominated by reliable branches.

[0087] (1) Evidence transformation and uncertainty quantification The anomaly scores of the two branches are converted into evidence quality functions, and their respective cognitive uncertainties are calculated: (35) (36) (37) (38) In the formula, and These are the evidence values ​​for the local distribution branch and the global semantic branch, respectively. and The cognitive uncertainty is represented by the local distribution branch and the global semantic branch, respectively. The lower the uncertainty, the more reliable the result of that branch. The confidence level of evidence for local distribution branches reflects the degree to which local branches support the current detection conclusion. The cognitive uncertainty of the local distribution branch represents the degree of unreliability of the output results of the local branch. The smaller the value, the higher the credibility of the detection results of the local branch. The evidence confidence score for the global semantic branch reflects the degree to which the global semantic branch supports the current detection conclusion. This represents the cognitive uncertainty of the global semantic branch. It characterizes the unreliability of the output of the global semantic branch; the smaller the value, the higher the reliability of the detection result of the global semantic branch.

[0088] (2) Generate dynamic fusion weights Based on the uncertainty of the two branches, the fusion weights are adaptively calculated: (39) (40) The lower the uncertainty (the higher the evidence value), the higher the fusion weight. and For dynamic fusion weights.

[0089] (3) Preliminary weighted fusion The outlier scores of the two branches are fused using dynamic fusion weights to obtain a preliminary fusion score: (41) In the formula, This is a preliminary fusion of abnormal scores.

[0090] 9. Calculate the consistency residual using the cross-space consistency anchoring matrix, and correct the weighted fusion score based on the consistency residual.

[0091] Even if both branches give high anomaly scores, it could be a false positive due to noise interference such as changes in lighting or dust. The morphological and semantic features of true defects must conform to the cross-space consistency prior learned during the training phase, while noise usually leads to inconsistencies in the two-space features. Therefore, the initial fusion score is constrained and corrected by calculating the consistency residual.

[0092] (1) Calculate the consistency residual The cross-space consistency anchoring matrix optimized during the training phase is invoked to calculate the consistency residual of the current test image: (42) (43) In the formula, This is a cross-space consistent anchoring matrix. The distribution matching degree vector of all local locations in the image to be tested is obtained by calculating the cosine similarity between the local embedding vector of each spatial location on the local embedding map and all prototype vectors, and summing them up. This is the predicted theoretical semantic matching degree vector. For consistency residuals, the larger the residual, the lower the matching degree of the features in the two spaces, and the more likely it is to be noise. This is the global semantic matching degree vector of actual observation. Let be a P-dimensional column vector, where the k-th element is the cosine similarity between the global embedding vector to be tested and the k-th prototype vector (since the vector has been normalized, this is equivalent to the vector dot product): (44) Where k=1, 2, ... P, the similarity values ​​corresponding to all prototypes are combined in order to obtain the global semantic matching degree vector of actual observation, as shown in the following formula.

[0093] (45) Each element in the vector takes values ​​in the range [-1, 1]. The larger the value, the higher the degree of matching between the global semantics and the corresponding prototype.

[0094] (2) Cross-space constraint correction Design an exponential correction function to correct the initial fusion score: (46) In the formula, To correct the intensity hyperparameter, To achieve a unified anomaly scoring system.

[0095] Correction effect: When the residual is When the correction coefficient approaches 1, the score of the true defect is basically preserved; when the residual As the value increases, the correction coefficient decreases rapidly, and the score of noisy samples is significantly suppressed.

[0096] 10. Spatial location based on local embedding graph The pixel-level anomaly score is used to restore the anomaly score map to the size of the input test image through bilinear interpolation upsampling; then, the upsampled score of each pixel in the anomaly score map upsampled to the size of the input test image is corrected.

[0097] (1) Calculate the local anomaly score for each pixel in the local embedding map. The pixel-level anomaly score, calculated for each spatial location on the local embedding map, is directly represented as the probability that the local region belongs to a defect: (45) (2) Bilinear interpolation upsampling Since the size of the local embedding map is 1 / 4 of the original image, bilinear interpolation is needed to restore the low-resolution pixel-level anomaly score map to the original image size. (46) In the formula, Spatial location in a localized embedded graph Pixel-level anomaly score at the location; It is a bilinear interpolation upsampling function; This is an anomaly score map upsampled to the original image size.

[0098] The upsampling score for each pixel is then corrected: (47) (48) (49) in, For cross-space consistent anchoring matrix, For spatial location Theoretical semantic matching degree vector at the location, For spatial location The local distribution matching degree vector at that location, For the corrected spatial position The final abnormal score at the location, To correct the intensity hyperparameter, For spatial location Pixel-level cross-spatial consistency residuals at the location. Spatial location at the original image resolution The pixel-level local distribution anomaly score is obtained after bilinear interpolation upsampling.

[0099] The second aspect of the present invention provides a weakly supervised defect detection system for industrial devices for implementing the method described in the first aspect. The system includes a structure-aware feature encoder module, a feature decoupling module, an anchor-guided hierarchical sparse memory module, a learnable prototype comparison learning module, a dual-space joint optimization module, a global branch prototype matching module, a local branch accelerated matching module, an evidence learning dynamic fusion module, a cross-space consistency constraint correction module, and a defect-aware region mapping module.

[0100] I. Structure-Aware Feature Encoder Module This module extracts structure-aware feature maps with geometric adaptability and multi-scale context from input images. It utilizes deformable convolutions to predict the spatial offset of each sampling point, enabling the receptive field to flexibly match the irregular deformation of defects. These offsets are then used as conditional signals to an attention weight generator, dynamically adjusting the response weights of dilated convolution branches with different dilation rates. This guides the network to automatically focus on the defect region and expand the effective receptive field. The final output feature representation preserves the geometric structure of local details while incorporating multi-scale information from the surrounding environment.

[0101] II. Feature Decoupling Module This module maps the structure-aware feature map into a compact embedding space and decouples it into two complementary representations through a decoupling mechanism: local distribution features and global semantic features. Local distribution features focus on characterizing the changes in local textures, edges, and fine-grained structures in the image, and are suitable for fine comparison of anomalous regions; global semantic features focus on the category information and global context of the overall object, and are used to determine whether the current sample deviates from the normal semantic pattern.

[0102] III. Anchored Guided Hierarchical Sparse Memory Module An anchor-guided hierarchical sparse memory module is located in a local branch and is used to build and maintain a high-quality, low-redundancy memory during the training phase. This module uses a cross-spatial consistency anchoring matrix as a structural prior, dividing memory entries into a mandatory "anchored core layer" and a "distribution-filling layer" to be sparsified, based on their association strength with the prototype vector. For entries in the distribution-filling layer, kernel density estimation is used to model the spatial gradient accumulation curve, adaptively determining the optimal sampling rate, and a consistency residual feedback mechanism is used to remove high-residual samples with cross-spatial inconsistencies. Ultimately, semantic-geometric joint sparsification and self-cleaning of the memory are achieved, while an inverted index from the prototype vector to the memory subset is established, significantly improving retrieval efficiency during the inference phase.

[0103] Among them, the structure-aware feature encoder module, the feature decoupling module, and the anchor-guided hierarchical sparse memory module are all trained using normal samples.

[0104] IV. Learnable Prototype Comparison Learning Module This module, located in the global branch, generates a set of highly discriminative prototype vectors during the training phase. It receives global semantic features as input and, through a category-level contrastive learning strategy, narrows the distance between features of samples of the same category (or the same normal pattern) and their corresponding prototypes, while simultaneously distancing features of samples from different categories or anomalous samples. After end-to-end optimization, the prototype vectors highly summarize the typical semantic patterns of normal devices, thus serving as a reliable reference benchmark for calculating the semantic similarity score of input samples during inference.

[0105] V. Dual-space joint optimization module The dual-space joint optimization module is the core of achieving dual-space consistency during the training phase. This module receives the anchoring matrix of the local branch and the prototype vector set of the global branch, with the direct optimization objective being to minimize the KL divergence of their corresponding feature distributions. During training, this module not only ensures statistical alignment between local and global semantic features but also backpropagates the consistency prior to memory updates and prototype learning, ensuring that both branches, while optimizing independently, are always guided and constrained by the other's spatial information. This joint optimization effectively avoids semantic drift in dual-space representations, providing a stable and reliable anchoring matrix for cross-space corrections during the inference phase.

[0106] Among them, the learnable prototype contrastive learning module and the dual-space joint optimization module require normal samples and abnormal samples (image-level labels only) for training.

[0107] VI. Global Branch Prototype Matching Module The global branch prototype matching module utilizes the discriminative prototype vector set learned during the training phase (each prototype represents a typical global semantic pattern of a normal device) to match the global semantic features of the input image. This module first calculates the cosine similarity between the global embedding vector and all prototype vectors, taking the highest similarity as the global semantic anomaly score. This score reflects the degree to which the image deviates from the normal pattern at the semantic level—when the image contains global anomalies, the global matching similarity is low, and the anomaly score is high; conversely, the score is low. This module provides anomaly judgment criteria from a holistic semantic perspective for subsequent fusion.

[0108] VII. Local Branch Accelerated Matching Module The local branch acceleration matching module is responsible for efficiently calculating the local anomaly score of the test image in the local feature space. This module utilizes a prototype-memory inverted association index pre-built during the training phase: first, it quickly matches the global semantic features of the test image with prototype vectors, activating the few most relevant prototype vectors. Then, it directly locates the corresponding subsets of the memory using the inverted index, thus avoiding a linear scan of the entire memory. Within the activated subset, it calculates the cosine distance between each local embedding vector and all entries in the subset, taking the maximum distance as the local anomaly score at the corresponding location of the local embedding vector. Finally, it summarizes these to obtain the image-level local distribution anomaly score. This module significantly reduces the retrieval time complexity while maintaining detection accuracy, meeting industrial real-time requirements.

[0109] VIII. Evidence Learning Dynamic Fusion Module The evidence learning dynamic fusion module, based on evidence learning theory, adaptively weights and fuses the anomaly scores from the global and local branches. First, the module converts the anomaly scores of the two branches into evidence quality parameters, quantifying the cognitive uncertainty of each branch's output—lower uncertainty indicates greater credibility of the branch's current result. Then, it dynamically calculates the fusion weights of the two branches, giving greater weight to the branch with higher credibility. Finally, a preliminary fusion score is obtained through weighted summation. This dynamic fusion mechanism can address the differences in the dependencies of different types of defects on different feature spaces (e.g., morphological defects depend on local branches, semantic defects depend on global branches), significantly improving the robustness and adaptability of the fusion results.

[0110] IX. Cross-space consistency constraint correction module This module refines the anomaly score obtained from the weighted fusion process by directly calling the cross-spatial consistency anchoring matrix optimized during training to calculate the consistency residual between the local distribution features and global semantic features of the current test image. A large residual indicates a conflict between the two spaces, requiring suppression of false responses potentially caused by noise or confused regions; conversely, a smaller residual strengthens the response to true defects. Through this statistical prior-based constraint correction, the module significantly reduces false alarms and improves the response contrast of defective regions, outputting a cleaner and more accurate unified anomaly score.

[0111] X. Defect-aware region mapping module The defect-aware region mapping module transforms the corrected unified anomaly score into intuitive defect localization results. This module first upsamples the score map to the resolution of the original input image using bilinear interpolation. Then, utilizing the cross-spatial consistency anchoring matrix optimized during training, it performs pixel-by-pixel consistency residual correction and confidence calibration for the anomaly response of each pixel. Finally, it generates a pixel-level anomaly heatmap or binary mask, clearly marking the location, shape, and confidence of each defect, achieving a complete output from image-level anomaly detection to pixel-level defect localization.

[0112] The beneficial effects of this invention are as follows: (1) The present invention adopts a weakly supervised learning paradigm. Only a small number of image-level anomaly labels are needed during the training phase (no pixel-level fine annotation is required). Based on a large number of normal samples, the workload and time cost of manual annotation are greatly reduced, making the invention more adaptable to industrial sites with limited annotation resources.

[0113] (2) This invention constructs a structure-aware feature encoder and introduces deformable convolution adaptive prediction spatial sampling offset, so that the receptive field can flexibly match the geometric deformation of the defect; at the same time, it uses the offset-driven cross-scale attention gating mechanism to fuse different receptive field features output by multi-branch dilated convolution, so that the encoder can capture micro-texture anomalies and macro-structure deviations at the same time, thereby enhancing the discrimination power against composite defects.

[0114] (3) This invention introduces a learnable cross-space consistency anchoring matrix to explicitly model the statistical correlation between local distribution features and global prototype vectors, and performs joint optimization with the constraint of minimizing KL divergence, so that the two feature spaces maintain structural consistency during training, forming a consistent prior benchmark in the training phase. During inference, this prior benchmark is used to constrain and correct the anomaly score, effectively suppressing false responses caused by noise such as changes in illumination and dust, while enhancing the response contrast of real defect areas and reducing false alarm and false negative rates.

[0115] (4) This invention uses an anchoring matrix to divide the entries in the memory into a mandatory "anchoring core layer" and a sparsifiable "distribution filling layer". The optimal sampling rate is adaptively determined by kernel density estimation, and redundant entries that are inconsistent across spaces are eliminated by consistency residual feedback, thereby realizing the semantics of the memory. Geometric joint sparsification and self-cleaning are implemented; at the same time, an inverted index is established from the prototype vector to the subset of the memory bank. During inference, only the relevant subset is activated for retrieval, reducing the time complexity from linear scanning to constant level, which meets the real-time detection requirements of industrial production lines.

[0116] (5) This invention quantifies the cognitive uncertainty of local and global branches based on evidence theory, and adaptively generates fusion weights according to the uncertainty, so that the branch with high credibility dominates the final judgment. This mechanism can flexibly cope with the differences between morphological and semantic dependent defects, avoid the performance bottleneck caused by fixed weight fusion, and enhance the adaptability and stability of the method in complex industrial scenarios.

[0117] (6) This invention generates a clear heat map or binary mask by upsampling the corrected anomaly score map back to the original image resolution, which can mark the location, shape and confidence level of defects, and provide reliable technical support for subsequent process improvement or equipment maintenance.

[0118] Other advantages, objectives, and features of the invention will be set forth in part in the description which follows, and in part will be apparent to those skilled in the art from the following examination, or may be learned from practice of the invention. The objectives and other advantages of the invention can be realized and obtained through the following description. Attached Figure Description

[0119] To make the objectives, technical solutions, and advantages of the present invention clearer, the preferred embodiments of the present invention will be described in detail below with reference to the accompanying drawings, wherein: Figure 1 This is a diagram illustrating the overall framework of the weakly supervised defect detection method for industrial devices based on a dual-space consistency anchoring matrix proposed in this invention. Figure 2 This is a framework diagram for the training phase; Figure 3 Here is a framework diagram of the structure-aware feature encoder module; Figure 4 Workflow diagram for the hierarchical sparse memory module for anchoring guidance; Figure 5 This is a diagram illustrating the overall framework of the dual-space joint optimization module. Figure 6 A framework diagram for the reasoning stage; Figure 7 This is a schematic diagram showing the location of defects in the screw to be tested. Detailed Implementation

[0120] The following specific examples illustrate the implementation of the present invention. Those skilled in the art can easily understand other advantages and effects of the present invention from the content disclosed in this specification. The present invention can also be implemented or applied through other different specific embodiments, and various details in this specification can be modified or changed based on different viewpoints and applications without departing from the spirit of the present invention. It should be noted that the illustrations provided in the following embodiments are only schematic representations of the basic concept of the present invention. Unless otherwise specified, the following embodiments and features can be combined with each other.

[0121] The accompanying drawings are for illustrative purposes only and are schematic diagrams, not actual pictures. They should not be construed as limiting the invention. To better illustrate the embodiments of the invention, some parts in the drawings may be omitted, enlarged, or reduced, and do not represent the actual product dimensions. It is understandable to those skilled in the art that some well-known structures and their descriptions may be omitted in the drawings.

[0122] In the accompanying drawings of the embodiments of the present invention, the same or similar reference numerals correspond to the same or similar components. In the description of the present invention, it should be understood that if terms such as "upper," "lower," "left," "right," "front," and "rear" indicate the orientation or positional relationship based on the orientation or positional relationship shown in the drawings, they are only for the convenience of describing the present invention and simplifying the description, and do not indicate or imply that the device or element referred to must have a specific orientation, or be constructed and operated in a specific orientation. Therefore, the terms used to describe positional relationships in the drawings are only for illustrative purposes and should not be construed as limiting the present invention. For those skilled in the art, the specific meaning of the above terms can be understood according to the specific circumstances.

[0123] This embodiment uses an M3×8 Phillips head pan head screw as the inspection object to simulate an industrial quality inspection scenario. All images are acquired using a fixed-pose industrial camera and a ring light source to ensure uniform illumination and consistent background. The image size is uniformly scaled to 256×256×3 RGB format.

[0124] The dataset contains a large number of normal samples and a small number of abnormal samples (abnormality types include thread defects, surface scratches, head deformation, etc.). Abnormal samples are only provided with image-level labels. The training and test sets are divided in an 8:2 ratio. The training set contains 960 normal samples and 40 abnormal samples (negative examples used only for prototype contrast learning), while the test set contains 200 normal images and 50 abnormal images. The specific implementation processes for the training and inference phases are described below.

[0125] Training phase 1. Training of Structure-Aware Feature Encoder An input image of a 256×256×3 screw is downsampled by a basic convolutional network (3 convolutional layers, stride 2 for the first two layers and stride 1 for the third layer) to obtain an initial feature map of size 64×64×64 (i.e., height and width are both 64, and the number of channels is 64). A deformable convolutional module predicts the spatial offsets in the x and y directions for each of the 9 sampling points of the 3×3 convolutional kernel, outputting an offset map with 18 channels (corresponding to the dx and dy offset components of each of the 9 sampling points). This offset map is then processed by global average pooling and global max pooling, and input into two layers of MLP+Sigmoid to generate attention weight vectors (dimension equal to the number of dilated convolution branches; in this example, 4 branches are used, with dilation rates set to 1, 2, 3, and 4 respectively). The attention weights dynamically adjust the responses of each branch, and after weighted fusion, a structure-aware feature map is output, still with a size of 64×64×64.

[0126] In this embodiment, after training, the encoder exhibits adaptive focusing capability: for thread defects (large-scale structural changes), the weight assumptions of void ratio branches 3 and 4 are increased to 0.38 and 0.42, respectively; for surface scratches (fine-grained texture), the weight assumptions of void ratio branches 1 and 2 are increased to 0.45 and 0.35, respectively.

[0127] 2. Feature decoupling The 64×64×64 structure-aware feature map is input into the feature decoupling module for feature decoupling, resulting in the following two types of features: Local distribution features: A bottleneck projector (two fully connected layers; the first layer maps the input dimension from 64 to 128 and then applies the ReLU activation function; the second layer maps the 128-dimensional dimension back to 64-dimensional) maps the 64-dimensional features at each spatial location into a 64-dimensional embedding, resulting in an unnormalized local embedding map (64×64×64). L2 normalization is then performed (adding a minimal constant ε=1e^(-1 / 2)). -8 This yields a normalized local embedding graph Z, where each position corresponds to a 64-dimensional unit vector.

[0128] Global semantic features: Global average pooling is performed on the structure-aware feature map in the spatial dimension to obtain a 64-dimensional vector. This vector is then mapped to 64 dimensions by independent bottleneck projectors (with identical structures and independent parameters). Finally, L2 normalization is performed to obtain a normalized global embedding vector. (Dimension 64).

[0129] In this example, the training set of 960 normal images generated a total of 960×64×64 = 3,932,160 local embedding vectors (as the initial candidate memory) and 960 global embedding vectors (for prototype learning). These are used to construct the hierarchical sparse memory and to learn the discriminative prototype set, respectively.

[0130] 3. Construction of an anchor-guided hierarchical sparse memory library (1) Initialize candidate memory All normalized local embedding vectors are used as the initial candidate memory base, with the number of entries... The figure is 3,932,160.

[0131] (2) Generation of cross-space consistent anchoring matrix During the initial forward propagation, the anchoring matrix A is calculated using cosine similarity from local embedding vectors (3,932,160 vectors) and global prototype vectors (in this embodiment, the number of prototype vectors P=100, randomly initialized and L2 normalized). , generate representation of the first The local feature and the first The semantic association strength of each prototype, with a size of ×100.

[0132] (3) Hierarchical stratification For each prototype Choose the one with the highest similarity to the cosine. One local embedding vector is forcibly retained in the "anchored core layer". A total of 100 prototypes retain 100 × 50 = 5000 features. The remaining local embedding vectors are assigned to the "distribution padding layer".

[0133] (4) Adaptive sparsification of the distributed filling layer For each local embedding vector of the distribution-filled layer Kernel density estimation (nearest neighbor number) is used The Gaussian kernel bandwidth is set to the median distance between all local embedding vector pairs (in this embodiment, the median is assumed to be 0.32). Local density is calculated. .

[0134] Construct a spatial gradient accumulation curve with density ≤ The number of features is used to find the inflection point of the curve and determine the optimal sampling rate. This embodiment assumes that the sampling rate corresponding to the inflection point is 1.2%, meaning random sampling is performed from the distribution filling layer, with a sampling probability of... Proportional (prioritize retaining rare modes) (5) Consistency residual feedback elimination For each locally embedded vector retained by the sampling, compute its inconsistency residual with the maximum associated prototype. Preset threshold =0.3, remove The local embedding vector.

[0135] (6) Final memory and inverted index After the above screening and elimination process, the final number of entries in the memory is assumed to be 4,200 (the exact number depends on the sampling and elimination results). An inverted index is also built: each local embedding vector belongs to the prototype vector that maximizes the cosine similarity, forming 100 subsets. Where (k=1…100), only the relevant subset needs to be activated during inference.

[0136] 4. Learnable prototype comparison and training (1) The normalized global embedding vectors of 960 normal samples in the training set are used as the positive sample set, and the normalized global embedding vectors of 40 abnormal samples (with only image-level labels provided) are used as the negative sample set. All global embedding vectors are 64-dimensional and have been L2 normalized.

[0137] (2) Set the total number of global prototypes Set the total number of global prototypes One hundred 64-dimensional vectors were generated using random initialization and then L2 normalized to distribute them on a unit hypersphere. These 100 prototype vectors serve as learnable network parameters and participate in subsequent backpropagation optimization.

[0138] (3) Comparative loss calculation: Attraction term calculation: A temperature-scaled cross-entropy loss is employed. For each normal sample's global embedding vector, its cosine similarity to all prototype vectors is calculated. Temperature parameters are set. The Softmax function is used to convert similarity into a probability assigned to each prototype vector, minimizing the negative log-likelihood so that the features of normal samples are closely attached to their best-matching prototype vector.

[0139] Introduce a prototype separation regularization term. Set an upper bound threshold for prototype separation. When the cosine similarity between any two different prototype vectors exceeds 0.3, a penalty term is generated, causing the 100 prototype vectors to be evenly distributed on the hypersphere, covering different normal semantic subspaces.

[0140] The total loss function is The weight coefficient of the exclusion term Used to balance cluster compactness and prototype separability.

[0141] (4) After 100 iterations, the prototype comparison loss It converges to approximately 0.009. At this point, the prototype vectors are uniformly distributed on the unit hypersphere, and the maximum cosine similarity between any two prototypes is less than 0.3.

[0142] 5. Dual-space joint optimization (1) KL divergence distribution alignment loss: For the local embedding map of each training image, based on the calculated cross-space consistency anchoring matrix, find the prototype index that is most relevant to each local embedding vector in the map. Introduce two parameter-independent learnable linear mapping matrices. and Using these two matrices, the local embedding vector and its corresponding best-matching prototype vector are projected onto a 10-dimensional probability space, and the probability distribution vectors are generated using the Softmax function. Calculate the KL divergence between the probability distributions of all local embedding vectors and their corresponding prototype vectors within the current training batch, and use the mean as the loss for that batch. .

[0143] (2) Global average feature alignment loss: For each training image, calculate the squared Euclidean distance between the spatial average features of its local embedding map and the global embedding vector, and average it over all images to obtain the result. This embodiment takes .

[0144] (3) The total loss is ,in , The total loss converged to 0.023.

[0145] (4) Consistent prior benchmark: After training, cross-spatial inconsistency measures were statistically analyzed using all normal samples (960 images). Calculate all mean and standard deviation This embodiment takes =0.042, =0.018, serving as the correction benchmark for the inference stage.

[0146] Reasoning stage 6. Dual-spatial feature extraction An image of the screw to be tested (256×256×3) is input into the trained structure-aware feature encoder to obtain a structure-aware feature map (64×64×64). This map is then split by the feature decoupling module. (1) Output normalized local embedding map (64×64×64), each position is a 64-dimensional unit vector.

[0147] (2) Output the normalized global embedding vector (64 dimensions).

[0148] 7. Calculation of anomaly scores for local and global branches (1) Calculate the global branch score. The cosine similarity was calculated with 100 prototype vectors, and the maximum value was taken. The maximum similarity was 0.623, which is the global semantic anomaly score. =1 0.623 = 0.377.

[0149] (2) Calculation of local branch scores, based on Based on prototype similarity, the three most similar prototype vectors are activated (let's assume prototypes 23, 47, and 71). A subset of the corresponding memory is retrieved using the inverted index and merged. The merged subset contains 1462 local embedding vectors (instead of traversing all approximately 4200). Within the activation subset, for each spatial location on the local embedding graph... Calculate the cosine distance between its local embedding vector and all local embedding vectors in the subset, and take the maximum distance as the pixel-level anomaly score at that location. The average value across all 64×64 locations is used to obtain the image-level local distribution anomaly score. , =0.712.

[0150] 8. Dynamic Fusion of Evidence Learning The evidence learning dynamic fusion module quantifies the cognitive uncertainty of each branch based on the abnormal scores output by the two branches, and then adaptively generates dynamic fusion weights to solve the problem that fixed weights cannot adapt to the detection needs of different defect types.

[0151] First, the anomaly scores of the local and global branches are converted into corresponding evidence quality parameters using an exponential function. The evidence value of the local branch is approximately 2.038, and the evidence value of the global branch is approximately 1.458. Then, the cognitive uncertainty of the two branches is calculated based on the evidence values. The uncertainty of the local branch is 0.329, and the uncertainty of the global branch is 0.407. The lower the uncertainty, the more reliable the current detection result of the branch.

[0152] Based on this, the evidence learning dynamic fusion module calculates the dynamic fusion weight according to the uncertainty of the two branches. Since the uncertainty of the local branch is lower and the credibility is higher, it obtains a fusion weight of 0.562, while the global branch obtains a fusion weight of 0.438. Finally, the anomaly scores of the two branches are weighted and summed according to their corresponding weights to obtain a preliminary fusion score of 0.565. This score integrates the anomaly information of local distribution features and global semantic features, and is more robust than the result of a single branch.

[0153] 9. Cross-space consistency constraint modification First, a distribution matching degree vector for all local locations in the image to be tested is constructed. This involves calculating the cosine similarity between the local embedding vector at each spatial location in the local embedding map and all prototype vectors, summing these to obtain the complete local distribution matching degree vector for the image. Simultaneously, using the anchoring matrix optimized during training, the above local distribution matching degree vector is mapped to a theoretical semantic matching degree vector. This theoretical value represents "what the global semantic response should theoretically look like if the local distribution features and global semantic features completely satisfy the consistency prior learned during training." Then, the theoretical semantic matching degree vector is compared with the actually observed global semantic matching degree vector; the difference between the two is the consistency residual. A larger residual indicates greater inconsistency between the two spatial features, and a higher likelihood of noise or false detection. The calculated consistency residual of the image under test is... =0.051, which is very close to the average of 0.042 of the inconsistency measure of all normal samples in the training set. This indicates that although the image was judged to be abnormal, its local distribution features and global semantic features still maintained good consistency, and it belongs to a typical real defect signal rather than environmental noise interference.

[0154] The second step of the correction is to exponentially correct the initial fusion score using the consistency residual. The correction strength is controlled by a hyperparameter, and with a correction strength of 2.0, the calculated correction coefficient is approximately 0.903 under the influence of a consistency residual of 0.051. This coefficient is close to 1, meaning that the initial fusion score of 0.565 is almost completely preserved, and the final unified anomaly score is approximately 0.510. Subsequently, a final judgment is made based on a pre-set image-level judgment threshold (0.30 in this embodiment) that is optimized based on the harmonic mean of precision and recall on the test set. Since 0.510 is significantly higher than this threshold, the judgment result that the image of the screw under test is an anomaly is output, thus completing the entire inference process from dual-space feature extraction to image-level anomaly judgment.

[0155] 10. Anomaly Detection (1) The pixel-level anomaly score map output by the local branch acceleration matching module is directly reused. The size is 64×64, and the value of each pixel is between 0 and 1. The larger the value, the higher the confidence that the local area belongs to the defect. The score map has obvious response in the thread defect area and the scratch area, but the resolution is low and the boundary is blurred.

[0156] (2) The 64×64 anomaly score map was restored to the original image resolution of 256×256 through bilinear interpolation to obtain an upsampled score map aligned with the input image space. This score map retains the approximate anomaly response intensity of each region, but the interpolation process only relies on spatial distance for smooth filling and does not consider cross-spatial consistency priors. Further correction is needed to suppress false responses caused by factors such as illumination and dust. (3) For each pixel in the upsampled score image, perform the following operations one by one: First, extract the local embedding vector corresponding to the pixel, and combine it with the subset of the activation memory saved during the training phase to construct a local distribution matching degree vector; then, using the part of the cross-space consistency anchoring matrix optimized during the training phase that corresponds to the activation subset, map the local distribution matching degree vector to a theoretical semantic matching degree vector. This vector represents the expected global semantic response pattern at this location under the premise of satisfying the consistency prior; at the same time, take the actual global semantic matching degree vector of the current image to be tested as the observation value, compare it with the theoretical value, and calculate the difference between the two as the pixel-level cross-space consistency residual of the pixel. The smaller the residual, the higher the probability that the location conforms to the consistency prior learned during the training phase and belongs to the real defect; the larger the residual, the more likely it is to originate from noise interference. Finally, use the residual to construct a correction coefficient to correct the upsampled score. In this embodiment, the residual of the real defect area is generally small, the correction coefficient is close to 1, and the defect response is completely preserved; the residual of the noise area is large, and the false response is significantly suppressed. The corrected result is a final anomalous thermal map with a size of 256×256.

[0157] (4) Defect Mask Generation and Location Output. A pixel-level judgment threshold of 0.40 is set, and the final anomaly heatmap is binarized to generate a defect mask. This mask accurately outlines the relevant defects. An example is given below. Figure 7 The four-panel comparison image shows, from left to right, the original image of the nail, the heat map, the binary mask image, and the overlay image. Figure 7 The original image shows a rust spot defect, specifically a small dark brown circular spot on the left edge of the nail head, a more obvious rust spot with a brownish-red hue on the right side of the center of the nail head, and an isolated dark rust spot on the lower right side of the shaft.

[0158] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention and are not intended to limit it. Although the present invention has been described in detail with reference to preferred embodiments, those skilled in the art should understand that modifications or equivalent substitutions can be made to the technical solutions of the present invention without departing from the spirit and scope of the present invention, and all such modifications or substitutions should be covered within the scope of the claims of the present invention.

Claims

1. A method for detecting weakly supervised defects in industrial devices based on a dual-space consistency anchoring matrix, characterized in that, This method includes a training phase and an inference phase; The training phase includes: 1) Extract structure-aware feature maps from the input image; 2) Map the structure-aware feature map to a unified embedding space, and decouple the structure-aware feature map into local distribution features and global semantic features through a decoupling mechanism; 3) Construct a prototype contrastive loss and generate a normalized prototype vector set through contrastive learning based on global semantic features; 4) Use local distribution features as the initial candidate memory; generate a cross-space consistency anchoring matrix; divide the initial candidate memory into an anchoring core layer and a distribution filling layer based on cosine similarity; perform consistency residual feedback elimination on the distribution filling layer; generate an inverted association index between the prototype vector and the corresponding memory subset based on the cross-space consistency anchoring matrix; 5) Calculate the KL divergence distribution alignment loss based on local distribution features, and construct the global average feature alignment loss based on local distribution features and global semantic features; combine the KL divergence distribution alignment loss, the global average feature alignment loss and the prototype comparison loss to perform joint optimization of local and global spaces, and generate a consistent prior benchmark; The reasoning stage includes: 6) Extract structure-aware feature maps from the image to be tested, and then split them into local embedding maps and global embedding vectors; 7) Select multiple prototype vectors similar to the global embedding vector, and use the inverted index generated during the training phase to retrieve the memory subset corresponding to the prototype vector; calculate the cosine distance between the local embedding vector at each spatial location on the local embedding graph and the memory entry vector in the memory subset to obtain the local distribution anomaly score; calculate the cosine similarity between the global embedding vector and all prototype vectors to obtain the global semantic anomaly score. 8) Based on evidence learning theory, the cognitive uncertainty of global and local branches is quantified, and the local distribution anomaly score and the global semantic anomaly score are adaptively weighted and fused according to the uncertainty. 9) Calculate the consistency residual using the cross-space consistency anchoring matrix, and correct the weighted fusion score based on the consistency residual.

2. The method according to claim 1, characterized in that, Decomposing the structure-aware feature map through a decoupling mechanism includes: projecting the structure-aware feature map into a unified embedding space through a bottleneck projector; Preserving the spatial dimension yields a local embedding map. Each spatial location in the local embedding map is defined as a local embedding vector. All local embedding vectors are L2 normalized, and all normalized local embedding vectors constitute local distribution features. Global average pooling is performed on the structure-aware feature map in the spatial dimension, and then the global embedding vector is obtained through another bottleneck projector. The global embedding vector is then L2 normalized. The normalized global embedding vectors corresponding to all samples constitute the global semantic features.

3. The method according to claim 2, characterized in that, Construct a prototype contrastive loss and, based on global semantic features, generate a normalized set of prototype vectors through contrastive learning, including: Random initialization P There are 1 prototype vector, and each prototype vector is L2 initialized; A prototype contrast loss is constructed, which includes an attraction term and a repulsion term. The attraction term is constructed based on the cosine similarity between each normalized global embedding vector and the prototype vector. The repulsion term is implemented using a prototype separation regularization loss, which penalizes cases where the cosine similarity between any two prototype vectors exceeds a preset upper bound. The prototype vectors are optimized by training with prototype contrastive loss, and a normalized set of discriminative prototype vectors is output.

4. The method according to claim 3, characterized in that, Use all normalized local embedding vectors in the local distribution features as the initial candidate memory bank; The initial candidate memory is divided into an anchoring core layer and a distribution filling layer based on cosine similarity, including: for each prototype vector, selecting the top vector with the highest cosine similarity. One normalized local embedding vector is assigned to the anchoring core layer, and the remaining normalized local embedding vectors in the local distribution features are assigned to the distribution filling layer. The consistency residual feedback removal process for the distributed filling layer includes: randomly sampling the distributed filling layer and retaining the sampled normalized local embedding vectors; calculating the inconsistency residual between each normalized local embedding vector retained in the distributed filling layer and the corresponding maximum associated prototype, and removing some normalized local embedding vectors based on a preset threshold; and merging the anchored core layer and the removed distributed filling layer to obtain a memory.

5. The method according to claim 4, characterized in that, The KL divergence distribution alignment loss is calculated based on local distribution characteristics as follows: for each normalized local embedding vector, the prototype vector most relevant to it is determined according to the cross-space consistency anchoring matrix; the normalized local embedding vector and the corresponding prototype vector are projected into probability distributions respectively through a learnable linear mapping matrix; the KL divergence between the two probability distributions is calculated as the KL divergence distribution alignment loss. The global average feature alignment loss is constructed based on local distribution features and global semantic features. This includes calculating the squared Euclidean distance between the spatial average features of the local embedding map of each image and its global embedding vector, which is used as the global average feature alignment loss. Generating a consistent prior benchmark involves using the mean and standard deviation of a measure of spatial inconsistency from all normal samples as a consistent prior benchmark.

6. The method according to claim 5, characterized in that, Calculating the local distribution anomaly score involves: calculating the cosine distance between the local embedding vector at each spatial location on the local embedding graph and the vector of each memory entry in the memory subset. In the formula, Spatial location in a localized embedding graph Pixel-level anomaly scores at the location To score the image-level local distribution anomalies of the image under test. For the semantically relevant activation memory subset corresponding to the current image to be tested, To activate a single normalized local embedding vector in a subset of the memory bank, The cosine similarity function for two normalized vectors takes values ​​in the range [-1, 1]. The location of the image to be tested in the local embedding graph space The normalized local embedding vector at that location, The height and width of the local embedded image; The global semantic anomaly score is calculated by: calculating the cosine similarity between the global embedding vector and all prototype vectors, and taking the maximum value to convert it into the global semantic anomaly score. In the formula, Image-level global semantic anomaly score; For the first A normalized prototype vector; This represents the number of prototype vectors. is the normalized global embedding vector of the image to be tested.

7. The method according to claim 6, characterized in that, The quantification of cognitive uncertainty in global and local branches based on evidence learning theory includes: converting local distribution anomaly scores and global semantic anomaly scores into evidence quality functions, and calculating their respective cognitive uncertainties based on the evidence quality functions.

8. The method according to claim 7, characterized in that, The consistency residual of the current image under test is calculated using the cross-spatial consistency anchoring matrix; A correction function is constructed based on the consistency residual to correct the weighted fusion score.

9. The method according to claim 8, characterized in that, The method also includes: based on spatial location in the local embedding graph The pixel-level anomaly score is used to restore the pixel-level anomaly score map to the size of the input image under test through bilinear interpolation upsampling; then, the upsampled score of each pixel in the anomaly score map upsampled to the size of the input image under test is corrected.

10. An industrial device weak supervision defect detection system for implementing the method according to any one of claims 1 to 9, characterized in that, The system includes: The structure-aware feature encoder module is used to extract structure-aware feature maps from the input image; The feature decoupling module is used to split the structure-aware feature map into local distribution features and global semantic features; A learnable prototype comparison module is used to generate prototype vectors based on global semantic features during the training phase; Anchor-guided hierarchical sparse memory module is used to generate a memory based on local distribution features during the training phase, while generating a cross-space consistent anchoring matrix, and generating an inverted index between the prototype vector and the corresponding memory subset based on the cross-space consistent anchoring matrix; A dual-space joint optimization module is used to generate a consistent prior benchmark during the training phase; The global branch prototype matching module is used to calculate the global semantic anomaly score during the inference phase; The local branch acceleration matching module is used to calculate local distribution anomaly scores during the inference phase. The evidence learning dynamic fusion module is used to weightedly fuse global semantic anomaly scores and local distribution anomaly scores; A cross-space consistency constraint module is used to correct the weighted fusion score; The defect-aware region mapping module is used to restore the anomaly score map to the size of the input image under test; and to correct the upsampled score of each pixel in the anomaly score map upsampled to the size of the input image under test.