open-mmlab/mmdetection · error · TypeError

Unsupported {type(mask)} data type

Error message

Unsupported {type(mask)} data type

What it means

mask2ndarray converts mask representations (BitmapMasks, PolygonMasks, Tensor, ndarray) to a numpy array. Any other Python type hits the TypeError branch — the function does not accept lists of masks, RLE strings, or other objects.

Source

Thrown at mmdet/models/utils/misc.py:250

    return ret


def mask2ndarray(mask):
    """Convert Mask to ndarray..

    Args:
        mask (:obj:`BitmapMasks` or :obj:`PolygonMasks` or
        torch.Tensor or np.ndarray): The mask to be converted.

    Returns:
        np.ndarray: Ndarray mask of shape (n, h, w) that has been converted
    """
    if isinstance(mask, (BitmapMasks, PolygonMasks)):
        mask = mask.to_ndarray()
    elif isinstance(mask, torch.Tensor):
        mask = mask.detach().cpu().numpy()
    elif not isinstance(mask, np.ndarray):
        raise TypeError(f'Unsupported {type(mask)} data type')
    return mask


def flip_tensor(src_tensor, flip_direction):
    """flip tensor base on flip_direction.

    Args:
        src_tensor (Tensor): input feature map, shape (B, C, H, W).
        flip_direction (str): The flipping direction. Options are
          'horizontal', 'vertical', 'diagonal'.

    Returns:
        out_tensor (Tensor): Flipped tensor.
    """
    assert src_tensor.ndim == 4
    valid_directions = ['horizontal', 'vertical', 'diagonal']
    assert flip_direction in valid_directions
    if flip_direction == 'horizontal':

View on GitHub (pinned to cfd5d3a985)

Solutions

  1. Wrap raw masks: np.stack(list_of_arrays) or pass a single ndarray of shape (N, H, W)
  2. Convert RLE with pycocotools.mask.decode first
  3. Wrap instance masks as BitmapMasks(masks, h, w) or PolygonMasks(...) for structured pipelines

Example fix

# before
mask2ndarray([arr1, arr2])  # list -> TypeError
# after
mask2ndarray(np.stack([arr1, arr2]))
Defensive patterns

Strategy: type-guard

Validate before calling

import numpy as np, torch
from mmdet.structures import BitmapMasks
assert isinstance(mask, (np.ndarray, torch.Tensor, list, tuple)) or hasattr(mask, 'to_ndarray')

Type guard

def is_mask2ndarray_input(m) -> bool:
    return (isinstance(m, (np.ndarray, torch.Tensor))
            or isinstance(m, (list, tuple))
            or hasattr(m, 'to_ndarray'))

Try / catch

try:
    arr = mask2ndarray(mask)
except TypeError:
    arr = mask2ndarray(BitmapMasks([np.asarray(m) for m in mask], h, w))

Prevention

When it happens

Trigger: Passing e.g. a list of per-instance numpy arrays, an RLE dict/string, a PIL Image, or None as the mask argument to mask2ndarray.

Common situations: Feeding custom dataset outputs directly into loss/metric code that expects a mmdet mask structure; converting from pycocotools RLE without first decoding to ndarray.

Related errors


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