Abstract
Conventional knowledge distillation (KD) approaches are designed for the student model to predict similar output as the teacher model for each sample. Unfortunately, the relationship across samples with same class is often neglected. In this paper, we explore to redefine the knowledge in distillation, capturing the relationship between each sample and its corresponding in-context samples (a group of similar samples with the same or different classes), and perform KD from an in-context sample retrieval perspective. As KD is a type of learned label smoothing regularization (LSR), we first conduct a theoretical analysis showing that the teacher's knowledge from the in-context samples is a crucial contributor to regularize the student training with the corresponding samples. Buttressed by the analysis, we propose a novel in-context knowledge distillation (IC-KD) framework that shows its superiority across diverse KD paradigms (offline, online, and teacher-free KD). Firstly, we construct a feature memory bank from the teacher model and retrieve in-context samples for each corresponding sample through retrieval-based learning. We then introduce Positive In-Context Distillation (PICD) to reduce the discrepancy between a sample from the student and the aggregated in-context samples with the same class from the teacher in the logit space. Moreover, Negative In-Context Distillation (NICD) is introduced to separate a sample from the student and the in-context samples with different classes from the teacher in the logit space. Extensive experiments demonstrate that IC-KD is effective across various types of KD, and consistently achieves state-of-the-art performance on CIFAR-100 and ImageNet datasets.
Introduction
Knowledge Distillation (KD) is a cornerstone technique for model compression, enabling a compact "student" model to inherit the performance of a larger "teacher" model. Traditionally, KD focuses on point-to-point supervision, where the student mimics the teacher's output for each individual training sample. However, this approach overlooks the rich relational information embedded in the data manifold—specifically, how a sample relates to its neighbors in the feature space.
The authors of this paper propose In-context Knowledge Distillation (IC-KD), a framework that redefines "knowledge" by incorporating inter-sample relationships. By retrieving and aggregating "in-context" samples from a teacher’s memory bank, IC-KD provides the student with a richer, more relational training signal that improves performance across classification and segmentation tasks.
Rethinking Knowledge Distillation
Traditional KD paradigms have evolved from simple point-to-point matching to capturing batch-wise correlations. However, IC-KD takes this a step further by using an explicit retrieval mechanism to find the most relevant neighbors for any given sample, regardless of whether they appear in the current training batch.
As illustrated in Figure 2, IC-KD differs from conventional, relational, and contrastive KD by focusing on the "neighborhood" structure. It treats KD as a form of learned label smoothing, where the optimal target for a student is not just the teacher's single-sample output, but an aggregation of outputs from similar in-context samples.
The IC-KD Framework
The IC-KD framework operates through a structured three-step process designed to refine the student's feature space:
- Feature Memory Bank & Retrieval: A memory bank of teacher features is constructed. For every training sample, retrieval-based learning identifies the most similar "in-context" samples—both positives (same class) and negatives (different classes).
- Positive In-Context Distillation (PICD): The student is trained to match an aggregated prediction derived from the teacher's outputs for the most similar same-class samples. This provides a more stable and generalized target than a single teacher logit.
- Negative In-Context Distillation (NICD): To sharpen decision boundaries, a contrastive loss is used to push the student's predictions away from the teacher's predictions of similar samples from different classes.
Experimental Results
The researchers evaluated IC-KD on major benchmarks including CIFAR-100, ImageNet, and Cityscapes, demonstrating its effectiveness across classification and semantic segmentation tasks.
- CIFAR-100: IC-KD consistently outperformed state-of-the-art methods. For a ResNet32x4 teacher and ResNet8x4 student, IC-KD achieved 77.22% accuracy, compared to the baseline student's 72.87% and standard KD's 73.33%.
- ImageNet: In a ResNet34 to ResNet18 setup, IC-KD reached 72.36% top-1 accuracy, surpassing previous benchmarks like NORM.
- Semantic Segmentation: On the Cityscapes dataset, IC-KD achieved a mean IoU (mIoU) of 76.89%, proving its utility in dense prediction tasks.
Conclusion
IC-KD represents a significant shift in how we conceptualize knowledge in distillation. By moving from individual sample matching to in-context sample retrieval, the framework captures the relational structure of the data manifold more effectively. This approach not only yields superior performance across various architectures and datasets but also maintains computational efficiency, making it a robust choice for real-world model compression.
Summary updated