A method of detecting out-of-distribution (OOD) samples

By designing an OOD detector in the deep learning model and using the standard deviation of feature space distance to detect OOD samples, the problem of the model's robustness to unseen data is solved, and the security and credibility of the model are improved, especially in applications such as medicine and autonomous driving.

CN119048831BActive Publication Date: 2025-10-14HANGZHOU DIANZI UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411176202.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-08-26
Publication Date
2025-10-14
Estimated Expiration
2044-08-26

AI Technical Summary

Technical Problem

Existing deep learning models have difficulty effectively detecting unseen OOD samples, leading to safety and credibility issues, which may have serious consequences, especially in fields such as medicine and autonomous driving.

Method used

By designing an OOD detector and using the standard deviation of distance in the feature space as an uncertainty indicator, an OOD detection algorithm is constructed to detect OOD samples, including dataset acquisition, model training and OOD detector design. The ResNet backbone network and SupCon loss are used for supervised contrastive learning, and the standard deviation of the feature vector is calculated to identify OOD samples.

Benefits of technology

The model's detection accuracy and security for OOD samples are improved, the misjudgment rate is reduced, and the robustness and credibility of the model are improved.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119048831B_ABST
    Figure CN119048831B_ABST
Patent Text Reader

Abstract

The application discloses a method for detecting OOD samples, first, data set acquisition and pretreatment are carried out; then, an image classification model for OOD detection is constructed and trained; an OOD detector is designed, and the OOD samples are detected by calculating the rejection region R of the OOD samples; finally, the pictures to be detected are input into the trained backbone network to extract features, and the OOD sample detection is realized through the designed OOD detector. The distance in the feature space is taken as the parameter for calculating uncertainty, and the OOD detection algorithm under different perspectives is provided; the OOD samples are not limited to be detected by the distance of a single class, the information utilization of the algorithm is more comprehensive, and the sensitivity of the model to the OOD samples is fully utilized.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The application is based on the field of out-of-distribution sample detection of image classification tasks, and particularly relates to a method for detecting OOD samples through uncertainty. BACKGROUND

[0002] In traditional deep learning, it is usually assumed that the samples used for testing of the model and the samples in the training set are of the same distribution, and these samples are referred to as ID (In-distribution) samples. However, when the model is actually deployed, many samples of different distribution from the training samples, i.e. OOD samples (Out-of-distribution), will be encountered. For example, in an image classification task, the training set of the model contains two training categories of cats and dogs, and when the model is actually deployed, a tiger test sample may be encountered, and the model will output the corresponding confidence that the picture is a cat or a dog, and take the class with higher confidence as the final output. In short, the deep learning network only maps the ID input to the corresponding true value label, and never refuses to map the OOD input to any class except in the training phase.

[0003] With the large number of applications of deep learning models in life, how to effectively detect OOD samples has become a problem to be solved in the practical application of the model. Some studies generate OOD samples by a generative model and insert them into the training during the training process, or retrain the model by changing the structure of the model, and good results have been achieved. However, these methods are a great test for training time and computational cost, greatly increasing the cost of model deployment. Some other studies have proposed post-hoc OOD detection methods, which do not require direct or indirect retraining of the model, but the OOD detection effect of this method is poor. Based on this, the application proposes a method for detecting OOD samples through uncertainty, which can effectively detect OOD samples without changing the training program and target. SUMMARY

[0004] In view of the deficiencies in the prior art, the application provides a method for detecting OOD samples through uncertainty.

[0005] The application mainly considers that with the gradual entry of deep learning models in China into thousands of households, the safety of the model needs to be further improved. A good deep learning model should not only perform well on training data, but also have a certain robustness to data that has not been seen. At the same time, in some application scenarios, the safety and credibility of the model are crucial. For example, in the fields of medical safety and autonomous driving, the wrong prediction of the model may have extremely serious consequences. Through OOD detection, the credibility and safety of the model can be improved.

[0006] A method for detecting out-of-distribution (OOD) samples, comprising the following steps:

[0007] Step 1: data set acquisition and preprocessing;

[0008] Step 2: constructing and training an image classification model for OOD detection;

[0009] Step 3: designing an OOD detector to detect OOD samples by calculating the rejection region R of OOD samples;

[0010] Step 4: inputting the picture to be detected into the trained backbone network to extract features, and implementing OOD sample detection through the designed OOD detector.

[0011] Further, the specific method of step 1 is as follows:

[0012] Step 1.1: select ID samples to train the network, download public data sets ImageNet-1k data set and CIFAR-10 data set.

