An unsupervised anomaly detection method and related devices
By constructing a training set containing only normal samples, multimodal modeling is performed using a frozen image encoder and a normality Bayesian encoder. Furthermore, a PAC-Bayes constraint and uncertainty control jump connection routing module are introduced to solve the problems of model robustness and anomaly localization accuracy in unsupervised anomaly detection, thus achieving more efficient anomaly detection.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- GUANGZHOU UNIVERSITY
- Filing Date
- 2026-05-08
- Publication Date
- 2026-07-28
AI Technical Summary
Existing unsupervised anomaly detection methods, when lacking abnormal samples, struggle to simultaneously achieve normal distribution modeling capabilities, anomaly suppression capabilities, reconstruction robustness, and anomaly localization accuracy. Furthermore, the models are prone to learning identity mappings, resulting in limited anomaly localization effectiveness.
By constructing a training set containing only normal samples, multi-scale features are extracted using a frozen image encoder, and probability distribution modeling is performed by combining a normality Bayesian encoder. A PAC-Bayes constraint and uncertainty control jump connection routing module are introduced to generate reconstructed images, and anomaly detection is performed through a multi-evidence fusion scoring module.
It improves the ability to characterize complex change patterns within normal samples, effectively suppresses the direct copying of abnormal regions to the reconstructed image, enhances the model's ability to correct abnormal perturbations, and improves the accuracy of anomaly detection.
Smart Images

