facebookresearch/detectron2 · error · AttributeError

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

Error message

Attribute '{key}' does not exist in the metadata of dataset '{self.name}': metadata is empty.

What it means

Same missing-attribute path as error 18, but the else branch indicates the metadata object holds only 'name' — i.e. the dataset was registered with no metadata at all besides its name, so any other attribute access fails with 'metadata is empty'.

Source

Thrown at detectron2/data/catalog.py:131

    }

    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)

        # Ensure that metadata of the same name stays consistent
        try:
            oldval = getattr(self, key)
            assert oldval == val, (
                "Attribute '{}' in the metadata of '{}' cannot be set "

View on GitHub (pinned to a2f4a8771a)

Solutions

  1. Populate metadata at registration time: MetadataCatalog.get(name).set(thing_classes=[...], **kwargs)
  2. Provide defaults in your code when metadata is empty (e.g. fall back to dummy_classes)
  3. Follow register_instances-style helpers that set both catalog and metadata together

Example fix

# before
DatasetCatalog.register("my_train", lambda: load_dicts(...))
meta = MetadataCatalog.get("my_train").thing_classes  # metadata is empty
# after
from detectron2.data import register_instances
register_instances("my_train", lambda: load_dicts(...), {"thing_classes": ["cat", "dog"]})
Defensive patterns

Strategy: type-guard

Validate before calling

from detectron2.data import MetadataCatalog
meta = MetadataCatalog.get(name)
required = ['thing_classes']
missing = [k for k in required if getattr(meta, k, None) is None]
assert not missing, f"metadata empty/missing for {name}: {missing}"

Type guard

def metadata_populated(name: str, keys) -> bool:
    meta = MetadataCatalog.get(name)
    return all(getattr(meta, k, None) is not None for k in keys)

Try / catch

try:
    classes = meta.thing_classes
except AttributeError as e:
    if "metadata is empty" in str(e):
        raise SystemExit(f"register metadata for {name} before training")
    raise

Prevention

When it happens

Trigger: Registering a dataset with only a loader: DatasetCatalog.register('x', loader) without any MetadataCatalog.get('x').set(...), then accessing MetadataCatalog.get('x').thing_classes.

Common situations: Minimal custom dataset registration that skips MetadataCatalog entirely; test/dummy datasets; following a tutorial that omitted metadata setup.

Related errors


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