{"record":{"id":"48ea0acf9f3f0008","repo":"WZMIAOMIAO/deep-learning-for-image-processing","slug":"illegal-stride-value-48ea0a","errorCode":null,"errorMessage":"illegal stride value.","messagePattern":"illegal stride value\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pytorch_classification/Test6_mobilenet/model_v3.py","lineNumber":96,"sourceCode":"        self.expanded_c = self.adjust_channels(expanded_c, width_multi)\n        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\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        layers.append(ConvBNActivation(cnf.expanded_c,\n                                       cnf.expanded_c,\n                                       kernel_size=cnf.kernel,","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/WZMIAOMIAO/deep-learning-for-image-processing/blob/1ec3fe6f374fc9969973a61f819de25658595afa/pytorch_classification/Test6_mobilenet/model_v3.py#L78-L114","documentation":"InvertedResidual.__init__ (MobileNetV3) validates that the inverted residual config's stride is either 1 or 2 and raises ValueError otherwise. Strides other than 1/2 are not implemented: the block only supports identity/shortcut (stride 1) or strided downsampling with stride 2.","triggerScenarios":"Constructing an InvertedResidualConfig with stride set to 3, 0, or any value outside [1, 2] and then instantiating InvertedResidual(cnf, norm_layer) — typically via a hand-written inverted_residual_setting passed to MobileNetV3.","commonSituations":"Users customizing the network architecture by editing the bneck configuration list and changing a stride for stronger downsampling; copying configs between MobileNet versions where some blocks allow different strides.","solutions":["Set every InvertedResidualConfig stride to 1 or 2 in your configuration list.","To downsample more aggressively, insert additional stride-2 blocks instead of using stride > 2.","If using the built-in model_name presets ('large'/'small'), don't modify the generated bneck_conf calls."],"exampleFix":"// before\nInvertedResidualConfig(16, 3, 24, 24, False, \"RE\", 3, 1, 1)\n// after\nInvertedResidualConfig(16, 3, 24, 24, False, \"RE\", 2, 1, 1)","handlingStrategy":"validation","validationCode":"strides = [cfg.stride for cfg in inverted_residual_setting]\nassert all(s in (1, 2) for s in strides), f\"illegal strides: {strides}\"","typeGuard":"def valid_stride(cnf) -> bool:\n    return getattr(cnf, 'stride', None) in (1, 2)","tryCatchPattern":"try:\n    block = InvertedResidual(cnf, norm_layer)\nexcept ValueError as e:\n    if \"illegal stride\" in str(e):\n        cnf.stride = 2 if cnf.stride > 1 else 1\n        block = InvertedResidual(cnf, norm_layer)\n    else:\n        raise","preventionTips":["Only modify strides in the preset bneck configs if you know the downsampling budget.","Validate all InvertedResidualConfig values before constructing the model.","Prefer the MobileNetV3Large/MobileNetV3Small factory helpers."],"tags":["pytorch","mobilenetv3","config","valueerror","stride"],"backgroundTag":"illegal-stride-value","analyzedSha":"1ec3fe6f374fc9969973a61f819de25658595afa","analyzedAt":"2026-08-30T09:19:11.901Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}