Product anomaly detection method and device based on image position prior and reconstruction

CN122989742APending Publication Date: 2026-10-27ZHEJIANG SHUANGYUAN TECH CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202611477510.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-09-22
Publication Date
2026-10-27

AI Technical Summary

Technical Problem

[0005]本发明提供了一种基于图像位置先验和重建的产品异常检测方法及装置,以解决现有的基于图像异常检测方法计算量大的问题

Benefits of technology

(1)通过位置感知建模机制,按照空间位置对待检测的当前产品图像进行分块,每个图像块只需与相同空间位置或关联空间位置的正常先验特征进行比较,减少重复计算,基于比较结果再进行缺陷的确认,能够有效降低计算量,并且能够避免不同空间区域之间因纹理、亮度、背景结构差异造成的误判,提高检测精度;该机制特别适用于印刷、包装、纸张、膜材等空间结构稳定、分辨率高、局部纹理差异明显的工业大图检测场景;

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122989742A_ABST
    Figure CN122989742A_ABST
Patent Text Reader

Abstract

The application provides a product anomaly detection method and device based on image position priori and reconstruction, which comprises the following steps: a prior feature library containing multiple groups of features divided according to spatial positions is established in advance; current product images are collected and divided according to spatial positions to obtain multiple image blocks, and a unique spatial identifier is generated for each image block; a pre-trained feature extractor is used to extract multiple layers of features from the image blocks, and feature matching is performed according to the unique spatial identifier and the prior feature library to obtain initial anomaly scores of the local blocks; high-confidence normal local blocks are screened according to the initial anomaly scores, the high-confidence normal local blocks are aggregated according to the unique spatial identifier to obtain an intra-image normal prototype; a to-be-confirmed local block and a corresponding intra-image normal prototype are reconstructed to obtain a reconstruction response, and an anomaly heat map is determined according to the reconstruction response and the initial anomaly scores; and the method can solve the problem of large calculation amount of the existing image anomaly detection method.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of industrial visual inspection technology, and in particular to a product anomaly detection method and apparatus based on image position prior and reconstruction. Background Technology

[0002] In industrial production, product appearance quality inspection directly impacts production efficiency, yield, and shipment quality. Traditional machine vision methods primarily rely on manually designed thresholds, edge, texture, morphological, or template matching rules. These methods lack robustness in scenarios involving changes in lighting, material batch variations, complex background textures, and random defect morphologies, requiring frequent parameter tuning and failing to meet the long-term stable inspection needs of complex production lines.

[0003] Existing deep learning-based defect detection methods mainly include supervised detection methods and unsupervised anomaly detection methods. Supervised methods, such as YOLO, Faster R-CNN, Mask R-CNN, and RT-DETR, have good detection capabilities for known defects, but rely on a large number of accurately labeled defect samples, making it difficult to cover novel defects, low-frequency defects, and long-tail defects. Unsupervised methods, such as AutoEncoder, DRAEM, PaDiM, PatchCore, FastFlow, and EfficientAD, can detect unknown anomalies based solely on good product samples, but in industrial large-scale image scenarios, they typically suffer from problems such as large feature library size, high GPU and memory consumption, complex multi-scale feature calculations, repetitive calculations in sliding window block inference, inference speed that cannot meet online detection requirements, and high rates of missed and false detections.

[0004] Existing unsupervised anomaly detection methods often employ global feature libraries, global thresholds, and distance calculations based on historical template libraries. These methods fail to adequately consider the differences in normal texture, brightness, background structure, and noise distribution across different spatial locations in large industrial images. They also struggle to handle normal drift caused by lighting, ink, background color, exposure, or material batches in scenarios such as printing, packaging, paper, and film materials. Patent CN120976191A proposes a deep learning-based industrial image anomaly detection method. This method uniformly stores normal features extracted from different spatial locations in a memory bank and maps these features to eight subspaces. Each subspace must independently perform global matching with the prototypes in the memory bank, which can easily lead to cross-location feature mismatches and reduce the accuracy of location-related anomaly detection. Furthermore, it employs two branches: autoencoder pixel-level reconstruction error and a global dynamic memory bank for feature matching. Each feature is calculated in parallel, and finally, they are weighted and summed. However, the following problems exist: the autoencoder may reconstruct the anomaly well due to its strong generalization ability, while feature matching may find a similar normal prototype from the memory. At this time, the two branches may give low anomaly responses at the same time, forming a "joint missed detection" and cannot achieve independent error correction. In addition, for high-resolution industrial large images, the image needs to be divided into a large number of local image blocks. Each local image block needs to be fully decoded and reconstructed at the pixel level. The process is time-consuming, computationally expensive, and has high hardware requirements. It is only suitable for scenarios with high accuracy requirements but low speed requirements, such as offline sampling inspection, and its applicability is not high. Summary of the Invention

[0005] This invention provides a product anomaly detection method and apparatus based on image location prior and reconstruction, to solve the problem of high computational load in existing image-based anomaly detection methods.

[0006] A product anomaly detection method based on image location prior and reconstruction includes: A prior feature library containing multiple sets of sample features divided according to spatial location is pre-established; The system acquires the current product image and divides it into blocks according to spatial location, obtaining multiple image blocks and generating a unique spatial identifier for each image block. A pre-trained feature extractor is used to extract multi-layer features from image patches, and the extracted multi-layer features are matched with a prior feature library based on unique spatial identifiers to obtain the initial anomaly score of the local patch. Based on the initial anomaly score, high-confidence normal local blocks are selected, and high-confidence normal local blocks are aggregated according to the unique spatial identifier to obtain the normal prototype inside the current graph. The remaining local blocks are then designated as local blocks to be confirmed. Reconstruct the local block to be confirmed with the corresponding normal prototype inside the current graph to obtain the reconstruction response, and generate an anomaly heatmap based on the reconstruction response and the initial anomaly score. Defect region maps are generated based on abnormal heat maps.

[0007] Furthermore, a prior feature library containing multiple sets of sample features divided according to spatial location is pre-established, including: Normal sample images are acquired and divided into blocks according to spatial location to obtain multiple sample image blocks, and a unique spatial identifier is assigned to each sample image block; Each sample image patch is input into a pre-trained feature extractor for multi-layer feature extraction to obtain multi-dimensional sample features at different feature layers. Multidimensional sample features from different feature layers are input into a pre-trained deep learning model for convolutional dimensionality reduction and channel attention weighting to obtain lightweight sample features for each feature layer. Based on the lightweight features of the samples, a set of normal prototypes, a set of normal prototype radii, and a set of local normal thresholds are established for each sample image block to form a priori feature library.

[0008] Furthermore, based on the sample lightweight features, a normal prototype set, a normal prototype radius set, and a local normal threshold set are established for each sample image patch, including: Based on the unique spatial identifier of the sample, the lightweight features of each feature layer of the same sample image block are combined as normal features to obtain the normal feature set of each sample image block. Clustering, core set sampling, or sparse sampling are performed on the normal feature set of each sample image patch to generate multiple normal prototypes at each feature layer, thus obtaining a normal prototype set. The distance between each normal feature and its corresponding normal prototype is calculated using a quantile statistical function, the normal prototype radius of each normal prototype is obtained, and a set of normal prototype radii for each sample image patch is formed. Normal sample verification images are acquired and segmented and multi-layer feature extraction is performed to obtain verification lightweight features of verification image blocks. Based on the verification lightweight features, the normal prototype set, and the normal prototype radius, the local normal threshold corresponding to the verification image block is calculated to form a local normal threshold set for each image block.

[0009] Furthermore, a pre-trained feature extractor is used to perform multi-layer feature extraction on image patches. Based on unique spatial identifiers, the extracted multi-layer features are matched with a prior feature library to obtain initial anomaly scores for local patches, including: A pre-trained feature extractor is used to extract local patches from the image patch, and multidimensional features of different feature layers are extracted based on the local patches; Multidimensional features from different feature layers are input into a pre-trained deep learning model for convolutional dimensionality reduction and channel attention weighting to obtain lightweight features for each feature layer. Based on the unique spatial identifier of the image patch, the normal prototype set and normal prototype radius set of the corresponding sample image patch are searched from the prior feature library for matching, and the initial anomaly score is calculated.

[0010] Furthermore, based on the initial anomaly score, high-confidence normal local blocks are selected, including: Local blocks whose initial anomaly scores are lower than the corresponding local normal threshold are selected as candidate local blocks, and candidate local blocks are counted. The stability of the neighborhood response and the validity of the spatial location of the candidate local block are judged, and the candidate local blocks with stable neighborhood response and valid spatial location are determined as high-confidence normal local blocks.

[0011] Furthermore, high-confidence normal local blocks are aggregated according to their unique spatial identifiers, including: Aggregate high-confidence normal local blocks with the same unique spatial identifier, and / or aggregate high-confidence normal local blocks within a preset neighborhood based on the unique spatial identifier to obtain the normal prototype inside the current graph.

[0012] Furthermore, the local block to be confirmed is reconstructed along with the corresponding normal prototype within the current graph to obtain a reconstruction response, including: Based on the unique spatial identifier of the image block to be confirmed, calculate the residual and similarity between the local block to be confirmed and the corresponding normal prototype in the current image. Combine the local block to be confirmed, the corresponding normal prototype in the current image, the residual, and the similarity to form the relationship vector between the local block to be confirmed and the normal prototype in the current image. The MLP reconstructor is used to process the relation vector to generate reconstruction weights for the local blocks to be confirmed. The reconstruction weights of the local blocks to be confirmed are weighted and combined with the corresponding normal prototypes inside the current graph to obtain reconstruction features; The difference between the reconstructed features and the local block to be confirmed is calculated to obtain the reconstruction response.

[0013] Furthermore, an anomaly heatmap is generated based on the reconstruction response and initial anomaly score, including: Based on the initial anomaly score of the local block to be confirmed, the initial abnormal local block, the uncertain local block, and the initial normal local block are determined. For an initial abnormal local block, if the corresponding reconstruction response is lower than or equal to the preset response value, the corresponding initial abnormal local block is regarded as a suppressable false detection region; if the corresponding reconstruction response is higher than the preset response value, the corresponding initial abnormal local block is regarded as a high-confidence abnormal region. For uncertain local blocks and initial normal local blocks, if the corresponding reconstruction response is higher than the preset response value, the corresponding uncertain local blocks and initial normal local blocks are regarded as candidate abnormal regions of local inconsistency; if the corresponding reconstruction response is lower than or equal to the preset response value, the corresponding uncertain local blocks and initial normal local blocks are regarded as normal regions for reconstruction verification. Multi-source anomaly information is fused for suppressable false detection regions, high-confidence anomaly regions, and locally inconsistent candidate anomaly regions to generate a final anomaly score, and an anomaly heatmap is generated based on the final anomaly score.

