ResNet feature-extracting backbone (no classification head).
Implements the ResNet topology from He et al. in its v1.5 form:
the downsampling stride sits on the bottleneck's 3x3 convolution.
The paper says only that "downsampling is performed by conv3_1,
conv4_1, and conv5_1 with a stride of 2" without naming which conv
carries it; the original MSRA release put it on the first 1x1 (v1).
v1.5 is what every modern reference builds and what the shipped
checkpoints were trained as., "Deep
Residual Learning for Image Recognition", CVPR 2016
(arXiv:1512.03385), restricted to the convolutional trunk: a 7×7
stem at stride 2, a 3×3 max-pool at stride 2, then four stages of
residual blocks producing feature maps at strides 4, 8, 16, and 32
relative to the input. No global pooling, no flatten, and no
linear classifier — those live on ResNetForImageClassification.
The block topology is selected by config.block_type: ResNet-18
and ResNet-34 use _BasicBlock, while the deeper ResNet-50
/ 101 / 152 / 200 / 269 and Wide ResNet variants use
_Bottleneck. This class is the canonical feature
extractor consumed by detection / segmentation heads (FPN,
Faster R-CNN, Mask R-CNN, …) and by self-supervised pretraining
pipelines.
Parameters
configResNetConfigresnet_18, resnet_50, …) for paper-cited
variants — they instantiate the correct config for you.Attributes
configResNetConfigstemnn.Sequentialmaxpoolnn.MaxPool2dlayer1, layer2, layer3, layer4nn.Sequentiallayer1 keeps the input spatial
size; layer2/layer3/layer4 each halve it. Output
channel counts are hidden_sizes[i] * block.expansion.feature_infolist[FeatureInfo]BackboneMixin for downstream FPN / decoder modules.Notes
Each residual stage applies the standard skip-connection update
where is the in-block transformation, is ReLU, and is either the identity (matched dimensions) or a 1×1 strided projection (dimension change at stage boundaries). The residual formulation lets gradients flow directly through the identity branch, making it practical to train networks with over a hundred layers — the central contribution of the original paper.
The output of forward is a BaseModelOutput whose
last_hidden_state has shape (B, C_4, H/32, W/32) with
—
e.g. (B, 512, H/32, W/32) for ResNet-18 and
(B, 2048, H/32, W/32) for ResNet-50.
Examples
Build a ResNet-50 backbone and run a single forward pass:
>>> import lucid
>>> from lucid.models.vision.resnet import resnet_50
>>> backbone = resnet_50()
>>> x = lucid.randn(2, 3, 224, 224) # (batch, channels, H, W)
>>> out = backbone(x)
>>> out.last_hidden_state.shape
(2, 2048, 7, 7)
Inspect the per-stage feature map descriptors for FPN integration:
>>> info = backbone.feature_info
>>> [(fi.stage, fi.num_channels, fi.reduction) for fi in info]
[(1, 256, 4), (2, 512, 8), (3, 1024, 16), (4, 2048, 32)]Used by 2
Constructors
1Initialise the module and validate the supplied config.
Parameters
configModelConfigconfig_class.Raises
TypeErrorconfig_class has not been set on the concrete subclass,
or if config is not an instance of config_class.Properties
1Instance methods
2forward(x: Tensor)Run the model with every covered weight compressed.
Pooled (B, C) feature vector, post head-norm.
The per-stage maps this backbone's feature_info describes
are left on stage_features for pyramid consumers.