open-mmlab/mmdetection · warning

DeprecationWarning: pretrained is a deprecated, please use "

Error message

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

What it means

Deprecation warning from ResLayer.__init__ (shared head): passing `pretrained='<checkpoint>'` is deprecated; the string is converted into init_cfg=dict(type='Pretrained', checkpoint=...). Specifying both pretrained and init_cfg raises an assertion instead.

Source

Thrown at mmdet/models/roi_heads/shared_heads/res_layer.py:54

        inplanes = 64 * 2**(stage - 1) * block.expansion

        res_layer = _ResLayer(
            block,
            inplanes,
            planes,
            stage_block,
            stride=stride,
            dilation=dilation,
            style=style,
            with_cp=with_cp,
            norm_cfg=self.norm_cfg,
            dcn=dcn)
        self.add_module(f'layer{stage + 1}', res_layer)

        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 a 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):
        res_layer = getattr(self, f'layer{self.stage + 1}')
        out = res_layer(x)
        return out

View on GitHub (pinned to cfd5d3a985)

Solutions

  1. Replace pretrained='...' with init_cfg=dict(type='Pretrained', checkpoint='...')
  2. Never set both pretrained and init_cfg (assertion error)
  3. Remove pretrained if weights are already loaded by the detector's load_from

Example fix

# before
shared_head=dict(type='ResLayer', depth=50, pretrained='torchvision://resnet50')
# after
shared_head=dict(type='ResLayer', depth=50, init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet50'))
Defensive patterns

Strategy: validation

Validate before calling

assert 'pretrained' not in layer_cfg, 'use init_cfg=dict(type=\'Pretrained\', checkpoint=...)'; assert not ('pretrained' in layer_cfg and 'init_cfg' in layer_cfg)

Prevention

When it happens

Trigger: Building a shared ResLayer with pretrained='torchvision://resnet50' or similar; old Faster R-CNN configs that loaded backbone weights through the shared head's pretrained arg.

Common situations: Porting old two-stage detector configs to the init_cfg weight-loading scheme introduced in mmdet 2.x; mixing pretrained and init_cfg triggers the assert above.

Related errors


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