[0014] Furthermore, a defect region map is generated based on the abnormal heat map, including: Based on the unique spatial identifier of each image block, the corresponding heatmap segmentation threshold is mapped to the current product image to form a spatial threshold map; the abnormal heatmap and the spatial threshold map are compared position by position to obtain a binary abnormal map, and morphological processing and connected component analysis are performed on the binary abnormal map to obtain a set of candidate defect regions; Each candidate defect region in the candidate defect region set is given a regional-level comprehensive score to obtain a regional-level comprehensive anomaly score. The regional-level comprehensive anomaly score is compared with the regional-level anomaly judgment threshold. Candidate defect regions that are not lower than the regional-level anomaly judgment threshold are determined as the final defect regions, and a defect region map is generated.

[0015] A product anomaly detection device based on image location prior and reconstruction includes: The feature library building module is used to pre-build a prior feature library containing multiple sets of sample features divided into blocks according to spatial location; The segmentation module is used to acquire the current product image and segment it according to spatial location to obtain multiple image blocks, and generate a unique spatial identifier for each image block; The initial scoring module is used to perform multi-layer feature extraction on image patches using a pre-trained feature extractor, and to perform feature matching with a prior feature library based on unique spatial identifiers to obtain the initial anomaly score of the local patch. The aggregation module is used to filter high-confidence normal local blocks based on the initial anomaly score, aggregate the high-confidence normal local blocks according to the unique spatial identifier, obtain the normal prototype inside the current graph, and treat the remaining local blocks as local blocks to be confirmed. The reconstruction module is used to reconstruct the local block to be confirmed with the corresponding normal prototype inside the current graph, obtain the reconstruction response, and generate an anomaly heatmap based on the reconstruction response and the initial anomaly score. The defect identification module generates a defect area map based on the anomaly heatmap.

[0016] Furthermore, the feature library building module pre-builds a prior feature library containing multiple sets of sample features divided according to spatial location, including: Normal sample images are acquired and divided into blocks according to spatial location to obtain multiple sample image blocks, and a unique spatial identifier is assigned to each sample image block; Each sample image patch is input into a pre-trained feature extractor for multi-layer feature extraction to obtain multi-dimensional sample features at different feature layers. Multidimensional sample features from different feature layers are input into a pre-trained deep learning model for convolutional dimensionality reduction and channel attention weighting to obtain lightweight sample features for each feature layer. Based on the lightweight features of the samples, a set of normal prototypes, a set of normal prototype radii, and a set of local normal thresholds are established for each sample image block to form a priori feature library.

[0017] Furthermore, the feature library building module establishes a normal prototype set, a normal prototype radius set, and a local normal threshold set for each sample image patch based on the sample lightweight features, including: Based on the unique spatial identifier of the sample, the lightweight features of each feature layer of the same sample image block are combined as normal features to obtain the normal feature set of each sample image block. Clustering, core set sampling, or sparse sampling are performed on the normal feature set of each sample image patch to generate multiple normal prototypes at each feature layer, thus obtaining a normal prototype set. The distance between each normal feature and its corresponding normal prototype is calculated using a quantile statistical function, the normal prototype radius of each normal prototype is obtained, and a set of normal prototype radii for each sample image patch is formed. Normal sample verification images are acquired and segmented and multi-layer feature extraction is performed to obtain verification lightweight features of verification image blocks. Based on the verification lightweight features, the normal prototype set, and the normal prototype radius, the local normal threshold corresponding to the verification image block is calculated to form a local normal threshold set for each image block.

[0018] Furthermore, the initial scoring module uses a pre-trained feature extractor to perform multi-layer feature extraction on image patches. Based on unique spatial identifiers, the extracted multi-layer features are matched with a prior feature library to obtain initial anomaly scores for local patches, including: A pre-trained feature extractor is used to extract local patches from the image patch, and multidimensional features of different feature layers are extracted based on the local patches; Multidimensional features from different feature layers are input into a pre-trained deep learning model for convolutional dimensionality reduction and channel attention weighting to obtain lightweight features for each feature layer. Based on the unique spatial identifier of the image patch, the normal prototype set and normal prototype radius set of the corresponding sample image patch are searched from the prior feature library for matching, and the initial anomaly score is calculated.

[0019] Furthermore, the aggregation module filters high-confidence normal local blocks based on the initial anomaly score, including: Local blocks whose initial anomaly score is lower than the corresponding local normal threshold are selected as candidate local blocks; The stability of the neighborhood response and the validity of the spatial location of the candidate local block are judged, and the candidate local blocks with stable neighborhood response and valid spatial location are determined as high-confidence normal local blocks.

[0020] Furthermore, the aggregation module aggregates high-confidence normal local blocks according to their unique spatial identifiers, including: Aggregate high-confidence normal local blocks with the same unique spatial identifier, and / or aggregate high-confidence normal local blocks within a preset neighborhood based on the unique spatial identifier to obtain the normal prototype inside the current graph.

[0021] Furthermore, the reconstruction module reconstructs the local block to be confirmed against the corresponding normal prototype within the current graph to obtain a reconstruction response, including: Based on the unique spatial identifier of the image block to be confirmed, calculate the residual and similarity between the local block to be confirmed and the corresponding normal prototype in the current image. Combine the local block to be confirmed, the corresponding normal prototype in the current image, the residual, and the similarity to form the relationship vector between the local block to be confirmed and the normal prototype in the current image. The MLP reconstructor is used to process the relation vector to generate reconstruction weights for the local blocks to be confirmed. The reconstruction weights of the local blocks to be confirmed are weighted and combined with the corresponding normal prototypes inside the current graph to obtain reconstruction features; The difference between the reconstructed features and the local block to be confirmed is calculated to obtain the reconstruction response.

[0022] Furthermore, the reconstruction module generates an anomaly heatmap based on the reconstruction response and the initial anomaly score, including: Based on the initial anomaly score of the local block to be confirmed, the initial abnormal local block, the uncertain local block, and the initial normal local block are determined. For an initial abnormal local block, if the corresponding reconstruction response is lower than or equal to the preset response value, the corresponding initial abnormal local block is regarded as a suppressable false detection region; if the corresponding reconstruction response is higher than the preset response value, the corresponding initial abnormal local block is regarded as a high-confidence abnormal region. For uncertain local blocks and initial normal local blocks, if the corresponding reconstruction response is higher than the preset response value, the corresponding uncertain local blocks and initial normal local blocks are regarded as candidate abnormal regions of local inconsistency; if the corresponding reconstruction response is lower than or equal to the preset response value, the corresponding uncertain local blocks and initial normal local blocks are regarded as normal regions for reconstruction verification. Multi-source anomaly information is fused for suppressable false detection regions, high-confidence anomaly regions, and locally inconsistent candidate anomaly regions to generate a final anomaly score, and an anomaly heatmap is generated based on the final anomaly score.

[0023] Furthermore, the defect determination module generates a defect region map based on the anomaly heatmap, including: Based on the unique spatial identifier of each image block, the corresponding heatmap segmentation threshold is mapped to the current product image to form a spatial threshold map; the abnormal heatmap and the spatial threshold map are compared position by position to obtain a binary abnormal map, and morphological processing and connected component analysis are performed on the binary abnormal map to obtain a set of candidate defect regions; Each candidate defect region in the candidate defect region set is given a regional-level comprehensive score to obtain a regional-level comprehensive anomaly score. The regional-level comprehensive anomaly score is compared with the regional-level anomaly judgment threshold. Candidate defect regions that are not lower than the regional-level anomaly judgment threshold are determined as the final defect regions, and a defect region map is generated.

[0024] The product anomaly detection method and apparatus based on image location prior and reconstruction provided by this invention have at least the following beneficial effects: (1) By using a position-aware modeling mechanism, the current product image to be detected is divided into blocks according to its spatial location. Each image block only needs to be compared with normal prior features of the same or related spatial locations, reducing redundant calculations. Based on the comparison results, the defect is confirmed, which can effectively reduce the amount of computation and avoid misjudgment caused by differences in texture, brightness, and background structure between different spatial regions, thereby improving detection accuracy. This mechanism is particularly suitable for industrial large-image detection scenarios with stable spatial structure, high resolution, and obvious local texture differences, such as printing, packaging, paper, and film materials. (2) Local normal thresholds, normal prototype radii, and normal prototypes are established for different sample image blocks, which can characterize the normal fluctuation range of different spatial regions and overcome the problem that a single global threshold cannot take into account both complex texture areas and low texture areas. At the same time, by using multiple normal prototypes and normal prototype radii, not only is the distance between the feature to be detected and the center of the normal prototype determined, but it is also determined whether it exceeds the normal variation range of the corresponding prototype, thereby improving the accuracy of normal boundary modeling; (3) After multidimensional feature extraction, a learnable 1×1 convolution dimensionality reduction, channel attention and multi-scale feature learnable fusion mechanism are introduced to adaptively weight the feature contributions of different levels, different channels and different spatial scales. This mechanism can reduce the storage volume of the prior feature library and the computational volume of anomaly matching, while retaining key features that are sensitive to texture destruction, edge changes, small defects and structural anomalies, and reducing the interference of invalid channels and redundant scale features on anomaly judgment, thereby taking into account detection accuracy, storage overhead and inference efficiency. (4) Based on prior feature library matching, a normal prototype reconstruction mechanism is introduced within the current graph. High-confidence normal local blocks with low external response, stable neighborhood and valid spatial location are selected from local blocks to form the normal prototype within the current graph. For regions that are judged as abnormal, uncertain or have local mutations in the prior, the normal prototype within the current graph is used to reconstruct them in a limited manner to determine whether the region can be reasonably explained by the normal structure of the current graph itself. Through this mechanism, regions with high prior response but can be stably reconstructed by the normal structure within the current graph can be identified as false responses caused by batch differences, light fluctuations, material changes or insufficient template coverage, thereby reducing false alarms. Regions with low prior response but difficult to be reconstructed by the normal structure within the current graph can be identified as candidate anomalies of local inconsistency in the current graph, thereby compensating for the prior omission of low contrast, small area, weak features or local mutation defects. This mechanism enables the system to not only rely on historical normal priors, but also to use the normal structure of the current graph itself for dynamic verification and self-consistency discrimination, thereby improving the stability and adaptability of anomaly detection under complex field conditions. (5) By integrating nearest neighbor distance, TopK nearest neighbor statistics, prototype radius excess, multi-channel anomaly response, template density, template confidence and regional statistical features, a multi-source anomaly scoring mechanism is formed to avoid the anomaly response being diluted in the global average or a single distance metric. This mechanism can enhance the detection capability of slight grayscale changes, small area defects, low contrast anomalies and local texture destruction. (6) When there are a few difficult samples on site, they are used as boundary calibration samples to optimize local threshold, normal prototype radius, channel weight, score fusion weight, etc. Difficult normal samples are used to expand normal boundaries, and difficult defect samples are used to enhance the abnormal response of low-scoring defects, weak defects and boundary defects, so as to take into account the open detection capability of unknown defects and the detection rate of known defects. In terms of engineering deployment, a dual-engine architecture of prior feature library and image processing is adopted, and the block rules, threshold strategy, back mapping rules and template version are solidified through deployment configuration contract. It supports fast switching, online updates, gray release and version rollback of multiple products, multiple cameras and multiple versions, reducing the detection deviation caused by the inconsistency between training end and deployment end parameters. Attached Figure Description

