-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathpredict.py
234 lines (188 loc) · 9.27 KB
/
predict.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
import os
import time
import logging
import torch
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
cudnn.benchmark = True
import numpy as np
import nibabel as nib
import imageio
def one_hot(ori, classes):
batch, h, w, d = ori.size()
new_gd = torch.zeros((batch, classes, h, w, d), dtype=ori.dtype).cuda()
for j in range(classes):
index_list = (ori == j).nonzero()
for i in range(len(index_list)):
batch, height, width, depth = index_list[i]
new_gd[batch, j, height, width, depth] = 1
return new_gd.float()
def tailor_and_concat(x, model):
temp = []
temp.append(x[..., :128, :128, :128])
temp.append(x[..., :128, 112:240, :128])
temp.append(x[..., 112:240, :128, :128])
temp.append(x[..., 112:240, 112:240, :128])
temp.append(x[..., :128, :128, 27:155])
temp.append(x[..., :128, 112:240, 27:155])
temp.append(x[..., 112:240, :128, 27:155])
temp.append(x[..., 112:240, 112:240, 27:155])
y = x.clone()
for i in range(len(temp)):
temp[i] = model(temp[i])
y[..., :128, :128, :128] = temp[0]
y[..., :128, 128:240, :128] = temp[1][..., :, 16:128, :]
y[..., 128:240, :128, :128] = temp[2][..., 16:128, :, :]
y[..., 128:240, 128:240, :128] = temp[3][..., 16:128, 16:128, :]
y[..., :128, :128, 128:155] = temp[4][..., 96:123]
y[..., :128, 128:240, 128:155] = temp[5][..., :, 16:128, 96:123]
y[..., 128:240, :128, 128:155] = temp[6][..., 16:128, :, 96:123]
y[..., 128:240, 128:240, 128:155] = temp[7][..., 16:128, 16:128, 96:123]
return y[..., :155]
def dice_score(o, t, eps=1e-8):
num = 2*(o*t).sum() + eps
den = o.sum() + t.sum() + eps
return num/den
def mIOU(o, t, eps=1e-8):
num = (o*t).sum() + eps
den = (o | t).sum() + eps
return num/den
def softmax_mIOU_score(output, target):
mIOU_score = []
mIOU_score.append(mIOU(o=(output==1),t=(target==1)))
mIOU_score.append(mIOU(o=(output==2),t=(target==2)))
mIOU_score.append(mIOU(o=(output==3),t=(target==4)))
return mIOU_score
def softmax_output_dice(output, target):
ret = []
# whole
o = output > 0; t = target > 0 # ce
ret += dice_score(o, t),
# core
o = (output == 1) | (output == 3)
t = (target == 1) | (target == 4)
ret += dice_score(o, t),
# active
o = (output == 3);t = (target == 4)
ret += dice_score(o, t),
return ret
keys = 'whole', 'core', 'enhancing', 'loss'
def validate_softmax(
valid_loader,
model,
load_file,
multimodel,
savepath='', # when in validation set, you must specify the path to save the 'nii' segmentation results here
names=None, # The names of the patients orderly!
verbose=False,
use_TTA=False, # Test time augmentation, False as default!
save_format=None, # ['nii','npy'], use 'nii' as default. Its purpose is for submission.
snapshot=False, # for visualization. Default false. It is recommended to generate the visualized figures.
visual='', # the path to save visualization
postprocess=False, # Default False, when use postprocess, the score of dice_ET would be changed.
valid_in_train=False, # if you are valid when train
):
H, W, T = 240, 240, 160
model.eval()
runtimes = []
ET_voxels_pred_list = []
for i, data in enumerate(valid_loader):
print('-------------------------------------------------------------------')
msg = 'Subject {}/{}, '.format(i + 1, len(valid_loader))
if valid_in_train:
data = [t.cuda(non_blocking=True) for t in data]
x, target = data[:2]
else:
x = data
x.cuda()
if not use_TTA:
torch.cuda.synchronize() # add the code synchronize() to correctly count the runtime.
start_time = time.time()
logit = tailor_and_concat(x, model)
torch.cuda.synchronize()
elapsed_time = time.time() - start_time
logging.info('Single sample test time consumption {:.2f} minutes!'.format(elapsed_time/60))
runtimes.append(elapsed_time)
if multimodel:
logit = F.softmax(logit, dim=1)
output = logit / 4.0
load_file1 = load_file.replace('7998', '7996')
if os.path.isfile(load_file1):
checkpoint = torch.load(load_file1)
model.load_state_dict(checkpoint['state_dict'])
print('Successfully load checkpoint {}'.format(load_file1))
logit = tailor_and_concat(x, model)
logit = F.softmax(logit, dim=1)
output += logit / 4.0
load_file1 = load_file.replace('7998', '7997')
if os.path.isfile(load_file1):
checkpoint = torch.load(load_file1)
model.load_state_dict(checkpoint['state_dict'])
print('Successfully load checkpoint {}'.format(load_file1))
logit = tailor_and_concat(x, model)
logit = F.softmax(logit, dim=1)
output += logit / 4.0
load_file1 = load_file.replace('7998', '7999')
if os.path.isfile(load_file1):
checkpoint = torch.load(load_file1)
model.load_state_dict(checkpoint['state_dict'])
print('Successfully load checkpoint {}'.format(load_file1))
logit = tailor_and_concat(x, model)
logit = F.softmax(logit, dim=1)
output += logit / 4.0
else:
output = F.softmax(logit, dim=1)
else:
x = x[..., :155]
logit = F.softmax(tailor_and_concat(x, model), 1) # no flip
logit += F.softmax(tailor_and_concat(x.flip(dims=(2,)), model).flip(dims=(2,)), 1) # flip H
logit += F.softmax(tailor_and_concat(x.flip(dims=(3,)), model).flip(dims=(3,)), 1) # flip W
logit += F.softmax(tailor_and_concat(x.flip(dims=(4,)), model).flip(dims=(4,)), 1) # flip D
logit += F.softmax(tailor_and_concat(x.flip(dims=(2, 3)), model).flip(dims=(2, 3)), 1) # flip H, W
logit += F.softmax(tailor_and_concat(x.flip(dims=(2, 4)), model).flip(dims=(2, 4)), 1) # flip H, D
logit += F.softmax(tailor_and_concat(x.flip(dims=(3, 4)), model).flip(dims=(3, 4)), 1) # flip W, D
logit += F.softmax(tailor_and_concat(x.flip(dims=(2, 3, 4)), model).flip(dims=(2, 3, 4)), 1) # flip H, W, D
output = logit / 8.0 # mean
output = output[0, :, :H, :W, :T].cpu().detach().numpy()
output = output.argmax(0)
name = str(i)
if names:
name = names[i]
msg += '{:>20}, '.format(name)
print(msg)
if savepath:
# .npy for further model ensemble
# .nii for directly model submission
assert save_format in ['npy', 'nii']
if save_format == 'npy':
np.save(os.path.join(savepath, name + '_preds'), output)
if save_format == 'nii':
# raise NotImplementedError
oname = os.path.join(savepath, name + '.nii.gz')
seg_img = np.zeros(shape=(H, W, T), dtype=np.uint8)
seg_img[np.where(output == 1)] = 1
seg_img[np.where(output == 2)] = 2
seg_img[np.where(output == 3)] = 4
if verbose:
print('1:', np.sum(seg_img == 1), ' | 2:', np.sum(seg_img == 2), ' | 4:', np.sum(seg_img == 4))
print('WT:', np.sum((seg_img == 1) | (seg_img == 2) | (seg_img == 4)), ' | TC:',
np.sum((seg_img == 1) | (seg_img == 4)), ' | ET:', np.sum(seg_img == 4))
nib.save(nib.Nifti1Image(seg_img, None), oname)
print('Successfully save {}'.format(oname))
if snapshot:
""" --- grey figure---"""
# Snapshot_img = np.zeros(shape=(H,W,T),dtype=np.uint8)
# Snapshot_img[np.where(output[1,:,:,:]==1)] = 64
# Snapshot_img[np.where(output[2,:,:,:]==1)] = 160
# Snapshot_img[np.where(output[3,:,:,:]==1)] = 255
""" --- colorful figure--- """
Snapshot_img = np.zeros(shape=(H, W, 3, T), dtype=np.uint8)
Snapshot_img[:, :, 0, :][np.where(output == 1)] = 255
Snapshot_img[:, :, 1, :][np.where(output == 2)] = 255
Snapshot_img[:, :, 2, :][np.where(output == 3)] = 255
for frame in range(T):
if not os.path.exists(os.path.join(visual, name)):
os.makedirs(os.path.join(visual, name))
# scipy.misc.imsave(os.path.join(visual, name, str(frame)+'.png'), Snapshot_img[:, :, :, frame])
imageio.imwrite(os.path.join(visual, name, str(frame)+'.png'), Snapshot_img[:, :, :, frame])
print('runtimes:', sum(runtimes)/len(runtimes))