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.__init__ requires stages_out_channels to contain exactly 5 entries (conv1 output plus the three stages plus the final conv5) and raises ValueError otherwise. Each entry fixes the output channel count of a corresponding layer group.
Source
Thrown at pytorch_classification/Test7_shufflenet/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.SequentialView on GitHub (pinned to 1ec3fe6f37)
Solutions
- Provide 5 channel counts, e.g. [24, 116, 232, 464, 1024] for the 1.0x model.
- Use the provided presets ShuffleNetV2_x0_5/x1_0/x1_5/x2_0(num_classes=...) which hard-code valid lists.
- Verify the order of positional args: stages_repeats first (3 items), stages_out_channels second (5 items).
Example fix
// before model = ShuffleNetV2(stages_repeats=[4, 8, 4], stages_out_channels=[116, 232, 464, 1024], num_classes=5) // after model = ShuffleNetV2(stages_repeats=[4, 8, 4], stages_out_channels=[24, 116, 232, 464, 1024], num_classes=5)
Defensive patterns
Strategy: validation
Validate before calling
assert len(stages_out_channels) == 5, f"stages_out_channels must have 5 ints, got {len(stages_out_channels)}"
model = ShuffleNetV2(stages_repeats=stages_repeats, stages_out_channels=stages_out_channels, num_classes=n) Type guard
def valid_channels(c) -> bool:
return isinstance(c, list) and len(c) == 5 and all(isinstance(v, int) and v > 0 for v in c) Try / catch
try:
model = ShuffleNetV2(stages_repeats=rep, stages_out_channels=ch, num_classes=n)
except ValueError as e:
if "stages_out_channels" in str(e):
model = ShuffleNetV2_x1_0(num_classes=n)
else:
raise Prevention
- Copy full 5-element presets like [24, 116, 232, 464, 1024].
- Use the factory functions for standard widths.
- Validate list lengths before model construction.
When it happens
Trigger: Calling ShuffleNetV2 with stages_out_channels of length != 5 — e.g. [24, 116, 232, 464] (missing the last conv5 value) or [116, 232, 464, 1024] (only the stages).
Common situations: Defining a custom width variant and omitting the leading 24 (first conv) or trailing 1024 (last conv) value; truncating a copied preset list; confusing the channel list with the repeats list.
Related errors
- expected stages_repeats as list of 3 positive ints
- illegal stride value.
- The inverted_residual_setting should not be empty.
- illegal stride value.
- image: {} isn't RGB mode.
AI-assisted analysis of WZMIAOMIAO/deep-learning-for-image-processing@1ec3fe6f37 (2026-08-30).
Data as JSON: /api/errors/ff0c7ec902fe60da.
Report an issue: GitHub.