open-mmlab/mmdetection · error · ValueError

new classes {new_classes} is not a subset of classes {old_cl

Error message

new classes {new_classes} is not a subset of classes {old_classes} in METAINFO.

What it means

BaseSemanticSegmentationDataset.get_label_map builds a mapping when the user-supplied metainfo['classes'] differs from the dataset class's default METAINFO['classes']. The new classes must be a subset of the original classes; otherwise (renamed or entirely new labels) it raises this ValueError. The mapping only supports dropping/reordering existing classes and mapping dropped ones to background (0).

Source

Thrown at mmdet/datasets/base_semseg_dataset.py:169

        is not equal to new classes in self._metainfo and nether of them is not
        None, `label_map` is not None.

        Args:
            new_classes (list, tuple, optional): The new classes name from
                metainfo. Default to None.


        Returns:
            dict, optional: The mapping from old classes in cls.METAINFO to
                new classes in self._metainfo
        """
        old_classes = cls.METAINFO.get('classes', None)
        if (new_classes is not None and old_classes is not None
                and list(new_classes) != list(old_classes)):

            label_map = {}
            if not set(new_classes).issubset(cls.METAINFO['classes']):
                raise ValueError(
                    f'new classes {new_classes} is not a '
                    f'subset of classes {old_classes} in METAINFO.')
            for i, c in enumerate(old_classes):
                if c not in new_classes:
                    # 0 is background
                    label_map[i] = 0
                else:
                    label_map[i] = new_classes.index(c)
            return label_map
        else:
            return None

    def _update_palette(self) -> list:
        """Update palette after loading metainfo.

        If length of palette is equal to classes, just return the palette.
        If palette is not defined, it will randomly generate a palette.
        If classes is updated by customer, it will return the subset of

View on GitHub (pinned to cfd5d3a985)

Solutions

  1. Use exactly the original class names (copy spelling/case from the dataset class METAINFO) and only drop entries to define your subset
  2. If you need genuinely new classes, subclass the dataset and override METAINFO = dict(classes=(...)) instead of passing metainfo at construction
  3. Check for typos/case differences between your classes list and METAINFO['classes'] programmatically before constructing

Example fix

# before
ds = CityscapesDataset(
  data_root='data/', data_prefix=dict(img_path='leftImg8bit', seg_map_path='gtFine'),
  metainfo=dict(classes=['road', 'sidewalk', 'vehicle']))  # 'vehicle' not in METAINFO
# after
from mmdet.datasets import CityscapesDataset
# option A: subset with exact names
ds = CityscapesDataset(..., metainfo=dict(classes=['road', 'sidewalk', 'car']))
# option B: new taxonomy -> subclass
class MyDataset(CityscapesDataset):
  METAINFO = dict(classes=('road', 'sidewalk', 'vehicle'))
ds = MyDataset(...)
Defensive patterns

Strategy: validation

Validate before calling

import inspect
ds_cls = CocoDataset  # whatever class you use
old = list(ds_cls.METAINFO['classes'])
new = my_classes
unknown = set(map(str, new)) - set(map(str, old))
assert not unknown, f'classes not in METAINFO (typo/subset violation): {sorted(unknown)}'

Type guard

def is_valid_class_subset(cls, new_classes) -> bool:
    old = list(cls.METAINFO.get('classes') or [])
    return set(map(str, new_classes)).issubset(set(map(str, old)))

Try / catch

try:
    ds = DatasetClass(..., metainfo=dict(classes=my_classes))
except ValueError as e:
    if 'not a subset of classes' in str(e):
        raise SystemExit(f'Fix class names {my_classes} to match METAINFO {cls.METAINFO["classes"]} or subclass with new METAINFO')
    raise

Prevention

When it happens

Trigger: Passing metainfo=dict(classes=[...]) to a semseg dataset where the list contains class names absent from the class's METAINFO['classes'], e.g. custom names for a model fine-tuned on a different label taxonomy, or reordered-with-renames.

Common situations: Fine-tuning a semseg model on a subset of ADE20K/Cityscapes but changing label spellings; loading a custom dataset by extending a built-in class; case mismatches ('Road' vs 'road').

Related errors


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