Out-of-Distribution Detection Scores: MSP, Energy, Mahalanobis, kNN, and ViM

Out-of-Distribution Detection Scores: MSP, Energy, Mahalanobis, kNN, and ViM
A previous post introduced out-of-distribution (OOD) detection: a score for strangeness, plus a threshold, added to a classifier that otherwise cannot say "I don't know". This post covers the scores themselves. Five names recur in the OOD literature: MSP, Energy, ViM, Mahalanobis, and kNN. All five read the same frozen model and answer the same question — does this input belong here? — and they often disagree. What follows is what each one measures, where it reads from, and where it fails.
First, the shared setup
Every score in this post is post-hoc: it reads a model that is already trained and frozen, and it needs no unfamiliar examples to work. One input is fed in, the model runs as normal, and the score is computed from what the model already produced — its features, its logits, or its probabilities.
That is why these five are the standard toolkit. They cost almost nothing to try, and they are the baselines every new method has to beat.
Two families, one hybrid
The five scores fall into two families, plus one hybrid that uses both:
- Logit space — MSP and Energy. They read the model's answer: the logits, or the probabilities built from them.
- Feature space — Mahalanobis and kNN. They ignore the answer and read the representation: where the input lands inside the network, compared with where the training data landed.
- Both — ViM. It reads the features and the logits, and combines them.
That split is the most useful thing to remember. When a score fails, it usually fails because of where it looks, not because of a bug.
MSP: the baseline everything is measured against
What it is: the model's top softmax probability. If the model says "dog, 92%", the MSP score is 0.92. High means familiar, low means strange.
Why it exists: Hendrycks and Gimpel showed in 2017 that this simple number is a weak but real guard against unfamiliar inputs — and it became the baseline the whole field compares against. It costs nothing: the model already computed it.
Why it's weak: the softmax must sum to 1. It only tells you which known class the input resembles most, not whether the input belongs to any known class at all. A wolf shown to a dog classifier can score 0.95 on "dog", because "dog" is simply the closest thing the model knows. MSP is most confident where caution is most warranted.
Use it for: the first number in every comparison. A new score that cannot beat MSP does not improve on the baseline.
Energy: reading the logits before normalisation
What it is: instead of the normalised probabilities, Energy uses the raw logits — the scores before the softmax. The energy of an input is
E(x) = −log Σ exp(fᵢ(x))
where fᵢ(x) are the logits, one per class. Low energy means familiar; flip the sign and it behaves like the other scores (high = familiar).
Why it beats MSP: normalising throws information away. The softmax only keeps the relative size of the logits; Energy keeps their total magnitude too. A familiar input tends to produce large logits across the board, so its total evidence is high and its energy is low. A strange input produces small, flat logits, so its energy is high. Same logits as MSP — but MSP looks at the winner, Energy looks at the whole field.
The cost: nothing. Same forward pass, same model, one extra line of arithmetic. Liu et al. (2020) showed it separates familiar from strange more cleanly than MSP, and that is why it is now the default logit-space score. (Its close cousin MaxLogit — just the largest logit — is the same idea with even less arithmetic.)
Its blind spot: it still only sees the answer, not the representation. If a strange input happens to produce confident-looking logits, Energy is fooled the same way MSP is.
Mahalanobis: distance to the nearest class centre
What it is: Mahalanobis ignores the classifier's output. Take the input's features — the vector from the layer just before the classifier — and ask how far it sits from each class's centre, where "centre" means the average feature of that class's training examples. The score is the distance to the nearest centre. Close means familiar.
The key detail is the scaling. A plain distance treats every direction equally, but features don't spread equally: some directions vary a lot within a class, others barely move. Mahalanobis scales the distance by the data's covariance — the spread of the training features — so a step in a direction where the class is tight counts for more than a step where it is loose. Lee et al. (2018) built the method this way, and it can be computed at several layers of the network at once.
Why it's strong: it is class-aware. An input that lands near some class centre is treated as familiar even if the classifier itself is unsure, and an input that lands far from every centre is flagged even if the softmax is confident.
Its blind spot: it assumes each class's features form a roughly Gaussian blob — one centre, one spread. If a class is actually two clusters, or shaped like a crescent, the single-centre picture is wrong, and the distance misleads. It also needs the covariance matrix inverted, which can be numerically fiddly on high-dimensional features.
kNN: distance to the nearest training examples
What it is: the simplest idea in the list. Store the training set's features. For a new input, find its k-th nearest training neighbour in feature space and measure the distance. Close neighbour means familiar; far away means strange.
Why it works: it makes no assumption about the shape of the classes. Two clusters, a crescent, a ring — kNN doesn't care, because it never fits a shape in the first place. Sun et al. (2022) showed this non-parametric distance beats the parametric Mahalanobis approach on the standard benchmarks, and on ImageNet-1K it cut the false-positive rate (at 95% true-positive rate) by 24.77% against a strong Mahalanobis-based baseline.
What makes it work: normalise the features first. The paper's key finding is that a compact, L2-normalised feature space is what makes nearest neighbours reliable — without normalisation, a few large-magnitude directions dominate the distance and the neighbours stop meaning anything.
The cost: memory and search. You have to store the training embeddings and search them at test time. On CIFAR-scale data that is trivial; on very large training sets it needs an index. And unlike MSP or Energy, it needs the training data at hand — not just the model.
ViM: the hybrid that turns the leftover into a class
What it is: ViM (Wang et al., 2022) starts from an observation the other scores miss. The classifier only uses part of the feature space — the directions its weights point along. The rest of the feature, the part orthogonal to everything the classifier cares about, is invisible to the logits. ViM measures exactly that leftover.
The procedure is:
- Find the principal subspace. Take the training features and find the directions they mostly live in — the D-dimensional subspace that captures most of their variation.
- Split every new feature in two. The part inside that subspace is the part the classifier can see. The part outside it — the residual — is the part the classifier can't explain.
- Turn the residual into a virtual logit. Scale the residual's size by a constant α, chosen so the average residual matches the average top logit on training data. Now the residual is on the same scale as a real logit.
- Add a virtual class. Append that virtual logit to the real logits and run the softmax over C+1 classes. The probability of the virtual class is the ViM score: if the residual is big, the virtual class wins, and the input is flagged.
Why it's strong: it combines both kinds of information — the class-dependent logits and the class-agnostic residual — so it catches strange inputs that either family alone would miss. On ImageNet with a BiT-S model, ViM averaged 90.91% AUROC across four hard OOD benchmarks, about 4 points ahead of the best baseline at the time.
Its blind spot: the residual is global — it measures deviation from the principal subspace of all the training data, without asking which class the input claims to be. A strange input that happens to sit inside the shared subspace but far from its claimed class can slip past.
The cheat sheet
Which one should you start with?
A reasonable order is:
- MSP first. It is free, it is the baseline, and it is reported in every paper. A score that does not beat it has not improved on the baseline.
- Energy next. Also free, and better than MSP on the same logits.
- kNN when you can afford the memory. If you have the training features stored, it's the strongest simple feature score, and it makes no shape assumptions.
- ViM when you want the strongest single post-hoc score. It needs a one-time principal-subspace computation on your training features, and then it's just as cheap at test time.
- Mahalanobis when your classes really are blob-shaped — or when you want the multi-layer version, which reads features from several depths at once.
Running them side by side is standard practice. No single score wins everywhere, and where they disagree indicates what kind of unfamiliarity the data contains.
What none of them solve — yet
- Near-OOD is still hard. All five separate far-OOD almost perfectly and near-OOD only partially. A wolf will always be harder than a handwritten digit.
- The threshold needs familiar data to set. Pick it on the wrong data and you flag everything or nothing. The score ranks; the threshold decides.
- Combining scores isn't automatic. Averaging two good scores only helps if they fail on different inputs. If they fail together, the combination inherits the same blind spot — that is the CombOOD result, covered in the next post in this series.
Discussion prompt for readers: Which score do you reach for first — and has one ever fooled you on a near-OOD input? What was it?


