-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinference_whole.py
467 lines (341 loc) · 19 KB
/
inference_whole.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import os
import math
import random
import torch
import nibabel as nib
from models import create_model
from PIL import Image
import numpy as np
from options.train_options import TrainOptions
from utils.NiftiPromptDataset import NiftiPromptDataset
import utils.NiftiDataset as NiftiDataset
from torch.utils.data import DataLoader
from tqdm.auto import tqdm
def seed_everything(seed):
"""
Seed all necessary random number generators to ensure reproducible results.
"""
random.seed(seed) # Python's built-in random module
np.random.seed(seed) # Numpy module
torch.manual_seed(seed) # PyTorch
if torch.cuda.is_available():
torch.cuda.manual_seed(seed) # PyTorch CUDA (for a single GPU)
torch.cuda.manual_seed_all(seed) # PyTorch CUDA (for all GPUs)
def save_volume_as_images(volume_tensor, output_dir, image_id, mode, suffix="" ):
"""
Save a 3D volume tensor as a series of 2D images along the last dimension.
Args:
- volume_tensor: 3D tensor representing the volume, with shape [Depth, Height, Width].
- image_id: Identifier for the volume.
- output_dir: Directory where the images will be saved.
"""
# Remove any singleton dimensions (especially if tensor has shape [1, 1, Depth, Height, Width])
volume_tensor = volume_tensor.squeeze()
# print('volume_tensor: ', volume_tensor.shape)
# Verify that we now have a 3D tensor after squeezing
if len(volume_tensor.shape) != 3:
raise ValueError("The input tensor should be 3D after removing singleton dimensions.")
# Create a folder for the current image_id
image_folder = os.path.join(output_dir, f"{image_id}_png_images_{suffix}")
os.makedirs(image_folder, exist_ok=True)
# Iterate through the last dimension (depth) of the volume tensor
for slice_index in range(volume_tensor.shape[-1]):
print(f'Generating slice {slice_index}th image of {suffix}...', end='\r')
# Get a single slice from the volume along the last dimension
slice_tensor = volume_tensor[:, :, slice_index]
# Save each slice as an image
save_slice_as_image(slice_tensor, image_folder,
image_id, slice_index, mode=mode)
def save_slice_as_image(slice_tensor, output_dir, image_id, slice_index, mode="G"):
"""
Save a 2D tensor slice as an image.
Args:
- slice_tensor: 2D tensor representing the slice.
- output_dir: Directory where the images will be saved.
- image_id: Identifier for the volume.
- slice_index: Index of the slice in the volume.
- mode: Mode in which the image should be saved ("G" for grayscale, "RGB" for RGB mode).
"""
slice_tensor = slice_tensor.transpose(0, 1)
# Ensure the output directory exists
os.makedirs(output_dir, exist_ok=True)
if mode == "G":
# Normalize the grayscale slice to the range [0, 255]
normalized_slice = slice_tensor - slice_tensor.min()
if normalized_slice.max() > 0:
normalized_slice = normalized_slice * \
(255.0 / normalized_slice.max())
img_array = normalized_slice.cpu().detach().numpy().astype(np.uint8)
# 'L' mode for grayscale image
slice_image = Image.fromarray(img_array, mode='L')
elif mode == "RGB":
# Find the unique values in the tensor
unique_values = torch.unique(slice_tensor)
# print('unique_values: ', unique_values)
color_map = {
-1.0000: [0, 0, 0], # Black color: background
-0.5000: [0, 255, 0], # Green color: right,left lung
0.0000: [255, 0, 0], # Red color: right,left lung
0.5000: [255, 255, 0] # Yellow color airway
}
# Create an empty array for the RGB image
rgb_array = np.zeros((*slice_tensor.shape, 3), dtype=np.uint8)
# Fill in the array with the mapped colors
for value, color in color_map.items():
mask = slice_tensor == value
rgb_array[mask.cpu().numpy()] = color
slice_image = Image.fromarray(rgb_array, mode='RGB')
else:
raise ValueError(f"Unsupported mode: {mode}")
# Save the image
slice_image.save(os.path.join(
output_dir, f"{image_id}_{slice_index:04d}.png"))
def save_as_nifti(tensor, filename):
np_image = tensor.cpu().squeeze().numpy()
nifti_image = nib.Nifti1Image(np_image, affine=np.eye(4))
nib.save(nifti_image, filename)
def prepare_batch(opt, image, ijk_patch_indices):
image_batches = []
for patch in ijk_patch_indices:
image_patch = image[..., patch[0]:patch[1], patch[2]:patch[3], patch[4]:patch[5]]
image_batches.append(image_patch)
return image_batches
def inference(opt, model, dataset, prompt, dataloader = None):
if opt.single_file is True:
model.eval()
with torch.no_grad():
data = dataset.get_one_instance(
opt.image_path, opt.label_path, opt.file_name)
whole_label = data["label"]
image_id = data['image_id']
# 1 ---------- creating the batches from the data --------------------
# a weighting matrix will be used for averaging the overlapped region
max_x, max_y, max_z = data["image"][0,:,:].shape[0], data["image"][0,:,:,:].shape[1], data["image"][0,:,:,:].shape[2]
weight_np = torch.zeros((max_x, max_y, max_z))
predictions = torch.zeros((max_x, max_y, max_z))
# prepare image batch indices
inum = int(
math.ceil((weight_np.shape[0] - opt.patch_size[0]) / float(opt.stride_inplane))) + 1
jnum = int(
math.ceil((weight_np.shape[1] - opt.patch_size[1]) / float(opt.stride_inplane))) + 1
knum = int(
math.ceil((weight_np.shape[2] - opt.patch_size[2]) / float(opt.stride_layer))) + 1
patch_total = 0
ijk_patch_indices = []
for i in range(inum):
for j in range(jnum):
for k in range(knum):
# print(f'i: {i}, j: {j}, k: {k}')
# if patch_total % opt.batch_size == 0:
# ijk_patch_indicies_tmp = []
istart = i * opt.stride_inplane
if istart + opt.patch_size[0] > max_x: # for last patch
istart = max_x - opt.patch_size[0]
iend = istart + opt.patch_size[0]
jstart = j * opt.stride_inplane
if jstart + opt.patch_size[1] > max_y: # for last patch
jstart = max_y - opt.patch_size[1]
jend = jstart + opt.patch_size[1]
kstart = k * opt.stride_layer
if kstart + opt.patch_size[2] > max_z: # for last patch
kstart = max_z - opt.patch_size[2]
kend = kstart + opt.patch_size[2]
ijk_patch_indices.append(
[istart, iend, jstart, jend, kstart, kend])
# if patch_total % opt.batch_size == 0:
# ijk_patch_indices.append(ijk_patch_indicies_tmp)
patch_total += 1
if opt.model not in opt.label2maskmethods:
batches = prepare_batch(opt, data["label"][0,:,:,:], ijk_patch_indices)
else:
batches = prepare_batch(opt, data["labelmask"][:,:,:,:], ijk_patch_indices)
# 1 ---------- creating the batches from the data --------------------
# 2 ---------- obtain the predictions from the batches --------------------
print('weight_np: ', weight_np.shape)
for i in tqdm(range(len(batches))):
batch = batches[i]
# print('batch: ', len(batch))
if opt.model not in opt.label2maskmethods:
data["label"] = batch.unsqueeze(0).unsqueeze(0)
else:
data["labelmask"] = batch.unsqueeze(0)
model.set_input(data)
model.test()
visuals = model.get_current_visuals()
prediction = visuals['fake_B'].squeeze(0)
istart = ijk_patch_indices[i][0]
iend = ijk_patch_indices[i][1]
jstart = ijk_patch_indices[i][2]
jend = ijk_patch_indices[i][3]
kstart = ijk_patch_indices[i][4]
kend = ijk_patch_indices[i][5]
predictions[istart:iend, jstart:jend, kstart:kend] += prediction[0,:,:,:].cpu()
weight_np[istart:iend, jstart:jend, kstart:kend] += 1.0
average_predictions = (predictions / weight_np) + 0.01
# 2 ---------- obtain the predictions from the batches --------------------
# 3 ---------- save the results --------------------
if opt.save_3Dvolume is True:
save_as_nifti(average_predictions, f"{opt.results_dir}/{image_id}.nii.gz")
save_volume_as_images(
average_predictions, opt.results_dir, f'{image_id}',suffix="prediction", mode="G")
save_volume_as_images(
data["image"], opt.results_dir, f'{image_id}',suffix="original",mode="G")
save_volume_as_images(
whole_label, opt.results_dir, f'{image_id}', suffix="label", mode="RGB")
# 3 ---------- save the results --------------------
else:
print("Folder Predictions Mode!")
model.eval()
with torch.no_grad():
for index_file, data_indexer in enumerate(dataloader):
# 1 ---------- obtain the data from the dataloader --------------------
data = dataset.get_one_instance(
data_indexer["data_path"][0], data_indexer["label_path"][0], data_indexer["file_name"][0])
data["image"] = data["image"].unsqueeze(0)
data["label"] = data["label"].unsqueeze(0)
whole_label = data["label"]
image_id = data['image_id']
if opt.prompt_customised is True and "prompt" in data:
data['prompt'] = [prompt]
# 1 ---------- obtain the data from the dataloader --------------------
# 2 ---------- creating the batches from the data --------------------
# a weighting matrix will be used for averaging the overlapped region
max_x, max_y, max_z = data["image"][0,0,:,:,:].shape[0], data["image"][0,0,:,:,:].shape[1], data["image"][0,0,:,:,:].shape[2]
weight_np = torch.zeros((max_x, max_y, max_z))
predictions = torch.zeros((max_x, max_y, max_z))
inum = int(
math.ceil((weight_np.shape[0] - opt.patch_size[0]) / float(opt.stride_inplane))) + 1
jnum = int(
math.ceil((weight_np.shape[1] - opt.patch_size[1]) / float(opt.stride_inplane))) + 1
knum = int(
math.ceil((weight_np.shape[2] - opt.patch_size[2]) / float(opt.stride_layer))) + 1
patch_total = 0
ijk_patch_indices = []
for i in range(inum):
for j in range(jnum):
for k in range(knum):
# print(f'i: {i}, j: {j}, k: {k}')
# if patch_total % opt.batch_size == 0:
# ijk_patch_indicies_tmp = []
istart = i * opt.stride_inplane
if istart + opt.patch_size[0] > max_x: # for last patch
istart = max_x - opt.patch_size[0]
iend = istart + opt.patch_size[0]
jstart = j * opt.stride_inplane
if jstart + opt.patch_size[1] > max_y: # for last patch
jstart = max_y - opt.patch_size[1]
jend = jstart + opt.patch_size[1]
kstart = k * opt.stride_layer
if kstart + opt.patch_size[2] > max_z: # for last patch
kstart = max_z - opt.patch_size[2]
kend = kstart + opt.patch_size[2]
ijk_patch_indices.append(
[istart, iend, jstart, jend, kstart, kend])
# if patch_total % opt.batch_size == 0:
# ijk_patch_indices.append(ijk_patch_indicies_tmp)
patch_total += 1
if opt.model not in opt.label2maskmethods:
batches = prepare_batch(opt, data["label"][0,0,:,:,:], ijk_patch_indices)
else:
batches = prepare_batch(opt, data["labelmask"][:,:,:,:], ijk_patch_indices)
# 2 ---------- creating the batches from the data --------------------
# 3 ---------- obtain the predictions from the batches --------------------
for i in tqdm(range(len(batches))):
batch = batches[i]
# print('batch: ', len(batch))
if opt.model not in opt.label2maskmethods:
data["label"] = batch.unsqueeze(0).unsqueeze(0)
else:
data["labelmask"] = batch.unsqueeze(0)
model.set_input(data)
model.test()
visuals = model.get_current_visuals()
prediction = visuals['fake_B'].squeeze(0)
istart = ijk_patch_indices[i][0]
iend = ijk_patch_indices[i][1]
jstart = ijk_patch_indices[i][2]
jend = ijk_patch_indices[i][3]
kstart = ijk_patch_indices[i][4]
kend = ijk_patch_indices[i][5]
predictions[istart:iend, jstart:jend, kstart:kend] += prediction[0,:,:,:].cpu()
weight_np[istart:iend, jstart:jend, kstart:kend] += 1.0
average_predictions = (predictions / weight_np) + 0.01
# 3 ---------- obtain the predictions from the batches --------------------
# 4 ---------- save the results --------------------
if opt.save_3Dvolume is True:
save_as_nifti(average_predictions, f"{opt.results_dir}/{image_id}.nii.gz")
save_volume_as_images(
average_predictions, opt.results_dir, f'{image_id}',suffix="prediction", mode="G")
save_volume_as_images(
data["image"], opt.results_dir, f'{image_id}',suffix="original",mode="G")
save_volume_as_images(
whole_label, opt.results_dir, f'{image_id}', suffix="label", mode="RGB")
# 4 ---------- save the results --------------------
seed = 50
seed_everything(seed)
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
os.environ["CUDA_LAUNCH_BLOCKING"] = "1"
if __name__ == '__main__':
# Parse options using TrainOptions
opt = TrainOptions().parse()
opt.epoch_count = 0
opt.which_epoch = "latest"
opt.data_path = "./test_small_file_ver2/"
opt.batch_size = 1
opt.serial_batches = True
opt.no_flip = True
opt.display_id = -1
opt.workers = 4
opt.label2maskmethods = ["medddpm", "medddpmtext", "medddpmtextcross", "medddpmvisualprompt"] # Specific Diffusion special processing
# User Case Description:
# When running the inference script, the parameters that must be specified include: --name, --netG, and --model. These parameters are essential for execution, and their values should match the corresponding entries in the checkpoint directory.
# Command Example:
# python inference_single.py --name AWVS_200*64_pix2pix_improved_promopts_text_embeddings_lr_0.0001_ver1 --model pix2pixclip --netG resnet_9blocks_with_text_encoderembeddings_lr_0.0001_ver1
# The parameters must be compatible with the model training configuration. For details, please refer to the 'opt' file in the checkpoint directory.
# In addition, the following are the parameters that need to be adjusted within the Python text file
# Inference Configuration:
opt.patch_size = (256, 256, 64) # The patch size used for inference. Needs to match the size in the model checkpoint.
opt.input_nc = 1
opt.save_3Dvolume = False # To save the file in nii.gz format
opt.single_file = False # If False, read all files in the path
# Custom Prompt Evaluation Options:
opt.prompt_customised = False # Set to False to use the original pre-extracted prompts. When set to True, allows for custom prompt evaluation.
opt.stride_inplane = 128 # Overlapping reconstruction method
opt.stride_layer = 32 # Overlapping reconstruction method
prompt = None # only be used when opt.prompt_customised is set to be true
# Select a single NIfTI (.nii.gz) file for imaging:
# Ensure the selected file and directory exist before proceeding.
opt.file_name = "AIIB23_169.nii.gz"
# Single File mode
visualisation_root = "../../data/3D-CycleGan-Pytorch_data/"
opt.image_path = f"{visualisation_root}/img/{opt.file_name}"
opt.label_path = f"{visualisation_root}/gtlung/{opt.file_name}"
# Result Configuration:
# Define a suffix to distinguish the output directory name, and set the directory to save the results.
customised_name = "whole_136-140"
opt.suffix = f'{customised_name}' + opt.which_epoch + f"_epochs_whole_stride_{opt.stride_inplane}_{opt.stride_layer}"
if opt.single_file is True:
opt.suffix = "_" + opt.file_name.split(".")[0] + "_" + opt.suffix
opt.results_dir = os.path.join('./results/', opt.name + opt.suffix)
os.makedirs(opt.results_dir, exist_ok=True)
if torch.cuda.is_available() and len(opt.gpu_ids) > 0:
opt.device = torch.device(f"cuda:{opt.gpu_ids[0]}")
torch.cuda.set_device(opt.device)
else:
opt.device = torch.device("cpu")
model = create_model(opt)
model.setup(opt)
model.load_networks(opt.which_epoch)
min_pixel = int(opt.min_pixel * ((opt.patch_size[0] * opt.patch_size[1] * opt.patch_size[2]) / 50))
Transforms = None # Define any required transforms here
# Create the dataset and dataloader for inference
dataset = NiftiPromptDataset(opt.data_path, path_A=opt.generate_path, path_B=opt.input_path,
which_direction='AtoB', transforms=Transforms,
train=True, test = True, label2mask=True if opt.model in opt.label2maskmethods else False)
if opt.single_file is False:
dataloader = DataLoader(dataset, batch_size=1,
shuffle=False, num_workers=opt.workers)
else:
dataloader = None
# Perform inference using the dataloader
inference(opt, model, dataset, dataloader = dataloader, prompt=prompt)