WZMIAOMIAO/deep-learning-for-image-processing · error · ValueError
illegal stride value.
Error message
illegal stride value.
What it means
In MobileNetV3's InvertedResidual block, the stride from InvertedResidualConfig must be 1 or 2. Any other stride raises this ValueError at construction time. MobileNetV3 inverted residual blocks only support single/double downsampling; larger strides are architecturally undefined here.
Source
Thrown at pytorch_segmentation/lraspp/src/mobilenet_backbone.py:101
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
self.dilation = dilation
@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
stride = 1 if cnf.dilation > 1 else cnf.stride
layers.append(ConvBNActivation(cnf.expanded_c,
cnf.expanded_c,View on GitHub (pinned to 1ec3fe6f37)
Solutions
- Set each config's stride to 1 or 2 only
- If you need more downsampling, add more blocks or adjust input image stride via the first ConvBNActivation layer instead
- Re-validate any generated/serialized InvertedResidualConfig list before constructing MobileNetV3
Example fix
// before
cnf = InvertedResidualConfig(16, 3, 64, 64, True, 'RE', 4, 1, 1) # stride=4
// after
cnf = InvertedResidualConfig(16, 3, 64, 64, True, 'RE', 2, 1, 1) # stride in {1,2} Defensive patterns
Strategy: validation
Validate before calling
for cnf in inverted_residual_setting:
assert cnf.stride in (1, 2), f"bad stride {cnf.stride} in {cnf}" Type guard
def strides_valid(cfgs):
return all(getattr(c, 'stride', None) in (1, 2) for c in cfgs) Try / catch
try:
model = MobileNetV3(inverted_residual_setting=cfgs, num_classes=nc)
except ValueError as e:
logging.error("invalid block config: %s", e)
raise SystemExit(1) Prevention
- Only assign stride 1 or 2 to InvertedResidualConfig
- Check positional-arg order when constructing configs manually
- Validate generated configs (e.g. from JSON) before model construction
- Achieve extra downsampling via the stem conv, not block strides
When it happens
Trigger: Hand-building `InvertedResidualConfig(cnf)` with `stride=3` (or 0, 4) and passing to `MobileNetV3(inverted_residual_setting=[cnf,...])`; editing the predefined bneck_cfg list for a custom resolution; loading a config from JSON where stride was wrongly specified.
Common situations: Custom backbone tuning for segmentation (developers try stride=4 to downsample faster); mis-ordered positional args when constructing InvertedResidualConfig manually (width/multiplier vs stride confusion); auto-generated NAS-style configs with unsupported strides.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- The inverted_residual_setting should not be empty.
- illegal stride value.
- return_layers are not present in model
- return_layers are not present in model
- The inverted_residual_setting should be List[InvertedResidua
AI-assisted analysis of WZMIAOMIAO/deep-learning-for-image-processing@1ec3fe6f37 (2026-08-30).
Data as JSON: /api/errors/a4d03ef5914ce7f5.
Report an issue: GitHub.