{"record":{"id":"fe58e60b78454810","repo":"open-mmlab/mmdetection","slug":"pretrained-must-be-a-str-or-none-fe58e6","errorCode":null,"errorMessage":"pretrained must be a str or None","messagePattern":"pretrained must be a str or None","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"mmdet/models/backbones/detectors_resnet.py","lineNumber":323,"sourceCode":"                if isinstance(m, nn.Conv2d):\n                    kaiming_init(m)\n                elif isinstance(m, (_BatchNorm, nn.GroupNorm)):\n                    constant_init(m, 1)\n\n            if self.dcn is not None:\n                for m in self.modules():\n                    if isinstance(m, Bottleneck) and hasattr(\n                            m.conv2, 'conv_offset'):\n                        constant_init(m.conv2.conv_offset, 0)\n\n            if self.zero_init_residual:\n                for m in self.modules():\n                    if isinstance(m, Bottleneck):\n                        constant_init(m.norm3, 0)\n                    elif isinstance(m, BasicBlock):\n                        constant_init(m.norm2, 0)\n        else:\n            raise TypeError('pretrained must be a str or None')\n\n    def make_res_layer(self, **kwargs):\n        \"\"\"Pack all blocks in a stage into a ``ResLayer`` for DetectoRS.\"\"\"\n        return ResLayer(**kwargs)\n\n    def forward(self, x):\n        \"\"\"Forward function.\"\"\"\n        outs = list(super(DetectoRS_ResNet, self).forward(x))\n        if self.output_img:\n            outs.insert(0, x)\n        return tuple(outs)\n\n    def rfp_forward(self, x, rfp_feats):\n        \"\"\"Forward function for RFP.\"\"\"\n        if self.deep_stem:\n            x = self.stem(x)\n        else:\n            x = self.conv1(x)","sourceCodeStart":305,"sourceCodeEnd":341,"githubUrl":"https://github.com/open-mmlab/mmdetection/blob/cfd5d3a985b0249de009b67d04f37263e11cdf3d/mmdet/models/backbones/detectors_resnet.py#L305-L341","documentation":"DetectoRS_ResNet.init_weights (legacy pretrained path) raises TypeError('pretrained must be a str or None') when self.pretrained is neither a string nor None, e.g. left as a dict from a misconfigured init_cfg or positional arg. It occurs at weight-initialization time, not construction.","triggerScenarios":"Constructing DetectoRS_ResNet with pretrained=dict(...) or a non-str value; init_cfg mishandling that leaves self.pretrained set to a dict; then calling model.init_weights() or runner init.","commonSituations":"Configs migrating from MMDetection 1.x style pretrained=dict(type='Pretrained', checkpoint=...) passed via the wrong argument; merging configs that leave a stale pretrained value.","solutions":["Remove the legacy pretrained argument; set weights via init_cfg=dict(type='Pretrained', checkpoint='...')","If pretrained must be used, pass a plain string path or None","Audit config inheritance (_delete_=True where needed) so stale pretrained keys do not leak"],"exampleFix":"# before\nmodel = dict(backbone=dict(type='DetectoRS_ResNet', pretrained=dict(checkpoint='torchvision://resnet50')))\n# after\nmodel = dict(backbone=dict(type='DetectoRS_ResNet', init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet50')))","handlingStrategy":"type-guard","validationCode":"assert model.backbone.pretrained is None or isinstance(model.backbone.pretrained, str)","typeGuard":"def clean_pretrained(p):\n    if p is None or isinstance(p, str):\n        return p\n    if isinstance(p, dict) and 'checkpoint' in p:\n        return p['checkpoint']\n    raise TypeError('pretrained must be a str or None')","tryCatchPattern":"try:\n    model.backbone.init_weights()\nexcept TypeError as e:\n    if 'pretrained' in str(e):\n        model.backbone.pretrained = None; model.backbone.init_weights()\n    else: raise","preventionTips":["Migrate to init_cfg-based loading","Purge legacy pretrained keys with _delete_=True","Run init_weights() once in a smoke test after config build"],"tags":["mmdet","detectors-resnet","pretrained-weights","init-weights","migration"],"backgroundTag":null,"analyzedSha":"cfd5d3a985b0249de009b67d04f37263e11cdf3d","analyzedAt":"2026-08-27T20:54:20.183Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}