WZMIAOMIAO/deep-learning-for-image-processing · error · ValueError

expected stages_out_channels as list of 5 positive ints

Error message

expected stages_out_channels as list of 5 positive ints

What it means

ShuffleNetV2's __init__ requires stages_out_channels to have exactly 5 entries: a 24-channel first-layer output plus one value per of the 4 stages. Any other length means the channel plan cannot be mapped onto the fixed stage structure, so a ValueError is raised.

Source

Thrown at pytorch_classification/mini_imagenet/model.py:95

            out = torch.cat((self.branch1(x), self.branch2(x)), dim=1)

        out = channel_shuffle(out, 2)

        return out


class ShuffleNetV2(nn.Module):
    def __init__(self,
                 stages_repeats: List[int],
                 stages_out_channels: List[int],
                 num_classes: int = 1000,
                 inverted_residual: Callable[..., nn.Module] = InvertedResidual):
        super(ShuffleNetV2, self).__init__()

        if len(stages_repeats) != 3:
            raise ValueError("expected stages_repeats as list of 3 positive ints")
        if len(stages_out_channels) != 5:
            raise ValueError("expected stages_out_channels as list of 5 positive ints")
        self._stage_out_channels = stages_out_channels

        # input RGB image
        input_channels = 3
        output_channels = self._stage_out_channels[0]

        self.conv1 = nn.Sequential(
            nn.Conv2d(input_channels, output_channels, kernel_size=3, stride=2, padding=1, bias=False),
            nn.BatchNorm2d(output_channels),
            nn.ReLU(inplace=True)
        )
        input_channels = output_channels

        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)

        # Static annotations for mypy
        self.stage2: nn.Sequential
        self.stage3: nn.Sequential

View on GitHub (pinned to 1ec3fe6f37)

Solutions

  1. Pass exactly 5 channel values, e.g. [24, 116, 232, 464, 1024] for shufflenet_v2_x1_0.
  2. Check the channel list source; make sure the initial 24-channel conv output is included.
  3. Copy the known variants (0.5x: [24,48,96,192,1024]; 1.0x: [24,116,232,464,1024]; 1.5x: [24,176,352,704,1024]; 2.0x: [24,244,488,976,2048]).

Example fix

// before
model = ShuffleNetV2(stages_repeats=[4, 8, 4], stages_out_channels=[116, 232, 464, 1024], num_classes=100)
// after
model = ShuffleNetV2(stages_repeats=[4, 8, 4], stages_out_channels=[24, 116, 232, 464, 1024], num_classes=100)
Defensive patterns

Strategy: validation

Validate before calling

assert len(stages_out_channels) == 5, f"stages_out_channels must have 5 entries, got {len(stages_out_channels)}"
assert all(isinstance(c, int) and c > 0 for c in stages_out_channels), "all channels must be positive ints"

Type guard

def is_valid_stages_out_channels(v) -> bool:
    return isinstance(v, (list, tuple)) and len(v) == 5 and all(isinstance(c, int) and c > 0 for c in v)

Try / catch

try:
    model = ShuffleNetV2(stages_repeats=stages_repeats, stages_out_channels=stages_out_channels, num_classes=num_classes)
except ValueError as e:
    logging.error("bad ShuffleNetV2 channel config: %s", e)
    raise

Prevention

When it happens

Trigger: Calling ShuffleNetV2(stages_repeats=[...], stages_out_channels=[...]) with a stages_out_channels list whose len() != 5, e.g. [116, 232, 464, 1024] or [24, 116, 232, 464, 1024, 2048].

Common situations: Borrowing channel lists from other architectures (ResNet/EfficientNet have different stage counts), forgetting the leading 24, or appending an extra head channel.

Related errors


AI-assisted analysis of WZMIAOMIAO/deep-learning-for-image-processing@1ec3fe6f37 (2026-08-30). Data as JSON: /api/errors/887a4ecf2719b80c. Report an issue: GitHub.