-
Notifications
You must be signed in to change notification settings - Fork 21
/
eval_vimeo90k.py
executable file
·156 lines (129 loc) · 6.63 KB
/
eval_vimeo90k.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""Evalute Space-Time Video Super-Resolution on Vimeo90k dataset.
"""
import os
import glob
import cv2
import argparse
import numpy as np
import torch
import logging
from skimage.metrics import peak_signal_noise_ratio
from skimage.color import rgb2ycbcr
from models import create_model
from utils import (mkdirs, parse_config, AverageMeter, structural_similarity,
read_seq_images, index_generation, setup_logger, get_model_total_params)
def main():
parser = argparse.ArgumentParser(description='Space-Time Video Super-Resolution Evaluation on Vimeo90k dataset')
parser.add_argument('--config', type=str, help='Path to config file (.yaml).')
args = parser.parse_args()
config = parse_config(args.config, is_train=False)
save_path = config['path']['save_path']
mkdirs(save_path)
setup_logger('base', save_path, 'test', level=logging.INFO, screen=True, tofile=True)
model = create_model(config)
model_params = get_model_total_params(model)
logger = logging.getLogger('base')
logger.info('use GPU {}'.format(config['gpu_ids']))
logger.info('Data: {} - {} - {}'.format(config['dataset']['name'], config['dataset']['mode'], config['dataset']['dataset_root']))
logger.info('Model path: {}'.format(config['path']['pretrain_model']))
logger.info('Model parameters: {} M'.format(model_params))
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model.load_state_dict(torch.load(config['path']['pretrain_model']), strict=True)
model.eval()
model = model.to(device)
LR_paths = sorted(glob.glob(os.path.join(config['dataset']['dataset_root'], config['dataset']['mode']+'_test', '*')))
PSNR = []
PSNR_Y = []
SSIM = []
SSIM_Y = []
for LR_path in LR_paths:
sub_save_path = os.path.join(save_path, LR_path.split('/')[-1])
mkdirs(sub_save_path)
sub_LR_paths = sorted(glob.glob(os.path.join(LR_path, '*')))
seq_PSNR = AverageMeter()
seq_PSNR_Y = AverageMeter()
seq_SSIM = AverageMeter()
seq_SSIM_Y = AverageMeter()
for sub_LR_path in sub_LR_paths:
sub_sub_save_path = os.path.join(sub_save_path, sub_LR_path.split('/')[-1])
mkdirs(sub_sub_save_path)
tested_index = []
sub_GT_path = sub_LR_path.replace('_LR', '')
imgs_LR = read_seq_images(sub_LR_path)
imgs_LR = imgs_LR.astype(np.float32) / 255.
imgs_LR = torch.from_numpy(imgs_LR).permute(0, 3, 1, 2).contiguous()
imgs_GT = read_seq_images(sub_GT_path)
indices_list = index_generation(config['dataset']['num_out_frames'], imgs_LR.shape[0])
clips_PSNR = AverageMeter()
clips_PSNR_Y = AverageMeter()
clips_SSIM = AverageMeter()
clips_SSIM_Y = AverageMeter()
for indices in indices_list:
inputs = imgs_LR[indices[::2]].unsqueeze(0).to(device)
with torch.no_grad():
outputs = model(inputs)
outputs = outputs.cpu().squeeze().clamp(0, 1).numpy()
# PSNR, SSIM for each frame
for idx, frame_idx in enumerate(indices):
if frame_idx in tested_index:
continue
tested_index.append(frame_idx)
output = (outputs[idx].squeeze().transpose((1, 2, 0)) * 255.0).round().astype(np.uint8)
target = imgs_GT[frame_idx]
output_y = rgb2ycbcr(output)[..., 0]
target_y = rgb2ycbcr(target)[..., 0]
psnr = peak_signal_noise_ratio(output, target)
psnr_y = peak_signal_noise_ratio(output_y, target_y, data_range=255)
ssim = structural_similarity(output, target)
ssim_y = structural_similarity(output_y, target_y)
cv2.imwrite(os.path.join(sub_save_path, '{:08d}.png'.format(frame_idx+1)), output[...,::-1])
clips_PSNR.update(psnr)
clips_PSNR_Y.update(psnr_y)
clips_SSIM.update(ssim)
clips_SSIM_Y.update(ssim_y)
msg = '{:3d} - PSNR: {:.6f} dB PSNR-Y: {:.6f} dB ' \
'SSIM: {:.6f} SSIM-Y: {:.6f}'.format(
frame_idx + 1, psnr, psnr_y, ssim, ssim_y
)
logger.info(msg)
msg = 'Folder {}/{} - Average PSNR: {:.6f} dB PSNR-Y: {:.6f} dB ' \
'Average SSIM: {:.6f} SSIM-Y: {:.6f} for {} frames; '.format(
LR_path.split('/')[-1], sub_LR_path.split('/')[-1],
clips_PSNR.average(), clips_PSNR_Y.average(),
clips_SSIM.average(), clips_SSIM_Y.average(),
clips_PSNR.count
)
logger.info(msg)
seq_PSNR.update(clips_PSNR.average())
seq_PSNR_Y.update(clips_PSNR_Y.average())
seq_SSIM.update(clips_SSIM.average())
seq_SSIM_Y.update(clips_SSIM_Y.average())
msg = 'Folder {} - Average PSNR: {:.6f} dB PSNR-Y: {:.6f} dB ' \
'Average SSIM: {:.6f} SSIM-Y: {:.6f} for {} clips; '.format(
LR_path.split('/')[-1], seq_PSNR.average(),
seq_PSNR_Y.average(), seq_SSIM.average(),
seq_SSIM_Y.average(), seq_PSNR.count
)
logger.info(msg)
PSNR.append(seq_PSNR.average())
PSNR_Y.append(seq_PSNR_Y.average())
SSIM.append(seq_SSIM.average())
SSIM_Y.append(seq_SSIM_Y.average())
logger.info('################ Tidy Outputs ################')
for path, psnr, psnr_y, ssim, ssim_y in zip(LR_paths, PSNR, PSNR_Y, SSIM, SSIM_Y):
msg = 'Folder {} - Average PSNR: {:.6f} dB PSNR-Y: {:.6f} dB ' \
'SSIM: {:.6f} dB SSIM-Y: {:.6f} dB. '.format(
path.split('/')[-1], psnr, psnr_y, ssim, ssim_y
)
logger.info(msg)
logger.info('################ Final Results ################')
logger.info('Data: {} - {} - {}'.format(config['dataset']['name'], config['dataset']['mode'], config['dataset']['dataset_root']))
logger.info('Model path: {}'.format(config['path']['pretrain_model']))
msg = 'Total Average PSNR: {:.6f} dB PSNR-Y: {:.6f} dB SSIM: {:.6f} dB ' \
'SSIM-Y: {:.6f} dB for {} clips.'.format(
sum(PSNR) / len(PSNR), sum(PSNR_Y) / len(PSNR_Y),
sum(SSIM) / len(SSIM), sum(SSIM_Y) / len(SSIM_Y), len(PSNR)
)
logger.info(msg)
if __name__ == '__main__':
main()