Home

Awesome

DataFree

A benchmark of data-free knowledge distillation from paper "Contrastive Model Inversion for Data-Free Knowledge Distillation"

Authors: Gongfan Fang, Jie Song, Xinchao Wang, Chengchao Shen, Xingen Wang, Mingli Song

CMI (this work)DeepInv
<img src="assets/cmi.png"><img src="assets/deepinv.png">
ZSKTDFQ
<img src="assets/zskt.png"><img src="assets/dfq.png">

Results

1. CIFAR-10

Methodresnet-34 <br> resnet-18vgg-11 <br> resnet-18wrn-40-2 <br> wrn-16-1wrn-40-2 <br> wrn-40-1wrn-40-2 <br> wrn-16-2
T. Scratch95.7092.2594.8794.8794.87
S. Scratch95.2095.2091.1293.9493.95
DAFL92.2281.1065.7181.3381.55
ZSKT93.3289.4683.7486.0789.66
DeepInv93.2690.3683.0486.8589.72
DFQ94.6190.8486.1491.6992.01
CMI94.8491.1390.0192.7892.52

2. CIFAR-100

Methodresnet-34 <br> resnet-18vgg-11 <br> resnet-18wrn-40-2 <br> wrn-16-1wrn-40-2 <br> wrn-40-1wrn-40-2 <br> wrn-16-2
T. Scratch78.0571.3275.8375.8375.83
S. Scratch77.1077.0165.3172.1973.56
DAFL74.4757.2922.5034.6640.00
ZSKT67.7434.7230.1529.7328.44
DeepInv61.3254.1353.7761.3361.34
DFQ77.0168.3254.7762.9259.01
CMI77.0470.5657.9168.8868.75

Quick Start

1. Visualize the inverted samples

Results will be saved as checkpoints/datafree-cmi/synthetic-cmi_for_vis.png

bash scripts/cmi/cmi_cifar10_for_vis.sh

2. Reproduce our results

Note: This repo was refactored from our experimental code and is still under development. I'm struggling to find the appropriate hyperparams for every methods (°ー°〃). So far, we only provide the hyperparameters to reproduce CIFAR-10 results for wrn-40-2 => wrn-16-1. You may need to tune the hyper-parameters for other models and datasets. More resources will be uploaded in the future update.

To reproduce our results, please download pre-trained teacher models from Dropbox-Models (266 MB) and extract them as checkpoints/pretrained. Also a pre-inverted data set with ~50k samples is available for wrn-40-2 teacher on CIFAR-10. You can download it from Dropbox-Data-Preinverted (133 MB) and extract them to run/cmi-preinverted-wrn402/.

3. Scratch training

python train_scratch.py --model wrn40_2 --dataset cifar10 --batch-size 256 --lr 0.1 --epoch 200 --gpu 0

4. Vanilla KD

# KD with original training data (beta>0 to use hard targets)
python vanilla_kd.py --teacher wrn40_2 --student wrn16_1 --dataset cifar10 --transfer_set cifar10 --beta 0.1 --batch-size 128 --lr 0.1 --epoch 200 --gpu 0 

# KD with unlabeled data
python vanilla_kd.py --teacher wrn40_2 --student wrn16_1 --dataset cifar10 --transfer_set cifar100 --beta 0 --batch-size 128 --lr 0.1 --epoch 200 --gpu 0 

# KD with unlabeled data from a specified folder
python vanilla_kd.py --teacher wrn40_2 --student wrn16_1 --dataset cifar10 --transfer_set run/cmi --beta 0 --batch-size 128 --lr 0.1 --epoch 200 --gpu 0 

5. Data-free KD

bash scripts/xxx/xxx.sh # e.g. scripts/zskt/zskt_cifar10_wrn402_wrn161.sh

Hyper-parameters used by different methods:

MethodadvbnohbalanceactcrGANExample
DAFL---scripts/dafl_cifar10.sh
ZSKT-----scripts/zskt_cifar10.sh
DeepInv----scripts/deepinv_cifar10.sh
DFQ--scripts/dfq_cifar10.sh
CMI--scripts/cmi_cifar10_scratch.sh

4. Use your models/datasets

You can register your models and datasets in registry.py by modifying NORMALIZE_DICT, MODEL_DICT and get_dataset. Then you can run the above commands to train your own models. As DAFL requires intermediate features from the penultimate layer, your model should accept an return_features=True parameter and return a (logits, features) tuple for DAFL.

5. Implement your algorithms

Your algorithms should inherent datafree.synthesis.BaseSynthesizer to implement two interfaces: 1) BaseSynthesizer.synthesize takes several steps to craft new samples and return an image dict for visualization; 2) BaseSynthesizer.sample fetches a batch of training data for KD.

Citation

If you found this work useful for your research, please cite our paper:

@article{fang2021contrastive,
  title={Contrastive Model Inversion for Data-Free Knowledge Distillation},
  author={Fang, Gongfan and Song, Jie and Wang, Xinchao and Shen, Chengchao and Wang, Xingen and Song, Mingli},
  journal={arXiv preprint arXiv:2105.08584},
  year={2021}
}

Reference