facebookresearch/detectron2 · error · AttributeError

Attribute '{}' does not exist in the metadata of dataset '{}

Error message

Attribute '{}' does not exist in the metadata of dataset '{}'. Available keys are {}.

What it means

MetadataCatalog attributes are accessed via __getattr__; when the requested key isn't set (and isn't a renamed legacy key), this AttributeError is raised listing the dataset name and the currently available metadata keys. The 'len(self.__dict__) > 1' branch means metadata exists but is missing this key.

Source

Thrown at detectron2/data/catalog.py:126

    _RENAMED = {
        "class_names": "thing_classes",
        "dataset_id_to_contiguous_id": "thing_dataset_id_to_contiguous_id",
        "stuff_class_names": "stuff_classes",
    }

    def __getattr__(self, key):
        if key in self._RENAMED:
            log_first_n(
                logging.WARNING,
                "Metadata '{}' was renamed to '{}'!".format(key, self._RENAMED[key]),
                n=10,
            )
            return getattr(self, self._RENAMED[key])

        # "name" exists in every metadata
        if len(self.__dict__) > 1:
            raise AttributeError(
                "Attribute '{}' does not exist in the metadata of dataset '{}'. Available "
                "keys are {}.".format(key, self.name, str(self.__dict__.keys()))
            )
        else:
            raise AttributeError(
                f"Attribute '{key}' does not exist in the metadata of dataset '{self.name}': "
                "metadata is empty."
            )

    def __setattr__(self, key, val):
        if key in self._RENAMED:
            log_first_n(
                logging.WARNING,
                "Metadata '{}' was renamed to '{}'!".format(key, self._RENAMED[key]),
                n=10,
            )
            setattr(self, self._RENAMED[key], val)

View on GitHub (pinned to a2f4a8771a)

Solutions

  1. Register the missing key: MetadataCatalog.get(name).thing_colors = [...] or set it in register call kwargs
  2. Check the printed available keys and adapt your code to not require the missing one
  3. For standard datasets, ensure you used builtin registration (import detectron2.data.datasets.builtin)

Example fix

# before
meta = MetadataCatalog.get("my_train")
colors = meta.thing_colors  # AttributeError
# after
MetadataCatalog.get("my_train").thing_colors = [(255,0,0),(0,255,0)]
colors = MetadataCatalog.get("my_train").thing_colors
Defensive patterns

Strategy: type-guard

Validate before calling

from detectron2.data import MetadataCatalog
meta = MetadataCatalog.get(name)
assert hasattr(meta, 'thing_colors'), f"missing thing_colors for {name}"

Type guard

def metadata_has(meta, key: str) -> bool:
    return getattr(meta, key, None) is not None

Try / catch

try:
    colors = meta.thing_colors
except AttributeError:
    colors = [(i * 37 % 255, i * 91 % 255, i * 53 % 255) for i in range(len(meta.thing_classes))]

Prevention

When it happens

Trigger: Accessing MetadataCatalog.get('my_dataset').thing_colors when only 'thing_classes' was set; reading metadata attributes a builtin dataset version doesn't populate.

Common situations: Custom datasets registered without setting all metadata downstream code reads (e.g. evaluator needs thing_classes/thing_colors); code assuming COCO-style metadata exists for any dataset; version changes adding new required metadata.

Related errors


AI-assisted analysis of facebookresearch/detectron2@a2f4a8771a (2026-08-27). Data as JSON: /api/errors/9ca5d643c8c3ffdd. Report an issue: GitHub.