Kernel PCA for Out-of-Distribution Detection:
Non-Linear Kernel Selection and Approximation


Abstract

Out-of-Distribution (OoD) detection is vital for the reliability of deep neural networks, the key of which lies in effectively characterizing the disparities between OoD and In-Distribution (InD) data. In this work, such disparities are exploited through a fresh perspective of non-linear feature subspaces. That is, a discriminative non-linear subspace is learned from InD features to capture representative patterns of InD, while informative patterns of OoD features cannot be well captured in such a subspace due to their different distribution. Grounded on this perspective, we exploit the deviations of InD and OoD features in such a non-linear subspace for effective OoD detection. To be specific, we leverage the framework of Kernel Principal Component Analysis (KPCA) to attain the discriminative non-linear subspace and deploy the reconstruction error on such subspace to distinguish InD and OoD data. Two challenges emerge: (i) the learning of an effective non-linear subspace, i.e., the selection of kernel function in KPCA, and (ii) the computation of the kernel matrix with large-scale InD data. For the former, we reveal two vital non-linear patterns that closely relate to the InD-OoD disparity, leading to the establishment of a Cosine-Gaussian kernel for constructing the subspace. For the latter, we introduce two techniques to approximate the Cosine-Gaussian kernel with significantly cheap computations. In particular, our approximation is further tailored by incorporating the InD data confidence, which is demonstrated to promote the learning of discriminative subspaces for OoD data. Our study presents new insights into the non-linear feature subspace for OoD detection and contributes practical explorations on the associated kernel design and efficient computations, yielding a KPCA detection framework with distinctively improved efficacy and efficiency.

1 Introduction↩︎

With the rapid advancement of the powerful learning abilities of Deep Neural Networks (DNNs) [1], [2], the trustworthiness of DNNs in security-sensitive scenarios has attracted considerable attention in recent years [3], [4]. Generally, samples from the training set and test set of DNNs are viewed as data from some In Distribution (InD) \(\mathbb{P}_{\rm in}\), while samples from other data sets are regarded as those from a different distribution \(\mathbb{P}_{\rm out}\), i.e., Out-of-Distribution (OoD) data. In practical deployments, DNNs trained on InD data can encounter OoD data and yield unreliable results with potential risks. Therefore, detecting whether a new sample is from \(\mathbb{P}_{\rm in}\) or \(\mathbb{P}_{\rm out}\) has been a valuable research topic in trustworthy deep learning, namely OoD detection [5].

a

b

Figure 1: The t-SNE [6] visualization on the original features \(\boldsymbol{z}\) (left) and the features \(\Phi(\boldsymbol{z})\) in subspace (right). Our KPCA detection method alleviates the linear inseparability between InD and OoD features in the original \(\boldsymbol{z}\)-space via the mapping \(\Phi\) with substantially improved OoD detection performance, illustrated by the distinguishable reconstruction errors..

Great efforts have been taken to put forward the research on OoD detection from varied perspectives. The key idea of those works lies in the proper use of different responses from DNNs, i.e., the logits [7], [8], gradients [9], [10], and features [11][13], to characterize the disparity between InD and OoD data. Among those utilized responses, predictive logits merely encode limited and coarse-grained categorical information, while the parameter gradients necessitate computationally intensive backward propagation processes. More focus has been shed on exploring the features through efficient forward processes, as rich representations in feature spaces enable greater flexibility for more nuanced characterization of the InD-OoD disparity, which is also the main interest in this work.

In feature-based OoD detection, a powerful paradigm is feature rectification, which operates on the premise that extremely large or small feature activations are correlated with anomalous OoD predictions. Such extreme values in features are then clipped (rectified) in different ways to suppress OoD feature responses [11], [14][20]. By contrast, another line of work aims at directly measuring the disparity between InD and OoD features through appropriate distances, e.g., Mahalanobis [21], [22] and \(\ell_2\) distances [12], [23] and other distance metrics [24], [25]. Despite their empirical success, the understandings on the InD-OoD disparity of these methods still remain limited. In feature rectification, the clipping is conducted indiscriminately across all dimensions, which risks discarding useful feature responses for InD predictions. For instance, globally removing the top-10% of feature values over all dimensions is shown to cause nearly a 1.3% accuracy drop on InD data [11]. In distance-based detection, it is assumed that the distance metrics can adequately capture the InD-OoD disparities in the feature space. However, those distance metrics fail fundamentally when handling non-linear patterns commonly encountered in real-world data. An example is presented in [25] that the \(\ell_2\) distance [12] fails to distinguish InD and OoD given a non-linear data distribution of a Swiss Roll shape. These findings underscore the importance of more nuanced interpretations on the InD-OoD disparity in the feature space.

In this work, we take the perspective of exploring the non-linear feature subspace, where InD data can be well described and yet OoD data cannot. To be specific, InD features from DNNs are commonly quite abstract and can intrinsically reside in a low-dimensional subspace, while the OoD features inherently cannot be well characterized by this subspace. Hence, such differences in this subspace can be an effective tool for distinguishing OoD data from InD data. Our perspective fundamentally differs from the aforementioned paradigms that directly operate in the whole feature space from DNNs. Instead, our work focuses on investigating the intrinsic patterns of features in a subspace that well distinguish InD from OoD. We further reveal that the immediate utilization of features or a naive linear subspace from InD data is insufficient in separating OoD data, due to the non-linear characteristics of real-world feature distributions, as discussed in [13], [26], [27]. Nevertheless, the non-linear patterns that relate to the InD-OoD separability have not been fully exploited, and it still remains a significant and open question to identify a discriminative non-linear feature subspace for OoD detection.

In our method, the framework of Kernel Principal Component Analysis (KPCA) [28], [29] is leveraged to construct the non-linear feature subspace, where the InD-OoD disparities are sufficiently revealed. Given a DNN \(f(\boldsymbol{x}):\mathbb{R}^d\rightarrow\mathbb{R}^c\) well-trained on InD data, KPCA is applied to the penultimate layer features \(\boldsymbol{z}\in\mathbb{R}^m\) of InD training samples and learns a non-linear subspace spanned by the principal components. In inference, given features \(\boldsymbol{\hat{z}}\) of an unknown sample \(\boldsymbol{\hat{x}}\), one can obtain the reconstructed counterpart of \(\boldsymbol{\hat{z}}\) by projecting \(\boldsymbol{\hat{z}}\) to the non-linear subspace and re-projecting it back. Consequently, the reconstruction error between \(\boldsymbol{\hat{z}}\) and its reconstructed counterpart can reflect the InD-OoD disparities and thereby serves as a detection metric. Here, InD features are compactly allocated along the axes of principal components, leading to small reconstruction errors, while OoD features, which inherently are not well learned with this subspace, give significantly larger reconstruction errors.

When applying KPCA to the OoD detection task, we emphasize two crucial issues w.r.t. efficacy and efficiency.

  • Non-linear kernel. By deploying KPCA, a non-linear mapping \(\phi\) with its inducing kernel \(k\), i.e., \(k(\boldsymbol{z}_1,\boldsymbol{z}_2)=\langle\phi(\boldsymbol{z}_1),\phi(\boldsymbol{z}_2)\rangle\), is imposed on input features \(\boldsymbol{z}\). To achieve effective OoD detection, the key is the identification of a suitable \(k\) or \(\phi\) that well models the non-linearity reflecting the InD-OoD disparities in the \(\boldsymbol{z}\)-space.

  • Computational complexity. KPCA is generally conducted on the kernel matrix \({\boldsymbol{K}}\in\mathbb{R}^{N_{\rm tr}\times N_{\rm tr}}\) of \(N_{\rm tr}\) training samples. Obviously, when applied to large-scale detection scenarios with a huge InD data size \(N_{\rm tr}\), such as \(N_{\rm tr}=\) 1,281,167 of the ImageNet-1K [30] as InD, KPCA faces nearly prohibitive calculations and a significantly high computational complexity.

In this work, we formulate a KPCA OoD detection framework with the aforementioned two issues resolved. Regarding the kernel selection, we reveal two vital non-linear patterns that closely relate to the InD-OoD disparities in the \(\boldsymbol{z}\)-space, and deduce a Cosine-Gaussian kernel that effectively characterizes such non-linearity. KPCA w.r.t. this Cosine-Gaussian kernel promotes the separability between InD and OoD features and produces substantially improved discriminative reconstruction errors than PCA [31], [32], as demonstrated in Figure 1. On the other hand, to alleviate huge computations on the kernel matrix in the \(\boldsymbol{z}\)-space, we introduce explicit mappings \(\Phi\) to approximate the Cosine-Gaussian kernel. The approximated mapping \(\Phi\) enables KPCA to learn principal components in the \(\Phi(\boldsymbol{z})\)-space and significantly reduces the computational complexity of reconstruction errors. Moreover, in the context of OoD detection, \(\Phi\) is also expected to benefit the differentiation between InD and OoD. In this regard, we explore two representative mappings: the data-independent Random Fourier Features [33] (RFFs) and the data-dependent Nyström method [34], and further refine the latter by involving task-specific insights on the InD data confidence. Our refined Nyström allows KPCA reconstruction errors to be even sensitive to distribution shifts and brings stronger detection results.

To summarize, this study further develops the core idea of KPCA-based reconstruction for OoD detection from our prior conference publication [35] into a formalized and principled KPCA detection framework, with main contributions below:

  • On non-linear kernel selection. We reveal two non-linear patterns essential for InD-OoD separability: imbalanced feature norms and \(\ell_2\) feature distances, in Section 3. These two properties not only serve as principled justifications for the Cosine-Gaussian kernel proposed in [35], but also provide crucial design guidelines for selecting and understanding kernel representations specific to differentiate InD and OoD. These insights are neither presented nor sufficiently analyzed in [35].

  • On task-driven kernel approximation. We incorporate a data-dependent Nyström approximation technique with a low-Energy sampling scheme specialized for the OoD detection task in Section 4.2. By reallocating landmarks toward boundary regions, this scheme actively promotes InD-OoD separability rather than simply preserving the global kernel reconstruction, yielding superior detection performance and reduced computational costs (Section 4.3). Together with the data-independent RFFs in [35], this work presents systematic explorations on efficient kernel approximation particularly tailored for OoD detection. Numerical approximation analyzes are provided in Section 4.4.

  • On learnable kernel representation. We further explore a parametric realization of our KPCA OoD detection framework, as an extension of the non-parametric Cosine-Gaussian kernel in [35]. This parametric learning paradigm supports learnable representations by optimizing kernel hyper-parameters and training neural networks with varied objectives, moving beyond fixed feature projections, as in Section 4.5 and Appendix 10. These primary and yet valuable explorations address the limitation noted in [35] by showcasing data-driven kernel optimization for future work, and also further validate the flexibility of our framework.

  • Regarding empirical results. We conduct extensive experiments in Section 5 to validate the new State-Of-The-Art (SOTA) results and low computational demands of our KPCA detection framework, including comparisons with a broader variety of latest strong baselines and in-depth analyses for the two approximation methods.

In the remainder, the background knowledge is outlined in Section 2. Section 3 presents the non-linear kernel selection in detail and Section 4 investigates our approximation techniques for efficient computations and explores learnable kernel representations. Extensive empirical results are shown in Section 5. Related works and concluding discussions are enclosed in Section 6 and Section 7, respectively.

2 Background↩︎

2.1 Out-of-Distribution Detection↩︎

