Hierarchical feature fusion and guided memory based few-shot anomaly detection method and system

By employing hierarchical feature fusion and guided memory methods, the accuracy and memory issues of cross-scale defect detection in high-resolution images are resolved, enabling rapid and accurate defect detection and pixel-level localization in industrial products. This approach is applicable to scenarios such as precision manufacturing and automotive parts.

CN122435366APending Publication Date: 2026-07-21SHANDONG UNIV
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
SHANDONG UNIV
Filing Date
2026-06-22
Publication Date
2026-07-21

AI Technical Summary

Technical Problem

Existing technologies suffer from low accuracy in cross-scale defect detection under high-resolution images, excessive memory consumption, and severe performance degradation in low-sample scenarios. They cannot balance memory consumption, low-sample adaptability, and accurate localization of defects across all scales.

Method used

A hierarchical feature fusion and guided memory approach is adopted. Multi-level features are extracted by freezing the pre-trained visual backbone network, spatial patch features are fused with equal weights, a dual-branch memory is constructed, cross-scale feature matching and normalization are performed, and pixel-level anomaly maps are generated.

Benefits of technology

It enables rapid, accurate, and low-cost surface defect detection and pixel-level positioning of industrial products, and is suitable for industrial scenarios such as precision manufacturing, electronic components, and automotive parts. It reduces memory consumption and improves detection stability and accuracy.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122435366A_ABST
    Figure CN122435366A_ABST
Patent Text Reader

Abstract

The present application belongs to the field of computer vision and industrial quality detection technology. A hierarchical feature fusion and guided memory few-shot anomaly detection method and system are proposed. The industrial images to be detected and a small amount of normal reference images are obtained, and multi-layer feature extraction is completed relying on the frozen pre-training backbone network to obtain global classification features and spatial patch features. The patch features are fused to form cross-scale fusion features, and the global classification features are used to greedily select representative samples to construct a dual-branch memory library. The test features and the library features are matched layer by layer to obtain the original anomaly distance, and the hierarchical normalized distance is generated by combining the statistical library normalization. The initial anomaly map is obtained by fusion, and the pixel positioning map and image anomaly score are output after optimization, which improves the defect detection accuracy in the few-shot scene.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the fields of computer vision and industrial quality inspection technology, specifically to a method and system for detecting few-sample anomalies by hierarchical feature fusion and guided memory. Background Technology

[0002] The statements in this section are merely background information related to the present invention and do not necessarily constitute prior art.

[0003] In the precision manufacturing industry, automated visual inspection solutions are becoming increasingly common in the product quality inspection process, with high-resolution imaging equipment becoming the mainstream hardware configuration for capturing minute surface damage. Unsupervised anomaly detection, which relies on normal samples to identify defects and eliminates the dependence on defect labeling, has become the mainstream technology for industrial surface quality inspection. With the implementation of flexible production line transformation and accelerated product iteration, the number of qualified product samples available for collection during the new product launch phase is scarce, making low-sample anomaly detection technology a key focus of industry research and development. Existing technologies generally rely on pre-trained networks for feature extraction and feature library modeling, and are widely used in surface defect screening for various types of workpieces, including electronic components and automotive parts.

[0004] Existing detection schemes rely solely on single-layer or fixed-ratio multi-layer features for computation, lacking a balanced cross-scale feature fusion mechanism. Furthermore, they often select samples at the patch dimension to build feature repositories, failing to leverage global image features to constrain sample selection logic and hindering the construction of a lightweight dual-branch storage architecture. This deficiency leads to high feature repository storage overhead, insufficient coverage of the normal feature space under limited sample conditions, a lack of hierarchical standardization logic for subsequent feature distances, and difficulty in suppressing cross-scale defect matching errors. Ultimately, these schemes fail to simultaneously meet the three core requirements of memory usage, limited sample adaptability, and accurate defect localization across all scales. Summary of the Invention

[0005] To address the problems of low accuracy, high memory consumption, and severe performance degradation in low-sample scenarios in existing technologies for cross-scale defect detection under high-resolution images, this invention provides a low-sample anomaly detection method and system based on hierarchical feature fusion and guided memory. This method enables rapid, accurate, and low-cost detection and pixel-level localization of surface defects in industrial products, and is suitable for automated surface quality inspection in industrial scenarios such as precision manufacturing, electronic components, and automotive parts.

[0006] To achieve the above objectives, the present invention adopts the following technical solution: In a first aspect, the present invention provides a few-sample anomaly detection method based on hierarchical feature fusion and guided memory.

[0007] A few-sample anomaly detection method based on hierarchical feature fusion and guided memory includes the following steps: The industrial test image and a small number of normal reference images are acquired. Multi-level feature extraction is performed on all images through a frozen pre-trained visual backbone network. The global classification features and spatial patch features corresponding to each layer are obtained. Multi-layer spatial patch features are fused using equal weight rules to generate cross-scale fused features. Representative reference samples are obtained by greedy coverage optimization based on global classification features. A dual-branch memory containing a reference feature library and a normal statistical library is constructed based on the filtering results. The cross-scale fusion features corresponding to the image under test are aligned and matched layer by layer with the features in the reference feature library to obtain the single-layer original anomaly distance. The single-layer original anomaly distance is normalized based on the normal statistical library to generate the hierarchical normalized distance. The multi-level hierarchical normalized distances are fused to generate the initial pixel anomaly map. After neighborhood optimization, the final pixel localization map and image-level anomaly score are output.

[0008] In one implementation of the first aspect of the present invention, multi-level feature extraction includes: The input image size is standardized, and the four preset output features of the backbone network are extracted by the hook grabbing mechanism. The output features of each layer are split and processed to separate the global classification features and spatial patch features corresponding to a single image. The global classification features are taken from the first data of the feature sequence of the deepest layer among the four layers.

