-
Notifications
You must be signed in to change notification settings - Fork 25
/
visualize.py
140 lines (114 loc) · 5.23 KB
/
visualize.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
import os
import sys
import time
from typing import Any, Dict, List, Tuple, Union
from datetime import datetime
import argparse
from tqdm import tqdm
import numpy as np
from importlib import import_module
#
import torch
from torch.utils.data import DataLoader
#
from loader import Loader
from utils.logger import Logger
from utils.utils import AverageMeterForDict
def parse_arguments() -> Any:
"""Arguments for running the baseline.
Returns:
parsed arguments
"""
parser = argparse.ArgumentParser()
parser.add_argument("--mode", default="val", type=str, help="Mode, train/val/test")
parser.add_argument("--features_dir", required=True, default="", type=str, help="Path to the dataset")
parser.add_argument("--use_cuda", action="store_true", help="Use CUDA for acceleration")
parser.add_argument("--data_aug", action="store_true", help="Enable data augmentation")
parser.add_argument("--adv_cfg_path", required=True, default="", type=str)
parser.add_argument("--model_path", required=False, type=str, help="path to the saved model")
#
parser.add_argument("--seq_id", default=-1, type=int, help="Selected sequence ID")
parser.add_argument("--shuffle", action="store_true", help="Shuffle order")
parser.add_argument("--visualizer", default="", type=str, help="Type of visualizer")
parser.add_argument("--show_conditioned", action="store_true", help="Show missed sample only")
return parser.parse_args()
def main():
args = parse_arguments()
print('Args: {}\n'.format(args))
if args.use_cuda and torch.cuda.is_available():
device = torch.device("cuda", 0)
else:
device = torch.device('cpu')
date_str = datetime.now().strftime("%Y%m%d-%H%M%S")
vis_file, vis_name = args.visualizer.split(':')
print('[Loader] load visualizer {} from {}'.format(vis_name, vis_file))
vis = getattr(import_module(vis_file), vis_name)()
if args.mode != 'test':
loader = Loader(args, device, is_ddp=False)
print('[Resume] Loading state_dict from {}'.format(args.model_path))
loader.set_resmue(args.model_path)
(train_set, val_set), net, _, _, evaluator = loader.load()
net.eval()
if args.mode == 'train':
dataloader = DataLoader(train_set,
batch_size=1,
shuffle=args.shuffle,
num_workers=0,
collate_fn=train_set.collate_fn,
drop_last=False)
elif args.mode == 'val':
dataloader = DataLoader(val_set,
batch_size=1,
shuffle=args.shuffle,
num_workers=0,
collate_fn=val_set.collate_fn,
drop_last=False)
with torch.no_grad():
for i, data in enumerate(tqdm(dataloader)):
if args.seq_id == -1:
data_in = net.pre_process(data)
out = net(data_in)
post_out = net.post_process(out)
torch.cuda.synchronize()
eval_out = evaluator.evaluate(post_out, data)
if args.show_conditioned:
if eval_out['mr_k'] == 0.0:
continue
print(f'\n\nSequence ID: {data["SEQ_ID"][0]}')
print(f'Evaluation Metrics:')
for name, val in eval_out.items():
print('-- {}: {:.4}'.format(name, val))
vis.draw_once(post_out, data, eval_out, show_map=True, split=args.mode)
else:
if args.seq_id == data['SEQ_ID'][0]:
data_in = net.pre_process(data)
out = net(data_in)
post_out = net.post_process(out)
eval_out = evaluator.evaluate(post_out, data)
print(f'\n\nSequence ID: {data["SEQ_ID"][0]}')
print(f'Evaluation Metrics:')
for name, val in eval_out.items():
print('-- {}: {:.4}'.format(name, val))
vis.draw_once(post_out, data, eval_out, show_map=True, split=args.mode)
break
else:
# test
loader = Loader(args, device, is_ddp=False)
print('[Resume] Loading state_dict from {}'.format(args.model_path))
loader.set_resmue(args.model_path)
test_set, net, _, _, _ = loader.load()
net.eval()
dl_test = DataLoader(test_set,
batch_size=1,
num_workers=0,
shuffle=False,
collate_fn=test_set.collate_fn)
with torch.no_grad():
for i, data in enumerate(tqdm(dl_test)):
data_in = net.pre_process(data)
out = net(data_in)
post_out = net.post_process(out)
vis.draw_once(post_out, data, {}, show_map=True, test_mode=True, split=args.mode)
print('\nExit...')
if __name__ == "__main__":
main()