-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
324 lines (259 loc) · 12.7 KB
/
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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
'''
M&Ms-2 predictive model.
You must supply 1 mandatory method:
- predict: uses the model to perform predictions over the test folder
Note that for simplicity, in this example a basic U-Net architecture is provided.
Two 2D U-Nets were trained for SA and LA files without cross-validation nor pre/post-processing
and with rotation,scaling, translation and affine shearing augmentation.
The current working directory at runtime will be the model folder.
Any nested imports inside other modules must be realtive to its folder.
'''
import numpy as np
from os.path import isfile
import os
import nibabel as nib
import skimage.transform as tr
import torch
import torch.nn as nn
import torchvision.transforms.functional as F
import sys
import collections
import SimpleITK as sitk
from architecture.generic_UNet import *
from architecture.preprocessing import PreprocessorFor2D,GenericPreprocessor
from architecture.segmentation_export import save_segmentation_nifti_from_softmax
from architecture.dir import mk_or_cleardir,mkdir_if_not_exist,sort_glob
from architecture.help import crop_sa_z_by_la,paste_la_to_sa, reindex_label
class model:
def __init__(self):
'''
IMPORTANT: Initializes the model wrapper WITHOUT PARAMETERS, it will be called as model()
'''
self.input_dim = 256
self.device = torch.device("cpu" if not torch.cuda.is_available() else "cuda")
def inference_la(self, net_unet,input_file,output_file):
'''
IMPORTANT: Mandatory. This function makes predictions for an entire test folder.
'''
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
# PreprocessorFor2D
d=collections.OrderedDict()
d[0]="nonCT"
normalization_schemes=d
d=collections.OrderedDict()
d[0]=False
use_mask_for_norm=d
transpose_forward=[0,1,2]
intensity_properties=None
preprocessor = PreprocessorFor2D(normalization_schemes, use_mask_for_norm,
transpose_forward, intensity_properties)
#properties == dct
data, s, properties = preprocessor.preprocess_test_case([input_file],np.array([999,1,1]))
#
# data=torch.FloatTensor(1,1,288,288)
# data=data.cuda()
# sa_ed_pred = net_unet(img)
patch_size=np.array([256,256])
do_mirroring=True
mirror_axes=(0,1)
use_sliding_window=True
step_size=0.5
use_gaussian=True
regions_class_order=None
pad_border_mode="constant"
pad_kwargs= {'constant_values': 0}
all_in_gpu=False
verbose=True
mixed_precision=True
net_unet.eval()
ret = net_unet.predict_3D(data, do_mirroring=do_mirroring, mirror_axes=mirror_axes,
use_sliding_window=use_sliding_window, step_size=step_size,
patch_size=patch_size, regions_class_order=regions_class_order,
use_gaussian=use_gaussian, pad_border_mode=pad_border_mode,
pad_kwargs=pad_kwargs, all_in_gpu=all_in_gpu, verbose=verbose,
mixed_precision=mixed_precision)
softmax=ret[1]
transpose_forward = [0,1,2]
if transpose_forward is not None:
transpose_backward = [0,1,2]
softmax = softmax.transpose([0] + [i + 1 for i in transpose_backward])
force_separate_z = None
interpolation_order = 1
interpolation_order_z = 0
region_class_order=None
npz_file=None
save_segmentation_nifti_from_softmax(softmax, output_file, properties, interpolation_order, region_class_order,
None, None,
npz_file, None, force_separate_z, interpolation_order_z)
def build_la_model(self):
trained_unet = torch.load(os.path.join('./unet', '2d_model_best.model'))
num_input_channels=1
base_num_features=32
num_classes=3
net_num_pool_op_kernel_sizes=[[2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2]]
conv_per_stage=2
conv_op=torch.nn.Conv2d
norm_op=torch.nn.InstanceNorm2d
norm_op_kwargs={'eps': 1e-05, 'affine': True}
dropout_op=torch.nn.Dropout2d
dropout_op_kwargs={'p': 0, 'inplace': True}
net_nonlin=torch.nn.LeakyReLU
net_nonlin_kwargs={'negative_slope': 0.01, 'inplace': True}
net_conv_kernel_sizes=[[3, 3], [3, 3], [3, 3], [3, 3], [3, 3], [3, 3], [3, 3]]
net_unet=Generic_UNet(num_input_channels, base_num_features, num_classes,
len(net_num_pool_op_kernel_sizes),
conv_per_stage, 2, conv_op, norm_op, norm_op_kwargs, dropout_op,
dropout_op_kwargs,
net_nonlin, net_nonlin_kwargs, False, False, lambda x: x, InitWeights_He(1e-2),
net_num_pool_op_kernel_sizes, net_conv_kernel_sizes, False, True, True).float()
net_unet.load_state_dict(trained_unet['state_dict'])
net_unet.to(self.device)
return net_unet
def predictLA(self, output_folder, net_unet, case, la_ed):
name=os.path.basename(la_ed)
img=sitk.ReadImage(la_ed)
print(la_ed)
img_arr=sitk.GetArrayFromImage(img)
img_arr=np.squeeze(img_arr)
new_img = sitk.GetImageFromArray(img_arr.astype(np.float32)[None])
new_img.SetSpacing([1,1,999])
sitk.WriteImage(new_img,os.path.join(output_folder,"img_tmp.nii.gz"))
self.inference_la(net_unet,os.path.join(output_folder,"img_tmp.nii.gz"),os.path.join(output_folder,"lab_tmp.nii.gz"))
pred_lab=sitk.ReadImage(os.path.join(output_folder,"lab_tmp.nii.gz"))
lab_arr=sitk.GetArrayFromImage(pred_lab)
new_lab=sitk.GetImageFromArray(lab_arr)
new_lab.CopyInformation(img)
mkdir_if_not_exist(os.path.join(output_folder,case))
new_lab=reindex_label(new_lab,{3:[2],1:[1]})
sitk.WriteImage(new_lab,os.path.join(output_folder,case,name.replace('.nii.gz', '_pred.nii.gz')))
sitk.WriteImage(img,os.path.join(output_folder,case,name))
def predict(self, input_folder, output_folder):
'''
IMPORTANT: Mandatory. This function makes predictions for an entire test folder.
'''
net_unet_la = self.build_la_model()
net_unet_sa = self.build_sa_model()
for case in os.listdir(input_folder):
print(case)
try:
la_ed = os.path.join(input_folder, case, case + '_LA_ED.nii.gz')
self.predictLA(output_folder, net_unet_la, case, la_ed)
la_es = os.path.join(input_folder, case, case + '_LA_ES.nii.gz')
self.predictLA(output_folder, net_unet_la, case, la_es)
except Exception as e:
print(e)
print("la done")
# for case in ["174"]:
for case in os.listdir(input_folder):
print(f"#############################################################################{case}")
try:
sa_ed = os.path.join(input_folder, case, case + '_SA_ED.nii.gz')
la_ed = os.path.join(output_folder, case, case + '_LA_ED_pred.nii.gz')
self.predictSA(output_folder, net_unet_sa, case, sa_ed,la_ed)
sa_es = os.path.join(input_folder, case, case + '_SA_ES.nii.gz')
la_es = os.path.join(output_folder, case, case + '_LA_ES_pred.nii.gz')
self.predictSA(output_folder, net_unet_sa, case, sa_es,la_es)
except Exception as e:
print(e)
#post processing
all_pred=sort_glob(f"{output_folder}/*/*pred.nii.gz")
for p_l in all_pred:
lab=sitk.ReadImage(p_l)
lab=reindex_label(lab,{3:[3]})
sitk.WriteImage(lab,p_l)
def inference_sa(self, net_unet,input_file,output_file):
'''
IMPORTANT: Mandatory. This function makes predictions for an entire test folder.
'''
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
# PreprocessorFor2D
d=collections.OrderedDict()
d[0]="nonCT"
normalization_schemes=d
d=collections.OrderedDict()
d[0]=False
use_mask_for_norm=d
transpose_forward=[0,1,2]
intensity_properties=None
preprocessor = GenericPreprocessor(normalization_schemes, use_mask_for_norm,
transpose_forward, intensity_properties)
#properties == dct
img=sitk.ReadImage(input_file)
spacing=np.array(img.GetSpacing())[[2, 1, 0]]
data, s, properties = preprocessor.preprocess_test_case([input_file],spacing)
#
# data=torch.FloatTensor(1,1,288,288)
# data=data.cuda()
# sa_ed_pred = net_unet(img)
patch_size=np.array([10,192,192])
do_mirroring=True
mirror_axes=(0,1,2)
use_sliding_window=True
step_size=0.5
use_gaussian=True
regions_class_order=None
pad_border_mode="constant"
pad_kwargs= {'constant_values': 0}
all_in_gpu=False
verbose=True
mixed_precision=True
net_unet.eval()
ret = net_unet.predict_3D(data, do_mirroring=do_mirroring, mirror_axes=mirror_axes,
use_sliding_window=use_sliding_window, step_size=step_size,
patch_size=patch_size, regions_class_order=regions_class_order,
use_gaussian=use_gaussian, pad_border_mode=pad_border_mode,
pad_kwargs=pad_kwargs, all_in_gpu=all_in_gpu, verbose=verbose,
mixed_precision=mixed_precision)
softmax=ret[1]
transpose_forward = [0,1,2]
if transpose_forward is not None:
transpose_backward = [0,1,2]
softmax = softmax.transpose([0] + [i + 1 for i in transpose_backward])
force_separate_z = None
interpolation_order = 1
interpolation_order_z = 0
region_class_order=None
npz_file=None
save_segmentation_nifti_from_softmax(softmax, output_file, properties, interpolation_order, region_class_order,
None, None,
npz_file, None, force_separate_z, interpolation_order_z)
def build_sa_model(self):
trained_unet = torch.load(os.path.join('unet', '3d_model_best.model'))
num_input_channels=1
base_num_features=32
num_classes=3
net_num_pool_op_kernel_sizes=[[1, 2, 2], [1, 2, 2], [1, 2, 2], [2, 2, 2], [1, 2, 2]]
conv_per_stage=2
conv_op=torch.nn.Conv3d
norm_op=torch.nn.InstanceNorm3d
norm_op_kwargs={'eps': 1e-05, 'affine': True}
dropout_op=torch.nn.Dropout3d
dropout_op_kwargs={'p': 0, 'inplace': True}
net_nonlin=torch.nn.LeakyReLU
net_nonlin_kwargs={'negative_slope': 0.01, 'inplace': True}
net_conv_kernel_sizes=[[1, 3, 3], [1, 3, 3], [1, 3, 3], [3, 3, 3], [3, 3, 3], [3, 3, 3]]
net_unet=Generic_UNet(num_input_channels, base_num_features, num_classes,
len(net_num_pool_op_kernel_sizes),
conv_per_stage, 2, conv_op, norm_op, norm_op_kwargs, dropout_op,
dropout_op_kwargs,
net_nonlin, net_nonlin_kwargs, False, False, lambda x: x, InitWeights_He(1e-2),
net_num_pool_op_kernel_sizes, net_conv_kernel_sizes, False, True, True).float()
net_unet.load_state_dict(trained_unet['state_dict'])
net_unet.to(self.device)
return net_unet
def predictSA(self, output_folder, net_unet, case, sa_ed,la_ed_lab):
name=os.path.basename(sa_ed)
img=sitk.ReadImage(sa_ed)
la_img=sitk.ReadImage(la_ed_lab)
crop_img=crop_sa_z_by_la(img,la_img)
sitk.WriteImage(crop_img,os.path.join(output_folder,"img_crop_tmp.nii.gz"))
self.inference_sa(net_unet,os.path.join(output_folder,"img_crop_tmp.nii.gz"),os.path.join(output_folder,"lab_crop_tmp.nii.gz"))
pred_lab=sitk.ReadImage(os.path.join(output_folder,"lab_crop_tmp.nii.gz"))
new_lab=paste_la_to_sa(img,pred_lab)
mkdir_if_not_exist(os.path.join(output_folder,case))
new_lab=reindex_label(new_lab,{3:[2],1:[1]})
sitk.WriteImage(new_lab,os.path.join(output_folder,case,name.replace('.nii.gz', '_pred.nii.gz')))
sitk.WriteImage(img,os.path.join(output_folder,case,name))
if __name__=="__main__":
p=model()
p.predict('../validation','../validation_lab')