{"record":{"id":"8f5aa934f80a1e35","repo":"WZMIAOMIAO/deep-learning-for-image-processing","slug":"illegal-stride-value-8f5aa9","errorCode":null,"errorMessage":"illegal stride value.","messagePattern":"illegal stride value\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"tensorflow_classification/Test11_efficientnetV2/model.py","lineNumber":77,"sourceCode":"        se_tensor = self.se_reduce(se_tensor)\n        se_tensor = self.se_expand(se_tensor)\n        return se_tensor * inputs\n\n\nclass MBConv(layers.Layer):\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 = 0.25,\n                 drop_rate: float = 0.,\n                 name: str = None):\n        super(MBConv, self).__init__(name=name)\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        expanded_c = input_c * expand_ratio\n\n        bid = itertools.count(0)\n        get_norm_name = lambda: 'batch_normalization' + ('' if not next(\n            bid) else '_' + str(next(bid) // 2))\n        cid = itertools.count(0)\n        get_conv_name = lambda: 'conv2d' + ('' if not next(cid) else '_' + str(\n            next(cid) // 2))\n\n        # 在EfficientNetV2中，MBConv中不存在expansion=1的情况所以conv_pw肯定存在\n        assert expand_ratio != 1\n        # Point-wise expansion\n        self.expand_conv = layers.Conv2D(\n            filters=expanded_c,\n            kernel_size=1,\n            strides=1,","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/WZMIAOMIAO/deep-learning-for-image-processing/blob/1ec3fe6f374fc9969973a61f819de25658595afa/tensorflow_classification/Test11_efficientnetV2/model.py#L59-L95","documentation":"EfficientNetV2's MBConv block only supports strides 1 or 2 (needed for the residual shortcut / downsampling logic). The constructor raises ValueError if a stride outside {1,2} is passed. All block construction in model.py assumes this constraint.","triggerScenarios":"Building MBConv with stride=3, 0, or any non-{1,2} integer, typically from a hand-edited architecture config or wrong parameter order (e.g. passing expand_ratio positionally where stride is expected).","commonSituations":"Custom EfficientNetV2 configs copied from papers with stride-3 downsampling, positional-arg mistakes when instantiating MBConv, or editing the model variants dict with unsupported strides.","solutions":["Use only stride=1 or stride=2 in MBConv definitions","Check positional argument order: (input_c, out_c, kernel_size, stride, expand_ratio, ...)","If larger downsampling is needed, stack multiple stride-2 blocks instead"],"exampleFix":"// before\nMBConv(24, 24, kernel_size=3, stride=3, expand_ratio=4)\n// after\nMBConv(24, 24, kernel_size=3, stride=2, expand_ratio=4)","handlingStrategy":"validation","validationCode":"def build_mbconv(**cfg):\n    assert cfg.get('stride', 1) in (1, 2), f\"MBConv stride must be 1 or 2, got {cfg.get('stride')}\"\n    return MBConv(**cfg)","typeGuard":"def stride_valid(stride) -> bool:\n    return isinstance(stride, int) and stride in (1, 2)","tryCatchPattern":"try:\n    block = MBConv(input_c, out_c, kernel_size, stride, expand_ratio)\nexcept ValueError as e:\n    print(f\"bad block config: {e}\"); raise","preventionTips":["Keep strides in {1,2} in any custom model_config dicts","Use keyword args to avoid positional mixups between stride and expand_ratio","Stack stride-2 blocks instead of one large stride"],"tags":["python","valueerror","tensorflow","efficientnet"],"backgroundTag":"invalid-stride-value","analyzedSha":"1ec3fe6f374fc9969973a61f819de25658595afa","analyzedAt":"2026-08-30T09:19:11.901Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}