{"record":{"id":"68356211b8462749","repo":"WZMIAOMIAO/deep-learning-for-image-processing","slug":"replace-stride-with-dilation-should-be-none-or-a-3","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/deeplab_v3/src/resnet_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/deeplab_v3/src/resnet_backbone.py#L64-L100","documentation":"ResNet supports replacing the 2x2 stride of stages 3-5 with dilation, controlled by a 3-element boolean sequence (one flag per stage). If replace_stride_with_dilation is provided but its length is not 3, __init__ raises ValueError, since the stage count is fixed.","triggerScenarios":"Calling resnet50/101/... (or _make_layer-driven constructors) with replace_stride_with_dilation of length != 3, e.g. [True] or a 4-element list, and not None.","commonSituations":"Configuring output_stride for DeepLab and getting the dilation flags wrong; using a list built conditionally that omitted entries; confusing it with other frameworks' per-layer dilation settings.","solutions":["Always pass exactly a 3-element list/tuple of booleans, e.g. (False, True, True), or pass None for the default strided behavior.","Check DeepLab config: typically replace_stride_with_dilation=(False, False, True) for output_stride=16 variants.","Normalize your config to a list and assert len == 3 before calling the model constructor."],"exampleFix":"// before\nmodel = resnet50(replace_stride_with_dilation=[True])\n// after\nmodel = resnet50(replace_stride_with_dilation=[False, False, True])","handlingStrategy":"validation","validationCode":"if replace_stride_with_dilation is not None:\n    assert len(replace_stride_with_dilation) == 3, \"need exactly 3 stage flags\"","typeGuard":"def is_valid_dilation_flag(flags) -> bool:\n    return flags is None or (hasattr(flags, '__len__') and len(flags) == 3)","tryCatchPattern":"try:\n    model = resnet50(replace_stride_with_dilation=flags)\nexcept ValueError as e:\n    logging.error(f\"{e}\"); raise","preventionTips":["Default to None unless intentionally using dilated output_stride","Define dilation flags as a constant tuple (False, False, True) etc.","Sanity-check output stride after model construction"],"tags":["value-error","resnet","dilation","configuration"],"backgroundTag":"invalid-dilation-configuration","analyzedSha":"1ec3fe6f374fc9969973a61f819de25658595afa","analyzedAt":"2026-08-30T09:19:11.901Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}