-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
65 lines (52 loc) · 2.27 KB
/
test.py
File metadata and controls
65 lines (52 loc) · 2.27 KB
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
import os
from easydict import EasyDict
from pathlib import Path
import json
import argparse
import torch
from core.model import PoseEstimator
from core.dataset import build_dataloader
from utils.utils import setup_logger, load_config
from utils.train_utils import load_checkpoint
from utils.eval_utils import evaluate
# evaluate model
def main(cfg_path, out_path, data_path=None, ckpt=None):
yaml_config = load_config(cfg_path)
config = EasyDict(yaml_config)
result_path = out_path / config.experiment_name
if not os.path.exists(result_path):
os.makedirs(result_path, exist_ok=True)
logger = setup_logger()
data_path = Path(config.dataset.data_path) if data_path is None else Path(data_path)
test_loader = build_dataloader(config=config, mode='test', data_path=data_path,
logger=logger, batchsize=32)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = PoseEstimator(in_channels=3, cfg=config.model, device=device).to(device)
# print('Model architecture:')
# print(model)
ckpt = ckpt if ckpt is not None else config.ckpt
checkpoint = load_checkpoint(ckpt)
model.load_state_dict(checkpoint['state_dict'])
logger.info(f'Start evaluate model: {config.model_name}')
mt_dict, _ = evaluate(model, test_loader, device, config, logger)
print(mt_dict)
if config.dataset.get('filter', None) is not None:
filter = config.dataset.filter
eval_name = f'{filter["name"]}-{filter["range"][0]}'
else:
eval_name = ''
with open(result_path / f'evaluation-{config.dataset.type}-{eval_name}.json', 'w') as f:
json.dump(mt_dict, f)
logger.info(f'Evaluation finished {config.model_name}')
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--cfg_path', type=str, default='cfg/eval.yaml')
parser.add_argument('--out_path', type=str, default='output')
parser.add_argument('--data_path', type=str, default=None)
parser.add_argument('--ckpt', type=str, default=None)
args = parser.parse_args()
out_path = Path(args.out_path)
data_path = args.data_path
ckpt = args.ckpt
cfg_path = args.cfg_path
main(cfg_path=cfg_path, out_path=out_path, data_path=data_path, ckpt=ckpt)