-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict_scratch.py
159 lines (124 loc) · 4.37 KB
/
predict_scratch.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
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
import gc
import os.path
from argparse import ArgumentParser, Namespace
from glob import glob
import numpy as np
import torch
from monai.data import DataLoader, Dataset
from monai.inferers import SlidingWindowInferer
from monai.transforms import (
Compose,
EnsureChannelFirstd,
LoadImaged,
NormalizeIntensityd,
Orientationd,
ToTensord,
)
from custom_transforms import CustomResized
from saver import NiftiSaver
# from saver import NiftiSaver
num_labels_with_bg = 14
roi_size = (128, 128, 64)
def main(params: Namespace):
device = torch.device("cpu" if params.gpu_index < 0 else f"cuda:{params.gpu_index}")
model = torch.jit.load(params.ckpt_path, map_location=device).eval()
pred_image_paths = glob(os.path.join(params.predict_dir, "*.nii.gz"))
pred_dicts = tuple({"image": img} for img in pred_image_paths)
keys = "image"
pred_transforms = Compose(
(
LoadImaged(reader="NibabelReader", keys=keys),
EnsureChannelFirstd(keys=keys),
CustomResized(keys=keys, roi_size=roi_size),
Orientationd(keys, axcodes="RAI"),
NormalizeIntensityd(keys="image"),
ToTensord(keys=keys),
)
)
pred_ds = Dataset(pred_dicts, pred_transforms)
saver = NiftiSaver(
params.output_dir,
output_dtype=np.uint8,
dtype=np.float32,
mode="nearest",
padding_mode="zeros",
separate_folder=False,
print_log=True, # make false for docker
)
sliding_inferer = SlidingWindowInferer(
roi_size,
params.sw_batch_size,
params.sw_overlap,
mode="gaussian",
cache_roi_weight_map=True,
)
if params.post_process:
from monai.transforms import KeepLargestConnectedComponent
keep_connected_component = KeepLargestConnectedComponent(
applied_labels=range(1, num_labels_with_bg),
is_onehot=False,
independent=True,
connectivity=params.connectivity,
)
dl = DataLoader(
pred_ds,
batch_size=1, # Because the images do not align and are not cropped
num_workers=params.num_workers,
)
with torch.inference_mode():
for batch in dl:
image = batch["image"].to(device)
output = sliding_inferer(image, model).cpu()
# channel_dim = 1
# sm = torch.softmax(output, dim=channel_dim)
# print(
# "Mean Max with Back",
# sm.max(dim=channel_dim).values.mean(),
# )
# sm_fore = sm[:, 1:, ...].max(dim=channel_dim).values
# print(
# "Mean Max without Back",
# sm_fore.mean(),
# )
# print(
# "Std Max without Back",
# sm_fore.std(),
# )
# print(
# "Max without Back",
# sm_fore.max(),
# )
# print(
# "Median without Back",
# sm_fore.median(),
# end="\n\n\n",
# )
# Squeezing for a single batch
argmax_out = output.squeeze(0).argmax(0)
post_out = (
keep_connected_component(argmax_out)
if params.post_process
else argmax_out
)
meta_data = {k: v[0] for k, v in batch["image_meta_dict"].items()}
saver(post_out, meta_data)
# Run garbage collector if RAM is OOM
# Reduced max GPU usage from 5G to 4G
# gc.collect()
# if params.gpu_index >= 0:
# torch.cuda.empty_cache()
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument(
"--ckpt_path", default="flare_supervised_checkpoint.pt", type=str
)
parser.add_argument("--predict_dir", default="inputs", type=str)
parser.add_argument("--output_dir", default="outputs", type=str)
parser.add_argument("--sw_batch_size", default=8, type=int)
parser.add_argument("--sw_overlap", default=0.25, type=float)
parser.add_argument("--num_workers", default=4, type=int)
parser.add_argument("--gpu_index", default=0, type=int)
parser.add_argument("--post_process", default=False, type=bool)
parser.add_argument("--connectivity", default=None, type=int)
args = parser.parse_args()
main(args)