torchdistill

A coding-free framework built on PyTorch for reproducible deep learning studies. PyTorch Ecosystem. 🏆26 knowledge distillation methods presented at CVPR, ICLR, ECCV, NeurIPS, ICCV, etc are implemented so far. 🎁 Trained models, training logs and configurations are available for ensuring the reproducibiliy and benchmark.

https://github.com/yoshitomo-matsubara/torchdistill

Science Score: 59.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
    Found 10 DOI reference(s) in README
  • Academic publication links
    Links to: arxiv.org, springer.com, ieee.org, acm.org, zenodo.org
  • Committers with academic emails
    2 of 6 committers (33.3%) from academic institutions
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (17.2%) to scientific vocabulary

Keywords

amazon-sagemaker-lab cifar10 cifar100 coco colab-notebook glue google-colab image-classification imagenet knowledge-distillation natural-language-processing nlp object-detection pascal-voc pytorch pytorch-ecosystem semantic-segmentation text-classification transformer
Last synced: 6 months ago · JSON representation

Repository

A coding-free framework built on PyTorch for reproducible deep learning studies. PyTorch Ecosystem. 🏆26 knowledge distillation methods presented at CVPR, ICLR, ECCV, NeurIPS, ICCV, etc are implemented so far. 🎁 Trained models, training logs and configurations are available for ensuring the reproducibiliy and benchmark.

Basic Info
Statistics
  • Stars: 1,543
  • Watchers: 18
  • Forks: 135
  • Open Issues: 0
  • Releases: 27
Topics
amazon-sagemaker-lab cifar10 cifar100 coco colab-notebook glue google-colab image-classification imagenet knowledge-distillation natural-language-processing nlp object-detection pascal-voc pytorch pytorch-ecosystem semantic-segmentation text-classification transformer
Created about 6 years ago · Last pushed 6 months ago
Metadata Files
Readme License Citation

README.md

torchdistill logo

torchdistill: A Modular, Configuration-Driven Framework for Knowledge Distillation

PyPI version Build Status GitHub Discussions DOI:10.1007/978-3-030-76423-4_3 DOI:10.18653/v1/2023.nlposs-1.18

torchdistill (formerly kdkit) offers various state-of-the-art knowledge distillation methods and enables you to design (new) experiments simply by editing a declarative yaml config file instead of Python code. Even when you need to extract intermediate representations in teacher/student models, you will NOT need to reimplement the models, that often change the interface of the forward, but instead specify the module path(s) in the yaml file. Refer to these papers for more details.

In addition to knowledge distillation, this framework helps you design and perform general deep learning experiments (WITHOUT coding) for reproducible deep learning studies. i.e., it enables you to train models without teachers simply by excluding teacher entries from a declarative yaml config file. You can find such examples below and in configs/sample/.

In December 2023, torchdistill officially joined PyTorch Ecosystem.

When you refer to torchdistill in your paper, please cite these papers instead of this GitHub repository.
If you use torchdistill as part of your work, your citation is appreciated and motivates me to maintain and upgrade this framework!

Documentation

You can find the API documentation and research projects that leverage torchdistill at https://yoshitomo-matsubara.net/torchdistill/

Forward hook manager

Using ForwardHookManager, you can extract intermediate representations in model without modifying the interface of its forward function.
This example notebook Open In Colab Open In Studio Lab will give you a better idea of the usage such as knowledge distillation and analysis of intermediate representations.

E.g., extract intermediate representations (feature map) of ResNet-18 for a random input batch ```python import torch from torchvision import models from torchdistill.core.forward_hook import ForwardHookManager

Define a model and choose torch device

model = models.resnet18(pretrained=False) device = torch.device('cpu')

Register forward hooks for modules of your interest

forwardhookmanager = ForwardHookManager(device) forwardhookmanager.addhook(model, 'conv1', requiresinput=True, requiresoutput=False) forwardhookmanager.addhook(model, 'layer1.0.bn2', requiresinput=True, requiresoutput=True) forwardhookmanager.addhook(model, 'fc', requiresinput=False, requires_output=True)

Define a random input batch and run the model

x = torch.rand(32, 3, 224, 224) y = model(x)

Extract input and/or output of the modules

iodict = forwardhookmanager.popiodict() conv1input = iodict['conv1']['input'] layer10bn2input = iodict['layer1.0.bn2']['input'] layer10bn2output = iodict['layer1.0.bn2']['output'] fcoutput = io_dict['fc']['output'] ```

1 experiment → 1 declarative PyYAML config file

In torchdistill, many components and PyTorch modules are abstracted e.g., models, datasets, optimizers, losses, and more! You can define them in a declarative PyYAML config file so that can be seen as a summary of your experiment, and in many cases, you will NOT need to write Python code at all. Take a look at some configurations available in configs/. You'll see what modules are abstracted and how they are defined in a declarative PyYAML config file to design an experiment.

