ViTKD: Practical Guidelines for ViT feature knowledge distillation

Zhendong Yang, Zhe Li, Ailing Zeng, Zexian Li, Chun Yuan, Yu Li

Abstract

Knowledge Distillation (KD) for Convolutional Neural Network (CNN) is extensively studied as a way to boost the performance of a small model. Recently, Vision Transformer (ViT) has achieved great success on many computer vision tasks and KD for ViT is also desired. However, besides the output logit-based KD, other feature-based KD methods for CNNs cannot be directly applied to ViT due to the huge structure gap. In this paper, we explore the way of feature-based distillation for ViT. Based on the nature of feature maps in ViT, we design a series of controlled experiments and derive three practical guidelines for ViT's feature distillation. Some of our findings are even opposite to the practices in the CNN era. Based on the three guidelines, we propose our feature-based method ViTKD which brings consistent and considerable improvement to the student. On ImageNet-1k, we boost DeiT-Tiny from 74.42% to 76.06%, DeiT-Small from 80.55% to 81.95%, and DeiT-Base from 81.76% to 83.46%. Moreover, ViTKD and the logit-based KD method are complementary and can be applied together directly. This combination can further improve the performance of the student. Specifically, the student DeiT-Tiny, Small, and Base achieve 77.78%, 83.59%, and 85.41%, respectively.

Introduction

Knowledge Distillation (KD) is a powerful technique for transferring knowledge from a large "teacher" model to a smaller "student" model. While highly effective for Convolutional Neural Networks (CNNs), standard feature-based distillation methods often underperform when applied to Vision Transformers (ViTs). This discrepancy arises from a "structure gap": the attention mechanisms in ViTs produce sparse and complex feature patterns that a smaller student model struggles to replicate directly, especially in deeper layers. In fact, forcing a student to mimic a teacher's deep-layer features can sometimes degrade performance.

Methodology: The ViTKD Framework

To address these challenges, the authors propose ViTKD, a feature-based distillation method governed by three practical guidelines derived from extensive controlled experiments:

  1. Mimicking for Shallow Layers: In the initial layers, the student model directly mimics the teacher's features. A simple linear layer aligns the feature dimensions, helping the student learn fundamental attention patterns.
  2. Generation for Deep Layers: In deeper layers, where direct mimicking becomes counterproductive, the student is tasked with reconstructing the teacher's full feature map from a subset of its own masked tokens. This generative approach forces the student to learn the underlying logic of the teacher's representations.
  3. FFN-out over MHA-out: Distillation is most effective when applied to the output of the Feed-Forward Network (FFN) rather than the Multi-Head Attention (MHA) module.
The ViTKD framework: Mimicking for shallow layers and Generation for deep layers.

The total training objective combines standard classification loss with these specialized distillation losses (mimicking at the start, generation at the end).

Experimental Results

ViTKD was evaluated primarily on the ImageNet-1k dataset. The results demonstrate consistent improvements across various model scales. Notably, ViTKD is complementary to traditional logit-based KD; when combined, the performance gains are even more pronounced.

Top-1 Accuracy improvements across different ViT model sizes using ViTKD.

Key performance highlights include:

  • DeiT-Tiny: Accuracy increased from 74.42% to 76.06% (and up to 77.78% with logit KD).
  • DeiT-Small: Accuracy increased from 80.55% to 81.95% (reaching 83.59% with logit KD).
  • DeiT-Base: Accuracy increased from 81.76% to 83.46% (reaching 85.41% with logit KD).

Beyond classification, the distilled features proved robust for downstream tasks. On the COCO dataset, a DeiT-Small backbone trained with ViTKD improved object detection performance by 1.21 mAP, indicating that the distilled knowledge provides a better foundation for complex visual understanding.

Conclusion

ViTKD rethinks feature distillation for Vision Transformers by acknowledging the limitations of direct mimicking in deep layers. By introducing a generative reconstruction task for deep features and focusing on shallow layer mimicking, ViTKD sets a new standard for ViT distillation. Future work may explore more sophisticated generative architectures or methods to bridge the gap between heterogeneous teacher-student architectures.

Summary updated

BibTeX
@misc{yang2022vitkd,
  author = {Zhendong Yang and Zhe Li and Ailing Zeng and Zexian Li and Chun Yuan and Yu Li},
  title = {{ViTKD: Practical Guidelines for ViT feature knowledge distillation}},
  year = {2022},
  eprint = {2209.02432},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2209.02432},
}
Figure