[0025] Figure 1 This is a flowchart of one embodiment of the product anomaly detection method based on image location prior and reconstruction provided by the present invention.

[0026] Figure 2 This is a schematic diagram of one embodiment of the product anomaly detection device based on image location prior and reconstruction provided by the present invention. Detailed Implementation

[0027] To better understand the above technical solutions, the following will provide a detailed explanation of the technical solutions in conjunction with the accompanying drawings and specific implementation methods.

[0028] refer to Figure 1 In some embodiments, a product anomaly detection method based on image location prior and reconstruction is provided, including: S1. Pre-establish a prior feature library containing multiple sets of sample features divided according to spatial location; S2. Acquire the current product image and divide it into blocks according to spatial location to obtain multiple image blocks, and generate a unique spatial identifier for each image block; S3. A pre-trained feature extractor is used to extract multi-layer features from the image patch, and the extracted multi-layer features are matched with the prior feature library based on the unique spatial identifier to obtain the initial anomaly score of the local patch. S4. Filter high-confidence normal local blocks based on the initial anomaly score, aggregate the high-confidence normal local blocks according to the unique spatial identifier, obtain the normal prototype inside the current graph, and take the remaining local blocks as local blocks to be confirmed. S5. Reconstruct the local block to be confirmed with the corresponding normal prototype inside the current map to obtain the reconstruction response, and generate an anomaly heatmap based on the reconstruction response and the initial anomaly score. S6. Generate a defect area map based on the abnormal heat map.

[0029] Further, in step S1, a prior feature library containing multiple sets of sample features divided according to spatial location is pre-established, including: S11. Acquire normal sample images and divide them into blocks according to spatial location to obtain multiple sample image blocks, and assign a unique spatial identifier to each sample image block; S12. Input each sample image block into the pre-trained feature extractor to perform multi-layer feature extraction and obtain multi-dimensional sample features of different feature layers. S13. Input the multi-dimensional sample features of different feature layers into the pre-trained deep learning model to perform convolution dimensionality reduction and channel attention weighting to obtain the lightweight sample features of each feature layer. S14. Based on the sample lightweight features, establish a normal prototype set, a normal prototype radius set, and a normal threshold set for each sample image block to form a priori feature library.

[0030] Specifically, in step S11, the normal sample image is divided into multiple sample image tiles according to a preset or configured tile_grid. Each sample image tile corresponds to a fixed spatial region in the original normal sample image, used to maintain the correspondence between local texture, brightness, background structure, and spatial position. For the sample image tile in row r and column c, its starting coordinates in the original normal sample image can be expressed as: (1) Where H and W represent the height and width of the normal sample image, respectively, and h t w t s represents the height and width of the sample image patch, respectively. y s x These represent the vertical and horizontal sliding step sizes, respectively. Using this coordinate calculation method, when the sample image block is located in the right boundary, lower boundary, or corner area, the end window automatically retreats into the image and anchors to the effective image boundary, thereby reducing abnormal responses and boundary misses caused by the extra pixel row or column (padding) areas at the edges of the sample image block.

[0031] Generate a unique spatial identifier for each sample image patch. The unique spatial identifier includes at least the product number, camera number, row index, and column index, and may further include the workstation number, inspection surface number, batch number, or registered region number.

[0032] Further, in step S12, each sample image patch is input into a pre-trained feature extractor, such as the DINOv3 feature extraction network, to extract semantic and texture features at different feature levels, thereby obtaining multi-dimensional sample features at different feature levels. Let the sample image patch in row r and column c be T. r,c Pre-trained feature extractor E θ If the selected feature layer set is L, then the multi-layer feature extraction process can be represented as: (2) in, This represents the sample image patch in row r and column c. Indicates the pre-trained feature extractor's first... Feature mapping of each feature layer Indicates the corresponding number l The sample features output by each feature layer, where L represents the set of feature layers. Shallow features are used to enhance the response to edges, textures, and small-scale defects; mid-level features are used to describe local structural changes; and deep features are used to express semantic and structural anomalies over a larger range, thereby improving the adaptability to defects of different sizes and shapes.

[0033] To address the differences between industrial images and general natural images in terms of texture distribution, lighting conditions, material state, and imaging methods, when a sufficient number of normal sample images are available, spatially aware neighborhood adaptive training is performed on the pre-trained feature extractor or its lightweight adaptation layer. This training belongs to spatial location-aware self-supervised neighborhood adaptation training, which includes neighborhood consistency constraints and weak contrast margin constraints. Using the unique spatial identifier of a sample and its neighborhood location as constraints, it ensures that normal features within the same spatial location and its neighborhood remain stably clustered, while preserving the inherent normal texture and structural differences between unique spatial identifiers of different samples. Its training constraints can be expressed as: (3) (4) (5) (6) (7) Among them, L aug Lightweight feature z representing the unique spatial identifier of the same sample i The consistency constraint between the two enhanced feature vectors obtained after enhancement, L near Lightweight feature z representing the unique spatial identifier of the same sample i The neighborhood stability constraint of its neighborhood, z j Let N(i) represent the lightweight feature of the j-th sample in the neighborhood set, and let L represent the neighborhood set. proto The lightweight feature z of the sample i Prototype constraints between the corresponding spatial normal prototype ptile(i) and the prototype constraint between the prototype and the prototype. This represents a constraint that preserves the differences between non-neighboring spatial regions, where Q(i) represents the set of non-neighboring regions, and z k ω represents the lightweight feature of the k-th sample in the non-neighborhood set; ω represents the set of lightweight features of all samples with unique spatial identifiers for the same sample; m represents the boundary value; λ1, λ2, and λ3 are weight coefficients; L adapt This indicates a general constraint.

[0034] By applying the above constraints, we can adapt to the distribution of industrial images on site while avoiding forcibly bringing the normal patterns of different spatial regions closer together.

[0035] In step S13, to reduce the storage requirements of the subsequent prior feature library and the distance calculation in the anomaly scoring stage, after obtaining multi-layered multi-dimensional sample features, a learnable 1×1 convolutional channel compression module is set up to map the multi-dimensional sample features into low-dimensional sample features. Simultaneously, a channel attention module is introduced to generate channel weights based on spatial mean, spatial variance, local gradient response, texture response, and spatial embedding information of the sample's unique spatial identifier, adaptively weighting feature channels at different positions and levels. This method enhances the expression of defect-sensitive channels and stable normal modeling channels, suppresses redundant channels and channels susceptible to illumination noise, thereby improving the stability of subsequent position template modeling, internal normal reconstruction, and anomaly residual calculation.

[0036] Let the sample image patch in row r and column c be in the... l The multidimensional sample features of the layer are The 1×1 convolution channel compression process can then be represented as: (8) in, Indicates the first Low-dimensional sample features after dimensionality reduction through convolutional channels. and Let represent the learnable weights and biases of a 1×1 convolution, respectively; BN() represents the normalization operation; and φ() represents the non-linear activation function.

[0037] Furthermore, channel attention input vectors are constructed and channel attention weights are generated based on channel statistics, local response information, and spatial identifier embedding. l The lightweight feature representation of the sample after channel attention weighting is as follows: (9) in, Indicates the first Lightweight features of layer samples Indicates channel attention weights. This indicates a channel-weighted operation.

[0038] To balance the expressive power of multi-layer features with the efficiency of industrial large-scale graph inference, this embodiment does not directly concatenate features from different levels into a single high-dimensional feature. Instead, it learns the contribution weights of each feature layer to anomaly detection during the training phase, assuming the th feature layer as the first feature layer. The learnable weight parameters corresponding to the layer are Then its normalized hierarchical weights are expressed as: (10) in, Indicates the first The fusion weights of layer features, β kThis represents the k-th feature layer in the feature layer set L.

[0039] Further, in step S14, based on the sample lightweight features, a normal prototype set, a normal prototype radius set, and a local normal threshold set are established for each sample image patch, including: S141. Based on the unique spatial identifier of the sample, combine the lightweight features of each feature layer of the same sample image block as normal features to obtain the normal feature set of each sample image block. S142. Cluster, core set sampling or sparse sampling is performed on the normal feature set of each sample image block to generate multiple normal prototypes at each feature layer and obtain a normal prototype set. S143. Use quantile statistics function to calculate the distance between each normal feature and the corresponding normal prototype, obtain the normal prototype radius of each normal prototype, and form a set of normal prototype radii for each sample image block. S144. Obtain normal sample verification images and perform block segmentation and multi-layer feature extraction to obtain verification lightweight features of verification image blocks. Calculate the local normal threshold corresponding to each verification image block based on the verification lightweight features, the normal prototype set, and the normal prototype radius to form a local normal threshold set for each image block.

[0040] Specifically, in step S141, the lightweight features of each feature layer of the same sample image block are combined as normal features, resulting in the normal feature set for each sample image block: (11) Among them, B g , l The image patch with unique spatial identifier g represents the sample image patch in the th... l The normal feature set of each feature layer This indicates that the sample image patch in the r-th row and c-th column of the n-th normal sample image is in the [r-th row and c-th column]. l Lightweight features of layer samples, D ok G represents the set of normal sample images used in database construction. r,c This represents the unique spatial identifier of the sample image block in the r-th row and c-th column of a normal sample image. This represents the unique spatial identifier of the sample image block in the r-th row and c-th column of the n-th normal sample image.

[0041] In this way, the normal feature set is isolated according to product, camera, workstation and spatial location, so that each sample image block only learns the normal texture, brightness and structure distribution in its corresponding spatial region, avoiding mutual interference between normal differences between different spatial regions.

