OneHex
OneHex · ML/CV and LLM engineering notes

← All posts

The accuracy was 90%. One slice was 17%.

I keep a habit now: before I trust an overall accuracy number, I print the slices.

The number that made me write this down was not from a huge benchmark. It was a 100-sample toy set I generated to show a teammate why “90% accurate” can be a useless sentence. Eighty-eight of the images were easy. The remaining twelve were night, far, and occluded. The easy slices were perfect. The hard slice was 16.7%. The average still came out to 90%.

If you only report the average, you will ship the easy cases and discover the hard ones in a bug report.

This is not a new idea. People who do computer vision professionally already know it. It still gets dropped under deadline, because the overall number is the one that fits in a slide. I would rather have the ugly table.

A set that looks fine until you split it

I used two classes, mug and bottle, and three conditions. The code is short on purpose. You should be able to paste it and get the same numbers I got.

from collections import defaultdict
from dataclasses import dataclass


@dataclass(frozen=True)
class Sample:
    condition: str
    label: str
    pred: str


def accuracy(rows: list[Sample]) -> float:
    if not rows:
        return float("nan")
    return sum(1 for r in rows if r.pred == r.label) / len(rows)


def by_condition(rows: list[Sample]) -> dict[str, float]:
    groups: dict[str, list[Sample]] = defaultdict(list)
    for row in rows:
        groups[row.condition].append(row)
    return {name: accuracy(items) for name, items in sorted(groups.items())}

The demo set is lopsided, because real logs are lopsided. Indoor close-ups are cheap to collect and easy to classify. Night shots at a distance, with a hand in the way, are not.

def build_demo_set() -> list[Sample]:
    rows: list[Sample] = []
    easy = (
        [("indoor-close-clear", "mug", "mug")] * 40
        + [("indoor-close-clear", "bottle", "bottle")] * 28
        + [("daylight-medium-clear", "mug", "mug")] * 12
        + [("daylight-medium-clear", "bottle", "bottle")] * 8
    )
    hard = (
        [("night-far-occluded", "mug", "bottle")] * 7
        + [("night-far-occluded", "bottle", "mug")] * 3
        + [("night-far-occluded", "mug", "mug")] * 2
    )
    for condition, label, pred in easy + hard:
        rows.append(Sample(condition, label, pred))
    return rows

I ran this file with Python 3. It printed:

n=100
overall_accuracy=90.00%
slice   daylight-medium-clear   n=20    accuracy=100.00%
slice   indoor-close-clear  n=68    accuracy=100.00%
slice   night-far-occluded  n=12    accuracy=16.67%
hard_confusion
  bottle->mug: 3
  mug->bottle: 7
  mug->mug: 2

Two of the twelve hard samples were correct. Seven mugs were called bottles. That is not a rounding error. That is a condition the model cannot do, hidden under a number that looks like a passing grade.

What I actually look at

I do not have a long ritual. I want three things on one page.

The overall number, because people will ask for it anyway. The same metric broken by the conditions I already know are different: lighting, distance, occlusion, camera, maybe time of day if the logs have it. And a confusion table on the worst slice, not only on the full set. The full-set confusion matrix here would be diluted by 88 perfect easy samples. You would barely see the swap.

If a slice has very few samples, I still print it. I just refuse to treat 16.7% on twelve examples as a precise claim. The point is not the second decimal. The point is that the slice exists and it is not close to the average.

I also want the count next to the percentage. 16.67% without n=12 is how a hard condition gets dismissed as noise, or, worse, how a two-sample slice gets treated like a benchmark.

Why the average drifts toward the easy data

This is just arithmetic, but it is the arithmetic that fools people.

68 indoor close-ups, all correct. 20 daylight mid-range, all correct. 12 hard cases, 2 correct. The mean is (68 + 20 + 2) / 100 = 0.90. The hard condition is 12% of the eval and almost none of the score. If your production traffic has more night and more occlusion than your eval set — and it often does, because those are the cases people bother to file — the number you reported is not the number you will get.

The fix is not a fancier model, not first. The fix is to stop letting the easy mass write the headline. Collect more of the slice that failed, or hold a separate threshold for it, or don’t ship that condition. All of those are product decisions. None of them show up if the only line in the report is accuracy=90%.

Don’t average the slice accuracies and call it a day

There is a second trap I have walked into.

If you take the three slice accuracies — 100%, 100%, and 16.7% — and average those three numbers, you get 72%. That is also the wrong headline. It pretends the twelve hard images are as important as the 68 indoor ones. Sometimes that is the product decision you want: every condition must clear a bar. Sometimes it is not, because the hard condition is rare and you have already told the user the feature does not work at night. Those are different contracts. The table should let you choose. A single mean, whether it is weighted by count or by slice, hides the choice.

I print both when I am being careful: the micro-average, which is just overall accuracy, and the per-slice numbers with their counts. I do not report a macro-average unless I am explicitly saying “every condition counts the same.”

How you cut the slices matters more than the printer. Lighting, distance, and occlusion are a decent default for camera work because they change the pixels in ways a model can latch onto, or fall over. If your logs already have camera ID, lens, or a “user reported bad result” flag, use those too. I would not invent a taxonomy of twenty buckets on a 100-row set. You will get slices with n=2 and a lot of false confidence. Start with the cuts you already believe are different, then split further only when a bucket is both large and mixed.

If you do not have condition labels yet, you can often recover a coarse cut from what you already stored: time of day, filename tokens, exposure, a bounding-box size as a stand-in for distance. It will be messy. A messy slice that shows a real dip is still more useful than a clean 90%.

What this is not

It is not a claim about a particular production system, and it is not a substitute for calibration, confidence, or a proper test set. A synthetic 100-row file will not tell you whether your night data is labeled well, or whether “occluded” was defined the same way in training and in eval. If the hard slice is mislabeled, you will “fix” the wrong thing.

It is also not an argument against overall metrics. I still compute them. I just don’t stop there.

If you already slice by condition, this will feel obvious. Good. The number of times I have seen a green average and a red slice in the same table is why I still run the script. The file I used is eval_slices.py. If your numbers come out different, the set changed. If they come out the same and you only had the 90%, you almost shipped the wrong story.