-
Notifications
You must be signed in to change notification settings - Fork 2
/
parameters.py
369 lines (316 loc) · 10.9 KB
/
parameters.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import json
import os
class hyper_parameters(object):
def __init__(self, input_time_step=20, output_time_step=50,
data_dir='data/',dataset='vehicle_ngsim',model_type='rnn',
num_class=3,coordinate_dim=2,inp_feat=('traj', 'speed')): #32
self.input_time_step = input_time_step
self.output_time_step = output_time_step
self.coordinate_dim = coordinate_dim
self.inten_num_class = num_class
self.inp_feat = inp_feat
self.out_feat = ('speed',)
self.encoder_feat_dim = self.coordinate_dim * len(self.inp_feat) # x, v
self.decoder_feat_dim = self.coordinate_dim * 1 # v
self.data_dir = data_dir
self.dataset = dataset
self.model_type = model_type
self.params_dict = {}
def _set_default_dataset_params(self):
if self.dataset=='human_kinect':
self.inp_feat = ('traj', 'speed')
self.input_time_step = 20
self.output_time_step = 10
self.inten_num_class = 12
self.coordinate_dim = 3
self.encoder_feat_dim = self.coordinate_dim * 2 # x,v
self.decoder_feat_dim =self.coordinate_dim
if self.dataset=='human_mocap':
self.inp_feat = ('traj', 'speed')
self.input_time_step = 20
self.output_time_step = 10
self.inten_num_class = 3
self.coordinate_dim = 3
self.encoder_feat_dim = self.coordinate_dim * 2
self.decoder_feat_dim =self.coordinate_dim
if self.dataset=='vehicle_holomatic':
self.inp_feat = ('feature', 'speed')
self.input_time_step = 20
self.output_time_step = 50
self.inten_num_class = 5
self.coordinate_dim = 2
self.traj_feature_dim = 8
self.encoder_feat_dim =self.coordinate_dim + self.traj_feature_dim
self.decoder_feat_dim = self.coordinate_dim
if self.dataset=='vehicle_ngsim':
self.inp_feat = ('feature', 'speed')
self.input_time_step = 20
self.output_time_step = 50
self.inten_num_class = 3
self.coordinate_dim = 2
self.traj_feature_dim = 4
self.encoder_feat_dim =self.coordinate_dim + self.traj_feature_dim
self.decoder_feat_dim = self.coordinate_dim
def train_param(self, param_dict=None):
default_train_params = dict(
dataset=self.dataset,
data_path=self.data_dir + self.dataset + '.pkl',
save_dir='output/'+self.dataset+'/'+self.model_type +'/',
init_model=None,
normalize_data=True,
input_time_step=self.input_time_step,
output_time_step=self.output_time_step,
inp_feat = self.inp_feat,
traj_intent_loss_ratio=[1, 0.0], #TODO: originally [1, 0.1], traj loss : intent loss
lr=0.01,
lr_schedule='multistep', # multistep
lr_decay_epochs=[7, 14],
lr_decay=0.1,
epochs=20,
batch_size=128,
coordinate_dim=self.coordinate_dim,
encoder=self.model_type,
encoder_feat_dim=self.encoder_feat_dim,
decoder=self.model_type,
decoder_feat_dim = self.decoder_feat_dim,
class_num=self.inten_num_class,
pool_type='linear_attn',
label_smooth=0.1,
traj_attn_intent_dim=64,
)
if param_dict is None and 'train_param' in self.params_dict:
param_dict = self.params_dict['train_param']
params = self._overwrite_params(default_train_params, param_dict)
params['log_dir'] = params['save_dir'] + 'log/'
dir_split = params['log_dir'].replace('\\','/').split('/')
base_dir=''
for _path in dir_split:
base_dir =os.path.join(base_dir,_path)
if not os.path.exists(base_dir):
os.mkdir(base_dir)
if params['encoder'] == 'fc':
params['pool_type'] = 'none'
return params
def encode_rnn_param(self, param_dict=None):
default_rnn_params = dict(
cell_type='gru',
feat_dim=self.encoder_feat_dim,
max_seq_len=self.input_time_step,
hidden_size=64,
num_layers=1,
dropout_fc=0.,
dropout_rnn=0.,
bidirectional=False,
)
if param_dict is None and 'encode_rnn_param' in self.params_dict:
param_dict = self.params_dict['encode_rnn_param']
param = self._overwrite_params(default_rnn_params, param_dict)
return param
def encode_fc_param(self, param_dict=None):
default_fc_params = dict(
feat_dim=self.encoder_feat_dim,
max_seq_len=self.input_time_step,
hidden_dims=[64,64],#[128,128],#[128,128,64]
dropout=0.,
)
if param_dict is None and 'encode_fc_param' in self.params_dict:
param_dict = self.params_dict['encode_fc_param']
param = self._overwrite_params(default_fc_params, param_dict)
return param
def decode_rnn_param(self, param_dict=None):
default_rnn_params = dict(
cell_type='gru',
feat_dim = self.decoder_feat_dim,
max_seq_len=self.output_time_step,
hidden_size=64,
num_layers=1,
dropout_fc=0.,
dropout_rnn=0.,
attention=True,
)
if param_dict is None and 'decode_rnn_param' in self.params_dict:
param_dict = self.params_dict['decode_rnn_param']
param = self._overwrite_params(default_rnn_params, param_dict)
return param
def decode_fc_param(self, param_dict=None):
default_fc_params = dict(
feat_dim=self.decoder_feat_dim,
max_seq_len=self.output_time_step,
hidden_dims=[64,64],
dropout=0.,
)
if param_dict is None and 'decode_fc_param' in self.params_dict:
param_dict = self.params_dict['decode_fc_param']
param = self._overwrite_params(default_fc_params, param_dict)
return param
def classifier_fc_param(self, param_dict=None):
default_fc_params = dict(
hidden_dims=[64],
dropout=0.,
num_class=self.inten_num_class,
)
if param_dict is None and 'classifier_fc_param' in self.params_dict:
param_dict = self.params_dict['classifier_fc_param']
param = self._overwrite_params(default_fc_params, param_dict)
return param
def print_params(self):
print('train parameters:')
t_param = self.train_param()
print(t_param)
print('encode_param:')
encode_param = self.encode_fc_param() if t_param['encoder'] == 'fc' else self.encode_rnn_param()
print(encode_param)
print('decode_param:')
decode_param = self.decode_fc_param() if t_param['decoder'] == 'fc' else self.decode_rnn_param()
print(decode_param)
print('classifier_fc_param:')
print(self.classifier_fc_param())
def _overwrite_params(self, old_param, new_param):
if new_param is None:
return old_param
for k, v in new_param.items():
old_param[k] = v
return old_param
def _save_parameters(self, log_dir=None):
params_dict = {}
params_dict['train_param'] = self.train_param()
params_dict['encode_rnn_param'] = self.encode_rnn_param()
params_dict['encode_fc_param'] = self.encode_fc_param()
params_dict['decode_rnn_param'] = self.decode_rnn_param()
params_dict['decode_fc_param'] = self.decode_fc_param()
params_dict['classifier_fc_param'] = self.classifier_fc_param()
if log_dir is None:
log_dir = params_dict['train_param']['log_dir']
with open(log_dir + 'hyper_parameters.json', 'w') as f:
json.dump(params_dict, f)
def _save_overwrite_parameters(self, params_key, params_value, log_dir=None):
params_dict = {}
params_dict['train_param'] = self.train_param()
params_dict['encode_rnn_param'] = self.encode_rnn_param()
params_dict['encode_fc_param'] = self.encode_fc_param()
params_dict['decode_rnn_param'] = self.decode_rnn_param()
params_dict['decode_fc_param'] = self.decode_fc_param()
params_dict['classifier_fc_param'] = self.classifier_fc_param()
params_dict[params_key] = params_value
if log_dir is None:
log_dir = params_dict['train_param']['log_dir']
with open(log_dir + 'hyper_parameters.json', 'w') as f:
json.dump(params_dict, f)
def _load_parameters(self, log_dir=None):
if log_dir is None:
log_dir = self.train_param()['log_dir']
with open(log_dir + 'hyper_parameters.json', 'r') as f:
self.params_dict = json.load(f)
class adapt_hyper_parameters(object):
def __init__(self, adaptor='none',adapt_step=1,log_dir=None):
self.adaptor = adaptor
self.adapt_step = adapt_step
self.log_dir = log_dir
self.params_dict = {}
adaptor=adaptor.lower()
if adaptor=='nrls' or adaptor=='mekf' or adaptor=='mekf_ma':
self.adapt_param=self.mekf_param
elif adaptor=='sgd':
self.adapt_param=self.sgd_param
elif adaptor=='adam':
self.adapt_param=self.adam_param
elif adaptor=='lbfgs':
self.adapt_param=self.lbfgs_param
def strategy_param(self,param_dict=None):
default_params = dict(
adapt_step=self.adapt_step,
use_multi_epoch=True,
multiepoch_thresh=(-1, -1),
)
if param_dict is None and 'strategy_param' in self.params_dict:
param_dict = self.params_dict['strategy_param']
params = self._overwrite_params(default_params, param_dict)
return params
def mekf_param(self, param_dict=None):
default_params = dict(
p0=1e-2, # 1e-2
lbd=1-1e-6, # 1
sigma_r=1,
sigma_q=0,
lr=1, # 1
miu_v=0, #momentum
miu_p=0, # EMA of P
k_p=1, #look ahead of P
use_lookahead=False, # outer lookahead
la_k=1, # outer lookahead
la_alpha=1,
)
if param_dict is None and 'mekf_param' in self.params_dict:
param_dict = self.params_dict['mekf_param']
params = self._overwrite_params(default_params, param_dict)
return params
def sgd_param(self, param_dict=None):
default_params = dict(
lr=1e-6,
momentum=0.7,
nesterov=False,
use_lookahead=False, #look ahead
la_k=5,
la_alpha = 0.8,
)
if param_dict is None and 'sgd_param' in self.params_dict:
param_dict = self.params_dict['sgd_param']
param = self._overwrite_params(default_params, param_dict)
return param
def adam_param(self, param_dict=None):
default_params = dict(
lr=1e-6,
betas=(0.1, 0.99),
amsgrad=True,
use_lookahead=False, #look ahead
la_k=5,
la_alpha=0.8,
)
if param_dict is None and 'adam_param' in self.params_dict:
param_dict = self.params_dict['adam_param']
param = self._overwrite_params(default_params, param_dict)
return param
def lbfgs_param(self, param_dict=None):
default_params = dict(
lr=0.002,
max_iter=20,
history_size=100,
use_lookahead=False, #look ahead
la_k=1,
la_alpha=1.0,
)
if param_dict is None and 'lbfgs_param' in self.params_dict:
param_dict = self.params_dict['lbfgs_param']
param = self._overwrite_params(default_params, param_dict)
return param
def print_params(self):
print('adaptation optimizer:',self.adaptor)
print('adaptation strategy parameters:')
print(self.strategy_param())
if self.adaptor=='none':
print('no adaptation')
else:
print('adaptation optimizer parameters:')
print(self.adapt_param())
def _overwrite_params(self, old_param, new_param):
if new_param is None:
return old_param
for k, v in new_param.items():
old_param[k] = v
return old_param
def _save_parameters(self, log_dir=None):
params_dict = {}
params_dict['strategy_param'] = self.strategy_param()
params_dict['mekf_param'] = self.mekf_param()
params_dict['sgd_param'] = self.sgd_param()
params_dict['adam_param'] = self.adam_param()
params_dict['lbfgs_param'] = self.lbfgs_param()
if log_dir is None:
log_dir = self.log_dir
with open(log_dir + 'adapt_hyper_parameters.json', 'w') as f:
json.dump(params_dict, f)
def _load_parameters(self, log_dir=None):
if log_dir is None:
log_dir = self.log_dir
with open(log_dir + 'adapt_hyper_parameters.json', 'r') as f:
self.params_dict = json.load(f)