-
Notifications
You must be signed in to change notification settings - Fork 10
/
main_test.py
391 lines (324 loc) · 17.4 KB
/
main_test.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
import glob
import yaml
import torch
import imageio
import argparse
import numpy as np
from tqdm import tqdm
from copy import deepcopy
from pathlib import Path
from omegaconf import OmegaConf
from importlib import import_module
from torchvision.transforms.functional import resized_crop
from main import instantiate_from_config
from x2ct_nerf.modules.losses.lpips import LPIPS
from utils.logger.TestLogger import ExperimentLogger
from utils.metrics import Peak_Signal_to_Noise_Rate_total, Peak_Signal_to_Noise_Rate_2D, Structural_Similarity_slice, Structural_Similarity, mse2psnr, img2mse, to8b
@torch.no_grad()
def sort_test_data(config):
load_file_path = config['data']['params']['validation']['params']['test_images_list_file']
save_file_path = load_file_path.replace(".txt", "_sorted.txt")
print(save_file_path)
with open(load_file_path, 'r') as file:
sub_folders = file.readlines()
sub_folders.sort()
with open(save_file_path, 'w') as file:
for file_name in sub_folders:
file.write(f"{file_name}")
@torch.no_grad()
def load_config(config_path, display=False):
config = OmegaConf.load(config_path)
if display:
print(yaml.dump(OmegaConf.to_container(config)))
return config
@torch.no_grad()
def load_vqgan(config, model_module, ckpt_path=None):
model_module = model_module.split(".")
model_module, model_class = model_module[:-1], model_module[-1]
model_module = ".".join(model_module)
model_module = getattr(import_module(model_module), model_class)
model = model_module(**config.model.params)
if ckpt_path is not None:
sd = torch.load(ckpt_path, map_location="cpu")["state_dict"]
missing, unexpected = model.load_state_dict(sd, strict=False)
return model.eval()
@torch.no_grad()
def change_config_file_to_sorted(config, val_test):
config['model']['params']['lossconfig'] = {'target': "taming.modules.losses.vqperceptual.DummyLoss"}
test_path = config['data']['params']['validation']['params']['test_images_list_file'].split("/")
config['data']['params']['validation']['params']['opt']['ct_size'] = 128
test_path[-1] = f"{val_test}_sorted.txt"
test_path = "/".join(test_path)
config['data']['params']['validation']['params']['test_images_list_file'] = test_path
config['data']['params']['batch_size'] = config['input_ct_res']
return config
@torch.no_grad()
def change_config_file_to_fixed_test(config, batch=3):
config['model']['params']['lossconfig'] = {'target': "taming.modules.losses.vqperceptual.DummyLoss"}
test_path = config['data']['params']['validation']['params']['test_images_list_file'].split("/")
test_path[-1] = f"test.txt"
test_path = "/".join(test_path)
config['data']['params']['validation']['params']['test_images_list_file'] = test_path
config['data']['params']['batch_size'] = batch
return config
@torch.no_grad()
def change_config_file_to_fixed_val(config, batch=3):
config['model']['params']['lossconfig'] = {'target': "taming.modules.losses.vqperceptual.DummyLoss"}
test_path = config['data']['params']['validation']['params']['test_images_list_file'].split("/")
test_path[-1] = f"val.txt"
test_path = "/".join(test_path)
config['data']['params']['validation']['params']['test_images_list_file'] = test_path
config['data']['params']['batch_size'] = batch
return config
@torch.no_grad()
def change_config_file_to_fixed_train(config, batch=3):
config['model']['params']['lossconfig'] = {'target': "taming.modules.losses.vqperceptual.DummyLoss"}
test_path = config['data']['params']['validation']['params']['test_images_list_file'].split("/")
test_path[-1] = f"train.txt"
test_path = "/".join(test_path)
config['data']['params']['validation']['params']['test_images_list_file'] = test_path
config['data']['params']['batch_size'] = batch
return config
@torch.no_grad()
def calculate_metric_per_axis(target, output, perceptual_loss_fn, ct_min_max):
target_ = target.unsqueeze(1).repeat(1, 3, 1, 1)
output_ = output.unsqueeze(1).repeat(1, 3, 1, 1)
lpips = perceptual_loss_fn(target_.contiguous(), output_.contiguous()).mean()
### psnr and ssim
test_ct_recon_psnr = mse2psnr(img2mse(output, target))
test_psnr_dict = Peak_Signal_to_Noise_Rate_total(
target.unsqueeze(0), output.unsqueeze(0), size_average=False, PIXEL_MIN=ct_min_max[0],
PIXEL_MAX=ct_min_max[1]
)
test_ssim_dict = Structural_Similarity(target.unsqueeze(0).detach().cpu().numpy(),
output.unsqueeze(0).detach().cpu().numpy(),
PIXEL_MAX=1.0, size_average=True, channel_axis=False, gray_scale=True)
return lpips, test_ct_recon_psnr.item(), test_psnr_dict[-1].item(), test_ssim_dict[-1]
@torch.no_grad()
def calculate_metric_per_axis_2D(target, output, perceptual_loss_fn, ct_min_max):
target_ = target.unsqueeze(1).repeat(1, 3, 1, 1)
output_ = output.unsqueeze(1).repeat(1, 3, 1, 1)
lpips = perceptual_loss_fn(target_.contiguous(), output_.contiguous()).mean()
### psnr and ssim
test_axis_psnr = Peak_Signal_to_Noise_Rate_2D(target.unsqueeze(0), output.unsqueeze(0),
PIXEL_MIN=ct_min_max[0], PIXEL_MAX=ct_min_max[1], use_real_max=True)
test_axis_ssim = Structural_Similarity_slice(target.unsqueeze(0).detach().cpu().numpy(),
output.unsqueeze(0).detach().cpu().numpy(),
PIXEL_MAX=None, channel_axis=False)
return lpips, test_axis_psnr.item(), test_axis_ssim.item()
@torch.no_grad()
def calculate_metric_per_patient(recon, gt, curr_axis, DEVICE, logger, ct_min_max, batch_idx, save_freq, dicts,
testsavedir, prev_patient, perceptual_loss_fn,
total_ct_recon_lpips, total_ct_recon_psnr, total_ct_recon_ssim):
test_lpips_avg = 0
test_psnr_avg = 0
test_ssim_avg = 0
test_axis_count = 0
for axis_name in ['axial', 'coronal', 'sagittal']:
if len(recon.keys()) == 1 and curr_axis == 'axial':
target = gt[curr_axis].to(DEVICE) # 'axial', 'coronal', 'sagittal'
output = recon[curr_axis].to(DEVICE)
if axis_name == 'coronal':
target = target.permute(1, 2, 0)
output = output.permute(1, 2, 0)
elif axis_name == 'sagittal':
target = target.permute(2, 0, 1)
output = output.permute(2, 0, 1)
elif len(recon.keys()) == 3:
target = gt[axis_name].to(DEVICE)
output = recon[axis_name].to(DEVICE)
lpips_axis, test_psnr_axis, test_ssim_axis = calculate_metric_per_axis_2D(target, output, perceptual_loss_fn, ct_min_max)
logger.log_loss(loss_name=f"test_lpips_{axis_name}", loss=lpips_axis)
logger.log_loss(loss_name=f"test_psnr_ct_{axis_name}", loss=test_psnr_axis)
logger.log_loss(loss_name=f"test_ssim_ct_{axis_name}", loss=test_ssim_axis)
test_axis_count += 1
test_lpips_avg += lpips_axis
test_psnr_avg += test_psnr_axis
test_ssim_avg += test_ssim_axis
total_ct_recon_lpips[axis_name] = total_ct_recon_lpips[
axis_name] + lpips_axis if axis_name in total_ct_recon_lpips.keys() else lpips_axis
total_ct_recon_psnr[axis_name] = total_ct_recon_psnr[
axis_name] + test_psnr_axis if axis_name in total_ct_recon_psnr.keys() else test_psnr_axis
total_ct_recon_ssim[axis_name] = total_ct_recon_ssim[
axis_name] + test_ssim_axis if axis_name in total_ct_recon_ssim.keys() else test_ssim_axis
if batch_idx % save_freq == 0 and dicts['save_result']:
testsave_folder = f"{testsavedir}/{prev_patient}"
Path(testsave_folder).mkdir(exist_ok=True, parents=True)
output_img = output.data.cpu().numpy()
target_img = target.data.cpu().numpy()
for slice_idx in range(output_img.shape[0]):
slice_pred = output_img[slice_idx]
slice_gt = target_img[slice_idx]
slice_pred = to8b(slice_pred)
slice_gt = to8b(slice_gt)
slice = np.concatenate((slice_pred, slice_gt), axis=1)
filename = f"{testsave_folder}/{axis_name}_output_gt_{slice_idx:03d}.png"
imageio.imwrite(filename, slice)
test_lpips_avg = test_lpips_avg / test_axis_count
test_psnr_avg = test_psnr_avg / test_axis_count
test_ssim_avg = test_ssim_avg / test_axis_count
logger.log.write(f"# of test axis: {test_axis_count} / axial, coronal, sagittal")
logger.log_loss(loss_name=f"test_lpips_avg", loss=test_lpips_avg)
logger.log_loss(loss_name=f"test_psnr_avg", loss=test_psnr_avg)
logger.log_loss(loss_name=f"test_ssim_avg", loss=test_ssim_avg)
total_ct_recon_lpips["total_ct_recon_lpips"] = total_ct_recon_lpips[
"total_ct_recon_lpips"] + test_lpips_avg if "total_ct_recon_lpips" in total_ct_recon_lpips.keys() else test_lpips_avg
total_ct_recon_psnr["total_ct_recon_psnr"] = total_ct_recon_psnr[
"total_ct_recon_psnr"] + test_psnr_avg if "total_ct_recon_psnr" in total_ct_recon_psnr.keys() else test_psnr_avg
total_ct_recon_ssim["total_ct_recon_ssim"] = total_ct_recon_ssim[
"total_ct_recon_ssim"] + test_ssim_avg if "total_ct_recon_ssim" in total_ct_recon_ssim.keys() else test_ssim_avg
return total_ct_recon_lpips, total_ct_recon_psnr, total_ct_recon_ssim
@torch.no_grad()
def run_test_ramdom_image(dicts, prefix):
# few random slice
assert prefix is not None
saved_log_root = dicts['saved_log_root']
val_test = dicts['val_test']
testsavedir = f"{saved_log_root}/{val_test}_3dCT/{dicts['sub_folder']}"
for batch_idx, batch in enumerate(tqdm(dicts['data'].val_dataloader())):
# Inference one batch
log = dicts['model'].log_images(batch, split='val', p0=None, zoom_size=None)
test_output_img_full = log['reconstructions'][:, 0]
test_gt_img_full = log[dicts['model'].gt_key][:, 0]
test_output_img_full = test_output_img_full.data.cpu().numpy()
test_gt_img_full = test_gt_img_full.data.cpu().numpy()
testsave_folder = f"{testsavedir}"
Path(testsave_folder).mkdir(exist_ok=True, parents=True)
slices = None
for slice_idx in range(test_output_img_full.shape[0]):
slice_pred = to8b(test_output_img_full[slice_idx])
slice_target = to8b(test_gt_img_full[slice_idx])
slice = np.concatenate((slice_pred, slice_target), axis=1)
slices = np.concatenate((slices, slice), axis=0) if slices is not None else slice
filename = f"{testsave_folder}/{prefix}_OutputFull_GTFull.png"
imageio.imwrite(filename, slices)
return
@torch.no_grad()
def run_test(dicts, config):
saved_log_root = dicts['saved_log_root']
val_test = dicts['val_test']
save_freq = 1
DEVICE = dicts['device']
ct_min_max = config['data']['params']['train']['params']['opt']['CT_MIN_MAX']
testsavedir = f"{saved_log_root}/{val_test}_3dCT/{dicts['sub_folder']}"
logger = ExperimentLogger(testsavedir)
perceptual_loss_fn = LPIPS().eval().to(DEVICE)
count = 0
total_ct_recon_lpips = {}
total_ct_recon_psnr = {}
total_ct_recon_ssim = {}
recon = {}
gt = {}
prev_patient = None
for batch_idx, batch in enumerate(tqdm(dicts['data'].val_dataloader())):
for k, v in batch.items():
num_itr = v.shape[0] // dicts['sub_batch_size']
break
# Get output, target per patient
file_path_ = batch['file_path_'][0].split("/")
patient = file_path_[-3]
curr_axis = file_path_[-1].split("_")[0]
test_output_img = None
test_target_img = None
for it in range(num_itr):
sub_batch = {}
for k, v in batch.items():
sub_batch[k] = v[it*dicts['sub_batch_size']:(it+1)*dicts['sub_batch_size']]
log = dicts['model'].log_images(sub_batch, split='val')
sub_test_output_img = log['reconstructions'][:, 0]
sub_test_target_img = log['inputs'][:, 0]
test_output_img = torch.cat((test_output_img, sub_test_output_img), dim=0) if test_output_img is not None else sub_test_output_img
test_target_img = torch.cat((test_target_img, sub_test_target_img), dim=0) if test_target_img is not None else sub_test_target_img
if prev_patient is None or prev_patient == patient:
gt[curr_axis] = test_target_img.cpu()
recon[curr_axis] = test_output_img.cpu()
prev_patient = patient
else:
# calculate metric
logger.log.write("--------------------")
logger.log.write(f"Patient : {prev_patient}")
total_ct_recon_lpips, total_ct_recon_psnr, total_ct_recon_ssim = calculate_metric_per_patient(
recon, gt, curr_axis, DEVICE, logger, ct_min_max, batch_idx, save_freq,
dicts, testsavedir, prev_patient, perceptual_loss_fn,
total_ct_recon_lpips, total_ct_recon_psnr, total_ct_recon_ssim)
count += 1
gt[curr_axis] = test_target_img.cpu()
recon[curr_axis] = test_output_img.cpu()
prev_patient = patient
logger.log.write("--------------------")
logger.log.write(f"Patient : {prev_patient}")
total_ct_recon_lpips, total_ct_recon_psnr, total_ct_recon_ssim = calculate_metric_per_patient(
recon, gt, curr_axis, DEVICE, logger, ct_min_max, batch_idx, save_freq,
dicts, testsavedir, prev_patient, perceptual_loss_fn,
total_ct_recon_lpips, total_ct_recon_psnr, total_ct_recon_ssim)
count += 1
logger.log.write("---------- final results ----------")
for k, v in total_ct_recon_lpips.items():
k = k if k.split("_")[0] == 'total' else f"lpips_{k}"
logger.log_loss(loss_name=k, loss=v / count)
for k, v in total_ct_recon_psnr.items():
k = k if k.split("_")[0] == 'total' else f"psnr_{k}"
logger.log_loss(loss_name=k, loss=v / count)
for k, v in total_ct_recon_ssim.items():
k = k if k.split("_")[0] == 'total' else f"ssim_{k}"
logger.log_loss(loss_name=k, loss=v / count)
@torch.no_grad()
def main_test(args):
saved_log_root = args.save_dir
ckpt_name = args.ckpt_path.split("/")[-1] if args.ckpt_path is not None else args.ckpt_name
DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
if args.config_path is not None:
config_file = args.config_path
else:
config_file = glob.glob(f"{saved_log_root}/configs/*-project.yaml")[0]
config = load_config(config_file, display=False)
config['model']['params']['metadata']['encoder_params']['params']['zoom_resolution_div'] = 1
config['model']['params']['metadata']['encoder_params']['params']['zoom_min_scale'] = 0
if args.sub_batch_size is None:
args.sub_batch_size = config['input_ct_res']
assert config['input_ct_res'] % args.sub_batch_size == 0
# Set config
# prefix "None" for run_test, others for get random sample
configs = [
change_config_file_to_fixed_train(deepcopy(config)),
change_config_file_to_fixed_val(deepcopy(config)),
change_config_file_to_sorted(deepcopy(config), args.val_test)
]
prefixs = ['train', args.val_test, None]
dataset_type = config['data']['params']['validation']['params']['test_images_list_file'].split("/")[-2]
for prefix, cfg in zip(prefixs, configs):
model_module = cfg['model']['target']
if args.ckpt_path is not None:
model_file = args.ckpt_path
elif args.ckpt_name is not None:
model_file = f"{saved_log_root}/checkpoints/{ckpt_name}"
else:
raise ValueError("ckpt_path or ckpt_name should be provided")
model = load_vqgan(config=cfg, model_module=model_module, ckpt_path=model_file).to(DEVICE)
# dataloader
data = instantiate_from_config(cfg.data)
data.prepare_data()
data.setup()
dicts = {"saved_log_root": saved_log_root, 'sub_folder': ckpt_name.replace(".ckpt", ""),
"data": data, "model": model, "device": DEVICE, 'dataset_type': dataset_type,
"val_test": args.val_test, "save_result": args.save_result, "sub_batch_size": args.sub_batch_size,
"p0": None, "zoom_size": None}
if prefix is None:
run_test(dicts, cfg)
else:
run_test_ramdom_image(dicts, prefix)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Text to Radiance Fields')
parser.add_argument('--save_dir', type=str, help='path to saved_folder')
parser.add_argument('--val_test', type=str, default='val', help='val or test')
parser.add_argument('--save_result', action='store_false', help='save result, default = True, Set this to False to find best model')
parser.add_argument('--sub_batch_size', type=int, default=None)
parser.add_argument('--config_path', type=str, default="./configs/PerX2CT.yaml")
parser.add_argument('--ckpt_path', type=str, default="./checkpoints/PerX2CT.ckpt")
parser.add_argument('--ckpt_name', type=str, default='last.ckpt', help='ckpt file name')
args = parser.parse_args()
assert (args.ckpt_path is not None) or (args.ckpt_name is not None)
torch.set_grad_enabled(False)
print()
print(args.save_dir)
main_test(args)