Knowledge Distillation from A Stronger Teacher
Abstract
Unlike existing knowledge distillation methods focus on the baseline settings, where the teacher models and training strategies are not that strong and competing as state-of-the-art approaches, this paper presents a method dubbed DIST to distill better from a stronger teacher.
We empirically find that the discrepancy of predictions between the student and a stronger teacher may tend to be fairly severer.
As a result, the exact match of predictions in KL divergence would disturb the training and make existing methods perform poorly.
In this paper, we show that simply preserving the relations between the predictions of teacher and student would suffice, and propose a correlation-based loss to capture the intrinsic inter-class relations from the teacher explicitly.
Besides, considering that different instances have different semantic similarities to each class, we also extend this relational match to the intra-class level.
Our method is simple yet practical, and extensive experiments demonstrate that it adapts well to various architectures, model sizes and training strategies, and can achieve state-of-the-art performance consistently on image classification, object detection, and semantic segmentation tasks.
Code is available at: https://github.com/hunto/DIST_KD.
Introduction
The advent of automatic feature engineering fuels deep neural networks to achieve remarkable success in a plethora of computer vision tasks, such as image classification [17, 19, 38, 48, 53], object detection [2, 23] and semantic segmentation [5, 54].
In the path of pursuing better performance, current deep learning models generally grow deeper and wider [13, 45].
However, such heavy models are clumsy to deploy in practice due to the limitations of computational and memory resources.
For an efficient model with competitive performance to those larger models, knowledge distillation (KD) [16] has been proposed to boost the performance of the efficient model (student) by distilling the knowledge of a larger model (teacher) during training.
The essence of knowledge distillation relies on how to formulate and transfer the knowledge from teacher to student.
The most intuitive yet effective approach is to match the probabilistic prediction (response) scores between the teacher and student via Kullback–Leibler (KL) divergence [16].
In this way, the student can be guided with more informative signals during training, and is thus expected to have more promising performance than that being trained stand-alone.
Besides this vanilla prediction match, other works [11, 14, 34, 41] also investigate the knowledge within intermediate representations to further boost the distillation performance, but this usually induces additional training cost as a consequence.
For example, OFD [14] proposes to distill the information via multiple intermediate layers, but requires additional convolutions for feature alignments; CRD [41] introduces a contrastive loss to transfer pair-wise relationships, but it needs to hold a memory bank for all 128-d features of
ImageNet images, and produces additional 260M FLOPs of computation cost.
[Table]
[Figure]
Recently, a few studies [8, 29, 39] have been performed to address the poor learning issue of the student network when the student and teacher model sizes significantly differ.
For example,
TAKD [29] proposes to reduce the discrepancy of teacher and student by resorting to an additional teaching assistant of moderate model size; DGKD [39] further improves TAKD by densely gathering all the assistant models to guide the student.
However, increasing the model size is only one of the popular approaches to have a stronger teacher.
There lacks a thorough analysis on the training strategies to derive a stronger teacher and their effect on KD.
Most importantly, a generic enough solution is preferred to address the difficulty of KD brought by stronger teachers, rather than struggling to deal with different types of stronger teachers (with larger model size or stronger training strategy) individually.
To understand what makes a stronger teacher and their effect on KD, we systematically study the prevalent strategies for designing and training deep neural networks, and show that:
• Beyond scaling up the model size, a stronger teacher can also be derived through advanced training strategies, e
g. , label smoothing and data augmentation [51].
However, given a stronger teacher, the student’s performance on the vanilla KD could be dropped, even worse than training from scratch without KD, as shown in Figure 1.
• The discrepancy between teacher and student tends to get fairly larger when we switch their training strategy to a stronger one (see Figure 2).
In this case, an exact recovery of predictions via KL divergence could be challenging and lead to the failure of vanilla KD.
• Preserving the relation of predictions between teacher and student is sufficient and effective.
When transferring the knowledge from teacher to student, what we really care about is preserving the preference (relative ranks of predictions) by the teacher, instead of recovering the absolute values accurately.
Correlation between teacher and student predictions could be favored to relax the exact match of KL divergence and distill the intrinsic relations.
In this paper, we thus leverage the Pearson correlation coefficient [33] as a new match manner to replace the KL divergence.
In addition, besides the inter-class relations in prediction vector (see
Figure 3), with the intuition that different instances have different spectrum of similarities with respect to each class, we also propose to distill the intra-class relations for further boosting the performance as Figure 3.
Concretely, for each class, we gather its corresponding predicted probabilities of all instances in a batch, then transfer this relation from teacher to student.
Our proposed method (dubbed
DIST) is super simple, efficient, and practical, which can be implemented with only several lines of code (see Appendix A.1) and has almost the same training cost as the vanilla KD.
As a result, the student can be liberated from the burden of matching the exact output of a strong teacher, but only be guided appropriately to distill those truly informative relations.
Extensive experiments are conducted on benchmark datasets to verify our effectiveness on various tasks, including image classification, object detection, and semantic segmentation.
Experimental results show that our DIST significantly outperforms vanilla KD and those sophisticatedly-designed state-of-the-art KD methods.
For example, with the same baseline settings on ImageNet, our DIST
[Figure]
i
[Figure]
n
[Figure]
τ
[Figure] [Figure]
achieves the highest 72.07% accuracy on ResNet-18.
With the stronger strategy, our method obtains 82.3% accuracy on the recent transformer Swin-T [27], improving KD by 1%.
Revisiting Prediction Match of KD
In vanilla knowledge distillation [16], the knowledge is transferred from a pre-trained teacher model to a student model by minimizing the discrepancy between the prediction scores of the teacher and student models.
Formally, with the logits Z (s) ∈ R B × C and Z (t) ∈
R B × C of student and teacher networks, where
B and C denote batch size and the number of classes, respectively, the vanilla KD loss [16] is represented as
[Equation]
where KL refers to Kullback–Leibler divergence with
[Equation]
being the probabilistic prediction vectors, and τ is the temperature factor to control the softness of logits.
In addition to the teacher’s soft targets in Eq.(1), KD [16] stated that it is beneficial to train the student together with ground-truth labels, and the overall training loss is composed of the original classification loss L cls and KD loss L KD , i.e. ,
[Equation]
where L cls is usually the cross-entropy loss between the predictions of student network and groundtruth labels, α and β are factors for balancing the losses.
2.1 Catastrophic discrepancy with a stronger teacher
As illustrated in Section 1, the effect of a teacher on KD has not been sufficiently investigated, especially when the performance of pre-trained teacher grows stronger, such as with larger model size or being trained with more advanced and competing strategies, e.g. , label smoothing, mix-up [51], auto augmentations [9], etc .
With this regard, as Figure 2, we train ResNet-18 and ResNetstandalone with strategy B1 and strategy B2 2 , and obtain 4 trained models ( R18B1 , R18B2 , R50B1 , and R50B2 with accuracies 69.76%, 73.4%, 76.13%, and 78.5%, respectively), then compare their discrepancy using KL divergence ( τ = 1 and τ = 4 ) on the predicted probabilities Y .
We have the following observations:
2 Training with B2 obtains higher accuracy compared to B1, e.g. , 73.4% (B2) vs. 69.8% (B1) on ResNet-18.
tttssstttsss
Conventional KD
Instance Relation
Inter-class Relation
Intra-class
[Figure]
i n s t a n c e s s 2 tcorr.
ttt 3 sss 3 ts… corr.
Input
Teacher
Student
Output ttt 3 sss
Existing methods
Our DIST corr.
corr. corr.
corr. corr.
corr. corr.
[Figure]
• The outputs of ResNet-18 do not change much with the stronger strategy compared to ResNet-50.
This implies that the representational capacity limits the student’s performance, and it tends to be fairly challenging for the student to exactly match the teacher’s outputs as their discrepancy becomes larger.
• When the teacher and student
models are trained with a stronger strategy, the discrepancy between teacher and student would be larger.
This indicates that when we adopt KD with a stronger training strategy, the misalignment between KD loss and classification loss would be severer, thus disturbing the student’s training.
As a result, the exact match ( i.e. , the loss reaches the minimal if and only if the teacher and student outputs are exactly identical) with KL divergence seems way too overambitious and demanding since the discrepancy between student and teacher can be considerably huge.
Since the exact match can be detrimental with a stronger teacher, our intuition is to develop a relaxed manner for matching the predictions between the teacher and student.
DIST: Distillation from A Stronger Teacher 3.1 Relaxed match with relations
The prediction scores indicate the teacher’s confidence (or preference) over all classes.
For a relaxed match of predictions between the teacher and student, we are motivated to consider what we really care about for the teacher’s output.
Instead of the exact probabilistic values, actually, during inference, we are only concerned about their relations , i.e. , relative ranks of predictions of teacher.
In this way, for some metric d ( · , · ) with R C × R C → R + , the exact match can be formulated that d ( a , b ) =
if a = b for any two prediction vector as Y (s) i, : and Y (t) i, : in the KL divergence of Eq.(1).
Then as a relaxed match, we can introduce additional mappings ϕ ( · ) and ψ ( · ) with R C
→ R C such that
[Equation]
Therefore, d ( a , b ) = 0 does not necessarily require a and b should be exactly the same.
Nevertheless, since we care about the relation within a or b , the mappings ϕ and ψ should be isotone and do not affect the semantic information and inference result of the prediction vector.
With this regard, a simple yet effective choice for the isotone mapping is the positive linear transformation, namely,
[Equation]
where m 1 , m 2 , n 1 , and n 2 are constants with m 1 × m 2 > 0 .
As a result, this match could be invariant under separate changes in scale and shift for the predictions.
Actually, to satisfy the property Eq.(5), we can thus adopt the widely-used Pearson’s distance as the metric, i.e. ,
[Equation]
ρ p ( u , v ) is the Pearson correlation coefficient between two random variables u and v ,
[Equation]
where Cov ( u , v ) is the covariance of u and v , ¯ u and Std ( u ) denote the mean and standard derivation of u , respectively.
In this way, we can define the relation as correlation .
More specifically, and the original exact match in vanilla KD [16] can thus be relaxed and replaced by maximizing the linear correlation to preserve the relation of teacher and student on the probabilistic distribution of each instance, which we call inter-class relation .
Formally, for each pair of prediction vector Y (s) i, : and Y (t) i, : , the inter-relation loss can be formulated as
[Equation]
Some isotone mappings or metrics can also be used to relax the match as Eq.(4), such as cosine similarity investigated empirically in Section 4.5; other more advanced and delicate choices could be left as future work.
3.2 Better distillation with intra-relations
Besides the inter-class relation, where we transfer the relation of multiple classes in each instance, the prediction scores of multiple instances in each class are also informative and useful.
This scores indicate the similarities of multiple instances to one class.
For instance, suppose we have three images containing “cat”, “dog”, and “plane”, respectively, and they have three prediction scores on the ‘cat’ class, denoted as e , f , and g .
Generally, the picture “cat” should have the largest score to the “cat” class, while the “plane” should have the smallest score since it is inanimate.
This relation of “ e > f > g ” could also be transferred to the student.
Besides, even for the images from the same class, the intrinsic intra-class variance of the semantic similarities is actually also informative.
It indicates the prior from the teacher that which one is more reliable to cast in this class .
Therefore, we also encourage to distill this intra-relation for better performance.
Actually, define prediction matrix Y (s) and Y (t) with each row as Y (s) i, : and Y (t) i, : , then the above inter-relation is to maximize the correlation row-wisely (see Figure 3).
In contrast, for intra-relation, the corresponding loss is thus to maximize the correlation column-wisely, i.e. ,
[Equation]
As a result, the overall training loss L tr can be composed of the classification loss, inter-class KD loss, and intra-class KD loss, i.e. ,
[Equation]
(10) where α , β , and γ are factors for balancing the losses.
In this way, via the relation loss, we have endowed the student with freedom more or less to match the teacher network’s output adaptively, thus boosting the distillation performance to a great extent.
[Table] [Table]
Experiments 4.1 Experimental settings
Training strategies.
The training strategies of image classification task are summarized in Table 1.
CIFAR-100.
For fair comparisons, we use the same training strategies (referred to A1 in Table 1) and pretrained models following CRD [41].
ImageNet.
B1: for comparisons with previous KD methods, we train our baselines with the same simple training strategy as CRD [41].
B2: to validate the effectiveness of KD methods on modern training strategies, we follow EfficientNet [40] and design a training strategy B2, which can significantly improve the performance compared to B1.
B3: the strategy B3 is used for training Swin-Transformers [27], and contains even more stronger data augmentations and regularization.
Loss weights.
On CIFAR-100 and ImageNet, we set α = 1 , β = 2 , and γ = 2 in Eq.(10).
On object detection and semantic segmentation, these three factors are all equal to 1 .
For KD [16], we set α = 0 .
9 , β = 1 in Eq.(3), and use a default temperature τ = 4 .
Specifically, instead of using τ =on ImageNet, we choose a larger temperature τ = 4 on CIFAR-100, as it is easy to get overfit and the learned probabilistic distribution is sharp on CIFAR-100.
4.2 Image Classification
Baseline results on ImageNet.
We first compare our method with prior works using the baseline settings.
As shown in Table 2, our DIST significantly outperforms prior KD methods.
Note that our method is only conducted on the outputs of models, and has a similar computational cost as
KD [16].
Nevertheless, it even achieves better performance compared to those sophisticatedlydesigned methods.
For example, CRD [41] needs to preserve a memory bank for all 128-d features of ImageNet images, and produces additional 260M FLOPs of computation cost; SRRL [47] and
Review [7] require additional convolutions for feature alignments.
The implementation of DIST can be found in Appendix A.1, which is quite simple compared to these methods.
Distillation from stronger teacher models.
As the stronger teachers come from larger model sizes and stronger strategies, we here first conduct experiments to compare our DIST with the vanilla KD on different scales (model sizes) of ResNets with baseline strategy B1.
As shown in Table 3, when the teacher goes larger, the ResNet-18 students perform even worse than that with a medium-sized
ResNet-50 teacher.
Nevertheless, our DIST shows an upward trend with larger teachers, and the improvements compared to KD also become more significant, indicating that our DIST tackles better on the large discrepancy between the student and larger teacher.
Distillation from stronger training strategies.
Recently, the performance of models on ImageNet has been significantly improved by the sophisticated training strategies and strong data augmentations [Table] [Table] ( e.g. , TIMM [44] achieves 80.4% accuracy on ResNet-50 while the baseline strategy B1 only obtains 76.1%).
However, most of the KD methods still conduct experiments with simple training settings.
It is seldomly investigated whether the KD methods are suitable to the advanced strategies.
In this way, we conduct experiments with advanced training strategies and compare our method with vanilla KD, instance relation-based RKD [30], and SRRL [47].
We first train traditional CNNs with strong strategies, and also use a strong ResNet-50 with 80.1% accuracy trained by [44] as the teacher.
As results shown in Table 4, on both similar architectures (ResNet-18, ResNet-34) and dissimilar architectures (MobileNetV2, EfficientNet-B0), our DIST can achieve the best performance.
Note that RKD and SRRL can perform worse than training from scratch, especially when the students are small (ResNet-18 and MobileNet) or the architectures of teacher and student are fairly different (ResNet-50 and Swin-L), this might be because they focus on the intermediate features, which can be more challenging for the student to recover teacher’s features compared to predictions.
Furthermore, we experiment on the recent state-of-the-art Swin-Transformer [27].
The results show that our DIST gains improvements on even more stronger models and strategies.
For example, with
Swin-L teacher, our method improves ResNet-50 and Swin-T by 1.7% and 1.0%, respectively.
CIFAR-100.
The results on CIFAR-100 dataset in Table 5 show that, by distilling on the predicted logits, our method even outperforms those sophisticatedly-designed feature distillation methods.
4.3 Object Detection
We further investigate the effectiveness of DIST on downstream tasks.
We conduct experiments on
MS COCO object detection dataset [25], and simply leverage our DIST as an additional supervision on the final predictions of classes.
Following [37, 52], we use the same standard training strategies and utilize Cascade Mask R-CNN [2] with ResNeXt-101 backbone as the teacher for two-stage student of Faster R-CNN [23] with ResNet-50 backbone; while for one-stage RetinaNet [24] with
ResNet-50 backbone, the RetinaNet with ResNeXt-101 backbone is utilized as the teacher.
As shown in Table 6, our DIST achieves competitive results on COCO validation set.
For comparisons, we train the vanilla KD under the same settings as our DIST, the results show that our DIST significantly outperforms vanilla KD by simply replacing the loss functions.
Moreover, by combining
[Table] [Table]
[Table]
DIST with mimic, which minimizes the mean square error between FPN features of teacher and student, we can even outperform the state-of-the-art KD methods designed for object detection.
4.4 Semantic Segmentation
We also perform experiments on semantic segmentation, a challenging dense prediction task.
Following [37, 43, 46], we train DeepLabV3 [6] and PSPNet [54] with ResNet-18 backbone on Cityscapes dataset, and adopt our DIST on the predictions of classification head using a teacher with ResNet101 backbone of DeepLabV3.
As the results summarized in Table 7, with only the supervision of class predictions, our DIST can significantly outperform existing knowledge distillation methods on semantic segmentation task.
For example, our DIST outperforms recent state-of-the-art method
CIRKD [46] by 1.58% on PSPNet-R18.
This demonstrate our effectiveness on relation modeling.
4.5 Ablation studies
Effects of inter-class and intra-class correlations.
This paper proposes two types of relations: interclass and intra-class relations.
To validate the effectiveness of each relation, we conduct experiments to train students with these relations separately.
The results on Table 8 verify that, both inter-class and intra-class relations can outperform the vanilla KD; also, the performance could be further boosted by combining them together.
[Table]
Effect of intra-class relation in vanilla KD.
To investigate the effectiveness of intra-class relation in vanilla KD, we adopt experiments to train our DIST using KL divergence as the relation metric, denoted as DIST (KL div.)
As the results summarized in Table 8, adding intra-class relation in the vanilla KD can also improve the performance (from 71.21% to 71.62%).
However, when the student is trained with intra-class relation only, the improvement of using KL divergence is less significant than using Pearson correlation (70.61% vs. 71.55%), since the means and variances of intra-class distributions could be varied.
Effect of training students with KD loss only.
Training student with only the KD loss can better reflect the distillation ability and the information richness of supervision signals.
As results in Tableshow that, when the student is trained with only the KD loss, our DIST significantly outperforms the vanilla KD.
Without using the ground-truth labels, it can even outperform the standalone training accuracy, which indicates the effectiveness of our DIST in distilling those truly-beneficial relations.
The student and teacher models are ResNet-18 and ResNet-34, respectively.
The original accuracy of
ResNet-18 without KD is 69.76%.
[Table]
More ablation studies can be found in Section A.3.
Conclusion
This paper presents a new knowledge distillation (KD) method named DIST to implement better distillation from a stronger teacher.
We empirically study the catastrophic discrepancy problem between the student and a stronger teacher, and propose a relation-based loss to relax the exact match of KL divergence in a linear sense.
Our method DIST is simple yet effective in handling strong teachers.
Extensive experiments show our superiority in various benchmark tasks.
For example,
DIST even outperforms state-of-the-art KD methods designed specifically for object detection and semantic segmentation.
Acknowledgements
This work was supported in part by the Australian Research Council under Project DP and the University of Sydney Research Accelerator (SOAR) Prize.
Checklist
For all authors... (a) Do the main claims made in the abstract and introduction accurately reflect the paper’s contributions and scope?
[Yes] (b) Did you describe the limitations of your work?
[Yes] See Appendix.
(c) Did you discuss any potential negative societal impacts of your work?
[Yes] See Appendix.
(d) Have you read the ethics review guidelines and ensured that your paper conforms to them?
[Yes] 2.
If you are including theoretical results... (a) Did you state the full set of assumptions of all theoretical results?
[N/A] (b) Did you include complete proofs of all theoretical results?
[N/A] 3.
If you ran experiments... (a) Did you include the code, data, and instructions needed to reproduce the main experimental results (either in the supplemental material or as a URL)?
[Yes] Training details are provided in the paper.
Training code and logs are released at GitHub: https://github.com/hunto/DIST_KD.
(b) Did you specify all the training details (e.g., data splits, hyperparameters, how they were chosen)?
[Yes] (c) Did you report error bars (e.g., with respect to the random seed after running experiments multiple times)?
[Yes] Standard deviations on CIFAR-100 are reported.
(d) Did you include the total amount of compute and the type of resources used (e.g., type of GPUs, internal cluster, or cloud provider)?
[Yes] 4.
If you are using existing assets (e.g., code, data, models) or curating/releasing new assets... (a) If your work uses existing assets, did you cite the creators?
[Yes] (b) Did you mention the license of the assets?
[Yes] (c) Did you include any new assets either in the supplemental material or as a URL?
[Yes] Code and training logs are included.
(d) Did you discuss whether and how consent was obtained from people whose data you’re using/curating?
[N/A] (e) Did you discuss whether the data you are using/curating contains personally identifiable information or offensive content?
[N/A] 5.
If you used crowdsourcing or conducted research with human subjects... (a) Did you include the full text of instructions given to participants and screenshots, if applicable?
[N/A] (b) Did you describe any potential participant risks, with links to Institutional Review Board (IRB) approvals, if applicable?
[N/A] (c) Did you include the estimated hourly wage paid to participants and the total amount spent on participant compensation?
[N/A]