{"record":{"id":"e03a05678ebf58fa","repo":"WZMIAOMIAO/deep-learning-for-image-processing","slug":"replace-stride-with-dilation-should-be-none-or-a-3-e03a05","errorCode":null,"errorMessage":"replace_stride_with_dilation should be None or a 3-element tuple, got {}","messagePattern":"replace_stride_with_dilation should be None or a 3-element tuple, got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pytorch_segmentation/fcn/src/backbone.py","lineNumber":82,"sourceCode":"\nclass ResNet(nn.Module):\n\n    def __init__(self, block, layers, num_classes=1000, zero_init_residual=False,\n                 groups=1, width_per_group=64, replace_stride_with_dilation=None,\n                 norm_layer=None):\n        super(ResNet, self).__init__()\n        if norm_layer is None:\n            norm_layer = nn.BatchNorm2d\n        self._norm_layer = norm_layer\n\n        self.inplanes = 64\n        self.dilation = 1\n        if replace_stride_with_dilation is None:\n            # each element in the tuple indicates if we should replace\n            # the 2x2 stride with a dilated convolution instead\n            replace_stride_with_dilation = [False, False, False]\n        if len(replace_stride_with_dilation) != 3:\n            raise ValueError(\"replace_stride_with_dilation should be None \"\n                             \"or a 3-element tuple, got {}\".format(replace_stride_with_dilation))\n        self.groups = groups\n        self.base_width = width_per_group\n        self.conv1 = nn.Conv2d(3, self.inplanes, kernel_size=7, stride=2, padding=3,\n                               bias=False)\n        self.bn1 = norm_layer(self.inplanes)\n        self.relu = nn.ReLU(inplace=True)\n        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)\n        self.layer1 = self._make_layer(block, 64, layers[0])\n        self.layer2 = self._make_layer(block, 128, layers[1], stride=2,\n                                       dilate=replace_stride_with_dilation[0])\n        self.layer3 = self._make_layer(block, 256, layers[2], stride=2,\n                                       dilate=replace_stride_with_dilation[1])\n        self.layer4 = self._make_layer(block, 512, layers[3], stride=2,\n                                       dilate=replace_stride_with_dilation[2])\n        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))\n        self.fc = nn.Linear(512 * block.expansion, num_classes)\n","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/WZMIAOMIAO/deep-learning-for-image-processing/blob/1ec3fe6f374fc9969973a61f819de25658595afa/pytorch_segmentation/fcn/src/backbone.py#L64-L100","documentation":"ResNet backbone construction validates the `replace_stride_with_dilation` argument. It may be None (defaults to [False, False, False]) or a sequence of exactly 3 booleans — one per layer2/3/4 stride block. Passing anything whose length is not 3 raises this ValueError immediately in `_make_layer` setup of ResNet.__init__.","triggerScenarios":"Passing `replace_stride_with_dilation=[True, True]` or `[True]`; passing a tuple with more than 3 elements; passing an empty list; copying torchvision code that used a different number of stages.","commonSituations":"Building dilated FCN/DeepLab backbones where developers enable dilation on some stages but forget the third element; typos when copying torchvision ResNet examples; configuring a 2-stage custom ResNet variant but reusing 3-element validation.","solutions":["Pass exactly a 3-element tuple/list of booleans, e.g. (False, True, True)","Or pass None to use default strides everywhere","Verify each boolean maps to layer2/layer3/layer4 stride replacement in order"],"exampleFix":"// before\nbackbone = resnet50_fpn_backbone(replace_stride_with_dilation=[True, True])\n// after\nbackbone = resnet50_fpn_backbone(replace_stride_with_dilation=(False, True, True))","handlingStrategy":"validation","validationCode":"def check_dilation(replace_stride_with_dilation):\n    assert replace_stride_with_dilation is None or (\n        len(replace_stride_with_dilation) == 3 and all(isinstance(b, bool) for b in replace_stride_with_dilation))\ncheck_dilation(my_cfg)","typeGuard":"def is_valid_dilation(v):\n    return v is None or (isinstance(v, (list, tuple)) and len(v) == 3 and all(isinstance(b, bool) for b in v))","tryCatchPattern":"try:\n    backbone = resnet50_fpn_backbone(replace_stride_with_dilation=cfg.dilation)\nexcept ValueError as e:\n    logging.error(\"bad dilation config %s: %s\", cfg.dilation, e)\n    backbone = resnet50_fpn_backbone()","preventionTips":["Use None for default strides; only pass 3 booleans when doing dilation","Keep the tuple literal (False, True, True) inline for clarity","Validate config files at startup","Match element count to the backbone's number of stages"],"tags":["python","value-error","resnet","dilation","config"],"backgroundTag":"invalid-argument-value","analyzedSha":"1ec3fe6f374fc9969973a61f819de25658595afa","analyzedAt":"2026-08-30T09:19:11.901Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}