{"record":{"id":"1c9e07ec52116be3","repo":"WZMIAOMIAO/deep-learning-for-image-processing","slug":"illegal-stride-value","errorCode":null,"errorMessage":"illegal stride value.","messagePattern":"illegal stride value\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pytorch_classification/Test11_efficientnetV2/model.py","lineNumber":112,"sourceCode":"        scale = self.conv_expand(scale)\n        scale = self.act2(scale)\n        return scale * x\n\n\nclass MBConv(nn.Module):\n    def __init__(self,\n                 kernel_size: int,\n                 input_c: int,\n                 out_c: int,\n                 expand_ratio: int,\n                 stride: int,\n                 se_ratio: float,\n                 drop_rate: float,\n                 norm_layer: Callable[..., nn.Module]):\n        super(MBConv, self).__init__()\n\n        if stride not in [1, 2]:\n            raise ValueError(\"illegal stride value.\")\n\n        self.has_shortcut = (stride == 1 and input_c == out_c)\n\n        activation_layer = nn.SiLU  # alias Swish\n        expanded_c = input_c * expand_ratio\n\n        # 在EfficientNetV2中，MBConv中不存在expansion=1的情况所以conv_pw肯定存在\n        assert expand_ratio != 1\n        # Point-wise expansion\n        self.expand_conv = ConvBNAct(input_c,\n                                     expanded_c,\n                                     kernel_size=1,\n                                     norm_layer=norm_layer,\n                                     activation_layer=activation_layer)\n\n        # Depth-wise convolution\n        self.dwconv = ConvBNAct(expanded_c,\n                                expanded_c,","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/WZMIAOMIAO/deep-learning-for-image-processing/blob/1ec3fe6f374fc9969973a61f819de25658595afa/pytorch_classification/Test11_efficientnetV2/model.py#L94-L130","documentation":"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.","triggerScenarios":"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.","commonSituations":"Editing the EfficientNetV2 cfg dict to increase downsampling (someone sets stride=3), or copying a block spec from another network with larger strides.","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."],"exampleFix":"// before\nMBConv(input_c=24, out_c=48, stride=3, ...)\n// after\nMBConv(input_c=24, out_c=48, stride=2, ...)  # chain two stride-2 blocks for /4 downsampling","handlingStrategy":"validation","validationCode":"def build_mbconv(cfg):\n    stride = cfg[\"stride\"]\n    assert stride in (1, 2), f\"MBConv stride must be 1 or 2, got {stride}\"\n    return MBConv(input_c=cfg[\"in\"], out_c=cfg[\"out\"], stride=stride, ...)","typeGuard":"def is_legal_stride(s) -> bool:\n    return s in (1, 2)","tryCatchPattern":"try:\n    block = MBConv(input_c, out_c, stride=s, ...)\nexcept ValueError as e:\n    print(e)\n    block = MBConv(input_c, out_c, stride=min(s, 2), ...)","preventionTips":["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"],"tags":["pytorch","conv","config-validation","stride"],"backgroundTag":"invalid-argument-value","analyzedSha":"1ec3fe6f374fc9969973a61f819de25658595afa","analyzedAt":"2026-08-30T09:19:11.901Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}