[0013] Step 1.2: select OOD data set to test the OOD detection performance of the algorithm. The selection of OOD data set should meet the condition that the samples in OOD data set do not belong to any class in the training set.

[0014] For the simple ID data set benchmark CIFAR-10, select SVHN, LSUN, ISUN, Texture, and Place365 as OOD data sets. In order to match the simple benchmark CIFAR-10, adjust the picture size in the corresponding OOD data set to 32*32. For the difficult ID data set benchmark ImageNet-1k, select iNaturalist, SUN, Places, and Texture as OOD data sets. Similarly, in order to match the difficult benchmark ImageNet-1k, adjust the picture size in the corresponding OOD data set to 256*256.

[0015] Further, the specific method of step 2 is as follows:

[0016] Model the image classification task as a function approximation problem, that is, find a function f to map the input image I to the corresponding class label y∈Y={1,2,3,...,K}(K represents K classification categories), the formula is y=f(x). For convenience of expression, f is regarded as the composition of backbone network function g and classification head function h, the mathematical formula is as follows:

[0017]

[0018] ResNet is used as the backbone network for the image classification model. Considering the different training difficulties of CIFAR-10 and ImageNet-1k, ResNet-18 and ResNet-50 are used as the backbone networks for CIFAR-10 and ImageNet-1k, respectively. Fully connected layers are used as the classification heads for both tasks. Supervised contrastive learning (i.e., SupCon loss) is used during training to learn more robust feature representations.

[0019] When the model performs supervised contrastive learning, the training set data in the data set is enhanced by random clipping to obtain the training data set D train , I train is the training dataset D train A picture in, the feature vector z train =g(I train ). The loss function is as follows:

[0020]

[0021] Where P(i) is the set of all positive samples except image i, A(i) is the set of all samples except image i, τ is the temperature coefficient, and I is a batch from the training dataset D train The set of training images is composed of , and z is the feature vector obtained by the backbone network. Through deep learning backpropagation, the similarity of feature vectors of samples of the same class is continuously improved, while the similarity of feature vectors of samples of different classes is reduced until the model converges. The backbone network weights are then fixed, and the fully connected layer is trained separately, using the cross-entropy loss function. The result is a deep learning network model with image classification capabilities, namely an image classification model.

[0022] Furthermore, the specific method of step 3 is as follows:

[0023] Model the OOD detection task and consider a K-class image classification problem. Let X be the input space and assume that the data D in the distribution in ={(x i ,y i )} by the joint distribution P X,Y Defined in X×Y space. Define P0 as the joint distribution P X,Y For the marginal distribution on X, the OOD detection task is regarded as a single-sample hypothesis testing problem.

[0024]

[0025] Wherein x is a test sample or a real sample (hereinafter uniformly referred to as an input sample), a part of f, i.e., a backbone network function g, is used to construct a test statistic T(x,f), and the above hypothesis test problem rejection region R can be written as: R={x:T(x,f)<γ}, wherein γ is a set test statistic threshold.

[0026] Through the trained backbone network model (the operation of OOD detection for simple benchmarks and difficult benchmarks is similar), all pictures in the training data set D train are mapped to a feature space All feature vectors of the feature space are divided into K subsets, wherein the feature vectors in each subset belong to the same class. The class center of each subset is calculated:

[0027]

[0028] Wherein μ i represents the class center of each subset, and the feature vector of the input sample is far away from the feature space The input sample is identified as an OOD sample. Therefore, by calculating the standard deviation of the distance from all class centers as a means of detecting OOD samples, i.e.:

[0029] T(x,f)=std(d i (g(x),μ i )) (4)

[0030] d i (g(x),μ i )=||g(x)-μ i ||2 (5)

[0031] Wherein std(d i (g(x),μ i )) represents an uncertainty for the distance (i.e., the standard deviation of the distance from all class centers), and d i (g(x),μ i ) is the Euclidean distance of the input sample from μ i .

[0032] Further, the specific method of step 4 is as follows:

[0033] For the picture to be detected, the corresponding feature vector is selected according to the actual demand in the input backbone network, and then the feature vector is input into the corresponding OOD detector to calculate the rejection region R of the OOD sample through the function T(x,f), and finally the threshold γ is set to detect the OOD sample.

[0034] The method has the advantages and beneficial results that:

[0035] 1.The application innovatively proposes an out-of-distribution (OOD) sample detection algorithm based on uncertainty detection, which uses the distance in the feature space as a parameter to calculate uncertainty and provides OOD detection algorithms from different perspectives.

