open-mmlab/mmdetection · error · TypeError
pretrained must be a str or None
Error message
pretrained must be a str or None
What it means
ResLayer (shared head) accepts pretrained as either a str checkpoint path (converted into an init_cfg) or None; any other type raises TypeError in __init__.
Source
Thrown at mmdet/models/roi_heads/shared_heads/res_layer.py:67
self.add_module(f'layer{stage + 1}', res_layer)
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 a 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')
def forward(self, x):
res_layer = getattr(self, f'layer{self.stage + 1}')
out = res_layer(x)
return out
def train(self, mode=True):
super(ResLayer, self).train(mode)
if self.norm_eval:
for m in self.modules():
if isinstance(m, nn.BatchNorm2d):
m.eval()
View on GitHub (pinned to cfd5d3a985)
Solutions
- Pass a str path: pretrained='torchvision://resnet50' or a local .pth path string
- Pass pretrained=None and use init_cfg=dict(type='Pretrained', checkpoint=...) instead
- Convert Path objects with str(path) before passing
Example fix
# before
shared_head=dict(type='ResLayer', pretrained={'ckpt': 'r50.pth'})
# after
shared_head=dict(type='ResLayer', pretrained='r50.pth') Defensive patterns
Strategy: type-guard
Validate before calling
p = cfg.get('pretrained', None)
assert p is None or isinstance(p, str) Type guard
def valid_pretrained(v) -> bool: return v is None or isinstance(v, str)
Prevention
- Use init_cfg for new-style pretrained loading
- str(Path) any checkpoint paths before passing
When it happens
Trigger: shared_head=dict(type='ResLayer', pretrained=123) or pretrained={'checkpoint': ...} or a list of paths.
Common situations: Old-style configs or code passing the newer init_cfg dict into pretrained; programmatic construction passing a Path object or bool.
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
- LoadImageFromFile is not found in the test pipeline
- Visualization needs the "visualizer" termdefined in the conf
- config must be a filename or Config object, but got {type(co
- Unrecognized dataset: {dataset}
- The `file_client_args` is deprecated, please use `backend_ar
AI-assisted analysis of open-mmlab/mmdetection@cfd5d3a985 (2026-08-27).
Data as JSON: /api/errors/4247ba5b9e20e4bd.
Report an issue: GitHub.