{"record":{"id":"500361106b5d18f3","repo":"open-mmlab/mmdetection","slug":"pretrained-must-be-a-str-or-none","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/darknet.py","lineNumber":151,"sourceCode":"        self.norm_eval = norm_eval\n\n        assert not (init_cfg and pretrained), \\\n            'init_cfg and pretrained cannot be specified at the same time'\n        if isinstance(pretrained, str):\n            warnings.warn('DeprecationWarning: pretrained is deprecated, '\n                          'please use \"init_cfg\" instead')\n            self.init_cfg = dict(type='Pretrained', checkpoint=pretrained)\n        elif pretrained is None:\n            if init_cfg is None:\n                self.init_cfg = [\n                    dict(type='Kaiming', layer='Conv2d'),\n                    dict(\n                        type='Constant',\n                        val=1,\n                        layer=['_BatchNorm', 'GroupNorm'])\n                ]\n        else:\n            raise TypeError('pretrained must be a str or None')\n\n    def forward(self, x):\n        outs = []\n        for i, layer_name in enumerate(self.cr_blocks):\n            cr_block = getattr(self, layer_name)\n            x = cr_block(x)\n            if i in self.out_indices:\n                outs.append(x)\n\n        return tuple(outs)\n\n    def _freeze_stages(self):\n        if self.frozen_stages >= 0:\n            for i in range(self.frozen_stages):\n                m = getattr(self, self.cr_blocks[i])\n                m.eval()\n                for param in m.parameters():\n                    param.requires_grad = False","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/open-mmlab/mmdetection/blob/cfd5d3a985b0249de009b67d04f37263e11cdf3d/mmdet/models/backbones/darknet.py#L133-L169","documentation":"Darknet's legacy init_weights path only accepts pretrained as a string path/URL or None; the else-branch after handling str and falsy values raises this TypeError for any other type. This mirrors the deprecation of pretrained in favor of init_cfg.","triggerScenarios":"Passing pretrained=dict(checkpoint='...') or pretrained=['darknet53.pth'] to Darknet; passing a Path object (use str(path)) in some versions.","commonSituations":"Migrating old configs that wrapped pretrained in a dict; mixing init_cfg and pretrained arguments; loading from pathlib.Path without converting to str.","solutions":["Pass pretrained as a plain string path/URL or None","Better: drop pretrained and use init_cfg=dict(type='Pretrained', checkpoint='darknet53.pth')","If using pathlib.Path, convert with str(path)"],"exampleFix":"# before\nbackbone=dict(type='Darknet', pretrained=dict(ckpt='darknet53.pth'))\n# after\nbackbone=dict(type='Darknet', init_cfg=dict(type='Pretrained', checkpoint='darknet53.pth'))","handlingStrategy":"type-guard","validationCode":"assert pretrained is None or isinstance(pretrained, str), 'pretrained must be str or None'","typeGuard":"def is_valid_pretrained(p) -> bool:\n    return p is None or isinstance(p, str)","tryCatchPattern":null,"preventionTips":["Prefer init_cfg over pretrained","Convert Path to str","Never wrap checkpoints in dicts/lists"],"tags":["mmdet","darknet","pretrained-weights","init-cfg","config"],"backgroundTag":null,"analyzedSha":"cfd5d3a985b0249de009b67d04f37263e11cdf3d","analyzedAt":"2026-08-27T20:54:20.183Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}