File size: 1,420 Bytes
5effde4
 
494a531
 
 
 
 
 
 
5effde4
 
 
 
205f5a2
069d1bf
205f5a2
5effde4
069d1bf
 
 
8f636e5
069d1bf
 
 
5effde4
205f5a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5effde4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
---
license: cc-by-nc-sa-4.0
language:
- en
pipeline_tag: image-feature-extraction
tags:
- pathology
- foundation_model
- vit
---

# SP22M

ViT-small (22M parameters) trained on 423,000 H&E slides from the Mount Sinai Health System.
 

## Model Usage

To get started, first clone the repository with this command:
```bash
  git clone --no-checkout https://huggingface.co/MountSinaiCompPath/SP22M && cd SP22M && git sparse-checkout init --no-cone && git sparse-checkout set '/*' '!*.bin' && git checkout
```

Now you can use the following code:
```python
from PIL import Image
import numpy as np
import vision_transformer
import torch
import torch.nn as nn
import torchvision.transforms as transforms
from huggingface_hub import PyTorchModelHubMixin

class SP22M(nn.Module, PyTorchModelHubMixin):
    def __init__(self):
        super().__init__()
        self.encoder = vision_transformer.vit_small(num_classes=0)
    
    def forward(self, x):
        return self.encoder(x)

# Download up model
model = SP22M.from_pretrained("MountSinaiCompPath/SP22M")

# Set up transform
transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
])

# Image
img = np.random.randint(0, 256, size=224*224*3).reshape(224,224,3).astype(np.uint8)
img = Image.fromarray(img)
img = transform(img).unsqueeze(0)

# Inference
with torch.no_grad():
    h = model(img)
```