open-mmlab/mmdetection · error · TypeError
pretrained must be a str or None
Error message
pretrained must be a str or None
What it means
MobileNetV2.__init__ throws TypeError when the deprecated `pretrained` argument is neither a str nor None. The constructor accepts pretrained only as a checkpoint path string (converted to init_cfg) or None; any other type (dict, bool, list) is rejected. New configs should use init_cfg instead.
Source
Thrown at mmdet/models/backbones/mobilenet_v2.py:77
self.pretrained = pretrained
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:
if init_cfg is None:
self.init_cfg = [
dict(type='Kaiming', layer='Conv2d'),
dict(
type='Constant',
val=1,
layer=['_BatchNorm', 'GroupNorm'])
]
else:
raise TypeError('pretrained must be a str or None')
self.widen_factor = widen_factor
self.out_indices = out_indices
if not set(out_indices).issubset(set(range(0, 8))):
raise ValueError('out_indices must be a subset of range'
f'(0, 8). But received {out_indices}')
if frozen_stages not in range(-1, 8):
raise ValueError('frozen_stages must be in range(-1, 8). '
f'But received {frozen_stages}')
self.out_indices = out_indices
self.frozen_stages = frozen_stages
self.conv_cfg = conv_cfg
self.norm_cfg = norm_cfg
self.act_cfg = act_cfg
self.norm_eval = norm_eval
self.with_cp = with_cp
View on GitHub (pinned to cfd5d3a985)
Solutions
- Use init_cfg instead: MobileNetV2(init_cfg=dict(type='Pretrained', checkpoint='mobilenet_v2_8xb32_cifar10...'))
- If using pretrained, pass a str URL/path or None only
- Remove duplicate init_cfg when passing pretrained as str (they cannot be combined)
Example fix
// before model = dict(backbone=dict(type='MobileNetV2', pretrained=dict(type='Pretrained', checkpoint='...'))) // after model = dict(backbone=dict(type='MobileNetV2', init_cfg=dict(type='Pretrained', checkpoint='...')))
Defensive patterns
Strategy: validation
Validate before calling
assert pretrained is None or isinstance(pretrained, str), 'pretrained must be str or None'
Type guard
def is_valid_pretrained(p) -> bool:\n return p is None or (isinstance(p, str) and len(p) > 0)
Prevention
- Prefer init_cfg over pretrained in all new configs
- Validate pretrained type before building the backbone
When it happens
Trigger: Calling MobileNetV2(pretrained=dict(checkpoint=...)) or passing a Path/bool/list as pretrained; mixing old-style pretrained configs with new init_cfg usage.
Common situations: Migrating old mmdet v1/v2 configs that used pretrained='open-mmlab://...' or pretrained=dict(...); copy-pasting configs across mmdet versions where pretrained semantics changed.
Related errors
- pretrained must be a str or None
- pretrained must be a str or None
- `init_cfg` must contain the key "type"
- out_indices must be a subset of range(0, 8). But received {o
- pretrained must be a str or None
AI-assisted analysis of open-mmlab/mmdetection@cfd5d3a985 (2026-08-27).
Data as JSON: /api/errors/ae378de9e0c2c55f.
Report an issue: GitHub.