open-mmlab/mmdetection · error · KeyError

invalid depth {depth} for resnet

Error message

invalid depth {depth} for resnet

What it means

ResNet.__init__ raises KeyError when `depth` is not one of the supported depths keyed in arch_settings (18, 34, 50, 101, 152). Each depth maps to (block, stage_blocks); an unsupported depth has no architecture definition.

Source

Thrown at mmdet/models/backbones/resnet.py:395

                 out_indices=(0, 1, 2, 3),
                 style='pytorch',
                 deep_stem=False,
                 avg_down=False,
                 frozen_stages=-1,
                 conv_cfg=None,
                 norm_cfg=dict(type='BN', requires_grad=True),
                 norm_eval=True,
                 dcn=None,
                 stage_with_dcn=(False, False, False, False),
                 plugins=None,
                 with_cp=False,
                 zero_init_residual=True,
                 pretrained=None,
                 init_cfg=None):
        super(ResNet, self).__init__(init_cfg)
        self.zero_init_residual = zero_init_residual
        if depth not in self.arch_settings:
            raise KeyError(f'invalid depth {depth} for resnet')

        block_init_cfg = None
        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'])
                ]
                block = self.arch_settings[depth][0]

View on GitHub (pinned to cfd5d3a985)

Solutions

  1. Use one of the supported depths: 18, 34, 50, 101, 152
  2. For custom depths, subclass ResNet and add to arch_settings, e.g. arch_settings[100] = (Bottleneck, (3, 4, 23, 3))
  3. Upgrade mmdet if the depth you need was added later

Example fix

// before
backbone=dict(type='ResNet', depth=20)
// after
# custom depth via subclass
from mmdet.models.backbones.resnet import ResNet, Bottleneck
class ResNet100(ResNet):
    arch_settings = {100: (Bottleneck, (3, 4, 23, 3))}
backbone=dict(type='ResNet100', depth=100)
Defensive patterns

Strategy: validation

Validate before calling

from mmdet.models.backbones.resnet import ResNet\nassert depth in ResNet.arch_settings, f'depth must be one of {list(ResNet.arch_settings)}'

Type guard

def is_valid_resnet_depth(d):\n    from mmdet.models.backbones.resnet import ResNet\n    return d in ResNet.arch_settings

Prevention

When it happens

Trigger: ResNet(depth=20), ResNet(depth=1010), or depth as a string like '50' (not in the int-keyed dict). Custom depths need a manual subclass that extends arch_settings.

Common situations: Typos in configs (depth=155); attempting ResNeSt/ResNeXt-style depths on plain ResNet; using a depth supported in newer mmdet (e.g. 200) on an older install.

Related errors


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