forked from 2y7c3/Super-Resolution-Neural-Operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
131 lines (103 loc) · 3.79 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
import argparse
import os
import math
from functools import partial
import yaml
import torch
from torch.utils.data import DataLoader
from tqdm import tqdm
import datasets
import models
import utils
def batched_predict(model, inp, coord, cell, bsize):
with torch.no_grad():
model.gen_feat(inp)
n = coord.shape[1]
ql = 0
preds = []
while ql < n:
qr = min(ql + bsize, n)
pred = model.query_rgb(coord[:, ql: qr, :], cell)
ql = qr
preds.append(pred)
pred = torch.cat(preds, dim=2)
return pred
def eval_psnr(loader, model, data_norm=None, eval_type=None, eval_bsize=None, scale_max=4,
verbose=False,mcell=False):
model.eval()
if data_norm is None:
data_norm = {
'inp': {'sub': [0], 'div': [1]},
'gt': {'sub': [0], 'div': [1]}
}
t = data_norm['inp']
inp_sub = torch.FloatTensor(t['sub']).view(1, -1, 1, 1).cuda()
inp_div = torch.FloatTensor(t['div']).view(1, -1, 1, 1).cuda()
t = data_norm['gt']
gt_sub = torch.FloatTensor(t['sub']).view(1, 1, -1).cuda()
gt_div = torch.FloatTensor(t['div']).view(1, 1, -1).cuda()
if eval_type is None:
metric_fn = utils.calc_psnr
elif eval_type.startswith('div2k'):
scale = int(eval_type.split('-')[1])
metric_fn = partial(utils.calc_psnr, dataset='div2k', scale=scale)
elif eval_type.startswith('benchmark'):
scale = int(eval_type.split('-')[1])
metric_fn = partial(utils.calc_psnr, dataset='benchmark', scale=scale)
else:
raise NotImplementedError
val_res = utils.Averager()
pbar = tqdm(loader, leave=False, desc='val')
cnt = 0
for batch in pbar:
cnt+=1
for k, v in batch.items():
batch[k] = v.cuda(non_blocking=True)
inp = (batch['inp'] - inp_sub) / inp_div
coord = batch['coord']
cell = batch['cell']
if mcell == False: c = 1
else : c = max(scale/scale_max, 1)
if eval_bsize is None:
with torch.no_grad():
pred = model(inp, coord, cell*c)
else:
pred = batched_predict(model, inp, coord, cell*c, eval_bsize)
with torch.no_grad():
pred = pred * gt_div + gt_sub
pred.clamp_(0, 1)
res = metric_fn(pred, batch['gt'])
val_res.add(res.item(), inp.shape[0])
if verbose:
pbar.set_description('val {:.4f}'.format(val_res.item()))
return val_res.item()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--config',default='./configs/test_srno.yaml')
parser.add_argument('--model')
parser.add_argument('--scale_max', default='4')
parser.add_argument('--gpu', default='1')
parser.add_argument('--mcell', default=False)
args = parser.parse_args()
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
#print(os.environ['CUDA_VISIBLE_DEVICES'])
with open(args.config, 'r') as f:
config = yaml.load(f, Loader=yaml.FullLoader)
spec = config['test_dataset']
dataset = datasets.make(spec['dataset'])
dataset = datasets.make(spec['wrapper'], args={'dataset': dataset})
loader = DataLoader(dataset, batch_size=spec['batch_size'],
num_workers=8, pin_memory=True, shuffle=False)
model_spec = torch.load(args.model)['model']
model = models.make(model_spec, load_sd=True).cuda()
import time
t1= time.time()
res = eval_psnr(loader, model,
data_norm=config.get('data_norm'),
eval_type=config.get('eval_type'),
eval_bsize=config.get('eval_bsize'),
scale_max = int(args.scale_max),
verbose=True,
mcell=bool(args.mcell))
t2 =time.time()
print('result: {:.4f}'.format(res), utils.time_text(t2-t1))