open-mmlab/mmdetection · error · TypeError

pretrained must be a str or None

Error message

pretrained must be a str or None

What it means

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.

Source

Thrown at mmdet/models/backbones/darknet.py:151

        self.norm_eval = norm_eval

        assert not (init_cfg and pretrained), \
            'init_cfg and pretrained cannot be specified at the same time'
        if isinstance(pretrained, str):
            warnings.warn('DeprecationWarning: pretrained is deprecated, '
                          'please use "init_cfg" instead')
            self.init_cfg = dict(type='Pretrained', checkpoint=pretrained)
        elif pretrained is None:
            if init_cfg is None:
                self.init_cfg = [
                    dict(type='Kaiming', layer='Conv2d'),
                    dict(
                        type='Constant',
                        val=1,
                        layer=['_BatchNorm', 'GroupNorm'])
                ]
        else:
            raise TypeError('pretrained must be a str or None')

    def forward(self, x):
        outs = []
        for i, layer_name in enumerate(self.cr_blocks):
            cr_block = getattr(self, layer_name)
            x = cr_block(x)
            if i in self.out_indices:
                outs.append(x)

        return tuple(outs)

    def _freeze_stages(self):
        if self.frozen_stages >= 0:
            for i in range(self.frozen_stages):
                m = getattr(self, self.cr_blocks[i])
                m.eval()
                for param in m.parameters():
                    param.requires_grad = False

View on GitHub (pinned to cfd5d3a985)

Solutions

  1. Pass pretrained as a plain string path/URL or None
  2. Better: drop pretrained and use init_cfg=dict(type='Pretrained', checkpoint='darknet53.pth')
  3. If using pathlib.Path, convert with str(path)

Example fix

# before
backbone=dict(type='Darknet', pretrained=dict(ckpt='darknet53.pth'))
# after
backbone=dict(type='Darknet', init_cfg=dict(type='Pretrained', checkpoint='darknet53.pth'))
Defensive patterns

Strategy: type-guard

Validate before calling

assert pretrained is None or isinstance(pretrained, str), 'pretrained must be str or None'

Type guard

def is_valid_pretrained(p) -> bool:
    return p is None or isinstance(p, str)

Prevention

When it happens

Trigger: Passing pretrained=dict(checkpoint='...') or pretrained=['darknet53.pth'] to Darknet; passing a Path object (use str(path)) in some versions.

Common situations: Migrating old configs that wrapped pretrained in a dict; mixing init_cfg and pretrained arguments; loading from pathlib.Path without converting to str.

Related errors


AI-assisted analysis of open-mmlab/mmdetection@cfd5d3a985 (2026-08-27). Data as JSON: /api/errors/500361106b5d18f3. Report an issue: GitHub.