open-mmlab/mmdetection · error · KeyError

metric should be one of 'bbox', 'segm', 'proposal', 'proposa

Error message

metric should be one of 'bbox', 'segm', 'proposal', 'proposal_fast', but got {metric}.

What it means

LVISMetric.__init__ validates the `metric` argument against ['bbox', 'segm', 'proposal', 'proposal_fast'] and raises KeyError for anything else. This mirrors CocoMetric's contract but without 'proposal_fast' extras beyond the listed set; a typo or unsupported metric name fails fast at construction.

Source

Thrown at mmdet/evaluation/metrics/lvis_metric.py:104

                 iou_thrs: Optional[Union[float, Sequence[float]]] = None,
                 metric_items: Optional[Sequence[str]] = None,
                 format_only: bool = False,
                 outfile_prefix: Optional[str] = None,
                 collect_device: str = 'cpu',
                 prefix: Optional[str] = None,
                 file_client_args: dict = None,
                 backend_args: dict = None) -> None:
        if lvis is None:
            raise RuntimeError(
                'Package lvis is not installed. Please run "pip install '
                'git+https://github.com/lvis-dataset/lvis-api.git".')
        super().__init__(collect_device=collect_device, prefix=prefix)
        # coco evaluation metrics
        self.metrics = metric if isinstance(metric, list) else [metric]
        allowed_metrics = ['bbox', 'segm', 'proposal', 'proposal_fast']
        for metric in self.metrics:
            if metric not in allowed_metrics:
                raise KeyError(
                    "metric should be one of 'bbox', 'segm', 'proposal', "
                    f"'proposal_fast', but got {metric}.")

        # do class wise evaluation, default False
        self.classwise = classwise

        # proposal_nums used to compute recall or precision.
        self.proposal_nums = list(proposal_nums)

        # iou_thrs used to compute recall or precision.
        if iou_thrs is None:
            iou_thrs = np.linspace(
                .5, 0.95, int(np.round((0.95 - .5) / .05)) + 1, endpoint=True)
        self.iou_thrs = iou_thrs
        self.metric_items = metric_items
        self.format_only = format_only
        if self.format_only:
            assert outfile_prefix is not None, 'outfile_prefix must be not'

View on GitHub (pinned to cfd5d3a985)

Solutions

  1. Set metric to a valid value or list of valid values, e.g. metric=['bbox', 'segm']
  2. Use a Python list for multiple metrics instead of a comma-joined string
  3. Check for typos and case: values must be lowercase and exactly match the allowed set

Example fix

// before
val_evaluator=dict(type='LVISMetric', metric='bbox,segm')
// after
val_evaluator=dict(type='LVISMetric', metric=['bbox', 'segm'])
Defensive patterns

Strategy: type-guard

Validate before calling

ALLOWED = {'bbox', 'segm', 'proposal', 'proposal_fast'}
metrics = metric if isinstance(metric, list) else [metric]
bad = [m for m in metrics if m not in ALLOWED]
assert not bad, f'Invalid LVIS metrics: {bad}; allowed: {sorted(ALLOWED)}'

Type guard

def is_valid_lvis_metric(metric) -> bool:
    ALLOWED = {'bbox', 'segm', 'proposal', 'proposal_fast'}
    vals = metric if isinstance(metric, (list, tuple)) else [metric]
    return all(isinstance(m, str) and m in ALLOWED for m in vals)

Try / catch

try:
    evaluator = LVISMetric(metric=metric)
except KeyError as e:
    raise ValueError(f'Invalid metric config {metric}: {e}') from e

Prevention

When it happens

Trigger: Passing val_evaluator=dict(type='LVISMetric', metric='bbox_segm') or metric=['box'] or any string/list element not in the allowed list when building the evaluator.

Common situations: Copy-pasting a CocoMetric config with a metric key not valid for LVIS (e.g. 'proposal_fast' variants or 'segm' on a detection-only model); typos like 'boxes' or 'BBox'; assuming comma-separated 'bbox,segm' string works instead of a list.

Related errors


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