AberHu/Knowledge-Distillation-Zoo

Pytorch implementation of various Knowledge Distillation (KD) methods.

Python

1,763

65 commits

updated Nov 25, 2021

See the code

README

Knowledge-Distillation-Zoo

News! I'm going to have a major update of this repo. The new version will contain most of the methods in Todo list. Please stay tuned.

Pytorch implementation of various Knowledge Distillation (KD) methods.

This repository is a simple reference, mainly focuses on basic knowledge distillation/transfer methods. Thus many tricks and variations, such as step-by-step training, iterative training, ensemble of teachers, ensemble of KD methods, data-free, self-distillation, online distillation etc. are not considered. Hope it is useful for your project or research.

I will update this repo regularly with new KD methods. If there some basic methods I missed, please contact with me.

Lists

NameMethodPaper LinkCode Link
Baselinebasic model with softmax loss—code
Logitsmimic learning via regressing logitspapercode
STsoft targetpapercode
ATattention transferpapercode
Fitnethints for thin deep netspapercode
NSTneural selective transferpapercode
PKTprobabilistic knowledge transferpapercode
FSPflow of solution procedurepapercode
FTfactor transferpapercode
RKDrelational knowledge distillationpapercode
ABactivation boundarypapercode
SPsimilarity preservationpapercode
Sobolevsobolev/jacobian matchingpapercode
BSSboundary supporting samplespapercode
CCcorrelation congruencepapercode
LwMlearning without memorizingpapercode
IRGinstance relationship graphpapercode
VIDvariational information distillationpapercode
OFDoverhaul of feature distillationpapercode
AFDattention feature distillationpapercode
CRDcontrastive representation distillationpapercode
DMLdeep mutual learningpapercode
  • Note, there are some differences between this repository and the original papers:
    • For AT: I use the sum of absolute values with power p=2 as the attention.
    • For Fitnet: The training procedure is one stage without hint layer.
    • For NST: I employ polynomial kernel with d=2 and c=0.
    • For AB: Two-stage training, the first 50 epochs for initialization, the second stage only employs CE without ST.
    • For BSS: 80% epochs employ CE+BSS loss, the rest 20% only uses CE. In addition, warmup for the first 10 epochs.
    • For CC: For consistency, I only consider CC without instance congruence. Gaussian RBF kernel is employed because Bilinear Pool kernel is similar with PKT. I choose P=2 order Taylor of Gaussian RBF kernel. No special sampling strategy.
    • For LwM: I employ it after rb2 (middle conv layer) but not rb3 (last conv layer), because the base net is resnet with the end of GAP followed by a classifier. If after rb3, the grad-CAN has the same values across H and W in each channel.
    • For IRG: I only use one-to-one mode.
    • For VID: I set the hidden channel size to be same with the output channel size and remove BN in μ.
    • For AFD: I find the original implementation of attention is unstable, thus replace it with a SE block.
    • For DML: Just two nets are employed. Synchronous update to avoid multiple forwards.

Datasets

  • CIFAR10
  • CIFAR100

Networks

  • Resnet-20
  • Resnet-110

The networks are same with Tabel 6 in paper.

Training

  • Creating ./dataset directory and downloading CIFAR10/CIFAR100 in it.
  • Using the script example_train_script.sh to train various KD methods. You can simply specify the hyper-parameters listed in train_xxx.py or manually change them.
  • The hyper-parameters I used can be found in the training logs (code: ezed).
  • Some Notes:
    • Sobolev/LwM alone is unstable and may be used in conjunction with other KD methods.
    • BSS may occasionally destroy the training procedure, leading to poor results.
    • If not specified in the original papers, all the methods can be used on the middle feature maps or multiple feature maps are only employed after the last conv layer. It is simple to extend to multiple feature maps.
    • I assume the size (C, H, W) of features between teacher and student are the same. If not, you could employ 1*1 conv, linear or pooling to rectify them.

Results

  • The trained baseline models are used as teachers. For fair comparison, all the student nets have same initialization with the baseline models.
  • The initial models, trained models and training logs are uploaded here (code: ezed).
  • The trade-off parameter --lambda_kd and other hyper-parameters are not chosen carefully. Thus the following results do not reflect which method is better than the others.
  • Some relation based methods, e.g. PKT, RKD and CC, have less effectiveness on CIFAR100 dataset. It may be because there are more inter classes but less intra classes in one batch. You could increase the batch size, create memory bank or design advance batch sampling methods.