[0009] In one implementation of the first aspect of the present invention, multi-layer spatial patch features are fused using an equal-weight rule to generate cross-scale fused features. Representative reference samples are obtained by greedy coverage optimization based on global classification features, including: The weights are evenly distributed according to the total number of participating levels. All participating levels are assigned the same weight coefficient. Based on the corresponding weights, the spatial patch features of each level are superimposed one by one to obtain the cross-scale fusion features. A feature set is constructed using the global classification features corresponding to all normal reference images. Samples are randomly selected to initialize the candidate set. The minimum Euclidean distance from all remaining samples to the candidate set is calculated iteratively. Each time, the sample with the largest distance value is selected and added to the candidate set until the number of samples in the candidate set matches the total number of available reference images. The resulting candidate set is the representative reference sample after screening.

[0010] In one implementation of the first aspect of the present invention, constructing a dual-branch memory includes: The spatial patch features corresponding to each layer of representative reference samples are stored in the reference feature library. Offline, distance calculations are performed using all normal sample features and features in the reference feature library. The mean, standard deviation, and quantiles of the distances at each layer are calculated, and the mean, standard deviation, and quantiles are stored in the normal statistics library.

[0011] In one implementation of the first aspect of the present invention, the cross-scale fusion features corresponding to the image under test are aligned and matched layer by layer with the features in the reference feature library to obtain the single-layer original anomaly distance, including: First, the single-level patch features of the test side and the same-level patch features of the reference side are flattened in dimension. Then, the cosine similarity between each patch feature of the test side and all features of the reference side is enumerated one by one. A fixed number of reference features with the highest similarity ranking are selected and the mean is calculated. The original anomaly distance of the single layer is obtained by subtracting the mean from one.

[0012] In one implementation of the first aspect of the present invention, the original anomaly distance of a single layer is normalized based on a normal statistical library to generate a hierarchical normalized distance, and the hierarchical normalized distances of multiple layers are fused to generate an initial pixel anomaly map, including: Retrieve the mean and standard deviation of the corresponding level from the normal statistical database, subtract the mean of the corresponding level from the original anomaly distance of the single level, and then divide the result by the standard deviation of the corresponding level. Compare the result with the zero value and retain the non-negative value as the level normalized distance. The normalized distances of each layer are upsampled and scaled to the original input image size. The normalized distances of all upsampled layers are then superimposed using the equal weight rule. The superimposed results form the initial pixel anomaly map.

[0013] In one implementation of the first aspect of the present invention, the domain optimization process includes a local consistency smoothing operation, comprising: The initial pixel anomaly map is subjected to mean filtering to obtain a smoothed feature map. The initial pixel anomaly map and the smoothed feature map are combined according to a fixed weighting ratio to obtain a smoothed intermediate anomaly map.

[0014] In one implementation of the first aspect of the present invention, the domain optimization processing includes a multi-view test enhancement operation, which includes: The original input image is rotated at four different angles to obtain multi-view images. The entire process from feature extraction to anomaly image generation is performed on each view image. The arithmetic mean of the anomaly images generated from each view is calculated after the inverse coordinates are restored. The mean result is used as the final pixel location map. The mean, standard deviation and quantile of the final pixel location map are extracted. The initial image score is obtained by weighting the images with a fixed ratio. Then, the image score is standardized using the image score statistical parameters stored in the normal statistics library to obtain the image-level anomaly score.

[0015] Secondly, the present invention provides a few-sample anomaly detection system based on hierarchical feature fusion and guided memory.

[0016] A few-shot anomaly detection system based on hierarchical feature fusion and guided memory includes: The feature extraction unit is configured to: acquire industrial test images and a small number of normal reference images, and perform multi-level feature extraction on all images through a frozen pre-trained visual backbone network, and decompose them to obtain the global classification features and spatial patch features corresponding to each layer. The memory construction unit is configured to: fuse multi-layer spatial patch features using equal weight rules to generate cross-scale fusion features; obtain representative reference samples by greedy coverage optimization based on global classification features; and construct a dual-branch memory containing a reference feature library and a normal statistical library based on the screening results. The anomaly detection unit is configured to: align and match the cross-scale fusion features corresponding to the image under test with the features in the reference feature library layer by layer to obtain the single-layer original anomaly distance; normalize the single-layer original anomaly distance based on the normal statistical library to generate the hierarchical normalized distance; fuse the multi-layer hierarchical normalized distances to generate the initial pixel anomaly map; and output the final pixel localization map and image-level anomaly score after neighborhood optimization.

[0017] Thirdly, the present invention provides a computer device, comprising: a processor and a computer-readable storage medium; A processor, adapted to execute computer programs; A computer-readable storage medium storing a computer program, which, when executed by a processor, implements the few-sample anomaly detection method based on hierarchical feature fusion and guided memory, which is the first aspect of the present invention.

[0018] Compared with the prior art, the beneficial effects of the present invention are: This invention innovatively proposes a few-sample anomaly detection method based on hierarchical feature fusion and guided memory, solving the problems of low accuracy, high memory consumption, and severe performance degradation in few-sample scenarios of existing technologies under high-resolution images. Specifically, firstly, it fuses spatial patch features extracted from multiple layers with equal weight, balancing the fusion of deep and shallow layer feature information, improving the cross-scale feature representation capability, and adapting to the full-specification defect recognition needs from minor scratches to large-size deformations. Secondly, it uses a global classification feature greedily to select representative samples to build a dual-branch memory library, replacing traditional patch-level storage with image-level sampling, simplifying feature storage volume, reducing hardware memory consumption, and allowing a small number of normal samples to completely cover the normal feature distribution, thus improving detection stability under extremely few-sample conditions. Finally, it uses a normal statistical library to complete hierarchical distance normalization, unifying the numerical distribution standard of features at each layer, and multi-layer normalized distance fusion to generate anomaly maps. Combined with subsequent optimization processing to suppress texture false alarms, it simultaneously achieves pixel-level accurate defect localization and image-level anomaly judgment while simplifying deployment resources, making it suitable for rapid changeover in industrial production lines and edge device deployment.

