Two-stage training based few-shot anomaly detection method
By employing a two-stage training method, combining geometric consistency loss and edge feature adaptation network, the feature sparsity problem in few-shot anomaly detection is solved, improving the model's accuracy and robustness in anomaly detection, especially its feature sensitivity near the decision boundary.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- HUNAN FIRST NORMAL UNIV
- Filing Date
- 2025-08-12
- Publication Date
- 2026-05-12
AI Technical Summary
Existing few-sample anomaly detection methods, when features are sparse, rely on simple global feature optimization strategies that ignore the differences in importance of different features for anomaly detection, resulting in insufficient sensitivity of the model to normal features at the edge and anomalous features near the decision boundary.
A two-stage training approach is adopted, consisting of a pre-training stage and a metric learning stage. In the pre-training stage, the encoder is fine-tuned using geometric consistency loss. In the metric learning stage, an edge feature adaptation network is constructed, and the feature representation is optimized using a memory construction module, a dual-path feature construction module, and an edge triplet loss.
It significantly improves the model's performance in anomaly detection, especially the sensitivity of features near the decision boundary, and enhances the accuracy and robustness of anomaly detection. Experimental results show that it improves the IAUROC performance by 1.2% and 2.4% on the MVTec and VisA datasets, respectively.
Smart Images