TeacherStudentNameCIFAR10CIFAR100
-resnet-20Baseline92.37%68.92%
resnet-20resnet-20Logits93.30%70.36%
resnet-20resnet-20ST93.12%70.27%
resnet-20resnet-20AT92.89%69.70%
resnet-20resnet-20Fitnet92.73%70.08%
resnet-20resnet-20NST92.79%69.21%
resnet-20resnet-20PKT92.50%69.25%
resnet-20resnet-20FSP92.76%69.61%
resnet-20resnet-20FT92.98%69.90%
resnet-20resnet-20RKD92.72%69.48%
resnet-20resnet-20AB93.04%69.96%
resnet-20resnet-20SP92.88%69.85%
resnet-20resnet-20Sobolev92.78%69.39%
resnet-20resnet-20BSS92.58%69.96%
resnet-20resnet-20CC93.01%69.27%
resnet-20resnet-20LwM92.80%69.23%
resnet-20resnet-20IRG92.77%70.37%
resnet-20resnet-20VID92.61%69.39%
resnet-20resnet-20OFD92.82%69.93%
resnet-20resnet-20AFD92.56%69.63%
resnet-20resnet-20CRD92.96%70.33%
TeacherStudentNameCIFAR10CIFAR100
-resnet-20Baseline92.37%68.92%
-resnet-110Baseline93.86%73.15%
resnet-110resnet-20Logits92.98%69.78%
resnet-110resnet-20ST92.82%70.06%
resnet-110resnet-20AT93.21%69.28%
resnet-110resnet-20Fitnet93.04%69.81%
resnet-110resnet-20NST92.83%69.31%
resnet-110resnet-20PKT93.01%69.31%
resnet-110resnet-20FSP92.78%69.78%
resnet-110resnet-20FT93.01%69.49%
resnet-110resnet-20RKD93.21%69.36%
resnet-110resnet-20AB92.96%69.41%
resnet-110resnet-20SP93.30%69.45%
resnet-110resnet-20Sobolev92.60%69.23%
resnet-110resnet-20BSS92.78%69.71%
resnet-110resnet-20CC92.98%69.33%
resnet-110resnet-20LwM92.52%69.11%
resnet-110resnet-20IRG93.13%69.36%
resnet-110resnet-20VID92.98%69.49%
resnet-110resnet-20OFD93.13%69.81%
resnet-110resnet-20AFD92.92%69.60%
resnet-110resnet-20CRD92.92%70.80%
TeacherStudentNameCIFAR10CIFAR100
-resnet-110Baseline93.86%73.15%
resnet-110resnet-110Logits94.38%74.89%
resnet-110resnet-110ST94.59%74.33%
resnet-110resnet-110AT94.42%74.64%
resnet-110resnet-110Fitnet94.43%73.63%
resnet-110resnet-110NST94.43%73.55%
resnet-110resnet-110PKT94.35%73.74%
resnet-110resnet-110FSP94.39%73.59%
resnet-110resnet-110FT94.30%74.72%
resnet-110resnet-110RKD94.39%73.78%
resnet-110resnet-110AB94.63%73.91%
resnet-110resnet-110SP94.45%74.07%
resnet-110resnet-110Sobolev94.26%73.14%
resnet-110resnet-110BSS94.19%73.87%
resnet-110resnet-110CC94.49%74.43%
resnet-110resnet-110LwM94.19%73.28%
resnet-110resnet-110IRG94.44%74.96%
resnet-110resnet-110VID94.25%73.63%
resnet-110resnet-110OFD94.38%74.11%
resnet-110resnet-110AFD94.44%73.90%
resnet-110resnet-110CRD94.30%75.44%
Net1Net2NameCIFAR10CIFAR100
-resnet-20baseline92.37%68.92%
-resnet-110baseline93.86%73.15%
resnet20resnet20DML93.07%/93.37%70.39%/70.22%
resnet110resnet20DML94.45%/92.92%74.53%/70.29%
resnet110resnet110DML94.74%/94.79%74.72%/75.55%