Figure CN122473471A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of image processing technology, and in particular to an unsupervised anomaly detection method and related equipment. Background Technology
[0002] Anomaly detection is a crucial research area in computer vision, widely applied in scenarios such as industrial surface defect detection and medical image lesion detection. The core objective of this task is to identify anomalous regions deviating from the normal distribution in an input image, given a lack of sufficient anomalous samples. In practical applications, anomalous samples are typically characterized by their complexity and diversity, significantly fewer quantity than normal samples, and high acquisition and annotation costs. Therefore, many existing studies employ unsupervised anomaly detection methods trained solely on normal samples.
[0003] Existing unsupervised anomaly detection methods mainly fall into three categories. The first category is reconstruction-based methods, which learn the reconstruction capabilities of normal images through structures such as autoencoders and utilize the differences between the input image and the reconstructed image for anomaly detection. The second category is feature distribution modeling-based methods, which model the distribution of normal samples through high-dimensional feature extraction and statistical fitting. The third category is contrastive learning or teacher-student distillation-based methods, which improve anomaly discrimination capabilities by constraining the aggregation degree of normal samples in the feature space.
[0004] However, the above methods still have several shortcomings. For traditional reconstruction-based methods, the model easily learns identity mappings, leading to similarly well-reconstructed abnormal regions, thus weakening the abnormal response. For feature distribution modeling methods, most schemes only use unimodal distributions or deterministic feature representations, making it difficult to characterize the complex multimodal changes within normal samples, and also unable to explicitly utilize uncertainty information. For contrastive learning methods, although they can enhance the compactness of normal representations, they lack reconstruction and repair mechanisms and prior constraints, resulting in limited anomaly localization effectiveness. Summary of the Invention
[0005] The main objective of this invention is to provide an unsupervised anomaly detection method, apparatus, electronic device, storage medium, and program product, aiming to solve at least one problem in the prior art.
[0006] To achieve the above objectives, one aspect of this invention proposes an unsupervised anomaly detection method, the method comprising: The anomaly detection dataset is obtained and processed to form a training set; the training set contains only normal samples. Training samples are constructed based on normal samples in the training set. The training samples include training input and label information. The training input includes counterfactual pseudo-anomaly images corresponding to normal samples and multiple normal augmented views. The label information includes pseudo-anomaly region masks corresponding to the counterfactual pseudo-anomaly images. Based on the training input, multi-scale features are extracted using a frozen image encoder. A normal Bayesian encoder is used to model the probability distribution of the multi-scale features, resulting in the latent feature mean, latent feature variance, sampled latent features, and global posterior descriptor vector. The global posterior description vector is input into the normal prototype mixing module. A normal mixing prior is constructed using multiple learnable normal prototypes, and the deviation constraint of the posterior distribution is applied through PAC-Bayes constraints to obtain the prototype assignment probability, prototype assignment entropy, and prototype assignment distance. The latent feature variance, prototype assignment entropy and prototype assignment distance are input into the uncertainty control jump connection routing module to generate jump connection gating information. The sampled latent features and jump connection gating information are input into the counterfactual contrast reconstruction decoder to generate a reconstructed image with the corresponding normal sample as the reconstruction target. A joint loss is constructed based on normal samples, global posterior descriptor vectors, label information, and reconstructed images. The target model is trained using the joint loss to obtain the reconstructed model. The target model includes a frozen image encoder, a normality Bayesian encoder, a normal prototype hybrid module, an uncertainty-controlled jump connection routing module, and a counterfactual contrastive reconstruction decoder. The target reconstructed image of the image under test is obtained by processing the reconstruction model, and then the anomaly detection result is obtained by processing the multi-evidence fusion scoring module.
[0007] In some embodiments, training samples are constructed based on normal samples from the training set, including the following steps: Apply a counterfactual pseudo-anomaly construction operator to normal samples to generate counterfactual pseudo-anomaly images and record the corresponding pseudo-anomaly region masks; Among them, the antifactual pseudo-anomaly construction operator randomly adopts one or more of the following local perturbation methods: local CutPaste perturbation, local block scrambling perturbation, local noise injection perturbation, and local fuzzy perturbation; Multiple lightweight enhancement transformations are applied to normal samples to obtain multiple normal enhanced views; among them, lightweight enhancement transformations include color perturbation, slight blurring, and random grayscale conversion.
[0008] In some embodiments, the multi-scale features include the hierarchical features of each layer of the frozen image encoder. A normal Bayesian encoder is used to model the probability distribution of the multi-scale features to obtain the latent feature mean, latent feature variance, sampled latent features, and global posterior descriptor vector, including the following steps: The latent feature mean is obtained by predicting the features of the convolutional layer, and then the latent feature variance is obtained by processing it through tensor truncation operation. Sampled latent features are obtained from the posterior distribution using a reparameterization technique; wherein the posterior distribution is determined based on the latent feature mean and latent feature variance. Adaptive average pooling and flattening are performed on the sampled latent features corresponding to the deepest layer to obtain the global posterior description vector.
[0009] In some embodiments, the normal prototype hybridization module is pre-configured with a set of normal prototypes. A normal hybrid prior is constructed using multiple learnable normal prototypes, and deviation constraints on the posterior distribution are applied using PAC-Bayes constraints to obtain prototype assignment probabilities, prototype assignment entropy, and prototype assignment distances. This includes the following steps: The global posterior description vector is input into the linear layer and processed by the Softmax function to obtain the prototype assignment probability of the training sample to each normal prototype in the normal prototype set. Furthermore, a PAC-Bayes constraint based on Kullback-Leibler divergence is introduced to constrain the deviation between the posterior distribution and the normal prior, so as to determine the normal prototype index that is closest to the normal sample as the prototype assignment result. Among them, the normal prior is determined based on the mixed distribution of the normal prototype set; The prototype assignment entropy is obtained by quantifying the uncertainty of the assignment results of normal samples to the prototype. The distance between the normal samples and the prototype assignment results is quantized to obtain the prototype assignment distance.
[0010] In some embodiments, the latent feature variance, prototype assignment entropy, and prototype assignment distance are input into the uncertainty control jump connection routing module to generate jump connection gating information, including the following steps: The latent feature variance, prototype assignment entropy, and prototype assignment distance are concatenated and input into a multilayer perceptron, which is then activated by the Sigmoid function to obtain the jump connection gating coefficients as jump connection gating information.
[0011] In some embodiments, a joint loss is constructed based on normal samples, global posterior descriptive vectors, label information, and reconstructed images, including the following steps: Based on the counterfactual pseudo-anomaly images in the training input and the global posterior description vectors corresponding to each normal augmented view, a contrastive loss is constructed by using a similarity function to impose contrast constraints on the proximity of normal augmented views to each other and the distance of counterfactual pseudo-anomaly images from normal augmented views. Based on normal samples and reconstructed images, a counterfactual reconstruction loss is constructed using L1 regularization and structural similarity index. Based on the reconstructed image, multi-scale reconstruction features are extracted using a frozen image encoder. Based on the multi-scale features and the multi-scale reconstruction features, L1 regularization is used to construct the feature reconstruction loss. Based on hop gating information and label information, the routing constraint loss is constructed using L1 regularization. Based on the global posterior description vectors corresponding to each normal augmented view, the posterior consistency loss is constructed using Jensen-Shannon divergence. PAC-Bayes regularized loss is constructed based on PAC-Bayes constraints; We weighted and fused the contrast loss, counterfactual reconstruction loss, feature reconstruction loss, routing constraint loss, posterior consistency loss, and PAC-Bayes regularization loss to obtain the joint loss.
[0012] In some embodiments, the anomaly detection results are obtained by processing the multi-evidence fusion scoring module, including the following steps: The pixel residuals are constructed based on the image to be tested and the reconstructed image of the target. Based on the target image reconstruction, the first feature is extracted using a frozen image encoder; Based on the first feature and the second feature obtained from the frozen image encoder of the image to be tested, the feature residual is obtained by L1 regularization quantization; Based on the target latent feature variance obtained from the normal Bayesian encoder and the target prototype allocation entropy obtained from the normal prototype mixing module, a posterior uncertainty is constructed. Based on the target prototype assignment probability obtained from the normal prototype mixing module corresponding to the image under test, and the mixing prior of the image under test and the closest normal prototype, the prior deviation is obtained by constructing the Kullback-Leibler divergence. The pixel residuals, feature residuals, posterior uncertainty, and prior deviations are weighted and fused to obtain anomaly heatmaps and anomaly scores.
[0013] To achieve the above objectives, another aspect of the present invention provides an unsupervised anomaly detection device, the device comprising: The first module is used to obtain the anomaly detection dataset and organize it into a training set; the training set contains only normal samples. The second module is used to construct training samples based on normal samples in the training set. The training samples include training input and label information. The training input includes counterfactual pseudo-anomaly images corresponding to normal samples and multiple normal augmented views. The label information includes pseudo-anomaly region masks corresponding to the counterfactual pseudo-anomaly images. The third module is used to extract multi-scale features based on the training input using a frozen image encoder, and to model the probability distribution of the multi-scale features using a normal Bayesian encoder to obtain the latent feature mean, latent feature variance, sampled latent features, and global posterior descriptor vector. The fourth module is used to input the global posterior description vector into the normal prototype mixing module, construct a normal mixture prior using multiple learnable normal prototypes, and perform deviation constraints on the posterior distribution through PAC-Bayes constraints to obtain prototype assignment probability, prototype assignment entropy, and prototype assignment distance. The fifth module is used to input the latent feature variance, prototype assignment entropy and prototype assignment distance into the uncertainty control jump connection routing module to generate jump connection gating information. The sampled latent features and jump connection gating information are then input into the counterfactual contrast reconstruction decoder to generate a reconstructed image with the corresponding normal sample as the reconstruction target. The sixth module is used to construct a joint loss based on normal samples, global posterior description vectors, label information, and reconstructed images. The joint loss is used to train the target model to obtain the reconstructed model. The target model includes a frozen image encoder, a normality Bayesian encoder, a normal prototype hybrid module, an uncertainty-controlled jump connection routing module, and a counterfactual contrastive reconstruction decoder. The seventh module is used to obtain the target reconstructed image of the image under test through the reconstruction model, and then use the multi-evidence fusion scoring module to obtain the anomaly detection result.
[0014] To achieve the above objectives, another aspect of the present invention provides an electronic device, which includes a memory and a processor. The memory stores a computer program, and the processor executes the computer program to implement the aforementioned method.
[0015] To achieve the above objectives, another aspect of the present invention provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the aforementioned method.
[0016] To achieve the above objectives, another aspect of the present invention provides a computer program product, including a computer program that, when executed by a processor, implements the aforementioned method.
[0017] The embodiments of the present invention include at least the following beneficial effects: The present invention provides an unsupervised anomaly detection method, apparatus, electronic device, storage medium, and program product. This scheme obtains a training set by acquiring and organizing anomaly detection datasets; wherein the training set contains only normal samples; training samples are constructed based on the normal samples in the training set; wherein the training samples include training input and label information, the training input including counterfactual pseudo-anomaly images corresponding to normal samples and multiple normal enhanced views, and the label information including pseudo-anomaly region masks corresponding to the counterfactual pseudo-anomaly images; based on the training input, multi-scale features are extracted using a frozen image encoder, and the probability distribution of the multi-scale features is modeled using a normality Bayesian encoder to obtain the latent feature mean, latent feature variance, sampled latent features, and global posterior descriptor vector; the global posterior descriptor vector is input to a normal prototype mixing module, and a normal prototype is constructed using multiple learnable normal prototypes. A common prior is used, and a deviation constraint on the posterior distribution is applied through PAC-Bayesian constraints to obtain the prototype assignment probability, prototype assignment entropy, and prototype assignment distance. The latent feature variance, prototype assignment entropy, and prototype assignment distance are input into the uncertainty-controlled jump connection routing module to generate jump connection gating information. The sampled latent features and jump connection gating information are then input into the counterfactual contrastive reconstruction decoder to generate a reconstructed image using the corresponding normal sample as the reconstruction target. A joint loss is constructed based on the normal sample, global posterior description vector, label information, and reconstructed image. This joint loss is used to train the target model, resulting in a reconstruction model. The target model includes a frozen image encoder, a normality Bayesian encoder, a normal prototype hybrid module, an uncertainty-controlled jump connection routing module, and a counterfactual contrastive reconstruction decoder. The target reconstructed image of the test image is obtained through processing by the reconstruction model, and then the anomaly detection result is obtained using a multi-evidence fusion scoring module. This invention employs a normality Bayesian encoder and a normal prototype hybrid module to perform multimodal modeling of the probability distribution of normal samples and introduces PAC-Bayes constraints, which can more accurately characterize the complex variation patterns within normal samples. Furthermore, this invention utilizes an uncertainty-controlled jump connection routing module to generate gating information using posterior uncertainty, prototype allocation entropy, and allocation distance, which can effectively suppress the direct copying of abnormal regions to the reconstructed image through jump connections, thus amplifying the reconstruction differences between normal and abnormal samples. Simultaneously, this invention adopts a training strategy that uses counterfactual pseudo-abnormal images as input and normal images as reconstruction targets, which can effectively prevent the model from degenerating into an identity mapping, thereby enhancing the model's ability to correct abnormal perturbations. Attached Figure Description
[0018] Figure 1 This is a schematic diagram of an implementation environment for the unsupervised anomaly detection method provided in this embodiment of the invention; Figure 2 This is a flowchart illustrating the unsupervised anomaly detection method provided in an embodiment of the present invention; Figure 3 This is a schematic diagram of the overall process of the unsupervised anomaly detection method provided in the embodiments of the present invention; Figure 4 This is a schematic diagram illustrating the process architecture principle of the PBCC network structure provided in this embodiment of the invention; Figure 5 This is a schematic diagram of the unsupervised anomaly detection device provided in an embodiment of the present invention; Figure 6 This is a schematic diagram of the structure of the electronic device provided in an embodiment of the present invention. Detailed Implementation
[0019] To make the objectives, technical solutions, and advantages of this invention clearer, the invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative of the invention and are not intended to limit the invention. In the following description, when referring to the accompanying drawings, unless otherwise indicated, the same numbers in different drawings represent the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the embodiments of this invention; they are merely examples of apparatuses and methods consistent with some aspects of the embodiments of this invention as detailed in the appended claims.
[0020] It is understood that the terms "first," "second," etc., used in this invention may be used to describe various concepts, but unless specifically stated otherwise, these concepts are not limited by these terms. These terms are only used to distinguish one concept from another. For example, without departing from the scope of embodiments of this invention, first information may also be referred to as second information, and similarly, second information may also be referred to as first information. Depending on the context, the words "if" or "when" as used herein may be interpreted as "when," "in response to determination," or "in the event of a determination."
[0021] The terms “at least one,” “multiple,” “each,” “any,” etc., used in this invention, “at least one” includes one, two, or more than two; “multiple” includes two or more than two; “each” refers to each of the corresponding multiple; and “any” refers to any one of the multiple.
[0022] Unless otherwise defined, all technical and scientific terms used in this invention have the same meaning as commonly understood by one of ordinary skill in the art to which this invention pertains. The terminology used in this invention is for descriptive purposes only and is not intended to limit the invention.
[0023] To facilitate understanding of the technical content of the present invention, the meanings of the terms related to the technical features that may be involved in the present invention will be explained first: PAC: Probably Approximately Correct, is a core conceptual framework in computational learning theory used to characterize the ability of learning algorithms to achieve low generalization error with high confidence in a probabilistic sense.
[0024] PBCC: This is an abbreviation for PAC-Bayesian Counterfactual Contrastive, which can be described in Chinese as PAC-Bayesian Counterfactual Contrastive. BNPE: Bayesian Normality Posterior Encoder; NPM: Normal Prototype Mixture. UCSR: Uncertainty-Controlled Skip Routing. PCAM: PAC - Bayesian Contrastive Alignment Module.
[0025] In related technologies, how to simultaneously achieve normal distribution modeling capability, anomaly suppression capability, reconstruction robustness, and anomaly localization accuracy under the condition of training with only normal samples remains an urgent problem to be solved in this technical field.
[0026] In view of this, this invention provides an unsupervised anomaly detection method and related equipment. This method obtains a training set by acquiring and organizing an anomaly detection dataset; the training set contains only normal samples; training samples are constructed based on the normal samples in the training set; the training samples include training input and label information. The training input includes counterfactual pseudo-anomaly images corresponding to normal samples and multiple normal enhanced views; the label information includes pseudo-anomaly region masks corresponding to the counterfactual pseudo-anomaly images; based on the training input, multi-scale features are extracted using a frozen image encoder, and the probability distribution of the multi-scale features is modeled using a normality Bayesian encoder to obtain the latent feature mean, latent feature variance, sampled latent features, and global posterior description vector; the global posterior description vector is input to a normal prototype mixing module, and a normal mixing prior is constructed using multiple learnable normal prototypes and passed through PA. C-Bayes constraints are applied to the posterior distribution deviation to obtain the prototype assignment probability, prototype assignment entropy, and prototype assignment distance. The latent feature variance, prototype assignment entropy, and prototype assignment distance are input into the uncertainty control jump connection routing module to generate jump connection gating information. The sampled latent features and jump connection gating information are then input into the counterfactual contrastive reconstruction decoder to generate a reconstructed image using the corresponding normal sample as the reconstruction target. A joint loss is constructed based on the normal sample, global posterior description vector, label information, and the reconstructed image. This joint loss is used to train the target model, resulting in the reconstruction model. The target model includes a frozen image encoder, a normality Bayesian encoder, a normal prototype mixing module, an uncertainty control jump connection routing module, and a counterfactual contrastive reconstruction decoder. The target reconstructed image of the test image is obtained through processing by the reconstruction model, and then the anomaly detection result is obtained using a multi-evidence fusion scoring module. This invention employs a normality Bayesian encoder and a normal prototype hybrid module to perform multimodal modeling of the probability distribution of normal samples and introduces PAC-Bayes constraints, which can more accurately characterize the complex variation patterns within normal samples. Furthermore, this invention utilizes an uncertainty-controlled jump connection routing module to generate gating information using posterior uncertainty, prototype allocation entropy, and allocation distance, which can effectively suppress the direct copying of abnormal regions to the reconstructed image through jump connections, thus amplifying the reconstruction differences between normal and abnormal samples. Simultaneously, this invention adopts a training strategy that uses counterfactual pseudo-abnormal images as input and normal images as reconstruction targets, which can effectively prevent the model from degenerating into an identity mapping, thereby enhancing the model's ability to correct abnormal perturbations.
[0027] It is understood that the unsupervised anomaly detection method provided by this invention can be applied to any computer device with data processing and computing capabilities, and this computer device can be various terminals or servers. When the computer device in the embodiment is a server, the server is an independent physical server, or a server cluster or distributed system composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDN (Content Delivery Network), and big data and artificial intelligence platforms. Optionally, the terminal can be a smartphone, tablet, laptop, or desktop computer, but it is not limited to these.
[0028] like Figure 1 The diagram shown is a schematic representation of an implementation environment provided by an embodiment of the present invention. (Refer to...) Figure 1 The implementation environment includes at least one terminal 102 and a server 101. The terminal 102 and the server 101 can be connected via a network, either wirelessly or via a wired connection, to complete data transmission and exchange.
[0029] Server 101 can be a standalone physical server, a server cluster or distributed system consisting of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDN (Content Delivery Network), and big data and artificial intelligence platforms.
[0030] Additionally, server 101 can also be a node server in a blockchain network. Blockchain is a novel application model of computer technologies such as distributed data storage, peer-to-peer transmission, consensus mechanisms, and encryption algorithms.
[0031] Terminal 102 can be a smartphone, tablet computer, laptop computer, desktop computer, smart speaker, smartwatch, etc., but is not limited to these. Terminal 102 and server 101 can be directly or indirectly connected via wired or wireless communication, and this embodiment of the invention does not impose any limitations.
[0032] For example, based on Figure 1 The implementation environment shown in this embodiment of the invention provides an unsupervised anomaly detection method. The following description uses the application of this unsupervised anomaly detection method in server 101 as an example. It can be understood that this unsupervised anomaly detection method can also be applied to terminal 102.
[0033] Reference Figure 2 , Figure 2 This is an optional flowchart of an unsupervised anomaly detection method provided in an embodiment of the present invention. The execution subject of this unsupervised anomaly detection method can be any of the aforementioned computer devices (including servers or terminals). Figure 2 The method may include, but is not limited to, steps S100 to S700.
[0034] Step S100: Obtain the anomaly detection dataset and organize it to obtain the training set; The training set contains only normal samples. For example, in some specific embodiments, an image dataset of the domain to be detected is obtained. This dataset can be an industrial surface defect dataset, a medical image dataset, or other visual detection dataset containing both normal and abnormal samples. The dataset is divided into a training set, a validation set, and a test set. Preferably, the training and validation sets retain only normal samples, while the test set retains both normal and abnormal samples for final detection performance evaluation. Specifically, this invention employs an unsupervised anomaly detection setting that trains only on normal samples. That is, during the training phase, real anomaly images and their annotation information are not used; instead, counterfactual pseudo-anomaly images are constructed based on normal samples for model training.
[0035] In some preferred embodiments, the images of the samples can be preprocessed before training to improve the stability of the model's feature representation, including but not limited to: The training, validation, and test sets are preprocessed uniformly to reduce image noise, suppress illumination variations, standardize scale, and improve the stability of subsequent feature extraction. Preprocessing may include image resizing, pixel normalization, and standardization.
[0036] Let the input image be Its standardized form is denoted as Then standardization can be expressed as:
[0037] in, The average pixel value of the image. represents the standard deviation of image pixels.
[0038] After standardization, the image pixel values are then normalized to... The interval is used to facilitate subsequent numerical calculations by the neural network.
[0039] In this invention, to ensure network input consistency, it is preferable to uniformly adjust the images to a fixed resolution and keep the preprocessing process consistent between the training and testing phases.
[0040] Step S200: Based on the normal samples in the training set, construct the training samples; The training samples include training input and label information. The training input includes counterfactual pseudo-anomaly images corresponding to normal samples and multiple normal augmented views. The label information includes pseudo-anomaly region masks corresponding to the counterfactual pseudo-anomaly images. It should be noted that in some embodiments, step S200 may include the following steps: applying a counterfactual pseudo-anomaly construction operator to the normal sample to generate a counterfactual pseudo-anomaly image and recording the corresponding pseudo-anomaly region mask; wherein, the counterfactual pseudo-anomaly construction operator randomly adopts one or more of the following local perturbation methods: local CutPaste perturbation, local block scrambling perturbation, local noise injection perturbation, and local blurring perturbation; applying various different lightweight enhancement transformations to the normal sample respectively to obtain multiple normal enhanced views; wherein, the lightweight enhancement transformations include color perturbation, slight blurring, and random grayscale conversion.
[0041] For example, in some specific implementations, constructing counterfactual pseudo-anomaly samples and normal enhanced views can be achieved as follows: Since this invention employs an unsupervised anomaly detection method trained only on normal samples, it is necessary to construct auxiliary samples from normal images for training. Let the normal image be... This invention constructs three types of training inputs from normal images: 1. Counterfactual anomaly images ; 2. Normal Enhanced View ; 3. Normal Enhanced View .
[0042] Counterfactual pseudo-anomaly images: generated through local perturbation, using a counterfactual pseudo-anomaly construction operator. The algorithm randomly employs one or more of the following methods: Local CutPaste perturbation; Disruption of local blocks; Local noise injection disturbance; Local fuzzy perturbation.
[0043] Specifically, it can be expressed as:
[0044] in, This indicates the construction operator for counterfactual pseudo-anomalies. This represents the mask for the pseudo-anomaly region. After construction, the pseudo-anomaly image can be obtained. and the corresponding pseudo-anomaly region mask .
[0045] Normal Enhanced View: For the same normal image Two randomized lightweight enhancement transformations are applied, including color perturbation, slight blurring, and random grayscale conversion.
[0046] Two normal enhanced views are obtained:
[0047] in, and For data augmentation operators, this step constructs , , and They will be used together for model training.
[0048] Step S300: Based on the training input, multi-scale features are extracted using a frozen image encoder. The probability distribution of the multi-scale features is modeled using a normal Bayesian encoder to obtain the latent feature mean, latent feature variance, sampled latent features, and global posterior descriptor vector. For example, in some implementations, a frozen image encoder (feature extraction backbone) is used to extract multi-scale features.
[0049] It should be noted that multi-scale features include the hierarchical features of each layer of the frozen image encoder. In some embodiments, a normal Bayesian encoder is used to model the probability distribution of multi-scale features to obtain the latent feature mean, latent feature variance, sampled latent features, and global posterior descriptor vector. This can include the following steps: predicting the latent feature mean of the hierarchical features based on the convolutional layer, and then processing it through tensor truncation to obtain the latent feature variance; sampling latent features from the posterior distribution through reparameterization techniques; wherein the posterior distribution is determined based on the latent feature mean and latent feature variance; and performing adaptive average pooling and flattening on the sampled latent features corresponding to the deepest layer to obtain the global posterior descriptor vector.
[0050] For example, in some specific implementations, using a normality Bayesian encoder to establish a normal posterior distribution for the input samples can achieve the following: This invention designs a normal Bayesian encoder to model the probability distribution of multi-scale features output from the frozen feature extraction backbone. Unlike traditional methods that directly output deterministic features, this invention simultaneously predicts the latent mean and latent variance for each feature layer, and obtains the corresponding sampled latent features based on the latent mean and latent variance, thereby explicitly characterizing the position and uncertainty of the input sample in the normal feature space.
[0051] Building upon this, the present invention further performs adaptive average pooling on the deepest latent features to obtain a global posterior description vector. Specifically, adaptive average pooling is used to compress the deepest feature map to a 1×1 size, and then flatten it into a vector form. It should be noted that the present invention does not perform cross-layer fusion, but directly uses the deepest features for global aggregation.
[0052] The global posterior descriptor vector is used to calculate the probability of the input sample being assigned to multiple normal prototypes. Through this design, the model can not only obtain a normality representation of the image, but also determine whether the representation is stable and whether it is closer to a certain normal pattern, providing a basis for subsequent normal prototype matching, PAC-Bayes constraints, and anomaly scoring.
[0053] Let the frozen feature extraction backbone output the first... Layer features are Then BNPE models the feature and outputs the first... The latent posterior distribution of the layer:
[0054] in, Indicates the first Layer latent feature mean, Indicates the first Layer latent feature variance ( ), This represents the latent representation obtained by sampling from the posterior distribution.
[0055] Simultaneously, BNPE further outputs a global posterior representation for subsequent normal prototype assignment and PAC regularization constraints. The output of this step includes: Mean of latent features at each layer ; Variance of latent features at each layer ; latent features sampled at each layer ; Global posterior description vector.
[0056] Through the above design, the model can not only characterize image features, but also explicitly depict the uncertainty of an image being in a normal distribution.
[0057] Step S400: Input the global posterior description vector into the normal prototype mixing module, construct a normal mixing prior using multiple learnable normal prototypes, and constrain the deviation of the posterior distribution through PAC-Bayes constraints to obtain the prototype assignment probability, prototype assignment entropy, and prototype assignment distance. It should be noted that the normal prototype mixing module is pre-configured with a set of normal prototypes. In some embodiments, a normal mixed prior is constructed using multiple learnable normal prototypes, and the deviation of the posterior distribution is constrained by PAC-Bayes constraints to obtain the prototype assignment probability, prototype assignment entropy, and prototype assignment distance. This may include the following steps: inputting the global posterior description vector into a linear layer and passing it through a Softmax function to obtain the prototype assignment probability of the training sample to each normal prototype in the normal prototype set; further introducing PAC-Bayes constraints based on Kullback-Leibler divergence to constrain the deviation between the posterior distribution and the normal prior, so as to determine the normal prototype index closest to the normal sample as the prototype assignment result; wherein, the normal prior is determined based on the mixed distribution of the normal prototype set; the prototype assignment entropy is obtained by quantifying the uncertainty of the normal sample's association with the prototype assignment result; and the distance between the normal sample and the prototype assignment result is quantized to obtain the prototype assignment distance.
[0058] For example, in some specific implementations, constructing a normal prototype hybrid prior and applying PAC-Bayesian constraints can achieve the following: To more accurately characterize the multimodal distribution of normal samples, this invention designs a Normal Prototype Mixture (NPM) module. Instead of simply treating normal samples as a unimodal distribution, this module introduces multiple learnable normal prototypes to characterize the various normal patterns that may exist within the normal samples.
[0059] This module builds a normal prototype set as follows: Then the normal prior can be represented as a mixed distribution:
[0060] in, Indicates the prototype index. This represents the prior weight of the corresponding prototype ( ), This represents the latent representation obtained from sampling.
[0061] For each input sample, the global posterior representation output by BNPE is fed into NPM to calculate its assignment probability to each normal prototype:
[0062] in, This provides a global posterior description of the input image. This is the learnable parameter matrix.
[0063] Building upon this, the present invention further introduces PAC-Bayesian constraints to constrain the deviation between the posterior distribution and the normal prior, which can be expressed as:
[0064] in, Indicates the Kullback-Leibler divergence. and These are the weighting coefficients. In this invention, the posterior distribution mainly refers to the normal posterior distribution corresponding to the input sample. Post-prototype assignment Prior distribution mainly refers to normal prototype mixed prior. and prototype prior weights .
[0065] PAC-Bayes constraints are used to maintain the consistency between the normal posterior distribution and the normal prior distribution during the training phase, thereby improving the model's ability to characterize normal patterns and enhancing the model's ability to distinguish abnormal samples during the testing phase.
[0066] In this step, the output of the normal prototype hybrid module NPM includes: 1. Prototype assignment result: Represents the index of the normal prototype that is closest to the input sample; 2. Prototype Assignment Distance: Represents the distance between the input sample and the mean of the nearest normal prototype; 3. Prototype Assignment Entropy: Represents the degree of uncertainty regarding the prototype attribution of an input sample; 4. Prototype prior probability: Represents the weight of each normal prototype in the overall prior; 5. PAC-Bayes regularization term: indicates the degree of deviation of the current sample posterior distribution from the normal mixture prior.
[0067] Step S500: Input the latent feature variance, prototype assignment entropy and prototype assignment distance into the uncertainty control jump connection routing module to generate jump connection gating information. Input the sampled latent features and jump connection gating information into the counterfactual contrast reconstruction decoder to generate a reconstructed image with the corresponding normal sample as the reconstruction target. It should be noted that in some embodiments, inputting the latent feature variance, prototype allocation entropy, and prototype allocation distance into the uncertainty control jump connection routing module to generate jump connection gating information may include the following steps: concatenating the latent feature variance, prototype allocation entropy, and prototype allocation distance and inputting them into a multilayer perceptron, and then activating it through the Sigmoid function to obtain jump connection gating coefficients as jump connection gating information.
[0068] For example, in some specific implementations, the following can be achieved by using uncertainty control to connect routes and performing counterfactual comparison reconstruction: Traditional reconstruction-based anomaly detection networks, when employing a skip-connection structure, tend to directly copy local details of the anomaly region into the output image, resulting in well-reconstructed anomalies and thus reduced detection performance. To address this issue, this invention designs an Uncertainty-Controlled Skip Routing (UCSR) module.
[0069] Let the first The layer jump gating coefficient is Then it can be calculated in the following way:
[0070] in, This represents the Sigmoid function. Represents the prototype allocation entropy. This represents the distance between the sample and the corresponding normal prototype. The output shape of the gating coefficients is as follows: For each feature layer, the shape of the gating coefficients is [batch_size, 1, H, W], where H and W are the height and width of the corresponding layer feature map.
[0071] The specific application of this gating coefficient is as follows: 1. Controlling low-level feature propagation: The gating coefficients are directly multiplied by the latent features of the corresponding layer, i.e.:
[0072] 2. Intensity adjustment mechanism: The closer the gating coefficient is to 1, the more low-level features are allowed to pass through; the closer it is to 0, the more low-level features are suppressed. 3. Anomaly Repair Guidance: This gating mechanism reduces the reliance on skipping connections for suspicious areas, instead relying more on the decoder for reconstruction and repair. This effectively prevents abnormal details from being directly copied to the output image, improving the accuracy of anomaly detection.
[0073] Based on this, the present invention designs a Counterfactual Contrastive Reconstruction Decoder (CCRD), whose input is a pseudo-anomaly image. The latent representation and gated skip features are used to output the reconstructed image. ,Right now:
[0074] in, Indicates a frozen reconstruction decoder. This represents the latent representation obtained from sampling. This indicates jump-connection gate information.
[0075] The core of this step is that the input is a pseudo-abnormal image, while the output target is the corresponding normal image, so that the model learns "how to restore the abnormal input to a normal state", rather than simply copying the input image.
[0076] Step S600: Construct a joint loss based on normal samples, global posterior descriptive vector, label information and reconstructed image; train the target model using the joint loss to obtain the reconstructed model. The target model includes a frozen image encoder, a normality Bayesian encoder, a normal prototype hybrid module, an uncertainty control jump connection routing module, and a counterfactual contrast reconstruction decoder. It should be noted that in some embodiments, constructing a joint loss based on normal samples, global posterior descriptor vectors, label information, and reconstructed images may include the following steps: Based on the counterfactual pseudo-anomaly images and the global posterior descriptor vectors corresponding to each normal augmented view in the training input, a contrastive loss is constructed by using a similarity function to constrain the proximity of normal augmented views to each other and the distance of counterfactual pseudo-anomaly images from normal augmented views; based on normal samples and reconstructed images, a counterfactual reconstruction loss is constructed using L1 regularization and a structural similarity index; based on the reconstructed images, a multi-scale reconstruction loss is extracted using a frozen image encoder. The system constructs features, and uses L1 regularization to obtain the feature reconstruction loss based on multi-scale features and multi-scale reconstructed features. It also uses L1 regularization to obtain the routing constraint loss based on jump connection gating information and label information. Furthermore, it uses Jensen-Shannon divergence to obtain the posterior consistency loss based on the global posterior description vectors corresponding to each normal augmented view. Finally, it constructs the PAC-Bayes regularization loss based on PAC-Bayes constraints. The system then performs a weighted fusion of the contrastive loss, counterfactual reconstruction loss, feature reconstruction loss, routing constraint loss, posterior consistency loss, and PAC-Bayes regularization loss to obtain the joint loss.
[0077] For example, in some specific implementations, constructing a PAC-Bayes contrastive alignment constraint and jointly training the model can achieve the following: To enhance the compactness of normal sample representation and increase the gap between normal samples and pseudo-abnormal samples, this invention designs a PAC-Bayesian Contrastive Alignment Module (PCAM).
[0078] Set normal enhanced view , The global representations are respectively , Counterfactual pseudo-anomaly images The global representation of is Contrast constraints can bring normal views closer to each other while keeping pseudo-anomaly representations away from normal representations. The contrast loss can be expressed as:
[0079] in, Represents the similarity function. For temperature coefficient, This represents the closest normal prototype. This indicates a negative sample or a pseudo-abnormal sample.
[0080] This invention uses a combination of counterfactual reconstruction loss, feature reconstruction loss, PAC-Bayes regularization loss, routing constraint loss, and posterior consistency loss to train the model.
[0081] The counterfactual reconstruction loss can be expressed as:
[0082] in, To reconstruct the image, For L1 regularization, It is a structural similarity index. These are the weighting coefficients.
[0083] The feature reconstruction loss can be expressed as:
[0084] in, Indicates the first extracted from the frozen backbone network Layer features.
[0085] The routing constraint loss can be expressed as:
[0086] in, This is a mask for pseudo-abnormal regions.
[0087] The posterior consistency loss can be expressed as:
[0088] in, This represents the Jensen-Shannon divergence.
[0089] The PAC-Bayes regularized loss is given by formula (7).
[0090] Therefore, the total loss function during the training phase of this invention can be expressed as:
[0091] in, These are the weighting coefficients for each learnable loss term.
[0092] Through this joint training method, the present invention can simultaneously learn the distribution structure of normal samples, counterfactual repair capability, normal representation aggregation capability, and abnormal region suppression capability.
[0093] Step S700: The target reconstructed image of the image to be tested is obtained through reconstruction model processing, and then the anomaly detection result is obtained by multi-evidence fusion scoring module. It should be noted that in some embodiments, the anomaly detection result obtained by using the multi-evidence fusion scoring module may include the following steps: constructing pixel residuals based on the image to be tested and the reconstructed image of the target; extracting a first feature based on the reconstructed image of the target using a frozen image encoder; quantizing a feature residual using L1 regularization based on the first feature and the second feature obtained from the image to be tested according to the frozen image encoder; constructing a posterior uncertainty based on the target latent feature variance obtained from the image to be tested according to the normality Bayesian encoder and the target prototype allocation entropy obtained from the normal prototype mixing module; constructing a prior deviation using Kullback-Leibler divergence based on the target prototype allocation probability obtained from the image to be tested according to the normal prototype mixing module and the mixing prior of the image to be tested and the closest normal prototype; and weightedly fusing the pixel residuals, feature residuals, posterior uncertainty, and prior deviation to obtain an anomaly heatmap and anomaly score.
[0094] For example, in some specific implementations, after the PBCC model training is completed, the image to be tested is input during the actual application or testing phase, and the image is sequentially processed through the frozen feature extraction backbone, BNPE, NPM, UCSR, and CCRD to obtain the corresponding reconstructed image. The image to be tested and the reconstructed image are then fed back into the feature extraction backbone, and the anomaly is comprehensively scored by the Multi-Evidence Fusion Scorer (MFS) module. The multi-evidence in this invention includes: 1. Pixel residual; 2. Characteristic residuals; 3. Posterior uncertainty; 4. Degree of deviation from normal priors.
[0095] The pixel residual can be expressed as:
[0096] The characteristic residual can be expressed as:
[0097] Posterior uncertainty can be expressed as:
[0098] Prior deviation can be expressed as:
[0099] in, This represents the closest normal prototype.
[0100] The final image-level anomaly score is obtained by weighted fusion of the above-mentioned multiple pieces of evidence:
[0101] in, For fusion weighting coefficients.
[0102] Pixel-level anomaly heatmaps can be used to locate anomaly regions, while image-level anomaly scoring can be used to identify image-level anomalies. The heatmaps are generated by a multi-evidence fusion scoring module (MFS), specifically fusing pixel residuals: the absolute difference between the input image and the reconstructed image. Feature residual: The difference between the input image and the reconstructed image in terms of features at each layer; Posterior uncertainty: the variance of latent features at each layer; Prior deviation: The distance between the input sample and the nearest normal prototype; Post-processing methods for heatmaps include: 1. Normalization: Normalize each type of evidence image and scale it to the [0,1] interval; 2. Weighted Fusion: The normalized evidence maps are summed according to their weights to obtain the final anomaly heatmap; 3. Threshold processing: In the evaluation phase, performance indicators are calculated using different thresholds. In practical applications, appropriate thresholds can be set according to specific scenarios to generate binary anomaly masks.
[0103] To explain in detail the principle of the technical solution of the present invention, the overall process of the present invention will be described below with reference to some specific embodiments. It is easy to understand that the following is an explanation of the technical principle of the present invention and should not be regarded as a limitation of the present invention.
[0104] First, it's important to clarify that there's an existing zero-shot anomaly detection scheme based on Bayesian principles. This scheme builds upon a pre-trained visual-language model, models a probability distribution in the text prompt space, generates dynamic text prompts using this distribution, and then aligns them with image features to achieve anomaly detection. This scheme employs a zero-shot anomaly detection setup, meaning it's trained on seen categories in auxiliary training data and then tested directly on unseen categories, ensuring that seen and unseen categories do not overlap.
[0105] The specific process of this scheme is as follows: First, the input image is fed into an image encoder to extract multi-layer image features and fine-grained patch features, and the patch features are mapped to the image-text joint embedding space. At the same time, two types of text prompt libraries are constructed: normal and abnormal. Each prompt consists of context words, state words, and category words, where normal prompts correspond to descriptions of normal states, and abnormal prompts correspond to descriptions of abnormal states.
[0106] In the text modeling part, this scheme treats the contextual semantics and state semantics in the prompts as random variables and uses Bayesian inference to approximate their posterior distributions. Specifically, the scheme learns two types of distributions through the prompt flow module: one is an image-specific distribution related to the input image, used to model context words; the other is an image-independent distribution unrelated to the input image, used to model normal state words and abnormal state words respectively. Then, multiple sets of context vectors and state vectors are sampled from the above distributions through Monte Carlo sampling, and these are fused with learnable prompts in the prompt library to form multiple sets of dynamic text prompts.
[0107] In the cross-modal alignment part, this scheme feeds dynamic text prompts into the text encoder to obtain normal and anomalous text embeddings. Since the sampled text embeddings change dynamically during training and inference, this scheme further introduces a residual cross-modal attention module, allowing the text embeddings to interact with fine-grained patch features to obtain updated text features. Subsequently, the updated text features are aligned with multi-layer patch features to obtain multiple sets of anomaly segmentation results, which are then averaged to generate the final pixel-level anomaly map. Simultaneously, this scheme also combines the anomaly responses of the text branch and the image branch to calculate an image-level anomaly score for image-level anomaly detection.
[0108] The core idea of this approach is to improve the generalization ability of the visual-language model on unseen categories by constraining and sampling the text cue space in a Bayesian manner, and to achieve zero-shot anomaly detection by leveraging the cross-modal alignment of dynamic text cue with image features.
[0109] Unlike existing technologies, the PBCC method of this invention is not a zero-shot anomaly detection framework based on visual-language cue learning, but rather a counterfactual contrastive reconstruction framework trained only on normal samples. This invention does not rely on two types of text cue libraries (normal and abnormal), nor does it sample the probability distribution of text cue through a cue flow module. Instead, it models the normal distribution of the image using a normality Bayesian encoder, and then restricts the normal representation through a normal prototype mixture prior and PAC-Bayesian constraints. Furthermore, this invention utilizes counterfactual pseudo-anomaly construction, an uncertainty-controlled reconstruction path, and a multi-evidence fusion scoring mechanism to achieve anomaly detection and localization. Therefore, the aforementioned existing solutions belong to zero-shot anomaly detection schemes based on Bayesian cue distribution learning, while this invention belongs to an unsupervised anomaly detection scheme based on PAC-Bayesian counterfactual contrastive reconstruction. The two differ significantly in training methods, feature modeling methods, anomaly discrimination criteria, and overall technical approaches.
[0110] Specifically, the method of this invention is an unsupervised industrial defect detection method, that is, only normal samples are input, and without labeled defective samples, the model automatically identifies abnormal areas in industrial products that do not conform to normal patterns. The model consists of two processes: training and testing, but the only difference is in the input and some calculations. Therefore, the training process (which is more comprehensive) will be described first, and the differences between the testing process and the training process will be described later.
[0111] like Figure 3 The diagram shown is an overall flowchart of the unsupervised anomaly detection method based on PAC-Bayesian counterfactual contrastive reconstruction provided by this invention. This invention provides an unsupervised anomaly detection method based on PAC-Bayesian counterfactual contrastive reconstruction. This method is aimed at industrial surface defect detection, medical image anomaly detection, and other anomaly detection tasks with only normal samples. It adopts an unsupervised learning approach that trains only on normal samples. By constructing counterfactual pseudo-anomaly samples, establishing a normality Bayesian posterior distribution, introducing a normal prototype mixed prior, designing a reconstruction path controlled by uncertainty, and constructing a multi-evidence fusion anomaly scoring mechanism, it achieves image-level anomaly detection and pixel-level anomaly localization.
[0112] In some specific embodiments, the technical solution of the present invention can be implemented through the following process: Step 1: Obtain the anomaly detection dataset and divide it into training, validation, and test sets; Obtain an image dataset for the target area. This dataset can be an industrial surface defect dataset, a medical image dataset, or other visual inspection dataset containing both normal and abnormal samples. Divide the dataset into a training set, a validation set, and a test set. Preferably, the training and validation sets contain only normal samples, while the test set contains both normal and abnormal samples for final detection performance evaluation.
[0113] This invention employs an unsupervised anomaly detection setup that trains only on normal samples. That is, during the training phase, real anomaly images and their annotation information are not used. Instead, counterfactual pseudo-anomaly images are constructed based on normal samples for model training.
[0114] Step 2: Preprocess the input image to improve the stability of the model's feature representation; The training, validation, and test sets are preprocessed uniformly to reduce image noise, suppress illumination variations, standardize scale, and improve the stability of subsequent feature extraction. Preprocessing may include image resizing, pixel normalization, and standardization.
[0115] Let the input image be Its standardized form is denoted as Then standardization can be expressed as:
[0116] in, The average pixel value of the image. represents the standard deviation of image pixels.
[0117] After standardization, the image pixel values are then normalized to... The interval is used to facilitate subsequent numerical calculations by the neural network.
[0118] In this invention, to ensure network input consistency, it is preferable to uniformly adjust the images to a fixed resolution and keep the preprocessing process consistent between the training and testing phases.
[0119] Step 3: Construct counterfactual pseudo-anomaly samples and normal augmented views; Since this invention employs an unsupervised anomaly detection method trained only on normal samples, it is necessary to construct auxiliary samples from normal images for training. Let the normal image be... This invention constructs three types of training inputs from normal images: 1. Counterfactual anomaly images ; 2. Normal Enhanced View ; 3. Normal Enhanced View .
[0120] Counterfactual pseudo-anomaly images: generated through local perturbation, using a counterfactual pseudo-anomaly construction operator. The algorithm randomly employs one or more of the following methods: Local CutPaste perturbation; Disruption of local blocks; Local noise injection disturbance; Local fuzzy perturbation.
[0121] Specifically, it can be expressed as:
[0122] in, This indicates the construction operator for counterfactual pseudo-anomalies. This represents the mask for the pseudo-anomaly region. After construction, the pseudo-anomaly image can be obtained. and the corresponding pseudo-anomaly region mask .
[0123] Normal Enhanced View: For the same normal image Two randomized lightweight enhancement transformations are applied, including color perturbation, slight blurring, and random grayscale conversion.
[0124] Two normal enhanced views are obtained:
[0125] in, and For data augmentation operators, this step constructs , , and They will be used together for model training.
[0126] Step 4: Build the anomaly detection network PBCC based on PAC-Bayesian counterfactual comparison reconstruction; The PBCC network structure of this invention is constructed using a deep learning framework. The overall network structure is as follows: Figure 4 As shown, the PBCC network consists of a frozen feature extraction backbone, a normality Bayesian encoder, a normal prototype hybrid module, an uncertainty control jump connection routing module, a counterfactual contrast reconstruction decoder, a PAC-Bayes contrast alignment module, and a multi-evidence fusion scoring module.
[0127] in: A frozen image encoder (feature extraction backbone) is used to extract multi-scale features; A normality Bayesian encoder is used to establish the normality posterior distribution of the input image; The normal prototype hybrid module is used to construct multimodal priors for normal samples and calculate prototype information for input samples; The uncertainty control jump routing module is used to suppress abnormal details from directly passing through the decoding path; The counterfactual contrast reconstruction decoder is used to reconstruct pseudo-abnormal inputs into normal targets; The PAC-Bayesian contrast alignment module is used to enhance normal representation aggregation and separate pseudo-abnormal representations; The multi-evidence fusion scoring module is used to comprehensively output pixel-level anomaly maps and image-level anomaly scores.
[0128] Step 5: Use a normal Bayesian encoder to establish a normal posterior distribution for the input samples; This invention designs a normal Bayesian encoder to model the probability distribution of multi-scale features output from the frozen feature extraction backbone. Unlike traditional methods that directly output deterministic features, this invention simultaneously predicts the latent mean and latent variance for each feature layer, and obtains the corresponding sampled latent features based on the latent mean and latent variance, thereby explicitly characterizing the position and uncertainty of the input sample in the normal feature space.
[0129] Building upon this, the present invention further performs adaptive average pooling on the deepest latent features to obtain a global posterior description vector. Specifically, adaptive average pooling is used to compress the deepest feature map to a 1×1 size, and then flatten it into a vector form. It should be noted that the present invention does not perform cross-layer fusion, but directly uses the deepest features for global aggregation.
[0130] The global posterior descriptor vector is used to calculate the probability of the input sample being assigned to multiple normal prototypes. Through this design, the model can not only obtain a normality representation of the image, but also determine whether the representation is stable and whether it is closer to a certain normal pattern, providing a basis for subsequent normal prototype matching, PAC-Bayes constraints, and anomaly scoring.
[0131] Let the frozen feature extraction backbone output the first... Layer features are Then BNPE models the feature and outputs the first... The latent posterior distribution of the layer:
[0132] in, Indicates the first Layer latent feature mean, Indicates the first Layer latent feature variance ( ), This represents the latent representation obtained by sampling from the posterior distribution.
[0133] Simultaneously, BNPE further outputs a global posterior representation for subsequent normal prototype assignment and PAC regularization constraints. The output of this step includes: Mean of latent features at each layer ; Variance of latent features at each layer ; latent features sampled at each layer ; Global posterior description vector.
[0134] Through the above design, the model can not only characterize image features, but also explicitly depict the uncertainty of an image being in a normal distribution.
[0135] Step 6: Construct a normal prototype hybrid prior and apply PAC-Bayes constraints; To more accurately characterize the multimodal distribution of normal samples, this invention designs a Normal Prototype Mixture (NPM) module. Instead of simply treating normal samples as a unimodal distribution, this module introduces multiple learnable normal prototypes to characterize the various normal patterns that may exist within the normal samples.
[0136] This module builds a normal prototype set as follows: Then the normal prior can be represented as a mixed distribution:
[0137] in, Indicates the prototype index. This represents the prior weight of the corresponding prototype ( ), This represents the latent representation obtained from sampling.
[0138] For each input sample, the global posterior representation output by BNPE is fed into NPM to calculate its assignment probability to each normal prototype:
[0139] in, This provides a global posterior description of the input image. This is the learnable parameter matrix.
[0140] Building upon this, the present invention further introduces PAC-Bayesian constraints to constrain the deviation between the posterior distribution and the normal prior, which can be expressed as:
[0141] in, Indicates the Kullback-Leibler divergence. and These are the weighting coefficients. In this invention, the posterior distribution mainly refers to the normal posterior distribution corresponding to the input sample. Post-prototype assignment Prior distribution mainly refers to normal prototype mixed prior. and prototype prior weights .
[0142] PAC-Bayes constraints are used to maintain the consistency between the normal posterior distribution and the normal prior distribution during the training phase, thereby improving the model's ability to characterize normal patterns and enhancing the model's ability to distinguish abnormal samples during the testing phase.
[0143] In this step, the output of the normal prototype hybrid module NPM includes: 1. Prototype assignment result: Represents the index of the normal prototype that is closest to the input sample; 2. Prototype Assignment Distance: Represents the distance between the input sample and the mean of the nearest normal prototype; 3. Prototype Assignment Entropy: Represents the degree of uncertainty regarding the prototype attribution of an input sample; 4. Prototype prior probability: Represents the weight of each normal prototype in the overall prior; 5. PAC-Bayes regularization term: indicates the degree of deviation of the current sample posterior distribution from the normal mixture prior.
[0144] Step 7: Use uncertainty to control hop routes and perform counterfactual comparison reconstruction; Traditional reconstruction-based anomaly detection networks, when employing a skip-connection structure, tend to directly copy local details of the anomaly region into the output image, resulting in well-reconstructed anomalies and reduced detection performance. To address this issue, this invention designs an Uncertainty-Controlled Skip Routing (UCSR) module, which controls the skip-connection information based on the posterior uncertainty calculated in Step 6 and the degree of deviation from the normal prototype.
[0145] Let the first The layer jump gating coefficient is Then it can be calculated in the following way:
[0146] in, This represents the Sigmoid function. Represents the prototype allocation entropy. This represents the distance between the sample and the corresponding normal prototype. The output shape of the gating coefficients is as follows: For each feature layer, the shape of the gating coefficients is [batch_size, 1, H, W], where H and W are the height and width of the corresponding layer feature map.
[0147] The specific application of this gating coefficient is as follows: 1. Controlling low-level feature propagation: The gating coefficients are directly multiplied by the latent features of the corresponding layer, i.e.:
[0148] 2. Intensity adjustment mechanism: The closer the gating coefficient is to 1, the more low-level features are allowed to pass through; the closer it is to 0, the more low-level features are suppressed. 3. Anomaly Repair Guidance: This gating mechanism reduces the reliance on skipping connections for suspicious areas, instead relying more on the decoder for reconstruction and repair. This effectively prevents abnormal details from being directly copied to the output image, improving the accuracy of anomaly detection.
[0149] Based on this, the present invention designs a Counterfactual Contrastive Reconstruction Decoder (CCRD), whose input is a pseudo-anomaly image. The latent representation and gated skip features are used to output the reconstructed image. ,Right now:
[0150] in, Indicates a frozen reconstruction decoder. This represents the latent representation obtained from sampling. This indicates jump-connection gate information.
[0151] The core of this step is that the input is a pseudo-abnormal image, while the output target is the corresponding normal image, so that the model learns "how to restore the abnormal input to a normal state", rather than simply copying the input image.
[0152] Step 8: Construct PAC-Bayes contrast alignment constraints and jointly train the model; To enhance the compactness of normal sample representation and increase the gap between normal samples and pseudo-abnormal samples, this invention designs a PAC-Bayesian Contrastive Alignment Module (PCAM).
[0153] Set normal enhanced view , The global representations are respectively , Counterfactual pseudo-anomaly images The global representation of is Contrast constraints can bring normal views closer to each other while keeping pseudo-anomaly representations away from normal representations. The contrast loss can be expressed as:
[0154] in, Represents the similarity function. For temperature coefficient, This represents the closest normal prototype. This indicates a negative sample or a pseudo-abnormal sample.
[0155] This invention uses a combination of counterfactual reconstruction loss, feature reconstruction loss, PAC-Bayes regularization loss, routing constraint loss, and posterior consistency loss to train the model.
[0156] The counterfactual reconstruction loss can be expressed as:
[0157] in, To reconstruct the image, For L1 regularization, It is a structural similarity index. These are the weighting coefficients.
[0158] The feature reconstruction loss can be expressed as:
[0159] in, Indicates the first extracted from the frozen backbone network Layer features.
[0160] The routing constraint loss can be expressed as:
[0161] in, This is a mask for pseudo-abnormal regions.
[0162] The posterior consistency loss can be expressed as:
[0163] in, This represents the Jensen-Shannon divergence.
[0164] The PAC-Bayes regularized loss is given by formula (7).
[0165] Therefore, the total loss function during the training phase of this invention can be expressed as:
[0166] in, These are the weighting coefficients for each learnable loss term.
[0167] Through this joint training method, the present invention can simultaneously learn the distribution structure of normal samples, counterfactual repair capability, normal representation aggregation capability, and abnormal region suppression capability.
[0168] Step 9: During the testing phase, perform anomaly scoring and anomaly localization; After the PBCC model training is completed, the test image is input during the testing phase. The image is then processed sequentially through Freeze Feature Extraction Backbone, BNPE, NPM, UCSR, and CCRD to obtain the corresponding reconstructed image. The image to be tested and the reconstructed image are then fed back into the feature extraction backbone, and the anomaly is comprehensively scored by the Multi-Evidence Fusion Scorer (MFS) module. The multi-evidence in this invention includes: 1. Pixel residual; 2. Characteristic residuals; 3. Posterior uncertainty; 4. Degree of deviation from normal priors.
[0169] The pixel residual can be expressed as:
[0170] The characteristic residual can be expressed as:
[0171] Posterior uncertainty can be expressed as:
[0172] Prior deviation can be expressed as:
[0173] in, This represents the closest normal prototype.
[0174] The final image-level anomaly score is obtained by weighted fusion of the above-mentioned multiple pieces of evidence:
[0175] in, For fusion weighting coefficients.
[0176] Pixel-level anomaly heatmaps can be used to locate anomaly regions, while image-level anomaly scoring can be used to identify image-level anomalies. The heatmaps are generated by a multi-evidence fusion scoring module (MFS), specifically fusing pixel residuals: the absolute difference between the input image and the reconstructed image. Feature residual: The difference between the input image and the reconstructed image in terms of features at each layer; Posterior uncertainty: the variance of latent features at each layer; Prior deviation: The distance between the input sample and the nearest normal prototype; Post-processing methods for heatmaps include: 1. Normalization: Normalize each type of evidence image and scale it to the [0,1] interval; 2. Weighted Fusion: The normalized evidence maps are summed according to their weights to obtain the final anomaly heatmap; 3. Threshold processing: In the evaluation phase, performance indicators are calculated using different thresholds. In practical applications, appropriate thresholds can be set according to specific scenarios to generate binary anomaly masks. During the testing phase, metrics such as image-level AUROC, pixel-level AUROC, AP, and PRO can be used to evaluate the anomaly detection performance.
[0177] The main difference between the training and testing phases of this invention is as follows: The training phase uses only normal images and constructs counterfactual pseudo-anomaly images, pseudo-anomaly masks, and two normal augmented views online based on these normal images. It also utilizes all core modules such as BNPE, NPM, UCSR, and CCRD to calculate PAC-Bayes regularization, contrastive loss, counterfactual reconstruction loss, feature reconstruction loss, routing constraint loss, and posterior consistency loss. Its focus is on learning the "normal distribution and its repair mechanism." The testing phase also uses all core modules such as BNPE, NPM, UCSR, and CCRD, but it no longer constructs pseudo-anomaly images, pseudo-anomaly masks, or dual-view augmentations. It also does not require calculating training-related loss terms such as PAC-Bayes regularization and contrastive loss. It only inputs a single image to be tested, sequentially processing frozen feature extraction, normal posterior modeling, normal prototype matching, controlled reconstruction, and multi-evidence fusion scoring. Finally, it outputs a pixel-level anomaly heatmap and an image-level anomaly score. Its focus is on using the learned normal distribution to measure the degree to which the input sample deviates from the normal state. With this design, the training phase focuses on learning the representation of normal patterns and the ability to repair anomalies, while the testing phase efficiently utilizes this learned knowledge for anomaly detection.
[0178] In summary, this invention proposes an unsupervised anomaly detection method based on PAC-Bayesian counterfactual contrast reconstruction. Compared with existing technologies, this invention includes at least the following innovative methods and beneficial effects: (1) Design a normality Bayesian encoder to explicitly model the normal feature distribution and its uncertainty: Compared to traditional anomaly detection methods that directly output deterministic features, this invention designs a BNPE module that uses posterior mean and posterior variance to characterize the normality distribution of the input sample and outputs a multi-layer latent representation and its uncertainty. This design can explicitly characterize the degree of certainty of a sample in the normal space, providing probabilistic information support for subsequent reconstruction control, prior constraints, and anomaly scoring, thereby helping to improve the stability and interpretability of anomaly detection.
[0179] (2) Design a normal prototype hybrid prior and introduce PAC-Bayes regularization constraints: In contrast to treating normal samples uniformly as a single distribution, this invention designs an NPM module that constructs a multimodal normal mixture prior using multiple learnable normal prototypes, and constrains the posterior distribution to maintain consistency with the normal prior through a PAC-Bayes regularization term. This design can more accurately model the various modal differences that may exist within normal samples, thereby improving the model's adaptability to complex normal samples and enhancing its ability to distinguish abnormal samples.
[0180] (3) Design an uncertainty control hop routing module to suppress the direct transmission of abnormal information: To address the issue that abnormal regions in traditional U-shaped reconstruction structures are easily copied directly to the output via jump connections, this invention designs a UCSR module. This module comprehensively utilizes posterior variance, prototype allocation entropy, and prototype distance to generate jump connection gating coefficients, suppressing jump connection information in suspicious regions. This design makes the model rely more on the decoder for repair in abnormal regions, rather than simply copying input details, thus helping to amplify the differences between abnormal regions and normal reconstruction results.
[0181] (4) Design a counterfactual contrast reconstruction decoder to reconstruct the normal target from the pseudo-abnormal input: Compared to traditional autoencoders that use the input image itself as the reconstruction target, this invention designs a CCRD module that takes a counterfactual pseudo-anomaly image as input and the corresponding normal image as the output target, learning the mapping relationship from the anomalous input to the normal state. This design avoids the model degenerating into a simple identity mapping, improves the model's ability to correct anomalous perturbations, and thus enhances the anomaly detection effect.
[0182] (5) Design a PAC-Bayes contrast alignment module to enhance the aggregation capability of normal representations: This invention introduces a PCAM module during the training phase. Through contrast constraints between normal augmented views, alignment constraints between normal representations and normal prototypes, and separation constraints between pseudo-anomaly representations and normal representations, it improves the compactness and robustness of normal sample feature representations. Combined with PAC-Bayes regularization, this module further stabilizes the normal representation space, enhancing the model's generalization ability in unsupervised scenarios.
[0183] (6) Design a multi-evidence fusion scoring module to improve the robustness of abnormal scoring: Compared to detection methods that rely solely on pixel-level reconstruction errors, this invention designs an MFS module that fuses pixel residuals, feature residuals, posterior uncertainty, and prior deviation to obtain a final pixel-level anomaly heatmap and an image-level anomaly score. This design can characterize the degree to which a sample deviates from its normal state from multiple perspectives, improving the robustness and accuracy of anomaly detection.
[0184] like Figure 5As shown, this embodiment of the invention also provides an unsupervised anomaly detection device 900, which can implement the above-described method. This device may include: The first module 901 is used to obtain the anomaly detection dataset and organize it to obtain the training set; the training set contains only normal samples. The second module 902 is used to construct training samples based on normal samples in the training set; wherein, the training samples include training input and label information, the training input includes counterfactual pseudo-anomaly images corresponding to normal samples and multiple normal augmented views, and the label information includes pseudo-anomaly region masks corresponding to the counterfactual pseudo-anomaly images. The third module 903 is used to extract multi-scale features based on the training input using a frozen image encoder, and to model the probability distribution of the multi-scale features using a normal Bayesian encoder to obtain the latent feature mean, latent feature variance, sampled latent features, and global posterior descriptor vector. The fourth module 904 is used to input the global posterior description vector into the normal prototype mixture module, construct a normal mixture prior using multiple learnable normal prototypes, and perform deviation constraints on the posterior distribution through PAC-Bayes constraints to obtain prototype assignment probability, prototype assignment entropy and prototype assignment distance. The fifth module 905 is used to input the latent feature variance, prototype assignment entropy and prototype assignment distance into the uncertainty control jump connection routing module to generate jump connection gating information. The sampled latent features and jump connection gating information are then input into the counterfactual contrast reconstruction decoder to generate a reconstructed image with the corresponding normal sample as the reconstruction target. The sixth module, 906, is used to construct a joint loss based on normal samples, global posterior description vectors, label information, and reconstructed images. The joint loss is used to train the target model to obtain the reconstructed model. The target model includes a frozen image encoder, a normality Bayesian encoder, a normal prototype hybrid module, an uncertainty-controlled jump connection routing module, and a counterfactual contrastive reconstruction decoder. Module 7, 907, is used to obtain the target reconstructed image of the image under test through the reconstruction model, and then use the multi-evidence fusion scoring module to obtain the anomaly detection result.
[0185] It is understood that the content of the above method embodiments is applicable to the present device embodiments. The specific functions implemented by the present device embodiments are the same as those of the above method embodiments, and the beneficial effects achieved are also the same as those achieved by the above method embodiments.
[0186] This invention also provides an electronic device, which includes a memory and a processor. The memory stores a computer program, and the processor executes the computer program to implement the method described above. This electronic device can be any smart terminal, including tablet computers, in-vehicle computers, etc.
[0187] It is understood that the content of the above method embodiments is applicable to this device embodiment. The specific functions implemented by this device embodiment are the same as those of the above method embodiments, and the beneficial effects achieved are also the same as those achieved by the above method embodiments.
[0188] like Figure 6 As shown, Figure 6 The hardware structure of an electronic device 1000 according to another embodiment is illustrated. The electronic device 1000 includes: The processor 1001 can be implemented using a general-purpose CPU (Central Processing Unit), microprocessor, application-specific integrated circuit (aSIC), or one or more integrated circuits, and is used to execute relevant programs to implement the technical solutions provided in the embodiments of the present invention. The memory 1002 can be implemented as a read-only memory (ROM), a static storage device, a dynamic storage device, or a random access memory (RaM). The memory 1002 can store the operating system and other application programs. When the technical solutions provided in the embodiments of this specification are implemented through software or firmware, the relevant program code is stored in the memory 1002 and is called and executed by the processor 1001. Input / output interface 1003 is used to implement information input and output; The communication interface 1004 is used to enable communication and interaction between this device and other devices. Communication can be achieved through wired means (such as USB, network cable, etc.) or wireless means (such as mobile network, WIFI, Bluetooth, etc.). Bus 1005 transmits information between various components of the device (e.g., processor 1001, memory 1002, input / output interface 1003, and communication interface 1004); The processor 1001, memory 1002, input / output interface 1003 and communication interface 1004 are connected to each other within the device via bus 1005.
[0189] The electronic device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs.
[0190] This invention also provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the above-described method.
[0191] It is understood that the content of the above method embodiments is applicable to this storage medium embodiment. The specific functions implemented in this storage medium embodiment are the same as those in the above method embodiments, and the beneficial effects achieved are also the same as those achieved in the above method embodiments.
[0192] This invention also provides a computer program product, including a computer program that, when executed by a processor, implements the above-described method.
[0193] It is understood that the content of the above method embodiments is applicable to the embodiments of this program product. The specific functions implemented by the embodiments of this program product are the same as those of the above method embodiments, and the beneficial effects achieved are also the same as those achieved by the above method embodiments.
[0194] Memory, as a non-transitory computer-readable storage medium, can be used to store non-transitory software programs and non-transitory computer-executable programs. Furthermore, memory may include high-speed random access memory, and may also include non-transitory memory, such as at least one disk storage device, flash memory device, or other non-transitory solid-state storage device. In some embodiments, memory may optionally include memory remotely located relative to the processor, and these remote memories can be connected to the processor via a network. Examples of such networks include, but are not limited to, the Internet, intranets, local area networks, mobile communication networks, and combinations thereof.
[0195] This invention provides an unsupervised anomaly detection method, apparatus, electronic device, storage medium, and program product. It obtains a training set by acquiring and organizing an anomaly detection dataset; the training set contains only normal samples; training samples are constructed based on the normal samples in the training set; the training samples include training input and label information. The training input includes counterfactual pseudo-anomaly images corresponding to the normal samples and multiple normal enhanced views; the label information includes pseudo-anomaly region masks corresponding to the counterfactual pseudo-anomaly images; based on the training input, multi-scale features are extracted using a frozen image encoder; a normality Bayesian encoder is used to model the probability distribution of the multi-scale features to obtain the latent feature mean, latent feature variance, sampled latent features, and global posterior descriptor vector; the global posterior descriptor vector is input to a normal prototype mixing module, and a normal mixing prior is constructed using multiple learnable normal prototypes. The deviation constraint of the posterior distribution is applied through PAC-Bayes constraints to obtain the prototype assignment probability, prototype assignment entropy, and prototype assignment distance. The latent feature variance, prototype assignment entropy, and prototype assignment distance are input into the uncertainty control jump connection routing module to generate jump connection gating information. The sampled latent features and jump connection gating information are input into the counterfactual contrast reconstruction decoder to generate a reconstructed image with the corresponding normal sample as the reconstruction target. A joint loss is constructed based on the normal sample, global posterior description vector, label information, and reconstructed image. The target model is trained using the joint loss to obtain the reconstruction model. The target model includes a frozen image encoder, a normality Bayesian encoder, a normal prototype mixing module, an uncertainty control jump connection routing module, and a counterfactual contrast reconstruction decoder. The target reconstructed image of the test image is obtained through the reconstruction model, and then the anomaly detection result is obtained by processing it using a multi-evidence fusion scoring module. This invention employs a normality Bayesian encoder and a normal prototype hybrid module to perform multimodal modeling of the probability distribution of normal samples and introduces PAC-Bayes constraints, which can more accurately characterize the complex variation patterns within normal samples. Furthermore, this invention utilizes an uncertainty-controlled jump connection routing module to generate gating information using posterior uncertainty, prototype allocation entropy, and allocation distance, which can effectively suppress the direct copying of abnormal regions to the reconstructed image through jump connections, thus amplifying the reconstruction differences between normal and abnormal samples. Simultaneously, this invention adopts a training strategy that uses counterfactual pseudo-abnormal images as input and normal images as reconstruction targets, which can effectively prevent the model from degenerating into an identity mapping, thereby enhancing the model's ability to correct abnormal perturbations.
[0196] The embodiments described in this invention are for the purpose of more clearly illustrating the technical solutions of the embodiments of this invention, and do not constitute a limitation on the technical solutions provided by the embodiments of this invention. As those skilled in the art will know, with the evolution of technology and the emergence of new application scenarios, the technical solutions provided by the embodiments of this invention are also applicable to similar technical problems.
[0197] Those skilled in the art will understand that the technical solutions shown in the figures do not constitute a limitation on the embodiments of the present invention, and may include more or fewer steps than shown, or combine certain steps, or different steps.
[0198] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs.
[0199] Those skilled in the art will understand that all or some of the steps in the methods disclosed above, as well as the functional modules / units in the systems and devices, can be implemented as software, firmware, hardware, or suitable combinations thereof.
[0200] The preferred embodiments of the present invention have been described above with reference to the accompanying drawings, but this does not limit the scope of the claims of the present invention. Any modifications, equivalent substitutions, and improvements made by those skilled in the art without departing from the scope and spirit of the present invention should be within the scope of the claims of the present invention.
Claims
1. An unsupervised anomaly detection method, characterized in that, The method includes the following steps: An anomaly detection dataset is obtained and organized to form a training set; wherein, the training set contains only normal samples; Based on the normal samples in the training set, training samples are constructed; wherein, the training samples include training input and label information, the training input includes counterfactual pseudo-anomaly images corresponding to the normal samples and multiple normal augmented views, and the label information includes pseudo-anomaly region masks corresponding to the counterfactual pseudo-anomaly images. Based on the training input, multi-scale features are extracted using a frozen image encoder, and the probability distribution of the multi-scale features is modeled using a normal Bayesian encoder to obtain the latent feature mean, latent feature variance, sampled latent features, and global posterior descriptor vector. The global posterior description vector is input into the normal prototype mixing module. A normal mixing prior is constructed using multiple learnable normal prototypes, and the deviation constraint of the posterior distribution is performed through PAC-Bayes constraints to obtain the prototype assignment probability, prototype assignment entropy, and prototype assignment distance. The latent feature variance, the prototype allocation entropy, and the prototype allocation distance are input into the uncertainty control jump connection routing module to generate jump connection gating information. The sampled latent features and the jump connection gating information are input into the counterfactual comparison reconstruction decoder to generate a reconstructed image with the corresponding normal sample as the reconstruction target. Based on the normal samples, the global posterior descriptor vector, the label information, and the reconstructed image, a joint loss is constructed, and the target model is trained using the joint loss to obtain the reconstructed model; wherein, the target model includes the frozen image encoder, the normality Bayesian encoder, the normal prototype hybrid module, the uncertainty control jump connection routing module, and the counterfactual contrast reconstruction decoder; The target reconstructed image of the image under test is obtained by processing the image through the reconstruction model, and then the anomaly detection result is obtained by processing the image through the multi-evidence fusion scoring module.
2. The method according to claim 1, characterized in that, The process of constructing training samples based on the normal samples in the training set includes the following steps: Apply a counterfactual pseudo-anomaly construction operator to the normal sample to generate the counterfactual pseudo-anomaly image and record the corresponding pseudo-anomaly region mask; The antifactual pseudo-anomaly construction operator randomly employs one or more of the following local perturbation methods: local CutPaste perturbation, local block scrambling perturbation, local noise injection perturbation, and local fuzzy perturbation. Multiple lightweight enhancement transformations are applied to the normal samples to obtain multiple normal enhanced views; wherein, the lightweight enhancement transformations include color perturbation, slight blurring, and random grayscale conversion.
3. The method according to claim 1, characterized in that, The multi-scale features include the hierarchical features of each layer of the frozen image encoder. The step of using a normal Bayesian encoder to model the probability distribution of the multi-scale features to obtain the latent feature mean, latent feature variance, sampled latent features, and global posterior descriptor vector includes the following steps: The mean of the latent features is predicted based on the convolutional layer, and then the variance of the latent features is obtained by processing through tensor truncation operation; The sampled latent features are obtained from the posterior distribution by means of the latent features and the variance of the latent features; wherein the posterior distribution is determined based on the mean and variance of the latent features. Adaptive average pooling and flattening are performed on the sampled latent features corresponding to the deepest layer to obtain the global posterior description vector.
4. The method according to claim 1, characterized in that, The normal prototype hybrid module is pre-configured with a set of normal prototypes. The step of constructing a normal hybrid prior using multiple learnable normal prototypes and applying PAC-Bayesian constraints to deviate from the posterior distribution, thereby obtaining the prototype assignment probability, prototype assignment entropy, and prototype assignment distance, includes the following steps: The global posterior description vector is input into a linear layer and processed through a Softmax function to obtain the prototype assignment probability of the training sample to each normal prototype in the normal prototype set. Furthermore, a PAC-Bayes constraint based on Kullback-Leibler divergence is introduced to constrain the deviation between the posterior distribution and the normal prior, so as to determine the normal prototype index that the normal sample is closest to as the prototype assignment result. Wherein, the normal prior is determined based on the mixed distribution of the normal prototype set; The prototype assignment entropy is obtained by quantifying the degree of uncertainty in the assignment of the prototype to the normal samples; The distance between the normal sample and the prototype allocation result is quantized to obtain the prototype allocation distance.
5. The method according to claim 1, characterized in that, The step of inputting the latent feature variance, the prototype allocation entropy, and the prototype allocation distance into the uncertainty control jump connection routing module to generate jump connection gating information includes the following steps: The latent feature variance, the prototype assignment entropy, and the prototype assignment distance are concatenated and input into a multilayer perceptron, which is then activated by the Sigmoid function to obtain the jump connection gating coefficients as jump connection gating information.
6. The method according to claim 1, characterized in that, The method of constructing a joint loss based on the normal samples, the global posterior descriptive vector, the label information, and the reconstructed image includes the following steps: Based on the counterfactual pseudo-anomaly images in the training input and the global posterior description vectors corresponding to each of the normal augmented views, a contrast loss is constructed by using a similarity function to impose contrast constraints on the normal augmented views being close to each other and the counterfactual pseudo-anomaly images being far away from the normal augmented views. Based on the normal samples and the reconstructed image, the counterfactual reconstruction loss is constructed using L1 regularization and structural similarity index. Based on the reconstructed image, multi-scale reconstruction features are extracted using the frozen image encoder. Based on the multi-scale features and the multi-scale reconstruction features, L1 regularization is used to construct the feature reconstruction loss. Based on the hop gating information and the label information, the routing constraint loss is constructed using L1 regularization. Based on the global posterior description vector corresponding to each of the normal augmented views, the posterior consistency loss is constructed using Jensen-Shannon divergence. The PAC-Bayes regularized loss is constructed based on the PAC-Bayes constraints. The joint loss is obtained by weighted fusion of the contrast loss, the counterfactual reconstruction loss, the feature reconstruction loss, the routing constraint loss, the posterior consistency loss, and the PAC-Bayes regularization loss.
7. The method according to claim 1, characterized in that, The process of obtaining anomaly detection results using the multi-evidence fusion scoring module includes the following steps: The pixel residual is constructed based on the image to be tested and the reconstructed target image; Based on the target image reconstruction, the first feature is extracted using the frozen image encoder; Based on the first feature and the second feature obtained by the image under test according to the frozen image encoder, the feature residual is obtained by L1 regularization quantization; Based on the image under test, the posterior uncertainty is constructed according to the target latent feature variance obtained by the normal Bayesian encoder and the target prototype allocation entropy obtained by the normal prototype mixing module. Based on the target prototype allocation probability obtained by the normal prototype mixing module corresponding to the image under test and the mixing prior of the image under test and the closest normal prototype, the prior deviation is obtained by constructing the Kullback-Leibler divergence. The pixel residual, the feature residual, the posterior uncertainty, and the prior deviation are weighted and fused to obtain an anomaly heatmap and an anomaly score.
8. An unsupervised anomaly detection device, characterized in that, The device includes: The first module is used to obtain an anomaly detection dataset and organize it to obtain a training set; wherein, the training set only contains normal samples; The second module is used to construct training samples based on the normal samples in the training set; wherein the training samples include training input and label information, the training input includes counterfactual pseudo-anomaly images corresponding to the normal samples and multiple normal enhanced views, and the label information includes pseudo-anomaly region masks corresponding to the counterfactual pseudo-anomaly images. The third module is used to extract multi-scale features based on the training input using a frozen image encoder, and to model the probability distribution of the multi-scale features using a normal Bayesian encoder to obtain the latent feature mean, latent feature variance, sampled latent features, and global posterior descriptor vector. The fourth module is used to input the global posterior description vector into the normal prototype mixing module, construct a normal mixture prior using multiple learnable normal prototypes, and perform deviation constraints on the posterior distribution through PAC-Bayes constraints to obtain prototype assignment probability, prototype assignment entropy, and prototype assignment distance. The fifth module is used to input the latent feature variance, the prototype allocation entropy and the prototype allocation distance into the uncertainty control jump connection routing module to generate jump connection gating information, and input the sampled latent features and the jump connection gating information into the counterfactual comparison reconstruction decoder to generate a reconstructed image with the corresponding normal sample as the reconstruction target. The sixth module is used to construct a joint loss based on the normal samples, the global posterior descriptor vector, the label information, and the reconstructed image, and to train the target model using the joint loss to obtain the reconstructed model; wherein, the target model includes the frozen image encoder, the normality Bayesian encoder, the normal prototype hybrid module, the uncertainty control jump connection routing module, and the counterfactual contrast reconstruction decoder; The seventh module is used to process the target reconstructed image of the image under test through the reconstruction model, and then use the multi-evidence fusion scoring module to process and obtain the anomaly detection result.
9. An electronic device, characterized in that, The electronic device includes a memory and a processor, the memory storing a computer program, and the processor executing the computer program to implement the method according to any one of claims 1 to 7.
10. A computer program product, characterized in that, The computer program product includes a computer program that, when executed by a processor, implements the method according to any one of claims 1 to 7.