Figure CN121120504B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of few-sample anomaly detection technology, and in particular to a few-sample anomaly detection method based on two-stage training. Background Technology
[0002] Unsupervised anomaly detection (UAD) has achieved significant success in industrial quality inspection. By learning solely from defect-free samples, this method effectively addresses the problem of data imbalance and improves generalization ability to unseen anomaly types. However, obtaining a large number of normal samples in industrial environments still faces challenges such as access limitations or information confidentiality issues. Few-shot anomaly detection (FSAD) serves as an effective solution, achieving accurate defect detection and localization using only a small number of normal samples (e.g., 1 to 8 samples). Compared to UAD, FSAD exhibits significant advantages in training performance.
[0003] Anomaly detection methods based on memory banks are an effective User-Aware Detection (UAD) method. For example... Figure 1 As shown in (a), these methods first construct a memory containing a wide range of normal features. By measuring the distance between the query feature and its nearest neighbor in the memory, these methods calculate anomaly scores that enable high-precision image-level anomaly detection and pixel-level localization. However, as... Figure 1As shown in (b), in few-shot scenarios, the scarcity of normal samples leads to a sparse feature space. This phenomenon exacerbates the distance between samples, resulting in false negatives by misclassifying normal instances as anomalous instances. Furthermore, it blurs the decision boundary between normal and anomalous samples. Therefore, feature sparsity is a key issue in the implementation of FSAD. To address the feature sparsity problem, the literature [Y. Jiang, Y. Cao, and W. Shen, “Prototypical learningguided context-aware segmentation network for few-shot anomaly detection,” IEEE Trans. Neural Networks Learn. Syst., pp. 1–11, Oct. 2024.] proposes using the concept of a hypersphere to reduce the distance between normal features and the mean center, thereby constructing a more compact normal feature space. Subsequently, using the constructed adaptive features and the mean center, anomalies are detected through a segmentation network similar to FPN. The paper [A. Luo, G. Wen, Y. Cheng, S. Mei, H. Dong, and X. Liu, “Dmmgnet: A discrimination mapping and memory bank mean guidance-based network for high-performance few-sample industrial anomaly detection,” Neurocomputing, vol. 610, p. 128622, Dec. 2024.] proposes a mean-guided strategy. During the training phase, the global feature mean center guides feature optimization. During the inference phase, anomaly detection is performed based on the Mahalanobis distance between the test image and the mean center. These methods address feature sparsity by compressing the overall normal feature space. However, the simple global feature optimization strategy ignores the varying importance of different features for anomaly detection, leading to insufficient sensitivity of the model to normal features at edges and anomalous features near the decision boundary. Based on this, a few-sample anomaly detection method based on two-stage training is proposed. Summary of the Invention
[0004] The present invention aims to provide a few-sample anomaly detection method based on two-stage training to solve the problem that simple global feature optimization strategies ignore the differences in importance of different features for anomaly detection, resulting in insufficient sensitivity of the model to normal features at the edge and abnormal features near the decision boundary.
[0005] To achieve the above objectives, the present invention provides the following technical solution:
[0006] A few-shot anomaly detection method based on two-stage training includes the following steps:
[0007] S1. Pre-training phase: including online network and target network;
[0008] The online network includes a pre-trained encoder. Fully connected projection layer and complete prediction head A neural network composed of sequentially arranged components;
[0009] The target network includes a momentum update encoder. and projection layer A neural network composed of sequentially arranged components;
[0010] The online network and the target network are compared through geometric consistency loss L. GC Connection, the geometric consistency loss L GC Used for fine-tuning the pre-trained encoder;
[0011] S2, Metric Learning Stage: This includes an edge feature adaptation network, which comprises a memory bank construction module, a dual-path feature construction module, and an edge ternary loss.
[0012] The memory bank construction module first utilizes a finely tuned pre-trained encoder. Extract the original patch-level features, and then use an intermediate layer encoder. Construct a discriminative fine-grained memory;
[0013] The dual-path feature construction module first generates enhanced samples through basic geometric transformations. Then use the intermediate layer encoder Multi-scale features are extracted, and finally, local neighborhood aggregation is performed. Constructing a positive example feature set At the same time, Gaussian noise is directly added to the feature points of the enhanced samples. To obtain abnormal feature points at the feature level ;
[0014] The edge ternary loss first calculates all feature points of the enhanced image. and all abnormal feature points Feature points in memory The nearest Euclidean distance distribution is used, and then a positive loss is designed based on the normal distribution assumption. and negative losses The final dynamic ternary loss function is a weighted sum of the positive and negative losses;
[0015] S3. Anomaly Detection: The test image will directly obtain its corresponding patch-level feature points through a finely tuned pre-trained encoder and multilayer perceptron. The Euclidean distance from each point to the nearest point in the memory is calculated. The anomaly scores of all patch feature points constitute the pixel-level anomaly score map of the entire image. Bilinear interpolation is used to upsample the anomaly score map, and Gaussian filtering is used to smooth the results.
[0016] Furthermore, the feature extraction in the pre-training phase includes:
[0017] S101, Given a training image Applying two random geometric transformations to it, denoted as... and ;
[0018] S102, The pre-trained encoder then extracts the global feature embeddings. ;
[0019] S103. Next, through a projection layer with an activation function. Global features are embedded and mapped into a low-dimensional space. :
[0020]
[0021]
[0022] S104. The final output of the network's prediction head calculation:
[0023]
[0024] in, It is a fully connected layer, and ReLU is the activation function.
[0025] Furthermore, to prevent model collapse, the target network parameter η is updated using momentum. Given the momentum parameter τ, θ is updated as follows:
[0026]
[0027] in, These are the updated model parameters.
[0028] Furthermore, the geometric consistency loss L between the two random geometric transformation images GC for:
[0029]
[0030] Furthermore, the memory construction module extracts patch features by including the following steps:
[0031] S201. Divide the image into local regions of s×s.
[0032] S202, Aggregation through local domains Increase the receptive field to generate feature representations with high spatial discriminative power. .
[0033] S203. Finally, the memory bank M reduces the number of feature points in the memory bank using the core set algorithm:
[0034]
[0035] Furthermore, for each enhanced feature point Its distance from the nearest neighbor anchor point in the memory is defined as:
[0036]
[0037] For each abnormal feature point The distance to its nearest anchor point is:
[0038]
[0039] Furthermore, the mean of the nearest neighbor distances of the normally distributed feature points with standard deviation Calculated using the following formula:
[0040]
[0041]
[0042] in, Let N represent the nearest neighbor distance of the i-th feature point, and N be the total number of feature points.
[0043] Furthermore, the positive loss aims to narrow the distance between key normal feature points and the anchor points in the memory, only when the normal feature points... satisfy When, include in the loss function :
[0044]
[0045] The negative loss aims to increase the distance between outlier features and the memory anchor point, only if the outlier features... satisfy When, include in the loss function :
[0046]
[0047] Furthermore, the anomaly score of the test image is defined as the maximum value of the distances between all feature points, and its expression is:
[0048]
[0049] The principles and beneficial effects of this technical solution include at least the following:
[0050] 1) This proposal suggests a novel two-stage training network for few-shot anomaly detection. Through joint optimization of the pre-training and metric learning stages, this method effectively addresses the feature sparsity problem.
[0051] 2) This paper proposes a geometry-based contrastive learning network to enhance the adaptability of pre-trained models to geometric changes. This network aligns features with positional relationships in an image through geometric consistency loss.
[0052] 3) An edge feature adaptation network is proposed to generate significant discriminative decision boundaries. By considering the differences in importance of different features in anomaly detection, this network can amplify subtle differences between normal and anomalous features, especially those features close to the decision boundary.
[0053] 4) Extensive experiments were conducted on the MVtecAD and VisA datasets to validate the effectiveness of the proposed method. Experimental results show that, in two experimental settings, TSTNet improves the performance of the IAUROC metric by 1.2% and 2.4% respectively compared to the previous state-of-the-art models, significantly enhancing the performance of MVtecAD and VisA. Attached Figure Description
[0054] Figure 1 This is a schematic diagram illustrating feature sparsity in scenarios with few samples.
[0055] Figure 2 This is a schematic diagram of the overall process of a two-stage training network based on metric learning (TSTNet);
[0056] Figure 3 The relationship between TSTNet model performance and the number of MLP layers;
[0057] Figure 4 The t-SNE visualization results for test images of the MVTecAD dataset, where, Figure 4 (a) and Figure 4 (c) represents the feature distribution before training, while Figure 4 (b) and Figure 4 (d) represents the feature distribution after training;
[0058] Figure 5 These are outlier score images before and after training, where Figure 5(a) is the outlier score map before training. Figure 5 (b) is the abnormal score map after training. Detailed Implementation
[0059] To make the objectives, technical solutions, and advantages of this invention clearer, the invention will be further described in detail below with reference to the embodiments and accompanying drawings. Here, the illustrative embodiments and descriptions of this invention are used to explain the invention, but are not intended to limit the invention.
[0060] It should also be noted that, in order to avoid obscuring the invention with unnecessary details, only the processing steps closely related to the solution according to the invention are shown in the accompanying drawings, while other details that are not closely related to the invention are omitted.
[0061] It should be emphasized that the term "including / comprises" as used herein refers to the presence of a feature, element, or step, but does not exclude the presence or addition of one or more other features, elements, or steps.
[0062] It should be emphasized here that the step markers mentioned below are not a limitation on the order of the steps, but should be understood as meaning that the steps can be executed in the order mentioned in the embodiments, or in a different order than in the embodiments, or several steps can be executed simultaneously.
[0063] Example 1
[0064] refer to Figure 2 The proposed scheme is a two-stage training network based on metric learning (TSTNet) consisting of two complementary training stages: a pre-training stage and a metric learning stage.
[0065] In the pre-training phase, the geometry-based contrastive learning network fine-tunes the pre-trained encoder using a designed geometric consistency loss to adapt to geometric changes. In the metric learning phase, only the fine-tuned pre-trained encoder is retained, and a metric mapping module is introduced to construct an edge feature adaptation network. This network uses edge feature loss to form clear decision boundaries.
[0066] Specifically, the few-shot anomaly detection method based on two-stage training includes the following steps:
[0067] S1. Pre-training phase: including online network and target network;
[0068] This step employs a geometric contrastive learning framework, consisting of an online network and a target network, which learns by comparing two geometrically transformed versions of each training image. A geometric consistency loss is designed to connect the two networks, ensuring that the feature representation remains invariant under geometric transformations.
[0069] 1. Online network and target network
[0070] Online networks integrate pre-trained encoders Then comes the fully connected projection layer. and complete prediction head The target network mimics an online architecture and uses a momentum-updated encoder. and a projection layer However, it does not include a prediction layer.
[0071] Feature extraction during the pre-training phase includes:
[0072] S101, Given a training image Applying two random geometric transformations to it, denoted as... and ;
[0073] S102, The pre-trained encoder then extracts the global feature embeddings. ;
[0074] S103. Next, through a projection layer with an activation function. Global features are embedded and mapped into a low-dimensional space. :
[0075]
[0076]
[0077] S104. The final output of the network's prediction head calculation:
[0078]
[0079] in, It is a fully connected layer, and ReLU is the activation function.
[0080] 2. Geometrically consistent loss
[0081] During the pre-training phase, in order to enable the pre-trained encoder to learn common features from similar images, a geometric consistency loss L between two randomly geometrically transformed images is employed. GC :
[0082]
[0083] Furthermore, to prevent model collapse (e.g., outputting the same model), the target network parameter η is updated via momentum. Given the momentum parameter τ, θ is updated as follows:
[0084]
[0085] in, These are the updated model parameters.
[0086] S2, Metric Learning Stage: This includes an edge feature adaptation network, which comprises a memory bank construction module, a dual-path feature construction module, and an edge ternary loss.
[0087] To improve the performance of few-shot anomaly detection (FSAD), TSTNet constructed an edge feature adaptation network based on a geometric contrast learning network.
[0088] First, the pre-trained encoder based on the geometric contrastive learning network is retained. Next, the encoder is used to extract the original patch-level features, constructing a discriminative fine-grained memory that avoids representation degradation caused by traditional average aggregated feature prototypes. In addition, a lightweight MLP feature mapping module is integrated to form a new network—the edge feature adaptation network, which consists of three parts: a memory construction module, a dual-path feature construction module, and an edge ternary loss.
[0089] 1. Memory building module
[0090] fine-tuned encoder This mitigated the impact of covariate shift to some extent. To further reduce this impact, this study employed an intermediate layer encoder. To construct fine-grained features. Compared to deep features, which contain more abstract semantics biased towards the source domain, intermediate-layer features achieve an effective balance between capturing details and global information. This strategy has been validated in multiple studies.
[0091] This approach uses a patch feature construction paradigm, and the extraction of patch features includes the following steps:
[0092] S201. Divide the image into local regions of s×s.
[0093] S202, Aggregation through local domains Increase the receptive field to generate feature representations with high spatial discriminative power. .
[0094] S203. Finally, the memory bank M reduces the number of feature points in the memory bank using the core set algorithm:
[0095]
[0096] 2. Dual-path feature construction module
[0097] This study addresses the limitations of using original features as the guide during the construction of the memory bank by proposing a dual-path feature enhancement strategy.
[0098] First, enhanced samples are generated through basic geometric transformations (rotation / affine transformation / mirror). Using intermediate layer encoders Extract multi-scale features and perform local neighborhood aggregation operation. Constructing a positive example feature set This ensures the consistency between normal feature points and feature points in memory in the feature space.
[0099] The introduction of positive examples enables the model to learn feature maps of similar images during training, enhancing its robustness to positional changes. However, relying solely on positive samples to guide model training can lead to problems such as data imbalance and difficulty in learning anomalous features. To address this issue, this study introduces synthetic anomalous samples to generate negative samples in the ternary loss. Traditional image-level anomaly synthesis strategies have some inherent drawbacks, such as the risk of intra-class feature contamination in the patch feature space due to pseudo-anomaly regions generated by cropping-paste operations. To resolve this, this study directly adds Gaussian noise to the feature points of the enhanced samples. To obtain abnormal feature points at the feature level :
[0100]
[0101] 3. Marginal Triad Loss
[0102] In few-shot learning, the effectiveness of triplet loss depends on triples containing valuable information. During training, it's easy to select relatively simple triples, making it difficult to learn effective features, leading to training instability and slow convergence. To address this, this study proposes a dynamic triplet loss optimization strategy. Unlike traditional methods that force all normal features to converge to a single center, this scheme introduces a dynamic key feature point selection mechanism: applying distance constraints only to key feature points of the same type, while simultaneously applying separation constraints to anomalous features.
[0103] Specifically, this study calculates all feature points of the enhanced image. and all abnormal feature points Feature points in memory Nearest neighbor Euclidean distance distribution. For each enhanced feature point. Its distance from the nearest neighbor anchor point in the memory is defined as:
[0104]
[0105] For each abnormal feature point The distance to its nearest anchor point is:
[0106]
[0107] Assume these distances follow a normal distribution. , where the mean with standard deviation Calculated using the following formula:
[0108]
[0109]
[0110] in, Let N represent the nearest neighbor distance of the i-th feature point, and N be the total number of feature points.
[0111] Based on the normal distribution assumption, this study designed positive loss methods. and negative losses .
[0112] The positive loss aims to narrow the gap between key normal feature points and the memory anchor points, while avoiding overfitting. It only applies when normal feature points... satisfy When, include in the loss function :
[0113]
[0114] The negative loss aims to increase the distance between outlier features and the memory anchor point, only if the outlier features... satisfy When, include in the loss function :
[0115]
[0116] Ultimately, the dynamic ternary loss function is a weighted sum of the forward loss and the reproduction loss:
[0117]
[0118] and All of these are hyperparameters. It is used to balance the relative importance of the two types of loss, while m is used to control the distance between positive and negative samples.
[0119] S3, Anomaly Detection
[0120] During inference, the noise generation module in the pre-training and metric learning phases will be discarded, and the test image will directly obtain its corresponding patch-level feature points through the fine-tuned pre-trained encoder and multilayer perceptron. And calculate the Euclidean distance from each point to its nearest neighbor in memory. The anomaly score of the test image is defined as the maximum value of the distances between all feature points:
[0121]
[0122] The anomaly scores of all patch feature points constitute a pixel-level anomaly score map for the entire image. To restore the original image resolution, this study uses bilinear interpolation to upsample the anomaly score map and smooths the results using Gaussian filtering to eliminate local noise and enhance the continuity of the anomaly regions.
[0123] Those skilled in the art will understand that the exemplary components, systems, and methods described in conjunction with the embodiments disclosed herein can be implemented in hardware, software, or a combination of both. Whether implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this invention. When implemented in hardware, it can be, for example, electronic circuits, application-specific integrated circuits (ASICs), appropriate firmware, plug-ins, function cards, etc. When implemented in software, the elements of this invention are programs or code segments used to perform the desired tasks. The programs or code segments can be stored in a machine-readable medium or transmitted over a transmission medium or communication link via data signals carried in a carrier wave.
[0124] To fully demonstrate the effectiveness of this approach, a series of experiments were conducted on the most commonly used industrial anomaly detection datasets, MVTec and VISA. The MVTec dataset contains industrial images of 10 object types and 5 texture types. This dataset consists of a training set containing only normal images and a test set containing both normal and anomaly images. The training set contains 3629 defect-free images, and the test set contains 1258 defective images and 467 defect-free images of various defect types. VISA is a state-of-the-art industrial anomaly detection dataset, containing 12 types of industrial images, including 9621 defect-free images and 1200 defective images. Both datasets provide image-level and pixel-level labels, providing comprehensive support for performance evaluation. To evaluate the model's anomaly detection and localization performance, the area under the ROC curve at both the image and pixel levels was used as the evaluation metric.
[0125] For fair comparison, this study uses Wide_ResNet, the most commonly used network in industrial anomaly detection, as the backbone network. Images from both datasets were resized to 256×256 and standardized using the mean and standard deviation of the ImageNet dataset. The feature point dimension was set to 1024. Gaussian noise with a mean of 0 and a standard deviation of 0.1 was used to generate anomaly features. To accelerate inference, core set sampling was used to reduce the number of feature points, with a sampling rate of 0.1. The Adam optimizer was used in both the pre-training and metric learning phases, with the former trained for 3 epochs and the latter for 10 epochs, both with a learning rate of 0.001. All experiments were implemented on an Nvidia GeForce RTX 4070 GPU and an Intel i5-13600KF@3.50 GHz CPU.
[0126] Experimental Example 1
[0127] Comparative experiment
[0128] In actual production lines, systems often only need to know whether a product is abnormal without needing to know the location of the defect; therefore, image-level evaluation metrics are more important. Table 1 shows a performance comparison between the proposed TSTNet and state-of-the-art few-shot anomaly detection models (FSAD) in anomaly detection. As shown in Table 1, compared to other FSAD models, the proposed TSTNet achieves the best performance in anomaly detection. This is achieved through geometric feature alignment during the pre-training phase and metric learning phase.
[0129] Table 1. Performance comparison of state-of-the-art FSAD models for anomaly detection on the MVTec and VISA datasets.
[0130]
[0131] In Table 1, bold and underlined text represent the best and second-best results, respectively.
[0132] Specifically, the proposed TSTNet achieves performance improvements of 4.8%, 4.5%, and 2.5% over PCSNet in 2-sample, 4-sample, and 8-sample experiments on the MVTEC dataset, respectively, and improvements of 10%, 6.4%, and 5.1% over patchcore. Furthermore, TSTNet demonstrates excellent performance on the VISA dataset, achieving I-AUROC scores of 85.8%, 87.8%, and 89.0% in 1-sample, 2-sample, and 4-sample experiments, respectively. TSTNet significantly outperforms other memory-based and meta-learning-based FSAD methods, thanks to its effective mitigation of domain bias and selective optimization of the distance between key feature points and the memory. Moreover, TSTNet also surpasses methods based on language vision models.
[0133] This result is due to the fact that the geometric consistency loss function can effectively ensure that images with geometric relationships remain consistent in the feature space, and also to the fact that this scheme can effectively construct a more discriminative decision boundary.
[0134] Example 2
[0135] ablation experiment
[0136] To verify the effectiveness of each module in this scheme, a series of experiments were conducted on a 2-sample experimental setup of the MVTec dataset. The impact of various parameters on model performance was also verified.
[0137] Referring to Table 2, the geometry-based contrastive learning network uses L... GC The method reduces the distance between images at the image level, constructing a relatively compact feature space and achieving a 2.4% improvement in the I-AUROC metric. By optimizing the distance between enhanced images and memory features at the feature level, Lpos loss enhances adaptability to geometric changes, achieving a 4.4% I-AUROC gain. Meanwhile, L... neg The loss effectively generates fine-grained anomalous features, increasing the separation between memory features and anomalous features, thereby improving model performance on anomalous images by 5.9% I-AUROC. The ensemble marginal triplet loss L... MT By incorporating positive and negative pair loss, a feature space with a clear decision boundary is constructed, demonstrating an overall performance improvement of 8%. The I-AUROC value is improved by 7% by significantly enhancing the discrimination accuracy between normal and abnormal samples. Furthermore, a further 1.3% performance improvement in I-AUROC value is achieved through fine-tuning the pre-trained model.
[0138] Table 2 Ablation experiments of different modules under the experimental settings of MVTec dataset 2
[0139]
[0140] After verifying L MT After assessing its effectiveness, the potential advantages were further explored. However, the global feature optimization strategy cannot fully consider the differences in importance of different features in anomaly detection, especially those features that deviate from the normal distribution, which often play a decisive role.
[0141] To verify whether the edge feature mapping strategy is superior to the global feature optimization strategy, a global triplet loss (L...) is introduced. GT L is the edge triplet loss. MT An alternative. Referring to Table 3, the edge version outperforms the global version by 2.2% on I-AUROC. This result indicates that optimizing key features near the decision boundary is more effective than aligning all features.
[0142] Table 3 Global Triple Loss L GT With edge triplet loss L MT Comparison results
[0143]
[0144] This study systematically evaluates the impact mechanism of different negative sample generation strategies on the model's discriminative power. Referring to Table 4, the feature-level Gaussian noise anomaly synthesis strategy improves the AUROC score by 4.2 percentage points compared to the traditional image-level anomaly generation method NSA. This study suggests that the NSA method constructs pseudo-anomalies by transplanting local regions between normal samples. However, since the transplanted regions belong to the same normal distribution as the original image, most features remain within the normal feature clusters, with only a few boundary regions located outside the clusters. In contrast, the feature-level Gaussian noise anomaly synthesis strategy directly breaks through the normal distribution boundary through linear translation of the feature space, significantly increasing the minimum distance between the synthesized anomalous features and the normal prototype, thus significantly improving the model's sensitivity to capturing anomalous patterns.
[0145] Table 4 Performance Comparison of Image-Level and Feature-Level Models
[0146]
[0147] This study further reveals the dynamic balance between the depth of the feature mapping layer and the model's generalization ability. (Reference) Figure 3The detection performance of the model shows a significant negative correlation with network depth. Although deep mapping architectures possess stronger nonlinear representation capabilities, excessive layer stacking can trigger an information bottleneck effect, leading to irreversible decay of the geometric discriminative patterns of pre-trained features. This aligns with the theory of deep feature decoupling, proving that under conditions of few samples, shallow mappings can achieve optimal discriminative feature preservation through moderate nonlinear transformations, while parameter redundancy in deep structures can actually undermine the inherent representational advantages of pre-trained models.
[0148] To further evaluate the effectiveness of the proposed method in improving model performance, t-SNE was used to visualize the feature distribution on the MVTecAD dataset. (Refer to...) Figure 4 In the diagram, the blue dots, green dots, and red triangles represent memory features, normal features, and synthetic aberration features, respectively.
[0149] Visualization results show that after training, the normal samples of the memory features and their geometric transformations form more compact clusters in the embedding space. Meanwhile, the synthesized anomalous features are clearly distinguishable from the memory features. Furthermore, reference... Figure 5 This demonstrates the distribution of abnormal scores in the gridded subsets before and after training. This further indicates that the boundary between normal and abnormal features becomes clearer after training.
[0150] In summary, this paper presents a two-stage training network based on metric learning. This network, combining a pre-training stage and a metric learning stage, effectively addresses the domain bias and feature sparsity issues of anomaly detection methods in few-shot scenarios. This innovative approach provides new ideas and solutions for improving the accuracy and robustness of anomaly detection in few-shot scenarios.
[0151] In the pre-training stage, Euclidean distance loss is used to enable the pre-trained model to learn common features between similar images, while contrastive learning is employed to address the domain gap problem. In the metric learning stage, the pre-trained model, containing more spatial information, is used to construct fine-grained feature points, and a ternary loss is introduced to achieve clustering of normal feature points. This also increases the distance between abnormal and normal features, significantly improving the model's discriminative ability in anomaly detection tasks.
[0152] Experiments show that this collaborative optimization mechanism significantly enhances the detection performance of anomaly detection methods in few-shot scenarios and surpasses all current memory-based few-shot anomaly detection methods. It achieves the highest I-AUROC in almost all few-shot experimental settings on the MVTec and VISA datasets.
[0153] It should be clarified that the present invention is not limited to the specific configurations and processes described above and shown in the figures. For the sake of brevity, detailed descriptions of known methods are omitted here. In the above embodiments, several specific steps are described and shown as examples. However, the method process of the present invention is not limited to the specific steps described and shown. Those skilled in the art can make various changes, modifications, and additions, or change the order of steps, after understanding the spirit of the present invention.
[0154] In this invention, features described and / or illustrated for one embodiment may be used in the same or similar manner in one or more other embodiments, and / or combined with or in place of features of other embodiments.
[0155] The above description is merely a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, various modifications and variations of the embodiments of the present invention are possible. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the protection scope of the present invention.
Claims
1. A few-shot anomaly detection method based on two-stage training, characterized in that, Includes the following steps: S1. Pre-training phase: including online network and target network; The online network includes a pre-trained encoder. Fully connected projection layer and complete prediction head A neural network composed of sequentially arranged components; The target network includes a momentum update encoder. and projection layer A neural network composed of sequentially arranged components; The online network and the target network are compared through geometric consistency loss L. GC Connection, the geometric consistency loss L GC Used for fine-tuning the pre-trained encoder; S2, Metric Learning Stage: This includes an edge feature adaptation network, which comprises a memory bank construction module, a dual-path feature construction module, and an edge ternary loss. The memory bank construction module first uses a fine-tuned pre-trained encoder to extract the original patch-level features, and then uses an intermediate layer encoder to construct a discriminative fine-grained memory bank. The dual-path feature construction module first generates enhanced samples through basic geometric transformations. Then, multi-scale features are extracted using an intermediate encoder, and finally, local neighborhood aggregation is performed. Constructing a positive example feature set At the same time, Gaussian noise is directly added to the feature points of the enhanced samples. To obtain abnormal feature points at the feature level ; The edge ternary loss first calculates all feature points of the enhanced image. and all abnormal feature points Feature points in memory The nearest Euclidean distance distribution is used, and then a positive loss is designed based on the normal distribution assumption. and negative losses The final dynamic ternary loss function is a weighted sum of the positive and negative losses; S3. Anomaly Detection: The test image will directly obtain its corresponding patch-level feature points through a finely tuned pre-trained encoder and multilayer perceptron. The Euclidean distance from each point to the nearest point in the memory is calculated. The anomaly scores of all patch feature points constitute the pixel-level anomaly score map of the entire image. Bilinear interpolation is used to upsample the anomaly score map, and Gaussian filtering is used to smooth the results.
2. The few-sample anomaly detection method based on two-stage training according to claim 1, characterized in that, The feature extraction in the pre-training phase includes: S101, Given a training image Applying two random geometric transformations to it, denoted as... and ; S102, The pre-trained encoder then extracts the global feature embeddings. ; S103. Next, through a projection layer with an activation function. Global features are embedded and mapped into a low-dimensional space. : S104. The final output of the network's prediction head calculation: in, It is a fully connected layer, and ReLU is the activation function.
3. The few-sample anomaly detection method based on two-stage training according to claim 2, characterized in that, To prevent model collapse, the target network parameter θ is updated using momentum. Given the momentum parameter τ, θ is updated as follows: in, These are the updated model parameters.
4. The few-sample anomaly detection method based on two-stage training according to claim 3, characterized in that, The geometric consistency loss L between the two random geometric transformation images GC for: 。 5. The few-sample anomaly detection method based on two-stage training according to claim 1, characterized in that, The memory bank construction module extracts patch features through the following steps: S201. Divide the image into local regions of s×s; S202, Aggregation through local domains Increase the receptive field to generate feature representations with high spatial discriminative power. ; S203. Finally, the memory bank M reduces the number of feature points in the memory bank using the core set algorithm: 。 6. The few-shot anomaly detection method based on two-stage training according to claim 1, characterized in that, For each enhanced feature point Its distance from the nearest neighbor anchor point in the memory is defined as: For each abnormal feature point The distance to its nearest anchor point is: 。 7. The few-sample anomaly detection method based on two-stage training according to claim 1, characterized in that, The mean of the nearest neighbor distances of the feature points of the normal distribution with standard deviation Calculated using the following formula: in, Let N represent the nearest neighbor distance of the i-th feature point, and N be the total number of feature points.
8. The few-sample anomaly detection method based on two-stage training according to claim 7, characterized in that, The positive loss aims to narrow the distance between key normal feature points and the anchor points in the memory, only when the normal feature points... satisfy When, include in the loss function : The negative loss aims to increase the distance between outlier features and the memory anchor point, only if the outlier features... satisfy When, include in the loss function : Ultimately, the dynamic ternary loss function is a weighted sum of the forward loss and the reproduction loss: and All of these are hyperparameters. It is used to balance the relative importance of the two types of loss, while m is used to control the distance between positive and negative samples.
9. The few-sample anomaly detection method based on two-stage training according to claim 1, characterized in that, The anomaly score of the test image is defined as the maximum distance between all feature points, and its expression is: 。