Locally Most Powerful Bayesian Test for Out-of-Distribution Detection using Deep Generative Models

1 Introduction . In several real-world applications of deep learning models , detecting anomalous samples that significantly deviate from the distribution of the training data , that is , out-of-distribution ( OOD ) samples , is crucial for reliable decision making , and various OOD detection methods have been studied in this regard . Deep generative models [ 9 , 8 , 17 ] have also been investigated for OOD detection as an intuitive strategy owing to their ability to evaluate the likelihood of a test sample . However , recent studies have shown that deep generative models can assign higher likelihoods to OOD samples than in-distribution samples [ 12 ] , causing the direct use of raw likelihoods for OOD detection to be problematic . To address this challenge , several OOD scores have been recently proposed for deep generative models and have exhibited effective OOD detection performance [ 15 , 16 , 18 ] . Ren et al . [ 15 ] proposed the use of the ratio of the likelihood obtained from a model trained using pure input data to that obtained from a background model trained using noise-perturbed input data as an OOD score . Xiao et al . [ 18 ] proposed a likelihood regret score that can be calculated as the difference between the likelihood obtained with the optimized parameters for a test sample and that approximated by the VAE . Serrà et al . [ 16 ] proposed a penalized log-likelihood with an input complexity as an OOD score , where the input complexity can be computed as the normalized size of the compressed input image . In this paper , we propose a new OOD detection method that is optimal in some sense in the framework of a Bayesian hypothesis test—inspired by a uniformly most powerful Bayesian test [ 7 ] and a locally 35th Conference on Neural Information Processing Systems ( NeurIPS 2021 ) . most powerful test [ 14 ] . The Bayesian hypothesis test is based on the ratio of the posterior odds that the alternative hypothesis is true , given the observed data [ 3 ] . We formulate the OOD detection problem as a Bayesian test with the null hypothesis that a test sample is an in-distribution sample and the alternative hypothesis that the test sample is an OOD sample . Specifically , the null hypothesis is specified to represent the model trained on the in-distribution training set by the maximum likelihood estimation , as typically performed in previous studies . Against this specific null hypothesis , we propose to specify the alternative hypothesis to represent the model trained on the expanded training set with the test sample . Then , we show that the test with our proposed alternative hypothesis is locally most powerful in that the alternative hypothesis is specified so as to maximize the probability that the Bayes factor exceeds the evidence threshold in favor of the alternative hypothesis among all alternative hypotheses that have the model parameters in the neighborhood of the model parameter specified under the null hypothesis . That is , under this neighborhood parameter condition , the test with the proposed alternative hypothesis maximizes the probability of the correct detection of OOD samples . The proposed test is called the locally most powerful Bayesian test ( LMPBT ) . Because we specify the alternative hypothesis of the LMPBT for a test sample to represent the model trained on the expanded training set with that test sample , we need to re-train the model for each test sample . However , the computational cost of this retraining is significant . To address this issue , we adopt the upweighting method [ 10 ] . Using this method , we analyze the influence of upweighting a test sample on the parameter change and estimate the new parameters without retraining . However , computational issues are encountered when the upweighting method is directly applied in practice . First , the upweighting method involves the Hessian matrix , which is assumed to have all positive eigenvalues . However , the loss function of deep generative models is known to be nonconvex , and finding the global optimum is infeasible . Consequently , the Hessian can have negative eigenvalues . Second , the upweighting method requires the computation of the inverse of the Hessian matrix . However , deep generative models , which have numerous parameters , incur large costs for the calculation of the Hessian and its inverse . To address these issues , we use a low-rank approximation [ 2 , 20 , 21 , 13 ] of the Hessian . Consequently , we can ensure that the approximated Hessian has all positive eigenvalues and can significantly reduce the computational cost for calculating the inverse of the Hessian . We evaluated the performance of the LMPBT using deep generative models on benchmark datasets and demonstrated a more effective OOD detection performance than competing methods . In summary , the contributions of this study are as follows : • We propose a new OOD detection method—the locally most powerful Bayesian test ( LMPBT ) — that maximizes the probability of correct detection of OOD samples under some conditions . • We address the computational issues encountered when practically implementing the LMPBT for deep generative models . • Evaluations conducted of the LMPBT using variational autoencoders ( VAEs ) [ 9 ] and Glows [ 8 ] on benchmark datasets demonstrate state-of-the-art performance for OOD detection ( the code for the LMPBT is available at https : //github.com/keunseokim91/LMPBT ) . 2 Background : Deep Generative Models . Generally , deep probabilistic generative models comprise two sub-models : a generative model and an inference model . By introducing a latent variable z , the generative model defines the generative process of x from z , whereas the inference model infers the distribution of x given z . For example , in flow-based generative models , the generative and inference models are defined using an invertible function f , which is composed of a sequence of invertible functions , f = f1 ◦ f2 ◦ ... ◦ fk . The generative and inference processes between x and z can be expressed as x f1←→ h1 f2←→ h2 · · · fk←→ z , where the latent variable z has a tractable density pθ ( z ) . Then , the log probability density function of the model given a sample x is given by log pθ ( x ) = log pθ ( z ) + log ∣∣∣∣det ( ∂z∂x ) ∣∣∣∣ = log pθ ( z ) + k∑ i=1 log ∣∣∣∣det ( ∂hi∂hi−1 ) ∣∣∣∣ , ( 1 ) where we define h0 = x and hk = z . In VAEs , the generative and inference processes are modeled using the encoder–decoder structure . We assume that x|z ∼ pθ ( x|z ) and z ∼ p ( z ) , where pθ ( x|z ) is parameterized using deep neural networks with parameters θ and p ( z ) is a tractable prior distribution of z . The maximum likelihood estimate ( MLE ) of θ requires maximizing the marginal likelihood pθ ( x ) = ∫ pθ ( x|z ) p ( z ) dz . However , this computation is intractable , and variational inference is used to derive the evidence lower bound ( ELBO ) as follows : log pθ ( x ) ≥ Eqφ ( z|x ) [ log pθ ( x|z ) −DKL ( qφ ( z|x ) ||p ( z ) ) ] = ELBO , ( 2 ) where the variational posterior gφ ( z|x ) approximates the true posterior pθ ( z|x ) and is parameterized by deep neural networks with variational parameters φ and DKL is the Kullback–Liebler ( KL ) divergence . By Eq . ( 2 ) , the ELBO may be used as an approximation of the log-likelihood . Using the exact likelihood in Eq . ( 1 ) and the approximated likelihood in Eq . ( 2 ) , respectively , flow-based generative models and VAEs are typically trained by minimizing the negative log-likelihood of the training dataset { x1 , x2 , . . . , xn } as a training loss : − n∑ i=1 logL ( θ|xi ) = − n∑ i=1 log pθ ( xi ) . ( 3 ) 3 Locally Most Powerful Bayesian Test for OOD Detection . We formulate the OOD detection problem as a Bayesian hypothesis test . We compare the null hypothesis H0 that a test sample xt is an in-distribution sample and the alternative hypothesis H1 that the test sample xt is an OOD sample . Specifically , we consider simple null and alternative hypotheses as follows : H0 : θ = θ0 , ( 4 ) H1 : θ = θ1 , where θ0 and θ1 are representative parameters of a deep generative model for the in-distribution and OOD samples , respectively , and θ is a set of parameters from the parameter space Θ . The Bayesian hypothesis test is based on the ratio of the posterior odds that the alternative hypothesis is true given the observed data [ 3 ] . The posterior odds in favor of the alternative hypothesis equals the Bayes factor multiplied by the prior odds in favor of the alternative hypothesis . When the null and alternative hypotheses are both simple , the Bayes factor represents the ratio of the likelihoods of the data evaluated under the two hypotheses . The posterior odds for our hypothesis test in Eq . ( 4 ) for the test sample xt can be computed as P ( H1|xt ) P ( H0|xt ) = L ( θ1|xt ) L ( θ0|xt ) × P ( H1 ) P ( H0 ) . ( 5 ) Here , P ( Hi ) is the prior probability for hypothesis Hi , and L ( θ1|xt ) /L ( θ0|xt ) is the Bayes factor in favor of the alternative hypothesis , where L ( θ1|xt ) and L ( θ0|xt ) are the likelihoods of xt under H1 and H0 , respectively . To complete the test , we must specify θ0 and θ1 , and the prior probabilities for H0 and H1 . Typically , θ0 is specified as the MLE , denoted by θ̂ , which maximizes the log-likelihood function of the training dataset { x1 , x2 , . . . , xn } as follows : θ̂ = argmax θ∈Θ n∑ i=1 logL ( θ|xi ) . ( 6 ) However , the specification of θ1 is difficult owing to the lack of information on the OOD samples . In fact , the previous likelihood-ratio-based OOD detection methods [ 15 , 16 , 18 ] can be expressed using the hypothesis test in Eq . ( 4 ) with different specifications of θ1 . The OOD detection performance is affected by the specification of θ1 ; thus , it is important to choose θ1 optimally . 3.1 Proposed OOD score . In this paper , we propose a theoretically grounded method to specify θ1 optimally in some sense . Specifically , we specify θ1 as the MLE of the expanded training set with the test sample xt , denoted as θ̂t , as follows : θ̂t = argmax θ [ logL ( θ|xt ) + n∑ i=1 logL ( θ|xi ) ] . ( 7 ) Then , for each test sample xt , we need to solve the optimization problem in Eq . ( 7 ) to obtain θ̂t . However , retraining the model for each test sample is computation-intensive . Instead , we use a computational approximation for the solution of Eq . ( 7 ) , which allows us to obtain θ̂t easily using the already obtained θ̂ . Specifically , we adopt the upweighting method [ 10 ] for the approximation . This method analyzes the influence of a certain training data point on the parameter change by upweighting the loss of that point . It is known that the influence of upweighting the loss of a training data point x by scale is given by dθ̂t d | =0 = −H−1θ̂ ∇θ ( − logL ( θ̂|x ) ) , ( 8 ) where Hθ̂ = 1 n ∑n i=1∇2θ ( − logL ( θ̂|xi ) ) is the Hessian at θ̂ and ∇θ ( − logL ( θ̂|x ) ) is the loss gradient at x with respect to θ . In our case , the training dataset Xn does not contain the test sample xt ; thus , the weight of xt in the training loss in Eq . ( 3 ) can be regarded as zero . Then , adding the loss of xt to the training loss for Xn is equivalent to upweighting the loss of xt by 1/n . Finally , θ̂t can be approximated as θ̂t ≈ θ̃t = θ̂ − 1 n H−1 θ̂ ∇θ ( − logL ( θ̂|xt ) ) . ( 9 ) Finally , with the specification of θ0 = θ̂ and θ1 = θ̃t with the additional assumption of equal prior probabilities for H0 and H1 ( i.e. , P ( H1 ) = P ( H2 ) = 0.5 ) , we propose the log of the posterior odds in Eq . ( 5 ) as an OOD score for test sample xt , denoted by S ( xt ) , as follows : S ( xt ) = − logL ( θ̂|xt ) + logL ( θ̃t|xt ) . ( 10 ) In the next section , we show that the test using the proposed OOD score in Eq . ( 10 ) maximizes the probability that the alternative hypothesis is accepted when it is true ( i.e. , the probability of the correct OOD sample detection ) provided that the parameter specified under the alternative hypothesis is in the neighborhood of the parameter specified under the null hypothesis .

Summary: This paper proposes using a Bayesian hypothesis test for out-of-distribution detection with deep generative models.  The null hypothesis is that the newly observed data is in-sample and the alternative is that the new data is out-of-sample.  The test statistic is a likelihood ratio between the max likelihood model and the model fit to the given test input.  As this statistic is impractical to compute, the paper uses a perturbation of the MLE to find the parameters for the single sample test point.  The paper shows (Prop 1) that this test is the locally most powerful Bayesian test if the training set is sufficiently large.  Yet as the test requires computation of the inverse Hessian matrix, a low-rank approximation is necessary in practice.  Experiments are reported on benchmark image data sets (FashionMNIST, CIFAR, SVHN, CelebA, etc).  The proposed method performs well in terms of AUROC when compared to likelihood ratios and input complexity.  An ablation study of the rank of the Hessian approximation is also performs and shows the method is relatively insensitive to the rank.