3.4.10Convolutional Neural Networks

DenseNet and EfficientNet

2,985 words14 min readdifficulty · medium

Overview

Two landmark architectures that revolutionized CNN design: DenseNet maximizes feature reuse through dense connections, while EfficientNet systematically scales networks for optimal accuracy-efficiency tradeoffs.

Figure — DenseNet and EfficientNet

DenseNet: Dense Convolutional Networks

For layer \ell in a dense block: x=H([x0,x1,,x1])\mathbf{x}_\ell = H_\ell([\mathbf{x}_0, \mathbf{x}_1, \ldots, \mathbf{x}_{\ell-1}])

where [x0,x1,,x1][\mathbf{x}_0, \mathbf{x}_1, \ldots, \mathbf{x}_{\ell-1}] denotes concatenation along the channel dimension, and HH_\ell is a composite function: BN → ReLU → Conv3×3.

Why this matters:

  • Gradient highway: Gradients can flow directly to early layers (alleviates vanishing gradients)
  • Feature reuse: Late layers access raw low-level features from early layers
  • Parameter efficiency: No need to re-learn redundant features

Deriving Growth Rate and Parameters

Derivation from first principles:

  • Layer0: input has k0k_0 channels
  • Layer 1: receives k0k_0 channels, outputs kk new channels → total = k0+kk_0 + k
  • Layer 2: receives k0+kk_0 + k channels, outputs kk new channels → total = k0+2kk_0 + 2k
  • Layer \ell: receives k0+(1)kk_0 + (\ell-1)k channels, outputs kk → total = k0+kk_0 + \ell k
  • After LL layers: k0+Lkk_0 + Lk channels

Why small kk works: Typical k=12k = 12 or 3232. Each layer contributes a small set of feature maps to the "collective knowledge," but accesses the rich combined feature set from all predecessors.

H:BN-ReLU-Conv1×14k channelsBN-ReLU-Conv3×3k channelsH_\ell: \text{BN-ReLU-Conv1×1} \rightarrow 4k \text{ channels} \rightarrow \text{BN-ReLU-Conv3×3} \rightarrow k \text{ channels}

Why 1×1 convolution? Reduces dimensionality before expensive3×3 convolution.

Parameter calculation for one bottleneck layer:

  • If input has mm channels:
    • 1×1 conv: m×4k×1×1=4mkm \times 4k \times 1 \times 1 = 4mk parameters
    • 3×3 conv: 4k×k×3×3=36k24k \times k \times 3 \times 3 = 36k^2 parameters
  • Without bottleneck: m×k×3=9mkm \times k \times 3 = 9mk parameters

As mm grows (late in the block), 4mk9mk4mk \ll 9mk for reasonable kk, so bottlenecks save parameters.

If a dense block outputs mm channels, the transition layer compresses to θm\lfloor \theta m \rfloor channels where θ(0,1]\theta \in (0, 1] is the compression factor (typically θ=0.5\theta = 0.5).

Why compression? Controls model size and computational cost. Halving channels after each dense block keeps growth manageable.

Total layers: 1+(6+12+24+16)=591 + (6+12+24+16) = 59 conv layers inside dense blocks, plus transitions≈ 121 layers with bottlenecks.

Why this configuration works: Early blocks (few layers) learn low/mid-level features. Deep blocks (many layers) learn high-level abstractions. Each has access to all prior features.

Why this step? Each layer concatenates its output (k=8k=8) to the running total.

After transition with θ=0.5\theta=0.5: 48×0.5=2448 \times 0.5 = 24 channels.

Why it feels right: Both are "skip" connections that help gradients.

The fix: DenseNet concatenates feature maps: x=H([x0,,x1])\mathbf{x}_\ell = H_\ell([\mathbf{x}_0, \ldots, \mathbf{x}_{\ell-1}]). This preserves all prior information explicitly, while ResNet's addition merges features (loses individual identity). Concatenation enables true feature reuse but increases memory; addition is memory-efficient but reuses less.

EfficientNet: Compound Scaling

depth: d=αϕ\text{depth: } d = \alpha^\phi width: w=βϕ\text{width: } w = \beta^\phi resolution: r=γϕ\text{resolution: } r = \gamma^\phi

