WZMIAOMIAO/deep-learning-for-image-processing · error · ValueError
illegal stride value.
Error message
illegal stride value.
What it means
EfficientNet's MBConv block supports only stride 1 or 2 (identity or downsampling); the constructor validates the stride parameter and raises ValueError for anything else. The stride determines whether a shortcut connection and pooling/strided conv are built, so other values are unsupported.
Source
Thrown at pytorch_classification/model_complexity/model.py:146
cx["h"], cx["w"] = h, w
return cx
class MBConv(nn.Module):
def __init__(self,
kernel_size: int,
input_c: int,
out_c: int,
expand_ratio: int,
stride: int,
se_ratio: float,
drop_rate: float,
norm_layer: Callable[..., nn.Module]):
super(MBConv, self).__init__()
if stride not in [1, 2]:
raise ValueError("illegal stride value.")
self.has_shortcut = (stride == 1 and input_c == out_c)
activation_layer = nn.SiLU # alias Swish
expanded_c = input_c * expand_ratio
# 在EfficientNetV2中,MBConv中不存在expansion=1的情况所以conv_pw肯定存在
assert expand_ratio != 1
# Point-wise expansion
self.expand_conv = ConvBNAct(input_c,
expanded_c,
kernel_size=1,
norm_layer=norm_layer,
activation_layer=activation_layer)
# Depth-wise convolution
self.dwconv = ConvBNAct(expanded_c,
expanded_c,View on GitHub (pinned to 1ec3fe6f37)
Solutions
- Set every block's stride to 1 or 2 in the config.
- Stride 1 requires input_c == out_c for a shortcut; use stride 2 when changing channels/downsampling.
- Copy the canonical B0-B7 configs (e.g. [[1,16,1,1],[6,24,2,2],[6,40,2,2],...]) rather than editing stride fields ad hoc.
Example fix
// before
params = [[1, 16, 1, 3], [6, 24, 2, 3], ...] # stride 3 in first block
// after
params = [[1, 16, 1, 3], [6, 24, 2, 3], ...] # strides must be 1 or 2 only; fix [k_c, out_c, s, n]: stride s in {1,2} Defensive patterns
Strategy: validation
Validate before calling
strides = [p[2] for p in block_params]
assert all(s in (1, 2) for s in strides), f"block strides must be 1 or 2, got {strides}" Type guard
def is_legal_stride(s) -> bool:
return s in (1, 2) Try / catch
try:
block = MBConv(input_c=input_c, out_c=out_c, stride=stride, expand_ratio=e, se_ratio=se, drop_rate=dr, norm_layer=nn.BatchNorm2d)
except ValueError as e:
logging.error("MBConv config error: %s", e)
raise Prevention
- Only use 1 (keep channels, optional shortcut) or 2 (downsample) for block strides
- Copy canonical EfficientNet B0-B7 config tables
- Type-check configs so stride is an int, not a float/string
- Remember stride-1 shortcut requires input_c == out_c
When it happens
Trigger: Constructing MBConv (or an EfficientNet variant whose block config contains a stride) with a stride value outside [1, 2], e.g. 0, 3, or a float from a malformed config.
Common situations: Hand-editing the stage/block config list, importing stride settings from another architecture, or a typo when defining a custom EfficientNet variant.
Related errors
- illegal stride value.
- expected stages_repeats as list of 3 positive ints
- expected stages_out_channels as list of 5 positive ints
- 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/822432989f748c40.
Report an issue: GitHub.