open-mmlab/mmdetection · error · TypeError

pretrained must be a str or None

Error message

pretrained must be a str or None

What it means

PyramidVisionTransformer (PVT) __init__ raises TypeError when the deprecated `pretrained` argument is not a str or None. A str is wrapped into init_cfg=dict(type='Pretrained', checkpoint=...); None defers to init_cfg. Anything else is invalid.

Source

Thrown at mmdet/models/backbones/pvt.py:457

        if isinstance(pretrain_img_size, int):
            pretrain_img_size = to_2tuple(pretrain_img_size)
        elif isinstance(pretrain_img_size, tuple):
            if len(pretrain_img_size) == 1:
                pretrain_img_size = to_2tuple(pretrain_img_size[0])
            assert len(pretrain_img_size) == 2, \
                f'The size of image should have length 1 or 2, ' \
                f'but got {len(pretrain_img_size)}'

        assert not (init_cfg and pretrained), \
            'init_cfg and pretrained cannot be setting at the same time'
        if isinstance(pretrained, str):
            warnings.warn('DeprecationWarning: pretrained is deprecated, '
                          'please use "init_cfg" instead')
            self.init_cfg = dict(type='Pretrained', checkpoint=pretrained)
        elif pretrained is None:
            self.init_cfg = init_cfg
        else:
            raise TypeError('pretrained must be a str or None')

        self.embed_dims = embed_dims

        self.num_stages = num_stages
        self.num_layers = num_layers
        self.num_heads = num_heads
        self.patch_sizes = patch_sizes
        self.strides = strides
        self.sr_ratios = sr_ratios
        assert num_stages == len(num_layers) == len(num_heads) \
               == len(patch_sizes) == len(strides) == len(sr_ratios)

        self.out_indices = out_indices
        assert max(out_indices) < self.num_stages
        self.pretrained = pretrained

        # transformer encoder
        dpr = [

View on GitHub (pinned to cfd5d3a985)

Solutions

  1. Pass pretrained as a checkpoint path string, or preferably use init_cfg=dict(type='Pretrained', checkpoint='...')
  2. Do not pass both pretrained and init_cfg simultaneously

Example fix

// before
backbone=dict(type='PyramidVisionTransformer', pretrained=dict(type='Pretrained'))
// after
backbone=dict(type='PyramidVisionTransformer', init_cfg=dict(type='Pretrained', checkpoint='pvt_small.pth'))
Defensive patterns

Strategy: validation

Validate before calling

assert pretrained is None or isinstance(pretrained, str)

Prevention

When it happens

Trigger: PVT(pretrained=dict(...)) or pretrained=Path('pvt.pth'); combining pretrained with an explicit init_cfg.

Common situations: Migrating old configs to the init_cfg API; passing a config dict where a checkpoint path string was expected.

Related errors


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