OUT-OF-DISTRIBUTION DETECTION BASED ON IN-DISTRIBUTION DATA PATTERNS MEMO-RIZATION WITH MODERN HOPFIELD ENERGY

Abstract

Out-of-Distribution (OOD) detection is essential for safety-critical applications of deep neural networks. OOD detection is challenging since DNN models may produce very high logits value even for OOD samples. Hence, it is of great difficulty to discriminate OOD data by directly adopting Softmax on output logits as the confidence score. Differently, we detect the OOD sample with Hopfield energy in a store-then-compare paradigm. In more detail, penultimate layer outputs on the training set are considered as the representations of in-distribution (ID) data. Thus they can be transformed into stored patterns that serve as anchors to measure the discrepancy of unseen data for OOD detection. Starting from the energy function defined in Modern Hopfield Network for the discrepancy score calculation, we derive a simplified version SHE with theoretical analysis. In SHE, we utilize only one stored pattern to present each class, and these patterns can be obtained by simply averaging the penultimate layer outputs of training samples within this class. SHE has the advantages of hyperparameterfree and high computational efficiency. The evaluations of nine widely-used OOD datasets show the promising performance of such a simple yet effective approach and its superiority over State-of-the-Art models. Code is available at https://github.com/zjs975584714/SHE ood detection.

1. INTRODUCTION

Deep Neural Network (DNN) has yielded remarkable achievements in a broad range of fields in recent years (He et al., 2016; Huang et al., 2017) , and is extensively deployed in numerous real-world scenarios (Krizhevsky et al., 2017; Redmon & Farhadi, 2017) . One of its powerful capabilities lies in the promising generalization ability from training data to unseen in-distribution (ID) data. However, the finite training data cannot guarantee the completeness of data distribution, so it is inevitable to encounter out-of-distribution (OOD) data. The Softmax-based prediction allows OOD samples to gain high confidence in specific classes, which is unacceptable in practice, especially for safetyrelated areas. It can lead to erroneous collisions in autonomous driving or irreparably large financial losses. Therefore, OOD detection is critical with respect to AI safety (Amodei et al., 2016) . Existing efforts on OOD detection for DNN can be roughly divided into two categories. The first group of approaches requires designing and retraining new auxiliary networks specifically for OOD detection rather than directly using already trained models (Denouden et al., 2018; DeVries & Taylor, 2018; Yu & Aizawa, 2019; Zhang et al., 2020) . The objective should be modified accordingly and OOD samples are sometimes introduced to train the new networks. However, it is almost impossible to exhaust all kinds of OOD samples, and retraining can also be cumbersome. The methods of the second category elaborate on the confidence design for the network outputs, e.g., the logits, the Softmax probability (Liang et al., 2017; Liu et al., 2020; Sun et al., 2021) or embedding features (Lee et al., 2018; Sehwag et al., 2021; Sun et al., 2022) . By these means, there is no need to modify the backbone model and the objective, which motivates us to explore OOD detection in this manner. In deep learning, the intermediate layer output can be regarded as the representation of input data in the latent space. Further, as shown in Figure . 1 (left), guided by the training process, such representations of ID samples of the same category tend to present some common patterns for prediction accuracy. In contrast, these representations of OOD samples should deviate from such commonality since they are not considered during the training process. Based on this intuition, OOD detection can be formulated as a store-then-compare process: representations of ID samples within each category are maintained during the training procedure as stored patterns, and a test pattern will be compared with the store patterns. If there is a noticeable discrepancy, then it can be judged as an OOD sample. The critical question is how to measure the discrepancy between the OOD sample and the stored patterns under this setting? To accomplish this goal, we adopt the key idea of a classic memory network, Hopfield Network. The Hopfield Network (binary state) was first introduced in (Hopfield, 1982) and (Hopfield, 1984) Hopfield Network targets recovering distorted test patterns so that the recovered patterns are as close to the stored patterns as possible. It achieves this goal by specific update rules that minimize the predefined energy function. The more the recovered pattern resembles the stored pattern, the lower the energy is. Therefore, the energy function serves a vital role as it indicates the gap between the recovered patterns and the stored patterns. For OOD detection tasks, the energy function of the Hopfield Network is well-suited as a desirable measure of the discrepancy between the OOD sample and the stored patterns. In this paper, we propose a new OOD detection method HE with memorization of ID data patterns and the Modern Hopfield Energy function (Ramsauer et al., 2020) . In more detail, the representations of training ID samples are stored as patterns for each category in advance, and OOD samples are detected under the energy function. As the intermediate results are more informative than the highly-compressed final output logits, we preserve the outputs of the penultimate layer (i.e., the input of the final output layer) as representations. Furthermore, to address the challenges of the memory cost of pattern memorization, we derive a Simplified Hopfield Energy function-based method SHE. In SHE, only one pattern is required for each category and there is no hyperparameter to be tuned. Theoretical analysis proves the effectiveness of our design. The remarkable performances on nine widely-used OOD detection datasets on three different networks demonstrate the superiority of our proposed SHE (and HE) over state-of-the-art methods. We summarize the main contributions of our paper as follows: • We propose a Modern Hopfield Energy-based method HE for out-of-distribution detection. It uses store-then-compare paradigm that compares test samples with pre-stored patterns to measure the discrepancy from in-distribution data according to Hopfield energy. • We derive a simplified version of HE, named as SHE, which greatly reduces the memory and the computation cost. In addition, SHE is hyperparameter-free. Theoretical analysis is conducted to illustrate the effectiveness of SHE. • Extensive experiments on nine OOD detection datasets of three prominent computer vision backbone networks indicate both the effectiveness and the efficiency of our designed methods. Experiments on large-scale datasets (e.g., ImageNet-1k) also show the superior of our approach. In-depth analysis and ablation studies are also included to shed light on the mechanism behind it.

2. RELATED WORK

Network Redesign and Retrain. Given original network architectures are designed for target tasks like classification, a straightforward paradigm of OOD detection is to elaborate on the network ar-



proposed continuous state version. Modern Hopfield Network (both continuous and binary) was introduced in (Krotov & Hopfield, 2016), and (Ramsauer et al., 2020) proposed a new energy function for continuous state Hopfield networks and point out the relationship with the transformer.

