Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Making a ViT learn from less
The original ViT needed hundreds of millions of images. DeiT trains one on about a million by being clever with data and teachers:
- it creates many varied versions of each image (crops, mixes, erasures);
- it learns from a convolutional network that already knows images well, through an extra “distillation token” that copies the teacher’s answers.
The research exercise shows how much a teacher helps when labels are scarce. The demo shows the token counts for patch grids.
Most engineering vision problems have limited labelled data. DeiT’s recipe, strong augmentation plus a teacher model, is a practical template for training data-hungry models in such settings.
- 1. DeiT trained a ViT on…
- 2. DeiT's extra token learns from…
- 3. A good teacher for DeiT was…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Tokens: 196 patches + class token + distillation token = 198 tokens. The two tokens have separate heads.
- Hard distillation loss:
- L = ½ CE(class head, true label) + ½ CE(distillation head, teacher’s argmax);
- at test time the two heads’ softmaxes are averaged.
- Augmentation and regularisation:
- RandAugment and random erasing;
- Mixup: blend two images and their labels, x = λx₁ + (1 − λ)x₂;
- CutMix: paste a box from another image, with label weight 1 − box area / image area, so a 112 × 112 box in 224 × 224 gives 0.75;
- stochastic depth with drop-path rates rising linearly to 0.1;
- repeated augmentation.
- Teachers: a RegNetY-16GF CNN teacher gave better students than a ViT teacher, and hard labels worked better than soft ones in DeiT’s experiments.
- Cost: DeiT-B trained in about 3 days on 8 GPUs (24 GPU-days). Models: Ti 5 M, S 22 M, B 86 M parameters.
- 1. Tokens in DeiT with distillation (196 patches):
- 2. CutMix label weight for a 112 box in a 224 image:
- 3. DeiT preferred distillation labels that are…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.DeiT in code
Exercise 1.
- The token count was 198.
- The hard distillation loss was near 0 when model and teacher agreed with the labels, and large when the teacher disagreed.
- Mixup blended inputs and labels with weight 0.3.
Practice.
timmprovides DeiT models (deit_base_distilled_patch16_224) and the full recipe:--aug-repeats,--mixup,--cutmix,--drop-path.- For your own data, a strong pre-trained CNN or ViT teacher, plus unlabelled in-domain images labelled by it, is often the biggest single gain (exercise 2).
- AdamW with large weight decay (0.05), cosine schedule and warmup are part of the recipe.
import timm
student = timm.create_model("deit_small_distilled_patch16_224", num_classes=4)
cls_out, dist_out = student(images) # two heads (training mode)
loss = 0.5 * F.cross_entropy(cls_out, labels) + 0.5 * F.cross_entropy(dist_out, teacher(images).argmax(1))- 1. DeiT's optimiser:
- 2. At test time DeiT combines…
- 3. Biggest practical gain with scarce labels:
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Why augmentation and regularisation.
- A ViT has weak built-in priors (E12-22), so with a million images it overfits easily.
- Augmentation multiplies the effective data with label-preserving changes. Mixup and CutMix smooth the decision boundaries. Stochastic depth and weight decay regularise.
- Together they replace part of the data that a priors-free model needs.
Why a CNN teacher helps.
- The teacher’s predictions encode a CNN’s inductive biases (locality, translation equivariance) and its learned knowledge.
- Distillation passes these to the student through the labels, and DeiT found a CNN teacher better than a stronger ViT teacher.
- The teacher can label images and augmented views beyond the labelled set, giving more training signal, the same effect as E12-7’s transfer set.
The distillation token. Separate class and distillation tokens let the model fit the true labels and the teacher’s labels in different representations. Their learned embeddings start dissimilar and converge partially (cosine similarity about 0.93 in the final layer).
- 1. Augmentation helps a ViT because it…
- 2. A CNN teacher passes on…
- 3. Class and distillation tokens…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.What helps a ViT with few labels?
History.
- H. Touvron, M. Cord, M. Douze, F. Massa, A. Sablayrolles and H. Jégou, “Training data-efficient image Transformers and distillation through attention” (ICML 2021).
- DeiT III (2022) revisited the recipe and closed more of the gap without distillation.
Exercise 2.
- Task: the layout task from E12-22 (is the bright square in the top half?) with only 48 labelled images.
- Model: a one-layer ViT with position embeddings, trained for 300 steps:
- plain;
- with label-preserving augmentation (horizontal flips and horizontal shifts by whole patches);
- with augmentation plus hard distillation, where a teacher trained on plentiful data labels 32 fresh unlabelled images per batch.
| training | layout accuracy (48 labels) | (seed 1) | (256 labels) |
|---|---|---|---|
| teacher (plenty of data) | 1.000 | 1.000 | 1.000 |
| plain | 0.504 | 0.538 | 0.574 |
| augmentation | 0.510 | 0.560 | 0.694 |
| augmentation + hard distillation | 1.000 | 1.000 | 1.000 |
The code runs the 48-label setting with seed 0; the other columns come from native runs.
- With 48 labels the ViT does not learn the layout, plain or augmented: it is near chance.
- Augmentation starts to help with more labels: 0.57 to 0.69 with 256.
- Hard distillation solves the task in every setting. The teacher’s labels on unlabelled images supply the data the student lacks, consistent with DeiT’s large gains from a teacher and with E12-7.
DeiT in AI, robotics and aerospace
- Limited labelled imagery: inspection and remote-sensing teams can combine strong augmentation, a pre-trained teacher and unlabelled imagery to train compact ViTs.
- Edge deployment: DeiT-Tiny and Small fit embedded inference budgets while keeping Transformer flexibility.
- Teacher choice: a well-performing CNN can transfer its spatial priors to a ViT student.
References
- H. Touvron et al. (2021). “Training data-efficient image Transformers and distillation through attention.” Proc. ICML.
- H. Touvron, M. Cord, H. Jégou (2022). “DeiT III: revenge of the ViT.” Proc. ECCV.
- H. Zhang, M. Cisse, Y. Dauphin, D. Lopez-Paz (2018). “mixup: beyond empirical risk minimization.” Proc. ICLR.