forked from neosr-project/neosr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
53 lines (44 loc) · 1.91 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
import logging
from time import time
from os import path as osp
import torch
from neosr.data import build_dataloader, build_dataset
from neosr.models import build_model
from neosr.utils import get_root_logger, get_time_str, make_exp_dirs
from neosr.utils.options import dict2str, parse_options
def test_pipeline(root_path):
# parse options, set distributed setting, set ramdom seed
opt, _ = parse_options(root_path, is_train=False)
torch.set_default_device('cuda')
torch.backends.cudnn.benchmark = True
# mkdir and initialize loggers
make_exp_dirs(opt)
log_file = osp.join(opt['path']['log'],
f"test_{opt['name']}_{get_time_str()}.log")
logger = get_root_logger(
logger_name='neosr', log_level=logging.INFO, log_file=log_file)
# create test dataset and dataloader
test_loaders = []
for _, dataset_opt in sorted(opt['datasets'].items()):
test_set = build_dataset(dataset_opt)
test_loader = build_dataloader(
test_set, dataset_opt, num_gpu=opt['num_gpu'], dist=opt['dist'], sampler=None, seed=opt['manual_seed'])
logger.info(
f"Number of test images in {dataset_opt['name']}: {len(test_set)}")
test_loaders.append(test_loader)
# create model
model = build_model(opt)
for test_loader in test_loaders:
test_set_name = test_loader.dataset.opt['name']
logger.info(f'Testing {test_set_name}...')
start_time = time()
model.validation(
test_loader, current_iter=opt['name'], tb_logger=None, save_img=opt['val']['save_img'])
end_time = time()
total_time = end_time - start_time
n_img = len(test_loader.dataset)
fps = n_img / total_time
logger.info(f'Inference took {total_time:.2f} seconds, at {fps:.2f} fps.')
if __name__ == '__main__':
root_path = osp.abspath(osp.join(__file__, osp.pardir))
test_pipeline(root_path)