subject to the constraint: αβ2γ2approx2\alpha \cdot \beta^2 \cdot \gamma^2approx 2, with α1,β1,γ1\alpha \geq 1, \beta \geq 1, \gamma \geq 1.

Why this constraint? Approximates FLOPs doubling per unit increase in ϕ\phi.

Deriving the Scaling Law

Total FLOPs for a layer: FLOPsdw2r2\text{FLOPs} \propto d \cdot w^2 \cdot r^2

If we scale each dimension: FLOPsnew(dαϕ)(wβϕ)2(rγϕ)2\text{FLOPs}_{\text{new}} \propto (d \cdot \alpha^\phi) \cdot (w \cdot \beta^\phi)^2 \cdot (r \cdot \gamma^\phi)^2 =dw2r2αϕβ2ϕγ2ϕ= d \cdot w^2 \cdot r^2 \cdot \alpha^\phi \cdot \beta^{2\phi} \cdot \gamma^{2\phi} =FLOPsbase(αβ2γ2)ϕ= \text{FLOPs}_{\text{base}} \cdot (\alpha \cdot \beta^2 \cdot \gamma^2)^\phi

To double FLOPs per increment of ϕ\phi: αβ2γ2=2\alpha \cdot \beta^2 \cdot \gamma^2 = 2

Why this matters: For a given computational budget (FLOPs), this formula tells you how to distribute scaling across depth, width, and resolution for maximum accuracy.

Check: 1.2×1.12×1.152=1.2×1.21×1.32251.9221.2 \times 1.1^2 \times 1.15^2 = 1.2 \times 1.21 \times 1.3225 \approx 1.92 \approx 2

To create EfficientNet-B1 with ϕ=1\phi = 1:

  • Depth: dB1=1.21×dB0=1.2×dB0d_{B1} = 1.2^1 \times d_{B0} = 1.2 \times d_{B0}
  • Width: wB1=1.11×wB0=1.1×wB0w_{B1} = 1.1^1 \times w_{B0} = 1.1 \times w_{B0}
  • Resolution: rB1=1.151×224240r_{B1} = 1.15^1 \times 224 \approx 240 pixels

For EfficientNet-B7 with ϕ=6\phi = 6:

  • Depth: 1.263.0×dB01.2^6 \approx 3.0 \times d_{B0}
  • Width: 1.161.77×wB01.1^6 \approx 1.77 \times w_{B0}
  • Resolution: 1.156×2244561.15^6 \times 224 \approx 456 pixels

Why this step? Each dimension grows exponentially with ϕ\phi, but constrained so total FLOPs grow exponentially too (each increment of ϕ\phi ≈ 2× FLOPs).

  1. Expand: 1×1 conv to expand channels by factor tt (typically 6)
  2. Depthwise: 3×3 or 5×5 depthwise conv (one filter per channel)
  3. Squeeze-and-Excitation (SE): channel attention
  4. Project: 1×1 conv to project back to output channels
  5. Skip connection if input/output dimensions match

Why MBConv? Extremely parameter-efficient. Depthwise separable convolutions reduce FLOPs dramatically compared to standard convolutions.

Strategy 1: Depth only (α=2,β=1,γ=1\alpha = 2, \beta = 1, \gamma = 1)

  • FLOPs: 2×12×12=22 \times 1^2 \times 1^2 = 2
  • Result: Very deep, narrow network. Risk: vanishing gradients, harder to train.

Strategy 2: Width only (α=1,β=2,γ=1\alpha = 1, \beta = \sqrt{2}, \gamma = 1)

  • FLOPs: 1×2×1=21 \times 2 \times 1 = 2
  • Result: Wide, shallow network. Risk: limited representational depth.

Strategy 3: Resolution only (α=1,β=1,γ=2\alpha = 1, \beta = 1, \gamma = \sqrt{2})

  • FLOPs: 1×1×2=21 \times 1 \times 2 = 2
  • Result: Higher resolution, same depth/width. Risk: fine spatial details but no added abstraction layers.

Strategy 4: Compound scaling (α=1.2,β=1.1,γ=1.15ϕ=1\alpha = 1.2, \beta = 1.1, \gamma = 1.15 \phi = 1)

  • FLOPs: 1.2×1.21×1.322521.2 \times 1.21 \times 1.3225 \approx 2
  • Result: Balanced growth in all dimensions. Empirically achieves higher accuracy.

