An industrial anomaly detection method for small-batch zero-shot scenarios
By decoupling the matching adapter and the scoring adapter, the problem of unstable correspondence of the same physical component in small-batch zero-sample industrial anomaly detection is solved, achieving high-precision and high-efficiency anomaly detection, which is suitable for agile manufacturing scenarios.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- BEIHANG UNIV
- Filing Date
- 2026-05-23
- Publication Date
- 2026-07-17
AI Technical Summary
In small-batch, zero-sample industrial anomaly detection scenarios, existing technologies struggle to stably establish local correspondences between the same physical components when the target domain is unlabeled and there is no pre-existing normal template library. Furthermore, anomaly-anomaly shortcuts and semantic ambiguities exist, leading to a decrease in detection accuracy.
A decoupled matching adapter and scoring adapter are constructed through an offline assisted training phase. Block-level features are extracted using a frozen pre-trained visual backbone network for spatial alignment supervision and anomaly semantic learning. After offline training, the matching features and scoring features are decoupled during the online detection phase in the target domain.
It achieves high robustness and high precision anomaly detection under posture changes and spatial misalignment, making it suitable for agile manufacturing scenarios. It improves the ability to detect structural anomalies such as missing parts and damage, while maintaining the high efficiency of online detection.
Smart Images

Figure CN122415583A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of decoupled matching and scoring for small-batch zero-sample industrial anomaly detection, specifically involving an industrial anomaly detection method for small-batch zero-sample scenarios. Background Technology
[0002] Industrial anomaly detection is widely used in electronic manufacturing, precision assembly, automotive parts, display panels, semiconductor packaging, and other scenarios. Its goal is to automatically identify anomalies such as missing parts, damage, scratches, contamination, misalignment, and deformation with minimal manual annotation and line downtime costs. With the increasing demand for flexible and agile manufacturing, production sites are characterized by small batch sizes, rapid changeovers, and frequent product variations. Traditional detection solutions that rely on large libraries of normal and abnormal samples or stable templates for the target domain are becoming increasingly difficult to deploy quickly.
[0003] In actual production, many inspection tasks can only obtain a small number of unlabeled samples from the current batch to be inspected. Moreover, these samples often have differences in shooting posture, component position offset, partial occlusion, lighting changes, and product tolerance differences. If the single-image zero-sample anomaly detection method is still used, it is easy to fail to accurately identify structural anomalies due to the lack of reliable references. If the few-sample template matching method is used, it is often limited by the problems of collecting normal templates in advance, maintaining the template library, and templates becoming outdated.
[0004] Therefore, in recent years, a type of anomaly detection scheme has emerged that utilizes mutual scoring to compare unlabeled samples within the same batch. This type of scheme attempts to use other samples in the same batch as temporary references to reduce dependence on external template libraries. However, in practical applications, samples within a batch are often not strictly aligned. If "local matching" and "anomaly scoring" are performed simultaneously within a unified feature space, it is easily affected by both spatial misalignment and semantic ambiguity, leading to unstable anomaly scores, missed structural defects, and decreased online detection accuracy.
[0005] like Figure 1 As shown, in small-batch, zero-sample industrial anomaly detection scenarios, samples within a batch often have misalignment issues. Existing mutual scoring schemes are prone to failure modes such as anomaly-anomaly shortcuts and insufficient anomaly semantics. This invention proposes a technical approach to decouple matching and scoring to address these issues.
[0006] Technical solution of existing technology 1 Existing technology can be summarized as a zero-shot industrial anomaly detection scheme based on intra-batch sample mutual scoring. This type of scheme typically uses a pre-trained visual backbone network to extract local features of the image to be detected and the reference image. Then, within the feature space, it searches for similar local regions in other images of the current batch for each local region in the image to be detected. It constructs anomaly scores using the distance or dissimilarity between local regions, thereby obtaining pixel-level anomaly maps or image-level anomaly scores.
[0007] Specifically, this type of scheme generally includes the following process: First, acquire multiple unlabeled product images from the same detection batch; second, use a frozen pre-trained feature extractor to calculate the block-level feature representation of each image; then, for a certain image to be detected, use the remaining images in the same batch as a reference set, and perform nearest neighbor search or local similarity search on each local block in the image to be detected in the reference images; finally, construct an anomaly map based on the local distance obtained by matching, and aggregate the anomaly maps to obtain the anomaly score of the whole image.
[0008] The advantage of this type of approach is that it does not rely on explicit target domain annotations and can use the current batch as a temporary reference without having a pre-established normal template library, thus providing good deployment flexibility.
[0009] Disadvantages of existing technology 1 These approaches typically use the same set of features, unoptimized for anomaly semantics, to perform both "local matching" and "anomaly scoring" tasks. Because pre-trained features are more general visual representations and lack discriminative semantics specific to industrial anomalies, anomaly distances are prone to ambiguity. When samples within a batch exhibit pose changes, component offsets, or local deformations, existing techniques struggle to stably establish correspondences between "the same physical component," easily misassociating different components and resulting in overestimating or underestimating anomaly scores. Furthermore, when multiple anomaly regions exist, they may mismatch each other, forming anomaly-anomaly shortcuts. This causes the model to misinterpret anomalies as normal differences, leading to missed detections, especially for structural anomalies such as missing parts, fractures, and structural misalignments. Moreover, these approaches have very limited reference samples available in small-batch scenarios. If they still rely on uniform features for matching and scoring, the system lacks robustness and struggles to simultaneously meet the requirements of low-latency online detection and high-precision positioning.
[0010] Technical solution of existing technology 2 Existing technology two can be summarized as zero-shot or few-shot industrial anomaly detection schemes based on visual language models, cue learning, or external normal template libraries. These schemes typically enhance anomaly detection capabilities through textual cues, visual cues, prior templates, external normal sample libraries, or memory libraries, comparing the image to be detected with preset normal representations to output anomaly category, anomaly region, or anomaly score. While these schemes are effective in single-image zero-shot scenarios, most assume access to pre-prepared normal samples, memory features, or external semantic priors. Some schemes also assume a relatively stable spatial layout between the detected object and the template, thereby simplifying the anomaly comparison process.
[0011] Disadvantages of existing technology 2 Existing technologies often rely on offline-collected normal templates, additional text prompts, target domain priors, or external memory libraries, making them unsuitable for agile manufacturing scenarios characterized by rapid product switching, high template maintenance costs, and difficulty in collecting sufficient normal samples in advance. These solutions are more geared towards anomaly detection under conditions of single images or sufficient templates, and lack sufficient support for scenarios where "only unlabeled samples in the current small batch can be accessed, and samples within the batch are not aligned," making it difficult to fully utilize samples within the batch as temporary references. Furthermore, even with strong semantic representations, these solutions may not explicitly solve the core problem of "unreliable spatial correspondence." If it is not first determined whether the areas to be compared come from the same physical component, even with stronger scoring semantics, the final anomaly determination may still be affected by mismatches.
[0012] The technical problem to be solved by this invention is... 1. How to construct a reliable industrial anomaly detection solution when the target domain is unlabeled, there is no pre-existing normal template library, and only a small number of unlabeled samples in the current batch are accessible.
[0013] 2. How to stably establish local correspondences between the same physical components when there are attitude changes, spatial misalignments, and component position deviations in the samples within a batch.
[0014] 3. In anomaly detection, how to avoid the mutual interference between "local matching" and "anomaly scoring" in the existing unified feature framework, and suppress anomaly-anomaly shortcuts and semantic ambiguity problems.
[0015] 4. How to simultaneously output image-level anomaly scoring and pixel-level anomaly localization results while meeting the requirements for online detection efficiency. Summary of the Invention
[0016] To address the aforementioned technical problems, this invention proposes an industrial anomaly detection method for small-batch, zero-sample scenarios, achieving highly robust and accurate industrial anomaly detection.
[0017] The specific technical solution of this invention is as follows: An industrial anomaly detection method for small-batch, zero-sample scenarios includes the following steps: Step S1: In the offline assisted training phase, acquire auxiliary industrial image data that does not overlap with the product category or detection station of the target detection domain and has anomaly annotations; Step S2: Input the auxiliary industrial image data into the frozen pre-trained visual backbone network, extract multi-layer block-level visual features, and construct two mutually decoupled feature adaptation branches based on the multi-layer block-level visual features. The matching adapter outputs matching features to characterize whether the image blocks belong to the same physical component, and the scoring adapter outputs scoring features to characterize abnormal semantic differences between image blocks. Step S3: Construct spatial alignment supervision information for similar image pairs in the auxiliary training data. The spatial alignment supervision information includes foreground mask, image alignment transformation, and candidate corresponding parts set of the query image patch in the reference image. Step S4: Use the spatial alignment supervision information to train the matching adapter, so that the matching adapter learns the correspondence between the same parts of the query image patch and the reference image patch; Step S5: Based on the anomaly annotations in the auxiliary training data and the corresponding set of candidate identical parts, train the scoring adapter so that the scoring adapter learns the anomaly semantic measure of proximity between normal image patches and separation between image patch pairs containing anomalies; after the offline auxiliary training is completed, the parameters of the matching adapter and the scoring adapter remain fixed in the online detection stage of the target domain; Step S6: In the online detection stage of the target domain, acquire a batch of sample images to be detected, and for any image to be detected in the batch, construct an intra-batch reference set composed of other images to be detected in the same batch; Step S7: Input the image to be detected and each reference image in the batch reference set into the frozen pre-trained visual backbone network, and obtain matching features and scoring features through the trained matching adapter and scoring adapter respectively; Step S8: Based on the matching features between the image to be detected and the reference images in each batch, select the Top-k nearest neighbor candidate reference image blocks in the corresponding reference images for each query image block in the image to be detected, and obtain the soft correspondence weight between the query image block and the candidate reference image blocks according to the matching distance. Step S9: Within the same component alignment candidate range defined by the Top-k candidate reference image blocks, calculate the weighted anomaly semantic distance of the query image block relative to each candidate reference image block based on the scoring features, and fuse the anomaly scores of the same image to be detected relative to multiple intra-batch reference images to obtain a block-level anomaly map and an image-level anomaly score, and output the anomaly localization result and anomaly determination result.
[0018] The auxiliary training data consists of industrial image data that does not overlap with the product category or inspection station of the target detection domain. It can be derived from publicly available industrial anomaly detection datasets, historically labeled industrial inspection data, or other data collected from non-target product categories, and includes normal samples, abnormal samples, and their pixel-level or region-level anomaly annotations. This auxiliary training data is used only for offline training of the matching adapter and scoring adapter. During the online detection phase of the target domain, no labeled data is provided, the model is not fine-tuned for the target domain, and the pre-established normal template library of the target domain is not accessed. Only unlabeled sample images within the current detection batch are used as mutual references for batch zero-sample anomaly detection.
[0019] Preferably, step S2 includes the following steps: Step S21: Denote the input features of any layer to be processed as ; Step S22: Each adapter preferably adopts a trainable multi-scale Inception-style residual structure to enhance its adaptability to industrial anomaly detection tasks while maintaining the stability of pre-trained features. The calculation formula is as follows:
[0020]
[0021]
[0022]
[0023] Where F represents the block-level feature of the input adapter; F b Indicates Bottleneck characteristics after dimensionality reduction; , , These represent parallel convolution branches with different receptive fields; Indicates output projection convolution; Indicates the increment of residuals at multiple scales; G represents the residual scaling factor; m With g s These represent the matching adapter and the scoring adapter, respectively; P (I) With S (I) These represent matching features and rating features, respectively. Indicates the matching adapter, corresponding output It is used to characterize the physical component correspondence between local blocks and solve the problem of "where to match"; This indicates the rating adapter, corresponding to the output. It is used to characterize abnormal semantic differences between local blocks and solve the problem of "how to score".
[0024] Preferably, step S3 includes the following steps: Step S31: Generate a foreground mask based on the features of the pre-trained blocks. Supervision and computation loss are established only within the foreground block, thereby reducing the interference of the background region on part matching;
[0025] M(I) represents the input image I Foreground mask; This indicates that the mask is defined on a block-level grid with a height of h and a width of w; a value of 1 represents the foreground product area, and a value of 0 represents the background area. Step S32: For image pairs (A, B) of the same type, estimate the alignment transformation from image A to image B using an explicit registration algorithm. For query blocks Map its center position onto image B, and in the block grid of the reference image. Select several nearest neighbor discrete blocks at the mapped location to form a candidate set of corresponding components. , We select the 4 nearest neighbors to account for slight deformation and registration error:
[0026] in, This represents the set of candidate corresponding components for query block u; This represents the spatial alignment transformation from image A to image B; This indicates the position of the query block u after being mapped to image B by this transformation; Represents the set of foreground block indices for image B; Indicates in Select the four discrete blocks closest to the mapping location; Step S33: During training, affine enhancement G is further applied to images A and B. A and G B Then, the alignment matrix is updated synchronously to maintain the consistency between the enhanced image and the supervision information. For repetitive texture categories that are difficult to register stably, the candidate corresponding set of the same part is... It can be degraded into a nearest neighbor candidate set based on pre-trained features, and its calculation formula is as follows:
[0027] in, and These represent the enhanced images; and Let A and B represent the affine enhancement matrices applied to image A and image B, respectively. This represents the original alignment transformation obtained from the estimation before enhancement; express The inverse transform is used to keep the alignment supervision consistent in the enhanced image.
[0028] Preferably, step S4 includes the following steps: Step S41: Focus on optimizing only the matching adapter This allows it to learn pose-invariant local correspondences for image pairs (A, B) and query blocks. From the candidate corresponding set of parts Selecting from and The block with the smallest distance is used as the corresponding block for the positive sample. And randomly sample interference blocks from non-corresponding regions. The calculation formula is:
[0029]
[0030] in, Indicates that query block u is in The positive sample corresponding to the one with the smallest feature distance in the matching block. Indicates from Excluded Interference blocks obtained from post-sampling; and These represent block u in image A and block u in image B, respectively. v Matching features; Step S42: Set the target distance of positive sample pairs to 0 and the target distance of interference pairs to 1, and construct the matching loss function as follows:
[0031] This represents the training loss of the matching adapter; This represents the matching features of the block corresponding to the positive sample; This represents the matching features of the interfering block.
[0032] Preferably, step S5 includes the following steps: Step S51: Based on the candidate corresponding part set and the anomaly annotations in the auxiliary training data, further train the scoring adapter. This allows the system to learn anomaly semantic representations by downsampling pixel-level anomaly annotations in the auxiliary training data to a block grid, resulting in block-level labels. ;in This indicates that the corresponding block contains an exception. This indicates that the corresponding block is normal;
[0033] Step S52: For candidate pairs... If both the query block and the reference block are normal, then the target distance is... Set to 0; if at least one block is abnormal, then the target distance is... If we set it to 1, the scoring adapter only learns anomalous semantic differences within the aforementioned set of candidate components, and no longer undertakes the spatial alignment task. Its calculation formula is as follows:
[0034]
[0035] in, Representing an image I Medium block u Block-level exception markers; Indicates the distance between the target scores of candidate pairs u and v; when and When both are 0, It is 0 if it is not 1 otherwise; This represents the training loss of the scoring adapter; and These represent the scoring features of the query block and the reference block, respectively. Step S53: The scoring adapter is driven to make normal-normal pairs converge in the scoring feature space, while pairs containing anomalies maintain a large distance in the scoring feature space, thereby obtaining discriminative semantics for industrial defects.
[0036] Preferably, step S6 includes the following steps: Step S61: Obtain the image set of the next batch of samples to be tested in the same product category or the same workstation, and denot it as the batch image set; Step S62: For any image to be detected in the batch image set, construct the remaining images other than the image to be detected as the intra-batch reference set corresponding to the image to be detected.
[0037] Preferably, step S7 includes the following steps: Step S71: Input the image to be detected and the intra-batch reference image into the frozen pre-trained visual backbone network to obtain multi-layer block-level visual features; Step S72: For the input image I, in the selected feature layer Obtain the block feature tensor
[0038] Where h and w represent the height and width of the block mesh, respectively, and c represents the channel dimension; Step S73: Set lightweight adaptation mapping for each feature layer. After obtaining the adapted features Furthermore, the block features are normalized to reduce the impact of differences in feature scales at different levels on distance calculation, thus accommodating different defect scales and different component granularities. The calculation formula is as follows:
[0039]
[0040] in, Indicates the input image I In the l Normalized adaptation features of the u-th block position in the th feature layer; This represents the corresponding adaptation features before normalization; The L2 norm of this feature; It is a numerical stability constant used to avoid the denominator being zero; Step S74: Record the batch detected at the current moment as For any image to be detected Its reference set consists only of the remaining samples from the same batch and does not access the pre-established normal template library of the target domain. Distance-level fusion is used between multi-layer features to form a unified inter-block distance metric, calculated using the following formula:
[0041] in, This indicates the current small batch of tests. This represents the α-th image to be detected in this batch; express The batch reference set; B represents the number of reference images; B represents the batch size, therefore the number of reference images is B-1;
[0042] in, and Representing block u in image A and block u in image B respectively. v The fusion characteristics; This represents the set of feature layers participating in distance fusion. Indicates the number of floors.
[0043] Preferably, step S8 includes the following steps: Step S81: In the online detection stage of the target domain, the image to be detected... With each reference image The matching features between them are used to calculate the block-level distance matrix, assuming the block grid is flattened. The first block, then the second block The matching distance for each reference image is defined as follows:
[0044] This indicates that block u in the image to be detected is related to the first... i Zhang Reference Image R i Medium block v The distance between matching features; and These are the matching features of the two blocks; N = h × w represents the number of blocks after flattening. Step S82: For the query block In the reference image The top-k candidate blocks with the smallest distance are selected to form a candidate set. The corresponding weights are obtained by performing softmax normalization based on the matching distance. :
[0045]
[0046] In the reference image The middle is the query block. The selected Top-k nearest neighbor candidate set; Indicates index along the reference block v Choose the one with the smallest distance. k One candidate; Indicates candidate block v The soft correspondence weight for query block u; This represents the candidate block traversal variable in the normalized denominator; Represents an exponential function; Step S83: The weights of steps S81 and S82 only reflect the confidence of the same part alignment given by the matching adapter, and do not directly use the scoring adapter to participate in the nearest neighbor screening, thereby ensuring that the candidate set first satisfies the spatial constraint of "part consistency".
[0047] Preferably, step S9 includes the following steps: Step S91: For query block u, this invention does not directly calculate the anomaly score in the global block space, but only in the Top-k soft-corresponding candidate set of the same component obtained in step S8. Within the reference image R, a weighted anomaly semantic distance is calculated using the scoring features. i The anomaly score relative to this query block is calculated using the following formula:
[0048] in, Indicates the first i Zhang Reference Image R i For reference, query the anomaly score of block u; and These represent the scoring features of the block to be detected and the candidate reference block, respectively; this formula only applies when... Internal Press Weighted calculation of anomaly semantic distance; Step S92: The anomaly scores of all reference images are fused using the principle of "most likely normal interpretation first" to obtain a block-level anomaly map. Then, the maximum block-level anomaly score is taken as the image-level anomaly score. The calculation formula is as follows:
[0049]
[0050] Indicates the image to be detected A The final block-level exception response at block u; This means that the minimum anomaly score is selected as the most likely normal interpretation among all intra-batch reference images; Represents image-level anomaly score; This means selecting the largest abnormal response from all block locations for overall image anomaly detection; Step S93: Since the scoring is only performed within the set of soft corresponding candidates of the same component selected by the matching adapter, rather than directly scoring in the global nearest neighbor space, it can effectively suppress the abnormal-abnormal shortcut formed by the erroneous matching of an abnormal region to another abnormal region, and reduce the interference of spatial misalignment on the abnormal score.
[0051] Step S94: Based on the obtained block-level anomaly graph This can be upsampled or mapped into a pixel-level anomaly heatmap to achieve anomaly region localization; simultaneously, based on image-level anomaly scores... Compare with a preset threshold and output a normal / abnormal judgment result, a sorting signal, or a re-inspection alarm message; Step S95: By combining the block-level positioning results with the image-level anomaly score, the dual requirements of positioning and judgment in online industrial inspection can be met.
[0052] The beneficial effects of the industrial anomaly detection method for small-batch, zero-sample scenarios proposed in this invention are as follows: 1. This invention does not rely on a pre-established target domain normal template library, memory library or target domain annotation data. It can complete anomaly detection using only unlabeled samples in the current detection batch, making it more suitable for agile manufacturing scenarios where products are quickly switched and templates are difficult to maintain.
[0053] 2. This invention decouples "part matching" and "anomaly scoring" into two functionally different adaptation branches, allowing the matching adapter to focus on learning pose-robust physical correspondences and the scoring adapter to focus on learning anomaly discrimination semantics, thereby reducing the conflict that arises when a unified feature space undertakes two types of tasks simultaneously.
[0054] 3. This invention, by first establishing a Top-k soft correspondence relationship for the same component and then performing anomaly scoring on the corresponding area, can effectively avoid anomaly-anomaly shortcuts and improve the detection capability for structural anomalies such as missing parts, damage, and structural misalignment.
[0055] 4. This invention can maintain good detection stability even when there are pose changes, local offsets and spatial misalignment of samples within a batch, thus improving spatial robustness in online detection scenarios.
[0056] 5. This invention improves anomaly detection accuracy while maintaining inference efficiency on par with existing mutual scoring methods through a lightweight adapter and a restricted candidate scoring mechanism, making it suitable for online industrial inspection deployment.
[0057] 6. According to the experimental results in the paper, the proposed technical approach outperforms existing comparative methods on multiple industrial anomaly detection benchmark datasets, indicating that the invention is not only theoretically sound but also has strong engineering application value. Attached Figure Description
[0058] Figure 1 This diagram illustrates the challenges of small-batch, zero-sample industrial anomaly detection and the solution proposed in this invention.
[0059] Figure 2 The flowchart of the decoupled matching and scoring method of the present invention includes an offline assisted training stage in stage one and an online target domain detection stage in stage two. Detailed Implementation
[0060] The present invention will now be described in detail with reference to the accompanying drawings and specific embodiments.
[0061] The specific embodiments of the present invention are described below to enable those skilled in the art to understand the present invention. However, it should be understood that the present invention is not limited to the scope of the specific embodiments. For those skilled in the art, various changes are obvious as long as they are within the spirit and scope of the present invention as defined and determined by the appended claims. All inventions utilizing the concept of the present invention are protected.
[0062] This invention proposes an industrial anomaly detection method for small-batch, zero-sample scenarios. The method includes an offline assisted training phase (phase one) and an online target domain detection phase (phase two). For example... Figure 2 As shown, in the offline assisted training phase, labeled auxiliary industrial image data that does not overlap with the target detection domain is used for auxiliary training data preprocessing to construct foreground masks, spatial alignment transformations, and candidate corresponding sets of the same parts. Matching adapters and scoring adapters are then trained separately. In the online target domain detection phase, unlabeled samples in the current detection batch are used as a temporary reference set. The image to be detected and the reference images within the batch are input into the frozen pre-trained visual backbone network. Matching features and scoring features are obtained through the trained matching adapter and scoring adapter, respectively. First, a Top-k soft correspondence relationship of the same parts is established based on the matching features. Then, within the candidate range defined by the soft correspondence relationship, the anomaly semantic distance is calculated based on the scoring features, ultimately obtaining a block-level anomaly map and an image-level anomaly score. Notably, in the online target domain detection phase, the parameters of the matching adapter and scoring adapter remain fixed, and no target domain labeled data is used for training or fine-tuning.
[0063] The method of the present invention includes the following steps: Phase 1: Offline Assisted Training Phase Step S1: Obtain auxiliary training data During the offline assisted training phase, auxiliary industrial image data that does not overlap with the product category or detection station of the target detection domain and has anomaly annotations is acquired. Step S2: Extract auxiliary training features and construct a decoupled dual-branch adapter Images from auxiliary industrial image data are input into a frozen pre-trained visual backbone network to obtain multi-layer block-level visual features; for input image I, in the selected feature layer... Obtain the block feature tensor ;
[0064] Where h and w represent the height and width of the block grid, respectively, and c represents the channel dimension; a lightweight adaptation mapping is set for each feature layer. After obtaining the adapted features Furthermore, the block features are normalized to reduce the impact of differences in feature scales at different levels on distance calculation, thus accommodating different defect scales and different component granularities. The calculation formula is as follows:
[0065]
[0066] in, Indicates the input image I In the l Normalized adaptation features of the u-th block position in the th feature layer; This represents the corresponding adaptation features before normalization; The L2 norm of this feature; This is a numerical stability constant used to avoid zero denominators; distance-level fusion is employed between multi-layer features to form a unified inter-block distance metric, the calculation formula of which is:
[0067] in, and Representing block u in image A and block u in image B respectively. v The fusion characteristics; This represents the set of feature layers participating in distance fusion. Indicates the number of floors.
[0068] Let the input features of any layer to be processed be denoted as Each adapter preferably employs a trainable multi-scale Inception-style residual structure to enhance its adaptability to industrial anomaly detection tasks while maintaining the stability of pre-trained features. The calculation formula is as follows:
[0069]
[0070]
[0071]
[0072] Where F represents the block-level feature of the input adapter; F b Indicates Bottleneck characteristics after dimensionality reduction; , , These represent parallel convolution branches with different receptive fields; Indicates output projection convolution; Indicates the increment of residuals at multiple scales; G represents the residual scaling factor;m With g s These represent the matching adapter and the scoring adapter, respectively; P (I) With S (I) These represent matching features and rating features, respectively. Indicates the matching adapter, corresponding output It is used to characterize the physical component correspondence between local blocks and solve the problem of "where to match"; This indicates the rating adapter, corresponding to the output. It is used to characterize abnormal semantic differences between local blocks and solve the problem of "how to score".
[0073] Step S3: Construct spatial alignment supervision information Generate a foreground mask based on features from pre-trained blocks. Supervision and computation loss are established only within the foreground block, thereby reducing the interference of the background region on part matching;
[0074] M(I) represents the input image I Foreground mask; The mask is defined on a block-level grid with height h and width w; a value of 1 represents the foreground product area, and a value of 0 represents the background area; for the same image pair (A, B), the alignment transformation from image A to image B is estimated using an explicit registration algorithm. For query blocks Map its center position onto image B, and in the block grid of the reference image. Select several nearest neighbor discrete blocks at the mapped location to form a candidate set of corresponding components. , We select the 4 nearest neighbors to account for slight deformation and registration error:
[0075] in, This represents the set of candidate corresponding components for query block u; This represents the spatial alignment transformation from image A to image B; This indicates the position of the query block u after being mapped to image B by this transformation; Represents the set of foreground block indices for image B; Indicates in The four discrete blocks closest to the mapping location are selected; during training, affine enhancement G is further applied to images A and B. A and G BThen, the alignment matrix is updated synchronously to maintain the consistency between the enhanced image and the supervision information. For repetitive texture categories that are difficult to register stably, the candidate corresponding set of the same part is... It can be degraded into a nearest neighbor candidate set based on pre-trained features, and its calculation formula is as follows:
[0076] in, and These represent the enhanced images; and Let A and B represent the affine enhancement matrices applied to image A and image B, respectively. This represents the original alignment transformation obtained from the estimation before enhancement; express The inverse transform is used to keep the alignment supervision consistent in the enhanced image.
[0077] Step S4: Train the matching adapter Focus only on optimizing the matching adapter This allows it to learn pose-invariant local correspondences for image pairs (A, B) and query blocks. From the candidate corresponding set of parts Selecting from and The block with the smallest distance is used as the corresponding block for the positive sample. And randomly sample interference blocks from non-corresponding regions. The calculation formula is:
[0078]
[0079] in, Indicates that query block u is in The positive sample corresponding to the one with the smallest feature distance in the matching block. Indicates from Excluded Interference blocks obtained from post-sampling; and These represent block u in image A and block u in image B, respectively. v The matching features are determined by setting the target distance of positive sample pairs to 0 and the target distance of interference pairs to 1, and the matching loss function is constructed as follows:
[0080] This represents the training loss of the matching adapter; This represents the matching features of the block corresponding to the positive sample; This represents the matching features of the interfering block.
[0081] Step S5: Training the scoring adapter Based on the candidate corresponding component set and the anomaly annotations in the auxiliary training data, the scoring adapter is further trained. This allows the system to learn anomaly semantic representations by downsampling pixel-level anomaly annotations in the auxiliary training data to a block grid, resulting in block-level labels. ;in This indicates that the corresponding block contains an exception. This indicates that the corresponding block is normal;
[0082] For candidate responses If both the query block and the reference block are normal, then the target distance is... Set to 0; if at least one block is abnormal, then the target distance is... If we set it to 1, the scoring adapter only learns anomalous semantic differences within the aforementioned set of candidate components, and no longer undertakes the spatial alignment task. Its calculation formula is as follows:
[0083]
[0084] in, Representing an image I Medium block u Block-level exception markers; Indicates the distance between the target scores of candidate pairs u and v; when and When both are 0, It is 0 if it is not 1 otherwise; This represents the training loss of the scoring adapter; and These represent the scoring features of the query block and the reference block, respectively. The scoring adapter is driven to make normal-normal pairs converge in the scoring feature space, while pairs containing anomalies maintain a large distance in the scoring feature space, thereby obtaining discriminative semantics for industrial defects.
[0085] At this point, the offline assisted training phase is complete, and the trained matching adapter and scoring adapter are obtained; in the subsequent online target domain detection phase, the parameters of the two adapters remain fixed.
[0086] Phase Two: Online Target Domain Detection Phase Step S6: Obtain the detection batch and construct the intra-batch reference set A batch image set is obtained for the current time and for the same product category or workstation. This set is denoted as the batch image set. The number of images in the batch image set can be 4, 8, or other suitable numbers for online detection, but is not limited to these. For any image to be detected in the batch image set, the remaining images (excluding the image to be detected) are used to construct the corresponding intra-batch reference set. In other words, this invention does not rely on a pre-acquisition template library, but directly utilizes the remaining samples in the current batch as a temporary reference.
[0087] Record the current batch of tests as For any image to be detected Its intra-batch reference set is defined as:
[0088] in, This indicates the current small batch of tests. This represents the α-th image to be detected in this batch; express The batch reference set; B represents the number of reference images; B represents the batch size, therefore the number of reference images is B-1; Step S7: Input the image to be detected and each reference image in the batch reference set into the frozen pre-trained visual backbone network, and obtain matching features and scoring features through the trained matching adapter and scoring adapter respectively; Step S8: Establish Top-k soft correspondences based on matching features In the online target domain detection phase, the image to be detected... With each reference image The matching features between them are used to calculate the block-level distance matrix, assuming the block grid is flattened. The first block, then the second block The matching distance for each reference image is defined as follows:
[0089] This indicates that block u in the image to be detected is related to the first... i Zhang Reference Image R i Medium block v The distance between matching features; and These are the matching features of the two blocks respectively; N = h × w represents the number of blocks after flattening; for the query block In the reference image The top-k candidate blocks with the smallest distance are selected to form a candidate set. The corresponding weights are obtained by performing softmax normalization based on the matching distance. :
[0090]
[0091] In the reference image The middle is the query block. The selected Top-k nearest neighbor candidate set; Indicates index along the reference block v Choose the one with the smallest distance. k One candidate; Indicates candidate block v The soft correspondence weight for query block u; This represents the candidate block traversal variable in the normalized denominator; The exponential function is represented; the weights of steps S81 and S82 only reflect the confidence of the same part alignment given by the matching adapter, and the scoring adapter is not directly used to participate in the nearest neighbor screening, thereby ensuring that the candidate set first satisfies the spatial constraint of "part consistency".
[0092] Step S9: Perform anomaly scoring based on scoring features and fuse the output. For query block u, this invention does not directly calculate the anomaly score in the global block space, but only in the Top-k soft-corresponding candidate set of the same component obtained in step S8. Within the reference image R, a weighted anomaly semantic distance is calculated using the scoring features. i The anomaly score relative to this query block is calculated using the following formula:
[0093] in, Indicates the first i Zhang Reference Image R i For reference, query the anomaly score of block u; and These represent the scoring features of the block to be detected and the candidate reference block, respectively; this formula only applies when... Internal Press Weighted semantic distance of anomalies is calculated; the anomaly scores of all reference images are fused using the principle of "most likely normal interpretation first" to obtain a block-level anomaly map. Then, the maximum block-level anomaly score is taken as the image-level anomaly score. The calculation formula is as follows:
[0094]
[0095] This represents the final block-level anomaly response of the image A to be detected at block u; This means that the minimum anomaly score is selected as the most likely normal interpretation among all intra-batch reference images; Represents image-level anomaly score; This means that the maximum abnormal response is selected from all block locations for the entire image anomaly determination; since the scoring is only performed within the set of soft corresponding candidates of the same component selected by the matching adapter, rather than directly scoring in the global nearest neighbor space, it can effectively suppress the anomaly-anomaly shortcut formed by an anomaly region being incorrectly matched to another anomaly region, and reduce the interference of spatial misalignment on the anomaly score.
[0096] Based on the obtained block-level anomaly diagram This can be upsampled or mapped into a pixel-level anomaly heatmap to achieve anomaly region localization; simultaneously, based on image-level anomaly scores... It compares the results with a preset threshold and outputs a normal / abnormal judgment result, a sorting signal, or a re-inspection alarm message. By combining the block-level positioning result with the image-level abnormal score, it can meet the dual needs of positioning and judgment in online industrial inspection.
[0097] In addition to the preferred embodiments described above, the present invention can also employ the following alternative embodiments, which can equally achieve the objectives of the present invention: 1. The construction method of the intra-batch reference set is not limited to "other samples in the same batch". It can also be extended to sliding window samples adjacent to the current batch time, several samples that pass through the same production line continuously, or screened unlabeled online cache samples.
[0098] 2. The construction of spatial alignment supervision information is not limited to explicit registration of auxiliary data. It can also be achieved by generating pseudo-pairs through geometric transformation, constructing positive samples of the same part through pose enhancement, and establishing local alignment relationships based on key points or segmentation results.
[0099] 3. The specific network structures of the matching adapter and the scoring adapter are not limited to multi-scale convolutional structures, but can also use multilayer perceptrons, attention structures, residual bottleneck structures or other lightweight adaptation structures.
[0100] 4. Similarity measures in Top-k soft correspondences are not limited to Euclidean distance; cosine distance, Mahalanobis distance, learnable distance functions, or combinations thereof can also be used.
[0101] 5. The fusion method for multiple reference results is not limited to taking the minimum value. Weighted average, quantile fusion, robust statistical fusion or hierarchical fusion methods can also be used.
[0102] 6. The abnormal output format is not limited to block-level abnormal maps and image-level abnormal scores. It can also be extended to pixel-level segmentation maps, component-level abnormal location results, defect type prompts, or re-inspection priority scores.
[0103] 7. This invention can be applied to rigid or semi-rigid industrial products, and can also be applied to industrial inspection objects with certain non-rigid deformation after appropriate alignment and modeling.
[0104] Key technical points and points to be protected in this invention 1. Construct an intra-batch reference set using unlabeled samples from the current testing batch, without relying on a pre-established normal template library or target domain annotation data.
[0105] 2. Based on a unified pre-trained visual backbone, a matching adapter and a scoring adapter are set up separately to decouple the local matching task from the anomaly scoring task at the feature level.
[0106] 3. During the offline assisted training phase, the matching adapter is trained through spatial alignment supervision or same-part supervision to enable it to learn local physical correspondences that are invariant to the pose.
[0107] 4. In the online detection phase of the target domain, a Top-k soft correspondence relationship between the same components is established based on the matching features, and the abnormal semantic distance is calculated only within the aligned candidate region using the scoring features.
[0108] 5. By using the "match first, score later" decoupled reasoning mechanism in the online detection stage of the target domain to suppress anomaly-anomaly shortcuts, the ability to detect structural anomalies in scenarios where samples are not aligned within a batch is improved.
Claims
1. An industrial anomaly detection method for small-batch, zero-sample scenarios, characterized in that, Includes the following steps: Step S1: In the offline assisted training phase, acquire auxiliary industrial image data that does not overlap with the product category or detection station of the target detection domain and has anomaly annotations; Step S2: Input the auxiliary industrial image data into the frozen pre-trained visual backbone network, extract multi-layer block-level visual features, and construct two mutually decoupled feature adaptation branches based on the multi-layer block-level visual features. The matching adapter outputs matching features to characterize whether the image blocks belong to the same physical component, and the scoring adapter outputs scoring features to characterize abnormal semantic differences between image blocks. Step S3: Construct spatial alignment supervision information for similar image pairs in the auxiliary training data. The spatial alignment supervision information includes foreground mask, image alignment transformation, and candidate corresponding parts set of the query image patch in the reference image. Step S4: Use the spatial alignment supervision information to train the matching adapter, so that the matching adapter learns the correspondence between the same parts of the query image patch and the reference image patch; Step S5: Based on the anomaly annotations in the auxiliary training data and the corresponding set of candidate identical parts, train the scoring adapter so that the scoring adapter learns the anomaly semantic measure of proximity between normal image patches and separation between image patch pairs containing anomalies; after the offline auxiliary training is completed, the parameters of the matching adapter and the scoring adapter remain fixed in the online detection stage of the target domain; Step S6: In the online detection stage of the target domain, acquire a batch of sample images to be detected, and for any image to be detected in the batch, construct an intra-batch reference set composed of other images to be detected in the same batch; Step S7: Input the image to be detected and each reference image in the batch reference set into the frozen pre-trained visual backbone network, and obtain matching features and scoring features through the trained matching adapter and scoring adapter respectively; Step S8: Based on the matching features between the image to be detected and the reference images in each batch, select the Top-k nearest neighbor candidate reference image blocks in the corresponding reference images for each query image block in the image to be detected, and obtain the soft correspondence weight between the query image block and the candidate reference image blocks according to the matching distance. Step S9: Within the same component alignment candidate range defined by the Top-k candidate reference image blocks, calculate the weighted anomaly semantic distance of the query image block relative to each candidate reference image block based on the scoring features, and fuse the anomaly scores of the same image to be detected relative to multiple intra-batch reference images to obtain a block-level anomaly map and an image-level anomaly score, and output the anomaly localization result and anomaly determination result. The auxiliary training data consists of industrial image data that does not overlap with the product category or inspection station of the target detection domain. It can be derived from publicly available industrial anomaly detection datasets, historically labeled industrial inspection data, or other data collected from non-target product categories, and includes normal samples, abnormal samples, and their pixel-level or region-level anomaly annotations. This auxiliary training data is used only for offline training of the matching adapter and scoring adapter. During the online detection phase of the target domain, no labeled data is provided, the model is not fine-tuned for the target domain, and the pre-established normal template library of the target domain is not accessed. Only unlabeled sample images within the current detection batch are used as mutual references for batch zero-sample anomaly detection.
2. The industrial anomaly detection method for small-batch, zero-sample scenarios according to claim 1, characterized in that, Step S2 includes the following steps: Step S21: Denote the input features of any layer to be processed as ; Step S22: Each adapter preferably adopts a trainable multi-scale Inception-style residual structure to enhance its adaptability to industrial anomaly detection tasks while maintaining the stability of pre-trained features. The calculation formula is as follows: , , , , Where F represents the block-level feature of the input adapter; F b Indicates Bottleneck characteristics after dimensionality reduction; , , These represent parallel convolution branches with different receptive fields; Indicates output projection convolution; Indicates the increment of residuals at multiple scales; G represents the residual scaling factor; m With g s Let P(I) and S(I) represent the matching adapter and the scoring adapter, respectively; let P(I) and S(I) represent the matching feature and the scoring feature, respectively. Indicates the matching adapter, corresponding output It is used to characterize the physical component correspondence between local blocks and solve the problem of "where to match"; This indicates the rating adapter, corresponding to the output. It is used to characterize abnormal semantic differences between local blocks and solve the problem of "how to score".
3. The industrial anomaly detection method for small-batch, zero-sample scenarios according to claim 1, characterized in that, Step S3 includes the following steps: Step S31: Generate a foreground mask based on the features of the pre-trained blocks. Supervision and computation loss are established only within the foreground block, thereby reducing the interference of the background region on part matching; , M(I) represents the foreground mask of the input image I; This indicates that the mask is defined on a block-level grid with a height of h and a width of w; a value of 1 represents the foreground product area, and a value of 0 represents the background area. Step S32: For image pairs (A, B) of the same type, estimate the alignment transformation from image A to image B using an explicit registration algorithm. For query blocks Map its center position onto image B, and in the block grid of the reference image. Select several nearest neighbor discrete blocks at the mapped location to form a candidate set of corresponding components. , We select the 4 nearest neighbors to account for slight deformation and registration error: , in, This represents the set of candidate corresponding components for query block u; This represents the spatial alignment transformation from image A to image B; This indicates the position of the query block u after being mapped to image B by this transformation; Represents the set of foreground block indices for image B; Indicates in Select the four discrete blocks closest to the mapping location; Step S33: During training, affine enhancement G is further applied to images A and B. A and G B Then, the alignment matrix is updated synchronously to maintain the consistency between the enhanced image and the supervision information. For repetitive texture categories that are difficult to register stably, the candidate corresponding set of the same part is... It can be degraded into a nearest neighbor candidate set based on pre-trained features, and its calculation formula is as follows: , in, and These represent the enhanced images; and Let A and B represent the affine enhancement matrices applied to image A and image B, respectively. This represents the original alignment transformation obtained from the estimation before enhancement; express The inverse transform is used to keep the alignment supervision consistent in the enhanced image.
4. The industrial anomaly detection method for small-batch, zero-sample scenarios according to claim 1, characterized in that, Step S4 includes the following steps: Step S41: Focus on optimizing the matching adapter This allows it to learn pose-invariant local correspondences for image pairs (A, B) and query blocks. From the candidate corresponding set of components Selecting from and The block with the smallest distance is used as the corresponding block for the positive sample. And randomly sample interference blocks from non-corresponding regions. The calculation formula is: , , in, Indicates that query block u is in The positive sample corresponding to the one with the smallest feature distance in the matching block. Indicates from Excluded Interference blocks obtained from post-sampling; and Let u and v represent the matching features of block u in image A and block v in image B, respectively. Step S42: Set the target distance of positive sample pairs to 0 and the target distance of interference pairs to 1, and construct the matching loss function as follows: , This represents the training loss of the matching adapter; This represents the matching features of the block corresponding to the positive sample; This represents the matching features of the interfering block.
5. The industrial anomaly detection method for small-batch, zero-sample scenarios according to claim 1, characterized in that, Step S5 includes the following steps: Step S51: Based on the candidate corresponding part set and the anomaly annotations in the auxiliary training data, further train the scoring adapter. This allows the system to learn anomaly semantic representations by downsampling pixel-level anomaly annotations in the auxiliary training data to a block grid, resulting in block-level labels. ;in This indicates that the corresponding block contains an exception. This indicates that the corresponding block is normal; , Step S52: For candidate pairs... If both the query block and the reference block are normal, then the target distance is... Set to 0; if at least one block is abnormal, then the target distance is... If we set it to 1, the scoring adapter only learns anomalous semantic differences within the aforementioned set of candidate components, and no longer undertakes the spatial alignment task. Its calculation formula is as follows: , , in, This represents the block-level anomaly marker for block u in image I; Indicates the distance between the target scores of candidate pairs u and v; when and When both are 0, It is 0 if it is not 1 otherwise; This represents the training loss of the scoring adapter; and These represent the scoring features of the query block and the reference block, respectively. Step S53: The scoring adapter is driven to make normal-normal pairs converge in the scoring feature space, while pairs containing anomalies maintain a large distance in the scoring feature space, thereby obtaining discriminative semantics for industrial defects.
6. The industrial anomaly detection method for small-batch, zero-sample scenarios according to claim 1, characterized in that, Step S6 includes the following steps: Step S61: Obtain the image set of the next batch of samples to be tested in the same product category or the same workstation, and denot it as the batch image set; Step S62: For any image to be detected in the batch image set, construct the remaining images other than the image to be detected as the intra-batch reference set corresponding to the image to be detected.
7. The industrial anomaly detection method for small-batch, zero-sample scenarios according to claim 1, characterized in that, Step S7 includes the following steps: Step S71: Input the image to be detected and the intra-batch reference image into the frozen pre-trained visual backbone network to obtain multi-layer block-level visual features; Step S72: For the input image I, in the selected feature layer Obtain the block feature tensor , Where h and w represent the height and width of the block mesh, respectively, and c represents the channel dimension; Step S73: Set lightweight adaptation mapping for each feature layer. After obtaining the adapted features Furthermore, the block features are normalized to reduce the impact of differences in feature scales at different levels on distance calculation, thus accommodating different defect scales and different component granularities. The calculation formula is as follows: , , in, This represents the normalized adaptation feature of the input image I at the l-th feature layer and the u-th block position; This represents the corresponding adaptation features before normalization; The L2 norm of this feature; It is a numerical stability constant used to avoid the denominator being zero; Step S74: Record the batch detected at the current moment as For any image to be detected Its reference set consists only of the remaining samples from the same batch and does not access the pre-established normal template library of the target domain. Distance-level fusion is used between multi-layer features to form a unified inter-block distance metric, calculated using the following formula: , in, This indicates the current small batch of tests. This represents the α-th image to be detected in this batch; express The batch reference set; B represents the number of reference images; B represents the batch size, therefore the number of reference images is B-1; , in, and These represent the fusion features of block u in image A and block v in image B, respectively. This represents the set of feature layers participating in distance fusion. Indicates the number of floors.
8. The industrial anomaly detection method for small-batch, zero-sample scenarios according to claim 1, characterized in that, Step S8 includes the following steps: Step S81: In the online detection stage of the target domain, the image to be detected... With each reference image The matching features between them are used to calculate the block-level distance matrix, assuming the block grid is flattened. The first block, then the second block The matching distance for each reference image is defined as follows: , This indicates that block u in the image to be detected is compared with the i-th reference image R. i The matching feature distance of the middle block v; and These are the matching features of the two blocks; N = h × w represents the number of blocks after flattening. Step S82: For the query block In the reference image The top-k candidate blocks with the smallest distance are selected to form a candidate set. The corresponding weights are obtained by performing softmax normalization based on the matching distance. : , , In the reference image The middle is the query block. The selected Top-k nearest neighbor candidate set; This indicates selecting the k candidates with the smallest distance along the reference block index v; This represents the soft correspondence weight of candidate block v with respect to query block u; This represents the candidate block traversal variable in the normalized denominator; Represents an exponential function; Step S83: The weights of steps S81 and S82 only reflect the confidence of the same part alignment given by the matching adapter, and do not directly use the scoring adapter to participate in the nearest neighbor screening, thereby ensuring that the candidate set first satisfies the spatial constraint of "part consistency".
9. The industrial anomaly detection method for small-batch, zero-sample scenarios according to claim 1, characterized in that, Step S9 includes the following steps: Step S91: For query block u, this invention does not directly calculate the anomaly score in the global block space, but only in the Top-k soft-corresponding candidate set of the same component obtained in step S8. Within the query block, the weighted anomaly semantic distance is calculated using the scoring features to obtain the anomaly score of the reference image Ri relative to the query block. The calculation formula is as follows: , in, This represents the anomaly score of query block u when the i-th reference image Ri is used as a reference; and These represent the scoring features of the block to be detected and the candidate reference block, respectively; this formula only applies to... Internal Press Weighted calculation of anomaly semantic distance; Step S92: The anomaly scores of all reference images are fused using the principle of "most likely normal interpretation first" to obtain a block-level anomaly map. Then, the maximum block-level anomaly score is taken as the image-level anomaly score. The calculation formula is as follows: , , This represents the final block-level anomaly response of the image A to be detected at block u; This means that the minimum anomaly score is selected as the most likely normal interpretation among all intra-batch reference images; Represents image-level anomaly score; This indicates that the maximum abnormal response is selected from all block locations for overall image anomaly detection; Step S93: Since the scoring is only performed within the set of soft corresponding candidates of the same component selected by the matching adapter, rather than directly scoring in the global nearest neighbor space, it can effectively suppress the abnormal-abnormal shortcut formed by the erroneous matching of an abnormal region to another abnormal region, and reduce the interference of spatial misalignment on the abnormal score. Step S94: Based on the obtained block-level anomaly graph This can be upsampled or mapped into a pixel-level anomaly heatmap to achieve anomaly region localization; simultaneously, based on image-level anomaly scores... Compare with a preset threshold and output a normal / abnormal judgment result, a sorting signal, or a re-inspection alarm message; Step S95: By combining the block-level positioning results with the image-level anomaly score, the dual requirements of positioning and judgment in online industrial inspection can be met.