-
Notifications
You must be signed in to change notification settings - Fork 45
/
infer.py
53 lines (45 loc) · 2.03 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
import argparse
import logging
from pathlib import Path
import torch.backends.cudnn
import yaml
import scripts
from config import from_dict
if __name__ == '__main__':
# args parser
parser = argparse.ArgumentParser()
parser.add_argument('--cfg', default='config/default.yaml', help='config file path')
parser.add_argument('--save_dir', default='runs/tmp', help='fusion result save folder')
args = parser.parse_args()
# init config
config = yaml.safe_load(Path(args.cfg).open('r'))
config = from_dict(config) # convert dict to object
config = config
# init logger
log_f = '%(asctime)s | %(filename)s[line:%(lineno)d] | %(levelname)s | %(message)s'
logging.basicConfig(level=config.debug.log, format=log_f)
# init device & anomaly detector
torch.backends.cudnn.benchmark = True
torch.autograd.set_detect_anomaly(True)
# choose inference script
logging.info(f'enter {config.strategy} inference mode')
match config.strategy:
case 'fuse':
infer_p = getattr(scripts, 'InferF')
# check pretrained weights
if config.fuse.pretrained is None:
logging.warning('no pretrained weights specified, use official pretrained weights')
config.fuse.pretrained = 'https://github.com/JinyuanLiu-CV/TarDAL/releases/download/v1.0.0/tardal-dt.pth'
case 'fuse & detect':
infer_p = getattr(scripts, 'InferFD')
# check pretrained weights
if config.fuse.pretrained is None:
logging.warning('no pretrained weights specified, use official pretrained weights')
config.fuse.pretrained = 'https://github.com/JinyuanLiu-CV/TarDAL/releases/download/v1.0.0/tardal-ct.pth'
case 'detect':
raise NotImplementedError('detect mode is useless during inference period, please use fuse & detect mode')
case _:
raise ValueError(f'unknown strategy: {config.strategy}')
# create script instance
infer = infer_p(config, args.save_dir)
infer.run()