Generally, an OoD detection method justifies whether an input is from InD \(\mathbb{P}_{\rm in}\) or OoD \(\mathbb{P}_{\rm out}\) by a well-designed scoring function \(S(\cdot)\) as follows: \[\label{eq:ood-scoring} \text{category\;of\;}\boldsymbol{x}= \left\{ \begin{array}{ll} \mathrm{InD},&S(\boldsymbol{x})\geq s,\\ \mathrm{OoD},&S(\boldsymbol{x})<s. \end{array} \right.\tag{1}\] The scoring function \(S(\cdot)\) assigns a score \(S(\boldsymbol{x})\) for an input \(\boldsymbol{x}\). If \(S(\boldsymbol{x})\) is greater than a threshold \(s\), \(\boldsymbol{x}\) is viewed as an InD sample, and vice versa. The threshold \(s\) is usually selected such that most InD samples of the test dataset can be correctly identified. Obviously, the detection performance depends on how well the scoring function \(S(\cdot)\) characterizes the InD-OoD disparity. Existing methods leverage different outputs from DNNs and design justified scores correspondingly; a thorough review can refer to Section 6.

2.2 PCA for Out-of-Distribution Detection↩︎

The linear method, PCA [31], [32], is considered for OoD detection [13]. For inputs \(\boldsymbol{x}\in\mathbb{R}^d\), a well-trained DNN \(f:\mathbb{R}^d\rightarrow\mathbb{R}^c\) learns features \(\boldsymbol{z}\in\mathbb{R}^m\) before the last linear layer. In the learning phase, PCA firstly calculates the centered covariance matrix \({\boldsymbol{\Sigma}}\in\mathbb{R}^{m\times m}\) on features \(\{\boldsymbol{z}_i\}_{i=1}^{N_{\rm tr}}\) of the training (InD) data \(\{\boldsymbol{x}_i\}_{i=1}^{N_{\rm tr}}\): \[\label{eq:covar-mat} \boldsymbol{\Sigma}=\sum\nolimits_{i=1}^{N_{\rm tr}}\left(\boldsymbol{z}_i-\boldsymbol{\mu}_{\rm tr} \right)\left(\boldsymbol{z}_i-\boldsymbol{\mu}_{\rm tr}\right)^\top,\tag{2}\] with \(\boldsymbol{\mu}_{\rm tr}={1}/{N_{\rm tr}}\sum_{i=1}^{N_{\rm tr}}\boldsymbol{z}_i\). Through the eigendecomposition \(\boldsymbol{\Sigma}={\boldsymbol{U}}{\boldsymbol{\Lambda}}{\boldsymbol{U}}^\top\), the projection matrix \({\boldsymbol{U}}_q\in\mathbb{R}^{m\times q}\) is obtained by taking the first \(q\) columns of the eigenvector matrix \({\boldsymbol{U}}\in\mathbb{R}^{m\times m}\) w.r.t. the top-\(q\) eigenvalues in \({\boldsymbol{\Lambda}}\in\mathbb{R}^{m\times m}\).

In the inference phase, given a new sample \(\boldsymbol{\hat{x}}\in\mathbb{R}^d\) and its feature \(\boldsymbol{\hat{z}}\in\mathbb{R}^m\) from the DNN \(f\), we can project the centered \(\boldsymbol{\hat{z}}-\boldsymbol{\mu}_{\rm tr}\) to the \({\boldsymbol{U}}_q\)-subspace and re-project it back. The reconstruction error is computed as: \[\label{eq:reconstruction-error} e(\boldsymbol{\hat{x}})=\left\Vert{\boldsymbol{U}}_q{\boldsymbol{U}}_q^\top(\boldsymbol{\hat{z}}-\boldsymbol{\mu}_{\rm tr})-(\boldsymbol{\hat{z}}-\boldsymbol{\mu}_{\rm tr})\right\Vert_2.\tag{3}\] The subspace spanned by \({\boldsymbol{U}}_q\) is learned from InD data and thus gives high variances along principal components with informative patterns of InD, thereby yielding small reconstruction errors. In contrast, OoD data exhibits different patterns from InD data. Thus, in the low-dimensional \({\boldsymbol{U}}_q\)-subspace, OoD data is prone to produce large reconstruction errors along these extracted principal components. The scoring function with PCA for OoD detection is thereby set as \(S(\boldsymbol{\hat{x}})=-e(\boldsymbol{\hat{x}})\).

2.3 Kernel Approximation↩︎

We outline the approximation techniques adopted in our KPCA detection framework. In large-scale kernel methods, the expensive computations have been a long-standing problem, as an \(N_{\rm tr}\times N_{\rm tr}\) kernel matrix \(\boldsymbol{K}\) on training data requires \(\mathcal{O}(N_{\rm tr}^2)\) kernel evaluations, \(\mathcal{O}(N_{\rm tr}^2)\) space complexity, and \(\mathcal{O}(N_{\rm tr}^3)\) time complexity in solving the optimization problem. To alleviate such computational burden, the data-independent random Fourier features [33] and the data-dependent Nyström method [34] are two mainstream methods.

2.3.0.1 Random Fourier features

Random Fourier Features (RFFs) [33] deploy explicit mappings with random features to approximate the kernel function \(k\). RFFs are based on Bochner’s theorem [36] and construct random features \(\phi_{\rm rff}\) for a shift-invariant kernel \(k(\boldsymbol{z}_1,\boldsymbol{z}_2)=k(\boldsymbol{z}_1-\boldsymbol{z}_2)\): \[\begin{align} \label{eq:rff} \phi_{\rm rff}(\boldsymbol{z}) &\triangleq\sqrt{\frac{2}{M_r}}\left[\phi_1(\boldsymbol{z}),\cdots,\phi_{M_r}(\boldsymbol{z})\right],\\ \phi_i(\boldsymbol{z}) &=\cos\;(\boldsymbol{z}^\top\boldsymbol{\omega}_i+u_i),i=1,\cdots,M_r, \end{align}\tag{4}\] where \(M_r\) denotes the number of RFFs, and \(\boldsymbol{\omega}_i\in\mathbb{R}^m\) and \(u_i\in\mathbb{R}\) are i.i.d. sampled from the Fourier transform of the kernel function \(k(\cdot)\) and a uniform distribution \(\mathcal{U}(0,2\pi)\), respectively. We highlight two key properties of RFFs. (i) Computing the explicit mapping \(\phi_{\rm rff}(\boldsymbol{z})\in\mathbb{R}^{M_r}\), rather than the kernel matrix \(\boldsymbol{K}\), can be significantly more efficient when \(M_r\ll N_{\rm tr}\), while one could still guarantee \(k(\boldsymbol{z}_1,\boldsymbol{z}_2)\approx\phi_{\rm rff}(\boldsymbol{z}_1)^\top\phi_{\rm rff}(\boldsymbol{z}_2)\)[33]. (ii) The sampling distributions of \(\boldsymbol{\omega}_i\) and \(u_i\) are data-agnostic, and thereby RFFs are data-independent.

2.3.0.2 Nyström method

Instead of the explicit random features, the Nyström method [34] presents low-rank approximation to the kernel matrix through finite sampling. Firstly, \(M_n\) landmark samples \(\{\boldsymbol{\tilde{z}}_i\}_{i=1}^{M_n}\) are selected from the training set \(\{\boldsymbol{z}_i\}_{i=1}^{N_{\rm tr}}\) (\(M_n\ll N_{\rm tr}\)), and then we can draw sub-matrices \({\boldsymbol{\tilde{K}}}\in\mathbb{R}^{{M_n}\times{M_n}}\) and \({\boldsymbol{\bar} K}\in\mathbb{R}^{{N_{\rm tr}}\times{M_n}}\) from the full kernel matrix \(\boldsymbol{K}\) as \({\boldsymbol{\tilde{K}}}_{ij}=k(\boldsymbol{\tilde{z}}_i,\boldsymbol{\tilde{z}}_j)\) and \({\boldsymbol{\bar} K}_{ij}=k(\boldsymbol{z}_i,\boldsymbol{\tilde{z}}_j)\). Nyström method constructs a low-rank approximation of \(\boldsymbol{K}\) as \({\boldsymbol{\tilde{K}}}_n=\boldsymbol{\bar} K\boldsymbol{\tilde{K}}^\dagger\boldsymbol{\bar} K^\top\), where \(^\dagger\) denotes the matrix pseudo-inverse. Obviously, Nyström method is data-dependent and accelerates computations by working on the approximation associated with the selected \(M_n\) landmarks.

Figure 2: The framework of our KPCA detection method. A Cosine-Gaussian kernel is devised to model the non-linearity related to InD-OoD disparity in the \boldsymbol{z}-space (Section 3). Explicit mappings \Phi are built to approximate the Cosine-Gaussian kernel for efficient computations in the \Phi(\boldsymbol{z})-space (Section 4). The left and right histograms indicate the PCA and KPCA reconstruction errors on InD and OoD data, respectively, implying the effectiveness of the Cosine-Gaussian kernel in promoting the linear separability of between InD and OoD data.

3 Non-Linear Kernel for OoD Detection↩︎

In our work, we leverage Kernel PCA (KPCA) [28], [29] to identify discriminative non-linear feature subspaces for distinguishing InD and OoD data. The key of KPCA lies in finding proper kernels to model the non-linearity that closely relates to InD-OoD separability in the \(\boldsymbol{z}\)-space, so as for discriminative non-linear principal components and distinguishable reconstruction errors on InD and OoD data.

Our KPCA detection framework is illustrated in Figure 2. By analyzing InD and OoD features in the \(\boldsymbol{z}\)-space, we identify two non-linear patterns that reflect their disparities and derive a Cosine-Gaussian kernel to model such non-linearity: alleviating the adverse imbalance of feature norms through a Cosine kernel in Section 3.1 and promoting the beneficial feature distance relation by a Gaussian kernel in Section 3.2.

3.1 Adverse Imbalance of Feature Norms↩︎

Since InD and OoD data are assumed to be from different distributions, a substantial gap exists in their feature norms \(\|\boldsymbol{z}\|_2\). As shown in Figure 3 (a), samples in the CIFAR10 training and test datasets are from InD, and thus have similar feature norms, while samples from OoD datasets exhibit significant deviations in feature norms. In this subsection, we reveal that such norm imbalance between InD and OoD features has an adverse impact for OoD detection by violating the zero-mean assumption, when simply considering the linear subspace from PCA. Then, we further show that a Cosine kernel \(k_{\rm cos}\) can effectively mitigate this issue with KPCA.

An essential pre-processing step of PCA is the centering on the inputs, i.e., inputs subtracted by their means in both learning and inference phases (ref. \((\boldsymbol{z}_i-\boldsymbol{\mu}_{\rm tr})\) in Eqn.@eq:eq:covar-mat and \((\boldsymbol{\hat{z}}-\boldsymbol{\mu}_{\rm tr})\) in Eqn.@eq:eq:reconstruction-error ). In the learning phase, centering is applied to the training data for computing the covariance matrix and the principal components. Particularly, in inference, the centering is also required for given new inputs. In OoD detection, the available feature mean for centering is the mean of InD training features, i.e., \(\boldsymbol{\mu}_{\rm tr}\) in Eqn.@eq:eq:covar-mat , since the feature mean of OoD is unknown. Meanwhile, note that the InD and OoD feature norms are imbalanced, which naturally induces a significant offset between their respective means, as shown by the right dashed line in Figure 3 (b). Therefore, centering OoD features using the InD feature means \(\boldsymbol{\mu}_{\rm tr}\) is impractical for PCA, which results in misleading projections in the subspace and poor performance of reconstruction errors in Figure 3 (c).

Figure 3: Illustrations on the impact of the cosine kernel k_{\rm cos}. (a) Density histograms on the imbalance of InD and OoD feature norms \|\boldsymbol{z}\|_2. (b) The cosine kernel k_{\rm cos} significantly reduces the distance between the InD feature means and the OoD feature means, which benefits the centering process in PCA. The red stars denote the feature means. The dashed and solid lines denote the distance between feature means without and with the cosine kernel k_{\rm cos}, respectively. (c)&(d) The cosine kernel k_{\rm cos} leads to more separable reconstruction errors of InD and OoD features and thereby boosts OoD detection performance. InD: CIFAR10 [37]. OoD: LSUN [38] and places365 [39].

In this work, we note that a Cosine kernel \(k_{\rm cos}\) defined below can well address this issue. \[\begin{align} \label{eq:kernel-cosine} k_{\rm cos}(\boldsymbol{z}_1,\boldsymbol{z}_2) &=\frac{\boldsymbol{z}_1^\top\boldsymbol{z}_2}{\|\boldsymbol{z}_1\|_2\cdot\|\boldsymbol{z}_2\|_2} =\phi_{\rm cos}(\boldsymbol{z}_1)^\top\phi_{\rm cos}(\boldsymbol{z}_2),\\ \phi_{\rm cos}(\boldsymbol{z})&=\frac{\boldsymbol{z}}{\|\boldsymbol{z}\|_2}. \end{align}\tag{5}\] \(k_{\rm cos}\) is exactly induced from a non-linear mapping \(\phi_{\rm cos}(\boldsymbol{z})\) that restricts inputs \(\boldsymbol{z}\) onto a unit norm ball. By this construction, given the InD and OoD features, \(k_{\rm cos}\) inherently maps them to the unit sphere and thereby mitigates the adverse impact of their imbalanced feature norms. In this way, the offset between InD and OoD feature means gets significantly reduced, as indicated by the left solid shorter line in Figure 3 (b). Therefore, the centering based on \(\boldsymbol{\mu}_{\rm tr}\) w.r.t. the training data can now allocate OoD features nearby the same origin with InD features, so that the projection matrix learned from InD data can also be effective for OoD data, resulting in discriminative reconstruction errors in Figure 3 (d). In other words, a justified \(\phi_{\rm cos}(\boldsymbol{z})\)-space w.r.t. the cosine kernel \(k_{\rm cos}\), instead of the original \(\boldsymbol{z}\)-space, ensures that the subspace spanned by the principal components can well characterize the InD-OoD disparity.

3.2 Beneficial feature distance relation↩︎

After the imbalance of InD and OoD feature norms gets alleviated through \(k_{\rm cos}\), the resulting \(\phi_{\rm cos}(\boldsymbol{z})\)-space further enables explorations on another non-linear pattern related to the InD-OoD disparity, i.e., the beneficial \(\ell_2\) feature distance relation, which could even promote the InD-OoD separability.

In Figure 4, Multi-Dimensional Scaling (MDS) [40] is applied to InD and OoD data with and without the cosine kernel \(k_{\rm cos}\), respectively. MDS seeks low-dimensional embeddings where the \(\ell_2\) distance aligns well with that in the original space, making it an effective tool for analyzing the underlying \(\ell_2\) distance structure of the data. As illustrated in Figure 4, InD data in the \(\phi_{\rm cos}(\boldsymbol{z})\)-space is more concentrated w.r.t. the MDS embeddings built from the \(\ell_2\) distance information. Aside from the MDS visualizations, another work [12] directly calculates the \(\ell_2\) distances on \(\ell_2\)-normalized InD and OoD features as the detection score. Empirical results in [12] also indicate that the InD-InD \(\ell_2\) distances are significantly smaller than the InD-OoD \(\ell_2\) distances in the \(\phi_{\rm cos}(\boldsymbol{z})\)-space. All these evidences suggest (i) the insufficiency of the cosine kernel \(k_{\rm cos}\) alone in capturing the non-linearity related to the InD-OoD disparity and (ii) the promising potentials of leveraging the \(\ell_2\) distance relation in the \(\phi_{\rm cos}(\boldsymbol{z})\)-space for separating InD and OoD data.

Note that a Gaussian kernel \(k_{\rm gau}\), defined as \[\label{eq:kernel-gau} k_{\rm gau}(\boldsymbol{z}_1,\boldsymbol{z}_2)=e^{-\gamma\|\boldsymbol{z}_1-\boldsymbol{z}_2\|^2_2},\tag{6}\] is exactly able to preserve the \(\ell_2\) distance information between samples. Therefore, based on the above investigations, we leverage the Gaussian kernel \(k_{\rm gau}\) on top of the cosine kernel \(k_{\rm cos}\) to further model the discriminative \(\ell_2\) feature distance existing in InD and OoD data in the \(\phi_{\rm cos}(\boldsymbol{z})\)-space.

Figure 4: Low dimensional embeddings via MDS [40] on InD and OoD data in the \boldsymbol{z}-space and \phi_{\rm cos}(\boldsymbol{z})-space, respectively. InD: CIFAR10 [37]. OoD: LSUN [38] and places365 [39].

To sum up, the eventual kernel we propose for the OoD detection task is a composite Cosine-Gaussian kernel. This Cosine-Gaussian kernel models two vital non-linear properties hidden in InD and OoD features: alleviating the adverse imbalance of feature norms \(\|\boldsymbol{z}\|_2\) and promoting the beneficial \(\ell_2\) distance relation, and finally enables discriminative non-linear feature subspaces for separating InD and OoD data.

4 Kernel Approximation for OoD Detection↩︎

Given the derived Cosine-Gaussian kernel for modeling the non-linearity, we even face challenges of the nearly prohibitive calculations on the kernel matrix \({\boldsymbol{K}}\in\mathbb{R}^{N_{\rm tr}\times N_{\rm tr}}\) under large-scale detection scenarios with a huge InD data size \(N_{\rm tr}\), e.g., the common ImageNet-1K training set with \(N_{\rm tr}=\)​1,281,167. For efficient computations, we introduce approximation techniques in kernel methods, i.e., using an explicit non-linear mapping \(\Phi\) to approximate the Cosine-Gaussian kernel, as illustrated in our framework of Figure 2. In this way, KPCA learns non-linear principal components in the mapped \(\Phi\)-space, which excludes the kernel matrix \(\boldsymbol{K}\) and leads to a significantly reduced computational complexity for calculating the reconstruction error as the detection score.

In the composite Cosine-Gaussian kernel, the Cosine kernel \(k_{\rm cos}\) directly admits an exact mapping \(\phi_{\rm cos}\) in Eqn.@eq:eq:kernel-cosine . In contrast, the mapping associated with the Gaussian kernel \(k_{\rm gau}\) is infinite-dimensional. Nevertheless, \(k_{\rm gau}\) can still be effectively approximated through finite-dimensional mappings. Particularly, we explore two representative approximation mappings that differ in whether data is involved: (i) data-independent random Fourier features [33] in Section 4.1, and (ii) data-dependent Nyström approximation [34] specifically tailored for OoD detection in Section 4.2. Discussions on the advantageous efficiency and analytical approximation properties are presented in Section 4.3 and Section 4.4.

4.1 Random Fourier Features↩︎

The Random Fourier Features (RFFs) [33] are leveraged to approximate the Gaussian kernel \(k_{\rm gau}\). As outlined in Section 2.3, to build the approximated mapping \(\phi_{\rm rff}\) in Eqn.@eq:eq:rff , \(2M_r\) randomly-sampled weights \(\boldsymbol{w}\) and \(u\) are required. To be specific, \(M_r\) weights \(u\) are sampled from a uniform distribution: \(u\sim\mathcal{U}(0,2\pi)\), and the sampling distribution for \(\boldsymbol{\omega}\) is the Fourier transform of the Gaussian kernel \(k_{\rm gau}\), i.e., \(\boldsymbol{\omega}\sim\mathcal{N}({\boldsymbol{0}},\sqrt{2\gamma}{\boldsymbol{I}}_m)\), where \(\gamma\) is the Gaussian kernel width. Hence, \(\phi_{\rm rff}\) in RFFs is agnostic of data and can be straightforwardly applied.

Note that the Cosine-Gaussian kernel in our KPCA detection method is a composite of kernel functions. The final non-linear mapping \(\Phi\) is then formulated as \(\Phi(\boldsymbol{z})\triangleq\phi_{\rm rff}(\phi_{\rm cos}(\boldsymbol{z}))\). Given the mapping \(\Phi\), in the learning phase, the training InD features \(\{\boldsymbol{z}_i\}_{i=1}^{N_{\rm tr}}\) are mapped to \(\Phi\)-space, and a projection matrix \({\boldsymbol{U}}_q^\Phi\) is obtained by the eigendecomposition on the covariance matrix \({\boldsymbol{\Sigma}}^\Phi\) induced from \(\{\Phi(\boldsymbol{z}_i)\}_{i=1}^{N_{\rm tr}}\). In the inference phase, features \(\boldsymbol{\hat{z}}\) of a new sample \(\boldsymbol{\hat{x}}\) are firstly mapped to \(\Phi(\boldsymbol{\hat{z}})\) and then compute the reconstruction error \(e^\Phi\) based on \({\boldsymbol{U}}_q^\Phi\) and the mean \(\boldsymbol{\mu}^\Phi_{\rm tr}\). Algorithm 5 outlines the key steps.

Figure 5: The KPCA framework for OoD Detection

4.2 Nyström Approximation↩︎

In this section, we elaborate how to leverage the Nyström method [34] to construct an explicit mapping to approximate the Gaussian kernel for efficient OoD detection. As outlined in Section 2.3, Nyström is commonly executed to build a low-rank approximation matrix \({\boldsymbol{\tilde{K}}}_n\) based on \(M_n\) landmarks \(\{\boldsymbol{\tilde{z}}_i\}_{i=1}^{M_n}\) selected from \(N_{\rm tr}\) training samples \(\{\boldsymbol{z}_i\}_{i=1}^{N_{\rm tr}}\). This approximated \({\boldsymbol{\tilde{K}}}_n\) can further be decomposed as: \[\begin{align} \label{eq:nys} [{\boldsymbol{\tilde{K}}}_n]_{ij}=\phi_{\rm nys}(\boldsymbol{z}_i)^\top\phi_{\rm nys}(\boldsymbol{z}_j),\quad \phi_{\rm nys}(\boldsymbol{z}) \triangleq{\boldsymbol{\tilde{\Lambda}}}^{-1/2}{\boldsymbol{\tilde{U}}}^\top[k(\boldsymbol{z},\boldsymbol{\tilde{z}}_1),\cdots,k(\boldsymbol{z},\boldsymbol{\tilde{z}}_{M_n})]^\top. \end{align}\tag{7}\] Note that \({\boldsymbol{\tilde{U}}}\) and \({\boldsymbol{\tilde{\Lambda}}}\) are obtained through the eigendecomposition on the submatrix \({\boldsymbol{\tilde{K}}}={\boldsymbol{\tilde{U}}}{\boldsymbol{\tilde{\Lambda}}}{\boldsymbol{\tilde{U}}}^\top\) with \({\boldsymbol{\tilde{K}}}_{ij}=k(\boldsymbol{\tilde{z}}_i,\boldsymbol{\tilde{z}}_j)\). In this sense, the explicit mapping \(\phi_{\rm nys}\) can be adopted as a data-driven approximation towards the Gaussian kernel \(k_{\rm gau}\) through the selected \(M_n\) landmarks.

Obviously, the sampling scheme for the landmarks directly determines the approximation performance of \(\phi_{\rm nys}\), with studies ranging from naive uniform sampling to more sophisticated strategies [41], [42]. Nevertheless, in OoD detection contexts, the approximation performance of the Nyström mapping is no longer the main concern; instead, we aim to identify a set of particular InD landmarks to build the approximated mapping, such that the mapped space w.r.t. these specific landmarks can help reflect the InD-OoD disparity and facilitate the subspace learning for effective OoD detection.

In this work, an energy-based sampling scheme is designed for Nyström, tailored to boost OoD detection performance. To be specific, we consider the following energy function \(E\) applied to the logits of training samples \(\{\boldsymbol{x}_i\}_{i=1}^{N_{\rm tr}}\) from the well-trained DNN \(f\): \[\label{eq:energy-func} E(\boldsymbol{x};f)=E(\boldsymbol{z};f)=T\cdot\log\sum_{i=1}^{c}{\rm exp}\left(\frac{f_i(\boldsymbol{x})}{T}\right),\tag{8}\] where \(f_i(\boldsymbol{x})\) denotes the \(i\)-th element of the logits \(f(\boldsymbol{x})\in\mathbb{R}^c\) and \(T\) indicates the temperature coefficient. Here, high-energy samples correspond to those predictions, where the DNN \(f\) assigns a dominant logit to one class, implying that those samples are likely from InD. In contrast, low-energy samples demonstrate dispersed or nearly uniform logit distributions, indicating that they tend to align with OoD [7].

The proposed energy-based sampling scheme selects \(M_n\) low-energy InD training samples \(\{\boldsymbol{\tilde{x}}_i\}_{i=1}^{M_n}\) w.r.t. the smallest energy values \(E(\boldsymbol{\tilde{x}}_i;f)\) as landmarks to build the Nyström approximation mapping \(\phi_{\rm nys}\) of Eqn.@eq:eq:nys . These landmarks are from InD and typically reside near the InD-OoD boundary, where \(f\) is less confident on them. For KPCA-based OoD detection, the detection threshold is governed by the largest gap in landmark coverage over the InD support, since the reconstruction error at any point can be controlled by its distance to nearby landmarks. Uniform or density-proportional sampling concentrates landmarks in high-density regions, leaving large coverage gaps near low-density boundary areas, where the worst-case reconstruction error tends to be most pronounced. Our low-Energy scheme reallocates landmarks toward these under-covered boundary regions, thereby shrinking the largest coverage gap and suppressing the detection threshold. This yields a more compact acceptance region and directly limits false acceptance of OoD samples. We formalize this reasoning as follows, with an intuitive illustration in Figure 6.

Proposition 1. Let \(\mathcal{Z}_{\rm in} \subset \mathbb{R}^m\) be the support of InD features and \(e(\boldsymbol{z}; \tilde{\mathbf{Z}})\) be the KPCA reconstruction error derived from a landmark set \(\tilde{\mathbf{Z}} = \{\tilde{\boldsymbol{z}}_i\}_{i=1}^{M_n}\). By reallocating landmarks from a global uniform distribution to the low-Energy regions of \(\mathcal{Z}_{\rm in}\), the detection threshold \(\gamma\) necessary to encompass the InD manifold can be effectively suppressed, yielding a more compact acceptance region \(\mathcal{A} = \{\boldsymbol{z} \in \mathbb{R}^m \mid e(\boldsymbol{z}; \tilde{\mathbf{Z}}) \le \gamma\}\).

Based on the proposed low-energy supporting landmarks, the final non-linear mapping \(\Phi\) via Nyström approximation for the Cosine-Gaussian kernel is \(\Phi(\boldsymbol{z})\triangleq\phi_{\rm nys}(\phi_{\rm cos}(\boldsymbol{z}))\). The corresponding KPCA detection procedure is basically the same as KPCA via RFFs in Section 4.1, which is also outlined in Algorithm 5. We use KPCA\(_{\rm rff}\) and KPCA\(_{\rm nys}\) to denote our KPCA detection method via RFFs and Nyström, respectively.

Figure 6: An illustration on the comparisons between the naive uniform sampling and the proposed low-energy sampling, and their corresponding subspaces.

4.3 Computation Complexity↩︎

With approximation schemes, the computational efficiency of our KPCA detection framework is greatly improved. As illustrated in Algorithm 5, the computation of the kernel matrix K w.r.t. \(N_{\rm tr}\) training samples is skipped. Particularly, in the inference phase, the KPCA reconstruction error \(e^\Phi\) requires the approximated mapping \(\Phi\), the projection matrix \({\boldsymbol{U}}^\Phi_q\) and the mean mapped training feature vector \(\boldsymbol{\mu}^\Phi\). Both \({\boldsymbol{U}}^\Phi_q\) and \(\boldsymbol{\mu}^\Phi\) are computed only once in the learning phase and thus stored in preparation for inference. Therefore, the computation cost of \(\rm KPCA_{rff}\) and \(\rm KPCA_{nys}\) in inference comes from the construction of the explicit mapping \(\Phi\) on new features \(\boldsymbol{\hat{z}}\).

Regarding \(\Phi\), the primary mapping \(\phi_{\rm cos}\) w.r.t. the common cosine kernel \(k_{\rm cos}\) in \(\rm KPCA_{rff}\) and \(\rm KPCA_{nys}\) is an in-place operation and does not require additional computations. Thus, the mappings \(\phi_{\rm rff}\) and \(\phi_{\rm nys}\) to approximate the Gaussian kernel \(k_{\rm gau}\) take the main computation:

  • As illustrated in Eqn.@eq:eq:rff of \(\phi_{\rm rff}\), \(2M_r\) samplings of \(\boldsymbol{\omega}_i\) and \(u_i\), \(M_r\) dot-products and \(M_r\) additions are required for \(\phi_{\rm RFF}(\boldsymbol{\hat{z}})\). Accordingly, the time and memory complexity of \(\rm KPCA_{rff}\) is \({\cal O}(M_r)\).

  • To build \(\phi_{\rm nys}(\boldsymbol{\hat{z}})\) by Eqn.@eq:eq:nys , two eigenmatrices \(\boldsymbol{\tilde{\Lambda}}\), \(\boldsymbol{\tilde{U}}\) and \(M_n\) kernel evaluations on the \(M_n\) landmarks \(\{\boldsymbol{\tilde{z}}_i\}_{i=1}^{M_n}\) are demanded. Hence, the time and memory complexity of \(\rm KPCA_{nys}\) is \({\cal O}(M_n)\).

Note that there exists a relevant method KNN [12] that also explores the imbalanced feature norms and the beneficial \(\ell_2\) distance for OoD detection. KNN takes a rather straightforward way and adopts the non-parametric \(\ell_2\)-distance nearest neighbor searching on \(\ell_2\)-normalized inputs as the score: \[\label{eq:knn-score} S_{\rm knn}(\boldsymbol{\hat{x}})=-\min_{i:1,\cdots,N_{\rm tr}}\left\|\frac{\boldsymbol{\hat{z}}}{\|\boldsymbol{\hat{z}}\|_2}-\frac{\boldsymbol{z}_i}{\|\boldsymbol{z}_i\|_2}\right\|_2.\tag{9}\] Aside from the distinctive kernel perspective, our KPCA further outperforms KNN in the computational overhead. As in Eqn.@eq:eq:knn-score , KNN requires that all the training features \(\{\boldsymbol{z}_i\}_{i=1}^{N_{\rm tr}}\) have to be stored at hand and iterated in inference, implying a heavy \(\mathcal{O}(N_{\rm tr})\) time and memory complexity. In contrast, the \({\cal O}(M_r)\)/\({\cal O}(M_n)\) of \(\rm KPCA_{rff}\)/\(\rm KPCA_{nys}\) is substantially more efficient than the \(\mathcal{O}(N_{\rm tr})\) of KNN given that \(M_r,M_n\ll N_{\rm tr}\). Detailed comparisons on detection performance and efficiency are provided in Sections 5.2 and 5.3.

4.4 Analytical Discussion on Approximation Performance↩︎

The primary objective of our approximated KPCA is to enhance OoD detection performance. In this section, we supplement an analysis of the approximation performance on InD and OoD data for a comprehensive investigation into our detection method. While the statistical optimality of approximated KPCA via RFFs and Nyström has been extensively studied within the traditional machine learning regime under well-defined assumptions [43][48], we extend the analysis to this deep learning regime, bridge theoretical approximation guarantees with practical detection scenarios, and provide actionable insights for implementation.

We consider the empirical version for the exact KPCA on a finite training set \(\{\boldsymbol{z}_i\}_{i=1}^{N_{\rm tr}}\) without any approximations. This requires that the reconstruction error should be calculated in the original \(\boldsymbol{z}\)-space based on the kernel matrix \({\boldsymbol{K}}\in\mathbb{R}^{N_{\rm tr}\times N_{\rm tr}}\). To be specific, projections to the principal subspace based on \(\boldsymbol{K}\) can be easily implemented [28], [29], but how to reconstruct the projections in the principal subspace back to the original \(\boldsymbol{z}\)-space remains a non-trivial issue, known as the pre-image problem [49]. To address this issue, the following Proposition 2 [35] shows a flexible approach to directly obtain the KPCA reconstruction error \(e^{k}\) without building reconstructed features.

Given the eigen-decomposition on the kernel matrix \(\boldsymbol{K}\) of the training set \(\{\boldsymbol{z}_i\}_{i=1}^{N_{\rm tr}}\) as \({\boldsymbol{K}}={\boldsymbol{U}}^{k}{\boldsymbol{\Lambda}}^{k}{\boldsymbol{U}}^{{k}\top}\), and \(\boldsymbol{k}_{\boldsymbol{\hat{z}}}=[k(\boldsymbol{z}_1,\boldsymbol{\hat{z}}),\cdots,k(\boldsymbol{z}_{N_{\rm tr}},\boldsymbol{\hat{z}})]^\top\in\mathbb{R}^{N_{\rm tr}}\) as a vector of kernel values between new data \(\boldsymbol{\hat{z}}\) and \(\{\boldsymbol{z}_i\}_{i=1}^{N_{\rm tr}}\), we have Proposition 2 below for exact KPCA reconstruction errors \(e^k(\boldsymbol{\hat{z}})\) based on \(\boldsymbol{K}\) (no approximation) with proofs in Appendix 8.

Proposition 2. The exact KPCA reconstruction error \(e^k(\boldsymbol{\hat{z}})\) can be calculated as: \[\label{eq:reconstruction-error-kpca} e^k(\boldsymbol{\hat{z}})= \Vert {\boldsymbol{U}}^{k\top}_p\boldsymbol{k}_{\boldsymbol{\hat{z}}} \Vert_2,\qquad{(1)}\] where \({\boldsymbol{U}}^{k}_p\in\mathbb{R}^{N_{\rm tr}\times p}\) includes the last \(p\) columns of \({\boldsymbol{U}}^{k}\), i.e., those \(p\) eigenvectors w.r.t. the smallest-\(p\) eigenvalues in \({\boldsymbol{\Lambda}}^{k}\).

Figure 7: Approximation performance of the reconstruction errors on InD (left) and OoD (right) data between exact KPCA and approximated KPCA based on RFFs (random) and Nyström approximation (Nyström).

Given Proposition 2, numerical comparisons can be executed between exact KPCA and our approximated KPCA, with results of reconstruction errors on InD and OoD data shown in Figure 7. As illustrated, for this practical OoD detection scenario within the deep learning scheme, the RFFs and energy-based Nyström lead to a marginal gap in the reconstruction errors when compared to exact KPCA, empirically verifying their approximation performance. In addition, the data-dependent Nyström achieves better convergence than the data-independent RFFs.

Again, we highlight that the approximation performance is not the focus of this study, and we recommend related literature [43][48] for more in-depth and insightful theoretical explorations. Instead, the essence of our work lies in (i) exploring the non-linearity in InD and OoD data via task-specific kernels, and (ii) facilitating OoD detection in practical large-scale scenarios via task-specific kernel approximations.

4.5 Explorations on Learnable Kernels↩︎

While the non-parametric Cosine-Gaussian kernel is validated with effective feature subspaces promoting InD-OoD separability, it would be interesting to further investigate learnable representations for optimizing non-linear feature subspaces. Motivated by this, we consider data-driven kernel hyper-parameter optimization and parametric neural network formulations, each tailored to varied learning objectives. These studies underscore an important insight: for learnable kernels, the objective function should be carefully designed to effectively capture distribution shifts, so that the representations are learned to be discriminative for InD and OoD in the non-linear low-dimensional feature subspace. Such primary investigations further demonstrate the potential of our framework for scenarios demanding higher flexibility and these tentative explorations may help inspire more comprehensive future work in this direction. Details on implementation and numerical results are provided in Appendix 10.

5 Experiments↩︎

5.1 Setups↩︎

In line with [23], [50], the proposed KPCA detection framework is mainly evaluated on the large-scale ImageNet-1K benchmark [30] to illustrate its advantages in effectiveness and efficiency. The ImageNet-1K dataset is more challenging and diverse than the classic CIFAR benchmark [37]: There are 1,281,167 training images of \(224\times224\times3\) within 1,000 categories in ImageNet-1K, while CIFAR10 only contains 50,000 \(32\times32\times3\) training images from 10 classes. Besides, multiple different network structures are involved in experiments, including ResNet [51], MobileNet [52] and ViT (Vision Transformer) [53] models.

For ImageNet-1K as InD, OoD datasets include iNaturalist [54], SUN [55], Places [39] and Textures [56], which have been widely adopted in existing methods. We select ResNet50 models [51] of standard training and supervised contrastive learning [57], MobileNetV2 [52] of standard training, and the prevalent ViT-B/16 [53] for a fair comparison. Images from ImageNet-1K and the paired OoD datasets are resized and cropped to \(224\times224\times3\) as inputs for ResNet50 and MobileNetV2, and to \(384\times384\times3\) for ViT-B/16, respectively.

\(\rm KPCA_{rff}\) and \(\rm KPCA_{nys}\) are compared with a wide variety of prevailing OoD detection methods, covering logits-based, gradients-based, and features-based ones. A thorough review of these approaches can be found in Section 6.

For the evaluation metrics, we employ the widely-used (i) False Positive Rate (FPR) of OoD samples with 95% true positive rate on the InD test dataset, and (ii) Area Under the Receiver Operating Characteristic curve (AUROC). The average FPR and AUROC values over the selected multiple OoD datasets are viewed as the final comparison metrics.

The source code of this work has been publicly released1. All experiments are executed on 1 NVIDIA GeForce RTX 3090 GPU.

Section 5.2 presents comparisons with these strong baselines on the ImageNet-1K benchmark with ResNet50 and ViT-B/16. Additional experiments, including those on MobileNetV2 and CIFAR, are provided in Appendix 9 for reference, along with complete results w.r.t. the following Tables 3, 5, ¿tbl:tab:exp-ablation-sampling?.

Table 1: Comparisons among a wide variety of OoD detection methods with ResNet50 models trained on ImageNet-1K.
method OoD data sets AVERAGE
iNaturalist SUN Places Textures
FPR\(\downarrow\) AUROC\(\uparrow\) FPR\(\downarrow\) AUROC\(\uparrow\) FPR\(\downarrow\) AUROC\(\uparrow\) FPR\(\downarrow\) AUROC\(\uparrow\) FPR\(\downarrow\) AUROC\(\uparrow\)
Standard Training
MSP [58] 54.99 87.74 70.83 80.86 73.99 79.76 68.00 79.61 66.95 81.99
MaxLogit [59] 54.49 91.05 65.45 84.96 67.60 83.69 57.09 86.71 61.16 86.60
ODIN [60] 47.66 89.66 60.15 84.59 67.89 81.78 50.23 85.62 56.48 85.41
Energy [7] 55.72 89.95 59.26 85.89 64.92 82.86 53.72 85.99 58.41 86.17
Mahala [21] 97.00 52.65 98.50 42.41 98.40 41.79 55.80 85.01 87.43 55.47
ViM [61] 68.86 87.13 79.62 81.67 83.81 77.80 14.95 96.74 61.81 85.83
DML [62] 47.32 91.61 57.40 86.14 61.43 84.68 52.80 86.72 54.74 87.28
KNN [12] 59.00 86.47 68.82 80.72 76.28 75.76 11.77 97.07 53.97 85.01
SHE [24] 34.22 90.18 54.19 84.69 45.35 90.15 45.09 87.93 44.71 88.24
GradNorm [9] 31.24 91.79 38.53 88.87 46.29 86.28 46.76 83.66 40.70 87.63
\(_{\rm rff}\) 50.07 89.32 62.56 83.74 72.76 78.91 9.02 98.14 48.60 87.53
\(_{\rm nys}\) 38.61 91.18 43.47 90.20 57.35 85.30 13.16 97.41 38.15 91.02
Regularized Training
SSD+ [63] 57.16 87.77 78.23 73.10 81.19 70.97 36.37 88.52 63.24 80.09
KNN+ [12] 30.18 94.89 48.99 88.63 59.15 84.71 15.55 95.40 38.47 90.91
TNN [25] 28.73 95.08 45.38 88.82 56.54 85.05 18.72 94.25 37.34 90.80
Mahala++ [22] 32.47 95.16 63.65 86.57 69.19 84.01 14.77 95.61 45.02 90.34
MCF [62] 36.29 93.77 51.18 89.50 57.38 86.78 28.46 94.35 43.33 91.10
MNC [62] 10.94 97.88 25.34 94.49 34.99 91.82 50.57 85.21 30.46 92.35
DML+ [62] 13.57 97.50 30.21 94.01 39.06 91.42 36.31 89.70 29.79 93.16
NNGuide [23] 12.02 97.47 31.62 91.66 38.88 90.12 24.93 91.52 26.86 92.69
\(_{\rm rff}\) 23.61 95.86 41.07 91.25 53.52 87.27 10.23 97.04 32.11 92.86
\(_{\rm nys}\) 6.88 98.43 19.22 94.84 29.79 92.31 24.49 94.19 20.09 94.94

5.2 Comparisons on Detection Performance↩︎

5.2.1 Results on ResNet↩︎

Table 1 shows the detection performance of various detection methods on ResNet50 models of standard training and regularized training. For the former, our KPCA employs the PyTorch-released ResNet50 checkpoint [64] that is trained by a standard cross-entropy loss. For the latter, our KPCA adopts the ResNet50 checkpoint released with KNN [12], which is trained with supervised contrastive learning [57]. In both cases, the detection score of KPCA is the negative of the reconstruction error \(S(\boldsymbol{\hat{x}})=-e^\Phi(\boldsymbol{\hat{x}})\).

In Table 1, our KPCA detection method achieves the lowest FPR and highest AUROC values, and indicates that exploring non-linear patterns is more advantageous than other compared methods in distinguishing OoD data from InD data. Besides, the substantial improvements of \(\rm KPCA_{nys}\) over \(\rm KPCA_{rff}\) indicate that the data-dependent Nyström approximation is more beneficial in learning a discriminative InD subspace than the data-independent RFFs.

5.2.2 Results on ViT↩︎

Table 2 shows comparisons on the ViT-B/16 model. We follow the settings in [23] and adopt a ViT-B/16 checkpoint that is sequentially pre-trained on ImageNet-21K and fine-tuned on ImageNet-1K. This transfer learning is a common and practical learning scheme. As shown in Table 2, \(\rm KPCA_{rff}\) and \(\rm KPCA_{nys}\) are suitable for features learned by the visual attention mechanism of ViT and achieve superior performance than KNN [12], indicating the effectiveness of appropriate non-linear kernels. Besides, the ensemble of \(\rm KPCA_{rff}\) and \(\rm KPCA_{nys}\) achieves new SOTA detection performance over other compared baselines.

Table 2: Comparisons among a wide variety of OoD detection methods with ViT-B/16 trained on ImageNet-1K.
method OoD data sets AVERAGE
iNaturalist SUN Places Textures
FPR\(\downarrow\) AUROC\(\uparrow\) FPR\(\downarrow\) AUROC\(\uparrow\) FPR\(\downarrow\) AUROC\(\uparrow\) FPR\(\downarrow\) AUROC\(\uparrow\) FPR\(\downarrow\) AUROC\(\uparrow\)
MSP [58] 27.58 93.96 57.43 85.18 61.13 84.34 53.21 84.96 49.84 87.11
Energy [7] 12.64 97.34 48.05 86.47 56.41 82.22 46.79 85.72 40.97 87.94
Mahala [21] 4.94 98.85 58.77 88.15 65.62 85.46 43.49 90.30 43.21 90.69
ViM [61] 3.42 99.20 49.66 88.05 59.69 83.68 42.55 88.46 38.83 89.85
GradNorm [9] 14.06 96.62 46.68 86.60 56.70 82.84 43.37 87.73 40.20 88.45
ReAct [11] 15.70 96.81 53.23 84.87 61.68 80.08 52.73 83.90 45.84 86.41
KNN [12] 29.46 94.07 72.15 83.88 74.17 81.47 51.21 87.18 56.75 86.65
MaxLogit [59] 13.19 97.19 47.45 86.80 54.36 83.13 44.70 86.11 39.93 88.31
NNGuide [23] 9.17 97.96 45.64 90.03 53.82 87.25 39.26 90.01 36.97 91.31
Mahala++ [22] 4.69 98.92 54.68 88.86 64.09 85.69 37.64 91.63 40.28 91.28
RankFeat+Weight [20], [50] 12.14 97.48 48.69 86.14 55.80 82.22 45.94 85.82 40.64 87.91
\(_{\rm rff}\) 10.30 97.88 68.73 83.47 76.59 78.86 49.40 89.15 51.25 87.34
\(_{\rm nys}\) 4.20 99.00 49.30 87.15 61.00 82.47 53.17 87.04 41.92 88.92
\(_{\rm rff}\)+KPCA\(_{\rm nys}\) 1.06 99.65 30.07 90.52 41.30 86.11 56.38 78.08 32.20 88.59

5.2.3 Comparisons with feature rectification↩︎

A series of methods propose that the abnormal responses in features lead to over-confidence on OoD data and should be removed. Generally, those extremely large or small feature values are viewed as anomalous information causing OoD predictions. Therefore, a range of techniques have been developed to clip extreme feature values [11], [14][20], termed feature rectification. Feature rectification still relies on an additional detector applied to the refined features or logits to produce detection scores, yet it has demonstrated outstanding detection performance and also a highly-flexible compatibility with other features-based detection methods [12], [13].

Our KPCA detection method can be equipped with feature rectification for even improved detection results. To be specific, a fusion strategy is proposed in [13] to combine regularized PCA reconstruction errors with feature rectification. Thereby, for a fair comparison, we follow this fusion strategy in [13] and boost KPCA reconstruction errors via a pioneering, simple, and effective feature rectification method ReAct [11]. Table 3 presents comparisons with more feature rectification methods on ImageNet-1K with ResNet50.

Table 3: Detection performance of KPCA enhanced by feature rectification. Results are of ResNet50 on ImageNet-1K.
method AVERAGE
FPR\(\downarrow\) AUROC\(\uparrow\)
DICE [65] 35.71 90.92
ReAct [11] 31.43 92.95
BATS [14] 27.11 94.28
VRA [15] 25.49 94.57
ASH-B [16] 22.73 95.06
SCALE [17] 20.05 95.71
LINe [19] 20.69 95.03
DDCS [18] 19.36 95.55
GradOrth[66] 18.57 96.31
DICE [65]+ReAct 28.57 93.30
GradNorm[9]+ReAct 25.13 94.22
LowDim[10]+ReAct 23.03 95.45
NNGuide [23]+ReAct 19.72 95.45
RegPCA[13]+ReAct 18.66 95.76
\(_{\rm rff}\)+ReAct 17.68 95.98
\(_{\rm nys}\)+ReAct 15.26 96.79
Table 4: Comparisons on the computation complexity among KNN, \(\rm KPCA_{rff}\) and \(\rm KPCA_{nys}\) (ResNet50 on ImageNet-1K). Experiments are executed on the same machine for a fair comparison. KNN is implemented via Faiss [67].
method
complexity time cost (ms) storage cost
KNN
\(N_{\rm tr}=1,281,167\) \(\approx\) 15.59 \(\approx\) 20 GiB
\(_{\rm rff}\) \(\mathcal{O}(M_r)\), \(M_r=4,096\) \(\approx\) 0.464 \(\approx\) 93 MiB
\(_{\rm nys}\) \(\mathcal{O}(M_n)\), \(M_n=2,048\) \(\approx\) 0.212 \(\approx\) 83 MiB

Table 3 indicates the superior detection performance of KPCA boosted by feature rectification. Our KPCA equipped with ReAct outperforms other powerful feature rectification methods [14][19]. Besides, under the same feature-clipping technique, KPCA also achieves the lowest FPR and highest AUROC values among other strong baselines [9], [10], [13], [23], [65]. The potential of KPCA can be further enhanced by incorporating more advanced feature rectification techniques.

All these experiments support the critical role of the non-linearity for separating InD and OoD data. The derived Cosine-Gaussian kernel and the corresponding non-linear feature subspace well seize the InD-OoD disparities and produce reconstruction errors that are sensitive to distribution shifts with new SOTA detection results.

5.3 Comparisons on Computation Complexity↩︎

We provide numerical results on the time and memory complexity of our KPCA detection method in the inference phase, i.e., the computation cost for calculating the KPCA reconstruction error \(e^\Phi\) given features \(\boldsymbol{\hat{z}}\) of a new sample \(\boldsymbol{\hat{x}}\). Particularly, the KNN method [12] is incorporated as a reference baseline, since both the Cosine-Gaussian kernel and the nearest neighbor searching explore the imbalanced feature norms and the \(\ell_2\) distance relation for OoD detection but in rather different perspectives and implementations.

Table 4 shows detailed numerical comparisons on ImageNet-1K with ResNet50. In the inference phase for new features \(\boldsymbol{\hat{z}}\), it is essential for KNN [12] to store and iterate all the 1,281,167 training features for the nearest neighbor searching, costing nearly 20 GiB memory and 16 ms to calculate the nearest distance for \(\boldsymbol{\hat{z}}\). In contrast, as outlined in Algorithm 5, our KPCA constructs explicit mappings without keeping and visiting the whole training set in inference, and achieves a significantly cheap computation complexity:

  • \(\rm KPCA_{rff}\) directly samples \(M_r=4,096\) weights of \(\boldsymbol{\omega}_i\) and \(u_i\) to obtain mapped features \(\Phi(\boldsymbol{\hat{z}})\), and adopts pre-calculated projection matrix \({\boldsymbol{U}}^\Phi_q\) and mean training vector \(\boldsymbol{\mu}^\Phi\) to determine the reconstruction error \(e^\Phi\).

  • \(\rm KPCA_{nys}\) only needs to keep and iterate \(M_n=2,048\) support landmarks \(\{\boldsymbol{\tilde{z}}\}_{i=1}^{M_{n}}\) and pre-calculated matrices \(\boldsymbol{\tilde{\Lambda}}\), \(\boldsymbol{\tilde{U}}\), \({\boldsymbol{U}}^\Phi_q\) and \(\boldsymbol{\mu}^\Phi\) to acquire \(\Phi(\boldsymbol{\hat{z}})\) and \(e^\Phi\).

As shown in Table 4, both \(\rm KPCA_{rff}\) and \(\rm KPCA_{nys}\) are highly resource-efficient compared with KNN, and complete inference in less than 1 ms and fewer than 100 MiB of memory occupancy. Besides, \(\rm KPCA_{nys}\) even outperforms \(\rm KPCA_{rff}\), indicating the superiority of the data-dependent approximation over data-independent random features.

Table 5: Detection results among a variety of kernels approximated via random features and the Nyström approximation.
kernel approx. AVERAGE
FPR\(\downarrow\) AUROC\(\uparrow\)
PCA - 84.41 57.46
Cosine - 58.40 83.86
Polynomial random 96.91 47.22
Nyström 86.33 64.65
Laplacian random 94.82 50.17
Nyström 81.41 38.72
Gaussian random 94.88 50.60
Nyström 38.15 35.44
Cosine-Poly. random 68.01 77.95
Nyström 58.42 83.79
Cosine-Lap. random 62.54 80.60
Nyström 54.05 86.71
Cosine-Gau. random 48.60 87.53
Nyström 38.15 91.02

5.4 Alternative Kernels↩︎

We explore alternative kernels for OoD detection, extending beyond the proposed Cosine-Gaussian kernel.

  • A Laplacian kernel \(k_{\rm lap}\) characterizes the \(\ell_1\) distance between two samples \(\boldsymbol{z}_1\) and \(\boldsymbol{z}_2\): \[\label{eq:kernel-lap} k_{\rm lap}(\boldsymbol{z}_1,\boldsymbol{z}_2)=e^{-\gamma\|\boldsymbol{z}_1-\boldsymbol{z}_2\|_1}.\tag{10}\] The Laplacian kernel \(k_{\rm lap}\) is shift-invariant, thus RFFs of Eqn.@eq:eq:rff can be applied to approximate \(k_{\rm lap}\). Note that the Fourier transform of \(k_{\rm lap}\) to sample \(\boldsymbol{\omega}_i\) is a Cauchy distribution \(\boldsymbol{\omega}\sim\frac{\gamma^2}{\pi\gamma(\boldsymbol{\omega}^2+\gamma^2)}\).

  • A Polynomial kernel \(k_{\rm poly}\) does not hold the \(\ell_1\) nor \(\ell_2\) distance-preserving property and is defined based on the inner product as follows: \[\label{eq:kernel-poly} k_{\rm poly}(\boldsymbol{z}_1,\boldsymbol{z}_2)=(\boldsymbol{z}_1^\top\boldsymbol{z}_2+c)^d.\tag{11}\] \(k_{\rm poly}\) is not shift-invariant and thereby the RFFs cannot be employed to approximate \(k_{\rm poly}\). We adopt another technique to obtain an explicit random mapping for \(k_{\rm poly}\), namely the Tensor Sketch approximation [68].

We evaluate these individual kernels and their concatenations with the Cosine kernel for OoD detection; results are compared comprehensively in Table 5. All experiments are executed on the ImageNet-1K benchmark and the PyTorch-released ResNet50 model. We can have the following observations.

5.4.0.1 The Cosine kernel is indispensable for OoD detection

Without the Cosine kernel, these individual Polynomial, Laplacian, and Gaussian kernels all show a substantial detection performance drop. Besides, KPCA w.r.t. a single Cosine kernel significantly enhances PCA. It implies that feature normalization via the Cosine kernel is essential for learning a discriminative subspace to separate InD and OoD data.

5.4.0.2 The Gaussian kernel shows the best detection results on top of the Cosine kernel

In Table 5, both Cosine-Laplacian kernel and Cosine-Polynomial kernel cannot bring detection performance gains compared to the single Cosine kernel, which illustrates that the \(\ell_1\)-distance relation from \(k_{\rm lap}\) and the inner-product information from \(k_{\rm poly}\) in the \(\phi_{\rm cos}(\boldsymbol{z})\)-space actually hurt the separability between InD and OoD features.

5.4.0.3 The data-dependent Nyström approximation outperforms the data-independent random features in OoD detection

For the three kernels of Cosine-Polynomial, Cosine-Laplacian, and Cosine-Gaussian, KPCA via Nyström achieves lower FPR and higher AUROC values than that via random features, which implies the superiority of data-dependent approximation over the data-independent way.

Ablation studies on the sampling scheme for Nyström in \(\rm KPCA_{nys}\). Average detection results are reported.

Detection performance of KPCA applied to intermediate-layer features. Average detection results are reported.

5.5 Ablation Studies↩︎

In our \(\rm KPCA_{nys}\), an energy-based sampling scheme is specifically devised for Nyström, which selects those low-energy InD training samples \(\{\boldsymbol{\tilde{x}}_i\}_{i=1}^{M_n}\) with the smallest-\(M_n\) energy values as support landmarks. This sampling scheme shows to produce discriminative non-linear principal components capturing InD-OoD disparities and improved detection results. Ablations are studied in this section to verify its effectiveness. Particularly, we consider another two sampling configurations as follows:

  • A uniform sampling is widely adopted in Nyström [34], [41] and is investigated in OoD detection for ablations.

  • Regarding the proposed energy-based sampling scheme, we consider those high-energy InD training samples with the largest-\(M_n\) energy values as landmarks for ablations.

Comparisons among different sampling schemes are presented in Table ¿tbl:tab:exp-ablation-sampling?, with full results in Table ¿tbl:response:tab:full-auroc? of Appendix 9.

As illustrated, our low-energy support landmarks outperform the uniformly-sampled landmarks for \(\rm KPCA_{nys}\), which indicates that appropriate landmarks in relation to this specific OoD detection task can bring detection performance gains. Besides, low-energy landmarks show to be superior over high-energy ones in separating InD and OoD with \(\rm KPCA_{nys}\). The reason can be that the approximation based on those boundary InD samples leads to reconstruction errors that are more sensitive to distribution shifts, as discussed in Section 4.2.

While our subspace learning is typically applied to penultimate-layer features, this study extends the analysis by evaluating KPCA on intermediate-layer features of a ResNet50 model to assess their discriminative capability. A typical ResNet50 architecture2 consists of one beginning convolution block, followed by four sequential layer blocks (layer1, layer2, layer3 and layer4), and a final linear classification layer. Beyond its default use at layer4, we apply KPCA to features from earlier blocks (layer1, layer2 and layer3) to investigate performance in their respective non-linear subspaces with results shown in Table ¿tbl:tab:exp-interlayer-feat?.

As demonstrated, shallow-layer features (layer1 and layer2) does not yield effective non-linear subspaces to well separate InD and OoD data, while deeper features (layer3 and layer4) exhibit enhanced detection results. This observation aligns with the hierarchical learning of DNNs: shallow layers capture fine-grained image patterns and higher-level semantic discrimination emerges in deeper layers. Consequently, the penultimate-layer features (layer4), adopted in our work by default, demonstrate the strongest discriminative ability and achieve the best detection performance for both \(\rm KPCA_{rff}\) and \(\rm KPCA_{nys}\). This choice is also supported by prior findings that penultimate-layer feature collapse can aid OoD detection [69].

5.6 Sensitivity Analysis↩︎

A comprehensive sensitivity analysis is executed to illustrate the effects of hyper-parameters in our KPCA detection framework. \(\rm KPCA_{rff}\) and \(\rm KPCA_{nys}\) share two common hyper-parameters: the bandwidth \(\gamma\) of the Gaussian kernel and the number of columns \(q\) of the projection matrix \({\boldsymbol{U}}^\Phi_q\). Besides, their distinctive hyper-parameters are the number of RFFs \(M_r\) in \(\rm KPCA_{rff}\) and the number of support landmarks \(M_n\) in \(\rm KPCA_{nys}\). In the following, we discuss the influence of each hyper-parameter, and report detection results by varying one hyper-parameter with the others fixed. All experiments are conducted on the ImageNet-1K benchmark and the PyTorch-released ResNet50 checkpoint.

The column size \(q\) in the projection matrix \({\boldsymbol{U}}^\Phi_q\) indicates the number of preserved principal components and determines the amount of information captured by the projected subspace for InD and OoD data. Generally, \(q\) is selected as the minimum number of principal components required to capture a cumulative explained variance ratio that exceeds a given threshold. Thereby, we investigate the effect of \(q\) by exploring the detection performance under varied values of the explained variance ratio, which is calculated as the ratio of the sum of eigenvalues w.r.t. the preserved principal components over the sum of all eigenvalues.

Figure 8 illustrates the detection performance of \(\rm KPCA_{rff}\) and \(\rm KPCA_{nys}\) under varied explained variance ratios. For \(\rm KPCA_{nys}\), a sufficiently large value of the explained variance ratio, i.e., over 99%, is essential to keep most principal components for superior detection results. Regarding \(\rm KPCA_{rff}\), a modest value of the explained variance ratio is instead suggested, which might be due to the fact that the random features make the useful information for distinguishing OoD samples concentrated in fewer principal components.

Figure 8: A sensitivity analysis on the explained variance ratio of \rm KPCA_{rff} (left) and \rm KPCA_{nys} (right). The average FPR and AUROC values of OoD datasets in the ImageNet-1K benchmark are reported. The Gaussian kernel width \gamma and the numbers of RFFs and landmarks M_r,M_n are fixed.
Figure 9: A sensitivity analysis on the Gaussian kernel width \gamma of \rm KPCA_{rff} (left) and \rm KPCA_{nys} (right). The average FPR and AUROC values of OoD datasets in the ImageNet-1K benchmark are reported. The explained variance ratio and the numbers of RFFs and landmarks M_r,M_n are fixed.
Figure 10: A sensitivity analysis on the number of RFFs M_r of \rm KPCA_{rff} (left) and the number of landmarks M_n of \rm KPCA_{nys} (right). The average results of OoD datasets in the ImageNet-1K benchmark are reported. The explained variance ratio and the Gaussian kernel width \gamma are fixed.

The Gaussian kernel width \(\gamma\) directly affects the mapped data distribution. For a large \(\gamma\), \(k_{\rm gau}(\boldsymbol{z}_1,\boldsymbol{z}_2)=e^{-\gamma\left\Vert\boldsymbol{z}_1-\boldsymbol{z}_2\right\Vert_2^2}\approx0\) for \(\boldsymbol{z}_1\neq\boldsymbol{z}_2\), which indicates that the mappings of \(\boldsymbol{z}_1\) and \(\boldsymbol{z}_2\) are (nearly) mutually-orthogonal. In this case, a PCA in the mapped space would become meaningless. For a small \(\gamma\), KPCA reconstruction errors will approach standard PCA ones, as discussed in [70].

Figure 9 demonstrates the FPR and AUROC values of varied Gaussian kernel widths \(\gamma\) in \(\rm KPCA_{rff}\) and \(\rm KPCA_{nys}\). Clearly, neither a too large nor a too small kernel width can well characterize the patterns of InD-OoD disparities, and a mild value of \(\gamma\) should be carefully tuned in both \(\rm KPCA_{rff}\) and \(\rm KPCA_{nys}\) for outstanding detection performance. Moreover, it can be observed that \(\rm KPCA_{nys}\) is significantly more sensitive to variations in \(\gamma\) compared to \(\rm KPCA_{rff}\). This difference may stem from the fact that the low-rank approximation based on selected InD landmark InD requires more precise kernel configurations to effectively learn discriminative subspaces than the data-independent random features approximation.

In \(\rm KPCA_{rff}\) and \(\rm KPCA_{nys}\), the number of RFFs \(M_r\) and the number of support landmarks \(M_n\) respectively determine the approximation ability of random features to the kernel function and of the low-rank matrix to the full kernel matrix. Thereby, the larger the \(M_r\) and \(M_n\), the better random features and low-rank features approximate the Gaussian kernel \(k_{\rm gau}\) [33], [34].

Figure 10 indicates the FPR and AUROC values of \(\rm KPCA_{rff}\) and \(\rm KPCA_{nys}\) under varied numbers of RFFs and support landmarks \(M_r,M_n\). As \(M_r\) and \(M_n\) increase, the detection performance of both \(\rm KPCA_{rff}\) and \(\rm KPCA_{nys}\) improves, since the random features and low-rank features gradually converge to the Gaussian kernel. Besides, Figure 10 also provides supplementary results to discussions on the computational complexity in Section 5.3. Under the same value of \(M_r\) and \(M_n\), the low-rank mapping of \(\rm KPCA_{nys}\) significantly outperforms the random mapping of \(\rm KPCA_{rff}\) in detection performance. We can further find in Figure 10 that the low-rank mapping built by sampling 1,024 support landmarks even exceeds the detection performance of the 4,096-dimensional random mapping, which strongly supports the superiority of the data-dependent low-rank approximation in OoD detection.

6 Related Work↩︎

Prevalent OoD detection methods devise the scoring function \(S(\cdot)\) of Eqn.@eq:eq:ood-scoring based on different outputs from DNNs to characterize the InD-OoD disparities, categorized as follows.

Logits-based approaches exploit the abnormal responses reflected in the predictive logits or probabilities from DNNs to detect OoD data. Early methods adopt either the maximum logits [59] or probability [58], [60] or the energy function on logits [7] as the detection score. Besides, decoupling on logits is explored to regularize the model training from the perspectives of InD confidence [71] and similarity [62], respectively. The Wasserstein distance [8] is applied to the predictive probabilities and leads to a novel loss function to optimize the network for enhanced detection performance.

Gradients-based methods investigate gradient disparities between InD and OoD data for OoD detection. Norms of full gradients are firstly studied in [9]. The projections of gradients onto a low-rank feature space are investigated in [66] for more discriminative gradient norms. Gradient information is also utilized to regularize the model training together with auxiliary sampled OoD data [72]. A recent work [10] leverages low-dimensional gradient representations to train an auxiliary linear network for effective detection.

Features-based approaches capture the feature information causing over-confident OoD predictions in different ways. A series of feature rectification methods clip the abnormal feature values [11], [14][20] to remove anomalous information in distinct ways. Besides, feature distances in different forms, such as the Mahalanobis distance [21], [22], the \(\ell_2\) distance [12], [23], the tangent distance [25], and the Hopfield energy [24], can be efficacious detection metrics. More explorations on feature norms [61], [73] and feature subspaces [13], [35], [74], [75], have exhibited excellent detection performance.

Aside from the aforementioned 3 types of detection methods, a broad variety of researches have been developed for investigating InD-OoD disparities from distinctive perspectives with novel insights, including but not limited to utilizing the parameters [50], [65], the ensemble technique [76], the diffusion models [77], and the contrastive learning [78]. In addition to those practical approaches, theoretical understandings for OoD detection are discussed in [79], [80]. OoD detection is also extended in more scenarios, such as applications on the tabular data [81] and balancing the InD generalization [82].

Random Fourier Features (RFFs) [33], as explicit mappings to approximate kernels, are initially developed to accelerate large-scale kernel machines, and have been extensively studied and applied across a broad range of fields, such as kernel approximation [83], kernel learning [84], generalization [85], optimization [86] and deep network architectures [87].

Nyström method [34] with various sampling schemes [42] has been applied not only to kernels, such as kernel learning [41], [88] and approximated KPCA [43][48], but also to diverse domains beyond kernels, e.g., interpreting the self-attention in transformers [89] and differentiation optimization [90]. There is significant potential in bridging these classic machine learning methods with modern deep learning frameworks, leveraging their theoretical foundations and computational efficiency to address contemporary challenges.

7 Conclusion and Discussion↩︎

Exploring the non-linear feature subspace remains a fresh, significant, and open perspective for OoD detection. In this work, we leverage the framework of KPCA to identify discriminative non-linear feature subspaces to detect OoD data in an efficacious and efficient way. Firstly, we analyze InD and OoD features, observe the non-linear patterns related to InD-OoD disparities, and derive an effective Cosine-Gaussian kernel that models such patterns. Then, two distinct explicit mappings \(\Phi(\cdot)\) to approximate the Cosine-Gaussian kernel are leveraged: data-independent random features and data-driven Nyström approximation, so as to achieve efficient computations of the KPCA reconstruction errors in the \(\Phi(\boldsymbol{z})\)-space. Particularly, an energy-based sampling scheme is proposed to refine the Nyström method for more discriminative subspaces between InD and OoD. Extensive experiments verify that our KPCA method achieves new SOTA OoD detection performance with high efficiency, and provide in-depth analyses on kernel choices, sampling schemes, intermediate-layer features, and hyper-parameters.

One limitation of KPCA detection is the hyper-parameter tuning of its task-specific Cosine-Gaussian kernel for non-linear mappings. Our preliminary explorations on learnable kernel representations in Section 4.5 and Appendix 10 mitigate this issue through different learning techniques, while also suggesting further improvements, such as incorporating outliers and tailored optimization objectives for distribution shifts. We hope that the proposed effective kernel verified empirically in our work could benefit the research community from the perspective of exploring the non-linearity in feature space in future studies on OoD detection.

8 Proof↩︎

The proof of Proposition 2 is presented.

Proof. Recall \(\boldsymbol{z}\in\mathbb{R}^m\) and suppose the feature mapping w.r.t the kernel \(k\) is denoted as \(\Phi:\mathbb{R}^m\rightarrow\mathbb{R}^M\) with \(k(\boldsymbol{z}_1,\boldsymbol{z}_2)=\Phi(\boldsymbol{z}_1)^\top\Phi(\boldsymbol{z}_2)\). Given a finite training set \(\{\boldsymbol{z}_i\}_{i=1}^{N_{\rm tr}}\), eigendecomposition is executed on the covariance matrix on the mapped training set \(\{\Phi(\boldsymbol{z}_i)\}_{i=1}^{N_{\rm tr}}\), leading to the left eigenmatrix \({\boldsymbol{U}}^\Phi\in\mathbb{R}^{M\times M}\). By taking the first \(q\) columns in \({\boldsymbol{U}}^\Phi\), we obtain the projection matrix \({\boldsymbol{U}}^\Phi_q\) with \({\boldsymbol{U}}^\Phi=\left[{\boldsymbol{U}}^\Phi_q,{\boldsymbol{U}}^\Phi_p\right]\).

For the reconstruction error \(e^\Phi(\boldsymbol{\hat{z}})\) of a new test sample \(\boldsymbol{\hat{z}}\in\mathbb{R}^m\) in the mapped \(\Phi(\boldsymbol{z})\)-space, we have:

\[\begin{align} \label{eq:reconstruction-error-residual-proof} \left(e^{\Phi}(\boldsymbol{\hat{z}})\right)^2 &=\left\Vert (\Phi(\boldsymbol{\hat{z}})-\boldsymbol{\mu}^\Phi)-{\boldsymbol{U}}^\Phi_q{\boldsymbol{U}}_q^{\Phi\top}(\Phi(\boldsymbol{\hat{z}})-\boldsymbol{\mu}^\Phi) \right\Vert_2^2\\ &=\left((\Phi(\boldsymbol{\hat{z}})-\boldsymbol{\mu}^\Phi)-{\boldsymbol{U}}^\Phi_q{\boldsymbol{U}}_q^{\Phi\top}(\Phi(\boldsymbol{\hat{z}})-\boldsymbol{\mu}^\Phi)\right)^\top \left((\Phi(\boldsymbol{\hat{z}})-\boldsymbol{\mu}^\Phi)-{\boldsymbol{U}}^\Phi_q{\boldsymbol{U}}_q^{\Phi\top}(\Phi(\boldsymbol{\hat{z}})-\boldsymbol{\mu}^\Phi)\right)\\ &=(\Phi(\boldsymbol{\hat{z}})-\boldsymbol{\mu}^\Phi)^\top(\Phi(\boldsymbol{\hat{z}})-\boldsymbol{\mu}^\Phi) -(\Phi(\boldsymbol{\hat{z}})-\boldsymbol{\mu}^\Phi)^\top{\boldsymbol{U}}^\Phi_q{\boldsymbol{U}}_q^{\Phi\top}(\Phi(\boldsymbol{\hat{z}})-\boldsymbol{\mu}^\Phi)\\ &=(\Phi(\boldsymbol{\hat{z}})-\boldsymbol{\mu}^\Phi)^\top \left({\boldsymbol{I}}-{\boldsymbol{U}}^\Phi_q{\boldsymbol{U}}_q^{\Phi\top}\right) (\Phi(\boldsymbol{\hat{z}})-\boldsymbol{\mu}^\Phi)\\ &=(\Phi(\boldsymbol{\hat{z}})-\boldsymbol{\mu}^\Phi)^\top {\boldsymbol{U}}^\Phi_p{\boldsymbol{U}}_p^{\Phi\top} (\Phi(\boldsymbol{\hat{z}})-\boldsymbol{\mu}^\Phi)\\ &=\left\Vert{\boldsymbol{U}}_p^{\Phi\top}(\Phi(\boldsymbol{\hat{z}})-\boldsymbol{\mu}^\Phi) \right\Vert_2^2. \end{align}\tag{12}\] We have \(e^\Phi(\boldsymbol{\hat{z}})=\left\Vert{\boldsymbol{U}}_p^{\Phi\top}(\Phi(\boldsymbol{\hat{z}})-\boldsymbol{\mu}^\Phi)\right\Vert_2\). In this sense, the reconstruction error is interpreted as the norm of features projected into the residual subspace, i.e., the \({\boldsymbol{U}}_p\)-space.

Therefore, to obtain KPCA reconstruction errors via the kernel matrix \(\boldsymbol{K}\), we can employ the kernel trick to project \(\boldsymbol{\hat{z}}\) into the residual space through \(\boldsymbol{K}\), as typically conducted for dimension reduction through KPCA [28], [29]. Accordingly, the KPCA reconstruction error \(e^k\) is calculated as the norm of features projected into the residual space w.r.t. the eigenvectors of small eigenvalues of the kernel matrix \(\boldsymbol{K}\), as stated in Proposition 2. The proof is completed. ◻

9 Additional Experiments on OoD detection↩︎

In this section, additional experiments on OoD detection are provided, including results of MobileNetV2 [52] on the ImageNet-1K [30] benchmark, results of ResNet18 [51] on the CIFAR10 [37] benchmark, and full results on each OoD dataset w.r.t. Tables 3, 5, ¿tbl:tab:exp-ablation-sampling? in the main text.

9.1 Experiments on MobileNet↩︎

Table 6 supplements detection results on the ImageNet-1K benchmark and MobileNetV2. We use the pre-trained checkpoint of MobileNetV2 released by PyTorch [64]. The KPCA detection method is enhanced via the pioneering feature rectification baseline ReAct [11].

Similar to the results of ResNet50 in Table 3 of the main text, \(\rm KPCA_{rff}\) and \(\rm KPC_{nys}\) equipped with ReAct outperform a wide variety of different detection methods that are either feature rectification or combined with feature rectification, indicating the superiority of exploring the non-linearity in the feature space via suitable kernels on different network structures.

Table 6: Comparisons for KPCA enhanced by feature pruning. Results are of MobileNetV2 on ImageNet-1K.
method OoD data sets AVERAGE
iNaturalist SUN Places Textures
FPR\(\downarrow\) AUROC\(\uparrow\) FPR\(\downarrow\) AUROC\(\uparrow\) FPR\(\downarrow\) AUROC\(\uparrow\) FPR\(\downarrow\) AUROC\(\uparrow\) FPR\(\downarrow\) AUROC\(\uparrow\)
MSP [58] 64.29 85.32 77.02 77.10 79.23 76.27 73.51 77.30 73.51 79.00
Energy [7] 59.50 88.91 62.65 84.50 69.37 81.19 58.05 85.03 62.39 84.91
ODIN [60] 58.54 87.51 57.00 85.83 59.87 84.77 52.07 85.04 56.87 85.79
Mahala [21] 62.11 81.00 47.82 83.66 52.09 83.63 92.38 33.06 63.60 71.01
ViM [61] 91.83 77.47 94.34 70.24 93.97 68.26 37.62 92.65 79.44 77.15
DICE [65] 43.28 90.79 38.86 90.41 53.48 85.67 33.14 91.26 42.19 89.53
ReAct [11] 43.07 92.72 52.47 87.26 59.91 84.07 40.20 90.96 48.91 88.75
BATS [14] 49.57 91.50 57.81 85.96 64.48 82.83 39.77 91.17 52.91 87.87
ASH-B [16] 31.46 94.28 38.45 91.61 51.80 87.56 20.92 95.07 35.66 92.13
NNGuide [23] 45.73 91.19 51.03 87.87 60.60 84.44 29.50 92.47 46.72 88.99
GradOrth [66] 26.81 93.17 30.82 93.18 40.27 89.12 12.69 97.52 27.65 93.25
DICE [65]+ReAct 41.75 89.84 39.07 90.39 54.41 84.03 19.98 95.86 38.80 90.03
LowDim [10]+ReAct 34.80 93.22 50.09 89.44 57.05 87.02 40.19 90.91 45.53 90.15
NNGuide [23]+ReAct 37.87 92.83 43.51 89.57 55.92 85.24 14.34 96.30 37.91 90.99
RegPCA [13]+ReAct 35.84 93.66 40.35 90.77 52.38 86.76 18.44 95.39 36.75 91.65
\(_{\rm rff}\)+ReAct 31.72 94.27 40.77 90.98 55.69 86.42 10.48 97.49 34.66 92.29
\(_{\rm nys}\)+ReAct 23.69 95.83 22.73 95.55 38.19 91.91 21.08 95.87 26.42 94.79

ll|cc training & method & FPR\(\downarrow\) & AUROC\(\uparrow\)
& MSP [58] & 57.67 & 90.86
&ODIN [60] & 36.16 & 92.30
&Energy [7] & 38.02 & 93.05
&GODIN [71] & 32.80 & 93.52
&Mahala [21] & 37.94 & 86.50
&KNN [12] & 30.77 & 94.15
&\(_{\rm rff}\) & &
&\(_{\rm nys}\) & &
& CSI [78] & 24.16 & 95.89
&SSD+ [63] & 16.52 & 97.15
&KNN+ [12] & 11.07 & 97.93
&TNN [25] & 9.87 & 98.05
&\(_{\rm rff}\) & &
&\(_{\rm nys}\) & 7.64 & 98.31

9.2 Experiments on CIFAR10↩︎

Extensive experiments on the more realistic and challenging large-scale ImageNet-1K benchmark have demonstrated the advantageous detection performance and computational efficiency of our KPCA method. In addition, we further supplement results on the small-scale CIFAR10 benchmark for reference. For CIFAR10 as InD, OoD datasets include SVHN [91], LSUN [38], iSUN [92], Textures [56] and Places365 [39]. Following the settings in KNN [12], we choose ResNet18 models [51] that are trained via standard cross-entropy loss and supervised contrastive learning [57], and adopt the checkpoints released by KNN for a fair comparison. Images from CIFAR10 and the paired OoD datasets are resized to \(32\times32\times3\) as inputs for ResNet18.

As the kernel is inspired by the KNN approach [12], our KPCA detection method is primarily compared with KNN, and the comparisons follow the reported configurations in KNN [12], shown in Table ¿tbl:tab:exp-c10-r18?. For both standard and regularized training, \(\rm KPCA_{rff}\) and \(\rm KPCA_{nys}\) outperform KNN, highlighting the advantage of non-linear kernels in capturing the disparities between InD and OoD data in the feature space. Particularly, under regularized training, \(\rm KPCA_{rff}\) and \(\rm KPCA_{nys}\) exhibit nearly the same detection performance, which demonstrates that the random features and low-rank features achieve comparable approximation quality given features derived from supervised contrastive learning on such small-scale data. The superiority of low-rank features is mainly pronounced on more complex datasets, as illustrated by results on the ImageNet-1K benchmark in the main text.

9.3 Full Results↩︎

We provide the full detection results for each OoD dataset, corresponding to the three main experiments in the main text.

  • Table 7 presents the extended results of KPCA with feature rectification, complementing Table 3.

  • Table 8 gives a complete ablation study on kernel designs, extending Table 5.

  • Table ¿tbl:response:tab:full-auroc? lists the detailed results for different Nyström sampling strategies, supplementing Table ¿tbl:tab:exp-ablation-sampling?.

Table 7: Detection performance of KPCA enhanced by feature rectification. Results are of ResNet50 on ImageNet-1K.
method OoD data sets AVERAGE
iNaturalist SUN Places Textures
FPR\(\downarrow\) AUROC\(\uparrow\) FPR\(\downarrow\) AUROC\(\uparrow\) FPR\(\downarrow\) AUROC\(\uparrow\) FPR\(\downarrow\) AUROC\(\uparrow\) FPR\(\downarrow\) AUROC\(\uparrow\)
DICE [65] 26.66 94.49 36.08 90.98 47.63 87.73 32.46 90.46 35.71 90.92
ReAct [11] 20.38 96.22 24.20 94.20 33.85 91.58 47.30 89.80 31.43 92.95
BATS [14] 12.57 97.67 22.62 95.33 34.34 91.83 38.90 92.27 27.11 94.28
VRA [15] 15.70 97.12 26.94 94.25 37.85 91.27 21.47 95.62 25.49 94.57
ASH-B [16] 14.21 97.32 22.08 95.10 33.45 92.31 21.17 95.50 22.73 95.06
SCALE [17] 9.50 98.17 23.27 95.02 34.51 92.26 12.93 97.37 20.05 95.71
LINe [19] 22.52 94.44 19.48 95.26 12.24 97.56 28.54 92.84 20.69 95.03
DDCS [18] 11.63 97.85 18.63 95.68 28.78 92.89 18.40 95.77 19.36 95.55
GradOrth[66] 11.04 98.00 19.61 95.76 33.67 91.78 11.19 98.06 18.57 96.31
DICE [65]+ReAct 20.08 96.11 26.50 93.83 38.34 90.61 29.36 92.65 28.57 93.30
GradNorm[9]+ReAct 14.88 97.01 25.54 94.19 36.49 91.12 23.60 94.57 25.13 94.22
LowDim[10]+ReAct 19.87 95.77 45.34 90.52 0.00 100.00 26.90 95.50 23.03 95.45
NNGuide [23]+ReAct 11.12 97.70 20.51 95.26 29.99 92.70 17.27 96.11 19.72 95.45
RegPCA[13]+ReAct 10.17 97.97 18.50 95.80 27.31 93.39 18.67 95.95 18.66 95.76
\(_{\rm rff}\)+ReAct 10.77 97.85 18.70 95.75 28.69 93.13 12.57 97.21 17.68 95.98
\(_{\rm nys}\)+ReAct 8.83 98.20 12.20 97.31 22.98 94.83 17.02 96.80 15.26 96.79
Table 8: Detection results among a variety of kernels approximated via random features and the Nyström approximation.
kernel approx. OoD datasets AVERAGE
iNaturalist SUN Places Textures
FPR\(\downarrow\) AUROC\(\uparrow\) FPR\(\downarrow\) AUROC\(\uparrow\) FPR\(\downarrow\) AUROC\(\uparrow\) FPR\(\downarrow\) AUROC\(\uparrow\) FPR\(\downarrow\) AUROC\(\uparrow\)
PCA - 95.46 52.01 97.98 44.86 97.99 45.19 46.22 87.77 84.41 57.46
Cosine - 67.25 83.41 75.53 79.93 82.48 73.83 8.33 98.29 58.40 83.86
Polynomial random 96.03 53.07 98.26 42.84 97.85 45.02 95.50 47.96 96.91 47.22
Nyström 92.89 62.45 96.21 56.71 97.47 52.75 58.74 86.68 86.33 64.65
Laplacian random 94.65 50.25 94.68 50.29 95.28 49.80 94.66 50.34 94.82 50.17
Nyström 83.57 34.05 75.93 39.76 84.57 35.02 81.58 46.07 81.41 38.72
Gaussian random 94.46 50.83 95.17 50.33 94.80 50.46 95.09 50.80 94.88 50.60
Nyström 34.11 37.71 51.44 28.39 42.22 33.33 24.82 42.32 38.15 35.44
Cosine-Poly. random 54.10 84.48 75.97 75.04 82.82 69.01 59.15 83.27 68.01 77.95
Nyström 67.28 83.41 75.78 79.79 82.51 73.65 8.10 98.30 58.42 83.79
Cosine-Lap. random 76.18 77.95 77.54 76.70 84.47 70.16 11.97 97.57 62.54 80.60
Nyström 58.49 84.10 55.72 87.61 65.61 82.57 36.37 92.58 54.05 86.71
Cosine-Gau. random 50.07 89.32 62.56 83.74 72.76 78.91 9.02 98.14 48.60 87.53
Nyström 38.61 91.18 43.47 90.20 57.35 85.30 13.16 97.41 38.15 91.02

Ablation studies on the sampling scheme for Nyström in \(\rm KPCA_{nys}\). Both FPR and AUROC results of each OoD dataset are listed under the 3 sampling scheme of ResNet50 trained on ImageNet-1K.

10 KPCA with Learnable Representations for OoD Detection↩︎

While the non-parametric Cosine-Gaussian kernel is validated with effective feature subspaces promoting InD and OoD separability, it would be interesting to further investigate the utility of learnable representations for optimizing non-linear feature subspaces. In this section, we explore the potentials of learnable kernel representations within our KPCA-based OoD detection framework.

We note that deploying learnable representations in kernel-based methods encompasses distinct and diverse directions, e.g., using learnable kernels or parametric neural networks. We implement (i) a Shallow AutoEncoder (Shallow-AE) network [93], and (ii) several representative kernel learning methods including RFF-based Optimization (RFF-OPT) [94] and Deep Kernel Gaussian Process (DKGP) [95] for evaluations. The explanations for each method and the results are outlined below, respectively.

[93]. We consider the shallow autoencoders as mappings [93]. Following approaches similar to [93] and for a fair comparison, an encoder \(\phi_{e}\) and a decoder \(\phi_{d}\) are applied to the penultimate-layer features from a ResNet50 model pretrained on ImageNet-1K. \(\phi_{e}\) maps the inputs \(\boldsymbol{z}\) into a latent space and \(\phi_d\) performs the reconstruction \(\phi_d(\phi_e(\boldsymbol{z}))\). \(\phi_{e}\) and \(\phi_d\) are optimized by minimizing the differences between inputs and the reconstructions. To be specific, we choose to minimize the Mean Squared Error (MSE) between \(\boldsymbol{z}\) and \(\phi_d(\phi_e(\boldsymbol{z}))\) as follows: \[\label{response:eq:mse} \min_{\theta_{\phi_e},\theta_{\phi_d}}\sum_{i}\|\boldsymbol{z}_i-\phi_d(\phi_e(\boldsymbol{z}_i))\|_2^2.\tag{13}\] Both the encoder \(\phi_e\) and the decoder \(\phi_d\) are two-layer multilayer perceptrons (MLPs). During inference, the negative MSE is adopted as the OoD detection score, as InD inputs are expected to be well reconstructed with low error (MSE) under this optimization setup. The results are presented in Table ¿tbl:response:tab:kernel-learning? (shallow AE).

[94]. RFF-OPT is specifically optimized based on Random Fourier Features (RFFs) [33]. In our KPCA-based framework, RFF is a data-independent kernel approximation technique that plays key roles especially with large-scale data. In RFF-OPT, an optimal subset of the naive RFFs is learned by solving the following kernel alignment problem given training data \((\boldsymbol{z}_i,y_i)\in\mathbb{R}^m\times\{-1,1\}\): \[\label{response:eq:alignment} \max_{\boldsymbol{q}\in{\cal P}_{M_r}}\sum_{i,j}y^iy^j\sum_{n=1}^{M_r}\boldsymbol{q}_n\Phi_{\rm RFF}^n(\boldsymbol{z}_i)\cdot\Phi_{\rm RFF}^n(\boldsymbol{z}_j),\tag{14}\] where \({\cal P}_{M_r}\) is defined as a set of discrete distributions approximating the uniform distribution under the \(f\)-Divergence (\(D_f\)) within a constant \(\rho\): \({\cal P}_{M_r}\mathrel{\vcenter{:}}=\{\boldsymbol{q}:D_f(\boldsymbol{q}||\frac{\boldsymbol{1}}{M_r}\leq\rho)\}\), and \(\Phi_{\rm RFF}^n(\boldsymbol{z}_i)\) denotes the \(n\)-th element in the mapped RFFs: \(\Phi_{\rm RFF}^n(\boldsymbol{z}_i)=\cos({\boldsymbol{\omega}}_n^\top\phi_{\rm cos}(\boldsymbol{z}_i)+u_n)\). Therefore, Problem (14 ) aims at finding weights \(\boldsymbol{q}\) so that the RFFs-approximated kernel matrix matches the correlation matrix \(\boldsymbol{y}\boldsymbol{y}^\top\). For more details, please kindly refer to [94].

For a fair comparison, experiments with RFF-OPT are implemented on the penultimate-layer features from a ResNet50 model pretrained on ImageNet-1K, as done in our main comparisons with RFF-based method in the manuscript. We follow the public code3 of RFF-OPT [94] and extends the bi-classification in Problem (14 ) to multi-classification for ImageNet-1K. Due to memory limitation, 10% of the ImageNet-1K training features are randomly selected and adopted to solve Problem (14 ). A sparse subset of the naive RFFs is obtained and applied to our KPCA framework for OoD detection, achieving superior detection performance with reduced computations (fewer mapped dimensions), as demonstrated in Table ¿tbl:response:tab:kernel-learning? (RFF-OPT).

[95]. Gaussian Processes (GPs) [96] have been widely utilized and is closely related to kernel methods. In [95], Deep Kernel GPs (DKGPs) are formulated by applying GPs on the deep-layer features from neural networks, thereby encapsulating the expressive power of deep networks within kernel-based architectures with learnable representations. Therefore, similarly in our KPCA framework for OoD detection, the parameters of the Gaussian kernel can be learned through DKGP by maximizing the following Evidence Lower BOund (ELBO) \({\cal L}_{\rm ELBO}\): \[\label{response:eq:dkgp} \max_{\gamma_k,\gamma_v}{\cal L}_{\rm ELBO}=\sum_i\mathbb{E}_q\left[\log p(y_i|{\boldsymbol{g}}_i)\right]-{\rm KL}\left[q\|p\right],\tag{15}\] where the optimization parameters include the kernel parameters \(\gamma_k\) and the variational parameters \(\gamma_v\). The first term of \({\cal L}_{\rm ELBO}\) is the expected log-likelihood for every sample given the latent function \(\boldsymbol{g}\) and the variational distribution \(q\). The second term (KL divergence) regularizes the approximation to ensure \(q\) remaining close to the GP prior \(p\).

We introduce DKGP into our KPCA framework to learn the Gaussian kernel. In experiments, the Gaussian kernel parameters are obtained by solving Problem (15 ) on the \(\ell_2\)-normalized penultimate-layer features from ResNet50 pretrained on ImageNet-1K. Then, RFF and Nyström mappings based on the learned Gaussian kernel are both implemented to perform KPCA for OoD detection, with results in Table ¿tbl:response:tab:kernel-learning? (RFF-DKGP and Nyström-DKGP).

Table ¿tbl:response:tab:kernel-learning? compares our proposed KPCA-based framework without (w/o) and with (w/) learnable representations for OoD detection. The two kernel learning methods, RFF-OPT [94] and DKGP [95], improve the detection performance. Both methods optimize our proposed Cosine-Gaussian kernel towards different objectives: the kernel alignment of Problem (14 ) in RFF-OPT and the classification ELBO of Problem (15 ) in DKGP. Their enhanced detection results with representations learned under varied objectives further validate the flexibility of the Cosine-Gaussian kernel and its effectiveness in capturing the non-linear patterns that separate OoD from InD data.

In contrast, the shallow AE [93] yields weaker detection results. This may stem from its optimization objective: the MSE-based objective in Problem (13 ) primarily focuses on feature-level reconstruction fidelity on InD data, and does not explicitly encourage InD-OoD separability. Unlike our Cosine-Gaussian kernel, which is designed for this separability, the AE learns a latent space optimized for reconstruction rather than discrimination. Nevertheless, as an effective learnable non-linear mapping method, AE offers a promising direction for OoD detection through objectives explicitly designed to capture distribution shifts to yield discriminative latent spaces.

Detection performance of KPCA with learnable representations. The average FPR and AUROC results over the selected 4 OoD datasets are reported.

References↩︎

[1]
Jonathan Ho, Ajay Jain, and Pieter Abbeel. Denoising diffusion probabilistic models. Advances in neural information processing systems, 33:6840–6851, 2020.
[2]
Long Ouyang, Jeffrey Wu, Xu Jiang, Diogo Almeida, Carroll Wainwright, Pamela Mishkin, Chong Zhang, Sandhini Agarwal, Katarina Slama, Alex Ray, et al. Training language models to follow instructions with human feedback. Advances in Neural Information Processing Systems, 35:27730–27744, 2022.
[3]
Haochen Liu, Yiqi Wang, Wenqi Fan, Xiaorui Liu, Yaxin Li, Shaili Jain, Yunhao Liu, Anil Jain, and Jiliang Tang. Trustworthy ai: A computational perspective. ACM Transactions on Intelligent Systems and Technology, 14(1):1–59, 2022.
[4]
Clark Barrett, Brad Boyd, Elie Bursztein, Nicholas Carlini, Brad Chen, Jihye Choi, Amrita Roy Chowdhury, Mihai Christodorescu, Anupam Datta, Soheil Feizi, et al. Identifying and mitigating the security risks of generative ai. Foundations and Trends® in Privacy and Security, 6(1):1–52, 2023.
[5]
Jingkang Yang, Kaiyang Zhou, Yixuan Li, and Ziwei Liu. Generalized out-of-distribution detection: A survey. International Journal of Computer Vision, 132(12):5635–5662, 2024.
[6]
Laurens Van der Maaten and Geoffrey Hinton. Visualizing data using t-sne. Journal of machine learning research, 9(11), 2008.
[7]
Weitang Liu, Xiaoyun Wang, John Owens, and Yixuan Li. Energy-based out-of-distribution detection. Advances in neural information processing systems, 33:21464–21475, 2020.
[8]
Yinan Wang, Wenbo Sun, Jionghua Jin, Zhenyu Kong, and Xiaowei Yue. Wood: Wasserstein-based out-of-distribution detection. IEEE Transactions on Pattern Analysis and Machine Intelligence, 46(2):944–956, 2023.
[9]
Rui Huang, Andrew Geng, and Yixuan Li. On the importance of gradients for detecting distributional shifts in the wild. Advances in Neural Information Processing Systems, 34:677–689, 2021.
[10]
Yingwen Wu, Tao Li, Xinwen Cheng, Jie Yang, and Xiaolin Huang. Low-dimensional gradient helps out-of-distribution detection. IEEE Transactions on Pattern Analysis and Machine Intelligence, 2024.
[11]
Yiyou Sun, Chuan Guo, and Yixuan Li. React: Out-of-distribution detection with rectified activations. Advances in Neural Information Processing Systems, 34:144–157, 2021.
[12]
Yiyou Sun, Yifei Ming, Xiaojin Zhu, and Yixuan Li. Out-of-distribution detection with deep nearest neighbors. In International Conference on Machine Learning, pages 20827–20840. PMLR, 2022.
[13]
Xiaoyuan Guan, Zhouwu Liu, Wei-Shi Zheng, Yuren Zhou, and Ruixuan Wang. Revisit pca-based technique for out-of-distribution detection. In Proceedings of the IEEE/CVF International Conference on Computer Vision, pages 19431–19439, 2023.
[14]
Yao Zhu, YueFeng Chen, Chuanlong Xie, Xiaodan Li, Rong Zhang, Hui Xue, Xiang Tian, Yaowu Chen, et al. Boosting out-of-distribution detection with typical features. Advances in Neural Information Processing Systems, 35:20758–20769, 2022.
[15]
Mingyu Xu, Zheng Lian, Bin Liu, and Jianhua Tao. Vra: variational rectified activation for out-of-distribution detection. Advances in Neural Information Processing Systems, 36:28941–28959, 2023.
[16]
Andrija Djurisic, Nebojsa Bozanic, Arjun Ashok, and Rosanne Liu. Extremely simple activation shaping for out-of-distribution detection. In The Eleventh International Conference on Learning Representations, 2023.
[17]
Kai Xu, Rongyu Chen, Gianni Franchi, and Angela Yao. Scaling for training time and post-hoc out-of-distribution detection enhancement. In The Twelfth International Conference on Learning Representations, 2024.
[18]
Yue Yuan, Rundong He, Yicong Dong, Zhongyi Han, and Yilong Yin. Discriminability-driven channel selection for out-of-distribution detection. In Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition, pages 26171–26180, 2024.
[19]
Yong Hyun Ahn, Gyeong-Moon Park, and Seong Tae Kim. Line: Out-of-distribution detection by leveraging important neurons. In 2023 IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR), pages 19852–19862. IEEE, 2023.
[20]
Yue Song, Nicu Sebe, and Wei Wang. Rankfeat: Rank-1 feature removal for out-of-distribution detection. Advances in Neural Information Processing Systems, 35:17885–17898, 2022.
[21]
Kimin Lee, Kibok Lee, Honglak Lee, and Jinwoo Shin. A simple unified framework for detecting out-of-distribution samples and adversarial attacks. Advances in neural information processing systems, 31, 2018.
[22]
Maximilian Müller and Matthias Hein. Mahalanobis++: Improving OOD detection via feature normalization. In Proceedings of the 42nd International Conference on Machine Learning, volume 267 of Proceedings of Machine Learning Research, pages 45151–45184, 2025.
[23]
Jaewoo Park, Yoon Gyo Jung, and Andrew Beng Jin Teoh. Nearest neighbor guidance for out-of-distribution detection. In Proceedings of the IEEE/CVF International Conference on Computer Vision, pages 1686–1695, 2023.
[24]
Jinsong Zhang, Qiang Fu, Xu Chen, Lun Du, Zelin Li, Gang Wang, Shi Han, Dongmei Zhang, et al. Out-of-distribution detection based on in-distribution data patterns memorization with modern hopfield energy. In The Eleventh International Conference on Learning Representations, 2022.
[25]
Xuhui Li, Zhen Fang, Yonggang Zhang, Ning Ma, Jiajun Bu, Bo Han, and Haishuai Wang. Characterizing submanifold region for out-of-distribution detection. IEEE Transactions on Knowledge and Data Engineering, 2024.
[26]
Ibrahima Ndiour, Nilesh Ahuja, and Omesh Tickoo. Out-of-distribution detection with subspace techniques and probabilistic modeling of features. arXiv preprint arXiv:2012.04250, 2020.
[27]
Alireza Zaeemzadeh, Niccolo Bisagno, Zeno Sambugaro, Nicola Conci, Nazanin Rahnavard, and Mubarak Shah. Out-of-distribution detection using union of 1-dimensional subspaces. In Proceedings of the IEEE/CVF conference on Computer Vision and Pattern Recognition, pages 9452–9461, 2021.
[28]
Bernhard Schölkopf, Alexander Smola, and Klaus-Robert Müller. Kernel principal component analysis. In International conference on artificial neural networks, pages 583–588. Springer, 1997.
[29]
Bernhard Schölkopf, Alexander Smola, and Klaus-Robert Müller. Nonlinear component analysis as a kernel eigenvalue problem. Neural computation, 10(5):1299–1319, 1998.
[30]
Jia Deng, Wei Dong, Richard Socher, Li-Jia Li, Kai Li, and Li Fei-Fei. Imagenet: A large-scale hierarchical image database. In IEEE Conference on Computer Vision and Pattern Recognition, pages 248–255, 2009.
[31]
Karl Pearson. Liii. on lines and planes of closest fit to systems of points in space. The London, Edinburgh, and Dublin philosophical magazine and journal of science, 2(11):559–572, 1901.
[32]
Hervé Abdi and Lynne J Williams. Principal component analysis. Wiley interdisciplinary reviews: computational statistics, 2(4):433–459, 2010.
[33]
Ali Rahimi and Benjamin Recht. Random features for large-scale kernel machines. Advances in neural information processing systems, 20, 2007.
[34]
Christopher Williams and Matthias Seeger. Using the nyström method to speed up kernel machines. In Advances in Neural Information Processing Systems, volume 13. MIT Press, 2000.
[35]
Kun Fang, Qinghua Tao, Kexin Lv, Mingzhen He, Xiaolin Huang, and JIE YANG. Kernel pca for out-of-distribution detection. In Advances in Neural Information Processing Systems, volume 37, pages 134317–134344, 2024.
[36]
Walter Rudin. Fourier analysis on groups, volume 121967. Wiley Online Library, 1962.
[37]
A Krizhevsky. Learning multiple layers of features from tiny images. Master’s thesis, University of Toronto, 2009.
[38]
Fisher Yu, Ari Seff, Yinda Zhang, Shuran Song, Thomas Funkhouser, and Jianxiong Xiao. Lsun: Construction of a large-scale image dataset using deep learning with humans in the loop. arXiv preprint arXiv:1506.03365, 2015.
[39]
Bolei Zhou, Agata Lapedriza, Aditya Khosla, Aude Oliva, and Antonio Torralba. Places: A 10 million image database for scene recognition. IEEE transactions on pattern analysis and machine intelligence, 40(6):1452–1464, 2017.
[40]
Ingwer Borg and Patrick JF Groenen. Modern multidimensional scaling: Theory and applications. Springer Science & Business Media, 2007.
[41]
Petros Drineas and Michael W. Mahoney. On the nyström method for approximating a gram matrix for improved kernel-based learning. Journal of Machine Learning Research, 6(72):2153–2175, 2005.
[42]
Sanjiv Kumar, Mehryar Mohri, and Ameet Talwalkar. Sampling methods for the nyström method. The Journal of Machine Learning Research, 13(1):981–1006, 2012.
[43]
David Lopez-Paz, Suvrit Sra, Alex Smola, Zoubin Ghahramani, and Bernhard Schölkopf. Randomized nonlinear component analysis. In International conference on machine learning, pages 1359–1367. PMLR, 2014.
[44]
Enayat Ullah, Poorya Mianjy, Teodor Vanislavov Marinov, and Raman Arora. Streaming kernel pca with \(\mathcal{O}(\sqrt n)\) random features. 31, 2018.
[45]
Nicholas Sterge, Bharath Sriperumbudur, Lorenzo Rosasco, and Alessandro Rudi. Gain with no pain: Efficiency of kernel-pca by nyström sampling. In International Conference on Artificial Intelligence and Statistics, pages 3642–3652. PMLR, 2020.
[46]
Fredrik Hallgren. Kernel pca with the nyström method. arXiv preprint arXiv:2109.05578, 2021.
[47]
Bharath K Sriperumbudur and Nicholas Sterge. Approximate kernel pca: Computational versus statistical trade-off. The Annals of Statistics, 50(5):2713–2736, 2022.
[48]
Nicholas Sterge and Bharath K Sriperumbudur. Statistical optimality and computational efficiency of nystrom kernel pca. Journal of Machine Learning Research, 23(337):1–32, 2022.
[49]
JT-Y Kwok and IW-H Tsang. The pre-image problem in kernel methods. IEEE transactions on neural networks, 15(6):1517–1525, 2004.
[50]
Yue Song, Wei Wang, and Nicu Sebe. Rankfeat&rankweight: Rank-1 feature/weight removal for out-of-distribution detection. IEEE Transactions on Pattern Analysis and Machine Intelligence, 2024.
[51]
Kaiming He, Xiangyu Zhang, Shaoqing Ren, and Jian Sun. Deep residual learning for image recognition. In IEEE Conference on Computer Vision and Pattern Recognition, pages 770–778, 2016.
[52]
Mark Sandler, Andrew Howard, Menglong Zhu, Andrey Zhmoginov, and Liang-Chieh Chen. Mobilenetv2: Inverted residuals and linear bottlenecks. In Proceedings of the IEEE conference on computer vision and pattern recognition, pages 4510–4520, 2018.
[53]
Alexey Dosovitskiy, Lucas Beyer, Alexander Kolesnikov, Dirk Weissenborn, Xiaohua Zhai, Thomas Unterthiner, Mostafa Dehghani, Matthias Minderer, Georg Heigold, Sylvain Gelly, Jakob Uszkoreit, and Neil Houlsby. An image is worth 16x16 words: Transformers for image recognition at scale. In International Conference on Learning Representations, 2021.
[54]
Grant Van Horn, Oisin Mac Aodha, Yang Song, Yin Cui, Chen Sun, Alex Shepard, Hartwig Adam, Pietro Perona, and Serge Belongie. The inaturalist species classification and detection dataset. In Proceedings of the IEEE conference on computer vision and pattern recognition, pages 8769–8778, 2018.
[55]
Jianxiong Xiao, James Hays, Krista A Ehinger, Aude Oliva, and Antonio Torralba. Sun database: Large-scale scene recognition from abbey to zoo. In 2010 IEEE computer society conference on computer vision and pattern recognition, pages 3485–3492. IEEE, 2010.
[56]
Mircea Cimpoi, Subhransu Maji, Iasonas Kokkinos, Sammy Mohamed, and Andrea Vedaldi. Describing textures in the wild. In Proceedings of the IEEE conference on computer vision and pattern recognition, pages 3606–3613, 2014.
[57]
Prannay Khosla, Piotr Teterwak, Chen Wang, Aaron Sarna, Yonglong Tian, Phillip Isola, Aaron Maschinot, Ce Liu, and Dilip Krishnan. Supervised contrastive learning. Advances in neural information processing systems, 33:18661–18673, 2020.
[58]
Dan Hendrycks and Kevin Gimpel. A baseline for detecting misclassified and out-of-distribution examples in neural networks. In International Conference on Learning Representations, 2016.
[59]
Dan Hendrycks, Steven Basart, Mantas Mazeika, Andy Zou, Joseph Kwon, Mohammadreza Mostajabi, Jacob Steinhardt, and Dawn Song. Scaling out-of-distribution detection for real-world settings. In International Conference on Machine Learning, pages 8759–8773. PMLR, 2022.
[60]
Shiyu Liang, Yixuan Li, and R Srikant. Enhancing the reliability of out-of-distribution image detection in neural networks. In International Conference on Learning Representations, 2018.
[61]
Haoqi Wang, Zhizhong Li, Litong Feng, and Wayne Zhang. Vim: Out-of-distribution with virtual-logit matching. In Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition, pages 4921–4930, 2022.
[62]
Zihan Zhang and Xiang Xiang. Decoupling maxlogit for out-of-distribution detection. In Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition, pages 3388–3397, 2023.
[63]
Vikash Sehwag, Mung Chiang, and Prateek Mittal. Ssd: A unified framework for self-supervised outlier detection. In International Conference on Learning Representations, 2020.
[64]
Adam Paszke, Sam Gross, Francisco Massa, Adam Lerer, James Bradbury, Gregory Chanan, Trevor Killeen, Zeming Lin, Natalia Gimelshein, Luca Antiga, et al. Pytorch: An imperative style, high-performance deep learning library. Advances in neural information processing systems, 32, 2019.
[65]
Yiyou Sun and Yixuan Li. Dice: Leveraging sparsification for out-of-distribution detection. In European Conference on Computer Vision, pages 691–708. Springer, 2022.
[66]
Sima Behpour, Thang Long Doan, Xin Li, Wenbin He, Liang Gou, and Liu Ren. Gradorth: A simple yet efficient out-of-distribution detection with orthogonal projection of gradients. Advances in Neural Information Processing Systems, 36:38206–38230, 2023.
[67]
Jeff Johnson, Matthijs Douze, and Hervé Jégou. Billion-scale similarity search with gpus. IEEE Transactions on Big Data, 7(3):535–547, 2019.
[68]
Ninh Pham and Rasmus Pagh. Fast and scalable polynomial kernels via explicit feature maps. In Proceedings of the 19th ACM SIGKDD international conference on Knowledge discovery and data mining, pages 239–247, 2013.
[69]
Joost Van Amersfoort, Lewis Smith, Yee Whye Teh, and Yarin Gal. Uncertainty estimation using a single deep deterministic neural network. In International conference on machine learning, pages 9690–9700. PMLR, 2020.
[70]
Heiko Hoffmann. Kernel pca for novelty detection. Pattern recognition, 40(3):863–874, 2007.
[71]
Yen-Chang Hsu, Yilin Shen, Hongxia Jin, and Zsolt Kira. Generalized odin: Detecting out-of-distribution image without learning from out-of-distribution data. In Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition, pages 10951–10960, 2020.
[72]
Sina Sharifi, Taha Entesari, Bardia Safaei, Vishal M Patel, and Mahyar Fazlyab. Gradient-regularized out-of-distribution detection. In European Conference on Computer Vision, pages 459–478. Springer, 2024.
[73]
Yeonguk Yu, Sungho Shin, Seongju Lee, Changhyun Jun, and Kyoobin Lee. Block selection method for using feature norm in out-of-distribution detection. In Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition, pages 15701–15711, 2023.
[74]
Yiye Chen, Yunzhi Lin, Ruinian Xu, and Patricio A Vela. Wdiscood: Out-of-distribution detection via whitened linear discriminant analysis. In Proceedings of the IEEE/CVF International Conference on Computer Vision, pages 5298–5307, 2023.
[75]
Barış Zöngür, Robin Hesse, and Stefan Roth. Activation subspaces for out-of-distribution detection. In Proceedings of the IEEE/CVF International Conference on Computer Vision, pages 3509–3519, 2025.
[76]
Kun Fang, Qinghua Tao, Xiaolin Huang, and Jie Yang. Revisiting deep ensemble for out-of-distribution detection: A loss landscape perspective. International Journal of Computer Vision, 132(12):6107–6126, 2024.
[77]
Zhenzhen Liu, Jin Peng Zhou, Yufan Wang, and Kilian Q Weinberger. Unsupervised out-of-distribution detection with diffusion inpainting. In International Conference on Machine Learning, pages 22528–22538. PMLR, 2023.
[78]
Jihoon Tack, Sangwoo Mo, Jongheon Jeong, and Jinwoo Shin. Csi: Novelty detection via contrastive learning on distributionally shifted instances. Advances in neural information processing systems, 33:11839–11852, 2020.
[79]
Zhen Fang, Yixuan Li, Jie Lu, Jiahua Dong, Bo Han, and Feng Liu. Is out-of-distribution detection learnable? Advances in Neural Information Processing Systems, 35:37199–37213, 2022.
[80]
Peyman Morteza and Yixuan Li. Provable guarantees for understanding out-of-distribution detection. In Proceedings of the AAAI Conference on Artificial Intelligence, volume 36, pages 7831–7840, 2022.
[81]
Zhilin Zhao, Longbing Cao, and S Yu Philip. Out-of-distribution detection by regaining lost clues. Artificial Intelligence, 339:104275, 2025.
[82]
Zhilin Zhao, Longbing Cao, and Kun-Yu Lin. Supervision adaptation balancing in-distribution generalization and out-of-distribution detection. IEEE Transactions on Pattern Analysis and Machine Intelligence, 45(12):15743–15758, 2023.
[83]
Fanghui Liu, Xiaolin Huang, Yudong Chen, and Johan AK Suykens. Random features for kernel approximation: A survey on algorithms, theory, and beyond. IEEE Transactions on Pattern Analysis and Machine Intelligence, 44(10):7128–7148, 2021.
[84]
Kun Fang, Fanghui Liu, Xiaolin Huang, and Jie Yang. End-to-end kernel learning via generative random fourier features. Pattern Recognition, 134:109057, 2023.
[85]
Tianbao Yang, Yu-Feng Li, Mehrdad Mahdavi, Rong Jin, and Zhi-Hua Zhou. Nyström method vs random fourier features: A theoretical and empirical comparison. Advances in neural information processing systems, 25, 2012.
[86]
Mikhail Belkin, Daniel Hsu, Siyuan Ma, and Soumik Mandal. Reconciling modern machine-learning practice and the classical bias–variance trade-off. Proceedings of the National Academy of Sciences, 116(32):15849–15854, 2019.
[87]
Jian Li, Yong Liu, and Weiping Wang. Convolutional spectral kernel learning with generalization guarantees. Artificial Intelligence, 313:103803, 2022.
[88]
Qinghua Tao, Francesco Tonin, Alex Lambert, Yingyi Chen, Panagiotis Patrinos, and Johan Suykens. Learning in feature spaces via coupled covariances: Asymmetric kernel svd and nyström method. In International Conference on Machine Learning, pages 47929–47947. PMLR, 2024.
[89]
Yunyang Xiong, Zhanpeng Zeng, Rudrasis Chakraborty, Mingxing Tan, Glenn Fung, Yin Li, and Vikas Singh. Nyströmformer: A nyström-based algorithm for approximating self-attention. In Proceedings of the AAAI conference on artificial intelligence, volume 35, pages 14138–14148, 2021.
[90]
Ryuichiro Hataya and Makoto Yamada. Nyström method for accurate and scalable implicit differentiation. In International Conference on Artificial Intelligence and Statistics, pages 4643–4654. PMLR, 2023.
[91]
Yuval Netzer, Tao Wang, Adam Coates, Alessandro Bissacco, Bo Wu, and Andrew Y Ng. Reading digits in natural images with unsupervised feature learning. In Proceedings of the NIPS Workshop on Deep Learning and Unsupervised Feature Learning, 2011.
[92]
Pingmei Xu, Krista A Ehinger, Yinda Zhang, Adam Finkelstein, Sanjeev R Kulkarni, and Jianxiong Xiao. Turkergaze: Crowdsourcing saliency with webcam based eye tracking. arXiv preprint arXiv:1504.06755, 2015.
[93]
Felix Meissen, Johannes Paetzold, Georgios Kaissis, and Daniel Rueckert. Unsupervised anomaly localization with structural feature-autoencoders. In International MICCAI Brainlesion Workshop, pages 14–24. Springer, 2022.
[94]
Aman Sinha and John C Duchi. Learning kernels with random features. Advances in neural information processing systems, 29, 2016.
[95]
Andrew Gordon Wilson, Zhiting Hu, Ruslan Salakhutdinov, and Eric P Xing. Deep kernel learning. In Artificial intelligence and statistics, pages 370–378. PMLR, 2016.
[96]
Christopher KI Williams and Carl Edward Rasmussen. Gaussian processes for machine learning, volume 2. MIT press Cambridge, MA, 2006.

  1. https://github.com/fanghenshaometeor/ood-kernel-pca-ext↩︎

  2. https://docs.pytorch.org/vision/main/_modules/torchvision/models/resnet.html#resnet50↩︎

  3. https://github.com/duchi-lab/learning-kernels↩︎