WZMIAOMIAO/deep-learning-for-image-processing · error · ValueError
replace_stride_with_dilation should be None or a 3-element t
Error message
replace_stride_with_dilation should be None or a 3-element tuple, got {} What it means
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__.
Source
Thrown at pytorch_segmentation/fcn/src/backbone.py:82
class ResNet(nn.Module):
def __init__(self, block, layers, num_classes=1000, zero_init_residual=False,
groups=1, width_per_group=64, replace_stride_with_dilation=None,
norm_layer=None):
super(ResNet, self).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
self._norm_layer = norm_layer
self.inplanes = 64
self.dilation = 1
if replace_stride_with_dilation is None:
# each element in the tuple indicates if we should replace
# the 2x2 stride with a dilated convolution instead
replace_stride_with_dilation = [False, False, False]
if len(replace_stride_with_dilation) != 3:
raise ValueError("replace_stride_with_dilation should be None "
"or a 3-element tuple, got {}".format(replace_stride_with_dilation))
self.groups = groups
self.base_width = width_per_group
self.conv1 = nn.Conv2d(3, self.inplanes, kernel_size=7, stride=2, padding=3,
bias=False)
self.bn1 = norm_layer(self.inplanes)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(block, 64, layers[0])
self.layer2 = self._make_layer(block, 128, layers[1], stride=2,
dilate=replace_stride_with_dilation[0])
self.layer3 = self._make_layer(block, 256, layers[2], stride=2,
dilate=replace_stride_with_dilation[1])
self.layer4 = self._make_layer(block, 512, layers[3], stride=2,
dilate=replace_stride_with_dilation[2])
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(512 * block.expansion, num_classes)
View on GitHub (pinned to 1ec3fe6f37)
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
Example fix
// before backbone = resnet50_fpn_backbone(replace_stride_with_dilation=[True, True]) // after backbone = resnet50_fpn_backbone(replace_stride_with_dilation=(False, True, True))
Defensive patterns
Strategy: validation
Validate before calling
def check_dilation(replace_stride_with_dilation):
assert replace_stride_with_dilation is None or (
len(replace_stride_with_dilation) == 3 and all(isinstance(b, bool) for b in replace_stride_with_dilation))
check_dilation(my_cfg) Type guard
def is_valid_dilation(v):
return v is None or (isinstance(v, (list, tuple)) and len(v) == 3 and all(isinstance(b, bool) for b in v)) Try / catch
try:
backbone = resnet50_fpn_backbone(replace_stride_with_dilation=cfg.dilation)
except ValueError as e:
logging.error("bad dilation config %s: %s", cfg.dilation, e)
backbone = resnet50_fpn_backbone() Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- replace_stride_with_dilation should be None or a 3-element t
- the cfg file not exist...
- Unsupported fields:{} in cfg
- conv2d filter size must be int type.
- return_layers are not present in model
AI-assisted analysis of WZMIAOMIAO/deep-learning-for-image-processing@1ec3fe6f37 (2026-08-30).
Data as JSON: /api/errors/e03a05678ebf58fa.
Report an issue: GitHub.