The official implementation for: CKD: Contrastive Knowledge Distillation from A Sample-wise Perspective.
Environments:
- Python 3.8
- PyTorch 1.10.0
- torchvision 0.11.0
Install the package:
sudo pip3 install -r requirements.txt
sudo python3 setup.py develop
- Training on CIFAR-100
-
Download the
cifar_teachers.tarat https://github.com/ZhouXinzzzzz/CKD/releases/tag/checkpoints and untar it to./download_ckptsviatar xvf cifar_teachers.tar.# for instance, our CKD method. python3 tools/train.py --cfg configs/cifar100/ckd/res32x4_res8x4.yaml # you can also change settings at command line python3 tools/train.py --cfg configs/cifar100/ckd/res32x4_res8x4.yaml SOLVER.BATCH_SIZE 128 SOLVER.LR 0.1
- Training on ImageNet
-
Download the dataset at https://image-net.org/ and put them to
./data/imagenet# for instance, our CKD method. python3 tools/train.py --cfg configs/imagenet/r34_r18/ckd.yaml
- Training on Places365
-
Download the
places_teachers.tarat https://github.com/ZhouXinzzzzz/CKD/releases/tag/checkpoints and untar it to./download_ckptsviatar xvf places_teachers.tar.# for instance, our CKD method. python3 tools/train.py --cfg configs/places365/r34_r18/ckd.yaml
- Training on MS-COCO
- see detection.md
- Extension: Visualizations
- Jupyter notebooks: tsne and correlation_matrices
- create a python file at
mdistiller/distillers/and define the distiller
from ._base import Distiller
class MyDistiller(Distiller):
def __init__(self, student, teacher, cfg):
super(MyDistiller, self).__init__(student, teacher)
self.hyper1 = cfg.MyDistiller.hyper1
...
def forward_train(self, image, target, **kwargs):
# return the output logits and a Dict of losses
...
# rewrite the get_learnable_parameters function if there are more nn modules for distillation.
# rewrite the get_extra_parameters if you want to obtain the extra cost.
...-
regist the distiller in
distiller_dictatmdistiller/distillers/__init__.py -
regist the corresponding hyper-parameters at
mdistiller/engines/cfg.py -
create a new config file and test it.
If this repo is helpful for your research, please consider citing the paper:
@misc{zhu2025ckdcontrastiveknowledgedistillation,
title={CKD: Contrastive Knowledge Distillation from A Sample-wise Perspective},
author={Wencheng Zhu and Xin Zhou and Pengfei Zhu and Yu Wang and Qinghua Hu},
year={2025},
eprint={2404.14109},
archivePrefix={arXiv},
primaryClass={cs.CV},
url={https://arxiv.org/abs/2404.14109},
}- Thanks for DKD and ReviewKD. We build this library based on the DKD's codebase and the ReviewKD's codebase.