Code Architecture — SqueezeNet Notebook
Overview
The notebook implements the SqueezeNet Fire module from scratch in PyTorch and builds a SqueezeNet adapted for CIFAR-10, in two versions: plain, and with the paper’s simple bypass (identity shortcuts around fire3, 5, 7 and 9, Section 6). It trains both and a small standard CNN the same way for 3 epochs and compares parameter counts and test accuracy. It runs on a GPU if there is one (the saved run used a Tesla T4).
Section-by-Section Breakdown
1. Setup & Imports
- PyTorch, torchvision, NumPy, matplotlib; nothing is installed
- Seed 42;
CUBLAS_WORKSPACE_CONFIGset before importing torch, cuDNN deterministic,torch.use_deterministic_algorithms(True, warn_only=True) - Figures as JPEG at 80 dpi; on Kaggle an
atexithook copies the executed notebook toexecuted_solution.ipynb
2. Data Loading (CIFAR-10)
- Training transforms: RandomCrop(32, padding=4), RandomHorizontalFlip, normalisation; test: normalisation only
- Downloaded quietly (no progress bars)
- DataLoaders with batch size 256 and 2 workers; the training loader has a seeded generator
- 50,000 training and 10,000 test images, 10 classes, 32×32 RGB
3. Fire Module
FireModule(inplanes, squeeze_planes, expand1x1_planes, expand3x3_planes)squeeze: 1×1 conv → ReLUexpand1x1: 1×1 conv → ReLU;expand3x3: 3×3 conv (padding 1) → ReLU- Forward: squeeze, then both expand branches on the squeezed tensor, concatenated along the channel dimension
- Sanity check: a 64→16→(64+64) module maps (1, 64, 16, 16) to (1, 128, 16, 16) and has 11,408 parameters
4. SqueezeNet Model
SqueezeNet(num_classes=10, simple_bypass=False)conv1: 3×3, 64 filters, stride 1 (the paper’s is 7×7, 96 filters, stride 2), ReLU, max-pool 3×3 stride 2- fire2–fire9 with the paper’s filter counts (Table 1); max-pool after fire4 and fire8
- Dropout 0.5 after fire9 (Section 3.3.1)
classifier: 1×1 conv to 10 channels, then global average pooling (no fully connected layers)fire_with_bypass(): whensimple_bypass=True, fire3, fire5, fire7 and fire9 returnfire(x) + x; these are the four modules whose input and output channels match, so the bypass is an identity and adds no parameters- Kaiming-uniform initialisation for every convolution, biases zero
- Both versions are built after
torch.manual_seed(42), so they start from identical weights; each has 727,626 parameters count_params(model)sumsp.numel()over the parameters
5. Baseline CNN
BaselineCNN: conv 3×3 (3→64) → pool → conv 3×3 (64→128) → pool → conv 3×3 (128→256) → pool → FC (4,096→512) → dropout 0.5 → FC (512→10)- 2,473,610 parameters, 3.40 times as many as SqueezeNet; fc1 alone has 2,097,664 (85%)
6. Training Loop
train_model(): CrossEntropyLoss, Adam (learning rate 0.001), cosine annealing over the epochs- Reseeds torch and the training loader’s generator at the start, so every model sees the same shuffling and augmentation order
- Records training loss, training accuracy and test accuracy after each epoch
7–8. Training
- SqueezeNet and SqueezeNet + bypass, then the baseline, each for 3 epochs; results go into a
resultsdict with history, training time and parameter count
9. Comparison
- Table of parameters, final test accuracy and training time for the three models
- Computed messages: the baseline’s parameter ratio, bypass minus plain SqueezeNet, and best SqueezeNet minus baseline
10. Visualization
- Training loss and test accuracy per epoch for the three models, and a bar chart of parameter counts
11. What the Paper Found
- The paper’s ImageNet results with table and section references, labelled as not reproduced here
12. Summary
### What the run above showswith the saved run’s numbers
Data Flow / Shapes (CIFAR-10, batch 256)
| Layer | Output shape | Notes |
|---|---|---|
| Input | (256, 3, 32, 32) | |
| conv1 (3×3, 64, stride 1) + ReLU | (256, 64, 32, 32) | |
| maxpool1 (3×3, stride 2) | (256, 64, 16, 16) | |
| fire2 (s16, e64+64) | (256, 128, 16, 16) | |
| fire3 (s16, e64+64) | (256, 128, 16, 16) | simple bypass |
| fire4 (s32, e128+128) | (256, 256, 16, 16) | |
| maxpool2 (3×3, stride 2) | (256, 256, 8, 8) | |
| fire5 (s32, e128+128) | (256, 256, 8, 8) | simple bypass |
| fire6 (s48, e192+192) | (256, 384, 8, 8) | |
| fire7 (s48, e192+192) | (256, 384, 8, 8) | simple bypass |
| fire8 (s64, e256+256) | (256, 512, 8, 8) | |
| maxpool3 (3×3, stride 2) | (256, 512, 4, 4) | |
| fire9 (s64, e256+256) | (256, 512, 4, 4) | simple bypass |
| dropout 0.5 | (256, 512, 4, 4) | |
| classifier (1×1, 10) | (256, 10, 4, 4) | |
| global average pool | (256, 10) |
“Simple bypass” rows add the module’s input to its output only when simple_bypass=True.
Deliberate Simplifications vs Full Paper
- CIFAR-10 instead of ImageNet: 32×32 images and 10 classes instead of ImageNet’s 1,000 classes.
- Smaller conv1 and classifier: 3×3, 64 filters, stride 1 instead of 7×7, 96 filters, stride 2; 10 output channels instead of 1,000. fire2–fire9 keep the paper’s filter counts, so the model has 727,626 parameters instead of 1,248,424.
- No complex bypass: only plain SqueezeNet and the simple bypass are built; the paper’s third variant, with 1×1 convolution shortcuts, is left out.
- Short training with different settings: 3 epochs with Adam at learning rate 0.001, instead of the paper’s longer training with a learning rate starting at 0.04 (Section 3.3.1).
- No compression: Deep Compression (pruning, quantization, Huffman coding) is not applied.
- Baseline is not AlexNet: the comparison model is a small 3-convolution CNN.