-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy patheval.py
140 lines (118 loc) · 5.87 KB
/
eval.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 numpy as np
import argparse
from data.nuscenes_pred_split import get_nuscenes_pred_split
from data.ethucy_split import get_ethucy_split
from utils.utils import print_log, AverageMeter, isfile, print_log, AverageMeter, isfile, isfolder, find_unique_common_from_lists, load_list_from_folder, load_txt_file
""" Metrics """
def compute_ADE(pred_arr, gt_arr):
ade = 0.0
for pred, gt in zip(pred_arr, gt_arr):
diff = pred - np.expand_dims(gt, axis=0) # samples x frames x 2
dist = np.linalg.norm(diff, axis=-1) # samples x frames
dist = dist.mean(axis=-1) # samples
ade += dist.min(axis=0) # (1, )
ade /= len(pred_arr)
return ade
def compute_FDE(pred_arr, gt_arr):
fde = 0.0
for pred, gt in zip(pred_arr, gt_arr):
diff = pred - np.expand_dims(gt, axis=0) # samples x frames x 2
dist = np.linalg.norm(diff, axis=-1) # samples x frames
dist = dist[..., -1] # samples
fde += dist.min(axis=0) # (1, )
fde /= len(pred_arr)
return fde
def align_gt(pred, gt):
frame_from_data = pred[0, :, 0].astype('int64').tolist()
frame_from_gt = gt[:, 0].astype('int64').tolist()
common_frames, index_list1, index_list2 = find_unique_common_from_lists(frame_from_gt, frame_from_data)
assert len(common_frames) == len(frame_from_data)
gt_new = gt[index_list1, 2:]
pred_new = pred[:, index_list2, 2:]
return pred_new, gt_new
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', default='nuscenes_pred')
parser.add_argument('--results_dir', default=None)
parser.add_argument('--data', default='test')
parser.add_argument('--log_file', default=None)
args = parser.parse_args()
dataset = args.dataset.lower()
results_dir = args.results_dir
if dataset == 'nuscenes_pred': # nuscenes
data_root = f'datasets/nuscenes_pred'
gt_dir = f'{data_root}/label/{args.data}'
seq_train, seq_val, seq_test = get_nuscenes_pred_split(data_root)
seq_eval = globals()[f'seq_{args.data}']
else: # ETH/UCY
gt_dir = f'datasets/eth_ucy/{args.dataset}'
seq_train, seq_val, seq_test = get_ethucy_split(args.dataset)
seq_eval = globals()[f'seq_{args.data}']
if args.log_file is None:
log_file = os.path.join(results_dir, 'log_eval.txt')
else:
log_file = args.log_file
log_file = open(log_file, 'a+')
print_log('loading results from %s' % results_dir, log_file)
print_log('loading GT from %s' % gt_dir, log_file)
stats_func = {
'ADE': compute_ADE,
'FDE': compute_FDE
}
stats_meter = {x: AverageMeter() for x in stats_func.keys()}
seq_list, num_seq = load_list_from_folder(gt_dir)
print_log('\n\nnumber of sequences to evaluate is %d' % len(seq_eval), log_file)
for seq_name in seq_eval:
# load GT raw data
gt_data, _ = load_txt_file(os.path.join(gt_dir, seq_name+'.txt'))
gt_raw = []
for line_data in gt_data:
line_data = np.array([line_data.split(' ')])[:, [0, 1, 13, 15]][0].astype('float32')
if line_data[1] == -1: continue
gt_raw.append(line_data)
gt_raw = np.stack(gt_raw)
data_filelist, _ = load_list_from_folder(os.path.join(results_dir, seq_name))
for data_file in data_filelist: # each example e.g., seq_0001 - frame_000009
# for reconsutrction or deterministic
if isfile(data_file):
all_traj = np.loadtxt(data_file, delimiter=' ', dtype='float32') # (frames x agents) x 4
all_traj = np.expand_dims(all_traj, axis=0) # 1 x (frames x agents) x 4
# for stochastic with multiple samples
elif isfolder(data_file):
sample_list, _ = load_list_from_folder(data_file)
sample_all = []
for sample in sample_list:
sample = np.loadtxt(sample, delimiter=' ', dtype='float32') # (frames x agents) x 4
sample_all.append(sample)
all_traj = np.stack(sample_all, axis=0) # samples x (framex x agents) x 4
else:
assert False, 'error'
# convert raw data to our format for evaluation
id_list = np.unique(all_traj[:, :, 1])
frame_list = np.unique(all_traj[:, :, 0])
agent_traj = []
gt_traj = []
for idx in id_list:
# GT traj
gt_idx = gt_raw[gt_raw[:, 1] == idx] # frames x 4
# predicted traj
ind = np.unique(np.where(all_traj[:, :, 1] == idx)[1].tolist())
pred_idx = all_traj[:, ind, :] # sample x frames x 4
# filter data
pred_idx, gt_idx = align_gt(pred_idx, gt_idx)
# append
agent_traj.append(pred_idx)
gt_traj.append(gt_idx)
"""compute stats"""
for stats_name, meter in stats_meter.items():
func = stats_func[stats_name]
value = func(agent_traj, gt_traj)
meter.update(value, n=len(agent_traj))
stats_str = ' '.join([f'{x}: {y.val:.4f} ({y.avg:.4f})' for x, y in stats_meter.items()])
print_log(f'evaluating seq {seq_name:s}, forecasting frame {int(frame_list[0]):06d} to {int(frame_list[-1]):06d} {stats_str}', log_file)
print_log('-' * 30 + ' STATS ' + '-' * 30, log_file)
for name, meter in stats_meter.items():
print_log(f'{meter.count} {name}: {meter.avg:.4f}', log_file)
print_log('-' * 67, log_file)
log_file.close()