-
Notifications
You must be signed in to change notification settings - Fork 2
/
inference.py
230 lines (171 loc) · 6.46 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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import time
import os
import numpy as np
import torch
from torch.utils.data import DataLoader
from torch.optim import lr_scheduler
from network.unet import UNet2D5
from tqdm import tqdm
import SimpleITK as sitk
import sys
import torchio
from torchio import ImagesDataset, Image, Subject, Queue, DATA
import multiprocessing as mp
from torchvision.transforms import Compose
from torchio.data.sampler import ImageSampler
from torchio.transforms import (
ZNormalization,
RandomMotion,
CenterCropOrPad,
Rescale,
RandomNoise,
RandomFlip,
RandomAffine,
ToCanonical,
Resample
)
from utilities.sampling import GridSampler, GridAggregator
import nibabel as nib
import pandas as pd
from torch import nn
#from apex import amp
# Define training and patches sampling parameters
patch_size = (128,128,128)
NB_CLASSES = 2
MODALITIES = ['t2']
def inference_padding(paths_dict,
model,
transformation,
device,
pred_path,
cp_path,
opt):
model.load_state_dict(torch.load(cp_path))
model.to(device)
model.eval()
subjects_dataset_inf = ImagesDataset(
paths_dict,
transform=transformation)
# batch_loader_inf = DataLoader(subjects_dataset_inf, batch_size=1)
#window_size = (256,256,256)
window_size = patch_size
border = (0,0,0)
for batch in tqdm(subjects_dataset_inf):
batch_pad = CenterCropOrPad((288,128,48))(batch)
mod_used = MODALITIES[-1]
data = batch_pad[mod_used][DATA].cuda().unsqueeze(0)
reference = torchio.utils.nib_to_sitk(batch[mod_used][DATA].numpy(), batch[mod_used]['affine'])
affine_pad = batch_pad[mod_used]['affine']
name = batch[mod_used]['stem']
with torch.no_grad():
logits, _ = model(data, 'source')
labels = logits.argmax(dim=1, keepdim=True)
labels = labels[0,0,...].cpu().numpy()
output = labels
output = torchio.utils.nib_to_sitk(output.astype(float), affine_pad)
output = sitk.Resample(
output,
reference,
sitk.Transform(),
sitk.sitkNearestNeighbor,
)
sitk.WriteImage(output, pred_path.format(name))
def main():
opt = parsing_data()
print("[INFO] Reading data.")
# Dictionary with data parameters for NiftyNet Reader
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 or Wrong gpu id, please run without --cuda")
# FOLDERS
fold_dir = opt.model_dir
checkpoint_path = os.path.join(fold_dir,'models', './CP_{}.pth')
checkpoint_path = checkpoint_path.format(opt.epoch_infe)
assert os.path.isfile(checkpoint_path), 'no checkpoint found'
if opt.output_dir is None:
output_path = os.path.join(fold_dir,'output')
else:
output_path = opt.output_dir
if not os.path.exists(output_path):
os.makedirs(output_path)
output_path = os.path.join(output_path,'output_{}.nii.gz')
# SPLITS
split_path = opt.dataset_split
assert os.path.isfile(split_path), 'split file not found'
print('Split file found: {}'.format(split_path))
# Reading csv file
df_split = pd.read_csv(split_path,header =None)
list_file = dict()
list_split = ['inference', 'validation']
for split in list_split:
list_file[split] = df_split[df_split[1].isin([split.lower()])][0].tolist()
# filing paths
paths_dict = {split:[] for split in list_split}
for split in list_split:
for subject in list_file[split]:
subject_data = []
for modality in MODALITIES:
subject_modality = opt.path_file+subject+modality+'.nii.gz'
if os.path.isfile(subject_modality):
subject_data.append(Image(modality, subject_modality, torchio.INTENSITY))
#subject_data.append(Image(modality, path_file[domain]+subject+modality+'.nii.gz', torchio.INTENSITY))
if len(subject_data)>0:
paths_dict[split].append(Subject(*subject_data))
transform_inference = (
ToCanonical(),
ZNormalization(),
)
transform_inference = Compose(transform_inference)
# MODEL
norm_op_kwargs = {'eps': 1e-5, 'affine': True}
dropout_op_kwargs = {'p': 0, 'inplace': True}
net_nonlin = nn.LeakyReLU
net_nonlin_kwargs = {'negative_slope': 1e-2, 'inplace': True}
print("[INFO] Building model.")
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)
paths_inf = paths_dict['inference']+paths_dict['validation']
inference_padding(paths_inf, model, transform_inference, device, output_path, checkpoint_path, opt)
def parsing_data():
parser = argparse.ArgumentParser(
description='3D Segmentation Using PyTorch and NiftyNet')
parser.add_argument('-epoch_infe',
type=str,
default = 'best')
parser.add_argument('-model_dir',
type=str)
parser.add_argument('-dataset_split',
type=str,
default='dataset_split.csv')
parser.add_argument('-path_file',
type=str,
default='../data/VS_T1/target/')
parser.add_argument('-add_sym',
type=int,
default=0,
choices=[0,1])
parser.add_argument('-output_dir',
type=str,
default=None)
parser.add_argument('-nb_classes',
type=int,
default=10)
parser.add_argument('-modalities',
default=MODALITIES)
opt = parser.parse_args()
return opt
if __name__ == '__main__':
main()