Skip to content

Write annotation id and area in COCO output - #102

Open
lorinczszabolcs wants to merge 1 commit into
lightly-ai:mainfrom
lorinczszabolcs:write-coco-annotation-id-and-area
Open

lorinczszabolcs wants to merge 1 commit into
lightly-ai:mainfrom
lorinczszabolcs:write-coco-annotation-id-and-area

Conversation

@lorinczszabolcs

Copy link
Copy Markdown
Contributor

What has changed and why?

Closes #97.

  • COCO object detection and instance segmentation output write an annotation id, sequential from 1, and an area. pycocotools indexes annotations by id and reads area for every annotation that is not a crowd.
  • The area is the sum of the polygon areas, the number of foreground pixels of an RLE mask, or the box width × height. The new helper get_coco_area computes it from the COCO segmentation.
  • COCO object detection output also writes "iscrowd": 0. COCOeval reads iscrowd for every ground truth annotation, so without it the evaluation fails with KeyError: 'iscrowd'. RT-DETR output uses the same code and also gets the three fields.

Ids from a COCO input file are not kept, because the label model has no id field. The output numbers the annotations from 1.

How has it been tested?

  • New unit tests check the written id and area for polygons, RLE masks and boxes, and the area helper for multi-part polygons and RLE.
  • The area of the polygon annotations from the COCO dataset fixture is the same as in the source file.
  • The coco_to_coco integration tests ignore id and area in the output too, because the output numbers and computes them itself.
  • make all-checks passes with 198 passed and 1 skipped, on Python 3.8 and 3.15.
  • pycocotools loads and evaluates the written files without changes. An RLE mask from a non-COCO input gives segm AP 1.0, with the script from #94 minus the lines that add id and area. The COCO object detection fixture gives bbox AP 1.0, with the script below.
check_bbox_pycocotools.py
"""Export COCO boxes with labelformat and evaluate them with pycocotools as written."""

import json
import sys
from pathlib import Path

from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval

from labelformat.formats import COCOObjectDetectionInput, COCOObjectDetectionOutput

out = Path(sys.argv[2])
COCOObjectDetectionOutput(output_file=out).save(
    label_input=COCOObjectDetectionInput(input_file=Path(sys.argv[1]))
)
data = json.loads(out.read_text())
gt = COCO(str(out))
# The predictions are the ground truth boxes with score 1.0.
dt = gt.loadRes([dict(a, score=1.0) for a in data["annotations"]])
ev = COCOeval(gt, dt, iouType="bbox")
ev.evaluate(); ev.accumulate(); ev.summarize()
print("AP:", ev.stats[0])

Run it with uv run --with pycocotools python check_bbox_pycocotools.py tests/fixtures/object_detection/COCO/train.json out.json.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

COCO outputs omit annotation id and area, so pycocotools can't load them

1 participant