Five OOD Detection Scores, Explained: MSP, Energy, Mahalanobis, kNN and ViM

The first post in this series introduced out-of-distribution (OOD) detection: a score for strangeness plus a threshold, bolted onto a classifier that otherwise can't say "I don't know." This one is about the scores.
Five names come up again and again: MSP, Energy, Mahalanobis, kNN and ViM. All five read the same frozen model and ask the same question, "does this input belong here?", and they often give different answers. For each one I'll cover what it measures, where in the network it reads from, and where it breaks.
What they have in common
All five are post-hoc. They work on a model that's already trained and frozen, and in their basic form they need no examples of strange inputs. You feed one input through the model as usual and compute the score from what the model already produced: its features, its logits or its probabilities.
That's why these five are the standard toolkit. They cost almost nothing to try, and every new method has to beat them.
Where each one looks
The five split into two families plus one hybrid:
- Logit space: MSP and Energy. They read the model's answer, meaning 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.

If you remember one thing from this post, make it this split. When a score fails, it's usually because of where it looks.
MSP, the baseline
MSP is the model's top softmax probability. If the model says "dog, 92%", the score is 0.92. High is familiar, low is strange.
Hendrycks and Gimpel (2017) showed this number is a weak but real guard against unfamiliar inputs, and it became the baseline for the whole field. It's free, since the model already computed it.
Its weakness comes from the softmax having to sum to 1. MSP tells you which known class an input resembles most, never whether it belongs to any of them. A wolf shown to a dog classifier can score 0.95 on "dog" simply because "dog" is the closest thing the model knows, so MSP is at its most confident in exactly the cases where you'd want caution.
Use it as the first number in every comparison. A new score that can't beat MSP hasn't improved anything.
Energy: the logits before normalisation
Energy uses the raw logits, the scores before the softmax. For logits fᵢ(x), one per class, the energy of an input is
E(x) = −log Σ exp(fᵢ(x))
Low energy means familiar. Flip the sign and it reads like the others, with high meaning familiar.
It beats MSP because the softmax throws information away. Normalising keeps only the relative size of the logits; Energy also keeps their overall magnitude. Familiar inputs tend to produce large logits across the board, so their total evidence is high and their energy is low. Strange inputs produce small, flat logits and high energy. It's the same logits MSP uses, but MSP only looks at the winner while Energy looks at all of them.
It costs nothing extra: same forward pass, one more line of arithmetic. Liu et al. (2020) showed it separates familiar from strange more cleanly than MSP, which is why it's now the default logit-space score. MaxLogit, which just takes the largest logit, is a close cousin with even less arithmetic.
It still only sees the answer, though. If a strange input happens to produce confident-looking logits, Energy gets fooled the same way MSP does.

Mahalanobis: distance to the nearest class centre
Mahalanobis ignores the classifier's output entirely. Take the input's feature vector from the layer just before the classifier, and measure how far it sits from each class's centre, where the centre is the average feature of that class's training examples. The score is the distance to the nearest centre. Close means familiar.
The scaling is what makes it work. A plain Euclidean distance treats every direction equally, but features don't spread equally: some directions vary a lot within a class and others barely move. Mahalanobis scales by the covariance of the training features, so a step along a direction where the class is tight counts for more than a step where it's loose. Lee et al. (2018) built the method this way and also computed it at several layers at once. (One caveat: their multi-layer version learns how to weight the layers using a small validation set, so the full recipe isn't quite as free of OOD data as the single-layer one.)
It's strong because it's class-aware. An input that lands near some class centre counts as familiar even when the classifier is unsure, and one that lands far from every centre gets flagged even when the softmax is confident.
The weak spot is the shape assumption. Mahalanobis treats each class as a roughly Gaussian blob with one centre and one spread. If a class is really two clusters, or shaped like a crescent, that picture is wrong and the distance misleads. It also needs the covariance matrix inverted, which can get numerically fiddly with high-dimensional features.
kNN: distance to the nearest training examples
This is the simplest idea on 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. A close neighbour means familiar, a distant one means strange.
kNN makes no assumption about the shape of the classes. Two clusters, a crescent, a ring: it never fits a shape, so none of that matters. Sun et al. (2022) showed this non-parametric distance beats parametric Mahalanobis-style approaches on the standard benchmarks, and on ImageNet-1K it cut FPR95 by 24.77% against a strong Mahalanobis-based baseline.
The detail that makes it work is normalisation. The paper's main finding is that nearest neighbours are only reliable in a compact, L2-normalised feature space. Without normalisation, a few large-magnitude directions dominate the distance and the neighbours stop meaning anything.
You pay in memory and search time. You have to keep the training embeddings around and search them at test time, which is trivial at CIFAR scale and needs a proper index for very large training sets. Unlike MSP or Energy, it also needs the training data on hand, not just the model.
ViM: turning the leftover into a class
ViM (Wang et al., 2022) starts from something the other scores miss. The classifier only uses part of the feature space, the directions its weights point along. Whatever part of a feature is orthogonal to those directions never shows up in the logits. ViM measures that leftover.
The recipe:
- Find the principal subspace: the D-dimensional subspace where most of the training features' variation lives.
- Split every new feature in two. The part inside the subspace is what the classifier can see. The part outside it, the residual, is what it can't explain.
- Turn the residual into a virtual logit. Scale its size by a constant α, chosen so the average residual matches the average top logit on training data, which puts it on the same scale as a real logit.
- Add a virtual class. Append the virtual logit to the real ones and run the softmax over C+1 classes. The virtual class's probability is the ViM score. A big residual means the virtual class wins and the input gets flagged.
Because it combines the class-specific logits with the class-agnostic residual, ViM catches strange inputs that either family would miss on its own. On ImageNet with a BiT-S model it averaged 90.91% AUROC across four hard OOD benchmarks, about 4 points ahead of the best baseline at the time.
The residual is global, though. It measures distance from the principal subspace of all the training data without asking which class the input claims to be. A strange input that sits inside the shared subspace but far from its claimed class can slip through.

Cheat sheet

Which one to start with
Here's the order I'd go in:
- MSP. It's free, it's the baseline, and every paper reports it.
- Energy. Also free, and better than MSP on the same logits.
- kNN, if you can afford to store the training features. It's the strongest simple feature-space score and assumes nothing about class shape.
- ViM, when you want a strong single post-hoc score. It led its own ImageNet benchmarks, needs a one-time principal-subspace computation on the training features, and is just as cheap as the others at test time.
- Mahalanobis, when your classes really are blob-shaped, or when you want the multi-layer version that reads features from several depths.
Running several side by side is normal practice. No single score wins everywhere, and the inputs where they disagree tell you what kind of strangeness your data contains.
What none of them solve yet
Near OOD is still hard. All five separate far OOD almost perfectly and near OOD only partly. A wolf will always be harder than a handwritten digit.
The threshold has to be set on familiar data. Pick it on the wrong data and you flag everything or nothing. The score ranks inputs; the threshold makes the call.
Combining scores doesn't automatically help. Averaging two good scores only pays off if they fail on different inputs. If they fail on the same ones, the combination inherits the same blind spot. That's the subject of the next post, which looks at CombOOD.
Which score do you reach for first? Has one ever fooled you on a near-OOD input?