Near proximity search-based few-sample visual defect detection method
By using IVF-PQ indexing and INT8 quantization feature compression, the inference efficiency and memory usage of few-sample visual defect detection are optimized, solving the problems of slow detection speed and large memory usage in existing technologies, and achieving efficient detection on edge devices.
Patent Information
- Application Number
- CN202510860861.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-25
- Publication Date
- 2025-10-31
AI Technical Summary
Existing few-sample visual anomaly detection methods are insufficient in terms of inference efficiency, memory usage, and indexing efficiency, making it difficult to meet the deployment requirements of real-time detection and resource-constrained devices.
We employ an approximate nearest neighbor search-based approach, utilizing IVF-PQ indexing, INT8 quantization, and feature pruning to optimize feature compression and dynamic parameter adjustment, thereby constructing an efficient visual defect detection system.
It significantly improves detection speed and reduces memory usage, enabling real-time detection on edge devices. Inference time is reduced from 100–200 ms to 30–50 ms, and memory usage is reduced from 1–2 GB to 200–500 MB.
Smart Images

Figure CN120876367A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of industrial intelligent technology, and more specifically, to a few-sample visual defect detection method based on approximate nearest neighbor search. Background Technology
[0002] With the continuous improvement of industrial intelligence and automation, visual anomaly detection technology plays a key role in production quality control, intelligent manufacturing, security monitoring and other fields. Traditional supervised detection methods rely on a large number of labeled anomaly samples, which are difficult to cover all possible defect types; while unsupervised or few-sample detection methods can discover unknown anomalies by building feature models on normal images, and have broad application prospects. Among the current mainstream few-sample visual anomaly detection algorithms, PatchCore [1] has achieved excellent image-level and pixel-level detection accuracy through deep feature extraction and memory retrieval, but its inference efficiency and memory overhead limit its deployment in edge devices and real-time scenarios.
[0003] Among known technologies, the core process of PatchCore[1] is as follows: First, feature extraction is performed. A pre-trained backbone network (such as ResNet-18 / 34) is used to extract multi-scale feature maps, and feature layers with high channel count and rich semantic information are selected. Next, patch feature aggregation is performed. The feature map is divided into N fixed-size patches, and the feature vector of each patch is expanded. Then, the memory bank is constructed. The patch features of normal samples are inserted into the kNN index structure for subsequent nearest neighbor retrieval. Next, the anomaly calculation is performed. For each patch feature of the test image, the distance metric is obtained through kNN retrieval to form an anomaly response map. Finally, post-processing and decision-making are performed. Gaussian filtering and multi-scale fusion are performed on the anomaly response map to generate the final anomaly heatmap and calculate the image-level score.
[0004] Despite its effectiveness, PatchCore [1] has the following shortcomings: 1. High inference latency: The inference time for a single image is usually between 100 and 200 ms, which is difficult to meet the requirements of real-time detection. 2. Large memory footprint: The memory bank built by normal samples usually occupies 1 to 2 GB of memory, which is not conducive to deployment on resource-constrained edge devices. 3. Indexing efficiency bottleneck: Traditional precise kNN (such as Faiss Flat index) takes a lot of time to retrieve in high-dimensional space and cannot make use of low-precision approximate retrieval. 4. Lack of dynamic adaptive capability: For different scenarios and sample sizes, the parameters need to be manually tuned and there is a lack of online optimization mechanism.
[0005] Therefore, there is an urgent need for a few-sample anomaly detection method based on approximate neighbor search, which can achieve high accuracy, low latency, low memory usage, and be suitable for applications with few samples and edge device deployments by optimizing the index structure, feature compression, and dynamic parameter adjustment. Summary of the Invention
[0006] Based on the aforementioned problems in the existing technology, the purpose of this application is to provide a few-sample visual defect detection method based on approximate neighbor search, so as to achieve the effect of few-sample anomaly detection.
[0007] The technical solution adopted by this application to solve its technical problem is: a few-sample visual defect detection method based on approximate nearest neighbor search, including an image acquisition module, a feature extraction module, a memory bank construction module, a feature compression module, an approximate nearest neighbor search module, and an anomaly detection and result output module; The image acquisition module is responsible for acquiring the image to be detected in real time, and the acquired image is automatically transmitted to the subsequent processing module; The feature extraction module is used to extract features from the acquired images. It uses a pre-trained deep neural network to extract multi-scale feature maps from the input images. The feature compression module is used to reduce the amount of feature data, thereby reducing memory and computational burden; The memory building module mainly runs during system initialization or model training and is used to build a feature memory for normal samples. The core component is the near nearest neighbor search module, which is responsible for quickly finding the normal sample features that are closest to the input features in the memory. The anomaly detection and result output module is used to convert the distance information obtained from the nearest neighbor search into anomaly scores and generate the final detection results.
[0008] Furthermore, this includes the following steps: S1. Feature extraction and compression; S2, Memory Construction and ANN Indexing; S3, Product Quantification (PQ) fine-grained search and distance approximation; S4. Detailed description of anomaly calculation.
[0009] Furthermore, in step S1, the specific steps are as follows: First, given the input image Based on pre-trained ResNet-18, for input images Feature extraction is performed, and feature maps output from layers 3 and 4 are selected. ,in These represent the image's channels, width, and height, respectively.
[0010] Next, regarding Perform channel pruning, retaining the front The most important channel, and meets the following conditions: ; in Indicates 1 normal form, A specified coefficient, the default value is 0.5.
[0011] Then, the pruned features are subjected to uniform symmetric INT8 quantization. The vector quantization formula is as follows: ; Obtain the total dimension of the compressed features (generally ), concatenated in order to form a feature vector ; Finally, after pruning and quantification, the overall compression ratio is calculated using the following formula to evaluate the compression effect: ; in, It is the number of channels before pruning. It is the number of channels after pruning; and These are the bit widths of each element before and after quantization (e.g., 32 and 8); for example, when , And the quantization changes from 32 bits to 8 bits, then ; Specifically, this can be achieved by comparing different pruning ratios. and quantization accuracy Below This allows us to quickly filter out several sets of candidate parameters that meet the memory / latency budget of edge devices. In other words, we can first determine the "maximum acceptable compression ratio". (Corresponding to minimum storage / fastest inference), then select those that make During deployment, the compression ratio can be dynamically adjusted based on the memory and detection accuracy of different devices to achieve a balance between speed and accuracy.
[0012] Furthermore, in step S2, the specific steps are as follows: First, extract all Patch vectors from the normal samples using the method described in step 1 above. , Build an IVF inverted index.
[0013] Then, through k-means in Train M coarse cluster centers , minimize: ; Finally, each Assigned to the nearest cluster Add to inverted list .
[0014] This step accelerates algorithm prediction by constructing a memory bank organization and inverted clustering, and using product quantization (PQ) fine-grained retrieval and distance approximation.
[0015] Furthermore, step S3 includes the following steps: S3.1 Subspace partitioning and codebook training; Let the dimension of the original feature vector be... PQ will Divided into equal parts There are three non-overlapping subspaces, each with a dimension of 1. Let the first... The coordinate index set corresponding to each subspace is Then for any vector , its first The block vector is denoted as ; The codebook is trained independently using the k-means algorithm in each subspace. The codebook for each subspace is ; pass ; Solve independently. In this way, through joint training, we obtain... code book; S3.2, Vector encoding; For each cluster vector in the memory bank In the Encode within each subspace and find the nearest codebook center index: ; Then vector The PQ code is represented as a code of length 1000. sequence of integers: ; Only need to store this Each index, without having to save the original real-valued vector, thus enabling each Storage capacity reduced to bits; S3.3, Query vector preprocessing and lookup table construction; For the input query vector Similarly, it is split into piece: ; Then, a lookup table (LUT) is pre-computed for each subspace: ; The table size is The computational cost is And it only needs to be built once; S3.4 Approximate distance calculation; For clusters Each database vector within Using its PQ code and a pre-computed lookup table, it can be directly approximated to its... Euclidean distance: ; Compared to accumulating distances dimension-by-dimensionally on the original real-valued vector, this method only requires With only one lookup table and addition operation, the time complexity is significantly reduced to [number]. / vector; S3.5. Combining coarse and fine search; Combining IVF coarse cluster retrieval, the complete ANN distance can be written as: ; Among them, the coarse clustering distance only needs to be in The computation is performed once at each center, with a complexity of [time]. PQ fine search for the first Each candidate vector needs to be ,common ; S3.6 Quantization error and accuracy guarantee; Let the deviation between the actual distance and the approximation be denoted as . ; Theoretically, PQ distributes the total error across all subspaces, and it can be proven that... ; When the dimensions of each subspace Moderate and When it is large enough, this upper bound is small, which can achieve significant speed and storage advantages while ensuring approximate accuracy; Retrieval complexity and storage overhead analysis: Query time complexity is... The memory storage capacity is .
[0016] pass , , With these configurations, the optimal compromise can be achieved between 30–50ms / image and 200–500MB of memory usage.
[0017] Furthermore, step S4 includes the following steps: S4.1, Patch-level exception response calculation; S4.2. Generation of pixel-level anomaly heatmaps.
[0018] 7. The method for detecting few-sample visual defects based on approximate nearest neighbor search according to claim 6, characterized in that: step S4.1 includes the following steps: S4.1.1, Patch partitioning; The compressed feature map Based on whether it is a sliding window or a non-overlapping window, it is divided into: Each patch yields a patch feature set. ; The typical window size is Step size is ,but ; S4.1.2, Nearest Neighbor Distance Search; For each Patch feature Using the IVF-PQ index to retrieve its predecessor Find the nearest neighbors and obtain the distance set. ; in Indicates the first The first patch in memory A close neighbor.
[0019] S4.1.3, Patch-level anomaly degree; This Calculate the i-th distance by averaging or using a weighted combination. Anomaly score for each patch ; To enhance sensitivity to minimum distance, you can select... or In response.
[0020] Furthermore, step S4.2 includes the following steps: S4.2.1 Response Mapping; Patch-level scoring Based on their spatial location on the feature map, they are remapped back to the original map coordinates to form a preliminary anomaly heatmap. If there is overlap between patches, average or maximum fusion can be used: ; in To cover pixels The set of all Patch indexes; S4.2.2, Gaussian smoothing; To eliminate quantization noise and boundary artifacts, Perform two-dimensional Gaussian filtering: ; in ; ; S4.2.3, Multi-scale response fusion; To accommodate anomaly regions at different scales, multiple patches (e.g., different window sizes) can be extracted. (or different levels of features), to obtain smoothed heatmaps respectively. Then, perform weighted fusion: ; Weight It can be determined through cross-validation or dynamic parameter adjustment.
[0021] The beneficial effects of this invention are: This solution addresses the shortcomings of existing technologies, such as slow detection speed, large memory usage, and difficulty in adjusting compression parameters. Firstly, regarding speed, this invention employs IVF-PQ (Inverted File with Product Quantization) indexing for approximate nearest neighbor search, combined with INT8 quantization and pruning, significantly improving inference efficiency. The inference time per image is reduced from 100–200 ms in the traditional PatchCore method to 30–50 ms, adapting to real-time detection scenarios and achieving a 2–4 times improvement in inference speed. Secondly, regarding memory, through memory bank compression (PQ encoding) and feature channel pruning, the memory bank storage requirement for normal samples is reduced from 1–2 GB to 200–500 MB, resulting in a significant reduction in memory usage. This allows for efficient deployment and stable operation even on resource-constrained edge devices (such as Jetson NX and Raspberry Pi). Finally, regarding compression parameter adjustment, we calculate the dynamic compression ratio using the cropping rate and memory occupancy rate. Simple and effective quantization adjustments can improve compression performance, achieving superior detection results in various application scenarios (such as high-performance servers or edge devices). Attached Figure Description
[0022] The accompanying drawings, which form part of this specification, 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 undue limitation of the invention. In the drawings: Figure 1 This is an overall schematic diagram of a few-sample visual defect detection method based on approximate neighbor search in this application; Figure 2 This is a flowchart illustrating a few-sample visual defect detection method based on approximate nearest neighbor search in this application; Figure 3 Image of the rolled edge defect; Figure 4 This is a mask image for the rolled edge defect. Figure 5 Heat map of hemming defects; Figure 6 This is a contour diagram of the hemming defect; Figure 7 The result of the test is shown in the figure. Detailed Implementation
[0023] It should be noted that, unless otherwise specified, the embodiments and features described in this application can be combined with each other. The present invention will now be described in detail with reference to the accompanying drawings and embodiments.
[0024] It should be noted that, unless otherwise specified, all technical and scientific terms used in this application have the same meaning as commonly understood by one of ordinary skill in the art to which this application pertains.
[0025] In this invention, unless otherwise stated, the directional terms such as "up" and "down" generally refer to the directions shown in the accompanying drawings, or to the vertical, perpendicular, or gravitational direction; similarly, for ease of understanding and description, "left" and "right" generally refer to the left and right shown in the accompanying drawings; "inner" and "outer" refer to the inner and outer contours of each component itself, but the above directional terms are not intended to limit this invention.
[0026] like Figure 1-6 As shown, this application provides a few-sample visual defect detection method based on approximate nearest neighbor search, including an image acquisition module, a feature extraction module, a memory construction module, a feature compression module, an approximate nearest neighbor search module, and an anomaly detection and result output module.
[0027] The image acquisition module is responsible for acquiring images to be inspected in real time, typically using imaging devices such as integrated industrial cameras or USB cameras. Image acquisition can be triggered by timed events (e.g., capturing one frame per second) or by external signals (e.g., photoelectric switches on a production line) to ensure a stable and reliable image source. The acquired images are automatically transmitted to subsequent processing modules.
[0028] The feature extraction module is used to extract features from the acquired images. It utilizes pre-trained deep neural networks (such as ResNet-18 and EfficientNet) to extract multi-scale feature maps from the input images. The images are first preprocessed (such as normalization and resizing) to ensure compatibility with the neural network input requirements. The feature extraction network extracts multiple layers of features from intermediate layers (such as the third and fourth layers), which preserve the spatial and semantic information of the image. The extracted high-dimensional features are then passed to the feature compression module.
[0029] The feature compression module is used to reduce the amount of feature data, thereby reducing memory and computational burden. Its main operating methods include the following: 1. Pruning: Prune channels based on the L1 norm of the features, removing channels with low information content and retaining only important channels.
[0030] 2. Quantization: INT8 quantization is used to convert floating-point features into 8-bit integers, thereby reducing memory usage.
[0031] 3. Block quantization: Divide the high-dimensional feature vector into multiple subspaces (e.g., 16 blocks), and quantize each subspace independently to facilitate subsequent fast approximate search.
[0032] The feature memory construction module primarily runs during system initialization or model training, and is used to build a feature memory for normal samples. The system first extracts features from normal samples and compresses them into low-dimensional feature vectors. These features are then clustered using an inverted index (IVF) structure, forming multiple coarse cluster centers. Product quantization (PQ) is further applied within each coarse cluster to compress the high-dimensional features into multiple subspaces, each corresponding to a codebook. The feature memory supports various parameter configurations (such as the number of clusters and the quantized codebook size), allowing for flexible adjustment based on device performance.
[0033] The core component is the Approximate Nearest Neighbor Search module, responsible for quickly finding the closest normal sample features to the input features in the memory. After feature extraction and compression of the input image, the system first determines its coarse cluster. Within the cluster, Product Quantization (PQ) is used to calculate the approximate distance between each feature and the features stored in the memory. The number of retrieved nearest neighbors is configurable (e.g., 3 or 5), and the system returns these neighbors and their distances. This module employs efficient table lookup and distance calculation strategies, enabling large-scale sample retrieval within milliseconds.
[0034] The anomaly detection and output module converts distance information obtained from nearest neighbor search into anomaly scores and generates the final detection results. The system calculates patch-level anomalies based on the distance between each image patch feature and its nearest neighbor features; all patch anomalies are remapped to pixel-level heatmaps to form anomaly detection results. The system can also perform Gaussian smoothing and multi-scale fusion on the heatmaps to eliminate noise and improve detection accuracy. The final output includes: an image-level anomaly score (numerically representing the overall anomaly level) and a pixel-level anomaly heatmap (visualizing the anomaly areas).
[0035] A few-sample visual defect detection method based on approximate neighbor search includes the following steps: S1. Feature extraction and compression; S2, Memory Construction and ANN Indexing; S3, Product Quantification (PQ) fine-grained search and distance approximation; S4. Detailed description of anomaly calculation In step S1, the specific steps are as follows: First, given the input image Based on pre-trained ResNet-18, for input images Feature extraction is performed, and feature maps output from layers 3 and 4 are selected. ,in These represent the image's channels, width, and height, respectively.
[0036] Next, regarding Perform channel pruning, retaining the front The most important channel, and meets the following conditions: ; in Indicates 1 normal form, A specified coefficient, the default value is 0.5.
[0037] Then, the pruned features are subjected to uniform symmetric INT8 quantization. The vector quantization formula is as follows: ; Obtain the total dimension of the compressed features (generally ), concatenated in order to form a feature vector .
[0038] Finally, after pruning and quantification, the overall compression ratio is calculated using the following formula to evaluate the compression effect: ; in, It is the number of channels before pruning. It is the number of channels after pruning; and These are the bit widths of each element before and after quantization (e.g., 32 and 8); for example, when , And the quantization changes from 32 bits to 8 bits, then ; Specifically, this can be achieved by comparing different pruning ratios. and quantization accuracy Below This allows us to quickly filter out several sets of candidate parameters that meet the memory / latency budget of edge devices. In other words, we can first determine the "maximum acceptable compression ratio". (Corresponding to minimum storage / fastest inference), then select those that make During deployment, the compression ratio can be dynamically adjusted based on the memory and detection accuracy of different devices to achieve a balance between speed and accuracy.
[0039] In step S2, the specific steps are as follows: First, extract all Patch vectors from the normal samples using the method described in step 1 above. , Build an IVF inverted index.
[0040] Then, through k-means in Train M coarse cluster centers , minimize: ; Finally, each Assigned to the nearest cluster Add to inverted list Finally, each Assigned to the nearest cluster Add to inverted list .
[0041] This step accelerates algorithm prediction by constructing a memory bank organization and inverted clustering, and using product quantization (PQ) fine-grained retrieval and distance approximation.
[0042] Step S3 includes the following steps: S3.1 Subspace partitioning and codebook training; Let the dimension of the original feature vector be... PQ will Divided into equal parts There are three non-overlapping subspaces, each with a dimension of 1. Let the first... The coordinate index set corresponding to each subspace is Then for any vector , its first The block vector is denoted as ; The codebook is trained independently using the k-means algorithm in each subspace. The codebook for each subspace is ; pass ; Solve independently. In this way, through joint training, we obtain... Code book.
[0043] S3.2, Vector Coding For each cluster vector in the memory bank In the Encode within each subspace and find the nearest codebook center index: ; Then vector The PQ code is represented as a code of length 1000. sequence of integers: ; Only need to store this Each index, without having to save the original real-valued vector, thus enabling each Storage capacity reduced to bits.
[0044] S3.3, Query vector preprocessing and lookup table construction; For the input query vector Similarly, it is split into piece: ; Then, a lookup table (LUT) is pre-computed for each subspace: ; The table size is The computational cost is And it only needs to be built once.
[0045] S3.4 Approximate distance calculation; For clusters Each database vector within Using its PQ code and a pre-computed lookup table, it can be directly approximated to its... Euclidean distance: ; Compared to accumulating distances dimension-by-dimensionally on the original real-valued vector, this method only requires With only one lookup table and addition operation, the time complexity is significantly reduced to [number]. / vector.
[0046] S3.5. Combining coarse and fine search; Combining IVF coarse cluster retrieval, the complete ANN distance can be written as: ; Among them, the coarse clustering distance only needs to be in The computation is performed once at each center, with a complexity of [time]. PQ fine search for the first Each candidate vector needs to be ,common .
[0047] S3.6 Quantization error and accuracy guarantee; Let the deviation between the actual distance and the approximation be denoted as . ; Theoretically, PQ distributes the total error across all subspaces, and it can be proven that... ; When the dimensions of each subspace Moderate and When large enough, this upper bound is small, allowing for significant speed and storage advantages while maintaining approximate accuracy.
[0048] Retrieval complexity and storage overhead analysis: Query time complexity is... The memory storage capacity is .
[0049] pass , , With these configurations, the optimal compromise can be achieved between 30–50ms / image and 200–500MB of memory usage.
[0050] This step enables more efficient storage and retrieval of vectors within each cluster. ProductQuantization (PQ) significantly compresses memory and accelerates distance calculation during queries by quantizing high-dimensional vectors in blocks.
[0051] Step S4 includes the following steps: S4.1, Calculation of catch-level exception response; S4.2. Generation of pixel-level anomaly heatmaps.
[0052] This step transforms the retrieved distance information into pixel-by-pixel anomaly responses, ultimately yielding image-level and pixel-level anomaly scores.
[0053] Step S4.1 includes the following steps: S4.1.1, Patch partitioning; The compressed feature map Based on whether it is a sliding window or a non-overlapping window, it is divided into: Each patch yields a patch feature set. ; The typical window size is Step size is ,but ; S4.1.2, Nearest Neighbor Distance Search; For each Patch feature Using the IVF-PQ index to retrieve its predecessor Find the nearest neighbors and obtain the distance set. ; in Indicates the first The first patch in memory A close neighbor.
[0054] S4.1.3, Patch-level anomaly degree; This Calculate the i-th distance by averaging or using a weighted combination. Anomaly score for each patch ; To enhance sensitivity to minimum distance, you can select... or In response.
[0055] Step S4.2 includes the following steps: S4.2.1 Response Mapping; Patch-level scoring Based on their spatial location on the feature map, they are remapped back to the original map coordinates to form a preliminary anomaly heatmap. If there is overlap between patches, average or maximum fusion can be used: ; in To cover pixels The collection of all Patch indexes.
[0056] S4.2.2, Gaussian Smoothing To eliminate quantization noise and boundary artifacts, Perform two-dimensional Gaussian filtering: ; in ; .
[0057] S4.2.3, Multi-scale response fusion; To accommodate anomaly regions at different scales, multiple patches (e.g., different window sizes) can be extracted. (or different levels of features), to obtain smoothed heatmaps respectively. Then, perform weighted fusion: ; Weight It can be determined through cross-validation or dynamic parameter adjustment.
[0058] This solution addresses the shortcomings of existing technologies, such as slow detection speed, large memory usage, and difficulty in adjusting compression parameters. Firstly, in terms of speed, this invention employs IVF-PQ (Inverted File with Product Quantization) indexing for approximate nearest neighbor search, combined with INT8 quantization and pruning, significantly improving inference efficiency. The inference time per image is reduced from 100–200ms in the traditional PatchCore method to 30–50ms, adapting to real-time detection scenarios and achieving a 2–4 times improvement in inference speed. Secondly, regarding memory, through memory bank compression (PQ encoding) and feature channel pruning, the memory bank storage requirement for normal samples is reduced from 1–2GB to 200–500MB, resulting in a significant reduction in memory usage. This allows for efficient deployment and stable operation even on resource-constrained edge devices (such as Jetson NX and Raspberry Pi). Finally, in terms of compression parameter adjustment, we calculate the dynamic compression ratio based on the cropping rate and memory occupancy rate. Simple and effective quantization adjustments can improve compression performance, achieving superior detection results in various application scenarios (such as high-performance servers or edge devices).
[0059] This document provides an example of edge defect detection: This example demonstrates the application of the present invention in the detection of edge defects in automotive parts roofs. Through six core modules, the system can automatically detect defects such as cracks, burrs, and deformations in the edge area of the roof on an assembly line.
[0060] 1. Image acquisition module A laser-guided 3D industrial camera is installed next to the production line, with the lens facing the rolled edge area of the top cover. Ensure the scanning height of the rolled edge area is approximately consistent, with a 5mm tolerance. A robotic arm grips each top cover, and the robot triggers the camera to scan and acquire images via I / O. The robot operates at a constant speed of 300mm / s, outputting segmented depth map images, which are then converted to grayscale images with a resolution of 1024×1024 pixels. The acquired images... Stored in a buffer for processing. To ensure stable acquisition each time, the acquisition frame rate is set to 2048fps (adjustable), with fixed exposure time and gain. The trigger mode is external trigger.
[0061] 2. Feature Extraction Module Images acquired After standardization preprocessing, the input size is adjusted to that of the deep learning model (e.g., 224×224), and multi-scale features are extracted using a pre-trained ResNet-18 network. The ResNet-18 structure consists of multiple convolutional layers, with the feature maps output from layers 3 and 4 preserved for anomaly detection. The feature extraction process can be represented as follows: ; Feature map and middle These represent the number of channels, The feature map space size.
[0062] 3. Feature Compression Module The feature compression module compresses high-dimensional feature maps into low-dimensional features, reducing memory and computational costs. First, for... and Perform channel pruning. Pruning is based on the L1 norm, selecting the channel with the highest information content. ; in Indicates the number of channels to be reserved (e.g.) ), The value is set to 0.5. The pruned features are then subjected to INT8 quantization. ; The quantized features are further compressed using Product Quantization (PQ), dividing the high-dimensional feature vector into... Subspaces (e.g.) Each subspace is quantized independently. The quantized codebook for each subspace is trained using k-means. ; Each compressed feature is represented as a PQ code: ; in Indicates the first The quantization index of each subspace. The dimensionality of the compressed features is significantly reduced (e.g., from 512 to 128).
[0063] 4. Memory construction module In the offline phase, 50 normal images of the top cover edge were acquired, their features were extracted and compressed, and a memory bank of normal samples was constructed. The compressed features of all normal samples were stored in the memory bank and indexed using an IVF-PQ structure. First, k-means clustering was used to construct... One coarse cluster: ; Features within each coarse cluster are further compressed using PQ encoding, and the memory storage structure is as follows: ,in This indicates the codebook size for each subspace. The compressed memory size is approximately 300MB, suitable for Jetson NX edge devices.
[0064] 5. Approximate nearest neighbor search module During the detection process, the system extracts compressed features from the image to be detected and performs an Approximate Nearest Neighbor (ANN) search in the memory. First, the cluster to which the test features belong is determined based on IVF coarse clustering: ; Within this cluster, the approximate distance to each candidate vector is calculated using a PQ lookup table: ; Test features and their nearest Nearest neighbor distance Used for subsequent anomaly calculation. This module only requires 30–50ms per inference, making it suitable for real-time detection.
[0065] 6. Anomaly Detection and Result Output Module The anomaly score of each patch feature is calculated using its distance to its nearest neighbors: ; All Patch anomalies Mapping to the original image coordinates generates an anomaly heatmap. To eliminate noise, the heatmap is Gaussian smoothed. ; in It is a two-dimensional Gaussian kernel function. This indicates the smoothness level. The final image-level anomaly level is determined based on the maximum value of the heatmap. ; like If the value exceeds a threshold (e.g., 0.75), the system determines that there is a defect in the top cover edge and issues an alarm through the HMI (Human Machine Interface) or industrial alarm system.
[0066] Anomaly detection effect Test dataset: The piping on the roof of a car body. The training dataset contains 10 images, and the outlier dataset contains 5 images.
[0067] Defect detection: The effect of abnormal defect detection is as follows Figure 3-5 As shown.
[0068] Detection speed and memory experiment Datasets: MVTecAD (15 sub-datasets, such as bottle and cable), BrainMRI (1024x1024 resolution).
[0069] Metrics: Image-level AUROC, Pixel-level AUROC, Inference Time (ms / image), Memory Usage (MB).
[0070] Hardware: NVIDIA GFRTX 3090 GPU, Jetson Nano (edge device).
[0071] Baseline: Original PatchCore (WideResNet50, FaissIndexFlatL2, 10% sampling).
[0072] Experiment: Detect the running results as follows Figure 6 As shown, the embodiments described above are merely some, not all, embodiments of the present invention. All other embodiments obtained by those skilled in the art based on the embodiments of the present invention without inventive effort should fall within the scope of protection of the present invention.
[0073] It should be noted that the terminology used herein is for the purpose of describing particular embodiments only and is not intended to limit the exemplary embodiments according to this application. As used herein, the singular form is intended to include the plural form as well, unless the context clearly indicates otherwise. Furthermore, it should be understood that when the terms "comprising" and / or "including" are used in this specification, they indicate the presence of features, steps, operations, devices, components, and / or combinations thereof.
[0074] It should be noted that the terms "first," "second," etc., used in the specification, claims, and accompanying drawings of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of this application described herein can be implemented in sequences other than those illustrated or described herein.
[0075] 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.
[0076] The above description is merely a preferred embodiment of the present invention. The scope of protection of the present invention is not limited to the above embodiments. All technical solutions falling within the scope of the present invention's concept are within the scope of protection of the present invention. It should be noted that for those skilled in the art, any improvements and modifications made without departing from the principles of the present invention should also be considered within the scope of protection of the present invention.
Claims
1. A few-sample visual defect detection method based on approximate nearest neighbor search, characterized in that: It includes an image acquisition module, a feature extraction module, a memory bank construction module, a feature compression module, an approximate nearest neighbor search module, and an anomaly detection and result output module; The image acquisition module is responsible for acquiring the image to be detected in real time, and the acquired image is automatically transmitted to the subsequent processing module; The feature extraction module is used to extract features from the acquired images. It uses a pre-trained deep neural network to extract multi-scale feature maps from the input images. The feature compression module is used to reduce the amount of feature data, thereby reducing memory and computational burden; The memory building module mainly runs during system initialization or model training and is used to build a feature memory for normal samples. The core component is the near nearest neighbor search module, which is responsible for quickly finding the normal sample features that are closest to the input features in the memory. The anomaly detection and result output module is used to convert the distance information obtained from the nearest neighbor search into anomaly scores and generate the final detection results.
2. The method for detecting few-sample visual defects based on approximate nearest neighbor search according to claim 1, characterized in that: Includes the following steps: S1. Feature extraction and compression; S2, Memory Construction and ANN Indexing; S3, Product Quantification (PQ) fine-grained search and distance approximation; S4. Detailed description of anomaly calculation.
3. The method for detecting few-sample visual defects based on approximate nearest neighbor search according to claim 2, characterized in that: In step S1, the specific steps are as follows: First, given the input image Based on pre-trained ResNet-18, for input images Feature extraction is performed, and feature maps output from layers 3 and 4 are selected. ,in These represent the image's channels, width, and height, respectively. Next, regarding Perform channel pruning, retaining the front The most important channel, and meets the following conditions: ; in Indicates 1 normal form, A specified coefficient, the default value is 0.5; Then, the pruned features are subjected to uniform symmetric INT8 quantization. The vector quantization formula is as follows: ; Obtain the total dimension of the compressed features (generally ), concatenated in order to form a feature vector ; Finally, after pruning and quantification, the overall compression ratio is calculated using the following formula to evaluate the compression effect: ; in, It is the number of channels before pruning. It is the number of channels after pruning; and These are the bit widths of each element before and after quantization (e.g., 32 and 8); for example, when , And the quantization changes from 32 bits to 8 bits, then ; Specifically, this can be achieved by comparing different pruning ratios. and quantization accuracy Below This allows us to quickly filter out several sets of candidate parameters that meet the memory / latency budget of edge devices. In other words, we can first determine the "maximum acceptable compression rate". (Corresponding to minimum storage / fastest inference), then select those that make During deployment, the compression ratio can be dynamically adjusted based on the memory and detection accuracy of different devices to achieve a balance between speed and accuracy.
4. The method for detecting few-sample visual defects based on approximate nearest neighbor search according to claim 2, characterized in that: In step S2, the specific steps are as follows: First, extract all Patch vectors from the normal samples using the method described in step 1 above. , Build an IVF inverted index; Then, through k-means in Train M coarse cluster centers , minimize: ; Finally, each Assigned to the nearest cluster Add to the sort list ; This step accelerates algorithm prediction by constructing a memory bank organization and inverted clustering, and using product quantization (PQ) fine-grained retrieval and distance approximation.
5. The method for detecting few-sample visual defects based on approximate nearest neighbor search according to claim 2, characterized in that: Step S3 includes the following steps: S3.1 Subspace partitioning and codebook training; Let the dimension of the original feature vector be... PQ will Divided into equal parts There are three non-overlapping subspaces, each with a dimension of 1. Let the first... The coordinate index set corresponding to each subspace is Then for any vector , its first The block vector is denoted as ; The codebook is trained independently using the k-means algorithm in each subspace. The codebook for each subspace is ; pass ; Solve independently. In this way, through joint training, we obtain... code book; S3.2, Vector encoding; For each cluster vector in the memory bank In the Encode within each subspace and find the nearest codebook center index: ; Then vector The PQ code is represented as a code of length 1000. sequence of integers: ; Only need to store this Each index, without having to save the original real-valued vector, thus enabling each Storage capacity reduced to bits; S3.3, Query vector preprocessing and lookup table construction; For the input query vector Similarly, it is split into piece: ; Then, a lookup table (LUT) is pre-computed for each subspace: ; The table size is The computational cost is And it only needs to be built once; S3.4 Approximate distance calculation; For clusters Each database vector within Using its PQ code and a pre-computed lookup table, it can be directly approximated to its... Euclidean distance: ; Compared to accumulating distances dimension-by-dimensionally on the original real-valued vector, this method only requires With only one lookup table and addition operation, the time complexity is significantly reduced to [number]. / vector; S3.
5. Combining coarse and fine search; Combining IVF coarse cluster retrieval, the complete ANN distance can be written as: ; Among them, the coarse clustering distance only needs to be in The computation is performed once at each center, with a complexity of [time]. PQ fine search for the first Each candidate vector needs to be ,common ; S3.6 Quantization error and accuracy guarantee; Let the deviation between the actual distance and the approximation be denoted as . ; Theoretically, PQ distributes the total error across all subspaces, and it can be proven that... ; When the dimensions of each subspace Moderate and When it is large enough, this upper bound is small, which can achieve significant speed and storage advantages while ensuring approximate accuracy; Retrieval complexity and storage overhead analysis: Query time complexity is... The memory storage capacity is ; pass , , With these configurations, the optimal compromise can be achieved between 30–50ms / image and 200–500MB of memory usage.
6. The method for detecting few-sample visual defects based on approximate nearest neighbor search according to claim 2, characterized in that: Step S4 includes the following steps: S4.1, Patch-level exception response calculation; S4.2, Pixel-level anomaly heatmap generation.
7. The method for detecting few-sample visual defects based on approximate nearest neighbor search according to claim 6, characterized in that: Step S4.1 includes the following steps: S4.1.1, Patch partitioning; The compressed feature map Based on whether it is a sliding window or a non-overlapping window, it is divided into: Each patch yields a patch feature set. ; The typical window size is Step size is ,but ; S4.1.2, Nearest Neighbor Distance Search; For each Patch feature Using the IVF-PQ index to retrieve its predecessor Find the nearest neighbors and obtain the distance set. ; in Indicates the first The first patch in memory A close neighbor; S4.1.3, Patch-level anomaly level; This Calculate the i-th distance by averaging or using a weighted combination. Anomaly score for each patch ; To enhance sensitivity to minimum distance, you can select... or In response.
8. The method for detecting few-sample visual defects based on approximate nearest neighbor search according to claim 7, characterized in that: Step S4.2 includes the following steps: S4.2.1 Response Mapping; Patch-level scoring Based on their spatial location on the feature map, they are remapped back to the original map coordinates to form a preliminary anomaly heatmap. If there is overlap between patches, average or maximum fusion can be used: ; in To cover pixels The set of all Patch indexes; S4.2.2, Gaussian smoothing; To eliminate quantization noise and boundary artifacts, Perform two-dimensional Gaussian filtering: ; in ; ; S4.2.3, Multi-scale response fusion; To accommodate anomaly regions at different scales, multiple patches (e.g., different window sizes) can be extracted. (or different levels of features), to obtain smoothed heatmaps respectively. Then, perform weighted fusion: ; Weight It can be determined through cross-validation or dynamic parameter adjustment.
Citation Information
Patent Citations
Method for compressing target detection neural network
CN113052264A
Image anomaly detection method based on core memory bank residual error
CN117079029A
Cited By
Multi-modal data approximate query method and system based on hybrid block chain
CN121560962A
A hybrid blockchain-based multi-modal data approximate query method and system
CN121560962B
Wood surface defect detection method based on multi-view coding and feature memory bank
CN121708008A
Visual anomaly detection method and system in industrial field, terminal and storage medium
CN121724903A