[0036] 2.The application is not limited to detecting OOD samples by the distance of a single class, and the algorithm uses information more comprehensively and fully utilizes the sensitivity of the model to OOD samples.

[0037] 3.The algorithm for detecting OOD samples based on uncertainty detection designed by the application achieves satisfactory OOD detection effect. BRIEF DESCRIPTION OF DRAWINGS

[0038] Figure 1 is a step flowchart of an embodiment of the application.

[0039] Figure 2 is an actual block diagram of an embodiment of the application.

[0040] Figure 3 , 4 is a comparison result of the method of the application and existing OOD detection methods. DETAILED DESCRIPTION

[0041] The technical solutions of the application will be further described below in combination with the drawings and embodiments.

[0042] As shown in Figure 1 , a method for detecting OOD samples based on uncertainty detection includes the following steps:

[0043] Step 1: data set acquisition and preprocessing;

[0044] Step 1.1: select ID samples to train the network, download public data sets ImageNet-1k data set and CIFAR-10 data set.

[0045] The CIFAR-10 data set has the characteristics of low resolution and few categories, so the network model may form a more compact and localized feature representation in this case. In this case, the classification accuracy of the model on ID samples is usually high because it can capture key details and features in the picture. This provides strong support for our subsequent OOD detection.

[0046] The ImageNet-1k dataset has pictures with high resolution and multiple categories, so the network model may learn more rich and complex feature representations. In this case, the feature space may be more scattered and diverse, as the model needs to model more categories and variations. Due to the complexity of the dataset, the classification accuracy of the model on ID samples may be affected, and there may be more misclassifications. This poses a huge challenge for our subsequent OOD detection.

[0047] Step 1.2: Select OOD datasets to test the OOD detection performance of the algorithm. The selection of OOD datasets should meet the condition that the samples in the OOD dataset do not belong to any class in the training set.

[0048] For the simple ID dataset benchmark CIFAR-10, we selected the SVHN, LSUN, ISUN, Texture, and Place365 datasets as OOD datasets. To match the simple benchmark CIFAR-10, we adjusted the image size in the corresponding OOD datasets to 32*32. For the difficult ID dataset benchmark ImageNet-1k, we selected the iNaturalist, SUN, Places, and Texture datasets as OOD datasets. Similarly, to match the difficult benchmark ImageNet-1k, we adjusted the image size in the corresponding OOD datasets to 256*256.

[0049] Step 2: Build and train an image classification model for OOD detection;

[0050] Model the image classification task as a function approximation problem, i.e., find a function f that maps the input image I to the corresponding class label y∈Y={1,2,3,...,K}(K represents K classification categories), the formula is y=f(x). For convenience of expression, f is taken as the composition of backbone network function g and classification head function h, the mathematical formula is as follows:

[0051]

[0052] The application adopts ResNet as the backbone network of the image classification model, considers the different training difficulties of CIFAR-10 and ImageNet-1k, respectively uses ResNet-18 and ResNet-50 as different backbone networks of the CIFAR-10 and ImageNet-1k tasks, and finally uses a full connection layer as a classification head. A large number of studies show that the network model after contrast learning has significant characteristics and advantages in feature space expression. Contrast learning aims to learn feature representation by comparing the similarity and difference between different samples, and these features can better distinguish samples of different categories, so that the model can more accurately classify ID samples. In OOD detection, these discriminative features are very important for distinguishing samples of unknown distribution and detecting abnormal situations. Therefore, the application performs supervised contrast learning (that is, adding SupCon loss in the training process) in the training process to learn more robust feature representation.

[0053] It is worth noting that when the model performs supervised contrast learning, the training dataset D train is obtained by randomly cropping the training set data in the dataset. train is a picture in the training dataset D train , and the feature vector z train =g(I train ). The loss function is as follows:

[0054]

[0055] Where P(i) is the set of all positive samples except picture i, A(i) is the set of all samples except picture i, tau is the temperature coefficient, I is a set of training pictures from the training dataset D train , and z is the feature vector obtained by the backbone network. Through the back propagation of deep learning, the similarity of feature vectors of the same class samples is continuously improved, and the similarity of feature vectors of different class samples is continuously reduced, until the model converges. Compared with the model without adding SupCon loss, the features extracted by the model are more robust. Then the weights of the backbone network are fixed, and the full connection layer is trained alone, and the loss function is the classic cross-entropy function. Finally, the deep learning network model with image classification ability, that is, the image classification model, is obtained.