Why compound scaling wins: Depth adds abstraction, width adds capacity per layer, resolution captures finer patterns. Scaling all three avoids bottlenecks in any single dimension.

Why it feels right: More is better, right?

The fix: Unconstrained scaling explodes FLOPs: 2×4=32×2 \times 4 = 32\times increase FLOPs (not 2×). You blow your compute budget. The αβ2γ22\alpha \beta^2 \gamma^2 \approx 2 constraint ensures you scale within a target computational envelope. Empirically, the exponents 1, 2, 2 reflect how depth, width, and resolution impact FLOPs in convolutional layers.

Comparing DenseNet and EfficientNet

| Aspect | DenseNet | EfficientNet | |--------|--------------| | Core Idea | Dense connectivity for feature reuse | Compound scaling for efficiency | | Parameter Efficiency | Fewer params via feature reuse | Fewer params via MBConv + AutoML | | Memory | High (concatenation stores all intermediate features) | Low (inverted bottlenecks) | | Training | Can be memory-intensive | Efficient, but needs careful scaling | | Strength | Excellent gradient flow, feature reuse | State-of-the-art accuracy/FLOP tradeoff | | Use Case | Tasks where feature reuse matters (medical imaging, fine-grained classification) | Resource-constrained or production deployment |

Recall Feynman Explanation (ELI12)

Imagine you're building a LEGO tower (a neural network).

DenseNet: Every time you add a new layer of LEGO bricks, you attach it not just to the layer right below, but you run strings (connections) to every layer below it. So the top brick is connected to the bottom, the middle, everything. This means every brick can "see" what all the earlier bricks learned. It's like everyone in a class sharing their notes with everyone else—nobody mises out on important info. The downside? You need a lot of string (memory).

EfficientNet: Now imagine you want to make your tower taller. You could just stack more layers (make it deeper). Or make each layer wider (more bricks per layer). Or use bigger bricks (higher resolution images). EfficientNet says: do all three, but in a balanced way. If you make it20% deeper, make it 10% wider and use 15% bigger bricks, all at once. This way, the tower stays strong and balanced without topling over or using too much material.

Both are about being smart: DenseNet shares everything, EfficientNet scales everything proportionally.


Connections

  • ResNet - DenseNet extends skip connections to full dense connectivity
  • Inception - Multi-scale processing, both use1×1 bottlenecks
  • MobileNet - EfficientNet builds on MBConv blocks from MobileNetV2
  • Neural Architecture Search - EfficientNet uses NAS to find baseline architecture
  • Batch Normalization - Critical component in both architectures
  • Model Compression - Both architectures motivate efficiency research

#flashcards/ai-ml

What is the dense connectivity pattern in DenseNet? :: Each layer receives feature maps from all preceding layers via concatenation along the channel dimension, and passes its output to all subsequent layers.

What is the growth rate kk in DenseNet and why can it be small?
kk is the number of feature maps each layer produces. It can be small (e.g., 12-32) because each layer accesses the rich collective feature set from all predecessors via concatenation, so it only needs to add a small new contribution.
How many channels does a DenseNet block output if the input has k0k_0 channels, there are LL layers, and growth rate is kk?
k0+Lkk_0 + L \cdot k channels, since each of the LL layers adds kk new feature maps to the concatenated set.
What is the purpose of 1×1 bottleneck layers in DenseNet?
To reduce dimensionality before expensive 3×3 convolutions, preventing explosion of input channels as the dense block progresses. Typical bottleneck: BN-ReLU-Conv1×1 → 4k channels → BN-ReLU-Conv3×3 → k channels.
What do transition layers do in DenseNet?
Reduce spatial dimensions (via 2×2 average pooling) and compress channels (via 1×1 conv with compression factor θ\theta, typically 0.5) between dense blocks to control model size.

What is compound scaling in EfficientNet? :: Uniformly scaling network depth (d=αϕd = \alpha^\phi), width (w=βϕw = \beta^\phi), and resolution (r=γϕr = \gamma^\phi) together using a compound coefficient ϕ\phi, subject to αβ2γ22\alpha \beta^2 \gamma^2 \approx 2 to control FLOPs.

