Zero-Shot Out-of-Distribution Detection with Feature Correlations

1 INTRODUCTION . Even when deep neural networks ( DNNs ) achieve impressive accuracy on challenging tasks , they do not always visibly falter on misclassified examples : in those cases they can often make predictions that are both very confident and completely incorrect . Yet , predictive uncertainty is essential in real-world contexts tolerating minimal error margins such as autonomous vehicle control and medical , financial and legal fields . In this work , we focus on flagging test examples that do not contain any of the classes modeled in the train distribution . Such examples are often referred to as being out-of-distribution ( OOD ) , and while their existence has been well-known for some time , the challenges of identifying them and a baseline method to do so in a variety of tasks such as image classification , text classification , and speech recognition were presented by Hendrycks and Gimpel ( 2017 ) . Recently , Nalisnick et al . ( 2019a ) identified a similar problem with generative models : they demonstrate that flow-based models , VAEs , and PixelCNNs can not distinguish images of common objects such as dogs , trucks , and horses ( i.e . CIFAR-10 ) from those of house numbers ( i.e . SVHN ) , assigning a higher likelihood to the latter when the model is trained on the former . They report similar findings across several other pairs of popular image datasets . While we might expect neural networks to respond differently to OOD examples than to in-distribution ( ID ) examples , exactly where and how to find these differences in activity patterns is not at all clear . Hendrycks and Gimpel ( 2017 ) and others ( Nguyen et al. , 2015 ; Yu et al. , 2011 ) showed that looking at the maximal softmax value is insufficient . In Section 2 we describe some other recent approaches to this problem . In this work , we find that characterizing activity patterns by feature correlations— computed with an extension of Gram matrices that we introduce—lets us quantify anomalies to allow state-of-the-art ( SOTA ) detection rates on OOD examples . Intuition . We identify out-of-distribution examples by jointly considering the class assigned at the output layer and the activity patterns in the intermediate layers . For example , if an image is 1The code for this work is available at https : //github.com/zeroshot-ood/ood-detection predicted to be a dog , yet the intermediate activity patterns are somehow atypical of those seen by the network for other dog images during training , then that is a strong indicator of an OOD example . This effectively allows us to detect incongruence between the prediction made by the network and the path by which it arrived at that prediction . Strengths . Unlike those previous works that assume access to OOD examples and train an auxiliary classifier for identifying anomalous activity patterns , our method finds differences in activity patterns without requiring access to any OOD examples , and it works across architectures . We hope this will also help further our understanding of how neural networks respond differently to OOD examples in general , not just how a particular network responds to examples coming from a particular distribution . Contributions . This work includes the following contributions : 1 . We extend Gram matrices to compute effective feature correlations . 2 . Using the pth-order Gram matrices , we present a new technique for computing classconditional anomalies in activity patterns . 3 . We evaluate this technique on OOD detection , testing on • competitive architectures : DenseNet , ResNet ; • benchmark OOD datasets including : CIFAR-10 , CIFAR-100 , SVHN , TinyImageNet , LSUN and iSUN . 4 . Zero-shot : crucially , our method does not require access to OOD samples for tuning hyperparameters or for training auxiliary models . 5 . Nevertheless , we report results which , for the challenging and important cases of far-fromdistribution examples , are generally better than or equal to the state-of-the-art method for OOD detection that does require access to OOD examples . 2 RELATED WORK . Previous work which aims to improve OOD detection can be roughly grouped by several themes : Bayesian Neural Networks . A nice early Bayesian approach ( Gal and Ghahramani , 2016 ) estimates predictive uncertainty by using an ensemble of sub-networks instantiated by applying dropout at test time . As opposed to implicitly learning a distribution over the predictions by learning a distribution over the weights , Chen et al . ( 2019 ) and Malinin and Gales ( 2018 ) explicitly parameterize a Dirichlet distribution over the output class distributions using DNNs in order to obtain a better estimate of predictive uncertainty ; the main differences between these methods is that Chen et al . ( 2019 ) use ELBO , which only requires the in-distribution dataset for training whereas Malinin and Gales ( 2018 ) use a contrastive loss which requires access to ( optionally synthetic ) OOD examples . Using any pre-trained softmax deep neural network with OOD examples . Lee et al . ( 2018b ) — to the best of our knowledge , the current SOTA technique by a significant margin—compute the Mahalanobis distance between the test sample ’ s feature representations and the class-conditional gaussian distribution at each layer ; they then represent each sample as a vector of the Mahalanobis distances , and finally train a logistic regression detector on these representations to identify OOD examples . Another technique in this category is ODIN ( Liang et al. , 2018 ) : they use a mix of temperature scaling at the softmax layer and input perturbations to achieve better results . In fact , both Lee et al . ( 2018b ) and Liang et al . ( 2018 ) add small input perturbations to achieve better results ; the former do so to increase the confidence score , while the latter do so to increase the softmax score . Recently , Quintanilha et al . ( 2019 ) achieve results comparable to that of Lee et al . ( 2018b ) by training a logistic regression detector that looks at the means and standard deviations of various channels activations . Unlike the previous two techniques , Quintanilha et al . ( 2019 ) achieves comparable results even without the use of input perturbations , which allows it to be applicable to non-continuous domains . Our work , too , does not involve input perturbations . All of these techniques depend on OOD examples for fine-tuning hyperparameters ( Liang et al. , 2018 ) or for training auxiliary OOD classifiers ( Lee et al . ( 2018b ) ; Quintanilha et al . ( 2019 ) ) . Furthermore , these classifiers neither transfer between one non-training distribution and another , nor do they transfer between networks , so separate classifiers must be trained for each ( In-Distribution , OOD , Architecture ) triplet . In many real-world applications , this is infeasible : we can not assume advance access to all possible OOD distributions . Our work does not require access to OOD samples . Alternative Training Strategies . Lee et al . ( 2018a ) jointly train a classifier , a generator and an adversarial discriminator such that the classifier produces a more uniform distribution on the boundary examples generated by the generator ; they use OOD examples to fine-tune hyperparameters . DeVries and Taylor ( 2018 ) train neural networks with a multi-task loss for jointly learning to classify and estimate confidence . Shalev et al . ( 2018 ) use multiple semantic dense representations as the target instead of sparse one-hot vectors and use a cosine-similarity based measure for detecting OODs . Building on the idea proposed by Lee et al . ( 2018a ) , Hendrycks et al . ( 2019a ) propose an Outlier Exposure ( OE ) technique . They regularize a softmax classifier to predict uniform distribution on ( any ) OOD distribution and show the resulting model can identify examples from unseen OOD distributions ; this differs significantly from previous works which used the same OOD distributions for both training and testing . Unlike other methods , they retain the architecture of the classifier and introduce just one additional hyperparameter—the regularization rate—and also demonstrate that their model is quite robust to the choice of OOD examples chosen for the regularization . However , while the OE method is able to generalize across different non-training distributions , it understandably does not achieve the rates of Lee et al . ( 2018b ) on most cases . Recently , Hendrycks et al . ( 2019b ) make significant advances in detecting near-distribution outliers without having any knowledge of the exact out-of-distribution examples by using in-distribution examples in a self-supervised training setting . Generative Models . Ren et al . ( 2019 ) hypothesize that stylistic factors might impact the likelihood assignment and propose to detect OOD examples by computing a likelihood ratio which depends on the semantic factors that remain after the dominant stylistic factors are cancelled out . On the other hand , Nalisnick et al . ( 2019b ) argue that samples generated by a generative model reside in the typical set , which might not necessarily coincide with areas of high density . They demonstrate empirically that OOD examples can be identified by checking if an input resides in the typical set of the generative model . Unlike the standard experimental setting , they aim to identify distributional shift , which predicts if a batch of examples are OOD . 3 EXTENDING GRAM MATRICES FOR OUT-OF-DISTRIBUTION DETECTION . Overview In light of the above considerations , we are interested in proposing a method that does not require access to any OOD examples , that does not introduce hyperparameters that need tuning , and that works across architectures . Gram matrices can be used to compute pairwise feature correlations , and are often used in DNNs to encode stylistic attributes like textures and patterns ( Gatys et al. , 2016 ) . We extend these matrices as will be described below , and then use them to compute class-conditional bounds of pairwise feature correlations at multiple layers of the network . Starting with a pre-trained network , we compute these bounds over only the training set , and can then use them to effectively discriminate between in-distribution samples and out-of-distribution samples at test time . Unlike other SOTA algorithms , we do not need to “ look ” at any out-of-distribution samples to tune any parameters ; the only tuning required is that of a normalizing factor , which we compute using a randomly-selected validation partition of the ( in-distribution ) test set . Notation If the considered deep convolutional network has L layers and the lth layer has nl channels , we consider feature co-occurrences between the ∑ 1 < =l < =L nl∗ ( nl+1 ) 2 pairs of featuremaps . ( Note that by “ layer ” we refer to any set of values obtained immediately after applying convolution or activation functions . ) We use the following notation : Fl ( D ) The feature map at the l-th layer for input image D ; when referring to an arbitrary image D , we just write Fl . It can be stored in a matrix of dimensions nl × pl , where nl is the number of channels at the l-th layer and pl , the number of pixels per channel , is the height times the width of the feature map . Dc/f ( D ) The predicted class for input image D Train The set of all train examples Va The set of all validation examples . 10 % of the examples not used in training are randomly chosen as validation examples . Te The set of all test examples , disjoint as usual from the training and validation sets . We assume that only the test set may contain out-of-distribution examples . Gram Matrices and Higher order Gram Matrices We compute pairwise feature correlations between channels of the l-th layer using the Gram matrix : Gl = FlF > l ( 1 ) where Fl is an nl × pl matrix as defined above . In order to compute feature correlations with more prominent activations of the feature maps , we define a higher-order gram matrix , which we write Gpl , to be a matrix computed identically to the regular Gram matrix , but where , instead of using a raw channel activation a , we use ap , the pth power of each activation . Gpl is therefore computed using F p l , where the power of Fl is computed element-wise ; in an effort to retain uniform scale across all orders of Gram matrices for a given layer , we compute the ( element-wise ) p-th root . The p-th order gram matrix is thus computed as : Gpl = ( F pl F p l > ) 1p ( 2 ) We show in Section 5 that higher p values help significantly in improving the OOD detectability . In our experiments , we limit the value of p to 10 , as exponents beyond 10 are not worth the extra computation that is needed to avoid overflow errors2 . The flattened upper ( or lower ) triangular matrix along with the diagonal entries is denoted as Gpl . The set of all orders of gram matrices ( in our case { 1 , . . . 10 } ) to be considered is denoted by P . The schematic diagram of the proposed algorithm is shown in Fig . 15 ( in Appendix A ) . Preprocessing If we compute Gpl for every layer l and every order p ∈ P , we obtain a total of NS = ∑ p∈P ∑L l=1 1 2nl ( nl+1 ) correlations for any image D. The preprocessing involves computing the class-specific minimum and maximum values for the correlations : for every class c , the minimum and maximum values for each of the NS correlations are computed over all training examples D classified as c. We keep track of the minimum and maximum values of the NS correlations for all the classes in 4-D arrays Mins and Maxs , each of the order ( C × L× |P | × max 1≤l≤L nl ( nl+1 ) 2 ) . Since each layer has different number of channels , the 4-th dimension has been large enough to accommodate the layer with the highest number of channels . Algorithm 1 Compute the minimum and maximum values of feature co-occurrences for each class , layer and order

Summary: This paper proposes a new scoring function for OOD detection based on calculating the total deviation of the pairwise feature correlations. The method only requires in-distribution data for tuning its hyper-parameters and can use a pre-trained classifier directly. Its performance is evaluated with small image datasets, which are commonly used in this line of work. Two additional experiments are performed to analyze the effect of two factors (the layer of the feature and the order of Gram matrix) in the method.