← all repositories
lars76/kmeans-anchor-boxes

YOLO's anchor boxes need k-means, but k-means lies

A dead-simple repo that reveals why standard k-means fails for anchor-box clustering and what actually works.

532 stars Python Computer Vision
kmeans-anchor-boxes
Velocity · 7d
+0.2
★ / day
Trend
steady
star history

What it does

Implements k-means clustering using IoU (Intersection over Union) distance instead of Euclidean distance, specifically for generating anchor-box priors in YOLO-style object detectors. The repo includes a test harness against the VOC 2007 dataset to reproduce the YOLO9000 paper’s reported numbers.

The interesting bit

Standard k-means with IoU converges to worse results than the paper claims. The author chased this down: running until convergence traps you in local minima, and restarting 50 times is too slow. The fix that actually hit the paper’s targets? Swap the mean for the median — k-medians clustering. It’s a one-word change that closes a 5+ point IoU gap.

Key highlights

  • Reproduces YOLO9000 anchor clustering with IoU-based distance metric
  • Standard k-means undershoots paper benchmarks (≈55 vs. 61.0 IoU for k=5)
  • k-medians with random restarts hits 60.15 (k=5) and 67.13 (k=9), matching paper claims
  • Single-file implementation, ~100 lines, no dependencies beyond NumPy
  • Includes VOC 2007 test script with reported convergence plots

Caveats

  • Results vary with initialization; paper numbers are best-case, not guaranteed
  • Only validated on VOC 2007; no tests on COCO or modern datasets
  • Code appears unmaintained (last commit years ago, Python 3 unittest style)

Verdict

Grab this if you’re training a custom YOLO variant and need to generate anchors that don’t suck. Skip it if you’re using off-the-shelf pretrained weights or a framework that already handles anchor optimization internally.

heatdrop uses Google Analytics to see which pages get read — nothing else. Your call. How we handle data.