[0019] Advantages of additional aspects of the invention will be set forth in part in the description which follows, and in part will be obvious from the description, or may be learned by practice of the invention. Attached Figure Description

[0020] The accompanying drawings, which form part of this invention, are used to provide a further understanding of the invention. The illustrative embodiments of the invention and their descriptions are used to explain the invention and do not constitute an improper limitation of the invention.

[0021] Figure 1 A flowchart illustrating a few-sample anomaly detection method based on hierarchical feature fusion and guided memory, provided as an exemplary embodiment of the present invention; Figure 2 A multi-level cross-scale representation model structure diagram provided as an exemplary embodiment of the present invention; Figure 3 A global boot overlay memory model structure diagram provided as an exemplary embodiment of the present invention; Figure 4 A multi-granularity anomaly quantization model structure diagram provided as an exemplary embodiment of the present invention; Figure 5 A schematic diagram of a few-sample anomaly detection system based on hierarchical feature fusion and guided memory, provided as an exemplary embodiment of the present invention; Figure 6 A schematic diagram of a computer device provided for an exemplary embodiment of the present invention. Detailed Implementation

[0022] The present invention will be further described below with reference to the accompanying drawings and embodiments.

[0023] It should be noted that the following detailed descriptions are exemplary and intended to provide further illustration of the invention. Unless otherwise specified, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this invention pertains.

[0024] In modern precision manufacturing, micron-level defects on product surfaces (such as microscratches, microcracks, and microstructural damage) directly affect product performance and reliability, necessitating online quality inspection using high-resolution imaging (e.g., 4K and above). Unsupervised image anomaly detection, which identifies unknown anomalies without defect labeling, has become one of the core technologies in industrial quality inspection. Existing unsupervised anomaly detection methods are mainly divided into two categories: reconstruction-based methods reconstruct normal images using trained models, identifying anomalies with high reconstruction error; and feature embedding-based methods extract features from normal samples using pre-trained encoders, and determine the degree of deviation of test samples using distance metrics (e.g., PaDiM models the feature distribution of each patch location using a multivariate Gaussian distribution, and uses Mahalanobis distance for anomaly detection). However, most of these methods are trained and tested on low-resolution images, rely on a large number of normal samples, and are difficult to directly extend to high-resolution scenes.

[0025] To address the scarcity of normal samples in industrial settings, few-shot anomaly detection (FSAD) technology has emerged, requiring only a small number of normal samples (1-4 images) for model deployment. Typical methods, such as PatchCore, employ a greedy core set sampling strategy to construct a feature memory library of the most representative normal patches, preserving key diversity information with limited data. RegAD proposed the first general FSAD framework, enabling cross-class anomaly detection. However, these methods still rely on local feature distribution modeling, lacking global semantic constraints, and are prone to misclassifying normal textures as anomalies. This deficiency is further amplified in high-resolution scenes. In the field of high-resolution anomaly detection, existing solutions mainly follow two strategies: one is global downsampling to conventional resolution, which inevitably loses fine-grained discriminative information, leading to an unacceptably high false negative rate for micro-defects; the other is segmenting the image into independent patches for separate processing, which preserves local details but destroys global contextual dependencies, causing frequent false positives in normal texture areas. While recent research has attempted to reduce computational complexity through lightweight networks (such as knowledge distillation) or patch aggregation strategies, problems such as high inference overhead and difficulty in adapting to rapid production line changes remain.

[0026] In summary, existing technologies still have the following core bottlenecks in high-resolution, low-sample industrial anomaly detection: (1) Insufficient cross-scale defect adaptability. Existing methods rely on multi-layer features with fixed single-layer or static weights, which cannot simultaneously capture shallow fine-grained textures and deep global semantics, resulting in both micron-level micro-defects being missed and centimeter-level structural anomalies being misjudged. Common post-processing methods (such as Gaussian smoothing) cannot solve the positioning error caused by fixed-layer features, and the false alarm rate is difficult to meet the requirements of real-time industrial detection; (2) Excessive memory overhead and low-sample performance degradation. The number of features in high-resolution images increases quadratically with resolution. Storing all patch features leads to memory bank expansion and GPU resource exhaustion, which cannot support rapid production line changeover. At the same time, random reference sampling cannot fully cover the normal feature distribution when samples are scarce. The detection performance drops sharply under extremely low sample (1-shot) conditions, and the introduction of additional fine-tuning further increases deployment costs and cycle.

[0027] In view of the various problems existing in the existing schemes, this implementation proposes a few-shot anomaly detection method based on hierarchical feature fusion and guided memory. A high-resolution few-shot anomaly detection framework (HiRAD) based on a frozen pre-trained model is designed. No model fine-tuning is required throughout the process. Only a small number of normal reference samples (1-4 images) are needed to achieve accurate detection and pixel-level localization of industrial defects across scales. HiRAD consists of three collaborative modules: (1) Hierarchical cross-scale representation module, which uses the frozen DINOv2 ViT-L / 14 backbone network to extract multi-level features and constructs a cross-scale feature representation that takes into account both texture details and structural semantics through equal weight fusion; (2) Global guided coverage memory module, which uses the global classification token (CLS token) as a guide and constructs a compact and comprehensive normal pattern reference library through coverage optimization iterative selection strategy. At the same time, the reference features are kept on the CPU to reduce GPU memory usage; (3) Multi-granularity anomaly quantization module, which generates pixel-level anomaly localization map and image-level anomaly detection score through hierarchical feature alignment, nearest neighbor matching and multi-granularity score fusion. The detection robustness is further improved through local consistency smoothing and multi-view testing enhancement.

