open-mmlab/mmdetection · error · KeyError
`init_cfg` must contain the key "type"
Error message
`init_cfg` must contain the key "type"
What it means
DetectoRS_ResNet requires that any dict init_cfg containing a 'checkpoint' key also contains type='Pretrained' (the only supported init mode via this legacy path). An init_cfg dict without a 'type' key raises this KeyError.
Source
Thrown at mmdet/models/backbones/detectors_resnet.py:252
def __init__(self,
sac=None,
stage_with_sac=(False, False, False, False),
rfp_inplanes=None,
output_img=False,
pretrained=None,
init_cfg=None,
**kwargs):
assert not (init_cfg and pretrained), \
'init_cfg and pretrained cannot be specified at the same time'
self.pretrained = pretrained
if init_cfg is not None:
assert isinstance(init_cfg, dict), \
f'init_cfg must be a dict, but got {type(init_cfg)}'
if 'type' in init_cfg:
assert init_cfg.get('type') == 'Pretrained', \
'Only can initialize module by loading a pretrained model'
else:
raise KeyError('`init_cfg` must contain the key "type"')
self.pretrained = init_cfg.get('checkpoint')
self.sac = sac
self.stage_with_sac = stage_with_sac
self.rfp_inplanes = rfp_inplanes
self.output_img = output_img
super(DetectoRS_ResNet, self).__init__(**kwargs)
self.inplanes = self.stem_channels
self.res_layers = []
for i, num_blocks in enumerate(self.stage_blocks):
stride = self.strides[i]
dilation = self.dilations[i]
dcn = self.dcn if self.stage_with_dcn[i] else None
sac = self.sac if self.stage_with_sac[i] else None
if self.plugins is not None:
stage_plugins = self.make_stage_plugins(self.plugins, i)
else:
stage_plugins = NoneView on GitHub (pinned to cfd5d3a985)
Solutions
- Always include the type key: init_cfg=dict(type='Pretrained', checkpoint='...')
- Use only type='Pretrained' when supplying a checkpoint in this branch
- Validate init_cfg keys before constructing: assert 'type' in init_cfg
Example fix
# before backbone=dict(type='DetectoRS_ResNet', init_cfg=dict(checkpoint='resnet50.pth')) # after backbone=dict(type='DetectoRS_ResNet', init_cfg=dict(type='Pretrained', checkpoint='resnet50.pth'))
Defensive patterns
Strategy: validation
Validate before calling
if isinstance(init_cfg, dict) and 'checkpoint' in init_cfg:
assert init_cfg.get('type') == 'Pretrained', 'init_cfg needs type="Pretrained"' Type guard
def valid_init_cfg(cfg) -> bool:
return not isinstance(cfg, dict) or ('type' in cfg and (cfg.get('type') != 'Pretrained' or 'checkpoint' in cfg)) Prevention
- Always include type='Pretrained' when giving a checkpoint
- Copy init_cfg blocks from official configs
- Validate nested config merges
When it happens
Trigger: Passing init_cfg={'checkpoint':'resnet50.pth'} (missing 'type') to DetectoRS_ResNet; passing init_cfg with type='Constant' together with a checkpoint key through this legacy branch.
Common situations: Hand-writing init_cfg and forgetting the type field; merging configs where 'type' gets overridden or dropped; mixing new-style init_cfg with legacy pretrained handling.
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/dba1fd3c66965781.
Report an issue: GitHub.