Deep Generative Classifier for Out-of-distribution Sample Detection

1 INTRODUCTION . Out-of-distribution ( OOD ) detection , also known as novelty detection , refers to the task of identifying the samples that differ in some respect from the training samples . Recently , deep neural networks ( DNNs ) turned out to show unpredictable behaviors in case of mismatch between the training and testing data distributions ; for example , they tend to make high confidence prediction for the samples that are drawn from OOD or belong to unseen classes ( Szegedy et al. , 2014 ; Moosavi-Dezfooli et al. , 2017 ) . For this reason , accurately measuring the distributional uncertainty ( Malinin & Gales , 2018 ) of DNNs becomes one of the important challenges in many real-world applications where we can hardly control the testing data distribution . Several recent studies have tried to simply detect OOD samples using the confidence score defined by softmax probability ( Hendrycks & Gimpel , 2017 ; Liang et al. , 2018 ) or Mahalanobis distance from class means ( Lee et al. , 2018 ) , and they showed promising results even without re-training the model . However , all of them employ the DNNs designed for a discriminative ( or softmax ) classifier , which has limited power to locate OOD samples distinguishable with in-distribution ( ID ) samples in their latent space . To be specific , the softmax classifier is optimized to learn the discriminative latent space where the training samples are aligned along their corresponding class weight vectors , maximizing the softmax probability for the target classes . As pointed out in ( Hendrycks & Gimpel , 2017 ) , OOD samples are more likely to have small values of the softmax probability for all known classes , which means that their latent vectors get closer to the origin . As a result , there could be a large overlap between two sets of ID and OOD samples in the latent space ( Figure 1 ) , which eventually reduces the gap between their confidence scores and degrades the performance as well . In addition , most of existing confidence scores adopt additional calibration techniques ( Goodfellow et al. , 2014 ; Hinton et al. , 2015 ) to enhance the reliability of the detection , but they include several hyperparameters whose optimal values vary depending on the testing data distribution . In this situation , they utilized a small portion of each test set ( containing both ID and OOD samples ) for validation , and reported the results evaluated on the rest by using the optimal hyperparameter values for each test case . Considering the motivation of OOD detection that prior knowledge of test distributions is not available before we encounter them , such process of tuning the hyperparameters for each test case is not practical when deploying the DNNs in practice . In this paper , we propose a novel objective to train DNNs with a generative ( or distance ) classifier which is capable of effectively identifying OOD test samples . The main difference of our deep generative classifier is to learn separable class-conditional distributions in the latent space , by explicitly modeling them as a DNN layer . The generative classifier places OOD samples further apart from the distributions of all given classes , without utilizing OOD samples for its validation . Thus , based on the Euclidean distance between a test sample and the centers of the obtained class-conditional distributions , we can calculate how likely and how confidently the sample belongs to each class . This can be interpreted as a multi-class extension of unsupervised anomaly detection ( Ruff et al. , 2018 ) , and Gaussian discriminant analysis provides the theoretical background for incorporating the generative classifier into the DNNs . Our extensive experiments on images and tabular data demonstrate that the proposed classifier distinguishes OOD samples more accurately than the state-of-the-art method , while maintaining the classification accuracy for ID samples . 2 METRIC LEARNING-BASED DEEP GENERATIVE CLASSIFIER . We introduce a novel objective for training deep neural networks ( DNNs ) with a generative classifier , which is able to effectively detect out-of-distribution samples as well as classify in-distribution samples into known classes . We first derive the learning objective from the Gaussian discriminant analysis , and propose the distance-based confidence score for out-of-distribution sample detection . Metric learning objective for classification . The key idea of our objective is to optimize the deep learning model so that the latent representations ( i.e. , the outputs of the last layer ) of data samples in the same class gather together thereby form an independent sphere . In other words , it aims to learn each class-conditional distribution in the latent space to follow a normal distribution that is entirely separable from other class-conditional distributions . Using the obtained distributions , we can calculate the class-conditional probabilities that indicate how likely an input sample is generated from each distribution , and this probability can serve as a good measure of the confidence . We define the two terms based on the Euclidean distance between the data representations obtained by the DNNs , denoted by f ( x ) , and the center of each class-conditional distribution , denoted by ck . Given N training samples { ( x1 , y1 ) , . . . , ( xN , yN ) } from K different classes , the objective is described as follows . min W , c , b 1 N N∑ i=1 ( − log exp ( −‖f ( xi ; W ) − cyi‖ 2 + byi ) ∑K k=1 exp ( −‖f ( xi ; W ) − ck‖2 + bk ) + λ · ‖f ( xi ; W ) − cyi‖2 ) , ( 1 ) The objective includes three types of trainable parameters : the weights of DNNsW , the class centers c1 , . . . , cK and biases b1 , . . . , bK . All of them can be effectively optimized by stochastic gradient descent ( SGD ) and back-propagation , which are widely used in deep learning . Note that we directly optimize the latent space induced by the DNNs using Euclidean distance , similarly to other metric learning objectives . Existing deep metric learning based on the triplet loss ( Hoffer & Ailon , 2015 ; Schroff et al. , 2015 ) learns the distance among training samples utilizing their label information to capture their similarities into the metric space for a variety of retrieval tasks . On the other hand , our objective focuses on the distance between the samples and their target class centers for the accurate modeling of class-conditional distributions . Derivation from Gaussian discriminant analysis . Our objective for the generative classifier can be understood from the perspective of Gaussian discriminant analysis ( GDA ) ( Murphy , 2012 ) . The generative classifier defines the posterior distribution P ( y|x ) by using the class-conditional distribution P ( x|y ) and class prior P ( y ) . In case of GDA , each class-conditional distribution is assumed to follow the multivariate Gaussian distribution ( i.e. , P ( x|y = k ) = N ( x|µk , Σk ) ) and the class prior is assumed to follow the Bernoulli distribution ( i.e. , P ( y = k ) = βk∑ k′ βk′ ) . To simply fuse GDA with DNNs , we further fix all the class covariance matrices to the identity matrix ( i.e. , Σk = I ) . Then , the posterior probability that a sample f ( x ) belongs to the class k is described as P ( y = k|f ( x ) ) = P ( y = k ) P ( f ( x ) |y = k ) ∑ k′ P ( y = k ′ ) P ( f ( x ) |y = k′ ) = exp ( − 12‖f ( x ) − µk‖ 2 + log βk ) ∑ k′ exp ( − 1 2‖f ( x ) − µk′‖2 + log βk′ ) . Considering µk and log βk as the class center ck and bias bk respectively , the first term of our objective ( 1 ) is equivalent to the negative log posterior probability . That is , the objective eventually trains the classifier by maximizing the posterior probability for training samples . However , the direct optimization of the DNNs and other parameters by its gradient does not guarantee that the class-conditional distributions become the Gaussian distributions and the class centers are the actual class means of training samples . Thus , to enforce our GDA assumption , we minimize the Kullback-Leibler ( KL ) divergence between the k-th empirical class-conditional distribution and the Gaussian distribution whose mean and covariance are ck and I , respectively . The empirical class-conditional distribution is represented by the average of the dirac delta functions for all training samples of a target class , i.e. , Pk = 1Nk ∑ yi=k δ ( x − f ( xi ) ) , where Nk is the number of the training samples of the class k. Then , the KL divergence is formulated as KL ( Pk ‖ N ( ck , I ) ) = − ∫ 1 Nk ∑ yi=k δ ( x− f ( xi ) ) log [ 1 ( 2π ) d/2 exp ( −1 2 ‖x− ck‖2 ) ] dx + ∫ 1 Nk ∑ yi=k δ ( x− f ( xi ) ) log 1 Nk ∑ yi=k δ ( x− f ( xi ) ) dx = − 1 Nk ∑ yi=k log [ 1 ( 2π ) d/2 exp ( −1 2 ‖f ( xi ) − ck‖2 ) ] + log 1 Nk = 1 2Nk ∑ yi=k ‖f ( xi ) − ck‖2 + constant . The entropy term of the empirical class-conditional distribution can be calculated by using the definition of the dirac measure ( Murphy , 2012 ) . By minimizing this KL divergence for all the classes , we can approximate theK class-conditional Gaussian distributions . Finally , we complete our objective by combining this KL term with the posterior term using the λ-weighted sum in order to control the effect of the regularization . We remark that λ is the hyperparameter used for training the model , which depends on only ID , not OOD ; thus it does not need to be tuned for different test distributions . In-distribution classification . Since our objective maximizes the posterior probability for the target class of each sample P ( y = yi|x ) , we can predict the class label of an input sample to the class that has the highest posterior probability as follows . ŷ ( x ) = arg max k P ( y = k|x ) = arg max k ( −‖f ( x ) − ck‖2 + bk ) ( 2 ) In terms of DNNs , our proposed classifier replaces the fully-connected layer ( fc-layer ) computing the final classification score by wk · f ( x ) + bk with the distance metric layer ( dm-layer ) computing the distance from each center by −‖f ( x ) − ck‖2 + bk . In other words , the class label is mainly predicted by the distance from each class center , so we use the terms “ distance classifier ” and “ generative classifier ” interchangeably in the rest of this paper . The dm-layer contains the exactly same number of model parameters with the fully-connected layer , because only the weight matrix W = [ w1 ; . . . ; wK ] ∈ RK×d is replaced with the class center matrix C = [ c1 ; . . . ; cK ] ∈ RK×d . Out-of-distribution detection . Using the trained generative classifier ( i.e. , class-conditional distributions obtained from the classifier ) , the confidence score of each sample can be computed based on the class-conditional probability P ( x|y = k ) . Taking the log of the probability , we simply define the confidence score D ( x ) using the Euclidean distance between a test sample and the center of the closest class-conditional distribution in the latent space , D ( x ) = −min k ‖f ( x ) − ck‖2 . ( 3 ) This distance-based confidence score yields discriminative values between ID and OOD samples . In the experiment section , we show that the Euclidean distance in the latent space of our distance classifier is more effective to detect the samples not belonging to the K classes , compared to the Mahalanobis distance in the latent space of the softmax classifier . Moreover , it does not require further computation to obtain the class means and covariance matrix , and the predictive uncertainty can be measured by a single DNN inference . Relationship to deep one-class classifier . Recent studies on one-class classification , which have been mainly applied to anomaly detection , try to employ DNNs in order to effectively model the normality of a single class . Inspired by early work on one-class classification including one-class support vector machine ( OC-SVM ) ( Schölkopf et al. , 2001 ) and support vector data description ( SVDD ) ( Tax & Duin , 2004 ) , Ruff et al . ( 2018 ; 2019 ) proposed a simple yet powerful deep learning objective , DeepSVDD . It trains the DNNs to map samples of the single known class close to its class center in the latent space , showing that it finds a hypersphere of minimum volume with the center c : min W 1 N N∑ i=1 ‖f ( xi ; W ) − c‖2 . Our DNNs with the distance classifier can be interpreted as an extension of DeepSVDD for multiclass classification , which incorporatesK one-class classifiers into a single network . In the proposed objective ( 1 ) , the first term makes the K classifiers distinguishable for the multi-class setting , and the second term learns each classifier by gathering the training samples into their corresponding center , as done in DeepSVDD . The purpose of the one-class classifier is to determine whether a test sample belong to the target class or not , thus training it for each class is useful for detecting out-of-distribution samples in our task as well .

Summary: This paper proposes a metric learning-based generative model for detecting the out-of-distribution examples.  A new objective function is proposed to model class-dependent class-distribution into a Gaussian analysis models. For the proposed objective, the illustration of derived KL divergence under the Gaussian discriminative analysis assumption is well done.  The empirical results conclude the superiority of the proposed loss function in both tabular and image datasets, when comparing the plain network and one with a softmax.