open-mmlab/mmdetection · error · TypeError
pretrained must be a str or None
Error message
pretrained must be a str or None
What it means
ResNet.__init__ raises TypeError when the deprecated `pretrained` is neither a str nor None. A str becomes init_cfg=dict(type='Pretrained', ...); None means use init_cfg; other types are rejected, and passing both init_cfg and pretrained trips an assert.
Source
Thrown at mmdet/models/backbones/resnet.py:426
dict(
type='Constant',
val=1,
layer=['_BatchNorm', 'GroupNorm'])
]
block = self.arch_settings[depth][0]
if self.zero_init_residual:
if block is BasicBlock:
block_init_cfg = dict(
type='Constant',
val=0,
override=dict(name='norm2'))
elif block is Bottleneck:
block_init_cfg = dict(
type='Constant',
val=0,
override=dict(name='norm3'))
else:
raise TypeError('pretrained must be a str or None')
self.depth = depth
if stem_channels is None:
stem_channels = base_channels
self.stem_channels = stem_channels
self.base_channels = base_channels
self.num_stages = num_stages
assert num_stages >= 1 and num_stages <= 4
self.strides = strides
self.dilations = dilations
assert len(strides) == len(dilations) == num_stages
self.out_indices = out_indices
assert max(out_indices) < num_stages
self.style = style
self.deep_stem = deep_stem
self.avg_down = avg_down
self.frozen_stages = frozen_stages
self.conv_cfg = conv_cfgView on GitHub (pinned to cfd5d3a985)
Solutions
- Use init_cfg=dict(type='Pretrained', checkpoint='...') and remove pretrained
- If keeping pretrained, pass only a str path and remove init_cfg
- Pass a Path to a checkpoint file, not a loaded state_dict
Example fix
// before backbone=dict(type='ResNet', depth=50, pretrained='torchvision://resnet50', init_cfg=None) // after backbone=dict(type='ResNet', depth=50, init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet50'))
Defensive patterns
Strategy: validation
Validate before calling
assert not (init_cfg and pretrained), 'cannot set both'\nassert pretrained is None or isinstance(pretrained, str)
Prevention
- Migrate configs to init_cfg
- Run a config lint step in CI that rejects pretrained alongside init_cfg
When it happens
Trigger: ResNet(pretrained=dict(type='Pretrained', checkpoint=...)); ResNet(pretrained=True); combining pretrained and init_cfg.
Common situations: Legacy configs from mmdet v1/v2 using pretrained='torchvision://resnet50'; converting configs and leaving both keys; passing a loaded state_dict object instead of a path.
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/24c9c631f2e58fb5.
Report an issue: GitHub.