Home

Awesome

RepDistiller

This repo:

(1) covers the implementation of the following ICLR 2020 paper:

"Contrastive Representation Distillation" (CRD). Paper, Project Page.

<div style="text-align:center"><img src="http://hobbitlong.github.io/CRD/CRD_files/teaser.jpg" width="85%" height="85%"></div> <p></p>

(2) benchmarks 12 state-of-the-art knowledge distillation methods in PyTorch, including:

(KD) - Distilling the Knowledge in a Neural Network
(FitNet) - Fitnets: hints for thin deep nets
(AT) - Paying More Attention to Attention: Improving the Performance of Convolutional Neural Networks via Attention Transfer
(SP) - Similarity-Preserving Knowledge Distillation
(CC) - Correlation Congruence for Knowledge Distillation
(VID) - Variational Information Distillation for Knowledge Transfer
(RKD) - Relational Knowledge Distillation
(PKT) - Probabilistic Knowledge Transfer for deep representation learning
(AB) - Knowledge Transfer via Distillation of Activation Boundaries Formed by Hidden Neurons
(FT) - Paraphrasing Complex Network: Network Compression via Factor Transfer
(FSP) - A Gift from Knowledge Distillation: Fast Optimization, Network Minimization and Transfer Learning
(NST) - Like what you like: knowledge distill via neuron selectivity transfer

Installation

This repo was tested with Ubuntu 16.04.5 LTS, Python 3.5, PyTorch 0.4.0, and CUDA 9.0. But it should be runnable with recent PyTorch versions >=0.4.0

Running

  1. Fetch the pretrained teacher models by:

    sh scripts/fetch_pretrained_teachers.sh
    

    which will download and save the models to save/models

  2. Run distillation by following commands in scripts/run_cifar_distill.sh. An example of running Geoffrey's original Knowledge Distillation (KD) is given by:

    python train_student.py --path_t ./save/models/resnet32x4_vanilla/ckpt_epoch_240.pth --distill kd --model_s resnet8x4 -r 0.1 -a 0.9 -b 0 --trial 1
    

    where the flags are explained as:

    • --path_t: specify the path of the teacher model
    • --model_s: specify the student model, see 'models/__init__.py' to check the available model types.
    • --distill: specify the distillation method
    • -r: the weight of the cross-entropy loss between logit and ground truth, default: 1
    • -a: the weight of the KD loss, default: None
    • -b: the weight of other distillation losses, default: None
    • --trial: specify the experimental id to differentiate between multiple runs.

    Therefore, the command for running CRD is something like:

    python train_student.py --path_t ./save/models/resnet32x4_vanilla/ckpt_epoch_240.pth --distill crd --model_s resnet8x4 -a 0 -b 0.8 --trial 1
    
  3. Combining a distillation objective with KD is simply done by setting -a as a non-zero value, which results in the following example (combining CRD with KD)

    python train_student.py --path_t ./save/models/resnet32x4_vanilla/ckpt_epoch_240.pth --distill crd --model_s resnet8x4 -a 1 -b 0.8 --trial 1     
    
  4. (optional) Train teacher networks from scratch. Example commands are in scripts/run_cifar_vanilla.sh

Note: the default setting is for a single-GPU training. If you would like to play this repo with multiple GPUs, you might need to tune the learning rate, which empirically needs to be scaled up linearly with the batch size, see this paper

Benchmark Results on CIFAR-100:

Performance is measured by classification accuracy (%)

  1. Teacher and student are of the same architectural type.
Teacher <br> Studentwrn-40-2 <br> wrn-16-2wrn-40-2 <br> wrn-40-1resnet56 <br> resnet20resnet110 <br> resnet20resnet110 <br> resnet32resnet32x4 <br> resnet8x4vgg13 <br> vgg8
Teacher <br> Student75.61 <br> 73.2675.61 <br> 71.9872.34 <br> 69.0674.31 <br> 69.0674.31 <br> 71.1479.42 <br> 72.5074.64 <br> 70.36
KD74.9273.5470.6670.6773.0873.3372.98
FitNet73.5872.2469.2168.9971.0673.5071.02
AT74.0872.7770.5570.2272.3173.4471.43
SP73.8372.4369.6770.0472.6972.9472.68
CC73.5672.2169.6369.4871.4872.9770.71
VID74.1173.3070.3870.1672.6173.0971.23
RKD73.3572.2269.6169.2571.8271.9071.48
PKT74.5473.4570.3470.2572.6173.6472.88
AB72.5072.3869.4769.5370.9873.1770.94
FT73.2571.5969.8470.2272.3772.8670.58
FSP72.91N/A69.9570.1171.8972.6270.23
NST73.6872.2469.6069.5371.9673.3071.53
CRD75.4874.1471.1671.4673.4875.5173.94
  1. Teacher and student are of different architectural type.
Teacher <br> Studentvgg13 <br> MobileNetV2ResNet50 <br> MobileNetV2ResNet50 <br> vgg8resnet32x4 <br> ShuffleNetV1resnet32x4 <br> ShuffleNetV2wrn-40-2 <br> ShuffleNetV1
Teacher <br> Student74.64 <br> 64.6079.34 <br> 64.6079.34 <br> 70.3679.42 <br> 70.5079.42 <br> 71.8275.61 <br> 70.50
KD67.3767.3573.8174.0774.4574.83
FitNet64.1463.1670.6973.5973.5473.73
AT59.4058.5871.8471.7372.7373.32
SP66.3068.0873.3473.4874.5674.52
CC64.8665.4370.2571.1471.2971.38
VID65.5667.5770.3073.3873.4073.61
RKD64.5264.4371.5072.2873.2172.21
PKT67.1366.5273.0174.1074.6973.89
AB66.0667.2070.6573.5574.3173.34
FT61.7860.9970.2971.7572.5072.03
NST58.1664.9671.2874.1274.6874.89
CRD69.7369.1174.3075.1175.6576.05

Citation

If you find this repo useful for your research, please consider citing the paper

@inproceedings{tian2019crd,
  title={Contrastive Representation Distillation},
  author={Yonglong Tian and Dilip Krishnan and Phillip Isola},
  booktitle={International Conference on Learning Representations},
  year={2020}
}

For any questions, please contact Yonglong Tian (yonglong@mit.edu).

Acknowledgement

Thanks to Baoyun Peng for providing the code of CC and to Frederick Tung for verifying our reimplementation of SP. Thanks also go to authors of other papers who make their code publicly available.