facebookresearch/detectron2 · error · ValueError

Unsupported type {} for concatenation

Error message

Unsupported type {} for concatenation

What it means

Instances.cat concatenates fields across images: tensors via torch.cat, lists via chaining, and any type exposing a classmethod cat(). A field holding any other type (e.g. dict, str, numpy array) cannot be concatenated and raises ValueError.

Source

Thrown at detectron2/structures/instances.py:182

        if len(instance_lists) == 1:
            return instance_lists[0]

        image_size = instance_lists[0].image_size
        if not isinstance(image_size, torch.Tensor):  # could be a tensor in tracing
            for i in instance_lists[1:]:
                assert i.image_size == image_size
        ret = Instances(image_size)
        for k in instance_lists[0]._fields.keys():
            values = [i.get(k) for i in instance_lists]
            v0 = values[0]
            if isinstance(v0, torch.Tensor):
                values = torch.cat(values, dim=0)
            elif isinstance(v0, list):
                values = list(itertools.chain(*values))
            elif hasattr(type(v0), "cat"):
                values = type(v0).cat(values)
            else:
                raise ValueError("Unsupported type {} for concatenation".format(type(v0)))
            ret.set(k, values)
        return ret

    def __str__(self) -> str:
        s = self.__class__.__name__ + "("
        s += "num_instances={}, ".format(len(self))
        s += "image_height={}, ".format(self._image_size[0])
        s += "image_width={}, ".format(self._image_size[1])
        s += "fields=[{}])".format(", ".join((f"{k}: {v}" for k, v in self._fields.items())))
        return s

    __repr__ = __str__

View on GitHub (pinned to a2f4a8771a)

Solutions

  1. Store custom data in a torch.Tensor or a list (both supported)
  2. Give your custom class a classmethod cat(list_of_objs) so type(v).cat works
  3. Drop the unsupported field before calling cat: fields.pop('my_metadata')

Example fix

# before
instances.set('track_ids', np.array([1, 2]))
merged = Instances.cat([a, b])
# after
instances.set('track_ids', torch.as_tensor([1, 2]))
merged = Instances.cat([a, b])
Defensive patterns

Strategy: type-guard

Validate before calling

import torch
def field_is_concatenable(v) -> bool:
    return isinstance(v, torch.Tensor) or isinstance(v, list) or hasattr(type(v), 'cat')

Type guard

import torch
def can_cat_field(v) -> bool:
    return isinstance(v, (torch.Tensor, list)) or hasattr(type(v), 'cat')

Prevention

When it happens

Trigger: Instances.cat([inst_a, inst_b]) where a field holds an unsupported type such as a numpy array or a dict per instance.

Common situations: Custom fields added via instances.set('my_metadata', np.array(...)) or python dicts; concatenating per-GPU results in multi-GPU evaluation.

Related errors


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