Downloads · 30 days
45
68% of all-time downloads
AI4PATH/XMAG
XMAG is a image feature extraction model from AI4PATH. Use it for the image feature extraction task on the model card, and read the license before you ship it in a product. It is set up for transformers.
XMag is a pathology image encoder distilled from a high-magnification foundation model to a low-magnification student model.
Downloads · 30 days
45
68% of all-time downloads
All-time downloads
66
Public
Repo size
346 MB
Likes
1
Public
Click a slice to open those files.
.bin346 MB · 100%
From the Hugging Face model README
XMag is a pathology image encoder distilled from a high-magnification foundation model to a low-magnification student model.
224 x 224@5x or 2um mppimport torch
from PIL import Image
from torchvision import transforms
from transformers import AutoModel
eval_transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
])
model = AutoModel.from_pretrained("AI4PATH/XMAG", trust_remote_code=True)
model.eval()
image = Image.open("patch.png").convert("RGB")
pixel_values = eval_transform(image).unsqueeze(0)
with torch.no_grad():
outputs = model(pixel_values)
cls_embedding = outputs["cls_embedding"] # (1, 768)
patch_embeddings = outputs["patch_embeddings"] # (1, 256, 768)
pixel_values should be normalized RGB tensors with shape (B, 3, 224, 224).
The model does not apply preprocessing internally. Resize, ToTensor(), and ImageNet mean/std normalization should be done before calling the model.
The student sees a low-magnification 224 x 224 view of the tissue region. During training, the frozen UNIv2 teacher sees the corresponding high-magnification 896 x 896 region split into 4 x 4 subpatches. The student is trained to match both:
This release contains the student EMA backbone weights only. Projection heads used during distillation are not included.
pip install torch torchvision transformers huggingface_hub
The model code uses torch.hub to instantiate the DINOv2 ViT-B/14 backbone. The first load may need internet access to fetch the DINOv2 hub code, unless it is already cached.
If you use this model, please cite:
Su, Z., Akbar, A. R., & Niazi, M. K. K. (2025). Streamline pathology foundation model by cross-magnification distillation. arXiv preprint arXiv:2509.23097. Available at arXiv:2509.23097.
@article{su2025streamline,
title={Streamline pathology foundation model by cross-magnification distillation},
author={Su, Ziyu and Akbar, Abdul Rehman and Sajjad, Usama and Parwani, Anil V and Niazi, Muhammad Khalid Khan},
journal={arXiv preprint arXiv:2509.23097},
year={2025}
}