[0028] The specific process of the training phase is as follows: Figure 1 As shown, after inputting an industrial image, hierarchical cross-scale representation processing is first performed to extract and fuse features from the 6th, 12th, 18th, and 24th layers of the network, while preserving fine-grained texture and global semantic information. Then, the global guided overlay memory stage is entered, and a compact and comprehensive normal feature memory is constructed based on greedy sampling of global classification labels. Subsequently, multi-granularity anomaly quantization is performed to calculate patch-level feature differences, and multi-granularity statistics are fused to generate pixel-level anomaly maps and image-level anomaly scores. Finally, the defect detection and localization results are output.

[0029] In this implementation, the hierarchical cross-scale representation module extracts and fuses multi-level features from the frozen DINOv2 backbone network, preserving both shallow texture details and deep semantic information, thus solving the challenge of cross-scale detection of industrial defects. Existing anomaly detection methods typically rely on single deep features or simple concatenation of multi-level features: single-level features inevitably lose key information at other scales, while simple concatenation cannot balance the contributions of different semantic levels. Industrial anomalies vary drastically across spatial scales; minute texture defects (such as scratches and cracks) rely on low-level visual cues, while structural anomalies (such as deformation and missing parts) rely on high-level semantic information. The hierarchical cross-scale representation module addresses this contradiction at its root. The basic structure of the hierarchical cross-scale representation module is as follows: Figure 2 As shown, specifically, an industrial image with a size of 518×518 is input into a pre-trained network after freezing for processing. The network is used to complete multi-layer feature extraction at a limited level. After feature extraction, two processing branches are set up. The left branch performs reshaping and equal-weight fusion operations on the obtained patch features to finally generate cross-scale fusion features. The right branch takes the global classification label corresponding to the 24th layer and generates a global image representation based on the label.

[0030] Multi-layer feature extraction employs the self-supervised pre-trained DINOv2 ViT-Large / 14 as the feature extractor. DINOv2 learns a general visual representation through self-distillation, adapting to diverse scenarios without requiring fine-tuning for specific industrial defects. Its ViT architecture's hierarchical structure naturally encodes cross-scale information: shallow layers (layer 6) preserve high-resolution texture details, suitable for detecting minor defects such as scratches and cracks; middle layers (layers 12 and 18) capture medium-granularity structural patterns; and deep layers (layer 24) encode global semantic context, suitable for detecting large structural anomalies such as deformation and missing parts. This covers the full-dimensional feature space from low-level texture to high-level semantics, providing a complete feature foundation for cross-scale defect detection. Specific implementation steps include: The input industrial image size is fixed at 518×518, strictly aligned with the 14×14 patch size of DINOv2 to avoid edge feature offset. The image is divided into... There are 14 patches, where P=14 is the size of the ViT patch.

[0031] The outputs of layers 6, 12, 18, and 24 of DINOv2 are extracted using a forward hook mechanism. For layer l, the output is... Where B is the batch size, D=1024 is the feature embedding dimension, the global CLStoken at the beginning of the sequence is encoded as an image-level representation, and the subsequent 1369 positions are spatial patch tokens.

[0032] Separate the global CLS token and spatial patch token for each layer, and reshape the patch token into a spatial feature map: (1); Where h=w=37 is the spatial resolution of the patch. Represents the coverage of the patch feature map. Represents the width of the patch feature map; Representing the The spatial patch feature tensor after dimensional reshaping of the layer; Representing the backbone network The original output feature data of the layer; Represents the number of image batches input in a single operation; The embedding dimension represents a single feature vector; It represents the set of real numbers.

[0033] Extract the CLS token from layer 24 as a global feature for subsequent memo construction: (2); in, This represents the global classification feature vector taken from layer 24; This represents the original output feature data of layer 24 of the backbone network.

[0034] In industrial quality inspection scenarios, the scale and type of defects in new products are completely unknown, and biased weight allocation will inevitably lead to missed detection of defects at specific scales. To strictly adhere to the few-sample unsupervised setting and completely avoid relying on defect information, task-specific fine-tuning, or additional data priors, this invention adopts a fixed equal-weight cross-layer feature fusion strategy—requiring no learnable parameters, no fine-tuning steps, and no additional computational overhead, perfectly aligning with the core objective of rapid industrial deployment.

[0035] For the selected layer set L={6,12,18,24}, assign equal weights to each layer, as follows: (3); Where L represents the number of selected layers; Represents any network layer within set L; Representing the The fusion weight values ​​corresponding to the layer features. This weight configuration ensures that each semantic level contributes equally to the final anomaly representation, achieving robust adaptation to defects at unknown scales without introducing additional computational overhead.

[0036] Few-shot anomaly detection faces a core dual challenge: it must comprehensively cover the normal feature distribution with a very small number of samples (1-4 images) while avoiding memory explosion caused by storing all features. Existing methods typically employ random reference sampling, which easily leads to insufficient feature aggregation and distribution coverage, resulting in severe performance degradation under few-shot settings. Conversely, storing all normal features directly causes memory explosion, making edge deployment infeasible and unable to meet the rapid changeover requirements of industrial production lines. The globally guided coverage memory module solves this contradiction at its root by implementing a coverage optimization sampling strategy guided by a global CLS token, achieving compact and comprehensive normal pattern modeling at the image level. The basic structure is as follows: Figure 3 As shown, the multi-layer patch feature set and the global classification label set are jointly sent to the coverage optimization selection stage. This stage selects a corresponding number of representative samples based on the criterion of maximizing the feature space distribution coverage. Based on the multi-layer patch feature set, the multi-layer patch features corresponding to the selected representative samples are selected to complete the construction of the reference feature library. The reference feature library is used to store the multi-layer patch features of the corresponding number of samples. Based on the selected representative samples, the corresponding mean, standard deviation and 99th percentile related parameters are pre-calculated offline. Based on the above parameters, a normal statistical library is built.

