-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathModelTest.py
373 lines (295 loc) · 15 KB
/
ModelTest.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
370
371
372
373
# !/usr/bin/env python
# -*- coding:utf-8 -*-
import torch
import numpy as np
from tqdm import tqdm
from helper import Trainer
import os
import torch.nn.functional as F
from tools.metrics import record
def model_val(runid, engine, dataloader, device, logger, cfg, epoch):
logger.info('Start validation phase.....')
val_dataloder = dataloader['val']
valid_total_loss = []
valid_eps_kl_loss = []
valid_z_kl_loss = []
valid_rec_loss = []
valid_pred_loss = []
valid_rec_mape = {}
valid_rec_rmse = {}
valid_rec_mae = {}
valid_rec_mae['bike'] = []
valid_rec_mae['taxi'] = []
valid_rec_mae['bus'] = []
valid_rec_mae['speed'] = []
valid_rec_rmse['bike'] = []
valid_rec_rmse['taxi'] = []
valid_rec_rmse['bus'] = []
valid_rec_rmse['speed'] = []
valid_rec_mape['bike'] = []
valid_rec_mape['taxi'] = []
valid_rec_mape['bus'] = []
valid_rec_mape['speed'] = []
valid_pred_mape = {}
valid_pred_rmse = {}
valid_pred_mae = {}
valid_pred_mae['bike'] = []
valid_pred_mae['taxi'] = []
valid_pred_mae['bus'] = []
valid_pred_mae['speed'] = []
valid_pred_rmse['bike'] = []
valid_pred_rmse['taxi'] = []
valid_pred_rmse['bus'] = []
valid_pred_rmse['speed'] = []
valid_pred_mape['bike'] = []
valid_pred_mape['taxi'] = []
valid_pred_mape['bus'] = []
valid_pred_mape['speed'] = []
val_tqdm_loader = tqdm(enumerate(val_dataloder))
for iter, (bike, bus, taxi, speed, pos) in val_tqdm_loader:
tpos = pos[..., :56]
weather = pos[..., 56:]
bike_flow = bike
taxi_flow = taxi
bus_flow = bus
tpos = tpos.to(device)
weather = weather.to(device)
bike_flow = bike_flow.to(device)
taxi_flow = taxi_flow.to(device)
bus_flow = bus_flow.to(device)
speed = speed.to(device)
total_loss, eps_kl_loss, z_kl_loss, rec_loss, pred_loss, \
rec_mae, rec_rmse, rec_mape, \
gen_mae, gen_rmse, gen_mape, speed_pred = engine.eval(tpos, weather, bike_flow, taxi_flow, bus_flow, speed)
valid_total_loss.append(total_loss)
valid_eps_kl_loss.append(eps_kl_loss)
valid_z_kl_loss.append(z_kl_loss)
valid_rec_loss.append(rec_loss)
valid_pred_loss.append(pred_loss)
record(valid_rec_mae, valid_rec_rmse, valid_rec_mape, rec_mae, rec_rmse, rec_mape)
record(valid_pred_mae, valid_pred_rmse, valid_pred_mape, gen_mae, gen_rmse, gen_mape, only_last=True)
mvalid_total_loss = np.mean(valid_total_loss)
mvalid_eps_kl_loss = np.mean(valid_eps_kl_loss)
mvalid_z_kl_loss = np.mean(valid_z_kl_loss)
mvalid_rec_loss = np.mean(valid_rec_loss)
mvalid_pred_loss = np.mean(valid_pred_loss)
mvalid_rec_bike_mae = np.mean(valid_rec_mae['bike'])
mvalid_rec_bike_mape = np.mean(valid_rec_mape['bike'])
mvalid_rec_bike_rmse = np.mean(valid_rec_rmse['bike'])
mvalid_rec_taxi_mae = np.mean(valid_rec_mae['taxi'])
mvalid_rec_taxi_mape = np.mean(valid_rec_mape['taxi'])
mvalid_rec_taxi_rmse = np.mean(valid_rec_rmse['taxi'])
mvalid_rec_bus_mae = np.mean(valid_rec_mae['bus'])
mvalid_rec_bus_mape = np.mean(valid_rec_mape['bus'])
mvalid_rec_bus_rmse = np.mean(valid_rec_rmse['bus'])
mvalid_rec_speed_mae = np.mean(valid_rec_mae['speed'])
mvalid_rec_speed_mape = np.mean(valid_rec_mape['speed'])
mvalid_rec_speed_rmse = np.mean(valid_rec_rmse['speed'])
mvalid_pred_bike_mae = np.mean(valid_pred_mae['bike'])
mvalid_pred_bike_mape = np.mean(valid_pred_mape['bike'])
mvalid_pred_bike_rmse = np.mean(valid_pred_rmse['bike'])
mvalid_pred_taxi_mae = np.mean(valid_pred_mae['taxi'])
mvalid_pred_taxi_mape = np.mean(valid_pred_mape['taxi'])
mvalid_pred_taxi_rmse = np.mean(valid_pred_rmse['taxi'])
mvalid_pred_bus_mae = np.mean(valid_pred_mae['bus'])
mvalid_pred_bus_mape = np.mean(valid_pred_mape['bus'])
mvalid_pred_bus_rmse = np.mean(valid_pred_rmse['bus'])
mvalid_pred_speed_mae = np.mean(valid_pred_mae['speed'])
mvalid_pred_speed_mape = np.mean(valid_pred_mape['speed'])
mvalid_pred_speed_rmse = np.mean(valid_pred_rmse['speed'])
log = 'Epoch: {:03d}, Valid Total Loss: {:.4f}\n' \
'Valid Eps KL: {:.4f}\t\t\tValid Z KL: {:.4f} \n' \
'Valid Rec Loss: {:.4f}\t\t\tValid Pred Loss: {:.4f} \n' \
'Valid Rec Bike MAE: {:.4f}\t\t\tRMSE: {:.4f}\t\t\tMAPE: {:.4f} \n' \
'Valid Rec Taxi MAE: {:.4f}\t\t\tRMSE: {:.4f}\t\t\tMAPE: {:.4f} \n' \
'Valid Rec Bus MAE: {:.4f}\t\t\tRMSE: {:.4f}\t\t\tMAPE: {:.4f} \n' \
'Valid Rec Speed MAE: {:.4f}\t\t\tRMSE: {:.4f}\t\t\tMAPE: {:.4f} \n\n' \
'Valid Pred Bike MAE: {:.4f}\t\t\tRMSE: {:.4f}\t\t\tMAPE: {:.4f}\n' \
'Valid Pred Taxi MAE: {:.4f}\t\t\tRMSE: {:.4f}\t\t\tMAPE: {:.4f}\n' \
'Valid Pred Bus MAE: {:.4f}\t\t\tRMSE: {:.4f}\t\t\tMAPE: {:.4f}\n' \
'Valid Pred Speed MAE: {:.4f}\t\t\tRMSE: {:.4f}\t\t\tMAPE: {:.4f}\n'
logger.info(log.format(epoch, mvalid_total_loss, mvalid_eps_kl_loss, mvalid_z_kl_loss,
mvalid_rec_loss, mvalid_pred_loss,
mvalid_rec_bike_mae, mvalid_rec_bike_rmse, mvalid_rec_bike_mape,
mvalid_rec_taxi_mae, mvalid_rec_taxi_rmse, mvalid_rec_taxi_mape,
mvalid_rec_bus_mae, mvalid_rec_bus_rmse , mvalid_rec_bus_mape,
mvalid_rec_speed_mae, mvalid_rec_speed_rmse, mvalid_rec_speed_mape,
mvalid_pred_bike_mae, mvalid_pred_bike_rmse, mvalid_pred_bike_mape,
mvalid_pred_taxi_mae, mvalid_pred_taxi_rmse, mvalid_pred_taxi_mape,
mvalid_pred_bus_mae, mvalid_pred_bus_rmse, mvalid_pred_bus_mape,
mvalid_pred_speed_mae, mvalid_pred_speed_rmse, mvalid_pred_speed_mape,
))
return mvalid_pred_loss, mvalid_pred_speed_mae, mvalid_pred_speed_rmse, mvalid_pred_speed_mape,
def model_test(runid, engine, dataloader, device, logger, cfg, mode='Test'):
logger.info('Start testing phase.....')
test_dataloder = dataloader['test']
test_total_loss = []
test_eps_kl_loss = []
test_z_kl_loss = []
test_rec_loss = []
test_pred_loss = []
test_rec_mape = {}
test_rec_rmse = {}
test_rec_mae = {}
test_rec_mae['bike'] = []
test_rec_mae['taxi'] = []
test_rec_mae['bus'] = []
test_rec_mae['speed'] = []
test_rec_rmse['bike'] = []
test_rec_rmse['taxi'] = []
test_rec_rmse['bus'] = []
test_rec_rmse['speed'] = []
test_rec_mape['bike'] = []
test_rec_mape['taxi'] = []
test_rec_mape['bus'] = []
test_rec_mape['speed'] = []
test_pred_mape = {}
test_pred_rmse = {}
test_pred_mae = {}
test_pred_mae['bike'] = []
test_pred_mae['taxi'] = []
test_pred_mae['bus'] = []
test_pred_mae['speed'] = []
test_pred_rmse['bike'] = []
test_pred_rmse['taxi'] = []
test_pred_rmse['bus'] = []
test_pred_rmse['speed'] = []
test_pred_mape['bike'] = []
test_pred_mape['taxi'] = []
test_pred_mape['bus'] = []
test_pred_mape['speed'] = []
test_outputs_list = []
test_tqdm_loader = tqdm(enumerate(test_dataloder))
for iter, ((bike, bus, taxi, speed, pos)) in test_tqdm_loader:
tpos = pos[..., :56]
weather = pos[..., 56:]
bike_flow = bike
taxi_flow = taxi
bus_flow = bus
tpos = tpos.to(device)
weather = weather.to(device)
bike_flow = bike_flow.to(device)
taxi_flow = taxi_flow.to(device)
bus_flow = bus_flow.to(device)
speed = speed.to(device)
total_loss, eps_kl_loss, z_kl_loss, rec_loss, pred_loss, \
rec_mae, rec_rmse, rec_mape, \
gen_mae, gen_rmse, gen_mape, speed_pred = engine.eval(tpos, weather, bike_flow, taxi_flow, bus_flow, speed)
test_total_loss.append(total_loss)
test_eps_kl_loss.append(eps_kl_loss)
test_z_kl_loss.append(z_kl_loss)
test_rec_loss.append(rec_loss)
test_pred_loss.append(pred_loss)
test_outputs_list.append(speed_pred)
record(test_rec_mae, test_rec_rmse, test_rec_mape, rec_mae, rec_rmse, rec_mape)
record(test_pred_mae, test_pred_rmse, test_pred_mape, gen_mae, gen_rmse, gen_mape, only_last=True)
mtest_total_loss = np.mean(test_total_loss)
mtest_eps_kl_loss = np.mean(test_eps_kl_loss)
mtest_z_kl_loss = np.mean(test_z_kl_loss)
mtest_rec_loss = np.mean(test_rec_loss)
mtest_pred_loss = np.mean(test_pred_loss)
mtest_rec_bike_mae = np.mean(test_rec_mae['bike'])
mtest_rec_bike_mape = np.mean(test_rec_mape['bike'])
mtest_rec_bike_rmse = np.mean(test_rec_rmse['bike'])
mtest_rec_taxi_mae = np.mean(test_rec_mae['taxi'])
mtest_rec_taxi_mape = np.mean(test_rec_mape['taxi'])
mtest_rec_taxi_rmse = np.mean(test_rec_rmse['taxi'])
mtest_rec_bus_mae = np.mean(test_rec_mae['bus'])
mtest_rec_bus_mape = np.mean(test_rec_mape['bus'])
mtest_rec_bus_rmse = np.mean(test_rec_rmse['bus'])
mtest_rec_speed_mae = np.mean(test_rec_mae['speed'])
mtest_rec_speed_mape = np.mean(test_rec_mape['speed'])
mtest_rec_speed_rmse = np.mean(test_rec_rmse['speed'])
mtest_pred_bike_mae = np.mean(test_pred_mae['bike'])
mtest_pred_bike_mape = np.mean(test_pred_mape['bike'])
mtest_pred_bike_rmse = np.mean(test_pred_rmse['bike'])
mtest_pred_taxi_mae = np.mean(test_pred_mae['taxi'])
mtest_pred_taxi_mape = np.mean(test_pred_mape['taxi'])
mtest_pred_taxi_rmse = np.mean(test_pred_rmse['taxi'])
mtest_pred_bus_mae = np.mean(test_pred_mae['bus'])
mtest_pred_bus_mape = np.mean(test_pred_mape['bus'])
mtest_pred_bus_rmse = np.mean(test_pred_rmse['bus'])
mtest_pred_speed_mae = np.mean(test_pred_mae['speed'])
mtest_pred_speed_mape = np.mean(test_pred_mape['speed'])
mtest_pred_speed_rmse = np.mean(test_pred_rmse['speed'])
log = 'Test Total Loss: {:.4f}\n' \
'Test Eps KL: {:.4f}\t\t\tTest Z KL: {:.4f} \n' \
'Test Rec Loss: {:.4f}\t\t\tTest Pred Loss: {:.4f} \n' \
'Test Rec Bike MAE: {:.4f}\t\t\tRMSE: {:.4f}\t\t\tMAPE: {:.4f} \n' \
'Test Rec Taxi MAE: {:.4f}\t\t\tRMSE: {:.4f}\t\t\tMAPE: {:.4f} \n' \
'Test Rec Bus MAE: {:.4f}\t\t\tRMSE: {:.4f}\t\t\tMAPE: {:.4f} \n' \
'Test Rec Speed MAE: {:.4f}\t\t\tRMSE: {:.4f}\t\t\tMAPE: {:.4f} \n\n' \
'Test Pred Bike MAE: {:.4f}\t\t\tRMSE: {:.4f}\t\t\tMAPE: {:.4f}\n' \
'Test Pred Taxi MAE: {:.4f}\t\t\tRMSE: {:.4f}\t\t\tMAPE: {:.4f}\n' \
'Test Pred Bus MAE: {:.4f}\t\t\tRMSE: {:.4f}\t\t\tMAPE: {:.4f}\n' \
'Test Pred Speed MAE: {:.4f}\t\t\tRMSE: {:.4f}\t\t\tMAPE: {:.4f}\n'
logger.info(log.format(mtest_total_loss, mtest_eps_kl_loss, mtest_z_kl_loss,
mtest_rec_loss, mtest_pred_loss,
mtest_rec_bike_mae, mtest_rec_bike_rmse, mtest_rec_bike_mape,
mtest_rec_taxi_mae, mtest_rec_taxi_rmse, mtest_rec_taxi_mape,
mtest_rec_bus_mae, mtest_rec_bus_rmse , mtest_rec_bus_mape,
mtest_rec_speed_mae, mtest_rec_speed_rmse, mtest_rec_speed_mape,
mtest_pred_bike_mae, mtest_pred_bike_rmse, mtest_pred_bike_mape,
mtest_pred_taxi_mae, mtest_pred_taxi_rmse, mtest_pred_taxi_mape,
mtest_pred_bus_mae, mtest_pred_bus_rmse, mtest_pred_bus_mape,
mtest_pred_speed_mae, mtest_pred_speed_rmse, mtest_pred_speed_mape,))
predicts = torch.cat(test_outputs_list, dim=0)
if mode == 'Test':
pred_all = predicts.cpu()
path_save_pred = os.path.join('Results', cfg['model_name'], 'exp{:d}'.format(cfg['expid']), 'result_pred')
if not os.path.exists(path_save_pred):
os.makedirs(path_save_pred, exist_ok=True)
name = 'exp{:s}_Test_Loss:{:.4f}_mae:{:.4f}_rmse:{:.4f}_mape:{:.4f}'. \
format(cfg['model_name'], mtest_pred_loss, mtest_pred_speed_mae, mtest_pred_speed_rmse, mtest_pred_speed_mape)
path = os.path.join(path_save_pred, name)
np.save(path, pred_all)
logger.info('result of prediction has been saved, path: {}'.format(path))
logger.info('shape: ' + str(pred_all.shape))
logger.info('\n' + str(F.relu(
torch.tanh(cfg['model']['amplify_alpha'] * engine.CausalHMM.SCM.Weight_DAG.detach())).cpu().numpy()))
return mtest_pred_loss, mtest_pred_speed_mae, mtest_pred_speed_rmse, mtest_pred_speed_mape
def baseline_test(runid,
CausalHMM,
dataloader,
device,
logger,
cfg):
bike_scalar = dataloader['scalar_bike']
taxi_scalar = dataloader['scalar_taxi']
bus_scalar = dataloader['scalar_bus']
speed_scalar = dataloader['scalar_speed']
scalar = [bike_scalar, taxi_scalar, bus_scalar, speed_scalar]
engine = Trainer(CausalHMM=CausalHMM,
base_lr=cfg['train']['base_lr'],
weight_decay=cfg['train']['weight_decay'],
milestones=cfg['train']['milestones'],
lr_decay_ratio=cfg['train']['lr_decay_ratio'],
min_learning_rate=cfg['train']['min_learning_rate'],
max_grad_norm=cfg['train']['max_grad_norm'],
num_for_target=cfg['data']['num_for_target'],
num_for_predict=cfg['data']['num_for_predict'],
loss_weight=cfg['data']['num_for_predict'],
scaler=scalar,
device=device,
DAG_loss_weight=cfg['train']['DAG_loss_weight'],
)
best_mode_path = cfg['train']['best_mode']
logger.info("loading {}".format(best_mode_path))
save_dict = torch.load(best_mode_path)
engine.CausalHMM.load_state_dict(save_dict['CausalHMM_state_dict'], strict=False)
logger.info('model load success! {}\n'.format(best_mode_path))
total_param = 0
logger.info('Net\'s state_dict:')
for param_tensor in engine.CausalHMM.state_dict():
logger.info(param_tensor + '\t' + str(engine.CausalHMM.state_dict()[param_tensor].size()))
total_param += np.prod(engine.CausalHMM.state_dict()[param_tensor].size())
logger.info('Net\'s total params:{:d}\n'.format(int(total_param)))
logger.info('Optimizer\'s state_dict:')
for var_name in engine.CausalHMM_optimizer.state_dict():
logger.info(var_name + '\t' + str(engine.CausalHMM_optimizer.state_dict()[var_name]))
nParams = sum([p.nelement() for p in CausalHMM.parameters()])
logger.info('Number of model parameters is {:d}\n'.format(int(nParams)))
mtest_total_loss, mtest_pred_mae, mtest_pred_rmse, mtest_pred_mape = model_test(runid, engine, dataloader, device,
logger, cfg, mode='Test')
return mtest_total_loss, mtest_pred_mae, mtest_pred_rmse, mtest_pred_mape, \
mtest_total_loss, mtest_pred_mae, mtest_pred_rmse, mtest_pred_mape