[0056] Step 3: design an OOD detector to detect OOD samples by calculating the rejection region R of the OOD samples;

[0057] Modeling the OOD detection task, considering a K-class image classification problem, let X be the input space, and assume that the distribution of the data D in ={(xi ,y i )} by the joint distribution P X,Y Defined in X×Y space. Define P0 as the joint distribution P X,Y For the marginal distribution on X, the OOD detection task is regarded as a single-sample hypothesis testing problem.

[0058]

[0059] Where x is a test sample or a real sample (hereinafter referred to as an input sample), and a part of f, namely the backbone network function g, is used to construct the test statistic T(x,f). Then the rejection region R of the above hypothesis test problem can be written as: R = {x:T(x,f) <γ}, where γ is the set test statistic threshold.

[0060] Through the trained backbone network model (the operation of OOD detection for simple benchmarks and difficult benchmarks is similar), the training dataset D train All images are mapped into the feature space The feature space All feature vectors of are divided into K subsets, where each subset The eigenvectors in all belong to the same class. Compute the class center of each subset:

[0061]

[0062] Among them, μ i Represents the class center of each subset. It is generally believed that the feature vector of the input sample is far away from the feature space Then the input sample is identified as an OOD sample. Therefore, the present invention uses the standard deviation from all class centers as a means of detecting OOD samples, namely:

[0063] T(x,f)=std(d i (g(x),μ i )) (4)

[0064] d i (g(x),μ i )=||g(x)-μ i ||2 (5)

[0065] std(d i (g(x),μ i )) represents an uncertainty about the distance (i.e., the standard deviation of the distance from all cluster centers), d i (g(x),μ i ) is the input sample distance μ iEuclidean distance. In fact, from the perspective of uncertainty, the input of the model for OOD samples tends to show a kind of data uncertainty. Past methods focus on using the minimum distance (i.e. min(d i (g(x),μ i ))) as a method to detect OOD samples. Although this method has a certain advancement in indicators, it is a waste of information to judge whether the sample is an OOD sample from only one class. The sensitivity of the model to OOD samples is reflected in the output of all classes.

[0066] Evaluation indicators:

[0067] FPR95 (False Positive Rate at 95% True Positive Rate): It represents the false positive rate of the model when the true positive rate (True Positive Rate, TPR) reaches 95%. The formula is as follows:

[0068]

[0069] AUROC (Area Under the Receiver Operating Characteristic curve): The area under the ROC curve, the ROC curve takes FPR as the horizontal axis and TPR as the vertical axis. The ROC curve reflects the trade-off relationship between the true positive rate and the false positive rate of the model at different threshold values γ. AUROC represents the area under the ROC curve, and its numerical range is between 0 and 1. It is usually used to measure the ability of the model to distinguish between positive and negative samples. The closer AUROC is to 1, the better the model's distinguishing ability; the closer AUROC is to 0.5, the weaker the model's distinguishing ability, close to random prediction.

[0070] Step 4: Input the picture to be detected into the trained backbone network to extract features, and realize OOD sample detection through the designed OOD detector.

[0071] For the picture to be detected, select the corresponding input feature vector in the backbone network according to the actual demand, then input the feature vector into the corresponding OOD detector, calculate the rejection region R of the OOD sample through the function T(x,f), and finally detect the OOD sample according to the set threshold value γ.

[0072] Experimental data:

[0073] For example Figure 3 , Figure 4As shown, under the same reference, the same OOD data set, the same backbone network, compared with common OOD detection methods such as MSP, MaxLogits, Energy, Vim, KL-Matching, GradNorm, Mah, KNN and the like, our method has considerable advantages, which embodies the superiority of the present application.

[0074] The above is a further detailed description of the present application in combination with specific / preferred embodiments, and cannot be regarded as limiting the specific implementation of the present application to these descriptions. For ordinary skilled persons in the technical field to which the present application belongs, without departing from the concept of the present application, they can also make several substitutions or modifications to the described embodiments, and these substitutions or modifications shall be regarded as belonging to the protection scope of the present application.

[0075] The part of the present application not described in detail belongs to the technology known to those skilled in the art.

Claims

