open-mmlab/mmdetection · error · TypeError
pretrained must be a str or None
Error message
pretrained must be a str or None
What it means
SwinTransformer.__init__ raises TypeError when the deprecated `pretrained` argument is not a str or None. str is wrapped into init_cfg; None falls back to the init_cfg parameter. Other types are rejected.
Source
Thrown at mmdet/models/backbones/swin.py:570
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 specified 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')
super(SwinTransformer, self).__init__(init_cfg=init_cfg)
num_layers = len(depths)
self.out_indices = out_indices
self.use_abs_pos_embed = use_abs_pos_embed
assert strides[0] == patch_size, 'Use non-overlapping patch embed.'
self.patch_embed = PatchEmbed(
in_channels=in_channels,
embed_dims=embed_dims,
conv_type='Conv2d',
kernel_size=patch_size,
stride=strides[0],
norm_cfg=norm_cfg if patch_norm else None,
init_cfg=None)
View on GitHub (pinned to cfd5d3a985)
Solutions
- Use init_cfg=dict(type='Pretrained', checkpoint='swin_tiny.pth') and omit pretrained
- If using pretrained, pass a str URL/path with value None for init_cfg
Example fix
// before backbone=dict(type='SwinTransformer', pretrained='swin_tiny_patch4_window7_224.pth') // after backbone=dict(type='SwinTransformer', init_cfg=dict(type='Pretrained', checkpoint='swin_tiny_patch4_window7_224.pth'))
Defensive patterns
Strategy: validation
Validate before calling
assert pretrained is None or isinstance(pretrained, str)
Prevention
- Adopt init_cfg API
- Never pass state_dict objects as pretrained
When it happens
Trigger: SwinTransformer(pretrained=dict(...)) or pretrained=Path(...); providing both pretrained and a non-None init_cfg.
Common situations: Migrating old Swin configs (mmdet 2.x) that used pretrained='...'; passing a state_dict instead of a path string.
Related errors
- pretrained must be a str or None
- pretrained must be a str or None
- pretrained must be a str or None
- pretrained must be a str or None
- pretrained must be a str or None
AI-assisted analysis of open-mmlab/mmdetection@cfd5d3a985 (2026-08-27).
Data as JSON: /api/errors/e881adf81834fe4a.
Report an issue: GitHub.