-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathregression_age.py
More file actions
66 lines (56 loc) · 2.63 KB
/
regression_age.py
File metadata and controls
66 lines (56 loc) · 2.63 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
"""Example script to perform age regression using pre-trained checkpoint."""
from pathlib import Path
import numpy as np
import SimpleITK as sitk # noqa: N813
import torch
from huggingface_hub import hf_hub_download
from monai.transforms import Compose, ScaleIntensityd, SpatialPadd
from omegaconf import OmegaConf
from cinema import ConvViT
def run(seed: int, device: torch.device, dtype: torch.dtype) -> None:
"""Run age regression using fine-tuned checkpoint."""
trained_dataset, view = "mnms", "sax"
# load config to get class names
config_path = hf_hub_download(
repo_id="mathpluscode/CineMA",
filename=f"finetuned/regression_age/{trained_dataset}_{view}/config.yaml",
)
config = OmegaConf.load(config_path)
mean = config.data[config.data.regression_column].mean
std = config.data[config.data.regression_column].std
# load model
model = ConvViT.from_finetuned(
repo_id="mathpluscode/CineMA",
model_filename=f"finetuned/regression_age/{trained_dataset}_{view}/{trained_dataset}_{view}_{seed}.safetensors",
config_filename=f"finetuned/regression_age/{trained_dataset}_{view}/config.yaml",
)
model.eval()
model.to(device)
# load sample data
spatial_size = (192, 192, 16) if view == "sax" else (256, 256)
transform = Compose(
[
ScaleIntensityd(keys=view),
SpatialPadd(keys=view, spatial_size=spatial_size, method="end"),
]
)
exp_dir = Path(__file__).parent.parent.resolve()
ed_image = np.transpose(sitk.GetArrayFromImage(sitk.ReadImage(exp_dir / f"data/mnms/{view}_ed.nii.gz")))
es_image = np.transpose(sitk.GetArrayFromImage(sitk.ReadImage(exp_dir / f"data/mnms/{view}_es.nii.gz")))
image = np.stack([ed_image, es_image], axis=0) # (2, x, y, 1) or (2, x, y, z)
if view != "sax":
image = image[..., 0] # (2, x, y, 1) -> (2, x, y)
batch = transform({view: torch.from_numpy(image)})
batch = {k: v[None, ...].to(device=device, dtype=dtype) for k, v in batch.items()}
with torch.no_grad(), torch.autocast("cuda", dtype=dtype, enabled=torch.cuda.is_available()):
preds = model(batch) * std + mean
print(f"Using {view} view with model trained on {trained_dataset} dataset with seed {seed}.") # noqa: T201
print(f"The predicted age is {preds[0][0]}, ground truth should be 35.") # noqa: T201
if __name__ == "__main__":
dtype, device = torch.float32, torch.device("cpu")
if torch.cuda.is_available():
device = torch.device("cuda")
if torch.cuda.is_bf16_supported():
dtype = torch.bfloat16
for seed in range(3):
run(seed, device, dtype)