WZMIAOMIAO/deep-learning-for-image-processing · error · ValueError
expected stages_repeats as list of 3 positive ints
Error message
expected stages_repeats as list of 3 positive ints
What it means
ShuffleNetV2.__init__ asserts that stages_repeats is a list of exactly 3 entries (one repeat count per stage2/3/4) and raises ValueError with this message when len(stages_repeats) != 3. The architecture is fixed to three inverted-residual stages.
Source
Thrown at pytorch_classification/Test7_shufflenet/model.py:93
out = torch.cat((x1, self.branch2(x2)), dim=1)
else:
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 mypyView on GitHub (pinned to 1ec3fe6f37)
Solutions
- Pass exactly 3 repeat counts, e.g. stages_repeats=[4, 8, 4] (1.0x preset).
- Use the provided factory functions ShuffleNetV2_x0_5/x1_0/x1_5/x2_0(num_classes=...) instead of calling ShuffleNetV2 directly.
- Swap arguments if you passed them in the wrong order — repeats is 3 elements, channels is 5.
Example fix
// before model = ShuffleNetV2(stages_repeats=[4, 8, 4, 4], stages_out_channels=[24, 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_repeats) == 3, f"stages_repeats must have 3 ints, got {len(stages_repeats)}"
model = ShuffleNetV2(stages_repeats=stages_repeats, stages_out_channels=stages_out_channels, num_classes=n) Type guard
def valid_repeats(r) -> bool:
return isinstance(r, list) and len(r) == 3 and all(isinstance(v, int) and v > 0 for v in r) Try / catch
try:
model = ShuffleNetV2(stages_repeats=rep, stages_out_channels=ch, num_classes=n)
except ValueError as e:
if "stages_repeats" in str(e):
model = ShuffleNetV2_x1_0(num_classes=n)
else:
raise Prevention
- Prefer ShuffleNetV2_x0_5/x1_0/x1_5/x2_0 factories over direct construction.
- Check argument order: repeats (3) before channels (5).
- Validate list lengths before constructing the model.
When it happens
Trigger: Calling ShuffleNetV2(stages_repeats=..., stages_out_channels=...) with a list of length other than 3 — e.g. [4] , [4, 8], [4, 8, 4, 4], or forgetting the argument so a wrong default/None is used.
Common situations: Building a custom variant of ShuffleNetV2 with extra or fewer stages; misreading the preset tables and copying a 4-element list; passing stages_out_channels by mistake into the stages_repeats slot.
Related errors
- expected stages_out_channels as list of 5 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/3988f7c5e13ec620.
Report an issue: GitHub.