-
Notifications
You must be signed in to change notification settings - Fork 56
/
valid.py
36 lines (28 loc) · 1.14 KB
/
valid.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
import torch
from torchvision.transforms import functional as F
from data import valid_dataloader
from utils import Adder
import os
from skimage.metrics import peak_signal_noise_ratio
def _valid(model, args, ep):
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
gopro = valid_dataloader(args.data_dir, batch_size=1, num_workers=0)
model.eval()
psnr_adder = Adder()
with torch.no_grad():
print('Start GoPro Evaluation')
for idx, data in enumerate(gopro):
input_img, label_img = data
input_img = input_img.to(device)
if not os.path.exists(os.path.join(args.result_dir, '%d' % (ep))):
os.mkdir(os.path.join(args.result_dir, '%d' % (ep)))
pred = model(input_img)
pred_clip = torch.clamp(pred[2], 0, 1)
p_numpy = pred_clip.squeeze(0).cpu().numpy()
label_numpy = label_img.squeeze(0).cpu().numpy()
psnr = peak_signal_noise_ratio(p_numpy, label_numpy, data_range=1)
psnr_adder(psnr)
print('\r%03d'%idx, end=' ')
print('\n')
model.train()
return psnr_adder.average()