-
Notifications
You must be signed in to change notification settings - Fork 3
/
eval.py
83 lines (63 loc) · 2.88 KB
/
eval.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
# eval.py
# SPDX-License-Identifier: MIT
# See COPYING file for more details.
import os
import torch
from torchvision.transforms import functional as F
import numpy as np
from utils import Adder
from data import test_dataloader
from skimage.metrics import peak_signal_noise_ratio
import time
from pytorch_msssim import ssim
import torch.nn.functional as f
from skimage import img_as_ubyte
import cv2
def _eval(model, args):
state_dict = torch.load(args.test_model)
model.load_state_dict(state_dict['model'])
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
dataloader = test_dataloader(args.data_dir, batch_size=1, num_workers=0)
torch.cuda.empty_cache()
adder = Adder()
model.eval()
factor = 8
with torch.no_grad():
psnr_adder = Adder()
ssim_adder = Adder()
for iter_idx, data in enumerate(dataloader):
input_img, label_img, name = data
input_img = input_img.to(device)
h, w = input_img.shape[2], input_img.shape[3]
H, W = ((h+factor)//factor)*factor, ((w+factor)//factor*factor)
padh = H-h if h%factor!=0 else 0
padw = W-w if w%factor!=0 else 0
input_img = f.pad(input_img, (0, padw, 0, padh), 'reflect')
tm = time.time()
pred = model(input_img)[2]
pred = pred[:,:,:h,:w]
elapsed = time.time() - tm
adder(elapsed)
pred_clip = torch.clamp(pred, 0, 1)
pred_numpy = pred_clip.squeeze(0).cpu().numpy()
label_numpy = label_img.squeeze(0).cpu().numpy()
label_img = (label_img).cuda()
psnr_val = 10 * torch.log10(1 / f.mse_loss(pred_clip, label_img))
down_ratio = max(1, round(min(H, W) / 256))
ssim_val = ssim(f.adaptive_avg_pool2d(pred_clip, (int(H / down_ratio), int(W / down_ratio))),
f.adaptive_avg_pool2d(label_img, (int(H / down_ratio), int(W / down_ratio))),
data_range=1, size_average=False)
print('%d iter PSNR_dehazing: %.2f ssim: %f' % (iter_idx + 1, psnr_val, ssim_val))
ssim_adder(ssim_val)
if args.save_image:
save_name = os.path.join(args.result_dir, name[0])
pred_clip += 0.5 / 255
pred = F.to_pil_image(pred_clip.squeeze(0).cpu(), 'RGB')
pred.save(save_name)
psnr_mimo = peak_signal_noise_ratio(pred_numpy, label_numpy, data_range=1)
psnr_adder(psnr_val)
print('%d iter PSNR: %.2f time: %f' % (iter_idx + 1, psnr_mimo, elapsed))
print('==========================================================')
print('The average PSNR is %.2f dB' % (psnr_adder.average()))
print('The average SSIM is %.5f dB' % (ssim_adder.average()))
print("Average time: %f" % adder.average())