-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfoundation_models.py
More file actions
377 lines (316 loc) · 11.8 KB
/
foundation_models.py
File metadata and controls
377 lines (316 loc) · 11.8 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
from __future__ import annotations
from abc import ABC, abstractmethod
from functools import partial
from typing import Dict
import timm
import torch
from conch.open_clip_custom import create_model_from_pretrained
from torchvision import transforms
from transformers import AutoModel
from utils.constants import NormConstants
_BACKENDS: Dict[str, "FoundationBackend"] = {}
def register(cls):
_BACKENDS[cls.NAME.upper()] = cls()
return cls
class FoundationBackend(ABC):
"""
Abstract base for all foundation model backends.
Subclasses must implement the _build_model method.
"""
NAME: str = ""
IMG_SIZE: int = 224
MEAN = NormConstants.IMAGENET_MEAN.value
STD = NormConstants.IMAGENET_STD.value
@abstractmethod
def _build_model(
self, ckpt_path: str | None, device: torch.device
) -> torch.nn.Module:
"""
Create the *raw* model (weights NOT frozen, NOT on device).
:param ckpt_path: Path to the model checkpoint. If None, load pretrained weights.
:param device: Device on which the model will be used.
:return: A torch.nn.Module instance of the model.
"""
def get_model(self, ckpt_path: str | None, device: torch.device) -> torch.nn.Module:
"""
Load, freeze, and prepare the foundation model for inference.
:param ckpt_path: Path to the model checkpoint. If None, use default pretrained weights.
:param device: Device to move the model to (e.g., 'cpu' or 'cuda').
:return: A frozen torch.nn.Module in evaluation mode.
"""
model = self._build_model(ckpt_path).to(device).eval()
for p in model.parameters():
p.requires_grad = False
return model
def get_transform(self) -> transforms.Compose:
"""
Get the preprocessing transformation for the foundation model.
:return: A tuple of (transform pipeline, image size) for input preprocessing.
"""
return (
transforms.Compose(
[
transforms.Resize(self.IMG_SIZE),
transforms.CenterCrop(self.IMG_SIZE),
transforms.ToTensor(),
transforms.Normalize(mean=self.MEAN, std=self.STD),
]
),
self.IMG_SIZE,
)
@register
class UNI(FoundationBackend):
NAME = "UNI"
IMG_SIZE = 224
def _build_model(self, ckpt_path: str | None):
"""
Build the UNI vision transformer model.
:param ckpt_path: Local checkpoint path. If provided, load from disk.
:return: Initialized ViT model without classification head.
"""
if ckpt_path is not None:
model = timm.create_model(
"vit_large_patch16_224",
img_size=224,
patch_size=16,
init_values=1e-5,
num_classes=0,
dynamic_img_size=True,
)
model.load_state_dict(
torch.load(ckpt_path, map_location="cuda"), strict=True
)
else:
model = timm.create_model(
"hf-hub:MahmoodLab/uni",
pretrained=True,
init_values=1e-5,
dynamic_img_size=True,
)
return model
@register
class CONCH(FoundationBackend):
NAME = "CONCH"
IMG_SIZE = 448
MEAN = NormConstants.OPENAI_MEAN.value # different mean
STD = NormConstants.OPENAI_STD.value # different std
def _build_model(self, ckpt_path: str | None):
"""
Build the CONCH vision model and override forward pass.
:param ckpt_path: Checkpoint path or None to use hub weights.
:return: Model with forward bound to encode_image without contrast projection.
"""
if ckpt_path is not None:
model, preprocess = create_model_from_pretrained(
"conch_ViT-B-16", ckpt_path
)
else:
model, _ = create_model_from_pretrained(
"conch_ViT-B-16", checkpoint_path="hf_hub:MahmoodLab/conch"
)
model.forward = partial(
model.encode_image, proj_contrast=False, normalize=False
)
return model
def get_transform(self) -> transforms.Compose:
"""
Get the OpenAI-specific preprocessing for CONCH.
:return: A tuple of (transform pipeline, image size).
"""
return (
transforms.Compose(
[
transforms.Resize(
self.IMG_SIZE,
interpolation=transforms.InterpolationMode.BICUBIC,
),
transforms.CenterCrop(self.IMG_SIZE),
transforms.ToTensor(),
transforms.Normalize(mean=self.MEAN, std=self.STD),
]
),
self.IMG_SIZE,
)
@register
class UNI2H(FoundationBackend):
NAME = "UNI-2H"
IMG_SIZE = 224
def _build_model(self, ckpt_path: str | None):
"""
Build the UNI-2H vision transformer with specified architecture.
:param ckpt_path: Checkpoint to load weights from disk, ignored for hub.
:param device: Device context (not used in building).
:return: A timm ViT model with custom depth, heads, and mlp configuration.
"""
timm_kwargs = {
"img_size": 224,
"patch_size": 14,
"depth": 24,
"num_heads": 24,
"init_values": 1e-5,
"embed_dim": 1536,
"mlp_ratio": 2.66667 * 2,
"num_classes": 0,
"no_embed_class": True,
"mlp_layer": timm.layers.SwiGLUPacked,
"act_layer": torch.nn.SiLU,
"reg_tokens": 8,
"dynamic_img_size": True,
}
if ckpt_path is not None:
model = timm.create_model(
"hf-hub:MahmoodLab/UNI2-h", pretrained=False, **timm_kwargs
)
model.load_state_dict(
torch.load(ckpt_path, map_location="cuda"), strict=True
)
else:
model = timm.create_model(
"hf-hub:MahmoodLab/UNI2-h", pretrained=True, **timm_kwargs
)
return model
@register
class CONCH_V1_5(FoundationBackend):
NAME = "CONCH_V1_5"
IMG_SIZE = 448
def _build_model(self, ckpt_path: str | None):
"""
Build the TITAN backbone from HuggingFace and extract CONCH model.
:param ckpt_path: Path to the root directory of the TITAN HuggingFace repository (not a single checkpoint file).
:return: Extracted CONCH model from TITAN.
"""
if ckpt_path is not None:
titan = AutoModel.from_pretrained(
ckpt_path, trust_remote_code=True, local_files_only=True
)
else:
titan = AutoModel.from_pretrained(
"MahmoodLab/TITAN", trust_remote_code=True
)
model, _ = titan.return_conch()
return model
@register
class H_OPTIMUS_1(FoundationBackend):
NAME = "H-OPTIMUS-1"
IMG_SIZE = 224
MEAN = (0.707223, 0.578729, 0.703617)
STD = (0.211883, 0.230117, 0.177517)
def _build_model(self, ckpt_path: str | None):
"""
Build the H-optimus-1 vision transformer.
:param ckpt_path: Optional local checkpoint path. If provided, load from disk.
:return: Initialized model ready for feature extraction.
"""
if ckpt_path is not None:
model = timm.create_model(
"hf-hub:bioptimus/H-optimus-1",
pretrained=False,
init_values=1e-5,
dynamic_img_size=False,
)
model.load_state_dict(
torch.load(ckpt_path, map_location="cuda"), strict=True
)
else:
model = timm.create_model(
"hf-hub:bioptimus/H-optimus-1",
pretrained=True,
init_values=1e-5,
dynamic_img_size=False,
)
return model
def get_transform(self) -> transforms.Compose:
"""
Preprocessing for H-optimus-1.
"""
return (
transforms.Compose(
[
transforms.Resize(self.IMG_SIZE),
transforms.ToTensor(),
transforms.Normalize(mean=self.MEAN, std=self.STD),
]
),
self.IMG_SIZE,
)
@register
class VIRCHOW2(FoundationBackend):
NAME = "VIRCHOW2"
IMG_SIZE = 224 # Virchow2 is 224x224
def _build_model(self, ckpt_path: str | None):
"""
Build paige-ai/Virchow2 and override forward to return a 2560-D embedding:
concat(class_token, mean(patch_tokens)).
"""
# Need these for proper init as per the reference snippet
timm_kwargs = dict(mlp_layer=timm.layers.SwiGLUPacked, act_layer=torch.nn.SiLU)
if ckpt_path is not None:
model = timm.create_model(
"hf-hub:paige-ai/Virchow2", pretrained=False, **timm_kwargs
)
model.load_state_dict(
torch.load(ckpt_path, map_location="cuda"), strict=True
)
else:
model = timm.create_model(
"hf-hub:paige-ai/Virchow2", pretrained=True, **timm_kwargs
)
# Keep the original forward that returns token embeddings [B, 261, 1280]
_orig_forward = model.forward
def _forward_embedding(x: torch.Tensor) -> torch.Tensor:
"""
Returns a 2560-D embedding: [cls_token || mean(patch_tokens)].
Assumes tokens: 0=CLS, 1-4=register, 5: = patches (256 tokens).
"""
out = _orig_forward(x) # [B, 261, 1280]
cls_tok = out[:, 0] # [B, 1280]
patch_toks = out[:, 5:] # [B, 256, 1280]
pooled = patch_toks.mean(dim=1) # [B, 1280]
return torch.cat([cls_tok, pooled], dim=-1) # [B, 2560]
# Replace forward with the embedding-producing one
model.forward = _forward_embedding
return model
def get_transform(self):
return (
transforms.Compose(
[
transforms.Resize(
self.IMG_SIZE,
interpolation=transforms.InterpolationMode.BICUBIC,
antialias=True,
),
transforms.CenterCrop(self.IMG_SIZE),
transforms.ToTensor(), # replaces "MaybeToTensor()" for torchvision
transforms.Normalize(mean=self.MEAN, std=self.STD),
]
),
self.IMG_SIZE,
)
def _get_backend(name: str) -> FoundationBackend:
"""
Retrieve a registered foundation backend by name.
:param name: Identifier of the backend (case-insensitive).
:return: An instance of FoundationBackend.
:raises ValueError: If no backend is registered under the given name.
"""
try:
return _BACKENDS[name.upper()]
except KeyError as e:
raise ValueError(f"Unknown foundation model: {name!r}") from e
def get_foundation_model(params: dict, device: torch.device) -> torch.nn.Module:
"""
Return a frozen, evaluation-ready feature extractor.
:param params: Dictionary containing 'name' and optional 'ckpt_path'.
:param device: Device to which the model should be moved.
:return: Frozen torch.nn.Module in eval mode.
"""
backend = _get_backend(params.get("name", ""))
return backend.get_model(params.get("ckpt_path"), device)
def get_fm_transform(params: dict):
"""
Return preprocessing transform pipeline for a foundation model.
:param params: Dictionary containing 'name'.
:return: A tuple of (transform pipeline, image size).
"""
backend = _get_backend(params.get("name", ""))
return backend.get_transform()