-
Notifications
You must be signed in to change notification settings - Fork 1
/
inference.py
187 lines (139 loc) · 5.18 KB
/
inference.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import os
from tqdm import tqdm
import pandas as pd
import torch
from torch import nn
from monai.data import DataLoader, Dataset, NiftiSaver
from monai.transforms import (
Compose,
LoadNiftid,
AddChanneld,
NormalizeIntensityd,
Orientationd,
ToTensord,
)
from monai.utils import set_determinism
from monai.inferers import sliding_window_inference
from network.unet2d5 import UNet2D5
# Define training and patches sampling parameters
SPATIAL_SHAPE = (224,224,48)
NB_CLASSES = 2
# Number of worker
workers = 20
# Training parameters
val_eval_criterion_alpha = 0.95
train_loss_MA_alpha = 0.95
nb_patience = 10
patience_lr = 5
weight_decay = 1e-5
PHASES = ['training', 'validation', 'inference']
def infinite_iterable(i):
while True:
yield from i
def inference(paths_dict, model, transform_inference, device, opt):
# Define transforms for data normalization and augmentation
dataloaders = dict()
subjects_dataset = dict()
checkpoint_path = os.path.join(opt.model_dir,'models', './CP_{}.pth')
checkpoint_path = checkpoint_path.format(opt.epoch_inf)
assert os.path.isfile(checkpoint_path), 'no checkpoint found'
print(checkpoint_path)
model.load_state_dict(torch.load(checkpoint_path))
model = model.to(device)
for phase in ['inference']:
subjects_dataset[phase] = Dataset(paths_dict, transform=transform_inference)
dataloaders[phase] = DataLoader(subjects_dataset[phase], batch_size=1, shuffle=False)
model.eval() # Set model to evaluate mode
fold_name = 'output_pred'
# Iterate over data
with torch.no_grad():
saver = NiftiSaver(output_dir=os.path.join(opt.model_dir,fold_name))
for batch in tqdm(dataloaders['inference']):
inputs = batch['img'].to(device)
pred = sliding_window_inference(inputs, opt.spatial_shape, 1, model, mode='gaussian')
pred = pred.argmax(1, keepdim=True).detach()
saver.save_batch(pred, batch["img_meta_dict"])
def main():
opt = parsing_data()
set_determinism(seed=2)
if torch.cuda.is_available():
print('[INFO] GPU available.')
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
else:
raise Exception(
"[INFO] No GPU found.")
print("[INFO] Reading data")
# PHASES
split_path = os.path.join(opt.dataset_split)
df_split = pd.read_csv(split_path,header =None)
list_file = dict()
for phase in PHASES: # list of patient name associated to each phase
list_file[phase] = df_split[df_split[1].isin([phase])][0].tolist()
# CREATING DICT FOR DATASET
mod_ext = "_T2.nii.gz"
paths_dict = {split:[] for split in PHASES}
for split in PHASES:
for subject in list_file[split]:
subject_data = dict()
if os.path.exists(os.path.join(opt.path_data,subject+mod_ext)):
subject_data["img"] = os.path.join(opt.path_data,subject+mod_ext)
paths_dict[split].append(subject_data)
print(f"Nb patients in {split} data: {len(paths_dict[split])}")
# Logging hyperparameters
print("[INFO] Hyperparameters")
print('Spatial shape: {}'.format(opt.spatial_shape))
print(f"Inference on the {opt.phase} set")
# PREPROCESSING
all_keys = ["img"]
test_transforms = Compose(
(
LoadNiftid(keys=all_keys),
AddChanneld(keys=all_keys),
Orientationd(keys=all_keys, axcodes="RAS"),
NormalizeIntensityd(keys=all_keys),
ToTensord(keys=all_keys)
)
)
# MODEL
norm_op_kwargs = {"eps": 1e-5, "affine": True}
net_nonlin = nn.LeakyReLU
net_nonlin_kwargs = {"negative_slope": 1e-2, "inplace": True}
model= UNet2D5(input_channels=1,
base_num_features=16,
num_classes=NB_CLASSES,
num_pool=4,
conv_op=nn.Conv3d,
norm_op=nn.InstanceNorm3d,
norm_op_kwargs=norm_op_kwargs,
nonlin=net_nonlin,
nonlin_kwargs=net_nonlin_kwargs).to(device)
print("[INFO] Inference")
inference(paths_dict[opt.phase], model, test_transforms, device, opt)
def parsing_data():
parser = argparse.ArgumentParser(
description='Performing inference')
parser.add_argument('--model_dir',
type=str)
parser.add_argument("--dataset_split",
type=str,
default="splits/split_inextremis_budget1.csv")
parser.add_argument("--path_data",
type=str,
default="data/VS_MICCAI21/T2/")
parser.add_argument('--phase',
type=str,
default='inference')
parser.add_argument('--spatial_shape',
type=int,
nargs="+",
default=(224,224,48))
parser.add_argument('--epoch_inf',
type=str,
default='best')
opt = parser.parse_args()
return opt
if __name__ == '__main__':
main()