facebookresearch/detectron2 · error · ValueError
One annotation of image {image_id} contains empty 'bbox' val
Error message
One annotation of image {image_id} contains empty 'bbox' value! This json does not have valid COCO format. What it means
load_coco_json iterates annotations and rejects any whose 'bbox' list is empty (len 0), because an empty box is meaningless for training and indicates a malformed COCO json.
Source
Thrown at detectron2/data/datasets/coco.py:180
record["width"] = img_dict["width"]
image_id = record["image_id"] = img_dict["id"]
objs = []
for anno in anno_dict_list:
# Check that the image_id in this annotation is the same as
# the image_id we're looking at.
# This fails only when the data parsing logic or the annotation file is buggy.
# The original COCO valminusminival2014 & minival2014 annotation files
# actually contains bugs that, together with certain ways of using COCO API,
# can trigger this assertion.
assert anno["image_id"] == image_id
assert anno.get("ignore", 0) == 0, '"ignore" in COCO json file is not supported.'
obj = {key: anno[key] for key in ann_keys if key in anno}
if "bbox" in obj and len(obj["bbox"]) == 0:
raise ValueError(
f"One annotation of image {image_id} contains empty 'bbox' value! "
"This json does not have valid COCO format."
)
segm = anno.get("segmentation", None)
if segm: # either list[list[float]] or dict(RLE)
if isinstance(segm, dict):
if isinstance(segm["counts"], list):
# convert to compressed RLE
segm = mask_util.frPyObjects(segm, *segm["size"])
else:
# filter out invalid polygons (< 3 points)
segm = [poly for poly in segm if len(poly) % 2 == 0 and len(poly) >= 6]
if len(segm) == 0:
num_instances_without_valid_segmentation += 1
continue # ignore this instance
obj["segmentation"] = segm
View on GitHub (pinned to a2f4a8771a)
Solutions
- Clean the json: drop annotations with empty or invalid bbox before registering
- Fix the converter to skip objects without a valid 4-element bbox
- Or patch upstream to tolerate and skip, though cleaning the data is preferred
Example fix
# before
for ann in coco_json["annotations"]:
pass # empty bboxes present
# after
import json
d = json.load(open('instances.json'))
d["annotations"] = [a for a in d["annotations"] if a.get("bbox") and len(a["bbox"]) == 4]
json.dump(d, open('instances_clean.json', 'w')) Defensive patterns
Strategy: validation
Validate before calling
import json
with open('instances.json') as f:
d = json.load(f)
bad = [a for a in d['annotations'] if not a.get('bbox')]
assert not bad, f'{len(bad)} annotations have empty bbox' Type guard
def has_valid_bbox(ann) -> bool:
b = ann.get('bbox')
return isinstance(b, (list, tuple)) and len(b) == 4 and all(isinstance(v, (int, float)) for v in b) Try / catch
try:
d = load_coco_json(json_file, img_root, 'mydata')
except ValueError as e:
raise SystemExit(f'malformed COCO json: {e}') from e Prevention
- Run a COCO-format sanity check (pycocotools.COCO load) before registering
- Drop invalid annotations in your conversion pipeline
- Add CI validation of dataset jsons
When it happens
Trigger: Loading a COCO-format json (register_coco_instances / load_coco_json) where some annotation has "bbox": [] — often produced by converters that emit empty bboxes for degenerate objects instead of omitting the annotation.
Common situations: Third-party conversion scripts (VOC/CVAT/labelme -> COCO) writing empty bboxes; datasets with fully-cropped-out or zero-size objects; hand-edited jsons.
Related errors
- Encountered category_id={annotation_category_id} but this id
- Cannot match one checkpoint key to multiple keys in the mode
- Class with @configurable must have a 'from_config' classmeth
- {name} must take 'cfg' as the first argument!
- target of LazyCall must be a callable or defines a callable!
AI-assisted analysis of facebookresearch/detectron2@a2f4a8771a (2026-08-27).
Data as JSON: /api/errors/e3c0a4378f6ff42d.
Report an issue: GitHub.