open-mmlab/mmdetection · error · KeyError

invalid depth {depth} for darknet

Error message

invalid depth {depth} for darknet

What it means

Darknet backbone supports only the depths listed in Darknet.arch_settings (typically 53 and 101 in mmdet); any other depth value raises this KeyError at construction. The depth selects a (layers, channels) architecture template.

Source

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

    # Dict(depth: (layers, channels))
    arch_settings = {
        53: ((1, 2, 8, 8, 4), ((32, 64), (64, 128), (128, 256), (256, 512),
                               (512, 1024)))
    }

    def __init__(self,
                 depth=53,
                 out_indices=(3, 4, 5),
                 frozen_stages=-1,
                 conv_cfg=None,
                 norm_cfg=dict(type='BN', requires_grad=True),
                 act_cfg=dict(type='LeakyReLU', negative_slope=0.1),
                 norm_eval=True,
                 pretrained=None,
                 init_cfg=None):
        super(Darknet, self).__init__(init_cfg)
        if depth not in self.arch_settings:
            raise KeyError(f'invalid depth {depth} for darknet')

        self.depth = depth
        self.out_indices = out_indices
        self.frozen_stages = frozen_stages
        self.layers, self.channels = self.arch_settings[depth]

        cfg = dict(conv_cfg=conv_cfg, norm_cfg=norm_cfg, act_cfg=act_cfg)

        self.conv1 = ConvModule(3, 32, 3, padding=1, **cfg)

        self.cr_blocks = ['conv1']
        for i, n_layers in enumerate(self.layers):
            layer_name = f'conv_res_block{i + 1}'
            in_c, out_c = self.channels[i]
            self.add_module(
                layer_name,
                self.make_conv_res_block(in_c, out_c, n_layers, **cfg))
            self.cr_blocks.append(layer_name)

View on GitHub (pinned to cfd5d3a985)

Solutions

  1. Use depth=53 or depth=101 (the keys of Darknet.arch_settings in your mmdet version)
  2. Check Darknet.arch_settings in mmdet/models/backbones/darknet.py for supported depths
  3. For other depths use arch_ovewrite-style backbones or a different backbone class

Example fix

# before
backbone=dict(type='Darknet', depth=50)
# after
backbone=dict(type='Darknet', depth=53)
Defensive patterns

Strategy: validation

Validate before calling

from mmdet.models.backbones.darknet import Darknet
assert depth in Darknet.arch_settings, f'depth must be one of {list(Darknet.arch_settings)}'

Type guard

def is_supported_darknet_depth(depth) -> bool:
    return depth in Darknet.arch_settings  # typically {53, 101}

Try / catch

try:
    backbone = Darknet(depth=depth)
except KeyError:
    backbone = Darknet(depth=53)  # nearest supported template

Prevention

When it happens

Trigger: Passing depth=18, depth=152, or depth='53' (string) to Darknet; assuming ImageNet-style depth naming (18/34/50) applies.

Common situations: Copy-pasting ResNet-style configs and only changing type to Darknet while keeping depth=50; typos; expecting a depth that only exists in other repos/versions.

Related errors


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