Views
No views yet
vit_large_patch14_dinov2.lvd142m in timm, using DINOv2's key hyperparameters (img_size=224, init_values=1e-5).model(x) outputs [N, 1024])1import torch
2from PIL import Image
3import timm
4from huggingface_hub import hf_hub_download
5from torchvision import transforms
6
7# 1) Download weights
8ckpt_path = hf_hub_download(repo_id="majiabo/GPFM", filename="GPFM.pth")
9
10# 2) Build ViT-L/14 (DINOv2 config) model
11model = timm.create_model(
12 'vit_large_patch14_dinov2.lvd142m',
13 pretrained=False,
14 img_size=224,
15 init_values=1.0e-05,
16)
17state_dict = torch.load(ckpt_path, map_location='cpu')
18model.load_state_dict(state_dict, strict=True)
19model.eval()
20
21# 3) Preprocessing (consistent with GPFM project)
22mean = (0.485, 0.456, 0.406)
23std = (0.229, 0.224, 0.225)
24transform = transforms.Compose([
25 transforms.Resize((224, 224), interpolation=transforms.InterpolationMode.BICUBIC),
26 transforms.ToTensor(),
27 transforms.Normalize(mean=mean, std=std),
28])
29
30# 4) Extract features
31img = Image.open('your_tile_512x512.jpg').convert('RGB')
32x = transform(img).unsqueeze(0) # [1, 3, 224, 224]
33with torch.no_grad():
34 feat = model(x) # [1, 1024]
35print(feat.shape)pip install torch torchvision timm huggingface_hub pillow@article{ma2025generalizable,
title={A generalizable pathology foundation model using a unified knowledge distillation pretraining framework},
author={Ma, Jiabo and Guo, Zhengrui and Zhou, Fengtao and Wang, Yihui and Xu, Yingxue and Li, Jinbang and Yan, Fang and Cai, Yu and Zhu, Zhengjie and Jin, Cheng and others},
journal={Nature Biomedical Engineering},
pages={1--20},
year={2025},
publisher={Nature Publishing Group UK London}
}