{"record":{"id":"a4d03ef5914ce7f5","repo":"WZMIAOMIAO/deep-learning-for-image-processing","slug":"illegal-stride-value-a4d03e","errorCode":null,"errorMessage":"illegal stride value.","messagePattern":"illegal stride value\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pytorch_segmentation/lraspp/src/mobilenet_backbone.py","lineNumber":101,"sourceCode":"        self.out_c = self.adjust_channels(out_c, width_multi)\n        self.use_se = use_se\n        self.use_hs = activation == \"HS\"  # whether using h-swish activation\n        self.stride = stride\n        self.dilation = dilation\n\n    @staticmethod\n    def adjust_channels(channels: int, width_multi: float):\n        return _make_divisible(channels * width_multi, 8)\n\n\nclass InvertedResidual(nn.Module):\n    def __init__(self,\n                 cnf: InvertedResidualConfig,\n                 norm_layer: Callable[..., nn.Module]):\n        super(InvertedResidual, self).__init__()\n\n        if cnf.stride not in [1, 2]:\n            raise ValueError(\"illegal stride value.\")\n\n        self.use_res_connect = (cnf.stride == 1 and cnf.input_c == cnf.out_c)\n\n        layers: List[nn.Module] = []\n        activation_layer = nn.Hardswish if cnf.use_hs else nn.ReLU\n\n        # expand\n        if cnf.expanded_c != cnf.input_c:\n            layers.append(ConvBNActivation(cnf.input_c,\n                                           cnf.expanded_c,\n                                           kernel_size=1,\n                                           norm_layer=norm_layer,\n                                           activation_layer=activation_layer))\n\n        # depthwise\n        stride = 1 if cnf.dilation > 1 else cnf.stride\n        layers.append(ConvBNActivation(cnf.expanded_c,\n                                       cnf.expanded_c,","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/WZMIAOMIAO/deep-learning-for-image-processing/blob/1ec3fe6f374fc9969973a61f819de25658595afa/pytorch_segmentation/lraspp/src/mobilenet_backbone.py#L83-L119","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\ncnf = InvertedResidualConfig(16, 3, 64, 64, True, 'RE', 4, 1, 1)  # stride=4\n// after\ncnf = InvertedResidualConfig(16, 3, 64, 64, True, 'RE', 2, 1, 1)  # stride in {1,2}","handlingStrategy":"validation","validationCode":"for cnf in inverted_residual_setting:\n    assert cnf.stride in (1, 2), f\"bad stride {cnf.stride} in {cnf}\"","typeGuard":"def strides_valid(cfgs):\n    return all(getattr(c, 'stride', None) in (1, 2) for c in cfgs)","tryCatchPattern":"try:\n    model = MobileNetV3(inverted_residual_setting=cfgs, num_classes=nc)\nexcept ValueError as e:\n    logging.error(\"invalid block config: %s\", e)\n    raise SystemExit(1)","preventionTips":["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"],"tags":["python","value-error","mobilenet","stride","backbone"],"backgroundTag":"invalid-argument-value","analyzedSha":"1ec3fe6f374fc9969973a61f819de25658595afa","analyzedAt":"2026-08-30T09:19:11.901Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}