Skip to content
class

DenseNet

extendsPretrainedModelBackboneMixin
DenseNet(config: DenseNetConfig)

DenseNet feature-extracting backbone (no classification head).

Implements the densely-connected topology from Huang et al., "Densely Connected Convolutional Networks", CVPR 2017: a Conv-BN- ReLU-MaxPool stem followed by four _DenseBlock stages interleaved with three _Transition blocks that halve both the channel and spatial dimensions. Inside each dense block every layer receives the concatenated feature maps of all preceding layers in the block. A final BatchNorm + ReLU + global average pool produces a fixed-size feature regardless of input resolution.

Parameters

Frozen architecture spec. Use the factory functions (densenet_121, densenet_169, …) for the paper-cited variants.

Attributes

Stored copy of the config that built this model.
features_Features
Flat container holding the stem (conv0, norm0, relu0, pool0), the four dense blocks (denseblock1 … denseblock4), the three transitions (transition1 … transition3), and the final BatchNorm (norm5). State-dict key prefix is features.*.
Global average pool collapsing the final feature map to 1×11\times1.
feature_infolist[FeatureInfo]
Per-stage descriptor (channels + reduction factor) exposed via BackboneMixin.

Notes

From Huang et al., CVPR 2017. Each layer in a dense block computes

xℓ=Hℓ([x0,  x1,  …,  xℓ−1]),x_\ell = H_\ell\bigl( [x_0,\; x_1,\; \dots,\; x_{\ell-1}] \bigr),

where [⋅][\cdot] is channel-wise concatenation and HℓH_\ell is a BN → ReLU → 1×11\times1 Conv → BN → ReLU → 3×33\times3 Conv composite (the bottleneck variant used in all ImageNet DenseNets). Each HℓH_\ell contributes growth_rate new channels — typically k=32k = 32 — so the input width of layer ℓ\ell grows linearly as k0+(ℓ−1) kk_0 + (\ell - 1)\,k. Transition layers compress the channel count by a factor of 2 between blocks. DenseNet-121 has only 8 M parameters yet matches the accuracy of ResNet-50 (25 M parameters) on ImageNet.

Examples

Build a DenseNet-121 backbone and run a single forward pass:
>>> import lucid
>>> from lucid.models.vision.densenet import densenet_121
>>> backbone = densenet_121()
>>> x = lucid.randn(2, 3, 224, 224)
>>> out = backbone(x)
>>> out.last_hidden_state.shape   # (B, 1024, 1, 1)
(2, 1024, 1, 1)

Used by 2

Constructors

1
dunder

__init__

→None
__init__(config: DenseNetConfig)

Instance methods

2
fn

forward_features

→Tensor
forward_features(x: Tensor)

Run the backbone's feature-extraction pass.

Parameters

Input image batch, shape (B, C, H, W).

Returns

Tensor

Feature map at the deepest stage. Concrete subclasses may additionally stash earlier stages on the instance so multi-stage consumers can fetch them.

Examples

The map returned is the one feature_info lists last, so a
neck can size itself from that entry without running the model.
>>> import lucid
>>> from lucid.models import create_model
>>> backbone = create_model("resnet_18").eval()
>>> last = backbone.feature_info[-1]
>>> last.num_channels, last.reduction
(512, 32)
>>> backbone.forward_features(lucid.randn((1, 3, 64, 96))).shape
(1, 512, 2, 3)
Each side is divided by the advertised reduction on its own, so
a non-square input keeps its aspect ratio.