Merge pull request #4 from recursionpharma/more-code
Browse files- .vscode/settings.json +5 -0
- requirements.txt +9 -0
- vit_encoder.py +60 -0
.vscode/settings.json
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"flake8.args": [
|
| 3 |
+
"--max-line-length=120"
|
| 4 |
+
]
|
| 5 |
+
}
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
huggingface-hub==0.18.0
|
| 2 |
+
timm==0.9.7
|
| 3 |
+
torch==2.1.0+cu121
|
| 4 |
+
torchmetrics==1.2.0
|
| 5 |
+
torchvision==0.16.0+cu121
|
| 6 |
+
tqdm==4.66.1
|
| 7 |
+
transformers==4.35.2
|
| 8 |
+
xformers==0.0.22.post7
|
| 9 |
+
zarr==2.16.1
|
vit_encoder.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict
|
| 2 |
+
|
| 3 |
+
import timm.models.vision_transformer as vit
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def build_imagenet_baselines() -> Dict[str, torch.jit.ScriptModule]:
|
| 8 |
+
"""This returns the prepped imagenet encoders from timm, not bad for microscopy data."""
|
| 9 |
+
vit_backbones = [
|
| 10 |
+
_make_vit(vit.vit_small_patch16_384),
|
| 11 |
+
_make_vit(vit.vit_base_patch16_384),
|
| 12 |
+
_make_vit(vit.vit_base_patch8_224),
|
| 13 |
+
_make_vit(vit.vit_large_patch16_384),
|
| 14 |
+
]
|
| 15 |
+
model_names = [
|
| 16 |
+
"vit_small_patch16_384",
|
| 17 |
+
"vit_base_patch16_384",
|
| 18 |
+
"vit_base_patch8_224",
|
| 19 |
+
"vit_large_patch16_384",
|
| 20 |
+
]
|
| 21 |
+
imagenet_encoders = list(map(_make_torchscripted_encoder, vit_backbones))
|
| 22 |
+
return {name: model for name, model in zip(model_names, imagenet_encoders)}
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def _make_torchscripted_encoder(vit_backbone) -> torch.jit.ScriptModule:
|
| 26 |
+
dummy_input = torch.testing.make_tensor(
|
| 27 |
+
(2, 6, 256, 256),
|
| 28 |
+
low=0,
|
| 29 |
+
high=255,
|
| 30 |
+
dtype=torch.uint8,
|
| 31 |
+
device=torch.device("cpu"),
|
| 32 |
+
)
|
| 33 |
+
encoder = torch.nn.Sequential(
|
| 34 |
+
Normalizer(),
|
| 35 |
+
torch.nn.LazyInstanceNorm2d(
|
| 36 |
+
affine=False, track_running_stats=False
|
| 37 |
+
), # this module performs self-standardization, very important
|
| 38 |
+
vit_backbone,
|
| 39 |
+
).to(device="cpu")
|
| 40 |
+
_ = encoder(dummy_input) # get those lazy modules built
|
| 41 |
+
return torch.jit.freeze(torch.jit.script(encoder.eval()))
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def _make_vit(constructor):
|
| 45 |
+
return constructor(
|
| 46 |
+
pretrained=True, # download imagenet weights
|
| 47 |
+
img_size=256, # 256x256 crops
|
| 48 |
+
in_chans=6, # we expect 6-channel microscopy images
|
| 49 |
+
num_classes=0,
|
| 50 |
+
fc_norm=None,
|
| 51 |
+
class_token=True,
|
| 52 |
+
global_pool="avg", # minimal perf diff btwn "cls" and "avg"
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
class Normalizer(torch.nn.Module):
|
| 57 |
+
def forward(self, pixels: torch.Tensor) -> torch.Tensor:
|
| 58 |
+
pixels = pixels.float()
|
| 59 |
+
pixels /= 255.0
|
| 60 |
+
return pixels
|