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's __init__ validates its architecture hyperparameters before building layers. The stages_repeats argument must contain exactly 3 repeat counts (one per stage); passing a list of any other length makes the model structure ambiguous, so a ValueError is thrown immediately.
Source
Thrown at pytorch_classification/mini_imagenet/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] for shufflenet_v2_x1_0.
- Check the source of your config (YAML/JSON/CLI) for extra or missing entries.
- Copy the canonical values from a reference variant (0.5x: [4,8,4]; 1.0x: [4,8,4]; 1.5x: [4,8,4]; 2.0x: [4,8,4]) instead of inventing them.
Example fix
// before model = ShuffleNetV2(stages_repeats=[4, 8], stages_out_channels=[24, 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_repeats) == 3, f"stages_repeats must have 3 entries, got {len(stages_repeats)}"
assert all(isinstance(r, int) and r > 0 for r in stages_repeats), "all repeats must be positive ints" Type guard
def is_valid_stages_repeats(v) -> bool:
return isinstance(v, (list, tuple)) and len(v) == 3 and all(isinstance(r, int) and r > 0 for r 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 config: %s", e)
raise Prevention
- Keep architecture hyperparameters in a single validated config file
- Copy canonical variant presets instead of hand-writing lists
- Validate list lengths and element types at config load time
When it happens
Trigger: Calling ShuffleNetV2(stages_repeats=[...], stages_out_channels=[...]) with a stages_repeats list whose len() != 3, e.g. [2, 4] or [2, 4, 8, 4].
Common situations: Hand-editing a config dict, copy-pasting a variant from another model family, or loading YAML/JSON hyperparameters where an extra element was added or one was omitted.
Related errors
- expected stages_out_channels as list of 5 positive ints
- illegal stride value.
- illegal stride value.
- image: {} isn't RGB mode.
- dataset have {} classes, but input {}
AI-assisted analysis of WZMIAOMIAO/deep-learning-for-image-processing@1ec3fe6f37 (2026-08-30).
Data as JSON: /api/errors/85bca32e93c90beb.
Report an issue: GitHub.