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

  1. Set every block's stride to 1 or 2 in the config.
  2. Stride 1 requires input_c == out_c for a shortcut; use stride 2 when changing channels/downsampling.
  3. 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

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


AI-assisted analysis of WZMIAOMIAO/deep-learning-for-image-processing@1ec3fe6f37 (2026-08-30). Data as JSON: /api/errors/822432989f748c40. Report an issue: GitHub.