forked from wangchx67/FourLLIE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
177 lines (144 loc) · 6.04 KB
/
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
import argparse
import logging
import os.path as osp
import cv2
import lpips
from skimage.metrics import peak_signal_noise_ratio
from skimage.metrics import structural_similarity
import options.options as option
import utils.util as util
from data import create_dataset, create_dataloader
from models import create_model
# loss_fn_alex = lpips.LPIPS(net='alex').cuda()
loss_fn_alex = lpips.LPIPS(net='alex')
import torch
import numpy as np
#### options
parser = argparse.ArgumentParser()
parser.add_argument('-opt', type=str, default='./options/test/LOLv2_real.yml', help='Path to options YAML file.')
opt = option.parse(parser.parse_args().opt, is_train=False)
opt = option.dict_to_nonedict(opt)
def main():
save_imgs = True
model = create_model(opt)
save_folder = './results/{}'.format(opt['name'])
GT_folder = osp.join(save_folder, 'images/GT')
output_folder = osp.join(save_folder, 'images/output')
output_folder_s1 = osp.join(save_folder, 'images/output_s1')
input_folder = osp.join(save_folder, 'images/input')
util.mkdirs(save_folder)
util.mkdirs(GT_folder)
util.mkdirs(output_folder)
util.mkdirs(output_folder_s1)
util.mkdirs(input_folder)
print('mkdir finish')
util.setup_logger('base', save_folder, 'test', level=logging.INFO, screen=True, tofile=True)
logger = logging.getLogger('base')
for phase, dataset_opt in opt['datasets'].items():
val_set = create_dataset(dataset_opt)
val_loader = create_dataloader(val_set, dataset_opt, opt, None)
pbar = util.ProgressBar(len(val_loader))
psnr_rlt = {} # with border and center frames
psnr_rlt_avg = {}
psnr_total_avg = 0.
ssim_rlt = {} # with border and center frames
ssim_rlt_avg = {}
ssim_total_avg = 0.
lpips_rlt = {} # with border and center frames
lpips_rlt_avg = {}
lpips_total_avg = 0.
for val_data in val_loader:
folder = val_data['folder'][0]
idx_d = val_data['idx']
if psnr_rlt.get(folder, None) is None:
psnr_rlt[folder] = []
if ssim_rlt.get(folder, None) is None:
ssim_rlt[folder] = []
if lpips_rlt.get(folder, None) is None:
lpips_rlt[folder] = []
model.feed_data(val_data)
model.feed_data(val_data)
model.test()
visuals = model.get_current_visuals()
rlt_img = util.tensor2img(visuals['rlt']) # uint8
rlt_s1_img = util.tensor2img(visuals['rlt_s1']) # uint8
gt_img = util.tensor2img(visuals['GT']) # uint8
input_img = util.tensor2img(visuals['LQ'])
if save_imgs:
try:
tag = '{}.{}'.format(val_data['folder'], idx_d[0].replace('/', '-'))
print(osp.join(output_folder, '{}.png'.format(tag)))
cv2.imwrite(osp.join(output_folder, '{}.png'.format(tag)), rlt_img)
cv2.imwrite(osp.join(GT_folder, '{}.png'.format(tag)), gt_img)
cv2.imwrite(osp.join(output_folder_s1, '{}.png'.format(tag)), rlt_s1_img)
cv2.imwrite(osp.join(input_folder, '{}.png'.format(tag)), input_img)
except Exception as e:
print(e)
import ipdb;
ipdb.set_trace()
# calculate PSNR
# psnr = util.calculate_psnr(rlt_img, gt_img)
psnr = peak_signal_noise_ratio(rlt_img, gt_img)
psnr_rlt[folder].append(psnr)
# ssim = util.calculate_ssim(rlt_img, gt_img)
ssim = structural_similarity(rlt_img, gt_img, multichannel=True)
# ssim = 0
ssim_rlt[folder].append(ssim)
img, gt = rlt_img, gt_img
img = torch.from_numpy(np.float32(img))
gt = torch.from_numpy(np.float32(gt))
# img = img.permute(2, 0, 1).unsqueeze(0).cuda()
# gt = gt.permute(2, 0, 1).unsqueeze(0).cuda()
img = img.permute(2, 0, 1).unsqueeze(0)
gt = gt.permute(2, 0, 1).unsqueeze(0)
lpips_alex = loss_fn_alex(img, gt)
lpips_alex = lpips_alex.detach().cpu().numpy().squeeze()
lpips_rlt[folder].append(lpips_alex)
pbar.update('Test {} - {}'.format(folder, idx_d))
for k, v in psnr_rlt.items():
psnr_rlt_avg[k] = sum(v) / len(v)
psnr_total_avg += psnr_rlt_avg[k]
for k, v in ssim_rlt.items():
ssim_rlt_avg[k] = sum(v) / len(v)
ssim_total_avg += ssim_rlt_avg[k]
for k, v in lpips_rlt.items():
lpips_rlt_avg[k] = sum(v) / len(v)
lpips_total_avg += lpips_rlt_avg[k]
psnr_total_avg /= len(psnr_rlt)
ssim_total_avg /= len(ssim_rlt)
lpips_total_avg /= len(lpips_rlt)
log_s = '# Validation # PSNR: {:.4e}:'.format(psnr_total_avg)
for k, v in psnr_rlt_avg.items():
log_s += ' {}: {:.4e}'.format(k, v)
logger.info(log_s)
log_s = '# Validation # SSIM: {:.4e}:'.format(ssim_total_avg)
for k, v in ssim_rlt_avg.items():
log_s += ' {}: {:.4e}'.format(k, v)
logger.info(log_s)
log_s = '# Validation # LPIPS: {:.4e}:'.format(lpips_total_avg)
for k, v in lpips_rlt_avg.items():
log_s += ' {}: {:.4e}'.format(k, v)
logger.info(log_s)
psnr_all = 0
psnr_count = 0
for k, v in psnr_rlt.items():
psnr_all += sum(v)
psnr_count += len(v)
psnr_all = psnr_all * 1.0 / psnr_count
print(psnr_all)
ssim_all = 0
ssim_count = 0
for k, v in ssim_rlt.items():
ssim_all += sum(v)
ssim_count += len(v)
ssim_all = ssim_all * 1.0 / ssim_count
print(ssim_all)
lpips_all = 0
lpips_count = 0
for k, v in lpips_rlt.items():
lpips_all += sum(v)
lpips_count += len(v)
lpips_all = lpips_all * 1.0 / lpips_count
print(lpips_all)
if __name__ == '__main__':
main()