open-mmlab/mmdetection · error · ValueError

frozen_stages must be in range(-1, len(arch_setting) + 1). B

Error message

frozen_stages must be in range(-1, len(arch_setting) + 1). But received {frozen_stages}

What it means

CSPDarknet validates frozen_stages is an integer in [-1, len(arch_setting)] where arch_setting is the stage list for the chosen depth. frozen_stages=-1 means no freezing; values beyond the last stage index are rejected because there is no such stage to freeze.

Source

Thrown at mmdet/models/backbones/csp_darknet.py:206

                 conv_cfg=None,
                 norm_cfg=dict(type='BN', momentum=0.03, eps=0.001),
                 act_cfg=dict(type='Swish'),
                 norm_eval=False,
                 init_cfg=dict(
                     type='Kaiming',
                     layer='Conv2d',
                     a=math.sqrt(5),
                     distribution='uniform',
                     mode='fan_in',
                     nonlinearity='leaky_relu')):
        super().__init__(init_cfg)
        arch_setting = self.arch_settings[arch]
        if arch_ovewrite:
            arch_setting = arch_ovewrite
        assert set(out_indices).issubset(
            i for i in range(len(arch_setting) + 1))
        if frozen_stages not in range(-1, len(arch_setting) + 1):
            raise ValueError('frozen_stages must be in range(-1, '
                             'len(arch_setting) + 1). But received '
                             f'{frozen_stages}')

        self.out_indices = out_indices
        self.frozen_stages = frozen_stages
        self.use_depthwise = use_depthwise
        self.norm_eval = norm_eval
        conv = DepthwiseSeparableConvModule if use_depthwise else ConvModule

        self.stem = Focus(
            3,
            int(arch_setting[0][0] * widen_factor),
            kernel_size=3,
            conv_cfg=conv_cfg,
            norm_cfg=norm_cfg,
            act_cfg=act_cfg)
        self.layers = ['stem']

View on GitHub (pinned to cfd5d3a985)

Solutions

  1. Set frozen_stages to -1 (none) or 0..len(stages)-1, e.g. at most 3 for CSPDarknet with 4 stages
  2. Count stages in the arch_settings entry for your depth before choosing
  3. Use frozen_stages=-1 when you do not want freezing

Example fix

# before
model = dict(backbone=dict(type='CSPDarknet', depth=53, frozen_stages=5))
# after
model = dict(backbone=dict(type='CSPDarknet', depth=53, frozen_stages=3))
Defensive patterns

Strategy: validation

Validate before calling

n_stages = len(CSPDarknet.arch_settings[depth])
assert isinstance(frozen_stages, int) and -1 <= frozen_stages <= n_stages, f'frozen_stages must be in [-1, {n_stages}]'

Type guard

def valid_frozen_stages(v, n_stages: int) -> bool:
    return isinstance(v, int) and -1 <= v <= n_stages

Prevention

When it happens

Trigger: Setting frozen_stages=5 or larger on CSPDarknet depth 53 (4 stages, so valid range is -1..4); also non-integer values like frozen_stages=1.5 (not in range()).

Common situations: Copy-pasting ResNet configs where frozen_stages counts differently (ResNet allows up to 4); changing arch without adjusting frozen_stages; off-by-one confusion between number of stages and max freezable stage.

Related errors


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