open-mmlab/mmdetection · info

DeprecationWarning: pretrained is deprecated, please use "in

Error message

DeprecationWarning: pretrained is deprecated, please use "init_cfg" instead

What it means

Swin Transformer backbone constructor warns that the legacy `pretrained` string argument is deprecated in favor of the standardized `init_cfg` mechanism. The constructor asserts the two cannot be combined and converts `pretrained` into `init_cfg=dict(type='Pretrained', checkpoint=pretrained)` after warning. It is purely informational; behavior is preserved.

Source

Thrown at mmdet/models/backbones/swin.py:564

                 pretrained=None,
                 convert_weights=False,
                 frozen_stages=-1,
                 init_cfg=None):
        self.convert_weights = convert_weights
        self.frozen_stages = frozen_stages
        if isinstance(pretrain_img_size, int):
            pretrain_img_size = to_2tuple(pretrain_img_size)
        elif isinstance(pretrain_img_size, tuple):
            if len(pretrain_img_size) == 1:
                pretrain_img_size = to_2tuple(pretrain_img_size[0])
            assert len(pretrain_img_size) == 2, \
                f'The size of image should have length 1 or 2, ' \
                f'but got {len(pretrain_img_size)}'

        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:
            self.init_cfg = init_cfg
        else:
            raise TypeError('pretrained must be a str or None')

        super(SwinTransformer, self).__init__(init_cfg=init_cfg)

        num_layers = len(depths)
        self.out_indices = out_indices
        self.use_abs_pos_embed = use_abs_pos_embed

        assert strides[0] == patch_size, 'Use non-overlapping patch embed.'

        self.patch_embed = PatchEmbed(
            in_channels=in_channels,
            embed_dims=embed_dims,

View on GitHub (pinned to cfd5d3a985)

Solutions

  1. Replace `pretrained='...'` with `init_cfg=dict(type='Pretrained', checkpoint='...')` in the backbone config
  2. Never set both init_cfg and pretrained — the constructor asserts they are mutually exclusive

Example fix

// before
backbone=dict(type='SwinTransformer', pretrained='swin_tiny.pth', ...)
// after
backbone=dict(type='SwinTransformer', init_cfg=dict(type='Pretrained', checkpoint='swin_tiny.pth'), ...)
Defensive patterns

Strategy: validation

Validate before calling

cfg = dict(type='SwinTransformer', ...)
assert not ('pretrained' in cfg and 'init_cfg' in cfg)
cfg.setdefault('init_cfg', dict(type='Pretrained', checkpoint=cfg.pop('pretrained', None))) if cfg.get('pretrained') else None

Prevention

When it happens

Trigger: Calling SwinTransformer(..., pretrained='path-or-url') or passing pretrained in a config file instead of init_cfg.

Common situations: Old MMDetection/MMPretrain configs (pre-v2.x style) reused with newer mmdet; tutorials demonstrating `model = build_detector(cfg); model.backbone.pretrained = ...`.

Related errors


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