{"record":{"id":"2d66219183788d81","repo":"WZMIAOMIAO/deep-learning-for-image-processing","slug":"illegal-stride-value-2d6621","errorCode":null,"errorMessage":"illegal stride value.","messagePattern":"illegal stride value\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pytorch_classification/Test9_efficientNet/model.py","lineNumber":141,"sourceCode":"        self.out_c = self.adjust_channels(out_c, width_coefficient)\n        self.use_se = use_se\n        self.stride = stride\n        self.drop_rate = drop_rate\n        self.index = index\n\n    @staticmethod\n    def adjust_channels(channels: int, width_coefficient: float):\n        return _make_divisible(channels * width_coefficient, 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 = OrderedDict()\n        activation_layer = nn.SiLU  # alias Swish\n\n        # expand\n        if cnf.expanded_c != cnf.input_c:\n            layers.update({\"expand_conv\": 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.update({\"dwconv\": ConvBNActivation(cnf.expanded_c,\n                                                  cnf.expanded_c,\n                                                  kernel_size=cnf.kernel,","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/WZMIAOMIAO/deep-learning-for-image-processing/blob/1ec3fe6f374fc9969973a61f819de25658595afa/pytorch_classification/Test9_efficientNet/model.py#L123-L159","documentation":"InvertedResidual (EfficientNet MBConv block) only supports strides 1 and 2 because shortcut connection logic (use_res_connect) and the downsampling conv layers are designed for those values. Any other stride in the InvertedResidualConfig is rejected in __init__ with a ValueError at model-construction time.","triggerScenarios":"Building an EfficientNet variant whose _make_layers / configuration table supplies cnf.stride of 3, 4, 0, or a float instead of 1 or 2 when instantiating InvertedResidual.","commonSituations":"Hand-editing the efficientnet_config list to scale the network; porting block definitions from another architecture (e.g. HRNet strided blocks); typo like stride=22 instead of 2.","solutions":["Set every InvertedResidualConfig stride to 1 or 2 (use stride 2 only on the first block of each stage for downsampling)","Remove or replace the custom block with a different module if you need stride > 2, e.g. stack two stride-2 blocks or use pooling","Validate the config table values before constructing the model"],"exampleFix":"// before\nInvertedResidualConfig(input_c, kernel=3, expanded_c, out_c, use_se=True, activation='silu', stride=3)\n// after\nInvertedResidualConfig(input_c, kernel=3, expanded_c, out_c, use_se=True, activation='silu', stride=2)","handlingStrategy":"validation","validationCode":"for cnf in inverted_residual_setting:\n    if cnf.stride not in (1, 2):\n        raise ValueError(f'stride must be 1 or 2, got {cnf.stride}')","typeGuard":"def valid_stride(cnf) -> bool:\n    return cnf.stride in (1, 2)","tryCatchPattern":"try:\n    model = efficientnet(num_classes=5)\nexcept ValueError as e:\n    if 'illegal stride' in str(e):\n        print('Fix the stride in your block config table:', e)\n    raise","preventionTips":["Only use stride 2 on the first block of a downsampling stage","Copy stride values from the official EfficientNet paper table (1 or 2 only)","Unit-test model construction with the shipped default config before customizing"],"tags":["pytorch","efficientnet","valueerror","model-config"],"backgroundTag":"illegal-stride-value","analyzedSha":"1ec3fe6f374fc9969973a61f819de25658595afa","analyzedAt":"2026-08-30T09:19:11.901Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}