WZMIAOMIAO/deep-learning-for-image-processing · error · ValueError
return_layers are not present in model
Error message
return_layers are not present in model
What it means
IntermediateLayerGetter wraps a backbone and extracts intermediate feature maps whose names are given in return_layers. In __init__ it verifies that every requested layer key exists among model.named_children(); otherwise it raises ValueError, since there is no submodule by that name to return.
Source
Thrown at pytorch_segmentation/deeplab_v3/src/deeplabv3_model.py:39
Additionally, it is only able to query submodules that are directly
assigned to the model. So if `model` is passed, `model.feature1` can
be returned, but not `model.feature1.layer2`.
Args:
model (nn.Module): model on which we will extract the features
return_layers (Dict[name, new_name]): a dict containing the names
of the modules for which the activations will be returned as
the key of the dict, and the value of the dict is the name
of the returned activation (which the user can specify).
"""
_version = 2
__annotations__ = {
"return_layers": Dict[str, str],
}
def __init__(self, model: nn.Module, return_layers: Dict[str, str]) -> None:
if not set(return_layers).issubset([name for name, _ in model.named_children()]):
raise ValueError("return_layers are not present in model")
orig_return_layers = return_layers
return_layers = {str(k): str(v) for k, v in return_layers.items()}
# 重新构建backbone,将没有使用到的模块全部删掉
layers = OrderedDict()
for name, module in model.named_children():
layers[name] = module
if name in return_layers:
del return_layers[name]
if not return_layers:
break
super(IntermediateLayerGetter, self).__init__(layers)
self.return_layers = orig_return_layers
def forward(self, x: Tensor) -> Dict[str, Tensor]:
out = OrderedDict()
for name, module in self.items():View on GitHub (pinned to 1ec3fe6f37)
Solutions
- Print [name for name, _ in model.named_children()] and make return_layers keys exactly match one of those names.
- Use the return_layers dict appropriate for the chosen backbone (e.g. {'layer4': '0'} for ResNet-50, matching mobilenet's block names otherwise).
- If you need deeper/nested feature maps, restructure the model or wrap the sub-block rather than naming a nested path.
Example fix
// before
return_layers = {'features.6': '0'}
backbone = IntermediateLayerGetter(mobilenet, return_layers)
// after
return_layers = {'features': '0'}
backbone = IntermediateLayerGetter(mobilenet, return_layers) Defensive patterns
Strategy: validation
Validate before calling
children = {name for name, _ in model.named_children()}
missing = set(return_layers) - children
assert not missing, f"layers not in model: {missing}; have: {children}" Type guard
def layers_exist(model, return_layers: dict) -> bool:
names = {n for n, _ in model.named_children()}
return set(return_layers).issubset(names) Try / catch
try:
backbone = IntermediateLayerGetter(model, return_layers)
except ValueError as e:
logging.error(f"{e}; available: {[n for n, _ in model.named_children()]}"); raise Prevention
- Print model.named_children() when switching backbones
- Keep a per-backbone return_layers mapping table
- Remember only top-level children are matched, not nested paths
When it happens
Trigger: Constructing IntermediateLayerGetter(model, return_layers) with a dict key that does not match any direct child module name of the model, e.g. {'layer4': '0'} on a model whose blocks are named differently.
Common situations: Swapping backbones (resnet vs mobilenet) without updating return_layers keys; typo like 'layer3' vs 'layers.3'; expecting nested module paths when only top-level children are matched.
Related errors
- return_layers are not present in model
- return_layers are not present in model
- backbone should contain an attribute out_channelsspecifying
- Unknown iou type {}
- illegal stride value.
AI-assisted analysis of WZMIAOMIAO/deep-learning-for-image-processing@1ec3fe6f37 (2026-08-30).
Data as JSON: /api/errors/16883d2a432244f0.
Report an issue: GitHub.