-
Notifications
You must be signed in to change notification settings - Fork 56
/
eval.py
63 lines (49 loc) · 2 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
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
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()
with torch.no_grad():
psnr_adder = Adder()
# Hardware warm-up
for iter_idx, data in enumerate(dataloader):
input_img, label_img, _ = data
input_img = input_img.to(device)
tm = time.time()
_ = model(input_img)
_ = time.time() - tm
if iter_idx == 20:
break
# Main Evaluation
for iter_idx, data in enumerate(dataloader):
input_img, label_img, name = data
input_img = input_img.to(device)
tm = time.time()
pred = model(input_img)[2]
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()
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 = peak_signal_noise_ratio(pred_numpy, label_numpy, data_range=1)
psnr_adder(psnr)
print('%d iter PSNR: %.2f time: %f' % (iter_idx + 1, psnr, elapsed))
print('==========================================================')
print('The average PSNR is %.2f dB' % (psnr_adder.average()))
print("Average time: %f" % adder.average())