WZMIAOMIAO/deep-learning-for-image-processing · error · ValueError
illegal stride value.
Error message
illegal stride value.
What it means
InvertedResidual.__init__ (MobileNetV3) validates that the inverted residual config's stride is either 1 or 2 and raises ValueError otherwise. Strides other than 1/2 are not implemented: the block only supports identity/shortcut (stride 1) or strided downsampling with stride 2.
Source
Thrown at pytorch_classification/Test6_mobilenet/model_v3.py:96
self.expanded_c = self.adjust_channels(expanded_c, width_multi)
self.out_c = self.adjust_channels(out_c, width_multi)
self.use_se = use_se
self.use_hs = activation == "HS" # whether using h-swish activation
self.stride = stride
@staticmethod
def adjust_channels(channels: int, width_multi: float):
return _make_divisible(channels * width_multi, 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: List[nn.Module] = []
activation_layer = nn.Hardswish if cnf.use_hs else nn.ReLU
# expand
if cnf.expanded_c != cnf.input_c:
layers.append(ConvBNActivation(cnf.input_c,
cnf.expanded_c,
kernel_size=1,
norm_layer=norm_layer,
activation_layer=activation_layer))
# depthwise
layers.append(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 in your configuration list.
- To downsample more aggressively, insert additional stride-2 blocks instead of using stride > 2.
- If using the built-in model_name presets ('large'/'small'), don't modify the generated bneck_conf calls.
Example fix
// before InvertedResidualConfig(16, 3, 24, 24, False, "RE", 3, 1, 1) // after InvertedResidualConfig(16, 3, 24, 24, False, "RE", 2, 1, 1)
Defensive patterns
Strategy: validation
Validate before calling
strides = [cfg.stride for cfg in inverted_residual_setting]
assert all(s in (1, 2) for s in strides), f"illegal strides: {strides}" Type guard
def valid_stride(cnf) -> bool:
return getattr(cnf, 'stride', None) in (1, 2) Try / catch
try:
block = InvertedResidual(cnf, norm_layer)
except ValueError as e:
if "illegal stride" in str(e):
cnf.stride = 2 if cnf.stride > 1 else 1
block = InvertedResidual(cnf, norm_layer)
else:
raise Prevention
- Only modify strides in the preset bneck configs if you know the downsampling budget.
- Validate all InvertedResidualConfig values before constructing the model.
- Prefer the MobileNetV3Large/MobileNetV3Small factory helpers.
When it happens
Trigger: Constructing an InvertedResidualConfig with stride set to 3, 0, or any value outside [1, 2] and then instantiating InvertedResidual(cnf, norm_layer) — typically via a hand-written inverted_residual_setting passed to MobileNetV3.
Common situations: Users customizing the network architecture by editing the bneck configuration list and changing a stride for stronger downsampling; copying configs between MobileNet versions where some blocks allow different strides.
Related errors
- The inverted_residual_setting should not be empty.
- The inverted_residual_setting should be List[InvertedResidua
- illegal stride value.
- expected stages_repeats as list of 3 positive ints
- expected stages_out_channels as list of 5 positive ints
AI-assisted analysis of WZMIAOMIAO/deep-learning-for-image-processing@1ec3fe6f37 (2026-08-30).
Data as JSON: /api/errors/48ea0acf9f3f0008.
Report an issue: GitHub.