open-mmlab/mmdetection · error · KeyError

In the image with ID {} segment with ID {} is presented in P

Error message

In the image with ID {} segment with ID {} is presented in PNG and not presented in JSON.

What it means

During PQ computation, every segment id present in the prediction PNG must have a matching entry in the JSON segments_info. A label found in the PNG (other than VOID=0) but absent from JSON raises KeyError, mirroring the official panopticapi consistency checks.

Source

Thrown at mmdet/evaluation/functional/panoptic_utils.py:91

        # The predictions can only be on the local dist now.
        pan_pred = mmcv.imread(
            os.path.join(pred_folder, pred_ann['file_name']),
            flag='color',
            channel_order='rgb')
        pan_pred = rgb2id(pan_pred)

        gt_segms = {el['id']: el for el in gt_ann['segments_info']}
        pred_segms = {el['id']: el for el in pred_ann['segments_info']}

        # predicted segments area calculation + prediction sanity checks
        pred_labels_set = set(el['id'] for el in pred_ann['segments_info'])
        labels, labels_cnt = np.unique(pan_pred, return_counts=True)
        for label, label_cnt in zip(labels, labels_cnt):
            if label not in pred_segms:
                if label == VOID:
                    continue
                raise KeyError(
                    'In the image with ID {} segment with ID {} is '
                    'presented in PNG and not presented in JSON.'.format(
                        gt_ann['image_id'], label))
            pred_segms[label]['area'] = label_cnt
            pred_labels_set.remove(label)
            if pred_segms[label]['category_id'] not in categories:
                raise KeyError(
                    'In the image with ID {} segment with ID {} has '
                    'unknown category_id {}.'.format(
                        gt_ann['image_id'], label,
                        pred_segms[label]['category_id']))
        if len(pred_labels_set) != 0:
            raise KeyError(
                'In the image with ID {} the following segment IDs {} '
                'are presented in JSON and not presented in PNG.'.format(
                    gt_ann['image_id'], list(pred_labels_set)))

        # confusion matrix calculation

View on GitHub (pinned to cfd5d3a985)

Solutions

  1. Ensure the PNG writer assigns one unique id per segment and emits exactly one segments_info entry per id
  2. Use the official panopticapi id allocation (png_utils.id2rgb / color encoding) for generating predictions
  3. Treat ignore/void pixels as 0 (VOID) so they are skipped
  4. Re-generate results rather than hand-editing JSON to match

Example fix

# before
# PNG has ids {1,2,3}; segments_info lists only {1,2}
# after
segments_info = [{'id': i, 'category_id': c, 'iscrowd': 0, 'area': a} for i, c, a in segments]  # one entry per PNG id
Defensive patterns

Strategy: validation

Validate before calling

import numpy as np
def check_png_json_consistent(pan_png, segments_info, void=0):
    png_ids = set(np.unique(pan_png)) - {void}
    json_ids = {s['id'] for s in segments_info}
    assert png_ids == json_ids, f'PNG-only={png_ids-json_ids} JSON-only={json_ids-png_ids}'

Try / catch

try:
    pq_compute_single_core(...)
except KeyError as e:
    raise ValueError(f'Inconsistent panoptic results: {e}') from e

Prevention

When it happens

Trigger: Postprocessing code that writes instance ids into the PNG but omits/collapses corresponding segments_info entries; overlapping segments merged incorrectly; ids shifted between PNG and JSON.

Common situations: Custom panoptic result converters; re-using COCO instance ids instead of contiguous segment ids; VOID pixels encoded with nonzero values.

Related errors


AI-assisted analysis of open-mmlab/mmdetection@cfd5d3a985 (2026-08-27). Data as JSON: /api/errors/b321cb2f801c0ebb. Report an issue: GitHub.