Why does the compound scaling constraint use β2\beta^2 and γ2\gamma^2 rather than linear terms?
Because width affects FLOPs quadratically (input channels × output channels in convolutions) and resolution affects FLOPs quadratically (spatial dimensions H×WH \times W), while depth scales FLOPs linearly.

What are MBConv blocks and why does EfficientNet use them? :: Mobile Inverted Bottleneck Convolution blocks: expand channels (1×1 conv), apply depthwise conv (3×3 or 5×5), add SE attention, project down (1×1 conv), skip connection. Extremely parameter-efficient due to depthwise separable convolutions.

If EfficientNet-B0 has depth d0d_0 and you create B1 with ϕ=1\phi=1 and α=1.2\alpha=1.2, what is the new depth?
d1=αϕd0=1.21d0=1.2×d0d_1 = \alpha^\phi \cdot d_0 = 1.2^1 \cdot d_0 = 1.2 \times d_0 (20% deeper).
What is the key difference between DenseNet's skip connections and ResNet's skip connections?
DenseNet concatenates feature maps (preserves all prior information explicitly, increases channels), while ResNet adds feature maps element-wise (merges features, constant channels). Concatenation enables stronger feature reuse but requires more memory.
Why does compound scaling (all three dimensions) outperform scaling only depth, width, or resolution?
Scaling only one dimension creates bottlenecks: depth alone risks vanishing gradients, width alone limits abstraction, resolution alone adds detail without added capacity. Balanced scaling ensures no single dimension is a bottleneck, empirically achieving higher accuracy for a given FLOP budget.
What is the memory tradeoff of DenseNet's architecture?
High memory usage during training because concatenation stores all intermediate feature maps for backpropagation. However, parameter count is low due to feature reuse (no redundant re-learning).

Concept Map

uses

via

governed by

output channels k0 + Lk

enables

enables

reduces channels with Conv1x1

compress between blocks

scales

balances

DenseNet

EfficientNet

Dense Connectivity

Channel Concatenation

Growth Rate k

Bottleneck Layers

Transition Layers

Gradient Highway

Feature Reuse

Hinglish (regional understanding)

Intuition Hinglish mein samjho

DenseNet aur EfficientNet dono hi modern CNN architectures hain jo different problems solve karte hain. DenseNet ka core idea hai feature reuse — har layer apne output ko next layers ke sath concatenate kar deti hai. Matlabagar 4 layers hain, toh 4th layer ko1st, 2nd, aur 3rd teno layers ka output directly mil jata hai. Isse gradient flow smooth hota hai (vanishing gradient problem solve hota hai) aur redundant features re-learn karne ki zarurat nahi padti. Growth rate 'k' typically chhota rakha jata hai (jaise 12 ya 32) kyunki har layer ko pehle se bohot sare features mil rahe hote hain concatenation ke through. Lekin iska ek downside hai — memory usage bahut zyada hota hai training ke time, kyunki sare intermediate feature maps store karne padte hain.

EfficientNet bilkul different approach leta hai. Iska focus hai efficiency — kam parameters aur FLOPs mein maximum accuracy. Traditional tarike se log sirf depth (layers) ya sirf width (channels) badhate the, lekin EfficientNet kehta hai ki teno dimensions ko sath mein balanced tarike se scale karo: depth, width, aur resolution. Formula hai d = α^φ, w = β^φ, r = γ^φ, aur constraint hai ki α·β²·γ² ≈ 2, taki har increment of φ mein FLOPs double ho. Ye compound scaling empirically bohot better results deta hai kyunki koi bhi ek dimension bottleneck nahi banta. EfficientNet MBConv blocks use karta hai (MobileNetV2 se), jo depthwise separable convolutions ke through bahut kam parameters mein powerful features nikaal lete hain. Real-world deployment ke liye, especially mobile aur edge devices pe, EfficientNet kafi popular hai.

Dono architectures apne-apne domains mein revolutionary hain — DenseNet feature reuse ke liye best hai (medical imaging, fine-grained tasks), aur EfficientNet efficiency ke liye gold standard hai.

Go deeper — visual, from zero

Test yourself — Convolutional Neural Networks

Connections