WZMIAOMIAO/deep-learning-for-image-processing · error · ValueError

illegal stride value.

Error message

illegal stride value.

What it means

The InvertedResidual (MobileNetV3 block) only supports strides of 1 or 2, matching its skip-connection and downsampling design. InvertedResidualConfig-derived cnf.stride outside {1, 2} makes the block unconstructible, so __init__ raises ValueError('illegal stride value.').

Source

Thrown at pytorch_segmentation/deeplab_v3/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

  1. Set each InvertedResidualConfig stride to 1 or 2.
  2. If you need more downsampling, add extra blocks with stride 2 instead of one block with a larger stride.
  3. Validate your custom config list before constructing the model (all strides in [1, 2]).

Example fix

// before
InvertedResidualConfig(16, 3, 64, 64, False, 'RE', 3, 1, 1)
// after
InvertedResidualConfig(16, 3, 64, 64, False, 'RE', 2, 1, 1)
Defensive patterns

Strategy: validation

Validate before calling

assert all(cnf.stride in (1, 2) for cnf in inverted_residual_setting), "stride must be 1 or 2"

Type guard

def has_legal_strides(settings) -> bool:
    return all(getattr(s, 'stride', None) in (1, 2) for s in settings)

Try / catch

try:
    block = InvertedResidual(cnf, norm_layer)
except ValueError as e:
    logging.error(f"{e}; stride={cnf.stride}"); raise

Prevention

When it happens

Trigger: Building InvertedResidual with an InvertedResidualConfig whose stride is 0, 3, or any value other than 1 or 2 — e.g. hand-writing a custom inverted_residual_setting list.

Common situations: Custom/architecture-search configs with stride 3+; copy-paste editing a cnf entry and setting stride incorrectly; porting configs from other networks where larger strides are valid.

Related errors


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