Skip to main content

PyTorch Vision Transformers with Distillation

Based on the paper "Training data-efficient image transformers & distillation through attention".

This repository will allow you to use distillation techniques with vision transformers in PyTorch. Most importantly, you can use pretrained models for the teacher, the student, or even both! My motivation was to use transfer learning to decrease the amount of resources it takes to train a vision transformer.

Quickstart

Install with pip install distillable_vision_transformer and load a pretrained transformer with:

from distillable_vision_transformer import DistillableVisionTransformer
model = DistillableVisionTransformer.from_pretrained('ViT-B_16')

Installation

Install via pip:

pip install distillable_vision_transformer

Or install from source:

git clone https://github.com/Graeme22/DistillableVisionTransformer.git
cd DistillableVisionTransformer
pip install -e .

Usage

Load a model architecture:

from distillable_vision_transformer import DistillableVisionTransformer
model = DistillableVisionTransformer.from_name('ViT-B_16')

Load a pretrained model:

from distillable_vision_transformer import DistillableVisionTransformer
model = DistillableVisionTransformer.from_pretrained('ViT-B_16')

Default hyper parameters:

Param\Model ViT-B_16 ViT-B_32 ViT-L_16 ViT-L_32
image_size 384, 384 384, 384 384, 384 384, 384
patch_size 16 32 16 32
emb_dim 768 768 1024 1024
mlp_dim 3072 3072 4096 4096
num_heads 12 12 16 16
num_layers 12 12 24 24
num_classes 1000 1000 1000 1000
attn_dropout_rate 0.0 0.0 0.0 0.0
dropout_rate 0.1 0.1 0.1 0.1

If you need to modify these hyperparameters, just overwrite them:

model = DistillableVisionTransformer.from_name('ViT-B_16', patch_size=64, emb_dim=2048, ...)

Training

Wrap the student (instance of DistillableVisionTransformer) and the teacher (any network that you want to use to train the student) with a DistillationTrainer:

from distillable_vision_transformer import DistillableVisionTransformer, DistillationTrainer

student = DistillableVisionTransformer.from_pretrained('ViT-B_16')
trainer = DistillationTrainer(teacher=teacher, student=student) # where teacher is some pretrained network, e.g. an EfficientNet

For the loss function, it is recommended that you use the DistilledLoss class, which is a kind of hybrid between cross-entropy and KL-divergence loss. It takes as arguments teacher_logits, student_logits, and distill_logits, which are obtained from the forward pass on DistillationTrainer, as well as the true labels labels.

from distillable_vision_transformer import DistilledLoss

loss_fn = DistilledLoss(alpha=0.5, temperature=3.0)
loss = loss_fn(teacher_logits, student_logits, distill_logits, labels)

Inference

For inference, we want to use the DistillableVisionTransformer instance, not its DistillationTrainer wrapper.

import torch
from distillable_vision_transformer import DistillableVisionTransformer

model = DistillableVisionTransformer.from_pretrained('ViT-B_16')
model.eval()

inputs = torch.rand(1, 3, *model.image_size)
# we can discard the distillation tokens, as they are only needed to calculate loss
outputs, _ = model(inputs)

Contributing

If you find a bug, create a GitHub issue, or even better, submit a pull request. Similarly, if you have questions, simply post them as GitHub issues.

Metadata

Release files for distillable-vision-transformer 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for distillable-vision-transformer 1.0.1
File Size Uploaded
distillable_vision_transformer-1.0.1.tar.gz 8.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for distillable-vision-transformer 1.0.1
File Interpreter ABI Platform
distillable_vision_transformer-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 20.8 kB

Release files / distillable_vision_transformer-1.0.1.tar.gz

Download URL distillable_vision_transformer-1.0.1.tar.gz
Size 8.7 kB
Tags Source
SHA-256 checksum
How to use checksums
a7cc2daa4f455ab3705080c432e5a88db012764bb3de59968c894c621ba5f572
BLAKE2b-256 checksum
How to use checksums
bbdba79003717000e0b32820bd1a1eb38dd72e9f76215b336d7b1609513d0afe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

Release files / distillable_vision_transformer-1.0.1-py3-none-any.whl

Download URL distillable_vision_transformer-1.0.1-py3-none-any.whl
Size 12.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c853401eae3548f0f713680147537db4337515eb399def47ac5a7d4099bca6bb
BLAKE2b-256 checksum
How to use checksums
eb79a5212a21a0c6cc9ec76e93ddffbb30440778bf07fead70fc018526269a6f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page