-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpredict.py
183 lines (150 loc) · 6.9 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
import argparse
import tensorflow as tf
import os
from dataset import TFRdataset
import argparse
import SimpleITK as sitk
import numpy as np
from tqdm import tqdm
from utils.patches import reconstruct_from_patches, get_patch_from_3d_data,\
compute_patch_indices
from utils.sitk_utils import resample_to_spacing
def patch_wise_prediction(output, data, patch_shape,
overlap=0, batch_size=1):
"""
:param batch_size:
:param model:
:param data:
:param overlap:
:return:
"""
predictions = []
indices = compute_patch_indices(data.shape[-3:],
patch_size=patch_shape,
overlap=overlap,
start=0)
batch = []
i = 0
pbar = tqdm(enumerate(indices),
unit='patches',
total=len(indices),
desc='scan_patches')
sess = tf.get_default_session()
for j, _ in pbar:
while len(batch) < batch_size:
patch = get_patch_from_3d_data(data[0],
patch_shape=patch_shape,
patch_index=indices[i])
batch.append(patch)
i += 1
prediction = sess.run(output,
feed_dict={'img:0': np.asarray(batch)})
batch = []
for predicted_patch in prediction:
predictions.append(predicted_patch)
output_shape = [int(output.shape[1])] + list(data.shape[-3:])
return reconstruct_from_patches(predictions,
patch_indices=indices,
data_shape=output_shape)
def main(fpath, output_path):
if not os.path.isdir(output_path):
os.makedirs(output_path, exist_ok=False)
with tf.Session() as sess:
# initiate dataloader
dset = TFRdataset(fpath)
# initiate model
tf.saved_model.loader.load(sess,
['serve'],
dset.dset['fpath_model'])
output = sess.graph.get_tensor_by_name('Generator/Softmax_1:0')
pbar = tqdm(enumerate(dset.dset['test']),
unit='scan',
total=len(dset.dset['test']),
desc='images to segment')
for i, img in pbar:
# read image
sitk_img = sitk.ReadImage(fpath + img.split('./')[-1])
# resample image to dset_spacing
element_spacing = sitk_img.GetSpacing()
sitk_img_resampled = resample_to_spacing(
sitk_img,
target_spacing=dset.spacing,
interpolation='linear')
resampled_array = sitk_img_resampled
# add dims until rank fits with what is expected by model
while len(resampled_array.shape) < 5:
resampled_array = resampled_array[np.newaxis, :]
predicted = patch_wise_prediction(
output=output,
data=resampled_array,
overlap=0,
batch_size=1,
patch_shape=dset.dset['patch_shape'])
np_mask = np.argmax(predicted, 0).astype(np.uint8)
sitk_mask = sitk.GetImageFromArray(np_mask)
sitk_mask.SetSpacing(dset.spacing)
# resample prediction to original image spacing
if dset.num_modalities > 1:
sitk_mask_resampled = resample_to_spacing(
sitk_mask,
target_spacing=element_spacing[:-1],
interpolation='nearest')
else:
sitk_mask_resampled = resample_to_spacing(
sitk_mask,
target_spacing=element_spacing,
interpolation='nearest')
sitk_mask_resampled = sitk.GetImageFromArray(sitk_mask_resampled)
# if required, either crop or pad sitk_mask_resampled
if dset.num_modalities > 1:
offset = (np.array(sitk_mask_resampled.GetSize())
- np.array(sitk_img.GetSize()[:-1]))/2
else:
offset = (np.array(sitk_mask_resampled.GetSize())
- np.array(sitk_img.GetSize()))/2
cropping = np.zeros(shape=(len(offset), 2),
dtype=int)
padding_required = False
for index, value in enumerate(offset):
if value > 0.0:
cropping[index][0] = int(np.floor(value))
cropping[index][1] = int(-np.ceil(value))
elif value == 0.0:
cropping[index][0] = 0
cropping[index][1] = sitk_img.GetSize()[index]
elif value < 0.0:
padding_required = True
cropping[index][1] = sitk_img.GetSize()[index]
if padding_required:
new_array = sitk_mask_resampled
difference = np.array(sitk_img.GetSize()) \
- np.array(sitk_mask_resampled.GetSize())
difference = np.clip(difference,
0,
10e6).astype(int)
difference = np.roll(difference, 1)
difference = [(diff, 0) for diff in difference]
new_array = np.pad(new_array, difference, mode='constant')
sitk_mask_resampled = sitk.GetImageFromArray(new_array)
sitk_mask.SetSpacing(element_spacing)
sitk_mask_cropped = sitk_mask_resampled[
cropping[0][0]:cropping[0][1],
cropping[1][0]:cropping[1][1],
cropping[2][0]:cropping[2][1]]
sitk.WriteImage(
sitk_mask_cropped,
os.path.join(output_path, img.split('/')[-1] + '.nii.gz'))
tqdm.write("{} processed".format(img))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Predict on Test dataset of \
MSD Challenge Task")
parser.add_argument('-i', '--input', help='input (root) path for dataset',
required=True)
parser.add_argument('-o', '--output', help='full path where to put images',
required=True)
args = vars(parser.parse_args())
fpath = args['input']
output_path = args['output']
output_path = os.path.join(output_path,
fpath.split('/')[-2])
main(fpath, output_path)