E.g., instantiate CIFAR-10 datasets with a declarative PyYAML config file python from torchdistill.common import yaml_util config = yaml_util.load_yaml_file('./test.yaml') train_dataset = config['datasets']['cifar10/train'] test_dataset = config['datasets']['cifar10/test']

test.yaml yaml datasets: cifar10/train: !import_call key: 'torchvision.datasets.CIFAR10' init: kwargs: root: &root_dir '~/datasets/cifar10' train: True download: True transform: !import_call key: 'torchvision.transforms.Compose' init: kwargs: transforms: - !import_call key: 'torchvision.transforms.RandomCrop' init: kwargs: size: 32 padding: 4 - !import_call key: 'torchvision.transforms.RandomHorizontalFlip' init: kwargs: p: 0.5 - !import_call key: 'torchvision.transforms.ToTensor' init: - !import_call key: 'torchvision.transforms.Normalize' init: kwargs: &normalize_kwargs mean: [0.49139968, 0.48215841, 0.44653091] std: [0.24703223, 0.24348513, 0.26158784] cifar10/test: !import_call key: 'torchvision.datasets.CIFAR10' init: kwargs: root: *root_dir train: False download: True transform: !import_call key: 'torchvision.transforms.Compose' init: kwargs: transforms: - !import_call key: 'torchvision.transforms.ToTensor' init: - !import_call key: 'torchvision.transforms.Normalize' init: kwargs: *normalize_kwargs

If you want to use your own modules (models, loss functions, datasets, etc) with this framework, you can do so without editing code in the local package torchdistill/.
See the official documentation and Discussions for more details.

Benchmarks

Top-1 validation accuracy for ILSVRC 2012 (ImageNet)

Examples

Executable code can be found in examples/ such as - Image classification: ImageNet (ILSVRC 2012), CIFAR-10, CIFAR-100, etc - Object detection: COCO 2017, etc - Semantic segmentation: COCO 2017, PASCAL VOC, etc - Text classification: GoEmotions, etc - GLUE: CoLA, SST-2, MRPC, STS-B, QQP, MNLI, QNLI, RTE, WNLI, AX

For CIFAR-10 and CIFAR-100, some models are reimplemented and available as pretrained models in torchdistill. More details can be found here.

Some Transformer models fine-tuned by torchdistill for GLUE tasks are available at Hugging Face Model Hub. Sample GLUE benchmark results and details can be found here.

Google Colab Examples

The following examples are available in demo/. Note that these examples are for Google Colab users and compatible with Amazon SageMaker Studio Lab. Usually, examples/ would be a better reference if you have your own GPU(s).

CIFAR-10 and CIFAR-100

  • Training without teacher models Open In Colab Open In Studio Lab
  • Knowledge distillation Open In Colab Open In Studio Lab

GLUE

  • Fine-tuning without teacher models Open In Colab Open In Studio Lab
  • Knowledge distillation Open In Colab Open In Studio Lab

These examples write out test prediction files for you to see the test performance at the GLUE leaderboard system.

PyTorch Hub

If you find models on PyTorch Hub or GitHub repositories supporting PyTorch Hub, you can import them as teacher/student models simply by editing a declarative yaml config file.

e.g., If you use a pretrained ResNeSt-50 available in huggingface/pytorch-image-models (aka timm) as a teacher model for ImageNet dataset, you can import the model via PyTorch Hub with the following entry in your declarative yaml config file.

yaml models: teacher_model: key: 'resnest50d' repo_or_dir: 'huggingface/pytorch-image-models' kwargs: num_classes: 1000 pretrained: True

How to setup

  • Python >= 3.9
  • pipenv (optional)

Install by pip/pipenv

``` pip3 install torchdistill

or use pipenv

pipenv install torchdistill ```

Install from this repository (not recommended)

``` git clone https://github.com/yoshitomo-matsubara/torchdistill.git cd torchdistill/ pip3 install -e .

or use pipenv

pipenv install "-e ." ```

Issues / Questions / Requests / Pull Requests

Feel free to create an issue if you find a bug.
If you have either a question or feature request, start a new discussion here. Please search through Issues and Discussions and make sure your issue/question/request has not been addressed yet.

Pull requests are welcome. Please start with an issue and discuss solutions with me rather than start with a pull request.

Citation

If you use torchdistill in your research, please cite the following papers:
[Paper] [Preprint]
bibtex @inproceedings{matsubara2021torchdistill, title={{torchdistill: A Modular, Configuration-Driven Framework for Knowledge Distillation}}, author={Matsubara, Yoshitomo}, booktitle={International Workshop on Reproducible Research in Pattern Recognition}, pages={24--44}, year={2021}, organization={Springer} }

[Paper] [OpenReview] [Preprint]
bibtex @inproceedings{matsubara2023torchdistill, title={{torchdistill Meets Hugging Face Libraries for Reproducible, Coding-Free Deep Learning Studies: A Case Study on NLP}}, author={Matsubara, Yoshitomo}, booktitle={Proceedings of the 3rd Workshop for Natural Language Processing Open Source Software (NLP-OSS 2023)}, publisher={Empirical Methods in Natural Language Processing}, pages={153--164}, year={2023} }

