This repository has been archived by the owner on May 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
predict_single_image.py
311 lines (232 loc) · 11.2 KB
/
predict_single_image.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from tqdm import tqdm
import datetime
import argparse
import matplotlib.pyplot as plt
import math
import scipy
import torch
from torch.autograd import Variable
import sys
import os
import torch
from utils.utils import *
import torch.optim as optim
import torch.nn as nn
from generators import build_netG
from discriminators import build_netD
from utils.NiftiDataset import *
import utils.NiftiDataset as NiftiDataset
from torch.utils.data import DataLoader
from collections import OrderedDict
parser = argparse.ArgumentParser()
parser.add_argument('--multi_gpu', default=True, help='Multi or single Gpu')
parser.add_argument('--gpu_ids', default='1', help='Select the GPU')
parser.add_argument("--image", type=str, default='./Data_folder/test/patient_5/image.nii')
parser.add_argument("--label", type=str, default=None)
parser.add_argument("--result", type=str, default='./Data_folder/test/patient_5/result.nii', help='path to the .nii result to save')
parser.add_argument("--weights", type=str, default='./checkpoints/g_epoch_200.pth', help='generator weights to load')
parser.add_argument("--resample", default=False, help='Decide or not to resample the images to a new resolution')
parser.add_argument("--new_resolution", type=float, default=(0.625, 0.625, 1), help='New resolution')
parser.add_argument("--patch_size", type=int, nargs=3, default=[128, 128, 64], help="Input dimension for the generator")
parser.add_argument("--batch_size", type=int, nargs=1, default=1, help="Batch size to feed the network (currently supports 1)")
parser.add_argument("--stride_inplane", type=int, nargs=1, default=16, help="Stride size in 2D plane")
parser.add_argument("--stride_layer", type=int, nargs=1, default=16, help="Stride size in z direction")
args = parser.parse_args()
def from_numpy_to_itk(image_np,image_itk):
image_np = np.transpose(image_np, (2, 1, 0))
image = sitk.GetImageFromArray(image_np)
image.SetOrigin(image_itk.GetOrigin())
image.SetDirection(image_itk.GetDirection())
image.SetSpacing(image_itk.GetSpacing())
return image
def prepare_batch(image, ijk_patch_indices):
image_batches = []
for batch in ijk_patch_indices:
image_batch = []
for patch in batch:
image_patch = image[patch[0]:patch[1], patch[2]:patch[3], patch[4]:patch[5]]
image_batch.append(image_patch)
image_batch = np.asarray(image_batch)
# image_batch = image_batch[:, :, :, :, np.newaxis]
image_batches.append(image_batch)
return image_batches
# inference single image
def inference(write_image, model, image_path, label_path, result_path, resample, resolution, patch_size_x, patch_size_y, patch_size_z, stride_inplane, stride_layer, batch_size=1, segmentation=True, Logger=True):
# create transformations to image and labels
transforms1 = [
NiftiDataset.Resample(resolution, resample)
]
transforms2 = [
NiftiDataset.Padding((patch_size_x, patch_size_y, patch_size_z))
]
# read image file
reader = sitk.ImageFileReader()
reader.SetFileName(image_path)
image = reader.Execute()
# normalize the image
image = Normalization(image)
castImageFilter = sitk.CastImageFilter()
castImageFilter.SetOutputPixelType(sitk.sitkFloat32)
image = castImageFilter.Execute(image)
# create empty label in pair with transformed image
label_tfm = sitk.Image(image.GetSize(), sitk.sitkFloat32)
label_tfm.SetOrigin(image.GetOrigin())
label_tfm.SetDirection(image.GetDirection())
label_tfm.SetSpacing(image.GetSpacing())
sample = {'image': image, 'label': label_tfm}
for transform in transforms1:
sample = transform(sample)
# keeping track on how much padding will be performed before the inference
image_array = sitk.GetArrayFromImage(sample['image'])
pad_x = patch_size_x - (patch_size_x - image_array.shape[2])
pad_y = patch_size_x - (patch_size_y - image_array.shape[1])
pad_z = patch_size_z - (patch_size_z - image_array.shape[0])
image_pre_pad = sample['image']
for transform in transforms2:
sample = transform(sample)
image_tfm, label_tfm = sample['image'], sample['label']
# convert image to numpy array
image_np = sitk.GetArrayFromImage(image_tfm)
label_np = sitk.GetArrayFromImage(label_tfm)
label_np = np.asarray(label_np, np.float32)
# unify numpy and sitk orientation
image_np = np.transpose(image_np, (2, 1, 0))
label_np = np.transpose(label_np, (2, 1, 0))
if segmentation is True:
label_np = np.around(label_np)
# ----------------- Padding the image if the z dimension still is not even ----------------------
if (image_np.shape[2] % 2) == 0:
Padding = False
else:
image_np = np.pad(image_np, ((0,0), (0,0), (0, 1)), 'edge')
label_np = np.pad(label_np, ((0, 0), (0, 0), (0, 1)), 'edge')
Padding = True
# ------------------------------------------------------------------------------------------------
# a weighting matrix will be used for averaging the overlapped region
weight_np = np.zeros(label_np.shape)
# prepare image batch indices
inum = int(math.ceil((image_np.shape[0] - patch_size_x) / float(stride_inplane))) + 1
jnum = int(math.ceil((image_np.shape[1] - patch_size_y) / float(stride_inplane))) + 1
knum = int(math.ceil((image_np.shape[2] - patch_size_z) / float(stride_layer))) + 1
patch_total = 0
ijk_patch_indices = []
ijk_patch_indicies_tmp = []
for i in range(inum):
for j in range(jnum):
for k in range(knum):
if patch_total % batch_size == 0:
ijk_patch_indicies_tmp = []
istart = i * stride_inplane
if istart + patch_size_x > image_np.shape[0]: # for last patch
istart = image_np.shape[0] - patch_size_x
iend = istart + patch_size_x
jstart = j * stride_inplane
if jstart + patch_size_y > image_np.shape[1]: # for last patch
jstart = image_np.shape[1] - patch_size_y
jend = jstart + patch_size_y
kstart = k * stride_layer
if kstart + patch_size_z > image_np.shape[2]: # for last patch
kstart = image_np.shape[2] - patch_size_z
kend = kstart + patch_size_z
ijk_patch_indicies_tmp.append([istart, iend, jstart, jend, kstart, kend])
if patch_total % batch_size == 0:
ijk_patch_indices.append(ijk_patch_indicies_tmp)
patch_total += 1
batches = prepare_batch(image_np, ijk_patch_indices)
if Logger is True:
for i in range(len(batches)):
batch = batches[i]
batch = (batch - 127.5) / 127.5
batch = torch.from_numpy(batch[np.newaxis, :, :, :])
batch = Variable(batch.cuda())
pred = model(batch)
pred = pred.squeeze().data.cpu().numpy()
pred = (pred * 127.5) + 127.5
istart = ijk_patch_indices[i][0][0]
iend = ijk_patch_indices[i][0][1]
jstart = ijk_patch_indices[i][0][2]
jend = ijk_patch_indices[i][0][3]
kstart = ijk_patch_indices[i][0][4]
kend = ijk_patch_indices[i][0][5]
label_np[istart:iend, jstart:jend, kstart:kend] += pred[:, :, :]
weight_np[istart:iend, jstart:jend, kstart:kend] += 1.0
else:
for i in tqdm(range(len(batches))):
batch = batches[i]
batch = (batch - 127.5) / 127.5
batch = torch.from_numpy(batch[np.newaxis, :, :, :])
batch = Variable(batch.cuda())
pred = model(batch)
pred = pred.squeeze().data.cpu().numpy()
pred = (pred * 127.5) + 127.5
istart = ijk_patch_indices[i][0][0]
iend = ijk_patch_indices[i][0][1]
jstart = ijk_patch_indices[i][0][2]
jend = ijk_patch_indices[i][0][3]
kstart = ijk_patch_indices[i][0][4]
kend = ijk_patch_indices[i][0][5]
label_np[istart:iend, jstart:jend, kstart:kend] += pred[:, :, :]
weight_np[istart:iend, jstart:jend, kstart:kend] += 1.0
print("{}: Evaluation complete".format(datetime.datetime.now()))
# eliminate overlapping region using the weighted value
label_np = (np.float32(label_np) / np.float32(weight_np) + 0.01)
if segmentation is True:
label_np = abs(np.around(label_np))
# removed the 1 pad on z
if Padding is True:
label_np = label_np[:, :, 0:(label_np.shape[2]-1)]
# removed all the padding
label_np = label_np[:pad_x, :pad_y, :pad_z]
# convert back to sitk space
label = from_numpy_to_itk(label_np, image_pre_pad)
# ---------------------------------------------------------------------------------------------
# save label
writer = sitk.ImageFileWriter()
if resample is True:
print("{}: Resampling label back to original image space...".format(datetime.datetime.now()))
# label = resample_sitk_image(label, spacing=image.GetSpacing(), interpolator='bspline') # keep this commented
if segmentation is True:
label = resize(label, (sitk.GetArrayFromImage(image)).shape[::-1], sitk.sitkLinear)
label_array = np.around(sitk.GetArrayFromImage(label))
label = sitk.GetImageFromArray(label_array)
label.SetDirection(image.GetDirection())
label.SetOrigin(image.GetOrigin())
label.SetSpacing(image.GetSpacing())
else:
label = resize(label, (sitk.GetArrayFromImage(image)).shape[::-1], sitk.sitkBSpline)
label.SetDirection(image.GetDirection())
label.SetOrigin(image.GetOrigin())
label.SetSpacing(image.GetSpacing())
else:
label = label
if label_path is not None and segmentation is True:
reader = sitk.ImageFileReader()
reader.SetFileName(label_path)
true_label = reader.Execute()
true_label = sitk.GetArrayFromImage(true_label)
predicted = sitk.GetArrayFromImage(label)
dice = dice_coeff(predicted,true_label)
writer.SetFileName(result_path)
if write_image is True:
writer.Execute(label)
print("{}: Save evaluate label at {} success".format(datetime.datetime.now(), result_path))
if label_path is not None and segmentation is True:
print("Dice score:", dice)
return label, dice
else:
dice = None
return label, dice
if __name__ == "__main__":
from init import Options
opt = Options().parse()
if args.multi_gpu is True:
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu_ids # Multi-gpu selector for training
net = build_netG(opt).cuda() # load the network Unet
else:
torch.cuda.set_device(int(args.gpu_ids))
net = build_netG(opt).cuda()
net.load_state_dict(new_state_dict(args.weights))
result, dice = inference(True, net, args.image, None, args.result, args.resample, args.new_resolution,
args.patch_size[0],args.patch_size[1],args.patch_size[2], args.stride_inplane, args.stride_layer, segmentation=False, Logger=False)