1. A method for detecting OOD samples through uncertainty, characterized in that: The steps are as follows: Step 1: Dataset acquisition and preprocessing; Step 2: Build and train an image classification model for OOD detection; Step 3: Design an OOD detector and detect OOD samples by calculating the rejection region R of the OOD samples. The specific method is as follows: Model the OOD detection task and consider a K-class image classification problem. Let X be the input space and assume that the data D in the distribution in ={(x i ,y i )} by the joint distribution P X,Y Defined on the X×Y space; define P0 as the joint distribution P X,Y For the marginal distribution on X, the OOD detection task is regarded as a single-sample hypothesis testing problem; Where x is a test sample or a real sample, which is uniformly referred to as an input sample below. A part of f, namely the backbone network function g, is used to construct the test statistic T(x,f). Then the rejection region R of the above hypothesis test problem can be written as: R = {x:T(x,f) < γ}, where γ is the set test statistic threshold; Through the trained backbone network model, the training data set D train All images are mapped into the feature space The feature space All feature vectors of are divided into K subsets, where each subset The eigenvectors in all belong to the same class; compute the class center for each subset: Among them, μ i Represents the class center of each subset, and the feature vector of the input sample is far away from the feature space Then the input sample is identified as an OOD sample; therefore, the standard deviation from the center of all classes is calculated as a means of detecting OOD samples, that is: T(x,f)=std(d i (g(x), μ i )) 4) d i (g(x),μ i )=||g(x)-μ i ||2 (5) Among them, std(d i (g(x),μ i )) represents an uncertainty about the distance, that is, the standard deviation of the distance from all cluster centers, d i (g(x),μ i ) is the input sample distance μ i The Euclidean distance of Step 4: Input the image to be detected into the trained backbone network to extract features, and implement OOD sample detection through the designed OOD detector.

2. The method for detecting OOD samples by uncertainty according to claim 1, characterized in that: Step 1: Step 1.1: Select ID samples to train the network and download the public datasets ImageNet-1k and CIFAR-10. Step 1.2: Select an OOD dataset to test the OOD detection performance of the algorithm; the selection of the OOD dataset must meet the condition that the samples in the OOD dataset do not belong to any category in the training set; For the simple ID dataset benchmark CIFAR-10, five datasets, namely SVHN, LSUN, ISUN, Texture, and Place365, are selected as OOD datasets; in order to match the simple benchmark CIFAR-10, the image size in the corresponding OOD dataset is adjusted to 32*32; for the difficult ID dataset benchmark ImageNet-1k, four datasets, namely iNaturalist, SUN, Places, and Texture, are selected as OOD datasets; similarly, in order to match the difficult benchmark ImageNet-1k, the images in the corresponding OOD dataset are adjusted to 256*256.

3. The method for detecting OOD samples by uncertainty according to claim 2, characterized in that: Step 2: The image classification task is modeled as a function approximation problem, that is, finding a function f that maps the input image I to the corresponding category label y∈Y={1,2,3,...,k}, where K represents the K classification categories. The formula is expressed as y=f(x). For convenience of expression, f is regarded as a composite of the backbone network function g and the classification head function h. The mathematical formula is as follows: f=h°g ResNet is used as the backbone network for the image classification model. Considering the different training difficulties of CIFAR-10 and ImageNet-1k, ResNet-18 and ResNet-50 are used as the backbone networks for CIFAR-10 and ImageNet-1k respectively. Finally, fully connected layers are used as the classification heads. Supervised contrastive learning is performed during training, that is, SupConloss is added to the training process to learn more robust feature representations. When the model performs supervised contrastive learning, the training set data in the data set is enhanced by random clipping to obtain the training data set D train , I train is the training dataset D train A picture in, the feature vector z train =g(I train ); the loss function is as follows: Where P(i) is the set of all positive samples except image i, A(i) is the set of all samples except image i, τ is the temperature coefficient, and I is a batch from the training dataset D train The set of training images is composed of , z is the feature vector obtained by the backbone network; through the back propagation of deep learning, the similarity of the feature vectors of samples of the same type is continuously improved, and the similarity of the feature vectors of samples of different types is reduced until the model converges; then the weights of the backbone network are fixed, and the fully connected layer is trained separately, and the loss function is the cross entropy function; finally, a deep learning network model with graphic classification ability, namely the image classification model, is obtained.

4. The method for detecting OOD samples by uncertainty according to claim 3, characterized in that: Step 4: For the image to be detected, the corresponding backbone network is selected according to actual needs to extract the feature vector, and then the feature vector is input into the corresponding OOD detector to calculate the rejection region R of the OOD sample through the function T(x,f), and finally the OOD sample is detected according to the set threshold γ.

Citation Information

Patent Citations

  • Image class incremental learning method and system based on out-of-distribution detection

    CN117079011A

  • Equipment image distribution outside anomaly detection method based on comprehensive score function

    CN118521547A