A drop-in replacement for pycocotools that computes COCO-style AP/AR for
object detection, instance segmentation and keypoint detection. Ground-truth
indexing, IoU/OKS matching and precision–recall accumulation run in a
multithreaded Rust core (Rayon), exposed to Python through PyO3.
Bit-exact metrics. The complete precision, recall and scores
arrays are bit-identical to pycocotools on every tested input, not only
the rounded AP/AR summary.
Fast. On COCO val2017, 25–91× lower wall-clock time than pycocotools,
10–15× lower than faster-coco-eval and 2.5–4.4× lower than hotcoco.
Memory-efficient.59–86% lower peak RSS than pycocotools, and the
lowest of all four evaluators on every task.
Drop-in. The same COCO / COCOeval API for bbox, segm and
keypoints, plus the LVIS federated protocol. Prebuilt wheels for Linux,
macOS and Windows (CPython 3.8–3.15).
Per-batch precision, recall and F1. Scored from the same matching as
AP, per batch or accumulated over a validation loop: 0.26 ms for a
16-image batch and 0.09 s for all of COCO val2017, 52–780× and 27–580×
less time than hotcoco, Ultralytics, TorchMetrics, supervision,
faster-coco-eval and pycocotools
().
Status: alpha. Before replacing the reference evaluator in your pipeline,
check parity on your own evaluation parameters and COCOeval subclasses.
Installation
pip install ultrafast-pycocotools
Prebuilt wheels need no Rust compiler; NumPy is installed as a dependency.
Other platforms build from source with a stable Rust toolchain:
pip install git+https://github.com/developer0hye/ultrafast-pycocotools.
For LVIS, pass lvis_style=True to apply the federated annotation protocol
(negative and not-exhaustive category lists, 300 detections per image) and the
official metric names (AP, APr, APc, APf, AR@300, ...). See the
LVIS guide.
Use inside an existing framework
If a training or validation framework imports pycocotools internally,
register the replacement before importing that framework:
from ultrafast_pycocotools import init_as_pycocotools
init_as_pycocotools() # `import pycocotools` now resolves to ultrafast
This patches sys.modules for the whole Python process.
Benchmarks
In paired release-mode source builds for 0.1.19 and 0.1.18 on full COCO
val2017 (5,000 images, cached YOLO26n/YOLO26n-seg/YOLO26n-pose detections,
median of six fresh processes per build in PR #42 CI), two-thread wall time
fell by 28.5% on bbox (0.504 → 0.360 s), 24.9% on segm
(1.599 → 1.200 s), 11.0% on keypoints (0.482 → 0.430 s) and 25.6% on
boundary (0.104 → 0.077 s) on GitHub ubuntu-latest (x86-64), and by
25.4% on bbox, 26.0% on segm and 8.9% on keypoints on macos-latest
(Apple Silicon), with identical output digests on every task. Best-F1
750,000-detection sweeps (80 cat × 10 IoU, 2 threads) sped up 2.43× macro
/ 2.80× micro on ubuntu-latest and 2.19× macro / 3.61× micro on
macos-latest.
Paired measurements, workloads and limitations
COCO val2017 (all 5,000 images) with cached YOLO26n, YOLO26n-seg and
YOLO26n-pose detections: 733,070 boxes, 724,953 instance masks and 134,663
pose instances. Model inference is excluded; the timed region covers JSON
parsing, ground-truth indexing, matching, accumulation and summarization.
RF-DETR: optional ufcoco
backend for bbox and mask mAP during training and validation
(TrainConfig.eval_backend="ufcoco"), merged in
roboflow/rf-detr#1449 and
released in RF-DETR 1.11.0.
Benchmark
SAHI: optional backend for
sahi coco evaluate (pip install "sahi[ultrafast]", then
--backend ultrafast), merged in
obss/sahi#1452 and released in
SAHI 0.12.7.
The public COCO, COCOeval and mask (RLE) APIs are tested against
pycocotools, covering annotation query order, loadRes, compressed and
uncompressed RLE, custom Params (IoU thresholds, area ranges, maxDets),
iscrowd handling, score ties and subclass overrides. A few internals
intentionally differ:
Per-image matching records (evalImgs) are not stored by default; pass
store_eval_imgs=True if your code reads them.
Box-only detections omit the polygon segmentation derived from each box;
pass derive_segmentation=True if you read that field.
COCO(annotation_dict) borrows the dictionary instead of deep-copying it, and
ground-truth annotations are not rewritten in place.
Bit-exactness claims cover the tested inputs and configurations. See the
compatibility notes.
Extras
The same matching results provide per-category AP, precision–recall curves,
per-detection TP/FP matches, a confusion matrix, and precision, recall and F1
at a score threshold or at the best-F1 cut. Boundary IoU is available
as an additional evaluation mode (an extension, not a pycocotools metric):
For training and validation loops, DetectionMetrics scores batches of
per-image arrays or tensors (or COCO data) with the same matching as AP:
from ultrafast_pycocotools.metrics import DetectionMetrics
metric = DetectionMetrics(iou_thrs=0.5, score_thr="best_f1") # box_format="xyxy"
for images, targets in loader:
batch = metric.update(model(images), targets) # [{"boxes", "scores", "labels"}, ...]
total = metric.compute() # per_category / micro / macro, and the chosen score_thr
update() returns the batch's metrics; compute() returns them over every
batch, bit-identical to one evaluation over all images. Only a category id,
a score and one byte per IoU threshold are kept per detection, never boxes
or masks (batch_results=False skips the per-batch summary).
Precision, recall and F1 per category, micro and macro, at any IoU
thresholds, with every detection, a score threshold, or the score cut that
maximizes F1 (score_thr="best_f1"). Zero denominators give 0.
Boxes (xyxy or xywh) or masks, class-aware or class-agnostic. COCO's
crowd regions are honored, and its area ranges and maxDets are optional.
NumPy arrays, lists and PyTorch tensors; CPU tensors are read in place.
COCOeval.detection_metrics() computes the same metrics after
evaluate(), with that evaluation's parameters.
COCO val2017 with YOLO26n predictions, one thread, on an Intel Core i5-10400;
each library computes its own metric (P/R/F1, F1 or AP), so the times are
the cost of getting each library's numbers per batch:
git clone https://github.com/developer0hye/ultrafast-pycocotools.git
cd ultrafast-pycocotools
pip install -e ".[test,lvis-test]"
python bench/fetch_lvis_fixture.py
python -m pytest -q
cargo test -p ufcoco-core
python bench/reproduce.py quick --out bench/out/quick --verify-published quick
runs an end-to-end parity check on synthetic data, with no dataset download
or model weights. See the
reproduction guide,
CI and
release process.
Documentation, comments and examples are written in English.
License and credits
BSD-2-Clause.
The evaluation protocol and API follow
pycocotools by Piotr Dollár and
Tsung-Yi Lin (BSD-2-Clause). LVIS verification uses the official
LVIS API. Design decisions are in
DESIGN.md.