-
Notifications
You must be signed in to change notification settings - Fork 9
/
benchmark.py
141 lines (114 loc) · 5.73 KB
/
benchmark.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
141
import os
import glob
import pandas as pd
pd.options.display.float_format = '{:.3f}'.format
import argparse
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--dataset",
type=str,
default=None)
parser.add_argument("--weight",
type=float,
default=None)
parser.add_argument("--epoch_min",
type=int,
default=100)
parser.add_argument("--epoch_max",
type=int,
default=300)
parser.add_argument("--window_best",
type=int,
default=5)
args = parser.parse_args()
return args
def load_dataframe(fname, ckpt):
colnames=['Epoch', 'Seed', 'ADE', 'FDE', 'COL']
df = pd.read_csv(fname, header=None, names=colnames, usecols=[0,1,2,3,4])
df = df.drop_duplicates(subset=['Epoch', 'Seed'], keep='last')
df = df[df['Epoch'].isin(ckpt)]
# seed_list = [123]
# df = df[df['Seed'].isin(seed_list)]
df.reset_index(drop=True, inplace=True)
df = df.astype({"Epoch": int, "Seed": int})
return df
def relative(df_baseline, df_candidate):
gain_ade = (df_baseline['ADE'] - df_candidate['ADE']) / (df_baseline['ADE'] + 1e-8)
gain_fde = (df_baseline['FDE'] - df_candidate['FDE']) / (df_baseline['FDE'] + 1e-8)
gain_col = (df_baseline['COL'] - df_candidate['COL']) / (df_baseline['COL'] + 1e-8)
return gain_ade, gain_fde, gain_col
def select(df, window):
index_best = (df['ADE'] + df['FDE'] + df['COL']).argmin()
# local window
index_min = index_best - (window - 1) // 2
index_max = index_min + window
if index_min < 0:
index_min = 0
index_max = window
if index_max > df.shape[0] - 1:
index_max = df.shape[0]
index_min = index_max - window
return index_best, (index_min, index_max)
def compare(foldername, args):
epoch_list = [epoch for epoch in range(args.epoch_min, args.epoch_max + 10, 10)]
filevanilla = glob.glob(os.path.join(foldername, '*_0.0000.csv'))[0]
df_vanilla = load_dataframe(filevanilla, epoch_list)
if args.weight:
filename = glob.glob(os.path.join(foldername, '*_{:.4f}.csv'.format(args.weight)))[0]
df_snce = load_dataframe(filename, epoch_list)
print('\n----- Detailed Summary -----\n')
print('Vanilla:\n', df_vanilla)
print('')
print('SNCE:\n', df_snce)
print('\n----- Avg of {:d} Seeds -----\n'.format(len(df_snce['Seed'].unique())))
print('Vanilla:')
print(df_vanilla.mean())
print('')
print('SNCE:')
print(df_snce.mean())
else:
flist = glob.glob(os.path.join(foldername, '*.csv'))
flist.sort(key=lambda filename: float(filename.split('_')[-1][:-4]))
flist.remove(filevanilla)
print(" \t \t \tAverage \t \t Gain \t \t \t \t Best \t \t \t Gain ")
print(" \t \t ---------------------\t --------------------- \t \t -----------------------------\t ---------------------")
print("Method \tWeight\t ADE \t FDE \t COL \t ADE \t FDE \t COL \t \t Epoch \t ADE \t FDE \t COL \t ADE \t FDE \t COL ")
idx_best_vanilla, window_best_vanilla = select(df_vanilla, args.window_best)
best_vanilla = df_vanilla.iloc[idx_best_vanilla, :]
avg_vanilla = df_vanilla.iloc[window_best_vanilla[0]:window_best_vanilla[1], :].mean()
print("Vanilla\t 0.00 \t {:.3f} \t {:.3f} \t {:.3f} \t x \t x \t x \t \t {:.0f} \t {:.3f} \t {:.3f} \t {:.3f} \t x \t x \t x".format(avg_vanilla['ADE'], avg_vanilla['FDE'], avg_vanilla['COL'] * 100, best_vanilla['Epoch'], best_vanilla['ADE'], best_vanilla['FDE'], best_vanilla['COL'] * 100))
metric_vanilla = (avg_vanilla['ADE'], avg_vanilla['FDE'], avg_vanilla['COL'])
metric_snce = (float('inf'), float('inf'), float('inf')) # FDE & COL
weight_snce = float('nan')
for filename in flist:
weight = float(filename.split('_')[-1][:-4])
df_snce = load_dataframe(filename, epoch_list)
idx_best_snce, window_best_snce = select(df_snce, args.window_best)
best_snce = df_snce.iloc[idx_best_snce, :]
avg_snce = df_snce.iloc[window_best_snce[0]:window_best_snce[1], :].mean()
gain_best_ade, gain_best_fde, gain_best_col = relative(best_vanilla, best_snce)
gain_avg_ade, gain_avg_fde, gain_avg_col = relative(avg_vanilla, avg_snce)
print("S-NCE \t {:.3f}\t {:.3f} \t {:.3f} \t {:.3f} \t {:.1f}%\t {:.1f}%\t {:.1f}% \t {:.0f} \t {:.3f} \t {:.3f} \t {:.3f} \t {:.1f}%\t {:.1f}%\t {:.1f}%".format(weight, avg_snce['ADE'], avg_snce['FDE'], avg_snce['COL'] * 100, gain_avg_ade * 100, gain_avg_fde * 100, gain_avg_col * 100, best_snce['Epoch'], best_snce['ADE'], best_snce['FDE'], best_snce['COL'] * 100, gain_best_ade * 100, gain_best_fde * 100, gain_best_col * 100))
if metric_snce[0] + metric_snce[1] + metric_snce[2] * 100 > avg_snce['ADE'] + avg_snce['FDE'] + avg_snce['COL'] * 100:
metric_snce = (avg_snce['ADE'], avg_snce['FDE'], avg_snce['COL'])
weight_snce = weight
print('Optimal Weight:', weight_snce, '\n')
return metric_vanilla, metric_snce
def main():
args = parse_args()
if args.dataset is None:
result = list()
for dataset in ['eth', 'hotel', 'univ', 'zara1', 'zara2']:
print("Dataset:", dataset)
foldername = 'experiments/pedestrians/models/snce_' + dataset + '_vel'
(ade_vanilla, fde_vanilla, col_vanilla), (ade_snce, fde_snce, col_snce) = compare(foldername, args)
result.append([dataset, ade_vanilla, fde_vanilla, col_vanilla * 100, ade_snce, fde_snce, col_snce * 100, (1 - col_snce / col_vanilla) * 100 ])
df = pd.DataFrame(result, columns=['Dataset', 'ADE-Vanilla', 'FDE-Vanilla', 'COL-Vanilla', 'ADE-SNCE', 'FDE-SNCE', 'COL-SNCE', 'COL-Gain']).set_index('Dataset')
df.loc['Avg'] = df.mean()
print(df)
else:
print("Dataset:", args.dataset)
foldername = 'experiments/pedestrians/models/snce_' + args.dataset + '_vel'
compare(foldername, args)
if __name__ == "__main__":
main()