Todo List

  • KDSVD (now has some bugs)
  • QuEST: Quantized Embedding Space for Transferring Knowledge
  • EEL: Learning an Evolutionary Embedding via Massive Knowledge Distillation
  • OnAdvFD: Feature-map-level Online Adversarial Knowledge Distillation
  • CS-KD: Regularizing Class-wise Predictions via Self-knowledge Distillation
  • PAD: Prime-Aware Adaptive Distillation
  • DCM: Knowledge Transfer via Dense Cross-Layer Mutual-Distillation
  • ESKD: On the Efficacy of Knowledge Distillation
  • GKA: Teachers Do More Than Teach: Compressing Image-to-Image Models
  • KD-fn: Feature Normalized Knowledge Distillation for Image Classification
  • FSD: Knowledge Distillation via Adaptive Instance Normalization
  • CSKD: Improving Knowledge Distillation via Category Structure
  • CD & GKD: Channel Distillation: Channel-Wise Attention for Knowledge Distillation
  • CRCD: Complementary Relation Contrastive Distillation
  • MGD: Matching Guided Distillation
  • SSKD: Knowledge Distillation Meets Self-Supervision
  • KR: Distilling Knowledge via Knowledge Review
  • AFD-SAD: Show, Attend and Distill: Knowledge Distillation via Attention-based Feature Matching
  • SRRL: Knowledge Distillation via Softmax Regression Representation Learning
  • SemCKD: Cross-Layer Distillation with Semantic Calibration
  • SKD: Reducing the Teacher-Student Gap via Spherical Knowledge Disitllation
  • IFDM: Heterogeneous Knowledge Distillation using Information Flow Modeling
  • LKD: Local Correlation Consistency for Knowledge Distillation
  • HKD: Distilling Holistic Knowledge with Graph Neural Networks
  • LONDON: Lipschitz Continuity Guided Knowledge Distillation
  • CDD: Channel-Wise Knowledge Distillation for Dense Prediction
  • SCKD: Student Customized Knowledge Distillation: Bridging the Gap Between Student and Teacher
  • ICKD: Exploring Inter-Channel Correlation for Diversity-preserved Knowledge Distillation
  • DKMF: Distilling Knowledge by Mimicking Features
  • CRCD: Complementary Relation Contrastive Distillation
  • WCoRD: Wasserstein Contrastive Representation Distillation
  • LKD: Local Correlation Consistency for Knowledge Distillation
  • KDCL: Online Knowledge Distillation via Collaborative Learning
  • ONE: Knowledge Distillation by On-the-Fly Native Ensemble

Requirements

  • python 3.7
  • pytorch 1.3.1
  • torchvision 0.4.2

Acknowledgements

This repo is partly based on the following repos, thank the authors a lot.

If you employ the listed KD methods in your research, please cite the corresponding papers.

distillation
kd
kd-methods
knowledge-distillation
knowledge-transfer
model-compression
teacher-student

AberHu/Knowledge-Distillation-Zoo

Pytorch implementation of various Knowledge Distillation (KD) methods.

Python

1,763

65 commits

updated Nov 25, 2021

See the code

README

Knowledge-Distillation-Zoo

News! I'm going to have a major update of this repo. The new version will contain most of the methods in Todo list. Please stay tuned.

Pytorch implementation of various Knowledge Distillation (KD) methods.

This repository is a simple reference, mainly focuses on basic knowledge distillation/transfer methods. Thus many tricks and variations, such as step-by-step training, iterative training, ensemble of teachers, ensemble of KD methods, data-free, self-distillation, online distillation etc. are not considered. Hope it is useful for your project or research.

I will update this repo regularly with new KD methods. If there some basic methods I missed, please contact with me.

Lists

NameMethodPaper LinkCode Link
Baselinebasic model with softmax loss—code
Logitsmimic learning via regressing logitspapercode
STsoft targetpapercode
ATattention transferpapercode
Fitnethints for thin deep netspapercode
NSTneural selective transferpapercode
PKTprobabilistic knowledge transferpapercode
FSPflow of solution procedurepapercode
FTfactor transferpapercode
RKDrelational knowledge distillationpapercode
ABactivation boundarypapercode
SPsimilarity preservationpapercode
Sobolevsobolev/jacobian matchingpapercode
BSSboundary supporting samplespapercode
CCcorrelation congruencepapercode
LwMlearning without memorizingpapercode
IRGinstance relationship graphpapercode
VIDvariational information distillationpapercode
OFDoverhaul of feature distillationpapercode
AFDattention feature distillationpapercode
CRDcontrastive representation distillationpapercode
DMLdeep mutual learningpapercode
  • Note, there are some differences between this repository and the original papers:
    • For AT: I use the sum of absolute values with power p=2 as the attention.
    • For Fitnet: The training procedure is one stage without hint layer.
    • For NST: I employ polynomial kernel with d=2 and c=0.
    • For AB: Two-stage training, the first 50 epochs for initialization, the second stage only employs CE without ST.
    • For BSS: 80% epochs employ CE+BSS loss, the rest 20% only uses CE. In addition, warmup for the first 10 epochs.
    • For CC: For consistency, I only consider CC without instance congruence. Gaussian RBF kernel is employed because Bilinear Pool kernel is similar with PKT. I choose P=2 order Taylor of Gaussian RBF kernel. No special sampling strategy.
    • For LwM: I employ it after rb2 (middle conv layer) but not rb3 (last conv layer), because the base net is resnet with the end of GAP followed by a classifier. If after rb3, the grad-CAN has the same values across H and W in each channel.
    • For IRG: I only use one-to-one mode.
    • For VID: I set the hidden channel size to be same with the output channel size and remove BN in μ.
    • For AFD: I find the original implementation of attention is unstable, thus replace it with a SE block.
    • For DML: Just two nets are employed. Synchronous update to avoid multiple forwards.