[0037] Unlike methods such as PatchCore, which perform core set sampling at the patch level, this invention proposes an image-level coverage optimization sampling strategy based on a global CLS token. PatchCore generates thousands of patch features for each high-resolution image, and core set selection requires iterative distance calculations across hundreds of thousands of features, resulting in enormous computational overhead and memory usage that increases linearly with the number of images. This invention uses the global CLS token output by the hierarchical feature extraction module as the image-level core representation, representing each image with only a single D=1024-dimensional vector, thus raising the sampling granularity from the patch level to the image level and fundamentally reducing the search space and storage requirements.

[0038] The optimization objective of the algorithm is to select K representative samples (K is the number of shots, usually 1-4) from N training normal samples, such that the selected subset maximizes the coverage of the feature space. Formally, let... Given a global CLS token set of all normal samples, the objective is to minimize the maximum minimum distance (i.e., coverage radius) from all samples to a selected subset S: (4); in, To provide a global set of CLS tokens for training normal samples, For the selected set of memory samples; Represents the feature space coverage radius corresponding to the selected sample set; A set representing the global classification features corresponding to all normal samples; Represents any global feature vector within set X; It represents any global feature vector within set S.

[0039] The objective is equivalent to "spreading out" the selected sample points in the feature space, distributing them as widely as possible to cover all normal patterns. The specific execution process is as follows: First, a sample is randomly selected to initialize the memory S; in each iteration, the minimum Euclidean distance from all unselected samples to each point in S is calculated, and the sample with the largest distance is added to S; this iteration is repeated until |S|=K. This greedy process ensures that each step maximizes the expansion of the current coverage by the added sample, approximating the optimal coverage under strict K-shot constraints. For 0-shot scenarios, this strategy automatically degenerates into full-sample modeling, maintaining consistency across all scenarios.

[0040] Compared to the patch-level core set method, this strategy reduces the number of samples that need to be stored in the memory bank from hundreds of thousands of patch features to features of K images (K≤4), reducing memory usage by about two orders of magnitude.

[0041] To fully adapt to the low memory footprint, rapid production changeover, and high-efficiency inference requirements of high-resolution industrial deployments, this invention independently constructs a dual-branch memory bank for each product category. This structure natively adapts to the aforementioned coverage-optimized sampling strategy, achieving a balance between detection accuracy, memory efficiency, and inference speed. The memory bank consists of two closely cooperating core components: Reference feature library: Multi-level patch features of K samples selected by the coverage optimization strategy at layers L={6,12,18,24}. ,in, , Unlike patch-based methods that require hundreds of thousands of patch features to reside on the GPU, the reference feature library of this invention only needs to store multi-layer features of K optimized images (K is typically 1-4), fundamentally reducing memory overhead by two orders of magnitude. All features are persistently stored in CPU memory by default and only loaded onto the GPU on demand during the inference phase, perfectly adapting to the rapid switching needs of multi-product industrial production lines and supporting deployment on edge devices with limited GPU memory.

[0042] Normal statistical library: During the library construction phase, the statistical distribution of distances between all training normal samples and the reference feature library is pre-calculated offline, including the mean distances at each layer. Standard deviation The 99th quantile is used to normalize the original anomaly distance in the subsequent anomaly quantification module, eliminating the inherent scale differences in feature distribution across different levels and ensuring a balanced contribution of each semantic layer to the final anomaly assessment. This aligns naturally with the equal-weighted cross-layer fusion strategy. The 99th quantile serves as the statistical threshold for normal distance, providing a robust benchmark for subsequent negative truncation and false alarm suppression. All statistics are calculated offline in a single operation, incurring zero overhead during inference. This avoids distribution shift issues caused by limited reference samples in low-sample scenarios, ensuring the statistical robustness of anomaly distance calculation.

[0043] After constructing a cross-scale feature representation and a compact normal pattern memory, the multi-granularity anomaly quantization module is responsible for quantizing the feature differences between the query image and the memory into interpretable anomaly signals. Through multi-granularity fusion and domain-specific optimization strategies, it simultaneously outputs pixel-level anomaly localization maps and image-level anomaly detection scores. Its specific structure is as follows: Figure 4 As shown, the query image's cross-scale features and dual-branch memory are jointly input into the hierarchical feature alignment step. The hierarchical feature alignment process generates a pixel-level anomaly map. This pixel-level anomaly map has two processing paths. The left path calculates an image-level anomaly score based on the pixel-level anomaly map and then outputs the image-level detection result from the image-level anomaly score. The right path sends the pixel-level anomaly map into a neighborhood-specific optimization step that includes local smoothing and multi-view enhancement operations. After neighborhood-specific optimization, a pixel-level anomaly localization map is obtained.

[0044] This invention is for querying images The l-th layer patch feature Compare it with the reference features of the corresponding layer in the memory bank. Flattened into vector form: (5); (6); Where Np=1369 is the number of patches per layer; K is the number of samples in the memory bank; The feature matrix represents the flattened feature matrix of the l-th layer patch feature of the image under test. Represents the image to be tested. Layer original space patch characteristics; This represents the total number of patches obtained from dividing a single image; The embedding dimension represents a single feature vector; Representative of the reference sample The feature matrix after the layer patch features are flattened; The first one stored in the reference feature library Layer original patch characteristics; This represents the total number of representative reference samples within the memory bank.

[0045] The flattening operation merges the spatial and batch dimensions into a unified vector pool for nearest neighbor search. The vector for each query patch is then computed. Cosine similarity with all memory vectors: (7); in, This indicates the calculation of cosine similarity. Represents the number to be tested The patch and the first Cosine similarity of reference features; Represents the side to be tested The feature vectors corresponding to each patch; Represents the first in the reference feature library The feature vectors are 10. The choice of cosine similarity is based on its robustness in high-dimensional feature space. Compared with Euclidean distance, cosine similarity is less sensitive to changes in feature amplitude and can more stably measure directional consistency, thus adapting to the semantic matching characteristics of DINOv2's high-dimensional feature space.

