Deep learning models are increasingly deployed in safety-critical applications, where reliable out-of-distribution (OOD) detection is essential to ensure robustness. Existing methods predominantly rely on the penultimate-layer activations of neural networks, assuming they encapsulate the most informative in-distribution (ID) representations. In this work, we revisit this assumption to show that intermediate layers encode equally rich and discriminative information for OOD detection. Based on this observation, we propose a simple yet effective model-agnostic approach that leverages internal representations across multiple layers. Our scheme aggregates features from successive convolutional blocks, computes class-wise mean embeddings, and applies L_2 normalization to form compact ID prototypes capturing class semantics. During inference, cosine similarity between test features and these prototypes serves as an OOD score--ID samples exhibit strong affinity to at least one prototype, whereas OOD samples remain uniformly distant. Extensive experiments on state-of-the-art OOD benchmarks across diverse architectures demonstrate that our approach delivers robust, architecture-agnostic performance and strong generalization for image classification. Notably, it improves AUROC by up to 4.41% and reduces FPR by 13.58%, highlighting multi-layer feature aggregation as a powerful yet underexplored signal for OOD detection, challenging the dominance of penultimate-layer-based methods. Our code is available at: https://github.com/sgchr273/cosine-layers.git.
翻译:深度学习模型越来越多地部署于安全关键应用场景中,其中可靠的分布外(OOD)检测对确保鲁棒性至关重要。现有方法主要依赖神经网络的倒数第二层激活值,假设其封装了最具信息量的分布内(ID)表示。在本工作中,我们重新审视这一假设,证明中间层编码了同样丰富且具有判别力的信息用于OOD检测。基于此发现,我们提出一种简单而有效的模型无关方法,利用跨多个层的内部表示。该方案聚合来自连续卷积块的特征,计算类别均值嵌入,并应用L₂归一化形成紧凑的ID原型以捕捉类别语义。推理阶段,测试特征与这些原型之间的余弦相似度用作OOD评分——ID样本与至少一个原型表现出强亲和性,而OOD样本则均匀远离。在跨多种架构的最先进OOD基准上的大量实验表明,该方法实现了鲁棒且架构无关的性能,并在图像分类中展现出强泛化能力。值得注意的是,它最高将AUROC提升了4.41%,并将FPR降低了13.58%,凸显了多层特征聚合作为OOD检测中强大但未被充分利用的信号,挑战了基于倒数第二层方法的主导地位。我们的代码公开于:https://github.com/sgchr273/cosine-layers.git。