[0042] Furthermore, in step S142, clustering, core set sampling or sparse sampling are performed on the normal features of each sample image block to generate multiple normal prototypes in each feature layer, thereby obtaining a set of normal prototypes to characterize the various normal texture forms, brightness variations and structural patterns that may exist at this spatial location.

[0043] Let g be the unique spatial identifier of the sample image patch in the th case. l Each feature layer generates K g,l If there are 10 normal prototypes, then the set of normal prototypes can be represented as: (12) Among them, P g,l The image patch with unique spatial identifier g represents the sample image patch in the th... l The normal prototype set of each feature layer, P g,l,k This represents the k-th normal prototype.

[0044] When clustering is used to generate normal prototypes, the optimization objective can be expressed as: (13) Among them, z i Let C represent the i-th normal feature in the set of normal features. g,l,k This represents the subset of normal features assigned to the k-th normal prototype. The corresponding normal prototype P g,l,k It can be represented as: (14) By using multi-prototype modeling, multiple normal states within the same spatial location can be expressed separately, avoiding misjudgments caused by a single mean template failing to cover complex textures, local brightness variations, or multi-morphological normal backgrounds.

[0045] Furthermore, each normal feature is assigned to the nearest normal prototype, and by counting the number of different normal features corresponding to each normal prototype, the number of valid normal samples supporting each normal prototype can be obtained.

[0046] In step S143, for the k-th normal prototype, the radius of the normal prototype can be calculated based on the distance distribution of its corresponding normal feature set: (15) Among them, R g,l,k This represents the normal prototype radius of the k-th normal prototype. Indicates the first Quantile statistical function. The normal prototype radius is used to describe the normal range of fluctuations covered by the normal prototype.

[0047] In step S144, a normal sample verification image is acquired and segmented into blocks and multi-layer feature extraction is performed according to spatial location to obtain the verification lightweight feature of the verification image block. The minimum distance from the verification lightweight feature to the normal prototype in the set of normal prototypes with the same unique spatial identifier is calculated to obtain the verification minimum prototype distance. The TopK nearest neighbor mean of the verification lightweight feature is calculated, which is the average of the K minimum distances between the verification lightweight feature and the normal prototype. The excess amount of the verification minimum prototype distance beyond the corresponding normal prototype radius is calculated. The excess amount of the verification minimum prototype distance beyond the corresponding normal prototype radius, the TopK nearest neighbor mean of the verification lightweight feature, and the verification minimum prototype distance are normalized using center parameters and scale parameters, respectively. The normalized verification minimum prototype distance, the excess amount of the verification minimum prototype distance beyond the corresponding normal prototype radius, and the TopK nearest neighbor mean of the verification lightweight feature are weighted and summed to obtain the normal threshold of the verification lightweight feature. The normal thresholds of different feature layers are weighted and summed to obtain the local normal threshold of the verification local block. The local normal thresholds of all verification local blocks form a local normal threshold set.

[0048] For any lightweight verification feature Z i The minimum prototype distance from the corresponding normal prototype set to the verification prototype can be expressed as: (16) in, Indicates the validation of lightweight feature Z i With the normal prototype P in the normal prototype set of the g-th sample image patch g,l,k In the l Minimum prototype distance for verification within the layer.

[0049] Furthermore, the TopK nearest neighbor mean of the validation lightweight feature is the average of the K smallest distances between the validation lightweight feature and the normal prototype.

[0050] The distribution center and fluctuation scale can be represented by a centrality parameter and a scale parameter, respectively, and are calculated as follows: (17) (18) Where median(·) represents the median statistical function, and MAD(·) represents the median absolute deviation statistical function. To prevent extremely small constants with a denominator of zero, m g,l Represents the central parameter, s g,l The scale parameter is represented by (·), which represents the object of the corresponding statistical function. It can be the amount by which the minimum prototype distance exceeds the radius of the corresponding normal prototype, the TopK nearest neighbor mean of the lightweight feature, or the minimum prototype distance.

[0051] Central parameter m g,l The scale parameter s is used to characterize the center of the spatial location distance distribution. g,l The fluctuation scale used to characterize the distance distribution of this spatial location.

[0052] The minimum prototype distance and the mean distance of the TopK normal prototypes are standardized using the center parameter and scale parameter, respectively, and then weighted summation is performed to obtain the normal threshold of the verification local block.

[0053] This normalization method can reduce the problems of unique spatial identifiers for different samples and inconsistent fractional scales between different feature layers.

[0054] In some embodiments, the same steps can be performed on the abnormal sample verification image to obtain a set of local anomaly thresholds for each image patch.

[0055] The prior feature library stores template metadata corresponding to blocks and feature layers. By storing the above template metadata, position constraint matching, local threshold determination, score normalization, template confidence weighting, template version rollback, and online updates can be performed during the detection phase, thereby improving the stability and maintainability of anomaly detection in industrial large-scale image scenarios.

[0056] Further, in step S2, the detection stage is entered, the current product image is acquired and divided into blocks according to spatial location to obtain multiple image blocks, and a unique spatial identifier is generated for each image block. Each unique spatial identifier corresponds to a unique spatial identifier of a sample in the prior feature library.

[0057] Further, in step S3, a pre-trained feature extractor is used to perform multi-layer feature extraction on the image patch. Feature matching is performed based on the unique spatial identifier and the prior feature library to obtain the initial anomaly score of the local patch, including: S31. Use a pre-trained feature extractor to extract local blocks from the image patch, and extract multi-dimensional features of different feature layers based on the local blocks; S32. Input the multidimensional features of different feature layers into the pre-trained deep learning model to perform convolution dimensionality reduction and channel attention weighting to obtain lightweight features of each feature layer. S33. Based on the unique spatial identifier of the image block, search for the normal prototype set and normal prototype radius set of the corresponding sample image block in the prior feature library and match them to obtain the initial anomaly score of the local block.

[0058] Specifically, the minimum distance from the lightweight feature to the normal prototype in the set of normal prototypes with unique spatial identifiers is calculated to obtain the nearest neighbor distance. The average of the K minimum distances between the lightweight feature and the normal prototype is calculated to obtain the TopK nearest neighbor mean of the lightweight feature. The excess amount of the nearest neighbor distance beyond the radius of the corresponding normal prototype is calculated. The nearest neighbor distance, the TopK nearest neighbor mean of the lightweight feature, and the excess amount of the nearest neighbor distance beyond the radius of the corresponding normal prototype are normalized by the center parameter and the scale parameter, respectively. Then, a weighted sum is performed to obtain the initial anomaly score of the lightweight feature. The initial anomaly scores of multiple feature layers are weighted and summed to obtain the initial anomaly score of the local block.

[0059] For scenarios with slight registration errors, material deformation, or local texture drift, local matching can be performed within a neighborhood space with the same unique spatial identifier to reduce the impact of spatial misalignment on score calculation. Specifically, if multiple adjacent local blocks exhibit essentially consistent small displacements, a slight registration error is considered to exist. If the displacement changes continuously and smoothly with spatial position, material deformation is considered to exist. If the structural position remains basically unchanged, but the texture or feature distribution changes continuously in adjacent regions and can be stably matched by normal prototypes in the neighborhood, local texture drift is considered to exist.

[0060] Further, in step S4, after obtaining the initial anomaly score of the local block, high-confidence normal local blocks are selected based on the initial anomaly score, including: S41. Select local blocks whose initial abnormality score is lower than the corresponding local normal threshold as candidate local blocks; S42. Determine the neighborhood response stability and spatial location validity of the candidate local block, and identify the candidate local block with stable neighborhood response and valid spatial location as a high-confidence normal local block.

[0061] Specifically, local blocks with initial anomaly scores below a local normal threshold are designated as candidate local blocks. The proportion of high-confidence normal local blocks within their neighborhood and the fluctuation of their initial anomaly scores are statistically analyzed. When the majority of the neighborhood of a candidate local block consists of high-confidence normal local blocks (the proportion of high-confidence normal local blocks is greater than a preset value), the fluctuation of the initial anomaly score is within a preset range, and there are no obvious adjacent anomalies, the candidate local block is determined to have a stable neighborhood response. The spatial validity of the candidate local block is determined based on whether it is occluded, crosses boundaries, or has a normal exposure state. If the local block is not occluded, crosses boundaries, or has a normal exposure state, its spatial location is determined to be valid. Candidate local blocks with stable neighborhood responses and valid spatial locations are designated as high-confidence normal local blocks.

[0062] High-confidence normal local blocks are aggregated according to unique spatial identifiers to obtain an internal normal prototype. This internal normal prototype is not stored as a long-term fixed template, but dynamically participates in reconstruction during the current detection process to characterize the batch texture, illumination variations, material fluctuations, and local background structure differences inherent in the current image. In some embodiments, the screening process excludes image block boundary regions, pixel rows or columns (padding) regions with extra padding at the edges of image blocks, high-scoring regions with initial anomaly scores, isolated high-response neighborhoods, and regions with insufficient template support numbers to prevent abnormal or unreliable regions from participating in normal modeling.

[0063] Furthermore, high-confidence normal local blocks can be aggregated according to the same unique spatial identifier, adjacent unique spatial identifier, 3×3 adjacent unique spatial identifier, the same row and column region, or the same process texture region to obtain the normal prototype inside the current image, avoiding unconstrained mixed reconstruction between different spatial structures.

[0064] Further, in step S5, the local block to be confirmed is reconstructed with the corresponding normal prototype inside the current graph to obtain a reconstruction response, including: S51. Based on the unique spatial identifier of the image block where the local block to be confirmed is located, calculate the residual and similarity between the local block to be confirmed and the corresponding normal prototype in the current image. Combine the local block to be confirmed, the corresponding normal prototype in the current image, the residual, and the similarity to form a relationship vector between the local block to be confirmed and the normal prototype in the current image. S52. Use an MLP reconstructor to process the relation vector and generate reconstruction weights for the local blocks to be confirmed. S53. The reconstruction weights of the local blocks to be confirmed are weighted and combined with the corresponding normal prototypes inside the current graph to obtain reconstruction features. S54. Calculate the difference between the reconstructed features and the local block to be confirmed to obtain the reconstruction response.