[0046] Select the k=3 nearest neighbors with the highest similarity, and calculate the patch-level anomaly distance using their average similarity: (8); in, Represents the number to be tested The patch is in the 1st The original anomaly distance of a single layer; This represents the number of nearest neighbor features selected for the mean calculation; Representatives selected the top similarity rankings Operations on numerical values.

[0047] use The multi-nearest neighbor voting mechanism effectively suppresses the interference of single-sample noise on anomaly scores and improves the robustness of patch-level distance calculation. A larger distance indicates a lower match between the patch and all normal patterns, and a higher probability of anomalies. Since the feature distributions of different layers in DINOv2 have inherent scale differences, directly fusing the original distances across layers can lead to an imbalance between shallow and deep layer contributions. Therefore, the layer mean values ​​pre-stored in the normal statistics library are utilized. with standard deviation z-score normalization is performed on the original anomaly distance: (9); Normalization eliminates inter-layer scale differences, ensuring a balanced contribution from each semantic layer in subsequent fusion, thus naturally matching the equal-weighted cross-layer fusion strategy. (Truncation operation) The rationale is that regions with a distance standard deviation less than that of normal samples do not pose anomaly risks, and setting negative values ​​to zero can effectively suppress false alarm signals in complex industrial texture backgrounds. The normalized patch distances are then reconstructed into a two-dimensional anomaly distance map based on spatial location. After bilinear interpolation upsampling to the original input resolution H×W, an initial pixel-level anomaly map is generated by equal-weight fusion: (10); in, This represents the initial pixel anomaly map generated after multi-layer feature fusion. The operation represents upsampling the feature map to the resolution of the original image; These represent the height and width of the original input industrial image, respectively.

[0048] While pixel-level anomaly maps provide pixel-by-pixel anomaly location information, actual industrial quality inspection requires simultaneous output of image-level judgment scores to support automated sorting decisions. To balance local extreme anomalies with global overall anomalies, this invention employs a weighted combination strategy to calculate image-level anomaly scores. First, three complementary statistics are extracted from the pixel-level anomaly map: (11); (12); (13); in, Focus on the most significant local anomalies (representing the average score of the region with the highest values ​​in the anomaly map). Capture extreme local defects (representing the maximum score of all pixels within the initial pixel anomaly map). Reflects overall global anomalies (represents the average score of all pixels in the entire initial pixel anomaly map); This represents the set of pixels that rank in the top one percent of the initial pixel anomaly map; This represents the calculation of the pixel average. The final image-level score. : (14); Weights are determined based on the priority of industrial defect risks, with the most significant anomalies receiving the highest attention, while also considering extreme defects and the overall background. The weight configuration is further optimized through a grid search (step size 0.1) on an independent validation set. This process does not involve touching the test data, eliminating any risk of data leakage. Finally, the mean and standard deviation of normal image scores pre-stored in the normal statistics library are used to... z-score standardization is performed to obtain the final image-level anomaly score, making the detection results of different product categories comparable.

[0049] To further improve the robustness and adaptability of anomaly quantification in real industrial scenarios, this invention designs two lightweight inference optimization strategies, both of which are executed during the inference phase without introducing additional training overhead.

[0050] Local consistency smoothing is performed, specifically addressing the issue that patch-level anomaly distance calculations introduce discontinuous fractional jumps and high-frequency quantization noise at patch boundaries, leading to decreased anomaly localization accuracy and increased false alarms. Therefore, local consistency smoothing is applied to the fused pixel-level anomaly map: first, a locally smoothed map is obtained through mean filtering. Then, the original image and the smoothed image are combined using a weighted combination to eliminate spatial discontinuities while preserving the salience of defect areas: (15); in, These are weighting coefficients. This is an anomaly graph after mean filtering; This represents the intermediate anomaly graph after smoothing.

[0051] Enhanced Four-View Testing: Industrial defect patterns often exhibit random orientations and postures, while patch-level feature matching and anomaly scoring processes are inherently sensitive to spatial orientation. To eliminate orientation dependence and improve generalization ability, this invention designs an enhancement strategy for four-view testing: The input image is rotated by 0°, 90°, 180°, and 270° respectively, and a complete anomaly quantization process is independently executed to obtain anomaly maps for each view. Then, all anomaly maps are aligned to the original image coordinate system through inverse geometric transformation, and the average value is taken to obtain the orientation-robust final anomaly map. (16); in, Indicates the first Inverse transformation of a view, For the first Anomalies in a single view; Representing the The inverse coordinate transformation operation corresponding to the four-view design. The four-view design takes advantage of the asymmetry of industrial defect distribution. During four-view testing, the enhancement strategy can be superimposed with the local consistency smoothing, and the two complement each other to jointly improve the reliability and robustness of anomaly quantification in the real production line environment.

[0052] In summary, the high-resolution few-shot anomaly detection framework (HiRAD) based on the frozen pre-trained model proposed in this invention has the following significant advantages: (1) Strong cross-scale defect detection capability. Through the equal weight fusion of multi-level features of DINOv2, it retains both shallow texture details and deep semantic information, effectively covering defects of all scales from micron-level scratches to centimeter-level structural anomalies, eliminating the problem of missed detection and misjudgment caused by scale bias from the root cause; (2) Extremely high memory efficiency and adaptable to edge deployment. Unlike the patch-level method which requires hundreds of thousands of features to be resident in the GPU, the image-level coverage optimization sampling based on CLS token in this invention only needs to store multi-level features of 1-4 representative samples, reducing memory overhead by about two orders of magnitude. All features are resident on the CPU and loaded onto the GPU on demand. It supports a single card to carry multiple categories of memory and rapid production changeover. It can be deployed on edge devices. (3) It has excellent performance under extremely few samples. Coverage optimization sampling aims to minimize the coverage radius. It selects the most dispersed samples in the feature space through greedy iteration. It can fully cover the normal distribution in 1-shot and the accuracy is close to the full sample method. The 0-shot scene automatically degenerates into full sample modeling to maintain the consistency of the whole scene. (4) It is fast to deploy without fine-tuning. It is based on frozen pre-trained weights throughout the process and does not require defective samples or additional data priors. New products only need a few normal samples to complete the deployment in a few seconds. The inference speed meets the requirements of industrial real-time detection. (5) It has high detection robustness. z-score normalization eliminates the difference in hierarchical scale. Negative value truncation and local smoothing suppress texture false alarms. It has reached the leading level on the three major benchmark datasets MVTec AD, MVTec AD2 and VisA and has outstanding industrial application value.

