-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.py
68 lines (48 loc) · 1.96 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
import warnings
warnings.filterwarnings('ignore')
from accelerate import Accelerator
from torch.utils.data import DataLoader
from torchmetrics.functional import peak_signal_noise_ratio, structural_similarity_index_measure
from torchmetrics.functional.regression import mean_absolute_error
from torchvision.utils import save_image
from tqdm import tqdm
from config import Config
from data import get_data
from models import *
from utils import *
def test():
opt = Config('config.yml')
seed_everything(opt.OPTIM.SEED)
accelerator = Accelerator()
# Data Loader
val_dir = opt.TRAINING.VAL_DIR
val_dataset = get_data(val_dir, opt.MODEL.INPUT, opt.MODEL.TARGET, 'test', opt.TRAINING.ORI,
{'w': opt.TRAINING.PS_W, 'h': opt.TRAINING.PS_H})
testloader = DataLoader(dataset=val_dataset, batch_size=1, shuffle=False, num_workers=8, drop_last=False,
pin_memory=True)
# Model & Metrics
model = Model()
load_checkpoint(model, opt.TESTING.WEIGHT)
model, testloader = accelerator.prepare(model, testloader)
model.eval()
size = len(testloader)
stat_psnr = 0
stat_ssim = 0
stat_mae = 0
for _, test_data in enumerate(tqdm(testloader)):
# get the inputs; data is a list of [targets, inputs, filename]
inp = test_data[0].contiguous()
tar = test_data[1]
with torch.no_grad():
res = model(inp)
save_image(res, os.path.join("result", test_data[3][0]))
stat_psnr += peak_signal_noise_ratio(res, tar, data_range=1).item()
stat_ssim += structural_similarity_index_measure(res, tar, data_range=1).item()
stat_mae += mean_absolute_error(torch.mul(res, 255).flatten(), torch.mul(tar, 255).flatten()).item()
stat_psnr /= size
stat_ssim /= size
stat_lpips /= size
stat_mae /= size
print("MAE: {}, PSNR: {}, SSIM: {}".format(stat_mae, stat_psnr, stat_ssim))
if __name__ == '__main__':
test()