{"record":{"id":"e881adf81834fe4a","repo":"open-mmlab/mmdetection","slug":"pretrained-must-be-a-str-or-none-e881ad","errorCode":null,"errorMessage":"pretrained must be a str or None","messagePattern":"pretrained must be a str or None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"mmdet/models/backbones/swin.py","lineNumber":570,"sourceCode":"        if isinstance(pretrain_img_size, int):\n            pretrain_img_size = to_2tuple(pretrain_img_size)\n        elif isinstance(pretrain_img_size, tuple):\n            if len(pretrain_img_size) == 1:\n                pretrain_img_size = to_2tuple(pretrain_img_size[0])\n            assert len(pretrain_img_size) == 2, \\\n                f'The size of image should have length 1 or 2, ' \\\n                f'but got {len(pretrain_img_size)}'\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            self.init_cfg = init_cfg\n        else:\n            raise TypeError('pretrained must be a str or None')\n\n        super(SwinTransformer, self).__init__(init_cfg=init_cfg)\n\n        num_layers = len(depths)\n        self.out_indices = out_indices\n        self.use_abs_pos_embed = use_abs_pos_embed\n\n        assert strides[0] == patch_size, 'Use non-overlapping patch embed.'\n\n        self.patch_embed = PatchEmbed(\n            in_channels=in_channels,\n            embed_dims=embed_dims,\n            conv_type='Conv2d',\n            kernel_size=patch_size,\n            stride=strides[0],\n            norm_cfg=norm_cfg if patch_norm else None,\n            init_cfg=None)\n","sourceCodeStart":552,"sourceCodeEnd":588,"githubUrl":"https://github.com/open-mmlab/mmdetection/blob/cfd5d3a985b0249de009b67d04f37263e11cdf3d/mmdet/models/backbones/swin.py#L552-L588","documentation":"SwinTransformer.__init__ raises TypeError when the deprecated `pretrained` argument is not a str or None. str is wrapped into init_cfg; None falls back to the init_cfg parameter. Other types are rejected.","triggerScenarios":"SwinTransformer(pretrained=dict(...)) or pretrained=Path(...); providing both pretrained and a non-None init_cfg.","commonSituations":"Migrating old Swin configs (mmdet 2.x) that used pretrained='...'; passing a state_dict instead of a path string.","solutions":["Use init_cfg=dict(type='Pretrained', checkpoint='swin_tiny.pth') and omit pretrained","If using pretrained, pass a str URL/path with value None for init_cfg"],"exampleFix":"// before\nbackbone=dict(type='SwinTransformer', pretrained='swin_tiny_patch4_window7_224.pth')\n// after\nbackbone=dict(type='SwinTransformer', init_cfg=dict(type='Pretrained', checkpoint='swin_tiny_patch4_window7_224.pth'))","handlingStrategy":"validation","validationCode":"assert pretrained is None or isinstance(pretrained, str)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Adopt init_cfg API","Never pass state_dict objects as pretrained"],"tags":["mmdet","swin-transformer","pretrained","init-cfg"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"cfd5d3a985b0249de009b67d04f37263e11cdf3d","analyzedAt":"2026-08-27T20:54:20.183Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}