WZMIAOMIAO/deep-learning-for-image-processing · error · ValueError
illegal stride value.
Error message
illegal stride value.
What it means
InvertedResidual (EfficientNet MBConv block) only supports strides 1 and 2 because shortcut connection logic (use_res_connect) and the downsampling conv layers are designed for those values. Any other stride in the InvertedResidualConfig is rejected in __init__ with a ValueError at model-construction time.
Source
Thrown at pytorch_classification/Test9_efficientNet/model.py:141
self.out_c = self.adjust_channels(out_c, width_coefficient)
self.use_se = use_se
self.stride = stride
self.drop_rate = drop_rate
self.index = index
@staticmethod
def adjust_channels(channels: int, width_coefficient: float):
return _make_divisible(channels * width_coefficient, 8)
class InvertedResidual(nn.Module):
def __init__(self,
cnf: InvertedResidualConfig,
norm_layer: Callable[..., nn.Module]):
super(InvertedResidual, self).__init__()
if cnf.stride not in [1, 2]:
raise ValueError("illegal stride value.")
self.use_res_connect = (cnf.stride == 1 and cnf.input_c == cnf.out_c)
layers = OrderedDict()
activation_layer = nn.SiLU # alias Swish
# expand
if cnf.expanded_c != cnf.input_c:
layers.update({"expand_conv": ConvBNActivation(cnf.input_c,
cnf.expanded_c,
kernel_size=1,
norm_layer=norm_layer,
activation_layer=activation_layer)})
# depthwise
layers.update({"dwconv": ConvBNActivation(cnf.expanded_c,
cnf.expanded_c,
kernel_size=cnf.kernel,View on GitHub (pinned to 1ec3fe6f37)
Solutions
- Set every InvertedResidualConfig stride to 1 or 2 (use stride 2 only on the first block of each stage for downsampling)
- Remove or replace the custom block with a different module if you need stride > 2, e.g. stack two stride-2 blocks or use pooling
- Validate the config table values before constructing the model
Example fix
// before InvertedResidualConfig(input_c, kernel=3, expanded_c, out_c, use_se=True, activation='silu', stride=3) // after InvertedResidualConfig(input_c, kernel=3, expanded_c, out_c, use_se=True, activation='silu', stride=2)
Defensive patterns
Strategy: validation
Validate before calling
for cnf in inverted_residual_setting:
if cnf.stride not in (1, 2):
raise ValueError(f'stride must be 1 or 2, got {cnf.stride}') Type guard
def valid_stride(cnf) -> bool:
return cnf.stride in (1, 2) Try / catch
try:
model = efficientnet(num_classes=5)
except ValueError as e:
if 'illegal stride' in str(e):
print('Fix the stride in your block config table:', e)
raise Prevention
- Only use stride 2 on the first block of a downsampling stage
- Copy stride values from the official EfficientNet paper table (1 or 2 only)
- Unit-test model construction with the shipped default config before customizing
When it happens
Trigger: Building an EfficientNet variant whose _make_layers / configuration table supplies cnf.stride of 3, 4, 0, or a float instead of 1 or 2 when instantiating InvertedResidual.
Common situations: Hand-editing the efficientnet_config list to scale the network; porting block definitions from another architecture (e.g. HRNet strided blocks); typo like stride=22 instead of 2.
Related errors
- illegal stride value.
- expected stages_repeats as list of 3 positive ints
- expected stages_out_channels as list of 5 positive ints
- not support model name: {}
- image: {} isn't RGB mode.
AI-assisted analysis of WZMIAOMIAO/deep-learning-for-image-processing@1ec3fe6f37 (2026-08-30).
Data as JSON: /api/errors/2d66219183788d81.
Report an issue: GitHub.