[0053] Figure 5 This paper presents a few-shot anomaly detection system based on hierarchical feature fusion and guided memory, including: The feature extraction unit 501 is configured to: acquire industrial test images and a small number of normal reference images, and perform multi-level feature extraction on all images through a frozen pre-trained visual backbone network, and decompose them to obtain the global classification features and spatial patch features corresponding to each layer. The memory construction unit 502 is configured to: fuse multi-layer spatial patch features using equal weight rules to generate cross-scale fusion features; obtain representative reference samples by greedy coverage optimization based on global classification features; and construct a dual-branch memory containing a reference feature library and a normal statistical library based on the screening results. The anomaly detection unit 503 is configured to: align and match the cross-scale fusion features corresponding to the image under test with the features in the reference feature library layer by layer to obtain the single-layer original anomaly distance; normalize the single-layer original anomaly distance based on the normal statistical library to generate the hierarchical normalized distance; fuse the multi-layer hierarchical normalized distances to generate the initial pixel anomaly map; and output the final pixel localization map and image-level anomaly score after neighborhood optimization processing.

[0054] It is understood that the aforementioned units can be individually or entirely merged into one or more other units, or some of the units can be further divided into multiple functionally smaller units. This achieves the same operation without affecting the technical effects of the embodiments of the present invention. The aforementioned units are based on logical functional division. In practical applications, the function of one unit can be implemented by multiple units, or the function of multiple units can be implemented by one unit. In other embodiments of the present invention, the system may also include other units. In practical applications, these functions can also be implemented with the assistance of other units, and can be implemented collaboratively by multiple units.

[0055] According to another embodiment of the present invention, the system of this embodiment can be constructed by running a computer program (including program code) capable of performing the steps involved in the corresponding method of the present invention on a general-purpose computing device, such as a computer, which includes processing elements and storage elements such as a central processing unit (CPU), random access memory (RAM), and read-only memory (ROM). The computer program can be recorded on, for example, a computer-readable recording medium, loaded into the aforementioned computing device through the computer-readable recording medium, and run therein.

[0056] Figure 6 A computer device is shown, which includes a processor 601, a communication interface 602, and a computer-readable storage medium 603. The processor 601, communication interface 602, and computer-readable storage medium 603 can be connected via a bus or other means.

[0057] The communication interface 602 is used to receive and send data. The computer-readable storage medium 603 can be stored in the memory of the electronic device. The computer-readable storage medium 603 is used to store computer programs, which include program instructions. The processor 601 is used to execute the program instructions stored in the computer-readable storage medium 603.

[0058] The processor 601 is the computing and control core of an electronic device. It is suitable for implementing one or more instructions, specifically for loading and executing one or more instructions to achieve the corresponding method flow or corresponding function.

[0059] Processor 601 is configured to perform the following procedure: The industrial test image and a small number of normal reference images are acquired. Multi-level feature extraction is performed on all images through a frozen pre-trained visual backbone network. The global classification features and spatial patch features corresponding to each layer are obtained. Multi-layer spatial patch features are fused using equal weight rules to generate cross-scale fused features. Representative reference samples are obtained by greedy coverage optimization based on global classification features. A dual-branch memory containing a reference feature library and a normal statistical library is constructed based on the filtering results. The cross-scale fusion features corresponding to the image under test are aligned and matched layer by layer with the features in the reference feature library to obtain the single-layer original anomaly distance. The single-layer original anomaly distance is normalized based on the normal statistical library to generate the hierarchical normalized distance. The multi-level hierarchical normalized distances are fused to generate the initial pixel anomaly map. After neighborhood optimization, the final pixel localization map and image-level anomaly score are output.

[0060] Those skilled in the art will recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed in this invention can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can implement the described functions using different methods for each specific application, but such implementations should not be considered beyond the scope of this invention.

[0061] In the above embodiments, implementation can be achieved, in whole or in part, through software, hardware, firmware, or any combination thereof. When implemented in software, it can be implemented, in whole or in part, as a computer program product. A computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, all or part of the flow or function according to the embodiments of the present invention is generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in or transmitted through a computer-readable storage medium. The computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via wired (e.g., coaxial cable, fiber optic cable, digital cable) or wireless (e.g., infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that a computer can access or a data processing device such as a server or data center that integrates one or more available media. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., solid-state drive), etc.

[0062] The above description is merely a preferred embodiment of the present invention and is not intended to limit the invention. Various modifications and variations can be made to the present invention by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.

Claims

1. A few-sample anomaly detection method based on hierarchical feature fusion and guided memory, characterized in that, The process includes the following: The industrial test image and a small number of normal reference images are acquired. Multi-level feature extraction is performed on all images through a frozen pre-trained visual backbone network. The global classification features and spatial patch features corresponding to each layer are obtained. Multi-layer spatial patch features are fused using equal weight rules to generate cross-scale fused features. Representative reference samples are obtained by greedy coverage optimization based on global classification features. A dual-branch memory containing a reference feature library and a normal statistical library is constructed based on the filtering results. The cross-scale fusion features corresponding to the image under test are aligned and matched layer by layer with the features in the reference feature library to obtain the single-layer original anomaly distance. The single-layer original anomaly distance is normalized based on the normal statistical library to generate the hierarchical normalized distance. The multi-level hierarchical normalized distances are fused to generate the initial pixel anomaly map. After neighborhood optimization, the final pixel localization map and image-level anomaly score are output.

