Skip to content
pierrecavalierPublic

About

Re-implementation of ResNet (He et al., 2015) in PyTorch: 20- to 56-layer residual networks vs plain CNNs on CIFAR-10, with a Streamlit demo. M1 Math & AI.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

Re-implementing ResNet

Project for the "AI Methods" course taught by Guillermo Durand, first year of the Mathematics and Artificial Intelligence master at Université Paris-Saclay (2022–2023). Joint work by Lucas Biéchy and Pierre Cavalier, based on Deep Residual Learning for Image Recognition by Kaiming He, Xiangyu Zhang, Shaoqing Ren and Jian Sun (2015).

Residual building block: two weight layers with an identity shortcut added to their output

Question

Making a plain convolutional network deeper does not always make it more accurate: past about 20 layers, vanishing and exploding gradients can make a deeper network worse than a shallower one. He et al. add identity shortcuts that skip every two convolutions, so each block learns a residual F(x) and outputs F(x) + x. Do these shortcuts really let deeper CNNs train better? We rebuilt the CIFAR-10 networks from the paper to check.

Approach

  • Data: CIFAR-10 (50,000 training and 10,000 test images, 10 classes), images neither cropped nor resized.
  • Architecture (CIFAR-10 version of the paper): a 3x3 convolution from 3 to 16 channels, then three stages of n residual blocks (16, 32 and 64 channels, stride 2 at each stage transition), each block being two 3x3 convolutions with batch normalisation and ReLU, then average pooling and a linear classifier. With n = 3, 5, 7, 9 this gives networks of 20, 32, 44 and 56 layers.
  • Shortcuts across a dimension change: option (A) zero-padding, or option (B) projection with a 1x1 convolution.
  • Baselines: the same networks without shortcuts (plain CNNs), and the torchvision models.resnet18/34/50.
  • Training: Adam with PyTorch default parameters (instead of SGD in the paper, to save compute), mini-batches of 32, 10 epochs, PyTorch default weight initialisation, no dropout. Every reported curve is the mean over 5 identically configured models.

Results

  • ResNet vs plain CNN: after 10 epochs, the residual networks improve on average the accuracy of the plain CNNs by about 28% and their loss by about 43%. The gap grows with depth: the plain CNN gets worse as layers are added, the ResNet does not.
  • Depth: with shortcuts, deeper is better. The 56-layer ResNet beats the 20-layer one by 1.3% in accuracy and 2.2% in loss after 10 epochs.
  • Option (A) vs (B): the two shortcut types give very similar curves; (A) is slightly better overall, so, as in the paper, it is the default.
  • torchvision ResNets: under the same training setup they reached lower accuracy than our implementation (about 0.76 for ResNet-18/34/50 after 10 epochs).

Mean test accuracy after 10 epochs (average of the 5 runs stored in results/*_acc.csv):

Depth ResNet, option A ResNet, option B Plain CNN
20 0.813 0.811 0.788
32 0.821 0.819 0.708
44 0.820 0.819 0.632
56 0.826 0.816 0.584

Test accuracy and loss over 10 epochs for ResNets and plain CNNs of 20 to 56 layers

Accuracy and loss of ResNets with shortcut option A or B Accuracy and loss of our ResNets compared with torchvision ResNets

Repository structure

  • classes.py: convolution block with shortcut and the generic ResNet module.
  • models.py: the ResNet (options A and B), plain CNN and torchvision models used in the experiments and the demo.
  • data.py: CIFAR-10 loaders (full dataset and a 5,000-image subset).
  • train_test_functions.py: training and evaluation loops.
  • analysis.py: comparison plots (option A vs B, ResNet vs torchvision, ResNet vs CNN).
  • main.py: runs the full experiment and produces the figures.
  • code.ipynb: notebook version of the code.
  • results/: saved models, per-epoch accuracy and loss (*_acc.csv, *_loss.csv) and figures.
  • streamlit.py, streamlit_ressources/, streamlit_res/: interactive demo, with architecture diagrams of the networks it trains.
  • docs/: Sphinx documentation of the code; the built HTML is in docs/_build/html/index.html.

Usage

Dependencies: torch, torchvision, numpy, matplotlib, and for the demo streamlit, streamlit_timeline and requests. CIFAR-10 is downloaded automatically into data/ on first run.

Interactive demo

streamlit run streamlit.py

Choose a network type (ResNet or CNN) and a depth (8, 14 or 20 layers), then:

  • "View model" shows the architecture diagram of the selected network;
  • "Train model" trains it on a 5,000-image subset of CIFAR-10 for 5 epochs (about 3-5 minutes);
  • "Print accuracy of every model trained so far" compares the accuracy curves of all the models trained in the app.

Full experiment

python main.py

This opens the Sphinx documentation in the browser, trains the 12 ResNet and CNN models and the 3 torchvision ResNets on the full CIFAR-10 for 10 epochs, and plots the comparisons. Training takes a long time; its outputs are already saved in results/.

Report

ResNet_Cavalier_Biechy.pdf (in English, 7 pages).

References

About

Re-implementation of ResNet (He et al., 2015) in PyTorch: 20- to 56-layer residual networks vs plain CNNs on CIFAR-10, with a Streamlit demo. M1 Math & AI.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages