-
Notifications
You must be signed in to change notification settings - Fork 1
/
infer.py
77 lines (52 loc) · 2.06 KB
/
infer.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
import warnings
import time
from accelerate import Accelerator
from torch.utils.data import DataLoader
from torchmetrics.functional import peak_signal_noise_ratio, mean_squared_error, structural_similarity_index_measure
from torchvision.utils import save_image
from tqdm import tqdm
from config import Config
from data import get_validation_data
from models import *
from utils import *
warnings.filterwarnings('ignore')
opt = Config('config.yml')
seed_everything(opt.OPTIM.SEED)
def infer():
accelerator = Accelerator()
# Data Loader
val_dir = opt.TRAINING.VAL_DIR
val_dataset = get_validation_data(val_dir, {'w': opt.TRAINING.PS_W, 'h': opt.TRAINING.PS_H, 'ori': False})
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_rmse = 0
if not os.path.exists("result"):
os.makedirs("result")
total_time = 0
for idx, 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]
start_time = time.time()
with torch.no_grad():
res = model(inp)
end_time = time.time()
total_time += end_time - start_time
save_image(res, os.path.join(os.getcwd(), "result", test_data[2][0]))
stat_psnr += peak_signal_noise_ratio(res, tar, data_range=1)
stat_ssim += structural_similarity_index_measure(res, tar, data_range=1)
stat_rmse += mean_squared_error(torch.mul(res, 255), torch.mul(tar, 255), squared=False)
total_time /= len(testloader)
stat_psnr /= size
stat_ssim /= size
stat_rmse /= size
print("PSNR: {}, SSIM: {}, RMSE: {}, TIM: {}".format(stat_psnr, stat_ssim, stat_rmse, total_time))
if __name__ == '__main__':
infer()