simple-cocotools
A simple, modern alternative to pycocotools.
About
Why not just use Pycocotools?
- Code is more readable and hackable.
- Metrics are more transparent and understandable.
- Evaluation is fast.
- Only dependencies are
numpyandscipy. Nocythonextensions. - Code is more modern (type annotations, linting, etc).
Install
From PyPI
pip install simple-cocotools
From Repo
pip install "simple-cocotools @ git+ssh://git@github.com/fkodom/simple-cocotools.git"
For Contributors
# Clone this repository
gh repo clone fkodom/simple-cocotools
cd simple-cocotools
# Install all dev dependencies (tests etc.)
pip install -e .[all]
# Setup pre-commit hooks
pre-commit install
Usage
Expects target annotations to have the same format as model predictions. (The format used by all torchvision detection models.) You may already have code to convert annotations into this format, since it's required to train many detection models. If not, use 'AnnotationsToDetectionFormat' from this repo as an example for how to do that.
A minimal example:
from torchvision.detection.models import maskrcnn_resnet50_fpn
from simple_cocotools import CocoEvaluator
evaluator = CocoEvaluator()
model = maskrcnn_resnet50_fpn(pretrained=True).eval()
for images, targets in data_loader:
predictions = model(images)
evaluator.update(predictions, targets)
metrics = evaluator.summarize()
metrics will be a dictionary with format:
{
"box": {
"mAP": 0.40,
"mAR": 0.41,
"class_AP": {
"cat": 0.39,
"dog": 0.42,
...
},
"class_AR": {
# Same as 'class_AP' above.
}
}
"mask": {
# Same as 'box' above.
}
}
For a more complete example, see scripts/mask_rcnn_example.py.
Benchmarks
I benchmarked against several torchvision detection models, which have mAP scores reported on the PyTorch website.
Using a default score threshold of 0.5:
| Model | Backbone | box mAP (official) |
box mAP | box mAR | mask mAP (official) |
mask mAP | mask mAR |
|---|---|---|---|---|---|---|---|
| Mask R-CNN | ResNet50 | 37.9 | 36.9 | 43.2 | 34.6 | 34.1 | 40.0 |
| Faster R-CNN | ResNet50 | 37.0 | 36.3 | 42.0 | - | - | - |
| Faster R-CNN | MobileNetV3-Large | 32.8 | 39.9 | 35.0 | - | - | - |
Notice that the mAP for MobileNetV3-Large is artificially high, since it has a much lower mAR at that score threshold. After tuning the score threshold, so that mAP and mAR are more balanced:
| Model | Backbone | Threshold | box mAP | box mAR | mask mAP | mask mAR |
|---|---|---|---|---|---|---|
| Mask R-CNN | ResNet50 | 0.6 | 41.1 | 41.3 | 38.2 | 38.5 |
| Faster R-CNN | ResNet50 | 0.6 | 40.8 | 40.4 | - | - |
| Faster R-CNN | MobileNetV3-Large | 0.425 | 36.2 | 36.2 | - | - |
These scores are more reflective of model performance, in my opinion. Mask R-CNN slightly outperforms Faster R-CNN, and there is a noticeable (but not horrible) gap between ResNet50 and MobileNetV3 backbones. PyTorch docs don't mention what score thresholds were used for each model benchmark. ¯\(ツ)/¯
Ignoring the time spent getting predictions from the model, evaluation is very fast.
- Bbox: ~400 samples/second
- Bbox + mask: ~100 samples/second
- Using a Google Cloud
n1-standard-4VM (4 vCPUs, 16 GB RAM).
Note: Speeds are dependent on the number of detections per image, and therefore dependent on the model and score threshold.
Keypoints Usage
Keypoint mAP and mAR normally use pre-computed "sigmas" to determin the "correctness" of each keypoint prediction. Unfortunately, those sigmas are tailored specifically for human pose (as in the COCO dataset), and not applicable to other keypoint datasets.
NOTE: Sigmas are actually computed using the predictions of a specific model trained on COCO. To make this applicable to other datasets, you would need to train a model on that dataset, and then use the sigmas from that model. The logic is somewhat circular -- you need to train a model to get the sigmas, but you need the sigmas to compute mAP / mAR.
There's no way around this, unless a large body of pretrained models are already available for the dataset you're using. For most real-world problems, that is not the case. So, the open-source mAP / mAR keypoints metrics are not generally extensible to other datasets.
simple-cocotools does not use sigmas, and instead computes the average distance between each keypoint prediction and ground truth. This is a much simpler approach, and is more applicable to other datasets. It's roughly how the sigmas for COCO were originally computed. The downside is that it's not directly comparable to the official COCO keypoints mAP / mAR.
Some keypoints are more ambiguous than others. For example, "left hip" is much more ambiguous than "left eye" -- the exact location of "left eye" should be obvious, while "left hip" is hidden by the torso and clothing. The average distance for "left hip" will be much larger than "left eye", even if the predictions are correct. (This is how sigmas were used in the official COCO keypoints mAP / mAR.) For that reason, keypoint distances should be interpreted with some knowledge about the specific dataset at hand.
metrics will be a dictionary with format:
{
"box": {
"mAP": 0.40,
"mAR": 0.41,
"class_AP": {
"cat": 0.39,
"dog": 0.42,
...
},
"class_AR": {
# Same as 'class_AP' above.
}
}
"keypoints": {
"distance": 0.10,
"class_distance": {
"cat": {
"distance": 0.11,
"keypoint_distance": {
"left_eye": 0.12,
"right_eye": 0.13,
...
}
},
...
}
}
}
How It Works
TODO: Blog post on how simple-cocotools works.
- Match the predictions/labels together, maximizing the IoU between pairs with the same object class. SciPy's
linear_sum_assignmentmethod does most of the heavy lifting here. - For each IoU threshold, determine the number of "correct" predictions from the assignments above. Pairs with IoU < threshold are incorrect.
- For each image, count the number of total predictions, correct predictions, and ground truth labels for each object class and IoU threshold.
- Compute AP/AR for each class from the prediction counts above. Then compute mAP and mAR by averaging over all object classes.
Metadata
Release files for simple-cocotools 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| simple_cocotools-1.0.2.tar.gz | 19.5 kB | Without provenance | Details for simple_cocotools-1.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| simple_cocotools-1.0.2-py3-none-any.whl | Python 3 | none | any | Details for simple_cocotools-1.0.2-py3-none-any.whl |
Total release size: 35.6 kB
Release files / simple_cocotools-1.0.2.tar.gz
| Download URL | simple_cocotools-1.0.2.tar.gz |
|---|---|
| Size | 19.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a504d36be75c1ac0c5e479a897ffe8a87626106070a910cfb1007a7c7209da59
|
|
BLAKE2b-256 checksum How to use checksums |
5e5b061a89d0765dc96ad26dd0469f9bb0b3df6e899aed56af0e0f61209a436a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.8.4
|
Release files / simple_cocotools-1.0.2-py3-none-any.whl
| Download URL | simple_cocotools-1.0.2-py3-none-any.whl |
|---|---|
| Size | 16.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7b313ab8bd583fef2c1c3b71ce1f6f2164256d2e8ac696d6ac342fc01cd64430
|
|
BLAKE2b-256 checksum How to use checksums |
0b8a284c09973dcff383dd111af544423121b4078bfef00791203e590d6a5f71
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.8.4
|
Security for simple-cocotools 1.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.