Preserving Angles Improves Feature Distillation

Evelyn Mannix, Liam Hodgkinson, Howard Bondell

Abstract

Knowledge distillation methods compress models by training a student network using the classification outputs of a high quality teacher model, but can fail to effectively transfer the properties of computer vision foundation models from the teacher to the student. While it has been recently shown that feature distillation—where a teacher model's output features are replicated instead—can reproduce performance for foundation models across numerous downstream tasks, they fall short in matching critical properties such as robustness and out-of-distribution (OOD) detection performance. This paper overcomes this shortcoming by introducing Cosine-similarity Preserving Compression (CosPress), a feature distillation technique that learns a mapping to compress the latent space of the teacher model into the smaller latent space of the student, by preserving the cosine similarities between image embeddings. This enables direct optimisation of the student network and produces a more faithful reproduction of the teacher's properties. It is shown that distillation with CosPress on a variety of datasets, including ImageNet, produces more accurate models with greater performance on generalisability, robustness and OOD detection benchmarks, and that this technique provides a competitive pathway for training highly performant lightweight models on small datasets.

Introduction

Large Vision Transformer (ViT) foundation models, such as DINOv2, exhibit remarkable robustness and an innate capacity for out-of-distribution (OOD) detection. However, their substantial computational requirements often preclude deployment on edge devices. Knowledge distillation is the standard approach to address this, where a smaller "student" model is trained to mimic a larger "teacher."

Traditional distillation methods typically align final classification outputs or use Mean Squared Error (MSE) to match internal features. However, these approaches often fail to capture the underlying semantic structure of the teacher's latent space. State-of-the-art methods like Proteus struggle to transfer the teacher's robustness and OOD performance, resulting in students that may achieve high accuracy but lack the teacher's nuanced understanding of data relationships.

Methodology: Cosine-similarity Preserving Compression

The proposed method, CosPress (Cosine-similarity Preserving Compression), shifts the distillation focus from distance-based alignment to angle-based alignment. In high-dimensional latent spaces, the direction (angle) of a vector carries more semantic significance than its magnitude.

Teacher-Side Compression

Unlike prior methods that project student features up to the teacher's dimension, CosPress employs a teacher head ($h_\phi$) to compress the teacher’s high-dimensional latent space down to the student’s dimension. This avoids the "contamination" problem, where student-side projection heads become over-specialized in mimicking the teacher rather than learning generalizable features.

The CosPress architecture (right) compared to Proteus (left). CosPress utilizes a teacher head for dimensionality reduction and a cosine-similarity preserving objective.

Preserving Semantic Structure

The teacher head is trained using a dimensionality reduction objective based on a von-Mises Fisher kernel. By minimizing the KL divergence between the original teacher space and the compressed space, CosPress ensures that the cosine similarities between images in a batch are preserved. Mathematically, this approach is grounded in the Johnson-Lindenstrauss (JL) Lemma, which suggests that geometric properties like angles can be preserved during dimensionality reduction if the mapping is approximately orthogonal.

Experimental Evaluation

CosPress was evaluated across a comprehensive suite of benchmarks, including ImageNet-1K for pre-training and the OpenOOD benchmark for out-of-distribution detection. Robustness was tested on datasets such as ImageNet-V2, Sketch, ImageNet-R, and ImageNet-A, while fine-grained classification was assessed on tasks like Oxford Pets and Stanford Cars.

Results and Discussion

CosPress demonstrates significant improvements across all evaluated metrics, particularly in OOD detection and robustness.

Out-of-Distribution Performance

The most notable result is the dramatic improvement in OOD detection. For a ViT-Ti/14 student, CosPress improved the AUROC for "Far OOD" detection from 74.22% (using Proteus) to 91.03%. In certain configurations, the distilled student even surpassed the OOD performance of its own teacher.

Out-of-distribution (OOD) detection results comparing Proteus and CosPress across different student and teacher architectures. CosPress consistently achieves higher AUROC and lower FPR.

Accuracy and Robustness

On ImageNet-1K, CosPress achieved 76.6% linear probing accuracy for a ViT-Ti/14, outperforming both Proteus (76.1%) and standard distilled DINOv2 (75.9%). Furthermore, CosPress consistently outperformed previous methods on robustness benchmarks like ImageNet-A and ImageNet-R, effectively closing the gap between lightweight models and massive foundation models.

Geometric Insights

Analysis shows that CosPress naturally learns a mapping that is approximately left- and right-orthogonal. This property prevents the compression process from collapsing distinct semantic concepts into the same region of the latent space, thereby maintaining the "semantic map" developed by the foundation model during pre-training.

Conclusion

CosPress introduces a more faithful way to transfer the capabilities of large vision foundation models to lightweight architectures. By prioritizing the preservation of cosine similarities and utilizing teacher-side compression, it significantly enhances the OOD detection and robustness of student models. While a small generalization gap remains compared to models trained on massive proprietary datasets, CosPress provides a highly efficient and effective pathway for model specialization and deployment on resource-constrained devices.

Summary updated

BibTeX
@article{mannix2025preserving,
  author = {Evelyn Mannix and Liam Hodgkinson and Howard Bondell},
  title = {{Preserving Angles Improves Feature Distillation}},
  journal = {Accepted by TMLR},
  year = {2025},
  url = {https://openreview.net/forum?id=ZEhgODZkWU},
}
Figure