Overview
IntermediateLayerGetter extracts intermediate feature maps from a neural network model by intercepting activations at specified layers. It inherits from nn.ModuleDict and provides a clean interface for returning feature maps from named internal layers, originally derived from torchvision internals.
Description
The IntermediateLayerGetter class wraps an existing neural network model and intercepts activations from specified intermediate layers during the forward pass. It is used in segmentation architectures (such as DeepLabV3) to extract backbone feature maps that are then fed into the segmentation head.
Key Responsibilities
- Layer Validation: During
__init__, validates that all requested return_layers exist in the source model
- Module Pruning: Only keeps modules up to the last requested return layer, discarding deeper layers
- OrderedDict Construction: Creates an ordered mapping from original layer names to output keys
- Feature Extraction: During
forward, passes input through modules sequentially and captures specified layer activations
Code Reference
Source Location
| File |
Lines |
Repository
|
examples/image_segmenter/deeplabv3/intermediate_layer_getter.py |
L1-64 |
pytorch/serve
|
Key Class
class IntermediateLayerGetter(nn.ModuleDict):
"""
Extract intermediate feature maps from a model.
Lines 6-64.
Inherits from nn.ModuleDict to store only the necessary
modules and provide dictionary-like access.
"""
def __init__(self, model, return_layers):
"""
Initialize with a model and the layers to return.
Validates that all keys in return_layers exist as named
children of the model. Creates an OrderedDict of modules
up to and including the last requested layer.
Parameters:
model (nn.Module): The source model to extract layers from.
return_layers (dict): Mapping of {layer_name: output_key}.
Raises:
ValueError: If a requested layer name is not found in the model.
"""
...
def forward(self, x):
"""
Pass input through modules and return specified layer activations.
Iterates through the stored modules in order, passing the input
through each one. When a module's name matches a requested
return layer, its output is stored in the result OrderedDict.
Parameters:
x (torch.Tensor): Input tensor.
Returns:
OrderedDict: Mapping of {output_key: activation_tensor}
for each requested return layer.
"""
...
Import
import torch.nn as nn
from collections import OrderedDict
I/O Contract
| Method |
Input |
Output |
Notes
|
__init__(model, return_layers) |
model: nn.Module; return_layers: dict mapping layer names to output keys |
None (initializes ModuleDict) |
Validates layers exist, prunes unnecessary modules
|
forward(x) |
x: input tensor (B, C, H, W) |
OrderedDict of {output_key: activation_tensor} |
Returns activations at specified layers
|
return_layers Parameter
| Key |
Value |
Description
|
| Layer name (str) |
Output key (str) |
Maps the named child module of the source model to an output key in the returned OrderedDict
|
Usage Examples
import torchvision.models as models
# Load a pretrained ResNet-101 backbone
backbone = models.resnet101(pretrained=True)
# Extract features from layer3 and layer4
return_layers = {"layer3": "mid", "layer4": "out"}
feature_extractor = IntermediateLayerGetter(backbone, return_layers)
# Forward pass
import torch
x = torch.randn(1, 3, 512, 512)
features = feature_extractor(x)
# features["mid"] contains layer3 activations
# features["out"] contains layer4 activations
Example 2: Integration with DeepLabV3
# In the DeepLabV3 segmentation model, IntermediateLayerGetter
# extracts backbone features that feed into the ASPP head:
backbone = models.resnet101(pretrained=True)
return_layers = {"layer4": "out"}
backbone_extractor = IntermediateLayerGetter(backbone, return_layers)
# The extracted "out" features are passed to DeepLabHead
features = backbone_extractor(input_image)
segmentation_logits = deeplab_head(features["out"])
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.