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

CSPNeXt.__init__ validates frozen_stages lies in [-1, len(arch_setting)] for the chosen arch's stage list. An out-of-range value means you asked to freeze more stages than the backbone has, so construction fails immediately.

Source

Thrown at mmdet/models/backbones/cspnext.py:95

        norm_cfg: ConfigType = dict(type='BN', momentum=0.03, eps=0.001),
        act_cfg: ConfigType = dict(type='SiLU'),
        norm_eval: bool = False,
        init_cfg: OptMultiConfig = dict(
            type='Kaiming',
            layer='Conv2d',
            a=math.sqrt(5),
            distribution='uniform',
            mode='fan_in',
            nonlinearity='leaky_relu')
    ) -> None:
        super().__init__(init_cfg=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 = nn.Sequential(
            ConvModule(
                3,
                int(arch_setting[0][0] * widen_factor // 2),
                3,
                padding=1,
                stride=2,
                norm_cfg=norm_cfg,
                act_cfg=act_cfg),
            ConvModule(

View on GitHub (pinned to cfd5d3a985)

Solutions

  1. Use frozen_stages=-1 (no freeze) or a value 0..num_stages-1 for the chosen arch
  2. Check len(self.arch_settings[arch]) in cspnext.py to confirm stage count
  3. Lower the value one step at a time until construction succeeds

Example fix

# before
backbone=dict(type='CSPNeXt', arch='P5', frozen_stages=6)
# after
backbone=dict(type='CSPNeXt', arch='P5', frozen_stages=4)
Defensive patterns

Strategy: validation

Validate before calling

n = len(CSPNeXt.arch_settings[arch])
assert -1 <= frozen_stages <= n, f'frozen_stages out of range for arch {arch}'

Type guard

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

Prevention

When it happens

Trigger: Setting frozen_stages >= number of stages+1 (e.g. 5+ on CSPNeXt with ~5 stages); using frozen_stages values tuned for a different backbone family.

Common situations: Porting RTMDet/YOLOX configs across backbones; incrementing frozen_stages during transfer-learning experiments until it exceeds stage count; typo (e.g. frozen_stages=-2 is also out of range).

Related errors


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