[0065] Specifically, a lightweight MLP reconstructor is used to reconstruct normal features within the current graph under normal prototype constraints. Instead of directly generating the reconstruction results of the features to be tested using a conventional autoencoder, this reconstructor uses the normal prototype within the current graph as the reconstruction basis, learns the matching relationship between features in the local block to be confirmed and the normal prototype within the current graph, and outputs the reconstruction weights of each local block to be confirmed. Subsequently, the reconstructed features of the features to be confirmed are obtained through weighted combination. Let the local block to be confirmed be s. i The unique spatial identifier corresponds to the normal prototype within the current graph, g(i), and the k-th normal prototype within the current graph is... Construct a relationship vector between the feature to be confirmed and the normal prototype within the current graph, and generate reconstruction weights using an MLP: (19) (20) in, This represents the relationship vector between the local block to be confirmed and the k-th normal prototype. This represents a lightweight MLP reconstructor with parameter ψ. This represents the reconstruction weight of the k-th normal prototype, where K represents the number of normal prototypes in the current spatial group. Represents the residual. Indicates similarity.

[0066] By comparing the differences between the local block to be identified and its reconstructed features, a reconstruction response is obtained, which is used for secondary discrimination of the local block to be identified.

[0067] If the local block to be confirmed can be stably reconstructed by the normal prototype within the current graph, it indicates that the difference between the features of the local block to be confirmed and the features of the prior feature library may be explained by normal texture, lighting, or material fluctuations within the current graph. If the local block to be confirmed is difficult to reconstruct by the normal prototype within the current graph, it indicates that the local block to be confirmed has local inconsistencies relative to the normal structure of the current graph, and is more likely to be an anomaly. The reconstruction features and reconstruction response under the normal prototype constraint can be expressed as: (twenty one) (twenty two) in, Indicates reconstruction features, This represents the reconstruction response. The larger the reconstruction response, the more difficult it is for the local block to be confirmed to be interpreted by the normal prototype within the current graph.

[0068] The advantages of this approach are: the reconstruction results are restricted to the normal feature space spanned by normal prototypes within the current graph, avoiding the unconstrained reconstruction of abnormal features by ordinary reconstruction networks; at the same time, MLP only predicts the weights of normal prototype combinations, which can replace TopK retrieval and manual similarity weighting for each local block, reducing the computational overhead in industrial large graph inference.

[0069] Further, in step S5, an anomaly heatmap is generated based on the reconstructed response and the initial anomaly score, including: S55. Based on the initial anomaly score of the local block to be confirmed, determine the initial abnormal local block, the uncertain local block, and the initial normal local block; S56. For an initial abnormal local block, if the corresponding reconstruction response is lower than or equal to the preset response value, the corresponding initial abnormal local block is regarded as a suppressable false detection region; if the corresponding reconstruction response is higher than the preset response value, the corresponding initial abnormal local block is regarded as a high-confidence abnormal region. S57. For uncertain local blocks and initial normal local blocks, if the corresponding reconstruction response is higher than the preset response value, the corresponding uncertain local blocks and initial normal local blocks are regarded as candidate abnormal regions of local inconsistency; if the corresponding reconstruction response is lower than or equal to the preset response value, the corresponding uncertain local blocks and initial normal local blocks are regarded as normal regions for reconstruction verification. S58. Multi-source anomaly information is fused with the suppressed false detection region, high-confidence anomaly region and locally inconsistent candidate anomaly region to generate a final anomaly score, and an anomaly heatmap is generated based on the final anomaly score.

[0070] Specifically, in step S55, unconfirmed local blocks with initial anomaly scores greater than or equal to the normal threshold and less than the anomaly threshold are designated as uncertain local blocks, unconfirmed local blocks with initial anomaly scores greater than or equal to the anomaly threshold are designated as initial abnormal local blocks, and local blocks with initial anomaly scores less than the normal threshold but not designated as high-confidence normal local blocks are designated as initial normal local blocks.

[0071] Furthermore, in steps S56 and S57, the initial anomaly score and the co-anomaly score with the reconstruction response are integrated. Based on the initial judgment state of the initial anomaly score, differentiated internal reconstruction interpretation strategies are adopted for different regions, thereby achieving false detection suppression, false negative compensation, and secondary discrimination of uncertain regions.

[0072] For initial anomalous local blocks initially identified as anomalous by the initial anomaly score, reconstruction with the normal prototype within the current image is primarily used for anomaly verification. The process determines whether the initial anomalous local block can be reasonably reconstructed by the spatially and structurally relevant normal prototype within the current image, distinguishing between genuine defects and false responses from templates in the prior feature library caused by batch differences, lighting fluctuations, texture changes, material state changes, or insufficient historical template coverage. If the initial anomalous local block can be reconstructed by the normal prototype within the current image (i.e., the corresponding reconstruction response is lower than or equal to a preset response value), the initial anomaly score considers it anomalous, which could be a genuine defect or a false alarm caused by batch differences, lighting, material changes, or insufficient historical template coverage. If the reconstruction is deemed explanatory, it may indicate a normal variation in the current image, or it may be due to an unreliable internal normal prototype or an incorrect reconstruction of a genuine defect by the MLP. In this case, the corresponding initial anomalous local block is designated as a suppressable false detection area. If the initial anomalous local block is difficult to reconstruct by the normal prototype within the current image (i.e., the corresponding reconstruction response is higher than a preset response value), the corresponding initial anomalous local block is designated as a high-confidence anomalous area.

[0073] For initially normal local blocks that are initially judged to be normal based on the initial anomaly score but not confirmed as high-confidence normal local blocks, the reconstruction of the normal prototype within the current graph is mainly used for self-consistency detection of the current graph. The system identifies regions that, although not significantly deviating from the historical normal template, exhibit local abrupt changes, discontinuities, or weak anomalous responses relative to the normal prototype within the current graph—that is, regions with reconstruction responses higher than a preset response value—as candidate anomalous regions for local inconsistency. If the corresponding reconstruction response is lower than or equal to the preset response value, the corresponding uncertain local block and the initially normal local block are considered as normal regions for reconstruction verification.

[0074] For uncertain local blocks that are initially judged as uncertain based on the initial anomaly score, the reconstruction of the normal prototype inside the current graph is used as a secondary discrimination criterion. If the corresponding reconstruction response is higher than the preset response value, the corresponding uncertain local block is regarded as a candidate anomaly region for local inconsistency. If the corresponding reconstruction response is lower than or equal to the preset response value, the corresponding uncertain local block is regarded as a normal local block for reconstruction verification.

[0075] In summary, this approach enables the modeling and determination of unexplainable abnormal responses, thereby improving the stability, adaptability, and interpretability of unknown defect detection in high-resolution industrial images.

[0076] In step S58, multi-source anomaly information is fused to generate a final anomaly score. After completing prior database matching and reconstruction of normal features within the current image, the system uniformly fuses the multi-source information to generate a local block-level final anomaly score. The multi-source anomaly information involved in the fusion includes the initial anomaly score, reconstruction response, reconstruction confidence, image block-level score normalization result, template confidence, neighborhood anomaly support, number of effective sample support, and consistency across different feature layers.

[0077] Specifically, in step S58, multi-source anomaly information is fused from suppressable false detection regions, high-confidence anomaly regions, and locally inconsistent candidate anomaly regions to generate a final anomaly score, including: S581. For regions where false detections can be suppressed, the final anomaly score is calculated based on the initial anomaly score, reconstruction response, reconstruction confidence, template confidence, and neighborhood anomaly support. If the reconstruction confidence meets the requirements, the template confidence does not meet the high confidence requirements, and the neighborhood anomaly support is insufficient, the average of the normalized initial anomaly score and the reconstruction response is taken as the final anomaly score. Otherwise, the normalized initial anomaly score is taken as the final anomaly score. S582. For high-confidence anomaly regions, calculate the final anomaly score based on the initial anomaly score, reconstruction response, reconstruction confidence, and number of valid normal samples. If both the reconstruction confidence and the number of valid normal samples meet the requirements, take the larger value between the normalized initial anomaly score and the reconstruction response as the final anomaly score; otherwise, take the normalized initial anomaly score as the final anomaly score. S583. For locally inconsistent candidate anomaly regions, the judgment is made based on the reconstruction response, neighborhood anomaly support, consistency of different feature layers, and image block-level normalization results. If the consistency of different feature layers meets the requirements, or if the neighborhood anomaly support and image block-level normalization results meet the requirements at the same time, the larger value between the normalized initial anomaly score and the reconstruction response is taken as the final anomaly score; otherwise, the normalized initial anomaly score is taken as the final anomaly score.

[0078] Specifically, in step S581, the initial anomaly score is normalized using local normal thresholds and local anomaly thresholds. The reconstruction response is normalized using the reconstruction response distribution of normal samples within the same unique spatial identifier. The reconstruction support is determined based on the number of normal prototypes within the corresponding current graph. The reconstruction confidence is the smaller value between the reconstruction support and the reconstruction weight. The template confidence can be determined based on the number of corresponding valid normal sample supports, cross-batch stability, template version validity of normal features, and historical matching stability. The number of valid normal sample supports is the number of different normal features corresponding to the corresponding normal prototype. Cross-batch stability is calculated based on the coverage ratio of the corresponding normal prototype in all database batches and the degree of drift of the normal feature center in different batches relative to the prototype. The more batches covered and the smaller the batch center drift, the higher the cross-batch stability. The template version validity of normal features adopts two states: valid and invalid. During testing, the product model, camera serial number, workstation serial number, feature extractor version, image preprocessing parameter version, and image block division rule version are verified. If all of these information matches and the template has passed good product verification, the template version is valid. If any information is inconsistent, the template version is invalid and will not participate in the current testing. Historical matching stability is determined based on the matching results of the normal sample validation set and manually confirmed normal samples. The system counts the number of all confirmed normal features whose most recent normal prototype is the current prototype, and counts the number of normal features whose distance does not exceed the radius of the normal prototype. The ratio of these two counts is used as historical matching stability. When the template version is valid, cross-batch stability and historical matching stability are weighted and merged to obtain the template credibility. Neighborhood anomaly support is the percentage of valid local blocks with anomaly evidence within an 8-neighborhood.

[0079] In step S582, the number of valid normal samples supports the number of different normal features corresponding to each normal prototype.

[0080] In step S583, the consistency of different feature layers is the proportion of feature layers that support anomalies. The image block-level normalization result is the score of each local block in the image block whose initial anomaly score exceeds the normal threshold. The average of M scores is aggregated to obtain the image block-level normalization result.

[0081] For regions where false detections can be suppressed, the focus is on determining whether the low reconstruction response has sufficient confidence to decide whether to reduce the abnormal response generated by the template in the prior feature library, i.e., the initial abnormal score. For high-confidence abnormal regions, the focus is on verifying the consistency between the template in the prior feature library and the reconstructed abnormality, and eliminating interference caused by boundaries or low-confidence responses. For locally inconsistent candidate abnormal regions, the focus is on combining neighborhood continuity and image block-level normalization results to determine whether to promote them to the final abnormality.

