-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathevaluate_model.py
192 lines (161 loc) · 5.75 KB
/
evaluate_model.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import argparse
import os
import torch
from data.loader import data_loader
from models import TrajectoryGenerator
from utils import (
displacement_error,
final_displacement_error,
l2_loss,
int_tuple,
relative_to_abs,
get_dset_path,
)
import time
parser = argparse.ArgumentParser()
parser.add_argument("--log_dir", default="./", help="Directory containing logging file")
parser.add_argument("--dataset_name", default="zara2", type=str)
parser.add_argument("--delim", default="\t")
parser.add_argument("--loader_num_workers", default=4, type=int)
parser.add_argument("--obs_len", default=8, type=int)
parser.add_argument("--pred_len", default=12, type=int)
parser.add_argument("--skip", default=1, type=int)
parser.add_argument("--seed", type=int, default=72, help="Random seed.")
parser.add_argument("--batch_size", default=64, type=int)
parser.add_argument("--noise_dim", default=(16,), type=int_tuple)
parser.add_argument("--noise_type", default="gaussian")
parser.add_argument("--noise_mix_type", default="global")
parser.add_argument(
"--traj_lstm_input_size", type=int, default=2, help="traj_lstm_input_size"
)
parser.add_argument("--traj_lstm_hidden_size", default=32, type=int)
parser.add_argument(
"--heads", type=str, default="4,1", help="Heads in each layer, splitted with comma"
)
parser.add_argument(
"--hidden-units",
type=str,
default="16",
help="Hidden units in each hidden layer, splitted with comma",
)
parser.add_argument(
"--graph_network_out_dims",
type=int,
default=32,
help="dims of every node after through GAT module",
)
parser.add_argument("--graph_lstm_hidden_size", default=32, type=int)
parser.add_argument("--num_samples", default=20, type=int)
parser.add_argument(
"--dropout", type=float, default=0, help="Dropout rate (1 - keep probability)."
)
parser.add_argument(
"--alpha", type=float, default=0.2, help="Alpha for the leaky_relu."
)
parser.add_argument("--dset_type", default="test", type=str)
parser.add_argument(
"--resume",
default="./model_best.pth.tar",
type=str,
metavar="PATH",
help="path to latest checkpoint (default: none)",
)
parser.add_argument("--gpu_num", default="0", type=str)
def evaluate_helper(error, seq_start_end):
sum_ = 0
error = torch.stack(error, dim=1)
for (start, end) in seq_start_end:
start = start.item()
end = end.item()
_error = error[start:end]
_error = torch.sum(_error, dim=0)
_error = torch.min(_error)
sum_ += _error
return sum_
def get_generator(checkpoint):
n_units = (
[args.traj_lstm_hidden_size]
+ [int(x) for x in args.hidden_units.strip().split(",")]
+ [args.graph_lstm_hidden_size]
)
n_heads = [int(x) for x in args.heads.strip().split(",")]
model = TrajectoryGenerator(
obs_len=args.obs_len,
pred_len=args.pred_len,
traj_lstm_input_size=args.traj_lstm_input_size,
traj_lstm_hidden_size=args.traj_lstm_hidden_size,
n_units=n_units,
n_heads=n_heads,
graph_network_out_dims=args.graph_network_out_dims,
dropout=args.dropout,
alpha=args.alpha,
graph_lstm_hidden_size=args.graph_lstm_hidden_size,
noise_dim=args.noise_dim,
noise_type=args.noise_type,
)
model.load_state_dict(checkpoint["state_dict"])
model.cuda()
model.eval()
return model
def cal_ade_fde(pred_traj_gt, pred_traj_fake):
ade = displacement_error(pred_traj_fake, pred_traj_gt, mode="raw")
fde = final_displacement_error(pred_traj_fake[-1], pred_traj_gt[-1], mode="raw")
return ade, fde
def evaluate(args, loader, generator):
ade_outer, fde_outer = [], []
total_traj = 0
alltime = 0
step = 0
with torch.no_grad():
for batch in loader:
batch = [tensor.cuda() for tensor in batch]
(
obs_traj,
pred_traj_gt,
obs_traj_rel,
pred_traj_gt_rel,
non_linear_ped,
loss_mask,
seq_start_end,
) = batch
step += seq_start_end.shape[0]
start = time.time()
ade, fde = [], []
total_traj += pred_traj_gt.size(1)
for _ in range(args.num_samples):
pred_traj_fake_rel = generator(
obs_traj_rel, obs_traj, seq_start_end, 0, 3
)
pred_traj_fake_rel = pred_traj_fake_rel[-args.pred_len :]
pred_traj_fake = relative_to_abs(pred_traj_fake_rel, obs_traj[-1])
ade_, fde_ = cal_ade_fde(pred_traj_gt, pred_traj_fake)
ade.append(ade_)
fde.append(fde_)
ade_sum = evaluate_helper(ade, seq_start_end)
fde_sum = evaluate_helper(fde, seq_start_end)
ade_outer.append(ade_sum)
fde_outer.append(fde_sum)
elapsed = (time.time() - start)
alltime += elapsed
# print("avg time: ", alltime, step, total_traj)
ade = sum(ade_outer) / (total_traj * args.pred_len)
fde = sum(fde_outer) / (total_traj)
return ade, fde
def main(args):
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_num
checkpoint = torch.load(args.resume)
generator = get_generator(checkpoint)
path = get_dset_path(args.dataset_name, args.dset_type)
_, loader = data_loader(args, path)
ade, fde = evaluate(args, loader, generator)
print(
"Dataset: {}, Pred Len: {}, ADE: {:.12f}, FDE: {:.12f}".format(
args.dataset_name, args.pred_len, ade, fde
)
)
if __name__ == "__main__":
args = parser.parse_args()
torch.manual_seed(72)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
main(args)