Documentation Index
Fetch the complete documentation index at: https://mintlify.com/pytorch/vision/llms.txt
Use this file to discover all available pages before exploring further.
torchvision.models.feature_extraction provides two utilities that let you extract activations from any intermediate layer of a model without writing custom forward hooks. The implementation rewrites the model’s computation graph using torch.fx, pruning away unused nodes so only the required subgraph is executed. This is particularly useful for building detection and segmentation heads, multi-scale feature pyramids, or transfer learning pipelines that need representations from several depths simultaneously.
API Reference
get_graph_node_names
Discovers and returns all traceable node names for a model by tracing it in both train and eval modes.
(train_nodes, eval_nodes) tuple. Nodes are named using dot-separated paths through the module hierarchy, with a _{counter} suffix when the same operation appears more than once.
Train and eval node lists may differ when a model has branches gated by
self.training. If you only use the model in eval mode, read from eval_nodes.create_feature_extractor
Creates a new fx.GraphModule that returns specified intermediate nodes as an OrderedDict.
| Argument | Type | Description |
|---|---|---|
return_nodes | list[str] or dict[str, str] | Node names to extract. If a dict, keys are node names and values are output keys. Mutually exclusive with train_return_nodes / eval_return_nodes. |
train_return_nodes | list[str] or dict[str, str] | Nodes to extract in train mode only (requires eval_return_nodes too). |
eval_return_nodes | list[str] or dict[str, str] | Nodes to extract in eval mode only (requires train_return_nodes too). |
tracer_kwargs | dict | Passed to NodePathTracer / torch.fx.Tracer for customising tracing behaviour. User-supplied values are merged with TorchVision defaults. |
suppress_diff_warning | bool | Suppress the warning emitted when train and eval graphs differ. Default: False. |
concrete_args | dict | Concrete (non-proxy) arguments for torch.fx.Tracer.trace. API stability not guaranteed by PyTorch. |
Step-by-Step Usage
Discover node names
Use Example output (truncated):
get_graph_node_names to see all available intermediate nodes. Check the eval list for inference use cases.Building a Feature Pyramid Network
Feature extractors integrate directly withtorchvision.ops.FeaturePyramidNetwork for multi-scale detection heads:
Separate Train / Eval Return Nodes
Some models have Dropout or BatchNorm branches that only exist in train mode. Usetrain_return_nodes and eval_return_nodes when the desired nodes differ between modes:
Handling Non-Traceable Models
create_feature_extractor uses torch.fx symbolic tracing. Some models call Python builtins (e.g., int(x.shape[0])) or contain dynamic control flow that breaks tracing. Use tracer_kwargs to work around these:
leaf_modules
Pass a list of
nn.Module subclasses that should be treated as atomic leaves. Their forward will not be traced — the graph will hold a reference to the whole module instead.autowrap_functions
Pass standalone functions that should not be symbolically traced. Useful for wrapping builtins like
int or len.Tips and Caveats
By default,
tracer_kwargs is pre-populated to treat all torchvision.ops modules as leaves (e.g., RoIAlign, DeformConv2d). This prevents tracing into their CUDA-specific internals. User-supplied tracer_kwargs are merged with these defaults.