open-mmlab/mmdetection · error · ValueError

frozen_stages must be in range(-1, 8). But received {frozen_

Error message

frozen_stages must be in range(-1, 8). But received {frozen_stages}

What it means

MobileNetV2.__init__ requires frozen_stages to be an int in range(-1, 8): -1 means no freezing, 0 freezes the stem, and k (1..7) freezes stages up to k. Values outside that range are meaningless for this 8-stage backbone and raise ValueError.

Source

Thrown at mmdet/models/backbones/mobilenet_v2.py:86

            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')

        self.widen_factor = widen_factor
        self.out_indices = out_indices
        if not set(out_indices).issubset(set(range(0, 8))):
            raise ValueError('out_indices must be a subset of range'
                             f'(0, 8). But received {out_indices}')

        if frozen_stages not in range(-1, 8):
            raise ValueError('frozen_stages must be in range(-1, 8). '
                             f'But received {frozen_stages}')
        self.out_indices = out_indices
        self.frozen_stages = frozen_stages
        self.conv_cfg = conv_cfg
        self.norm_cfg = norm_cfg
        self.act_cfg = act_cfg
        self.norm_eval = norm_eval
        self.with_cp = with_cp

        self.in_channels = make_divisible(32 * widen_factor, 8)

        self.conv1 = ConvModule(
            in_channels=3,
            out_channels=self.in_channels,
            kernel_size=3,
            stride=2,
            padding=1,
            conv_cfg=self.conv_cfg,

View on GitHub (pinned to cfd5d3a985)

Solutions

  1. Use frozen_stages in -1..7 (e.g. -1 for no freezing, 1..7 for progressive freezing)
  2. If you meant to freeze everything through the last stage, use frozen_stages=7

Example fix

// before
backbone=dict(type='MobileNetV2', frozen_stages=9)
// after
backbone=dict(type='MobileNetV2', frozen_stages=2)
Defensive patterns

Strategy: validation

Validate before calling

assert frozen_stages in range(-1, 8), 'frozen_stages must be in range(-1, 8)'

Prevention

When it happens

Trigger: MobileNetV2(frozen_stages=8) or frozen_stages=-2; passing a non-int like frozen_stages='1' (would also fail the range check via `in` on range with TypeError for str).

Common situations: Copying frozen_stages=4 from a ResNet-based config (where it is valid) into MobileNetV2; freezing all stages by guessing the total count.

Related errors


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