Acknowledgments

This project has been supported by Travis CI's OSS credits and JetBrain's Free License Programs (Open Source) since November 2021 and June 2022, respectively.
PyCharm logo

References

Owner

  • Name: Yoshitomo Matsubara
  • Login: yoshitomo-matsubara
  • Kind: user
  • Location: Washington, United States

Applied Scientist at Amazon Alexa AI, Ph.D. in Computer Science

GitHub Events

Total
  • Create event: 2
  • Issues event: 6
  • Release event: 2
  • Watch event: 150
  • Delete event: 2
  • Issue comment event: 6
  • Push event: 83
  • Pull request event: 49
  • Fork event: 7
Last Year
  • Create event: 2
  • Issues event: 6
  • Release event: 2
  • Watch event: 150
  • Delete event: 2
  • Issue comment event: 6
  • Push event: 83
  • Pull request event: 49
  • Fork event: 7

Committers

Last synced: 9 months ago

All Time
  • Total Commits: 1,218
  • Total Committers: 6
  • Avg Commits per committer: 203.0
  • Development Distribution Score (DDS): 0.006
Past Year
  • Commits: 46
  • Committers: 2
  • Avg Commits per committer: 23.0
  • Development Distribution Score (DDS): 0.022
Top Committers
Name Email Commits
Yoshitomo Matsubara y****m@u****u 1,211
roymiles r****8@i****k 2
Alireza Furutanpey 6****u 2
Jingyu Lee d****0@g****m 1
Guocheng n****a@y****t 1
r50034835 r****s@h****m 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 25
  • Total pull requests: 212
  • Average time to close issues: about 20 hours
  • Average time to close pull requests: about 1 hour
  • Total issue authors: 19
  • Total pull request authors: 4
  • Average comments per issue: 1.76
  • Average comments per pull request: 0.02
  • Merged pull requests: 207
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 4
  • Pull requests: 38
  • Average time to close issues: about 3 hours
  • Average time to close pull requests: 10 minutes
  • Issue authors: 2
  • Pull request authors: 2
  • Average comments per issue: 1.5
  • Average comments per pull request: 0.0
  • Merged pull requests: 35
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • suhao16 (3)
  • cxchen100 (2)
  • 1396066796 (2)
  • Holmes2002 (2)
  • nirgoren (2)
  • yoshitomo-matsubara (2)
  • andynnnnn (1)
  • Calmepro777 (1)
  • jsrdcht (1)
  • sunshangquan (1)
  • KOOKOKOK (1)
  • jaideep11061982 (1)
  • nguyenvulong (1)
  • zhangruju (1)
  • Coderx7 (1)
Pull Request Authors
  • yoshitomo-matsubara (231)
  • roymiles (4)
  • rezafuru (3)
  • MostHumble (2)
Top Labels
Issue Labels
bug (8)
Pull Request Labels

Packages

  • Total packages: 2
  • Total downloads:
    • pypi 361 last-month
  • Total dependent packages: 2
    (may contain duplicates)
  • Total dependent repositories: 4
    (may contain duplicates)
  • Total versions: 55
  • Total maintainers: 1
proxy.golang.org: github.com/yoshitomo-matsubara/torchdistill
  • Versions: 27
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent packages count: 5.4%
Average: 5.6%
Dependent repos count: 5.8%
Last synced: 6 months ago
pypi.org: torchdistill

A Modular, Configuration-Driven Framework for Knowledge Distillation. Trained models, training logs and configurations are available for ensuring the reproducibility.

  • Versions: 28
  • Dependent Packages: 2
  • Dependent Repositories: 4
  • Downloads: 361 Last month
Rankings
Stargazers count: 2.0%
Dependent packages count: 3.2%
Forks count: 4.6%
Average: 5.8%
Dependent repos count: 7.7%
Downloads: 11.7%
Maintainers (1)
Last synced: 6 months ago

Dependencies

examples/hf_transformers/requirements.txt pypi
  • accelerate *
  • datasets >=1.1.3
  • pandas *
  • protobuf *
  • sentencepiece *
  • torch >=1.8.1
  • transformers >=4.6.1
setup.py pypi
  • cython *
  • numpy *
  • pycocotools >=2.0.2
  • pyyaml >=5.4.1
  • scipy *
  • torch >=1.11.0
  • torchvision >=0.12.0
.github/workflows/python-publish.yml actions
  • actions/checkout v2 composite
  • actions/setup-python v2 composite
  • pypa/gh-action-pypi-publish 27b31702a0e7fc50959f5ad993c78deac1bdfc29 composite
.github/workflows/documentation.yaml actions
  • actions/checkout v3 composite
  • actions/setup-python v2 composite
  • peaceiris/actions-gh-pages v3 composite
examples/legacy/hf_transformers/requirements.txt pypi
  • accelerate *
  • datasets >=1.1.3
  • pandas *
  • protobuf *
  • sentencepiece *
  • torch >=1.8.1
  • transformers >=4.6.1