open-mmlab/mmdetection · error · TypeError

Invalid type for palette: {type(palette)}

Error message

Invalid type for palette: {type(palette)}

What it means

get_palette raises TypeError when the palette argument is neither a known dataset palette name ('coco','voc','random',...), nor any other string (treated as an mmcv color val), nor a sequence of colors.

Source

Thrown at mmdet/visualization/palette.py:63

        np.random.seed(42)
        palette = np.random.randint(0, 256, size=(num_classes, 3))
        np.random.set_state(state)
        dataset_palette = [tuple(c) for c in palette]
    elif palette == 'coco':
        from mmdet.datasets import CocoDataset, CocoPanopticDataset
        dataset_palette = CocoDataset.METAINFO['palette']
        if len(dataset_palette) < num_classes:
            dataset_palette = CocoPanopticDataset.METAINFO['palette']
    elif palette == 'citys':
        from mmdet.datasets import CityscapesDataset
        dataset_palette = CityscapesDataset.METAINFO['palette']
    elif palette == 'voc':
        from mmdet.datasets import VOCDataset
        dataset_palette = VOCDataset.METAINFO['palette']
    elif is_str(palette):
        dataset_palette = [mmcv.color_val(palette)[::-1]] * num_classes
    else:
        raise TypeError(f'Invalid type for palette: {type(palette)}')

    assert len(dataset_palette) >= num_classes, \
        'The length of palette should not be less than `num_classes`.'
    return dataset_palette


def _get_adaptive_scales(areas: np.ndarray,
                         min_area: int = 800,
                         max_area: int = 30000) -> np.ndarray:
    """Get adaptive scales according to areas.

    The scale range is [0.5, 1.0]. When the area is less than
    ``min_area``, the scale is 0.5 while the area is larger than
    ``max_area``, the scale is 1.0.

    Args:
        areas (ndarray): The areas of bboxes or masks with the
            shape of (n, ).

View on GitHub (pinned to cfd5d3a985)

Solutions

  1. Pass a recognized palette name string (e.g. 'coco', 'voc', 'random') or a list of RGB tuples
  2. Fix the dataset METAINFO['palette'] / inferencer palette argument to a valid type
  3. Check for typos if you intended a named palette — unknown strings fall through to mmcv.color_val which may also fail

Example fix

// before
get_palette(3, 80)  # TypeError
// after
get_palette('random', 80)
Defensive patterns

Strategy: type-guard

Validate before calling

from mmengine.utils import is_str
import numpy as np
def valid_palette(p):
    return is_str(p) or (isinstance(p, (list, tuple)) and len(p) > 0)
assert valid_palette(palette)

Type guard

def is_valid_palette(p) -> bool:
    from mmengine.utils import is_str
    return is_str(p) or isinstance(p, (list, tuple))

Try / catch

try:
    get_palette(palette, n)
except TypeError as e:
    if 'Invalid type for palette' in str(e):
        palette = 'random'
        get_palette(palette, n)

Prevention

When it happens

Trigger: Calling mmdet.visualization.palette.get_palette(palette, num_classes) with e.g. an int, dict, or None as palette; reached from visualizer drawing (_draw_instances/_draw_panoptic_seg) when dataset_meta['palette'] holds an invalid value.

Common situations: Setting palette in a config or DetInferencer(palette=...) to a non-string/non-list value; custom datasets whose METAINFO palette entry is malformed.

Understand the failure class

Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.

Related errors


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