Views
No views yet

1from torchvision.transforms import v2
2from PIL import Image
3import requests
4import torch
5import timm
6import io
7
8# get example histology image
9url = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQc7_xZpGOfQT7sxKwf2w5lL4GAq6IX_CbTzP1NGeenzA&s"
10image = Image.open(io.BytesIO(requests.get(url).content))
11
12# load model from the hub
13model = timm.create_model(
14 model_name="hf-hub:1aurent/vit_base_patch8_224.kaiko_ai_towards_large_pathology_fms",
15 dynamic_img_size=True,
16 pretrained=True,
17).eval()
18
19# get image transform
20preprocessing = v2.Compose(
21 [
22 v2.ToImage(),
23 v2.Resize(size=224),
24 v2.CenterCrop(size=224),
25 v2.ToDtype(torch.float32, scale=True),
26 v2.Normalize(
27 mean=(0.5, 0.5, 0.5),
28 std=(0.5, 0.5, 0.5),
29 ),
30 ]
31)
32
33data = preprocessing(image).unsqueeze(0) # input is a (batch_size, num_channels, img_size, img_size) shaped tensor
34output = model(data) # output is a (batch_size, num_features) shaped tensor1@misc{ai2024largescale,
2 title = {Towards Large-Scale Training of Pathology Foundation Models},
3 author = {kaiko.ai and Nanne Aben and Edwin D. de Jong and Ioannis Gatopoulos and Nicolas Känzig and Mikhail Karasikov and Axel Lagré and Roman Moser and Joost van Doorn and Fei Tang},
4 year = {2024},
5 eprint = {2404.15217},
6 archivePrefix = {arXiv},
7 primaryClass = {cs.CV}
8}