-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_fastmri.py
168 lines (131 loc) · 5.67 KB
/
test_fastmri.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
import os
import sys
from tqdm import tqdm
import shutil
import argparse
import logging
import numpy as np
from skimage import io
from scipy.ndimage import zoom
import torch
import torch.nn as nn
from torchvision import transforms
from torch.utils.data import DataLoader
from networks.compare_models import build_model_from_name
from dataloaders.BRATS_dataloader_new import Hybrid as MyDataset
from dataloaders.BRATS_dataloader_new import ToTensor
from networks.mynet import TwoBranch
from utils import bright, trunc
from skimage.metrics import mean_squared_error, peak_signal_noise_ratio, structural_similarity
from option import args
test_data_path = args.root_path
snapshot_path = "model/" + args.exp + "/"
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
def normalize_output(out_img):
out_img = (out_img - out_img.min())/(out_img.max() - out_img.min() + 1e-8)
return out_img
from metric import nmse, psnr, ssim, AverageMeter
from collections import defaultdict
@torch.no_grad()
def evaluate(model, data_loader, device, save_path):
os.makedirs(save_path, exist_ok=True)
model.eval()
nmse_meter = AverageMeter()
psnr_meter = AverageMeter()
ssim_meter = AverageMeter()
output_dic = defaultdict(dict)
target_dic = defaultdict(dict)
input_dic = defaultdict(dict)
flag=0
last_name='no'
for data in data_loader:
pd, pdfs, _ = data
name = os.path.basename(pdfs[4][0]).split('.')[0]
if not last_name == name:
last_name = name
flag+=1
if flag < 3:
continue
elif flag >= 4:
break
else:
pass
target = pdfs[1]
mean = pdfs[2]
std = pdfs[3]
fname = pdfs[4]
slice_num = pdfs[5]
mean = mean.unsqueeze(1).unsqueeze(2)
std = std.unsqueeze(1).unsqueeze(2)
mean = mean.to(device)
std = std.to(device)
pd_img = pd[1].unsqueeze(1)
pdfs_img = pdfs[0].unsqueeze(1)
pd_img = pd_img.to(device)
pdfs_img = pdfs_img.to(device)
target = target.to(device)
outputs = network(pdfs_img, pd_img)['img_out']
outputs = outputs.squeeze(1)
outputs_save = outputs[0].cpu().numpy()/6.0
outputs_save = np.clip(outputs_save, a_min=-1, a_max=1)
io.imsave(save_path + str(name) + '_' + str(slice_num[0].cpu().numpy()) + '.png', target[0].cpu().numpy()/6.0)
io.imsave(save_path + str(name) + '_' + str(slice_num[0].cpu().numpy()) + '_in.png', pdfs_img[0][0].cpu().numpy()/6.0)
io.imsave(save_path + str(name) + '_' + str(slice_num[0].cpu().numpy()) + '_out.png', outputs_save)
outputs = outputs * std + mean
target = target * std + mean
inputs = pdfs_img.squeeze(1) * std + mean
output_dic[fname[0]][slice_num[0]] = outputs[0]
target_dic[fname[0]][slice_num[0]] = target[0]
input_dic[fname[0]][slice_num[0]] = inputs[0]
our_nmse = nmse(target[0].cpu().numpy(), outputs[0].cpu().numpy())
our_psnr = psnr(target[0].cpu().numpy(), outputs[0].cpu().numpy())
our_ssim = ssim(target[0].cpu().numpy(), outputs[0].cpu().numpy())
print('name:{}, slice:{}, psnr:{}, ssim:{}'.format(name, slice_num[0], our_psnr, our_ssim))
for name in output_dic.keys():
f_output = torch.stack([v for _, v in output_dic[name].items()])
f_target = torch.stack([v for _, v in target_dic[name].items()])
our_nmse = nmse(f_target.cpu().numpy(), f_output.cpu().numpy())
our_psnr = psnr(f_target.cpu().numpy(), f_output.cpu().numpy())
our_ssim = ssim(f_target.cpu().numpy(), f_output.cpu().numpy())
nmse_meter.update(our_nmse, 1)
psnr_meter.update(our_psnr, 1)
ssim_meter.update(our_ssim, 1)
print("==> Evaluate Metric")
print("Results ----------")
print("NMSE: {:.4}".format(np.array(nmse_meter.score).mean()))
print("PSNR: {:.4}".format(np.array(psnr_meter.score).mean()))
print("SSIM: {:.4}".format(np.array(ssim_meter.score).mean()))
print("NMSE: {:.4}".format(np.array(nmse_meter.score).std()))
print("PSNR: {:.4}".format(np.array(psnr_meter.score).std()))
print("SSIM: {:.4}".format(np.array(ssim_meter.score).std()))
print("------------------")
model.train()
return {'NMSE': nmse_meter.avg, 'PSNR': psnr_meter.avg, 'SSIM':ssim_meter.avg}
from dataloaders.fastmri import build_dataset
if __name__ == "__main__":
## make logger file
if not os.path.exists(snapshot_path):
os.makedirs(snapshot_path)
logging.basicConfig(filename=snapshot_path + "/log.txt", level=logging.INFO,
format='[%(asctime)s.%(msecs)03d] %(message)s', datefmt='%H:%M:%S')
logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
logging.info(str(args))
network = TwoBranch(args).cuda()
device = torch.device('cuda')
network.to(device)
if len(args.gpu.split(',')) > 1:
network = nn.DataParallel(network)
db_test = build_dataset(args, mode='val')
testloader = DataLoader(db_test, batch_size=1, shuffle=False, num_workers=4, pin_memory=True)
if args.phase == 'test':
save_mode_path = os.path.join(snapshot_path, 'best_checkpoint.pth')
print('load weights from ' + save_mode_path)
checkpoint = torch.load(save_mode_path)
weights_dict = {}
for k, v in checkpoint['network'].items():
new_k = k.replace('module.', '') if 'module' in k else k
weights_dict[new_k] = v
# breakpoint()
network.load_state_dict(weights_dict)
network.eval()
eval_result = evaluate(network, testloader, device, save_path = snapshot_path + '/result_case/')