A lightweight model for feature knowledge distillation using histopathology foundational models.
Shazam proposes a small and efficient model that distills knowledge from extracted features using histopathology foundational models. This approach effectively leverages the strong representational power of large-scale foundational models while optimizing computational efficiency through a lightweight distillation process.
Feature Knowledge Distillation
Transfers rich representations from foundational models into a smaller, more efficient model.
Lightweight and Scalable
Achieves high accuracy with lower computational cost, suitable for practical deployment in clinical settings.
Superior Performance
Outperforms existing CPath models and other fusion-based methods across multiple evaluation benchmarks.

This pipeline supports survival prediction using multi-teacher distillation from foundational models.
Case-to-feature Mapping
survival_analysis/jsonlink.py.pt paths using a JSON dictionary.WSI Patch Extraction
CLAM/create_patches_features_fp.py.h5 files.patches/ contains fewer .h5 files than the number of WSIs, verify the original .svs slides.CSV Splitting for Multi-GPU
survival_analysis/splitcsv.pyFeature Extraction with Multi-teacher Models
CLAM/extract_BRCA4cls.shSingle-model Training
survival_analysis/single_BRCA4cls.shMulti-teacher Distillation Training
Shazam_v2/multi_moe_distill_v3.py Shazam_v2/multi_moe_distill4cls.py
We directly use the environment configuration provided by the CLAM project.
conda env create -f env.yml
conda activate clam_latest
python train.py
This section explains the end-to-end tensor shape transformations inside the CrossAttentionClassifierWithDistillation model.
Each feature .pt file contains a tuple:
(features, labels) = torch.load("xxx_features.pt")
features: shape = [N, C_i]
where:
N: number of patches (WSIs)C_i: feature dimension of model i, e.g., 1280 (Virchow), 1024 (Uni), etc.labels: shape = [N] (long, class indices)
During training:
train_dataset = TensorDataset(*train_features_list, train_labels)
which means input to model:
features = [x1, x2, x3, x4] # x_i shape: [B, C_i]
Each foundational model's features x_i β [B, C_i] are mapped into a shared dimension d_model:
Output shape: `[B, d_model]` for each modality
After mapping:
features_stacked = torch.stack([mapped_1, mapped_2, mapped_3, mapped_4], dim=1)
[B, 4, d_model]Each layer applies attention across the 4 modalities (tokens):
Q, K, V: [B, 4, d_model] β Attention β Output: [B, 4, d_model]
Repeated num_layers times (e.g. 5).
fused_features = features.mean(dim=1)
[B, d_model]fused_features β Linear β ReLU β LayerNorm β Linear β logits
[B, num_classes]For distillation:
student_features: [B, d_model]
expert_features_list: [B, C_i] # for each i
mapped_expert = FeatureMapper(C_i β d_model)
student_features and each mapped_expert| Stage | Shape | Description |
|---|---|---|
| Raw Input | [B, C_i] | One per modality |
| After Mapping | [B, d_model] Γ 4 | Standardized into shared dimension |
| Stack (4 modalities) | [B, 4, d_model] | Cross-attention input |
| After Cross-Attention | [B, 4, d_model] | Contextually refined features |
| Mean Fusion | [B, d_model] | Aggregated single representation |
| Classifier Output | [B, num_classes] | Final prediction logits |
| Expert Mapping | [B, d_model] | Used in distillation loss |
Python
97.0%
Shell
3.0%
A lightweight model for feature knowledge distillation using histopathology foundational models.
Shazam proposes a small and efficient model that distills knowledge from extracted features using histopathology foundational models. This approach effectively leverages the strong representational power of large-scale foundational models while optimizing computational efficiency through a lightweight distillation process.
Feature Knowledge Distillation
Transfers rich representations from foundational models into a smaller, more efficient model.
Lightweight and Scalable
Achieves high accuracy with lower computational cost, suitable for practical deployment in clinical settings.
Superior Performance
Outperforms existing CPath models and other fusion-based methods across multiple evaluation benchmarks.

This pipeline supports survival prediction using multi-teacher distillation from foundational models.
Case-to-feature Mapping
survival_analysis/jsonlink.py.pt paths using a JSON dictionary.WSI Patch Extraction
CLAM/create_patches_features_fp.py.h5 files.patches/ contains fewer .h5 files than the number of WSIs, verify the original .svs slides.CSV Splitting for Multi-GPU
survival_analysis/splitcsv.pyFeature Extraction with Multi-teacher Models
CLAM/extract_BRCA4cls.shSingle-model Training
survival_analysis/single_BRCA4cls.shMulti-teacher Distillation Training
Shazam_v2/multi_moe_distill_v3.py Shazam_v2/multi_moe_distill4cls.py
We directly use the environment configuration provided by the CLAM project.
conda env create -f env.yml
conda activate clam_latest
python train.py
This section explains the end-to-end tensor shape transformations inside the CrossAttentionClassifierWithDistillation model.
Each feature .pt file contains a tuple:
(features, labels) = torch.load("xxx_features.pt")
features: shape = [N, C_i]
where:
N: number of patches (WSIs)C_i: feature dimension of model i, e.g., 1280 (Virchow), 1024 (Uni), etc.labels: shape = [N] (long, class indices)
During training:
train_dataset = TensorDataset(*train_features_list, train_labels)
which means input to model:
features = [x1, x2, x3, x4] # x_i shape: [B, C_i]
Each foundational model's features x_i β [B, C_i] are mapped into a shared dimension d_model:
Output shape: `[B, d_model]` for each modality
After mapping:
features_stacked = torch.stack([mapped_1, mapped_2, mapped_3, mapped_4], dim=1)
[B, 4, d_model]Each layer applies attention across the 4 modalities (tokens):
Q, K, V: [B, 4, d_model] β Attention β Output: [B, 4, d_model]
Repeated num_layers times (e.g. 5).
fused_features = features.mean(dim=1)
[B, d_model]fused_features β Linear β ReLU β LayerNorm β Linear β logits
[B, num_classes]For distillation:
student_features: [B, d_model]
expert_features_list: [B, C_i] # for each i
mapped_expert = FeatureMapper(C_i β d_model)
student_features and each mapped_expert| Stage | Shape | Description |
|---|---|---|
| Raw Input | [B, C_i] | One per modality |
| After Mapping | [B, d_model] Γ 4 | Standardized into shared dimension |
| Stack (4 modalities) | [B, 4, d_model] | Cross-attention input |
| After Cross-Attention | [B, 4, d_model] | Contextually refined features |
| Mean Fusion | [B, d_model] | Aggregated single representation |
| Classifier Output | [B, num_classes] | Final prediction logits |
| Expert Mapping | [B, d_model] | Used in distillation loss |
Python
97.0%
Shell
3.0%