WZMIAOMIAO/deep-learning-for-image-processing · error · ValueError
illegal stride value.
Error message
illegal stride value.
What it means
MBConv only supports strides 1 or 2, since shortcut downsampling and depthwise conv are built around those two values. Any other stride is rejected at module construction.
Source
Thrown at pytorch_classification/Test11_efficientnetV2/model.py:112
scale = self.conv_expand(scale)
scale = self.act2(scale)
return scale * x
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
- Use stride=1 or stride=2 only.
- To downsample more, stack multiple stride-2 blocks instead of one large-stride block.
- Fix the cfg/stage definition supplying the bad stride value.
Example fix
// before MBConv(input_c=24, out_c=48, stride=3, ...) // after MBConv(input_c=24, out_c=48, stride=2, ...) # chain two stride-2 blocks for /4 downsampling
Defensive patterns
Strategy: validation
Validate before calling
def build_mbconv(cfg):
stride = cfg["stride"]
assert stride in (1, 2), f"MBConv stride must be 1 or 2, got {stride}"
return MBConv(input_c=cfg["in"], out_c=cfg["out"], stride=stride, ...) Type guard
def is_legal_stride(s) -> bool:
return s in (1, 2) Try / catch
try:
block = MBConv(input_c, out_c, stride=s, ...)
except ValueError as e:
print(e)
block = MBConv(input_c, out_c, stride=min(s, 2), ...) Prevention
- Only use strides 1 or 2 in stage configs
- Achieve larger downsampling by stacking stride-2 blocks
- Validate cfg stride fields before constructing the model
When it happens
Trigger: Constructing MBConv with stride=3 or 0, typically from a hand-edited cfg where 'stride' was changed, or a custom stage list passing a wrong stride.
Common situations: Editing the EfficientNetV2 cfg dict to increase downsampling (someone sets stride=3), or copying a block spec from another network with larger 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
- not support data format '{self.data_format}'
- Transformer input dimension should be divisible by head dime
- illegal stride value.
- illegal stride value.
- illegal stride value.
AI-assisted analysis of WZMIAOMIAO/deep-learning-for-image-processing@1ec3fe6f37 (2026-08-30).
Data as JSON: /api/errors/1c9e07ec52116be3.
Report an issue: GitHub.