Code Architecture — Densely Connected Convolutional Networks (DenseNet) Notebook

Overview

The notebook builds a small DenseNet-BC from scratch in PyTorch (dense layers with bottlenecks, dense blocks, transition layers with compression), trains it on CIFAR-10 for 4 epochs, and plots how the channel count grows inside a dense block. The saved Kaggle run used a Tesla T4 GPU.

Notebook Structure

1. Setup and Imports

  • PyTorch, torchvision, NumPy, matplotlib; nothing is installed
  • CUBLAS_WORKSPACE_CONFIG set before importing torch; seed 42; cuDNN deterministic, torch.use_deterministic_algorithms(True, warn_only=True)
  • Figures as JPEG at 80 dpi; on Kaggle an atexit hook and a small background process copy the executed notebook to executed_solution.ipynb

2. Dense Layer (DenseLayer(in_channels, growth_rate))

  • BN → ReLU → 1×1 conv to 4k channels → BN → ReLU → 3×3 conv (padding 1) to k channels
  • Returns only the k new maps; the block does the concatenation

3. Dense Block (DenseBlock(in_channels, growth_rate, num_layers))

  • Layer i gets in_channels + i·k input channels: the concatenation of the block input and all earlier layers’ outputs (torch.cat(features, dim=1))
  • Output is the concatenation of everything: in_channels + num_layers·k channels
  • channel_history records the channel count after each layer

4. Transition Layer (TransitionLayer(in_channels, out_channels))

  • BN → ReLU → 1×1 conv → 2×2 average pooling (stride 2)
  • With compression θ = 0.5, out_channels = ⌊0.5 · in_channels⌋

5. Full DenseNet (DenseNet(growth_rate=12, num_blocks=3, num_layers_per_block=4, compression=0.5))

  • 3×3 conv to 2k = 24 channels → BN → ReLU
  • Block 1 (24 → 72) → transition (72 → 36, 32×32 → 16×16) → block 2 (36 → 84) → transition (84 → 42, 16×16 → 8×8) → block 3 (42 → 90)
  • BN → ReLU → global average pooling → nn.Linear(90, 10)
  • Prints 102,778 parameters and the channel history of each block

6. Data Loading (CIFAR-10)

  • Training: RandomCrop(32, padding=4), RandomHorizontalFlip, normalisation; test: normalisation only
  • Downloaded quietly into a temporary folder; batch size 128, 2 workers, seeded training loader

7. Training Loop

  • CrossEntropyLoss; SGD with learning rate 0.1, momentum 0.9, weight decay 1e-4
  • MultiStepLR(milestones=[2, 3], gamma=0.1): the learning rate is divided by 10 after epochs 2 and 3 (50% and 75% of training)
  • Epochs: 4; prints loss, training accuracy, test accuracy and time per epoch

8. Training Curves

  • Training loss, and training and test accuracy per epoch; prints the final test accuracy

9. Feature-Map Channel Growth Visualization

  • Bar chart of the channel count in block 1; prints the channel history and the channels each layer adds (np.diff)
  • Passes one test image through block 1 and shows the first 8 maps of the block input and of each layer’s new output

10. Summary

  • ### What the run above shows with the saved run’s numbers

Key Shapes (k = 12, batch of 128, 32×32 input)

Stage Shape
Input (128, 3, 32, 32)
Initial conv + BN + ReLU (128, 24, 32, 32)
Dense block 1 (128, 72, 32, 32)
Transition 1 (128, 36, 16, 16)
Dense block 2 (128, 84, 16, 16)
Transition 2 (128, 42, 8, 8)
Dense block 3 (128, 90, 8, 8)
Global average pool + flatten (128, 90)
Classifier (128, 10)

Inside each dense layer: (B, C_in, H, W) → 1×1 conv → (B, 48, H, W) → 3×3 conv → (B, 12, H, W).

Deliberate Simplifications vs Full Paper

  1. Depth: 3 blocks of 4 layers (12 dense layers, 102,778 parameters) instead of 100 to 250 layers (0.8M to 25.6M parameters) (Table 2).
  2. Training: 4 epochs, batch size 128, plain momentum, instead of 300 epochs, batch size 64 and Nesterov momentum (Section 4.2).
  3. Initial layer: the first 3×3 convolution is followed by BN and ReLU here.
  4. No ImageNet, CIFAR-100 or SVHN experiments, and no baseline network to compare with.