2. The few-sample anomaly detection method based on hierarchical feature fusion and guided memory as described in claim 1, characterized in that, Multi-level feature extraction, including: The input image size is standardized, and the four preset output features of the backbone network are extracted by the hook grabbing mechanism. The output features of each layer are split and processed to separate the global classification features and spatial patch features corresponding to a single image. The global classification features are taken from the first data of the feature sequence of the deepest layer among the four layers.

3. The few-sample anomaly detection method based on hierarchical feature fusion and guided memory as described in claim 1, characterized in that, Multi-layer spatial patch features are fused using an equal-weight rule to generate cross-scale fused features. Representative reference samples are obtained by greedy coverage optimization based on global classification features, including: The weights are evenly distributed according to the total number of participating levels. All participating levels are assigned the same weight coefficient. Based on the corresponding weights, the spatial patch features of each level are superimposed one by one to obtain the cross-scale fusion features. A feature set is constructed using the global classification features corresponding to all normal reference images. Samples are randomly selected to initialize the candidate set. The minimum Euclidean distance from all remaining samples to the candidate set is calculated iteratively. Each time, the sample with the largest distance value is selected and added to the candidate set until the number of samples in the candidate set matches the total number of available reference images. The resulting candidate set is the representative reference sample after screening.

4. The few-sample anomaly detection method based on hierarchical feature fusion and guided memory as described in claim 1, characterized in that, Constructing a two-branch memory includes: The spatial patch features corresponding to each layer of representative reference samples are stored in the reference feature library. Offline, distance calculations are performed using all normal sample features and features in the reference feature library. The mean, standard deviation, and quantiles of the distances at each layer are calculated, and the mean, standard deviation, and quantiles are stored in the normal statistics library.

5. The few-sample anomaly detection method based on hierarchical feature fusion and guided memory as described in claim 1, characterized in that, The cross-scale fusion features corresponding to the image under test are aligned and matched layer by layer with the features in the reference feature library to obtain the original anomaly distance of a single layer, including: First, the single-level patch features of the test side and the same-level patch features of the reference side are flattened in dimension. Then, the cosine similarity between each patch feature of the test side and all features of the reference side is enumerated one by one. A fixed number of reference features with the highest similarity ranking are selected and the mean is calculated. The original anomaly distance of the single layer is obtained by subtracting the mean from one.

6. The few-sample anomaly detection method based on hierarchical feature fusion and guided memory as described in claim 1, characterized in that, Based on a normal statistical database, the original anomaly distances of a single layer are normalized to generate hierarchical normalized distances. These hierarchical normalized distances are then fused to generate an initial pixel anomaly map, including: Retrieve the mean and standard deviation of the corresponding level from the normal statistical database, subtract the mean of the corresponding level from the original anomaly distance of the single level, and then divide the result by the standard deviation of the corresponding level. Compare the result with the zero value and retain the non-negative value as the level normalized distance. The normalized distances of each layer are upsampled and scaled to the original input image size. The normalized distances of all upsampled layers are then superimposed using the equal weight rule. The superimposed results form the initial pixel anomaly map.

7. The few-sample anomaly detection method based on hierarchical feature fusion and guided memory as described in claim 1, characterized in that, Domain optimization processing includes local consistency smoothing operations, including: The initial pixel anomaly map is subjected to mean filtering to obtain a smoothed feature map. The initial pixel anomaly map and the smoothed feature map are combined according to a fixed weighting ratio to obtain a smoothed intermediate anomaly map.

8. The few-sample anomaly detection method based on hierarchical feature fusion and guided memory as described in claim 1, characterized in that, Domain optimization processing includes multi-view test enhancement operations, which include: The original input image is rotated at four different angles to obtain multi-view images. The entire process from feature extraction to anomaly image generation is performed on each view image. The arithmetic mean of the anomaly images generated from each view is calculated after the inverse coordinates are restored. The mean result is used as the final pixel location map. The mean, standard deviation and quantile of the final pixel location are extracted. The initial image score is obtained by weighting the values ​​with a fixed ratio. Then, the image score is standardized using the image score statistical parameters stored in the normal statistics library to obtain the image-level anomaly score.

9. A few-sample anomaly detection system based on hierarchical feature fusion and guided memory, characterized in that, include: The feature extraction unit is configured to: acquire industrial test images and a small number of normal reference images, and perform multi-level feature extraction on all images through a frozen pre-trained visual backbone network, and decompose them to obtain the global classification features and spatial patch features corresponding to each layer. The memory construction unit is configured to: fuse multi-layer spatial patch features using equal weight rules to generate cross-scale fusion features; obtain representative reference samples by greedy coverage optimization based on global classification features; and construct a dual-branch memory containing a reference feature library and a normal statistical library based on the screening results. The anomaly detection unit is configured to: align and match the cross-scale fusion features corresponding to the image under test with the features in the reference feature library layer by layer to obtain the single-layer original anomaly distance; normalize the single-layer original anomaly distance based on the normal statistical library to generate the hierarchical normalized distance; fuse the multi-layer hierarchical normalized distances to generate the initial pixel anomaly map; and output the final pixel localization map and image-level anomaly score after neighborhood optimization.

10. A computer device, characterized in that, include: Processor and computer-readable storage media; A processor, adapted to execute computer programs; A computer-readable storage medium storing a computer program, which, when executed by the processor, implements the few-sample anomaly detection method based on hierarchical feature fusion and guided memory as described in any one of claims 1 to 8.