Datasets

  • CIFAR10
  • CIFAR100

Networks

  • Resnet-20
  • Resnet-110

The networks are same with Tabel 6 in paper.

Training

  • Creating ./dataset directory and downloading CIFAR10/CIFAR100 in it.
  • Using the script example_train_script.sh to train various KD methods. You can simply specify the hyper-parameters listed in train_xxx.py or manually change them.
  • The hyper-parameters I used can be found in the training logs (code: ezed).
  • Some Notes:
    • Sobolev/LwM alone is unstable and may be used in conjunction with other KD methods.
    • BSS may occasionally destroy the training procedure, leading to poor results.
    • If not specified in the original papers, all the methods can be used on the middle feature maps or multiple feature maps are only employed after the last conv layer. It is simple to extend to multiple feature maps.
    • I assume the size (C, H, W) of features between teacher and student are the same. If not, you could employ 1*1 conv, linear or pooling to rectify them.

Results

  • The trained baseline models are used as teachers. For fair comparison, all the student nets have same initialization with the baseline models.
  • The initial models, trained models and training logs are uploaded here (code: ezed).
  • The trade-off parameter --lambda_kd and other hyper-parameters are not chosen carefully. Thus the following results do not reflect which method is better than the others.
  • Some relation based methods, e.g. PKT, RKD and CC, have less effectiveness on CIFAR100 dataset. It may be because there are more inter classes but less intra classes in one batch. You could increase the batch size, create memory bank or design advance batch sampling methods.
TeacherStudentNameCIFAR10CIFAR100
-resnet-20Baseline92.37%68.92%
resnet-20resnet-20Logits93.30%70.36%
resnet-20resnet-20ST93.12%70.27%
resnet-20resnet-20AT92.89%69.70%
resnet-20resnet-20Fitnet92.73%70.08%
resnet-20resnet-20NST92.79%69.21%
resnet-20resnet-20PKT92.50%69.25%
resnet-20resnet-20FSP92.76%69.61%
resnet-20resnet-20FT92.98%69.90%
resnet-20resnet-20RKD92.72%69.48%
resnet-20resnet-20AB93.04%69.96%
resnet-20resnet-20SP92.88%69.85%
resnet-20resnet-20Sobolev92.78%69.39%
resnet-20resnet-20BSS92.58%69.96%
resnet-20resnet-20CC93.01%69.27%
resnet-20resnet-20LwM92.80%69.23%
resnet-20resnet-20IRG92.77%70.37%
resnet-20resnet-20VID92.61%69.39%
resnet-20resnet-20OFD92.82%69.93%
resnet-20resnet-20AFD92.56%69.63%
resnet-20resnet-20CRD92.96%70.33%
TeacherStudentNameCIFAR10CIFAR100
-resnet-20Baseline92.37%68.92%
-resnet-110Baseline93.86%73.15%
resnet-110resnet-20Logits92.98%69.78%
resnet-110resnet-20ST92.82%70.06%
resnet-110resnet-20AT93.21%69.28%
resnet-110resnet-20Fitnet93.04%69.81%
resnet-110resnet-20NST92.83%69.31%
resnet-110resnet-20PKT93.01%69.31%
resnet-110resnet-20FSP92.78%69.78%
resnet-110resnet-20FT93.01%69.49%
resnet-110resnet-20RKD93.21%69.36%
resnet-110resnet-20AB92.96%69.41%
resnet-110resnet-20SP93.30%69.45%
resnet-110resnet-20Sobolev92.60%69.23%
resnet-110resnet-20BSS92.78%69.71%
resnet-110resnet-20CC92.98%69.33%
resnet-110resnet-20LwM92.52%69.11%
resnet-110resnet-20IRG93.13%69.36%
resnet-110resnet-20VID92.98%69.49%
resnet-110resnet-20OFD93.13%69.81%
resnet-110resnet-20AFD92.92%69.60%
resnet-110resnet-20CRD92.92%70.80%
TeacherStudentNameCIFAR10CIFAR100
-resnet-110Baseline93.86%73.15%
resnet-110resnet-110Logits94.38%74.89%
resnet-110resnet-110ST94.59%74.33%
resnet-110resnet-110AT94.42%74.64%
resnet-110resnet-110Fitnet94.43%73.63%
resnet-110resnet-110NST94.43%73.55%
resnet-110resnet-110PKT94.35%73.74%
resnet-110resnet-110FSP94.39%73.59%
resnet-110resnet-110FT94.30%74.72%
resnet-110resnet-110RKD94.39%73.78%
resnet-110resnet-110AB94.63%73.91%
resnet-110resnet-110SP94.45%74.07%
resnet-110resnet-110Sobolev94.26%73.14%
resnet-110resnet-110BSS94.19%73.87%
resnet-110resnet-110CC94.49%74.43%
resnet-110resnet-110LwM94.19%73.28%
resnet-110resnet-110IRG94.44%74.96%
resnet-110resnet-110VID94.25%73.63%
resnet-110resnet-110OFD94.38%74.11%
resnet-110resnet-110AFD94.44%73.90%
resnet-110resnet-110CRD94.30%75.44%
Net1Net2NameCIFAR10CIFAR100
-resnet-20baseline92.37%68.92%
-resnet-110baseline93.86%73.15%
resnet20resnet20DML93.07%/93.37%70.39%/70.22%
resnet110resnet20DML94.45%/92.92%74.53%/70.29%
resnet110resnet110DML94.74%/94.79%74.72%/75.55%

