{"record":{"id":"dba1fd3c66965781","repo":"open-mmlab/mmdetection","slug":"init-cfg-must-contain-the-key-type","errorCode":null,"errorMessage":"`init_cfg` must contain the key \"type\"","messagePattern":"`init_cfg` must contain the key \"type\"","errorType":"validation","errorClass":"KeyError","httpStatus":null,"severity":"error","filePath":"mmdet/models/backbones/detectors_resnet.py","lineNumber":252,"sourceCode":"    def __init__(self,\n                 sac=None,\n                 stage_with_sac=(False, False, False, False),\n                 rfp_inplanes=None,\n                 output_img=False,\n                 pretrained=None,\n                 init_cfg=None,\n                 **kwargs):\n        assert not (init_cfg and pretrained), \\\n            'init_cfg and pretrained cannot be specified at the same time'\n        self.pretrained = pretrained\n        if init_cfg is not None:\n            assert isinstance(init_cfg, dict), \\\n                f'init_cfg must be a dict, but got {type(init_cfg)}'\n            if 'type' in init_cfg:\n                assert init_cfg.get('type') == 'Pretrained', \\\n                    'Only can initialize module by loading a pretrained model'\n            else:\n                raise KeyError('`init_cfg` must contain the key \"type\"')\n            self.pretrained = init_cfg.get('checkpoint')\n        self.sac = sac\n        self.stage_with_sac = stage_with_sac\n        self.rfp_inplanes = rfp_inplanes\n        self.output_img = output_img\n        super(DetectoRS_ResNet, self).__init__(**kwargs)\n\n        self.inplanes = self.stem_channels\n        self.res_layers = []\n        for i, num_blocks in enumerate(self.stage_blocks):\n            stride = self.strides[i]\n            dilation = self.dilations[i]\n            dcn = self.dcn if self.stage_with_dcn[i] else None\n            sac = self.sac if self.stage_with_sac[i] else None\n            if self.plugins is not None:\n                stage_plugins = self.make_stage_plugins(self.plugins, i)\n            else:\n                stage_plugins = None","sourceCodeStart":234,"sourceCodeEnd":270,"githubUrl":"https://github.com/open-mmlab/mmdetection/blob/cfd5d3a985b0249de009b67d04f37263e11cdf3d/mmdet/models/backbones/detectors_resnet.py#L234-L270","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"# before\nbackbone=dict(type='DetectoRS_ResNet', init_cfg=dict(checkpoint='resnet50.pth'))\n# after\nbackbone=dict(type='DetectoRS_ResNet', init_cfg=dict(type='Pretrained', checkpoint='resnet50.pth'))","handlingStrategy":"validation","validationCode":"if isinstance(init_cfg, dict) and 'checkpoint' in init_cfg:\n    assert init_cfg.get('type') == 'Pretrained', 'init_cfg needs type=\"Pretrained\"'","typeGuard":"def valid_init_cfg(cfg) -> bool:\n    return not isinstance(cfg, dict) or ('type' in cfg and (cfg.get('type') != 'Pretrained' or 'checkpoint' in cfg))","tryCatchPattern":null,"preventionTips":["Always include type='Pretrained' when giving a checkpoint","Copy init_cfg blocks from official configs","Validate nested config merges"],"tags":["mmdet","detectors-resnet","init-cfg","pretrained-weights","config"],"backgroundTag":null,"analyzedSha":"cfd5d3a985b0249de009b67d04f37263e11cdf3d","analyzedAt":"2026-08-27T20:54:20.183Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}