[0082] Furthermore, the initial anomaly scores and reconstruction responses are first normalized, and then corresponding fusion rules are applied based on the region type to which the local block to be confirmed belongs. For regions where false detections can be suppressed, the focus is on using reconstruction confidence, template confidence, and neighborhood anomaly support to resolve conflicts between features in the prior database and internal reconstruction. Anomaly scores are only reduced when the reconstruction results are reliable and there is a lack of other anomaly corroboration. For high-confidence anomaly regions, the focus is on verifying whether the consistent anomaly responses of the two detection branches have sufficient normal sample support, reconstruction support, and spatial continuity. When these conditions are met, the anomaly determination is directly enhanced. For locally inconsistent candidate anomaly regions, the focus is on using neighborhood continuity, consistency of different feature layers, and image block-level normalization results to determine whether to promote them to the final anomaly.

[0083] The initial anomaly score is used to characterize the degree of deviation of a local block from the historical normal prior in the prior feature library. The reconstruction response is used to characterize whether the local block can be interpreted by the normal structure inside the current image. The reconstruction confidence is used to measure the reliability of the internal reconstruction results. The template confidence and image block-level normalization results are used to correct the fractional scale differences between different spatial locations, different template qualities and different feature layers. Neighborhood consistency is used to suppress false detections caused by isolated noise responses and incomplete boundary context.

[0084] To eliminate the fractional scale differences between different image patches, different feature layers, and different template distributions, the final anomaly score s can be adjusted. i Perform robust normalization: (twenty three) in, Let g(i) represent the local normal threshold distribution of the spatial group to which the i-th local block belongs, median() represents the median, MAD() represents the absolute deviation of the median, and ε is a minimal constant to prevent the denominator from being zero. This is the final anomaly score after normalization.

[0085] Through the fusion of multi-source information described above, the system obtains a unified local block-level final anomaly score, which is used for subsequent image block-level heatmap generation, whole-image anomaly heatmap backmapping, and region-level defect determination. This fusion process enables the final anomaly score to simultaneously reflect the degree of deviation from historical normality, the degree of uninterpretability of the normal structure within the current image, and the stability of the spatial neighborhood response, thereby providing a more stable and reliable basis for subsequent defect output.

[0086] Further, in step S6, an anomaly heatmap is generated based on the final anomaly score, including: S61. Based on the unique spatial identifier, the final anomaly score is mapped to the corresponding image patch to form an image patch-level anomaly heat map. S62. Determine the corresponding position of the image block-level abnormal heat map in the current product image. If there is only one image block-level abnormal heat map at the corresponding position of the current product image, then map the image block-level abnormal heat map to the corresponding position of the current product image. S63. If there are two or more image block-level anomalous heatmaps at the same corresponding position in the current product image, then the multiple image block-level anomalous heatmaps are weighted, fused, and masked before being mapped to the corresponding position in the current product image. S64. After completing the mapping of all image block-level anomaly heatmaps, the anomaly heatmap is obtained.

[0087] Furthermore, generating a defect region map based on the anomaly heatmap also includes: S7. Based on the unique spatial identifier of each image block, map the corresponding heatmap segmentation threshold to the current product image to form a spatial threshold map; compare the abnormal heatmap with the spatial threshold map position by position to obtain a binary abnormal map, and perform morphological processing and connected component analysis on the binary abnormal map to obtain a set of candidate defect regions. S8. Perform a regional-level comprehensive score on each candidate defect region in the candidate defect region set to obtain a regional-level comprehensive anomaly score. Compare the regional-level comprehensive anomaly score with the regional-level anomaly judgment threshold. Determine the candidate defect regions that are not lower than the regional-level anomaly judgment threshold as the final defect regions and generate a defect region map.

[0088] The system first maps the final anomaly score at the image block level back to the local coordinate space of the corresponding image block, generating an image block-level anomaly heatmap. Then, based on the cropping coordinates, boundary type, effective region mask, and backmapping parameters of each image block in the original current product image, the image block-level anomaly heatmap is restored to the original image coordinate system, forming an anomaly heatmap. Let the image block-level anomaly heatmap in row r and column r be... Its starting coordinates in the original current product image are Then the original image coordinates corresponding to the local coordinates (u,v) of the image patch are: (twenty four) Where (x,y) represents the coordinates of the original image, and (u,v) represents the local coordinates of the image patch.

[0089] When multiple image patch-level anomalous heatmaps overlap and map to the same location in the original image, a weighted fusion is performed based on the reliability of that location within each image patch. Locations closer to the patch center and with complete context are assigned higher weights; locations closer to the patch boundary, with incomplete context, or belonging to invalid regions are assigned lower weights or excluded using a valid region mask. Specifically, this can be represented as follows: (25) Where A(x,y) represents the position of the original image. The final anomaly score at the location, This represents the set of image patches that cover the location of the original image. This represents the fusion weight of the corresponding image patch at that location. This represents the effective area mask, used to filter padding areas or invalid boundary areas. The fusion weights can be set based on their distance from the center of the image patch, giving higher weights to the central area and lower weights to the boundary areas.

[0090] For overlapping regions of anomalous heatmaps at the level of adjacent image blocks, the system employs center weight fusion, confidence weighted fusion, or multi-response weighted averaging to make the anomalous response contribution of the central region of the image block higher and the anomalous response contribution of the boundary region lower, thereby reducing abrupt changes in the fraction of the overlapping region and obtaining a continuous and stable anomalous heatmap.

[0091] Defect results are output based on anomaly heatmaps. After obtaining the anomaly heatmap, threshold segmentation, morphological processing, and connected component analysis are performed to obtain a set of candidate defect regions. Subsequently, for each candidate defect region, its maximum final anomaly score, average anomaly score, and area are calculated, and a region-level comprehensive anomaly score is calculated accordingly. Let the candidate defect region be R. m Its regional comprehensive anomaly score S(R) m This can be represented as: (26) Where A(x,y) represents the position The final anomaly score at the location, R m This represents the m-th candidate defect region. The first term represents the maximum final anomaly score of the region, the second term represents the average anomaly score of the region, and the third term represents the contribution of the region area. β1, β2, and β3 are the region-level score weights.

[0092] In some embodiments, the regional comprehensive anomaly score can be compared with the regional anomaly judgment threshold, and further filtering and grade judgment can be performed by combining geometric rules such as aspect ratio, continuity, directionality, boundary distance and continuity across image blocks to determine the final defect area.

[0093] The system further incorporates rules such as region area thresholds, aspect ratio ranges, boundary distances, directional consistency, and cross-image block continuity to filter and classify candidate defect regions. For final defect regions that meet the output conditions, the system outputs their defect location, defect contour, final anomaly score, defect level, and alarm result in the original image coordinate system. Through the step-by-step processing of local block-level anomaly scoring, image block-level heatmap fusion, and region-level defect determination, the continuity, stability, and engineering interpretability of unknown defect detection results in high-resolution industrial images can be improved.

[0094] Furthermore, the method also includes: Obtain confirmed difficult normal samples and difficult defective samples; The abnormal boundaries are calibrated based on the difficult normal samples and difficult defective samples.

[0095] When a small number of manually confirmed difficult normal samples and difficult defect samples exist on-site, the system uses these difficult samples to perform semi-supervised calibration of the anomaly judgment boundary. Difficult normal samples are mainly used to correct the normal variation range of the corresponding spatial location, including the local normal threshold. After repeated confirmation, normal features can also be selectively added to prevent areas that are manually confirmed as normal but have high anomaly scores from continuing to generate false alarms. Difficult defect samples are not written into the prior feature library, but are used to adjust the scoring weights in the matching of local anomaly thresholds and the prior feature library, internal feature reconstruction, and the final fusion process. At the same time, it is necessary to compare difficult defect samples with normal features under the same unique spatial identifier channel by channel to identify defect-sensitive channels that have significant response deviations in difficult defect samples, stable cross-sample responses, and small fluctuations in normal samples, and to improve the contribution of these channels to template distance, reconstruction error, and final anomaly score. The default implementation only updates the threshold, statistical parameters, fusion coefficients, and channel calibration weights, keeping the main parameters of the pre-trained feature extractor and MLP reconstructor unchanged. When the number of difficult defect samples is large and meets the preset requirements, the backbone network can also be frozen, and only the lightweight adaptation layer, channel attention module, MLP reconstructor, and scoring fusion layer are subjected to controlled incremental training.

[0096] Let the set of difficult normal samples for manual verification be . The set of difficult defect samples is The final anomaly score for the i-th difficult normal sample is The threshold of its spatial group is The final anomaly score for the j-th difficult normal sample is: The threshold of its spatial group is Then the difficult boundary calibration objective can be expressed as: (27) in, The first term is used to constrain the final anomaly score of difficult normal samples to be lower than the anomaly boundary of the corresponding spatial group, thereby reducing false alarms; the second term is used to constrain the final anomaly score of difficult defect samples to be higher than the anomaly boundary and maintain a certain interval, thereby reducing false negatives; m represents the expected interval between difficult defect samples and the anomaly boundary; θ represents the scoring weight, channel weight, MLP reconstructor parameters or lightweight scoring layer parameters involved in calibration. The first term represents the parameters before calibration; the third term is used to limit parameter offset and avoid overfitting of the system due to a small number of difficult samples, where λ is the regularization coefficient.

[0097] The calibration process can be completed without retraining the deep feature extraction network, or only minor adjustments can be made to the dimensionality reduction layer, channel attention layer, or scoring fusion layer when needed, thus balancing the ability to detect unknown defects, the detection rate of known defects, and the stability of field deployment.

[0098] The system continuously utilizes manually confirmed difficult normal samples and difficult defect samples to dynamically optimize template boundaries, channel weights, and scoring weights. For regions prone to false positives but manually confirmed as normal, the system includes them in the image patch corresponding to the unique spatial identifier or the normal variation range of the unique spatial identifier, and updates the normal radius, local normal threshold, template confidence, and relevant channel weights. For patterns with low scores but manually confirmed as defects, the system does not write them into the prior feature library, but instead increases the weight of their relevant channels, scoring items, or regional features in the final anomaly determination. For difficult normal samples, a conservative update method can be used to adjust the normal boundaries of the corresponding spatial group. (28) in, This represents the local normal threshold of the image patch corresponding to the updated unique spatial identifier. This indicates the threshold before the update. Represents the original set of normal sample scores. This represents the set of scores for difficult and normal samples within this spatial group. Indicates the first Quantile statistical function.

