-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate.py
266 lines (233 loc) · 9.21 KB
/
evaluate.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import torch
from torch.utils.data import random_split
from torchsummary import summary
import argparse
import numpy as np
import random
from codebase.models.BBBTimeSeriesPredModel import BBBTimeSeriesPredModel
from codebase.models.BBBTimeSeriesPredModel_FF import BBBTimeSeriesPredModel_FF
from codebase.train import train
import codebase.utils as ut
import data.data_utils as data_ut
from tqdm import tqdm
### RWSE Functions
def rwse(model, full_true_trajs, n_samples=100):
""" root-weighted square error (RWSE) captures
the deviation of a model’s probability
mass from real-world trajectories
"""
inputs = full_true_trajs[:model.n_input_steps, :, :].detach()
targets = full_true_trajs[model.n_input_steps:, :, :2].detach()
# tile based on number of samples
inputs = inputs.repeat(1,n_samples,1)
targets = targets.repeat(1,n_samples,1)
if model.BBB and model.rnn_cell_type=="LSTM":
wse = wse_bbb_rnn(model, inputs, targets)
elif not model.BBB and model.rnn_cell_type=="LSTM":
wse = wse_rnn(model, inputs, targets)
elif model.BBB and model.rnn_cell_type=="FF":
wse = wse_bbb_ff(model, inputs, targets)
elif not model.BBB and model.rnn_cell_type=="FF":
wse = wse_ff(model, inputs, targets)
else:
raise Exception('Incorrect model specified')
rwse = wse.mean().sqrt()
return rwse
def wse_bbb_rnn(model, inputs, targets):
pred = model.forward(inputs).detach()
if not model.constant_var:
pred = pred[:, :, :-1]
return ((targets - pred) ** 2).sum(-1).sum(0)
def wse_rnn(model, inputs, targets):
pred = model.forward(inputs).detach()
if not model.constant_var:
mean, var = ut.gaussian_parameters(pred, dim=-1)
else:
mean = pred
var = model.pred_var
sample_trajs = ut.sample_gaussian(mean, var)
return ((targets - sample_trajs) ** 2).sum(-1).sum(0)
def wse_bbb_ff(model,inputs,targets):
raise Exception('Yet to formulate bbb ff')
def wse_ff(model, inputs, targets):
pred = model.forward(inputs).detach()
if not model.constant_var:
mean, var = ut.gaussian_parameters_ff(pred, dim=0)
else:
mean = pred
var = model.pred_var
sample_trajs = ut.sample_gaussian(mean, var)
return ((targets - sample_trajs) ** 2).sum(-1).sum(0)
### RMSE Functions
def rmse(model, full_true_trajs, n_samples=100):
""" root-mean square error (RMSE) captures
the deviation of a model’s expected trajectory from
mass from real-world trajectories
"""
inputs = full_true_trajs[:model.n_input_steps, :, :].detach()
targets = full_true_trajs[model.n_input_steps:, :, :2].detach()
if model.BBB and model.rnn_cell_type=="LSTM":
mse = mse_bbb_rnn(model, inputs, targets, n_samples)
elif not model.BBB and model.rnn_cell_type=="LSTM":
mse = mse_rnn(model, inputs, targets, n_samples)
elif model.BBB and model.rnn_cell_type=="FF":
mse = mse_bbb_ff(model, inputs, targets, n_samples)
elif not model.BBB and model.rnn_cell_type=="FF":
mse = mse_ff(model, inputs, targets, n_samples)
else:
raise Exception('Incorrect model specified')
rmse = mse.mean().sqrt()
return rmse
def mse_bbb_rnn(model, inputs, targets, n_samples): #FIXME
sample_tensor = torch.zeros(*targets.shape,n_samples)
for i in range(n_samples):
pred = model.forward(inputs).detach()
if not model.constant_var:
pred = pred[:, :, :-1]
sample_tensor[:,:,:,i] = pred
mean_pred = sample_tensor.mean(3)
return ((targets - mean_pred) ** 2).sum(-1).sum(0)
def mse_rnn(model, inputs, targets, n_samples):
pred = model.forward(inputs).detach()
if not model.constant_var:
mean, var = ut.gaussian_parameters(pred, dim=-1)
else:
mean = pred
var = model.pred_var
return ((targets - mean) ** 2).sum(-1).sum(0)
def mse_bbb_ff(model, inputs, targets, n_samples):
raise Exception('Yet to formulate bbb ff')
def mse_ff(model, inputs, targets, n_samples):
pred = model.forward(inputs).detach()
if not model.constant_var:
mean, var = ut.gaussian_parameters_ff(pred, dim=0)
else:
mean = pred
var = model.pred_var
return ((targets - mean) ** 2).sum(-1).sum(0)
### Run on Execution ::
parser = argparse.ArgumentParser()
# Data
parser.add_argument('--dataset_name', type=str, default='highd')
parser.add_argument('--batch_size', type=int, default=30)
parser.add_argument('--n_input_steps', type=int, default=50)
parser.add_argument('--n_pred_steps', type=int, default=20)
parser.add_argument('--input_feat_dim', type=int, default=4)
parser.add_argument('--pred_feat_dim', type=int, default=2)
# Network
parser.add_argument('--hidden_feat_dim', type=int, default=100)
# Model
parser.add_argument('--cell', type=str, default='LSTM')
parser.add_argument('--constant_var', type=int, default=0)
parser.add_argument('--BBB', type=int, default=0)
parser.add_argument('--sharpen', type=int, default=0)
parser.add_argument('--likelihood_cost_form', type=str, default='gaussian')
parser.add_argument('--nlayers', type=int, default=1)
parser.add_argument('--dropout', type=float, default=0)
parser.add_argument('--pi', type=float, default=0.25)
parser.add_argument('--logstd1', type=int, default=-1)
parser.add_argument('--logstd2', type=int, default=-6)
# Train
parser.add_argument('--clip_grad', type=int, default=5)
parser.add_argument('--run', type=int, default=1)
parser.add_argument('--training', action='store_true', default=False)
args = parser.parse_args()
std1 = np.exp(args.logstd1)
std2 = np.exp(args.logstd2)
# # automatic
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
gpu = False if device == torch.device('cpu') else True
# Enforced settings:
if not args.BBB:
args.sharpen = False
layout = [
('model={:s}', args.cell),
('BBB={}', bool(args.BBB)),
('data={:s}', args.dataset_name),
('nlayers={:d}', args.nlayers),
('nhid={:d}', args.hidden_feat_dim),
('const_var={}', bool(args.constant_var)),
('dropout={:.1f}', args.dropout),
('clipgrad={}', str(args.clip_grad)),
('loss={:s}', args.likelihood_cost_form),
('run={:d}', args.run),
]
else:
layout = [
('model={}', args.cell),
('BBB={}', bool(args.BBB)),
('data={:s}', args.dataset_name),
('nlayers={:d}', args.nlayers),
('nhid={:d}', args.hidden_feat_dim),
('const_var={}', bool(args.constant_var)),
('dropout={:.1f}', args.dropout),
('clipgrad={}', str(args.clip_grad)),
('loss={:s}', args.likelihood_cost_form),
('sharpen={}', bool(args.sharpen)),
('pi={:.2f}', args.pi),
('logstd1={:d}', args.logstd1),
('logstd2={:d}', args.logstd2),
('run={:d}', args.run),
]
model_name = '_'.join([t.format(v) for (t, v) in layout])
if args.cell == 'LSTM':
model = BBBTimeSeriesPredModel(
num_rnn_layers=args.nlayers,
pi=args.pi,
std1=std1,
std2=std2,
gpu=gpu,
BBB=bool(args.BBB),
training=args.training,
sharpen=bool(args.sharpen),
dropout=args.dropout,
likelihood_cost_form=args.likelihood_cost_form,
input_feat_dim=args.input_feat_dim,
pred_feat_dim=args.pred_feat_dim,
hidden_feat_dim=args.hidden_feat_dim,
n_input_steps=args.n_input_steps,
n_pred_steps=args.n_pred_steps,
constant_var=bool(args.constant_var),
rnn_cell_type=args.cell,
name=model_name,
device=device).to(device)
elif args.cell == 'FF':
model = BBBTimeSeriesPredModel_FF(
num_hidden_layers=args.nlayers,
pi=args.pi,
std1=std1,
std2=std2,
gpu=gpu,
BBB=bool(args.BBB),
training=args.training,
sharpen=bool(args.sharpen),
dropout=args.dropout,
likelihood_cost_form=args.likelihood_cost_form,
input_feat_dim=args.input_feat_dim,
pred_feat_dim=args.pred_feat_dim,
hidden_feat_dim=args.hidden_feat_dim,
n_input_steps=args.n_input_steps,
n_pred_steps=args.n_pred_steps,
constant_var=bool(args.constant_var),
name=model_name,
device=device).to(device)
ut.load_final_model_by_name(model)
model.eval()
# read training set
training_set = data_ut.read_highd_data(
'highd_processed_tracks01-60_fr05_loc123456_p0.30',
args.batch_size, device)
# pick arbitrary test with some of trajectories
np.random.seed(0)
n_batches = len(training_set)
split = 0.05
ind = np.random.choice(range(n_batches), size=(int(n_batches * split),), replace=False)
test_set_batches = [training_set[i] for i in ind]
rwses = []
rmses = []
for test_set in tqdm(test_set_batches):
# calculate metrics and return results
rwses.append(rwse(model, test_set, n_samples=100).detach().item())
rmses.append(rmse(model, test_set, n_samples=100).detach().item())
print("RWSE: {:.3f} +/ {:.3f}".format(np.array(rwses).mean(), np.array(rwses).std()))
print("RMSE: {:.3f} +/ {:.3f}".format(np.array(rmses).mean(), np.array(rmses).std()))