-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_model.py
executable file
·95 lines (86 loc) · 3.14 KB
/
run_model.py
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
#!/usr/bin/env python3
"""
run_model.py - Runs segmentation on a trained model
References
----------
* https://github.com/Project-MONAI/tutorials/blob/main/3d_segmentation/brats_segmentation_3d.ipynb
"""
import argparse
from pathlib import Path
import matplotlib.pyplot as plt
import monai
from monai.inferers import sliding_window_inference
from monai.networks.nets import SegResNet
from monai.networks.nets import UNet as MonaiUNet
import torch
from config import IMAGE_RESOLUTION
from data.containers import dataset_dict
from data.transforms import multi_channel_multiclass_label, validation_postprocessor
SLICES = 64
SLICES_TO_SHOW = 4
SLICE_GAP = SLICES // SLICES_TO_SHOW
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-m", "--model_path", type=str, default="trained_models/unet-model.pth")
parser.add_argument(
"-i",
"--image_path",
type=str,
default="local_data/train/BraTS-GLI-00000-000",
)
args = parser.parse_args()
monai.utils.set_determinism(seed=42, additional_settings=None)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if "unet" in args.model_path.casefold():
model = MonaiUNet(
spatial_dims=3,
in_channels=4,
out_channels=3,
channels=(16, 32, 64, 128, 256),
strides=(2, 2, 2, 2),
num_res_units=2,
)
elif "resnet" in args.model_path.casefold():
model = SegResNet(
spatial_dims=3,
in_channels=4,
out_channels=3,
)
else:
raise ValueError("Invalid model type specified")
model.load_state_dict(torch.load(args.model_path))
model.to(device)
data = dataset_dict(Path(args.image_path))
transformed_image = multi_channel_multiclass_label()(data)
image, label = transformed_image["image"].unsqueeze(0).to(device), transformed_image["seg"].to(device)
with torch.no_grad():
model.eval()
output = sliding_window_inference(
inputs=image,
roi_size=IMAGE_RESOLUTION,
sw_batch_size=1,
predictor=model,
overlap=0.5,
)
processed_output = validation_postprocessor()(output[0]).to("cpu")
fig, ax = plt.subplots(SLICES_TO_SHOW - 1, 3, figsize=(8, 8))
for i, s in enumerate(range(SLICE_GAP, SLICES - 1, SLICE_GAP)):
ax[i, 0].imshow(transformed_image["image"][0, :, :, s], cmap="gray")
if s == SLICE_GAP:
ax[i, 0].set_title("Input Images\n")
ax[i, 0].set_xlabel(f"Slice {s}")
ax[i, 0].set_xticks([])
ax[i, 0].set_yticks([])
ax[i, 1].imshow(transformed_image["seg"][0, :, :, s].detach().cpu())
if s == SLICE_GAP:
ax[i, 1].set_title("Input Labels\n")
ax[i, 1].set_xlabel(f"Slice {s}")
ax[i, 1].set_xticks([])
ax[i, 1].set_yticks([])
ax[i, 2].imshow(processed_output[0, :, :, s].detach().cpu())
if s == SLICE_GAP:
ax[i, 2].set_title("Model Segmentations\n")
ax[i, 2].set_xlabel(f"Slice {s}")
ax[i, 2].set_xticks([])
ax[i, 2].set_yticks([])
plt.show()