[0099] This update method expands the normal range only when manually confirmed normal changes exceed the original boundaries, avoiding unstable threshold contraction or abnormal widening due to fluctuations in a single sample. By continuously introducing manually confirmed difficult normal samples and difficult defect samples, the system can gradually reduce false alarms in the field and enhance its adaptability to weak defects, boundary defects, low-contrast defects, and complex background fluctuations.

[0100] In some embodiments, a dual-engine deployment architecture is employed, decoupling image preprocessing and feature extraction from the prior feature library. Under this architecture, the prior feature library is not embedded in the deep model but is managed independently as an external file or database. Therefore, when adding new samples, updating templates, adjusting thresholds, switching products, or rolling back versions in the field, it is not necessary to re-export the deep feature extraction model; only updating the template library and configuration files is required to complete the deployment update.

[0101] The prior feature library stores information such as product number, camera number, workstation number, unique spatial identifier, normal prototype, normal prototype radius, local normal threshold, normalization parameters, channel weights, number of samples, template confidence, boundary type, and version number. During the training and export phase, a deployment configuration file is generated synchronously to record input size, image block segmentation rules, unique spatial identifier generation rules, local block strategy, output feature name, score fusion method, threshold strategy, template version, grayscale release strategy, and rollback strategy.

[0102] The prior feature library and deployment configuration are managed with independent versions, enabling the system to quickly switch between different prior feature libraries in scenarios with multiple products, multiple cameras, and multiple workstations. When on-site data changes, only the local templates, thresholds, and scoring parameters of the corresponding product, camera, workstation, or unique spatial identifier can be updated; when the effect of the new template is unstable, it can be quickly rolled back to a historical stable version.

[0103] By decoupling the management of prior feature libraries, deployment configurations, and feature engines, it supports online updates, partial updates, canary releases, version tracking, and rapid on-site recovery, avoiding the need to retrain or redeploy the complete deep model for each sample change. This improves the maintainability, stability, and engineering deployability of industrial high-resolution image anomaly detection systems.

[0104] refer to Figure 2 In some embodiments, a product anomaly detection device based on image location prior and reconstruction is provided, comprising: The feature library building module 201 is used to pre-build a prior feature library containing multiple sets of sample features divided into blocks according to spatial location; The segmentation module 202 is used to acquire the current product image and segment it according to spatial location to obtain multiple image blocks, and generate a unique spatial identifier for each image block; The initial scoring module 203 is used to perform multi-layer feature extraction on image patches using a pre-trained feature extractor, and to perform feature matching with a prior feature library based on a unique spatial identifier to obtain an initial anomaly score for the local patch. The aggregation module 204 is used to filter high-confidence normal local blocks based on the initial anomaly score, aggregate the high-confidence normal local blocks according to the unique spatial identifier, obtain the normal prototype inside the graph, and regard the remaining local blocks as local blocks to be confirmed. The reconstruction module 205 is used to reconstruct each local block with the corresponding normal prototype inside the graph, obtain the reconstruction response, and generate an anomaly heatmap based on the reconstruction response and the initial anomaly score. The defect determination module 206 is used to generate a defect area map based on the abnormal heat map.

[0105] Furthermore, the feature library building module 201 pre-builds a prior feature library containing multiple sets of features divided into blocks according to spatial location, including: Normal sample images are acquired and divided into blocks according to spatial location to obtain multiple sample image blocks, and a unique spatial identifier is assigned to each sample image block; Each sample image patch is input into a pre-trained feature extractor for multi-layer feature extraction to obtain multi-dimensional sample features at different feature layers. Multidimensional sample features from different feature layers are input into a pre-trained deep learning model for convolutional dimensionality reduction and channel attention weighting to obtain lightweight sample features for each feature layer. Based on the lightweight features of the samples, a set of normal prototypes, a set of normal prototype radii, and a set of local normal thresholds are established for each sample image block to form a priori feature library.

[0106] Furthermore, the feature library establishment module 201 establishes a normal prototype set, a normal prototype radius set, and a local normal threshold set for each sample image block based on the sample lightweight features, including: Based on the unique spatial identifier of the sample, the lightweight features of each feature layer of the same sample image block are combined as normal features to obtain the normal feature set of each sample image block. Clustering, core set sampling, or sparse sampling are performed on the normal feature set of each sample image patch to generate multiple normal prototypes at each feature layer, thus obtaining a normal prototype set. The distance between each normal feature and its corresponding normal prototype is calculated using a quantile statistical function, the normal prototype radius of each normal prototype is obtained, and a set of normal prototype radii for each sample image patch is formed. Normal sample verification images are acquired and segmented and multi-layer feature extraction is performed to obtain verification lightweight features of verification image blocks. Based on the verification lightweight features, the normal prototype set, and the normal prototype radius, the local normal threshold corresponding to the verification image block is calculated to form a local normal threshold set for each image block.

[0107] Furthermore, the initial scoring module 203 uses a pre-trained feature extractor to perform multi-layer feature extraction on the image patch. Based on the unique spatial identifier, the extracted multi-layer features are matched with the prior feature library to obtain the initial anomaly score of the local patch, including: A pre-trained feature extractor is used to extract local patches from the image patch, and multidimensional features of different feature layers are extracted based on the local patches; Multidimensional features from different feature layers are input into a pre-trained deep learning model for convolutional dimensionality reduction and channel attention weighting to obtain lightweight features for each feature layer. Based on the unique spatial identifier of the image patch, the normal prototype set and normal prototype radius set of the corresponding sample image patch are searched from the prior feature library for matching, and the initial anomaly score is calculated.

[0108] Furthermore, the aggregation module 204 filters high-confidence normal local blocks based on the initial anomaly score, including: Local blocks whose initial anomaly score is lower than the corresponding local normal threshold are selected as candidate local blocks; The stability of the neighborhood response and the validity of the spatial location of the candidate local block are judged, and the candidate local blocks with stable neighborhood response and valid spatial location are determined as high-confidence normal local blocks.

[0109] Furthermore, the aggregation module 204 aggregates high-confidence normal local blocks according to their unique spatial identifiers, including: Aggregate high-confidence normal local blocks with the same unique spatial identifier, and / or aggregate high-confidence normal local blocks within a preset neighborhood based on the unique spatial identifier to obtain the normal prototype inside the current graph.

[0110] Furthermore, the reconstruction module 205 reconstructs the local block to be confirmed and its corresponding normal prototype within the current graph to obtain a reconstruction response, including: Based on the unique spatial identifier of the image block to be confirmed, calculate the residual and similarity between the local block to be confirmed and the corresponding normal prototype in the current image. Combine the local block to be confirmed, the corresponding normal prototype in the current image, the residual, and the similarity to form the relationship vector between the local block to be confirmed and the normal prototype in the current image. The MLP reconstructor is used to process the relation vector to generate reconstruction weights for the local blocks to be confirmed. The reconstruction weights of the local blocks to be confirmed are weighted and combined with the corresponding normal prototypes inside the current graph to obtain reconstruction features; The difference between the reconstructed features and the local block to be confirmed is calculated to obtain the reconstruction response.

[0111] Furthermore, the reconstruction module generates an anomaly heatmap based on the reconstruction response and the initial anomaly score, including: Based on the initial anomaly score of the local block to be confirmed, the initial abnormal local block, the uncertain local block, and the initial normal local block are determined. For an initial abnormal local block, if the corresponding reconstruction response is lower than or equal to the preset response value, the corresponding initial abnormal local block is regarded as a suppressable false detection region; if the corresponding reconstruction response is higher than the preset response value, the corresponding initial abnormal local block is regarded as a high-confidence abnormal region. For uncertain local blocks and initial normal local blocks, if the corresponding reconstruction response is higher than the preset response value, the corresponding uncertain local blocks and initial normal local blocks are regarded as candidate abnormal regions of local inconsistency; if the corresponding reconstruction response is lower than or equal to the preset response value, the corresponding uncertain local blocks and initial normal local blocks are regarded as normal regions for reconstruction verification. Multi-source anomaly information is fused for suppressable false detection regions, high-confidence anomaly regions, and locally inconsistent candidate anomaly regions to generate a final anomaly score, and an anomaly heatmap is generated based on the final anomaly score.

[0112] Furthermore, the defect determination module 206 generates a defect region map based on the abnormal heat map, including: Based on the unique spatial identifier of each image block, the corresponding heatmap segmentation threshold is mapped to the current product image to form a spatial threshold map; the abnormal heatmap and the spatial threshold map are compared position by position to obtain a binary abnormal map, and morphological processing and connected component analysis are performed on the binary abnormal map to obtain a set of candidate defect regions; Each candidate defect region in the candidate defect region set is given a regional-level comprehensive score to obtain a regional-level comprehensive anomaly score. The regional-level comprehensive anomaly score is compared with the regional-level anomaly judgment threshold. Candidate defect regions that are not lower than the regional-level anomaly judgment threshold are determined as the final defect regions, and a defect region map is generated.