Todo List

  • KDSVD (now has some bugs)
  • QuEST: Quantized Embedding Space for Transferring Knowledge
  • EEL: Learning an Evolutionary Embedding via Massive Knowledge Distillation
  • OnAdvFD: Feature-map-level Online Adversarial Knowledge Distillation
  • CS-KD: Regularizing Class-wise Predictions via Self-knowledge Distillation
  • PAD: Prime-Aware Adaptive Distillation
  • DCM: Knowledge Transfer via Dense Cross-Layer Mutual-Distillation
  • ESKD: On the Efficacy of Knowledge Distillation
  • GKA: Teachers Do More Than Teach: Compressing Image-to-Image Models
  • KD-fn: Feature Normalized Knowledge Distillation for Image Classification
  • FSD: Knowledge Distillation via Adaptive Instance Normalization
  • CSKD: Improving Knowledge Distillation via Category Structure
  • CD & GKD: Channel Distillation: Channel-Wise Attention for Knowledge Distillation
  • CRCD: Complementary Relation Contrastive Distillation
  • MGD: Matching Guided Distillation
  • SSKD: Knowledge Distillation Meets Self-Supervision
  • KR: Distilling Knowledge via Knowledge Review
  • AFD-SAD: Show, Attend and Distill: Knowledge Distillation via Attention-based Feature Matching
  • SRRL: Knowledge Distillation via Softmax Regression Representation Learning
  • SemCKD: Cross-Layer Distillation with Semantic Calibration
  • SKD: Reducing the Teacher-Student Gap via Spherical Knowledge Disitllation
  • IFDM: Heterogeneous Knowledge Distillation using Information Flow Modeling
  • LKD: Local Correlation Consistency for Knowledge Distillation
  • HKD: Distilling Holistic Knowledge with Graph Neural Networks
  • LONDON: Lipschitz Continuity Guided Knowledge Distillation
  • CDD: Channel-Wise Knowledge Distillation for Dense Prediction
  • SCKD: Student Customized Knowledge Distillation: Bridging the Gap Between Student and Teacher
  • ICKD: Exploring Inter-Channel Correlation for Diversity-preserved Knowledge Distillation
  • DKMF: Distilling Knowledge by Mimicking Features
  • CRCD: Complementary Relation Contrastive Distillation
  • WCoRD: Wasserstein Contrastive Representation Distillation
  • LKD: Local Correlation Consistency for Knowledge Distillation
  • KDCL: Online Knowledge Distillation via Collaborative Learning
  • ONE: Knowledge Distillation by On-the-Fly Native Ensemble

Requirements

  • python 3.7
  • pytorch 1.3.1
  • torchvision 0.4.2

Acknowledgements

This repo is partly based on the following repos, thank the authors a lot.

If you employ the listed KD methods in your research, please cite the corresponding papers.

distillation
kd
kd-methods
knowledge-distillation
knowledge-transfer
model-compression
teacher-student

Significant stargazers

Kehan Lu

152 followers · starred Oct 2021

Taebaek Hwang

42 followers · starred Oct 2020

Hong Zhang

36 followers · starred Apr 2021

Lihe Yang

666 followers · starred Sep 2021