[0113] The product anomaly detection method and apparatus based on image location prior and reconstruction provided in the above embodiments have at least the following beneficial effects: (1) By using a position-aware modeling mechanism, the current product image to be detected is divided into blocks according to its spatial location. Each image block only needs to be compared with normal prior features of the same or related spatial locations, reducing redundant calculations. Based on the comparison results, the defect is confirmed, which can effectively reduce the amount of computation and avoid misjudgment caused by differences in texture, brightness, and background structure between different spatial regions, thereby improving detection accuracy. This mechanism is particularly suitable for industrial large-image detection scenarios with stable spatial structure, high resolution, and obvious local texture differences, such as printing, packaging, paper, and film materials. (2) Local normal thresholds, normal prototype radii, and normal prototypes are established for different sample image blocks, which can characterize the normal fluctuation range of different spatial regions and overcome the problem that a single global threshold cannot take into account both complex texture areas and low texture areas. At the same time, by using multiple normal prototypes and normal prototype radii, not only is the distance between the feature to be detected and the center of the normal prototype determined, but it is also determined whether it exceeds the normal variation range of the corresponding prototype, thereby improving the accuracy of normal boundary modeling; (3) After multidimensional feature extraction, a learnable 1×1 convolution dimensionality reduction, channel attention and multi-scale feature learnable fusion mechanism are introduced to adaptively weight the feature contributions of different levels, different channels and different spatial scales. This mechanism can reduce the storage volume of the prior feature library and the computational volume of anomaly matching, while retaining key features that are sensitive to texture destruction, edge changes, small defects and structural anomalies, and reducing the interference of invalid channels and redundant scale features on anomaly judgment, thereby taking into account detection accuracy, storage overhead and inference efficiency. (4) Based on prior feature library matching, a normal prototype reconstruction mechanism is introduced within the current graph. High-confidence normal local blocks with low external response, stable neighborhood and valid spatial location are selected from local blocks to form the normal prototype within the current graph. For regions that are judged as abnormal, uncertain or have local mutations in the prior, the normal prototype within the current graph is used to reconstruct them in a limited manner to determine whether the region can be reasonably explained by the normal structure of the current graph itself. Through this mechanism, regions with high prior response but can be stably reconstructed by the normal structure within the current graph can be identified as false responses caused by batch differences, light fluctuations, material changes or insufficient template coverage, thereby reducing false alarms. Regions with low prior response but difficult to be reconstructed by the normal structure within the current graph can be identified as candidate anomalies of local inconsistency in the current graph, thereby compensating for the prior omission of low contrast, small area, weak features or local mutation defects. This mechanism enables the system to not only rely on historical normal priors, but also to use the normal structure of the current graph itself for dynamic verification and self-consistency discrimination, thereby improving the stability and adaptability of anomaly detection under complex field conditions. (5) By integrating nearest neighbor distance, TopK nearest neighbor statistics, prototype radius excess, multi-channel anomaly response, template density, template confidence and regional statistical features, a multi-source anomaly scoring mechanism is formed to avoid the anomaly response being diluted in the global average or a single distance metric. This mechanism can enhance the detection capability of slight grayscale changes, small area defects, low contrast anomalies and local texture destruction. (6) When there are a few difficult samples on site, they are used as boundary calibration samples to optimize local threshold, normal prototype radius, channel weight, score fusion weight, etc. Difficult normal samples are used to expand normal boundaries, and difficult defect samples are used to enhance the abnormal response of low-scoring defects, weak defects and boundary defects, so as to take into account the open detection capability of unknown defects and the detection rate of known defects. In terms of engineering deployment, a dual-engine architecture of prior feature library and image processing is adopted, and the block rules, threshold strategy, back mapping rules and template version are solidified through deployment configuration contract. It supports fast switching, online updates, gray release and version rollback of multiple products, multiple cameras and multiple versions, reducing the detection deviation caused by the inconsistency between training end and deployment end parameters.

[0114] Although preferred embodiments of the invention have been described, those skilled in the art, upon learning the basic inventive concept, can make other changes and modifications to these embodiments. Therefore, the appended claims are intended to be interpreted as including both the preferred embodiments and all changes and modifications falling within the scope of the invention. Clearly, those skilled in the art can make various alterations and modifications to the invention without departing from its spirit and scope. Thus, if these modifications and modifications of the invention fall within the scope of the claims and their equivalents, the invention is also intended to include these modifications and modifications.

Claims

1. A product anomaly detection method based on image location prior and reconstruction, characterized in that, include: A prior feature library containing multiple sets of sample features divided according to spatial location is pre-established; The system acquires the current product image and divides it into blocks according to spatial location, obtaining multiple image blocks and generating a unique spatial identifier for each image block. A pre-trained feature extractor is used to extract multi-layer features from image patches, and the extracted multi-layer features are matched with a prior feature library based on unique spatial identifiers to obtain the initial anomaly score of the local patch. Based on the initial anomaly score, high-confidence normal local blocks are selected, and high-confidence normal local blocks are aggregated according to the unique spatial identifier to obtain the normal prototype inside the current graph. The remaining local blocks are then designated as local blocks to be confirmed. Reconstruct the local block to be confirmed with the corresponding normal prototype inside the current graph to obtain the reconstruction response, and generate an anomaly heatmap based on the reconstruction response and the initial anomaly score. Defect region maps are generated based on abnormal heat maps.

2. The method according to claim 1, characterized in that, A prior feature library containing features of multiple sets of samples divided according to spatial location is pre-established, including: Normal sample images are acquired and divided into blocks according to spatial location to obtain multiple sample image blocks, and a unique spatial identifier is assigned to each sample image block; Each sample image patch is input into a pre-trained feature extractor for multi-layer feature extraction to obtain multi-dimensional sample features at different feature layers. Multidimensional sample features from different feature layers are input into a pre-trained deep learning model for convolutional dimensionality reduction and channel attention weighting to obtain lightweight sample features for each feature layer. Based on the lightweight features of the samples, a set of normal prototypes, a set of normal prototype radii, and a set of local normal thresholds are established for each sample image block to form a priori feature library.

3. The method according to claim 2, characterized in that, Based on the aforementioned lightweight features, a normal prototype set, a normal prototype radius set, and a local normal threshold set are established for each sample image patch, including: Based on the unique spatial identifier of the sample, the lightweight features of each feature layer of the same sample image block are combined as normal features to obtain the normal feature set of each sample image block. Clustering, core set sampling, or sparse sampling are performed on the normal feature set of each sample image patch to generate multiple normal prototypes at each feature layer, thus obtaining a normal prototype set. The distance between each normal feature and its corresponding normal prototype is calculated using a quantile statistical function, the normal prototype radius of each normal prototype is obtained, and a set of normal prototype radii for each sample image patch is formed. Normal sample verification images are acquired and segmented and multi-layer feature extraction is performed to obtain verification lightweight features of verification image blocks. Based on the verification lightweight features, the normal prototype set, and the normal prototype radius, the local normal threshold corresponding to the verification image block is calculated to form a local normal threshold set for each image block.

4. The method according to claim 3, characterized in that, A pre-trained feature extractor is used to extract multi-layer features from image patches. Based on unique spatial identifiers, the extracted multi-layer features are matched with a prior feature library to obtain initial anomaly scores for local patches, including: A pre-trained feature extractor is used to extract local patches from the image patch, and multidimensional features of different feature layers are extracted based on the local patches; Multidimensional features from different feature layers are input into a pre-trained deep learning model for convolutional dimensionality reduction and channel attention weighting to obtain lightweight features for each feature layer. Based on the unique spatial identifier of the image patch, the normal prototype set and normal prototype radius set of the corresponding sample image patch are searched from the prior feature library for matching, and the initial anomaly score is calculated.

5. The method according to claim 3, characterized in that, High-confidence normal local blocks were selected based on the initial anomaly score, including: Local blocks whose initial anomaly score is lower than the corresponding local normal threshold are selected as candidate local blocks; The stability of the neighborhood response and the validity of the spatial location of the candidate local block are judged, and the candidate local block with stable neighborhood response and valid spatial location is determined as a high-confidence normal local block.

6. The method according to claim 1, characterized in that, Aggregate high-confidence normal local blocks according to their unique spatial identifiers, including: Aggregate high-confidence normal local blocks with the same unique spatial identifier, and / or aggregate high-confidence normal local blocks within a preset neighborhood based on the unique spatial identifier to obtain the normal prototype inside the current graph.

7. The method according to claim 2, characterized in that, Reconstruct the local block to be confirmed and its corresponding normal prototype within the current graph to obtain the reconstruction response, including: Based on the unique spatial identifier of the image block to be confirmed, calculate the residual and similarity between the local block to be confirmed and the corresponding normal prototype in the current image. Combine the local block to be confirmed, the corresponding normal prototype in the current image, the residual, and the similarity to form the relationship vector between the local block to be confirmed and the normal prototype in the current image. The MLP reconstructor is used to process the relation vector to generate reconstruction weights for the local blocks to be confirmed. The reconstruction weights of the local blocks to be confirmed are weighted and combined with the corresponding normal prototypes inside the current graph to obtain reconstruction features; The difference between the reconstructed features and the local block to be confirmed is calculated to obtain the reconstruction response.

8. The method according to claim 1, characterized in that, An anomaly heatmap is generated based on the reconstruction response and initial anomaly score, including: Based on the initial anomaly score of the local block to be confirmed, the initial abnormal local block, the uncertain local block, and the initial normal local block are determined. For an initial abnormal local block, if the corresponding reconstruction response is lower than or equal to the preset response value, the corresponding initial abnormal local block is regarded as a suppressable false detection region; if the corresponding reconstruction response is higher than the preset response value, the corresponding initial abnormal local block is regarded as a high-confidence abnormal region. For uncertain local blocks and initial normal local blocks, if the corresponding reconstruction response is higher than the preset response value, the corresponding uncertain local blocks and initial normal local blocks are regarded as candidate abnormal regions of local inconsistency; if the corresponding reconstruction response is lower than or equal to the preset response value, the corresponding uncertain local blocks and initial normal local blocks are regarded as normal regions for reconstruction verification. Multi-source anomaly information is fused for suppressable false detection regions, high-confidence anomaly regions, and locally inconsistent candidate anomaly regions to generate a final anomaly score, and an anomaly heatmap is generated based on the final anomaly score.

9. The method according to claim 1, characterized in that, Defect region maps are generated based on abnormal heatmaps, including: Based on the unique spatial identifier of each image block, the corresponding heatmap segmentation threshold is mapped to the current product image to form a spatial threshold map; the abnormal heatmap and the spatial threshold map are compared position by position to obtain a binary abnormal map, and morphological processing and connected component analysis are performed on the binary abnormal map to obtain a set of candidate defect regions; Each candidate defect region in the candidate defect region set is given a regional-level comprehensive score to obtain a regional-level comprehensive anomaly score. The regional-level comprehensive anomaly score is compared with the regional-level anomaly judgment threshold. Candidate defect regions that are not lower than the regional-level anomaly judgment threshold are determined as the final defect regions, and a defect region map is generated.

10. A product anomaly detection device based on image location prior and reconstruction, characterized in that, include: The feature library building module is used to pre-build a prior feature library containing multiple sets of sample features divided into blocks according to spatial location; The segmentation module is used to acquire the current product image and segment it according to spatial location to obtain multiple image blocks, and generate a unique spatial identifier for each image block; The initial scoring module is used to perform multi-layer feature extraction on image patches using a pre-trained feature extractor, and to perform feature matching with a prior feature library based on unique spatial identifiers to obtain the initial anomaly score of the local patch. The aggregation module is used to filter high-confidence normal local blocks based on the initial anomaly score, aggregate the high-confidence normal local blocks according to the unique spatial identifier, obtain the normal prototype inside the current graph, and treat the remaining local blocks as local blocks to be confirmed. The reconstruction module is used to reconstruct the local block to be confirmed with the corresponding normal prototype inside the current graph, obtain the reconstruction response, and generate an anomaly heatmap based on the reconstruction response and the initial anomaly score. The defect identification module generates a defect area map based on the anomaly heatmap.

Citation Information

Patent Citations

  • Industrial image anomaly detection method based on deep learning

    CN120976191A