-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
1377 lines (1162 loc) · 63.9 KB
/
utils.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import random
import numpy as np
import pickle
import torch
import torch.nn as nn
import torch.utils.data as utils_data
import torch.optim as optim
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, roc_curve, auc, roc_auc_score
import torch.nn.functional as F
import logging
import math
from time import time
import params
from sklearn import metrics
import torchvision
import matplotlib
from torchvision import transforms as tf
from matplotlib.backends.backend_pdf import PdfPages
from torchvision.utils import make_grid
from pytorch_grad_cam import GradCAM, HiResCAM, ScoreCAM, GradCAMPlusPlus, AblationCAM, XGradCAM, EigenCAM, FullGrad
from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
from pytorch_grad_cam.utils.image import show_cam_on_image
font = {'size' : 22}
matplotlib.rcParams['figure.figsize'] = (18, 12)
matplotlib.rc('font', **font)
def eval_top_func(p, model, lc_loss_func, ttlc_loss_func, task, te_dataset, device, model_tag = ''):
model = model.to(device)
te_loader = utils_data.DataLoader(dataset = te_dataset, shuffle = True, batch_size = p.BATCH_SIZE, drop_last= True, pin_memory= True, num_workers= 12)
vis_data_path = p.VIS_DIR + p.SELECTED_DATASET + '_' + model_tag + '.pickle'
best_model_path = p.MODELS_DIR + p.SELECTED_DATASET + '_' + model_tag + '.pt'
figure_name = p.SELECTED_DATASET + '_' + model_tag
if p.resume:
print("loading the best model")
if not os.path.exists(p.last_model_DIR):
print("the best model pasth does not exist")
exit()
model.load_state_dict(torch.load(p.last_model_DIR))
start = time()
robust_test_pred_time, test_pred_time, test_acc, test_loss, test_lc_loss, test_ttlc_loss, auc, max_j, precision, recall, f1 =\
eval_model(p,model, lc_loss_func, ttlc_loss_func, task, te_loader, ' N/A', device, eval_type = 'Test', vis_data_path = vis_data_path, figure_name = figure_name)
end = time()
total_time = end-start
#print("Test finished in:", total_time, "sec.")
#print("Final Test accuracy:",te_acc)
result_dic = {
'Test Acc': test_acc,
'Test Robust Pred Time': robust_test_pred_time,
'Test Pred Time': test_pred_time,
'Test Total Loss': test_loss,
# 'Test Classification Loss': test_lc_loss,
'Test Regression Loss': test_ttlc_loss,
'AUC': auc,
'Max Youden Index': max_j,
'Precision': precision,
'Recall': recall,
'F1':f1
}
return result_dic
def train_top_func(p, model, optimizer, lc_loss_func, ttlc_loss_func, task, curriculum,
tr_loader, val_loader, device, model_tag = ''):
model = model.to(device)
subnet_layers_list = []
for name, modelParam in model.named_parameters():
if "LSTM" in name or "fc" in name:
subnet_layers_list.append(modelParam)
if p.resume:
print("loading the best model")
if not os.path.exists(os.path.join(p.last_model_DIR, "BEST_VAL_LOSS_MODEL.pt")):
print("the best model path does not exist")
exit()
model.load_state_dict(torch.load(os.path.join(p.last_model_DIR, "BEST_VAL_LOSS_MODEL.pt")))
print("the best model is loaded")
# tr_data_loader = torch.utils.data.DataLoader(tr_dataset,batch_size=1, shuffle=True,
# num_workers=1, drop_last=True)
# val_data_loader = torch.utils.data.DataLoader(val_dataset,batch_size=1, shuffle=True,
# num_workers=1, drop_last=True)
scheduler = optim.lr_scheduler.StepLR(optimizer, p.LR_DECAY_EPOCH, p.LR_DECAY)
best_model_path = p.MODELS_DIR + p.SELECTED_DATASET + '_' + model_tag + '.pt'
best_rmse_val_loss = float("inf")
best_mse_val_loss = 0
patience = p.PATIENCE
best_val_pred_time = 0
best_epoch = 0
total_time = 0
curriculum_flag = curriculum['loss'] or curriculum['seq'] or curriculum['virtual']
val_ttlc_loss_history = []
for epoch in range(p.NUM_EPOCHS):
#print("Epoch: {} Started!".format(epoch+1))
start = time()
train_model(p, model, optimizer, scheduler, tr_loader, lc_loss_func, ttlc_loss_func, task,
curriculum, epoch+1, device, calc_train_acc= False, subnet_layers_list=subnet_layers_list)
val_start = time()
val_mse_loss, val_rmse_loss = eval_model(p, model, val_loader, ttlc_loss_func, device)
val_ttlc_loss_history.append(val_rmse_loss)
# print (val_rmse_loss)
# exit()
val_end = time()
print('val_time: {:.3f}'.format(val_end-val_start))
#print("Validation Accuracy:",val_acc,' Avg Pred Time: ', val_avg_pred_time, " Avg Loss: ", val_loss," at Epoch", epoch+1)
# if epoch<p.CL_EPOCH and curriculum_flag:
# print("No Early Stopping in CL Epochs.")
# continue
with open(os.path.join(p.RESULTS_DIR, 'Validation_Loss_History.txt'), 'w') as file:
for row in val_ttlc_loss_history:
# s = " ".join(map(str, row))
file.write(str(row)+'\n')
if val_rmse_loss<best_rmse_val_loss:
best_rmse_val_loss = val_rmse_loss
best_mse_val_loss = val_mse_loss
best_epoch = epoch
torch.save(model.state_dict(), p.last_model_DIR+'BEST_VAL_LOSS_MODEL.pt')
# torch.save(model.state_dict(), best_model_path)
patience = p.PATIENCE
save_plot_model_prediction(p, model, tr_loader, device, epoch, p.sample_image_DIR)
save_plot_model_prediction(p, model, val_loader, device, epoch, p.test_image_DIR)
else:
patience -= 1
end = time()
total_time += end-start
print("In the best epoch, MSE val loss :{:.5f}\tRMSE val Loss: {:.5f}\tat Epoch {}".format(best_mse_val_loss, best_rmse_val_loss, best_epoch+1))
print("Epoch: {} finished in {:.3f} sec\n".format(epoch+1, end-start))
if patience == 0:
print(' No performance improvement in Validation data after:', epoch+1, 'Epochs!')
# torch.save(model.state_dict(), p.last_model_DIR+'.pt')
# torch.save(model.state_dict(), p.last_model_DIR+'BEST_VAL_LOSS_MODEL.pt')
break
result_dic = {
'EarlyStopping Epoch': best_epoch + 1,
'Best Validaction loss': best_mse_val_loss,
'Best Validation Pred Time': best_val_pred_time,
'Best Validation Loss': best_rmse_val_loss,
'Validation Loss History' : val_ttlc_loss_history
}
return result_dic
def train_model(p, model, optimizer, scheduler, train_loader, lc_loss_func, ttlc_loss_func, task,
curriculum, epoch, device, vis_step = 20, calc_train_acc = True, subnet_layers_list=None):
grad_norm = dict()
for name, modelParam in model.named_parameters():
grad_norm[name] = []
grad_norm['CNN'] = []
grad_norm['GRU'] = []
grad_norm['FC'] = []
# number of batch
model_time = 0
avg_loss = 0
avg_loss_x = 0
avg_loss_y = 0
avg_rmse_loss = 0
all_start = time()
lstmNorm, CNNNorm =[], []
# Training loop over batches of data on train dataset
for batch_idx, data in enumerate(train_loader):
# if (batch_idx+1)%10:
# for layer in subnet_layers_list:
# layer.requires_grad = True
# else:
# for layer in subnet_layers_list:
# layer.requires_grad = False
data_occ = data['occ']
data_mask = data['mask']
data_v_x = data['v_x']
data_v_y = data['v_y']
label_x_fut = data['x_fut']
label_y_fut = data['y_fut']
start = time()
if (p.vector_field_available):
if model.model_type in ['pretrained_denseNet', 'pretrainedResnetModel', 'Resnet_LSTM', '3DCNN', 'Prob-CNN-LSTM-v6']:
data_tuple = torch.cat((data_occ + data_mask, data_v_x, data_v_y), 1).to(device)
data_norm = tf.Normalize((4.9260162e-02+6.9080950e-03, -1.3276902e+00, 1.2969130e-05), (0.21641071, 5.9344087, 0.00651047))
else:
data_tuple = torch.cat((data_occ, data_mask, data_v_x, data_v_y), 1).to(device)
data_norm = tf.Normalize((4.9260162e-02, 6.9080950e-03, -1.3276902e+00, 1.2969130e-05), (0.21641071, 0.08282737, 5.9344087, 0.00651047))
else:
if model.model_type in ['pretrained_denseNet', 'pretrainedResnetModel', 'Resnet_LSTM', '3DCNN', 'Prob-CNN-LSTM-v6']:
data_tuple = torch.cat((data_occ + data_mask, data_occ + data_mask, data_occ + data_mask), 1).to(device)
data_norm = tf.Normalize((4.9260162e-02+ 6.9080950e-03,4.9260162e-02+ 6.9080950e-03, 4.9260162e-02+ 6.9080950e-03), (0.21641071,0.21641071,0.21641071))
else:
data_tuple = torch.cat((data_occ, data_mask), 1).to(device)
data_norm = tf.Normalize((4.9260162e-02, 6.9080950e-03), (0.21641071, 0.08282737))
labels = torch.cat((label_x_fut, label_y_fut), 1).to(device)
label_norm = tf.Normalize((-5.6779733e+00, -1.0209690e-03), (1.0219698, 0.03529745))
if(p.input_normalization):
data_tuple = data_norm(data_tuple.permute(0,2,1,3,4)).permute(0,2,1,3,4)
if(p.output_normalization):
labels = labels
# labels = label_norm(labels.permute(1,0,2)).permute(1,0,2)
# 1. Clearing previous gradient values.
optimizer.zero_grad()
# 2. feeding data to model (forward method will be computed)
model.train()
output_dict = model(data_tuple, labels)
if(model.model_type in ['probabilistic', 'Prob-CNN-LSTM-v4', 'Prob-CNN-LSTM-v6']):
ttlc_pred_x, ttlc_pred_y, ttlc_pred_sigmaX, ttlc_pred_sigmaY, ttlc_pred_rho = output_dict['ttlc_pred']
else:
ttlc_pred_x, ttlc_pred_y = output_dict['ttlc_pred']
# 3. Calculating the loss value
if(model.model_type in ['probabilistic', 'Prob-CNN-LSTM-v4', 'Prob-CNN-LSTM-v6']):
# loss = ttlc_loss_func(ttlc_pred_x, ttlc_pred_y, ttlc_pred_sigmaX, ttlc_pred_sigmaY, ttlc_pred_rho,
# labels[:,0,:], labels[:,1,:]) # distance loss
ttlc_loss_y = ttlc_loss_func(labels[:,0,:], ttlc_pred_y, ttlc_pred_sigmaX, ttlc_pred_sigmaY, ttlc_pred_rho,
labels[:,0,:], labels[:,1,:]) # distance loss
ttlc_loss_x = ttlc_loss_func(ttlc_pred_x, labels[:,1,:], ttlc_pred_sigmaX, ttlc_pred_sigmaY, ttlc_pred_rho,
labels[:,0,:], labels[:,1,:]) # distance loss
loss = ttlc_loss_x*(p.loss_x_alpha) + ttlc_loss_y*(1-p.loss_x_alpha)
# loss = ttlc_loss_func(torch.cumsum(ttlc_pred_x, dim=-1), torch.cumsum(ttlc_pred_y, dim=-1),
# ttlc_pred_sigmaX, ttlc_pred_sigmaY, ttlc_pred_rho,
# torch.cumsum(labels[:,0,:], dim=-1), torch.cumsum(labels[:,1,:], dim=-1)) # distance loss
else:
ttlc_loss_x = ttlc_loss_func(torch.cumsum(ttlc_pred_x, dim=-1), torch.cumsum(labels[:,0,:], dim=-1)) # distance loss
ttlc_loss_y = ttlc_loss_func(torch.cumsum(ttlc_pred_y, dim=-1), torch.cumsum(labels[:,1,:], dim=-1))
# ttlc_loss_x = ttlc_loss_func(ttlc_pred_x,labels[:,0,:]) # velocity loss
# ttlc_loss_y = ttlc_loss_func(ttlc_pred_y,labels[:,1,:])
loss = ttlc_loss_x*(p.loss_x_alpha) + ttlc_loss_y*(1-p.loss_x_alpha)
# 4. Calculating new grdients given the loss value
loss.backward()
# 5. Updating the weights
### Gradient Norm Clipping
# nn.utils.clip_grad_norm_(subnet_layers_list, max_norm=1.0, norm_type=2)
# nn.utils.clip_grad_norm_(subnet_layers_list, max_norm=1.0, norm_type=2)
#Gradient Value Clipping
# nn.utils.clip_grad_value_(subnet_layers_list, clip_value=1.0)
nn.utils.clip_grad_value_(model.parameters(), clip_value=1.0)
optimizer.step()
#for monitoring only
# lstmNorm.append(model.LSTM_fc_x.weight.grad.norm().cpu().numpy())
# CNNNorm.append(model.init_conv7.weight.grad.norm().cpu().numpy())
#
#
avg_loss += loss.data/(len(train_loader))
avg_loss_x += ttlc_loss_x.data/(len(train_loader))
avg_loss_y += ttlc_loss_y.data/(len(train_loader))
with torch.no_grad():
all_traj_pred = np.cumsum(torch.cat((ttlc_pred_x.unsqueeze(1) ,ttlc_pred_y.unsqueeze(1)), dim=1).cpu().numpy(), axis = -1)
all_traj_gt = np.cumsum(labels.cpu().numpy(), axis = -1)
n_samples = all_traj_pred.shape[0] * all_traj_pred.shape[2]
traj_rmse = np.sum((all_traj_pred-all_traj_gt)**2)/n_samples
avg_rmse_loss += traj_rmse/(len(train_loader))
if (batch_idx+1) % 100 == 0:
avg_rmse_loss = np.sqrt(avg_rmse_loss)
# print('Epoch: ',epoch, '\tBatch: ', batch_idx+1,
# '\tTraining MSE Loss: ', avg_loss.cpu().numpy(), '\tTraining RMSE Loss: ',avg_rmse_loss )
print('Epoch: {}\tBatch: {}\tTraining MSE Loss: {:.5f}\tTraining RMSE Loss: {:.5f}\navg_loss_x: {:.5f}\tavg_loss_y: {:.5f}'.format(epoch,\
batch_idx+1, avg_loss.cpu().numpy(), avg_rmse_loss, avg_loss_x.cpu().numpy(), avg_loss_y.cpu().numpy() ))
# figure = visualize_interlayer_wrt_input(p=p, model=model, input=data_tuple, labels=labels)
# figure.savefig(p.RESULTS_DIR+'/epoch_{}'.format(batch_idx)+ '_batch_{}'.format(batch_idx)+'.png')
# figure.close()
avg_loss = 0
avg_loss_x = 0
avg_loss_y = 0
avg_rmse_loss = 0
# CNNNorm, lstmNorm =[], []
for name, modelParam in model.named_parameters():
grad_norm[name].append(modelParam.grad.norm())
end = time()
model_time += end-start
###############################################
fig, ax = plt.subplots()
for key in grad_norm:
if 'conv' in key:
grad_norm['CNN'].append(torch.stack(grad_norm[key]).cpu().numpy())
elif 'LSTM_ttlc' in key:
grad_norm['GRU'].append(torch.stack(grad_norm[key]).cpu().numpy())
elif 'LSTM_fc' in key:
grad_norm['FC'].append(torch.stack(grad_norm[key]).cpu().numpy())
for key in ['CNN', 'GRU', 'FC']:
data = np.average(grad_norm[key], axis=0)
ax.plot(data, label=key)
ax.set_xlabel('batch number')
ax.set_ylabel('Norm')
ax.set_title("gradient norm")
ax.legend(fontsize="10")
fig.savefig(p.RESULTS_DIR+'/epoch_{}'.format(epoch)+ '_batch_{}'.format(batch_idx)+'.png')
plt.close()
###############################################
all_end = time()
all_time = all_end - all_start
print('model time: {:.4f}\tall training time: {:.4f}'.format(model_time, all_time))
scheduler.step()
def eval_model(p, model, val_loader, ttlc_loss_func, device):
# number of batch
avg_loss = 0
avg_rmse_loss = 0
# Training loop over batches of data on train dataset
for batch_idx, data in enumerate(val_loader):
data_occ = data['occ']
data_mask = data['mask']
data_v_x = data['v_x']
data_v_y = data['v_y']
label_x_fut = data['x_fut']
label_y_fut = data['y_fut']
if (p.vector_field_available):
if model.model_type in ['pretrained_denseNet', 'pretrainedResnetModel', 'Resnet_LSTM', '3DCNN', 'Prob-CNN-LSTM-v6']:
data_tuple = torch.cat((data_occ + data_mask, data_v_x, data_v_y), 1).to(device)
else:
data_tuple = torch.cat((data_occ, data_mask, data_v_x, data_v_y), 1).to(device)
data_norm = tf.Normalize((4.9260162e-02, 6.9080950e-03, -1.3276902e+00, 1.2969130e-05), (0.21641071, 0.08282737, 5.9344087, 0.00651047))
else:
if model.model_type in ['pretrained_denseNet', 'pretrainedResnetModel', 'Resnet_LSTM', '3DCNN', 'Prob-CNN-LSTM-v6']:
data_tuple = torch.cat((data_occ + data_mask, data_occ + data_mask, data_occ + data_mask), 1).to(device)
else:
data_tuple = torch.cat((data_occ, data_mask), 1).to(device)
labels = torch.cat((label_x_fut, label_y_fut), 1).to(device)
label_norm = tf.Normalize((-5.6779733e+00, -1.0209690e-03), (1.0219698, 0.03529745))
# if(p.input_normalization):
# data_tuple = data_norm(data_tuple.permute(0,2,1,3,4)).permute(0,2,1,3,4)
if(p.input_normalization):
data_tuple = data_norm(data_tuple.permute(0,2,1,3,4)).permute(0,2,1,3,4)
if(p.output_normalization):
labels = label_norm(labels.permute(1,0,2)).permute(1,0,2)
# 1. Clearing previous gradient values.
with torch.no_grad():
# 2. feeding data to model (forward method will be computed)
if model.model_type in ['CNN-LSTM-v3', 'CNN-LSTM-v5', 'CNN-LSTM-v7']:
output_dict = model.eval().test(data_tuple)
else:
output_dict = model.eval()(data_tuple, labels)
if(model.model_type in ['probabilistic', 'Prob-CNN-LSTM-v4', 'Prob-CNN-LSTM-v6']):
ttlc_pred_x, ttlc_pred_y, ttlc_pred_sigmaX, ttlc_pred_sigmaY, ttlc_pred_rho = output_dict['ttlc_pred']
else:
ttlc_pred_x, ttlc_pred_y = output_dict['ttlc_pred']
# 3. Calculating the loss value
if(model.model_type in ['probabilistic', 'Prob-CNN-LSTM-v4', 'Prob-CNN-LSTM-v6']):
loss = ttlc_loss_func(ttlc_pred_x, ttlc_pred_y, ttlc_pred_sigmaX, ttlc_pred_sigmaY, ttlc_pred_rho,
labels[:,0,:], labels[:,1,:]) # distance loss
# loss = ttlc_loss_func(torch.cumsum(ttlc_pred_x, dim=-1), torch.cumsum(ttlc_pred_y, dim=-1),
# ttlc_pred_sigmaX, ttlc_pred_sigmaY, ttlc_pred_rho,
# torch.cumsum(labels[:,0,:], dim=-1), torch.cumsum(labels[:,1,:], dim=-1)) # distance loss
else:
# ttlc_loss_x = ttlc_loss_func(torch.cumsum(ttlc_pred_x, dim=-1), torch.cumsum(labels[:,0,:], dim=-1)) # distance loss
# ttlc_loss_y = ttlc_loss_func(torch.cumsum(ttlc_pred_y, dim=-1), torch.cumsum(labels[:,1,:], dim=-1))
ttlc_loss_x = ttlc_loss_func(ttlc_pred_x,labels[:,0,:]) # velocity loss
ttlc_loss_y = ttlc_loss_func(ttlc_pred_y,labels[:,1,:])
loss = ttlc_loss_x*(1-p.loss_x_alpha) + ttlc_loss_y*(1+p.loss_x_alpha)
avg_loss += loss.data/(len(val_loader))
all_traj_pred = np.cumsum(torch.cat((ttlc_pred_x.unsqueeze(1) ,ttlc_pred_y.unsqueeze(1)), dim=1).cpu().numpy(), axis = -1)
all_traj_gt = np.cumsum(labels.cpu().numpy(), axis = -1)
n_samples = all_traj_pred.shape[0] * all_traj_pred.shape[2]
traj_rmse = np.sum((all_traj_pred-all_traj_gt)**2)/n_samples
avg_rmse_loss += traj_rmse
if batch_idx+1 == len(val_loader):
avg_rmse_loss = avg_rmse_loss/(len(val_loader))
avg_rmse_loss = np.sqrt(avg_rmse_loss)
print('\tValidation Training MSE Loss: {:.5f}\tValidation RMSE Loss: {:.5f}'.format(avg_loss.cpu().numpy(), avg_rmse_loss))
return avg_loss, avg_rmse_loss
# def train_model(p, model, optimizer, scheduler, train_loader, lc_loss_func, ttlc_loss_func, task,
# curriculum, epoch, device, vis_step = 20, calc_train_acc = True):
# # Number of samples with correct classification
# # total size of train data
# # data_iter = iter(tr_data_loader)
# # # data_occ = next(data_iter)
# # data_occ,data_mask = next(data_iter)['occ'],next(data_iter)['mask']
# # data_v_x,data_v_y = next(data_iter)['v_x'],next(data_iter)['v_y']
# # label_x_fut, label_y_fut = next(data_iter)['x_fut'],next(data_iter)['y_fut']
# # data_occ = data_occ.to(device)
# # alaki=1
# # print (data_occ[-1].shape)
# # print ("dataLoader is fine")
# total = len(train_loader.dataset)
# # number of batch
# num_batch = int(np.floor(total/model.batch_size))
# model_time = 0
# avg_loss = 0
# avg_rmse_loss = 0
# all_start = time()
# if curriculum['loss']:
# loss_ratio = p.LOSS_RATIO_CL[epoch-1]
# else:
# loss_ratio = p.LOSS_RATIO_NoCL
# if curriculum['seq']:
# start_seq = int(p.START_SEQ_CL[epoch-1])
# end_seq = int(p.END_SEQ_CL[epoch-1])
# else:
# start_seq = 0
# end_seq = p.SEQ_LEN-p.IN_SEQ_LEN+1
# # Training loop over batches of data on train dataset
# for batch_idx, data in enumerate(train_loader):
# data_occ = data['occ']
# data_mask = data['mask']
# data_v_x = data['v_x']
# data_v_y = data['v_y']
# label_x_fut = data['x_fut']
# label_y_fut = data['y_fut']
# ttlc_status=1 #???????????????????????????????????
# #print('Batch: ', batch_idx)
# start = time()
# if (p.vector_field_available):
# if model.model_type in ['pretrained_denseNet', 'pretrainedResnetModel', 'Resnet_LSTM']:
# data_tuple = torch.cat((data_occ + data_mask, data_v_x, data_v_y), 1).to(device)
# else:
# data_tuple = torch.cat((data_occ, data_v_x, data_v_y, data_mask), 1).to(device)
# else:
# if model.model_type in ['pretrained_denseNet', 'pretrainedResnetModel', 'Resnet_LSTM']:
# data_tuple = torch.cat((data_occ + data_mask, data_occ + data_mask, data_occ + data_mask), 1).to(device)
# else:
# data_tuple = torch.cat((data_occ, data_mask), 1).to(device)
# labels = torch.cat((label_x_fut, label_y_fut), 1).to(device)
# # data_tuple = [data.to(device) for data in data_tuple]
# # labels = labels.to(device)
# # ttlc_status = ttlc_status.to(device)
# #start_point = random.randint(0,p.TR_JUMP_STEP)
# for seq_itr in range(start_seq,end_seq, p.TR_JUMP_STEP):
# # current_data = [data[:, seq_itr:(seq_itr+p.IN_SEQ_LEN)] for data in data_tuple]
# # 1. Clearing previous gradient values.
# optimizer.zero_grad()
# if model.__class__.__name__ == 'VanillaLSTM':
# model.init_hidden()
# # 2. feeding data to model (forward method will be computed)
# output_dict = model(data_tuple, labels)
# # lc_pred = output_dict['lc_pred']
# if(model.model_type == 'probabilistic'):
# ttlc_pred_x, ttlc_pred_y, ttlc_pred_sigmaX, ttlc_pred_sigmaY, ttlc_pred_rho = output_dict['ttlc_pred']
# else:
# ttlc_pred_x, ttlc_pred_y = output_dict['ttlc_pred']
# # 3. Calculating the loss value
# # if task == params.CLASSIFICATION or task == params.DUAL:
# # lc_loss = lc_loss_func(lc_pred, labels)
# # else:
# # lc_loss = 0
# if task == params.REGRESSION or task == params.DUAL:
# ttlc_label = torch.FloatTensor([(p.SEQ_LEN-seq_itr-p.IN_SEQ_LEN+1)/p.FPS])\
# .unsqueeze(0).expand(*ttlc_pred_x.size()).requires_grad_().to(device)
# # ttlc_notavailable = (ttlc_status == 0).to(torch.float).unsqueeze(-1) ###prev verion =>label ==0
# # ttlc_available = (ttlc_status == 1).to(torch.float).unsqueeze(-1)
# ttlc_notavailable = torch.tensor(ttlc_status == 0, dtype=torch.float32, device=device).unsqueeze(-1) ###prev verion =>label ==0
# ttlc_available = torch.tensor(ttlc_status == 1, dtype=torch.float32, device=device).unsqueeze(-1) ###prev verion =>label ==0
# ttlc_pred_x = ttlc_pred_x * ttlc_available + ttlc_label * ttlc_notavailable
# ttlc_pred_y = ttlc_pred_y * ttlc_available + ttlc_label * ttlc_notavailable
# if(model.model_type == 'probabilistic'):
# # loss = ttlc_loss_func(ttlc_pred_x, ttlc_pred_y, ttlc_pred_sigmaX, ttlc_pred_sigmaY, ttlc_pred_rho,
# # labels[:,0,:], labels[:,1,:]) # distance loss
# loss = ttlc_loss_func(torch.cumsum(ttlc_pred_x, dim=-1), torch.cumsum(ttlc_pred_y, dim=-1),
# ttlc_pred_sigmaX, ttlc_pred_sigmaY, ttlc_pred_rho,
# torch.cumsum(labels[:,0,:], dim=-1), torch.cumsum(labels[:,1,:], dim=-1)) # distance loss
# else:
# # ttlc_loss_x = ttlc_loss_func(torch.cumsum(ttlc_pred_x, dim=-1), torch.cumsum(labels[:,0,:], dim=-1)) # distance loss
# # ttlc_loss_y = ttlc_loss_func(torch.cumsum(ttlc_pred_y, dim=-1), torch.cumsum(labels[:,1,:], dim=-1))
# ttlc_loss_x = ttlc_loss_func(ttlc_pred_x,labels[:,0,:]) # velocity loss
# ttlc_loss_y = ttlc_loss_func(ttlc_pred_y,labels[:,1,:])
# loss = ttlc_loss_x*(1-p.loss_x_alpha) + ttlc_loss_y*(1+p.loss_x_alpha)
# else:
# ttlc_loss = 0
# # 4. Calculating new grdients given the loss value
# loss.backward()
# # 5. Updating the weights
# optimizer.step()
# avg_loss += loss.data/(len(train_loader))
# with torch.no_grad():
# traj_rmse = calc_rmse_metrics(p, torch.cat((ttlc_pred_x.unsqueeze(1),ttlc_pred_y.unsqueeze(1)), dim=1).cpu().numpy(),
# labels.cpu().numpy()) # distance loss
# avg_rmse_loss += traj_rmse/(len(train_loader))
# if (batch_idx+1) % 100 == 0:
# print('Epoch: ',epoch, ' Batch: ', batch_idx+1,
# ' Training MSE Loss: ', avg_loss.cpu().numpy(), ' Training RMSE Loss: ',avg_rmse_loss )
# avg_loss = 0
# avg_rmse_loss = 0
# end = time()
# model_time += end-start
# all_end = time()
# all_time = all_end - all_start
# print('model time: ', model_time, 'all training time: ', all_time)
# scheduler.step()
# all_preds = np.zeros(((num_batch*model.batch_size), p.SEQ_LEN-p.IN_SEQ_LEN+1, 3))
# all_labels = np.zeros((num_batch*model.batch_size))
# # Validation Phase on train dataset
# if calc_train_acc == True:
# raise('Depricated')
# def eval_model(p, model, ttlc_loss_func, test_loader, epoch, device):
# total = len(test_loader.dataset)
# # number of batch
# num_batch = int(np.floor(total/model.batch_size))
# avg_loss = 0
# avg_lc_loss = 0
# avg_ttlc_loss = 0
# # all_lc_preds = np.zeros(((num_batch*model.batch_size), int((p.SEQ_LEN-p.IN_SEQ_LEN+1)/p.RES) ,3))
# all_att_coef = np.zeros(((num_batch*model.batch_size), int((p.SEQ_LEN-p.IN_SEQ_LEN+1)/p.RES) ,4))
# # all_ttlc_preds_x = np.zeros(((num_batch*model.batch_size), p.SEQ_LEN-p.IN_SEQ_LEN+1,1))
# all_ttlc_preds = np.zeros(((num_batch*model.batch_size), 2, int((p.SEQ_LEN-p.IN_SEQ_LEN+1)/p.RES)))
# # all_ttlc_preds = []
# # all_ttlc_preds_y = []
# all_labels = np.zeros(((num_batch*model.batch_size, 2, int((p.SEQ_LEN-p.IN_SEQ_LEN+1)/p.RES))))
# # all_labels = []
# # plot_dicts = []
# time_counter = 0
# average_time = 0
# gf_time = 0
# nn_time = 0
# loss_ratio = 1
# avg_loss = 0
# for batch_idx, data in enumerate(test_loader):
# data_occ = data['occ']
# data_mask = data['mask']
# data_v_x = data['v_x']
# data_v_y = data['v_y']
# label_x_fut = data['x_fut']
# label_y_fut = data['y_fut']
# plot_info = None #???????????????????????????????????
# ttlc_status=1 #???????????????????????????????????
# #print('Batch: ', batch_idx)
# start = time()
# if (p.vector_field_available):
# if model.model_type in ['pretrained_denseNet', 'pretrainedResnetModel', 'Resnet_LSTM']:
# data_tuple = torch.cat((data_occ + data_mask, data_v_x, data_v_y), 1).to(device)
# else:
# data_tuple = torch.cat((data_occ, data_v_x, data_v_y, data_mask), 1).to(device)
# else:
# if model.model_type in ['pretrained_denseNet', 'pretrainedResnetModel', 'Resnet_LSTM']:
# data_tuple = torch.cat((data_occ+data_mask, data_occ+data_mask, data_occ+data_mask), 1).to(device)
# else:
# data_tuple = torch.cat((data_occ, data_mask), 1).to(device)
# labels = torch.cat((label_x_fut, label_y_fut), 1).to(device)
# #print(batch_idx, total)
# all_labels[(batch_idx*model.batch_size):((batch_idx+1)*model.batch_size)] = labels.data.cpu().numpy()
# # all_labels.append(labels.data)
# st_time = time()
# if model.model_type in ['CNN-LSTM-v3', 'CNN-LSTM-v5']:
# output_dict = model.test(data_tuple)
# else:
# output_dict = model.eval()(data_tuple, labels)
# end_time = time()-st_time
# # lc_pred = output_dict['lc_pred']
# # ttlc_pred_x, ttlc_pred_y = output_dict['ttlc_pred']
# if(model.model_type in ['probabilistic', 'Prob-CNN-LSTM-v4', 'Prob-CNN-LSTM-v6']):
# ttlc_pred_x, ttlc_pred_y, ttlc_pred_sigmaX, ttlc_pred_sigmaY, ttlc_pred_rho = output_dict['ttlc_pred']
# else:
# ttlc_pred_x, ttlc_pred_y = output_dict['ttlc_pred'][:2]
# ttlc_pred = torch.cat((ttlc_pred_x.unsqueeze(1), ttlc_pred_y.unsqueeze(1)),1)
# all_ttlc_preds[(batch_idx*model.batch_size):((batch_idx+1)*model.batch_size), :, :] = ttlc_pred.cpu().data
# # all_ttlc_preds_y[(batch_idx*model.batch_size):((batch_idx+1)*model.batch_size), seq_itr] = ttlc_pred_y.cpu().data
# # all_ttlc_preds.append(ttlc_pred.cpu().data)
# # all_ttlc_preds_y.append(ttlc_pred_y.cpu().data)
# if p.SELECTED_MODEL == 'REGIONATTCNN3':
# all_att_coef[(batch_idx*model.batch_size):((batch_idx+1)*model.batch_size), :, :] = output_dict['attention'].cpu().data
# with torch.no_grad():
# if(model.model_type in ['probabilistic', 'Prob-CNN-LSTM-v4', 'Prob-CNN-LSTM-v6']):
# loss = ttlc_loss_func(ttlc_pred_x, ttlc_pred_y, ttlc_pred_sigmaX, ttlc_pred_sigmaY, ttlc_pred_rho,
# labels[:,0,:], labels[:,1,:]) # distance loss
# # loss = ttlc_loss_func(torch.cumsum(ttlc_pred_x, dim=-1), torch.cumsum(ttlc_pred_y, dim=-1),
# # ttlc_pred_sigmaX, ttlc_pred_sigmaY, ttlc_pred_rho,
# # torch.cumsum(labels[:,0,:], dim=-1), torch.cumsum(labels[:,1,:], dim=-1)) # distance loss
# else:
# # ttlc_loss_x = ttlc_loss_func(torch.cumsum(ttlc_pred_x, dim=-1), torch.cumsum(labels[:,0,:], dim=-1)) # distance loss
# # ttlc_loss_y = ttlc_loss_func(torch.cumsum(ttlc_pred_y, dim=-1), torch.cumsum(labels[:,1,:], dim=-1))
# ttlc_loss_x = ttlc_loss_func(ttlc_pred_x,labels[:,0,:]) # velocity loss
# ttlc_loss_y = ttlc_loss_func(ttlc_pred_y,labels[:,1,:])
# loss = ttlc_loss_x*(1-p.loss_x_alpha) + ttlc_loss_y*(1+p.loss_x_alpha)
# avg_loss += loss.data/(len(test_loader))
# time_counter += 1
# average_time +=end_time
# # if eval_type == 'Test':
# # plot_dicts.append(plot_dict)
# #print('Average Time per whole sequence perbatch: {}'.format(average_time/time_counter))
# #print('gf time: {}, nn time: {}'.format(gf_time, nn_time))
# # all_labels = torch.vstack(all_labels).cpu().numpy()
# # all_ttlc_preds = torch.vstack(all_ttlc_preds).cpu().numpy()
# # all_ttlc_preds_y = torch.vstack(all_ttlc_preds_y).cpu().numpy()
# # avg_ttlc_loss = calc_metric(p, task, all_lc_preds, all_ttlc_preds
# # , all_att_coef, all_labels, epoch, eval_type =\
# # eval_type, figure_name = figure_name)
# avg_ttlc_loss = calc_metric(p, all_ttlc_preds, all_labels)
# # avg_loss = avg_ttlc_loss + avg_lc_loss
# # print("{}: Epoch: {}, Accuracy: {:.2f}%, Robust Prediction Time: {:.2f}, Prediction Time: {:.2f}, Total LOSS: {:.2f},LC LOSS: {:.2f},TTLC LOSS: {:.2f}, PRECISION:{}, RECALL:{}, F1:{}, FPR:{}, AUC:{}, Max J:{}".format(
# # eval_type, epoch, 100. * accuracy, robust_pred_time, pred_time, avg_loss, avg_lc_loss, avg_ttlc_loss, precision, recall, f1, FPR, auc, max_j))
# print("Epoch: {} \ttraj MSE loss:{:.4f} \ttraj RMSE loss: {:.4f}".format(epoch, avg_loss, avg_ttlc_loss))
# # if eval_type == 'Test':
# # with open(vis_data_path, "wb") as fp:
# # pickle.dump(plot_dicts, fp)
# # return robust_pred_time, pred_time, accuracy, avg_loss, avg_lc_loss, avg_ttlc_loss, auc, max_j, precision, recall, f1
# return avg_ttlc_loss
# if eval_type == 'Test':
# plot_dict = {
# 'tv': plot_info[0].numpy(),
# 'frames': plot_info[1].numpy(),
# 'preds':np.zeros((plot_info[1].shape[0], plot_info[1].shape[1], 3)),
# 'ttlc_preds': np.zeros((plot_info[1].shape[0], plot_info[1].shape[1])),
# 'att_coef': np.zeros((plot_info[1].shape[0], plot_info[1].shape[1], 4)),
# 'att_mask': np.zeros((plot_info[1].shape[0], plot_info[1].shape[1], 11, 26)),
# 'labels':labels.numpy(),
# 'data_file': plot_info[2]
# }
# # data_tuple = [data.to(device) for data in data_tuple]
# labels = labels.to(device)
# #ttlc_status = ttlc_status.to(device)
# for seq_itr in range(0,p.SEQ_LEN-p.IN_SEQ_LEN+1):
# if model.__class__.__name__ == 'VanillaLSTM':
# model.init_hidden()
# # current_data = [data[:, seq_itr:(seq_itr+p.IN_SEQ_LEN)] for data in data_tuple]
# st_time = time()
# output_dict = model(data_tuple)
# end_time = time()-st_time
# lc_pred = output_dict['lc_pred']
# ttlc_pred_x, ttlc_pred_y = output_dict['ttlc_pred']
# ttlc_pred = torch.cat((ttlc_pred_x.unsqueeze(1), ttlc_pred_y.unsqueeze(1)),1)
# if task == params.CLASSIFICATION or task == params.DUAL:
# lc_loss = lc_loss_func(lc_pred, labels)
# else:
# lc_loss = 0
# #_ , pred_labels = output.data.max(dim=1)
# #pred_labels = pred_labels.cpu()
# if eval_type == 'Test':
# if task == params.CLASSIFICATION or task == params.DUAL:
# plot_dict['preds'][:,p.IN_SEQ_LEN-1+seq_itr,:] = F.softmax(lc_pred, dim = -1).cpu().data
# if task == params.REGRESSION or task == params.DUAL:
# plot_dict['ttlc_preds'][:,p.IN_SEQ_LEN-1+seq_itr] = np.squeeze(ttlc_pred.cpu().detach().numpy(), -1)
# if 'REGIONATT' in p.SELECTED_MODEL:
# plot_dict['att_coef'][:,p.IN_SEQ_LEN-1+seq_itr,:] = output_dict['attention'].cpu().data
# if task == params.REGRESSION or task == params.DUAL:
# all_ttlc_preds[(batch_idx*model.batch_size):((batch_idx+1)*model.batch_size), seq_itr] = ttlc_pred.cpu().data
# # all_ttlc_preds_y[(batch_idx*model.batch_size):((batch_idx+1)*model.batch_size), seq_itr] = ttlc_pred_y.cpu().data
# # all_ttlc_preds.append(ttlc_pred.cpu().data)
# # all_ttlc_preds_y.append(ttlc_pred_y.cpu().data)
# if task == params.CLASSIFICATION or task == params.DUAL:
# all_lc_preds[(batch_idx*model.batch_size):((batch_idx+1)*model.batch_size), seq_itr] = F.softmax(lc_pred, dim = -1).cpu().data
# avg_lc_loss = avg_lc_loss + lc_loss.cpu().data / (len(test_loader)*(p.SEQ_LEN-p.IN_SEQ_LEN))
# if p.SELECTED_MODEL == 'REGIONATTCNN3':
# all_att_coef[(batch_idx*model.batch_size):((batch_idx+1)*model.batch_size), seq_itr] = output_dict['attention'].cpu().data
# time_counter += 1
# average_time +=end_time
# if eval_type == 'Test':
# plot_dicts.append(plot_dict)
# #print('Average Time per whole sequence perbatch: {}'.format(average_time/time_counter))
# #print('gf time: {}, nn time: {}'.format(gf_time, nn_time))
# all_labels = torch.vstack(all_labels).cpu().numpy()
# all_ttlc_preds = torch.vstack(all_ttlc_preds).cpu().numpy()
# # all_ttlc_preds_y = torch.vstack(all_ttlc_preds_y).cpu().numpy()
# avg_ttlc_loss, robust_pred_time, pred_time, accuracy, precision, recall, f1, FPR, auc, max_j =\
# calc_metric(p, task, all_lc_preds, all_ttlc_preds
# , all_att_coef, all_labels, epoch, eval_type =\
# eval_type, figure_name = figure_name)
# avg_loss = avg_ttlc_loss + avg_lc_loss
# print("{}: Epoch: {}, Accuracy: {:.2f}%, Robust Prediction Time: {:.2f}, Prediction Time: {:.2f}, Total LOSS: {:.2f},LC LOSS: {:.2f},TTLC LOSS: {:.2f}, PRECISION:{}, RECALL:{}, F1:{}, FPR:{}, AUC:{}, Max J:{}".format(
# eval_type, epoch, 100. * accuracy, robust_pred_time, pred_time, avg_loss, avg_lc_loss, avg_ttlc_loss, precision, recall, f1, FPR, auc, max_j))
# if eval_type == 'Test':
# with open(vis_data_path, "wb") as fp:
# pickle.dump(plot_dicts, fp)
# return robust_pred_time, pred_time, accuracy, avg_loss, avg_lc_loss, avg_ttlc_loss, auc, max_j, precision, recall, f1
# def calc_metric(p, task, all_lc_preds, all_ttlc_preds,
# all_att_coef, all_labels, epoch=None, eval_type = 'Test', figure_name= None):
def calc_metric(p, all_ttlc_preds, all_labels):
# num_samples = all_labels.shape[0]
# prediction_seq = p.SEQ_LEN-p.IN_SEQ_LEN+1
# all_preds = np.argmax(all_lc_preds, axis =-1)
# if eval_type == 'Test':
# plot_att_graphs(p, all_att_coef, prediction_seq, all_labels, all_preds, figure_name)
# if task == params.CLASSIFICATION or task == params.DUAL:
# auc, max_j = calc_roc_n_prc(p, all_lc_preds, all_labels, all_ttlc_preds, prediction_seq, num_samples, figure_name, thr_type = 'thr', eval_type = eval_type)
# accuracy, precision, recall, f1, FPR, all_TPs = calc_classification_metrics(p, all_preds, all_labels, all_ttlc_preds, prediction_seq, num_samples, eval_type, figure_name)
# robust_pred_time, pred_time = calc_avg_pred_time(p, all_TPs, all_labels, prediction_seq, num_samples)
# else:
# (accuracy, precision, recall, f1, FPR, all_TPs, auc, max_j, robust_pred_time, pred_time) = (0,0,0,0,0,0,0,0,0,0)
# avg_pred_time = 0
# if task == params.REGRESSION or task == params.DUAL:
rmse_loss = calc_rmse_metrics(p, all_ttlc_preds, all_labels)
# avg_ttlc_loss = calc_regression_metrics(p, all_ttlc_preds, all_labels, all_preds, prediction_seq, num_samples, eval_type, figure_name)
# else:
# avg_ttlc_loss = 0
# return rmse_loss, robust_pred_time, pred_time, accuracy, precision, recall, f1, FPR, auc, max_j
return rmse_loss
def plot_att_graphs(p, all_att_coef, prediction_seq, all_labels, all_preds, figure_name):
sum_att_coef = np.zeros((3,prediction_seq, 4))
count_att_coef = np.zeros((3,prediction_seq))
num_samples = all_att_coef.shape[0]
for i in range(num_samples):
for seq_itr in range(prediction_seq):
sum_att_coef[all_preds[i,seq_itr],seq_itr,:] += all_att_coef[i, seq_itr, :]
count_att_coef[all_preds[i,seq_itr],seq_itr] += 1
for i in range(3):
for seq_itr in range(prediction_seq):
sum_att_coef[i,seq_itr] = sum_att_coef[i,seq_itr]/(count_att_coef[i,seq_itr]+ 0.000000001)
''''''
names = ['LK', 'RLC', 'LLC']
# Creating dirs
figure_dir = os.path.join(p.FIGS_DIR, 'FR_att')
if not os.path.exists(figure_dir):
os.mkdir(figure_dir)
plot_dir = os.path.join(figure_dir, figure_name +p.unblanaced_ext+ '.png')
fig_obs_dir = os.path.join(figure_dir, figure_name +p.unblanaced_ext+ '.pickle')
att_ax = plt.figure()
ttlc_seq = (prediction_seq-np.arange(prediction_seq))/p.FPS
# Creating Figs
plt.plot(ttlc_seq, sum_att_coef[0,:,0], label = names[0], linewidth=5)
plt.plot(ttlc_seq, sum_att_coef[1,:,0], label = names[1], linewidth=5)
plt.plot(ttlc_seq, sum_att_coef[2,:,0], label = names[2], linewidth=5)
plt.xlim(ttlc_seq[0], ttlc_seq[-1])
att_ax.legend()
plt.xlabel('TTLC (s)')
plt.ylabel('Average Attention Coefficient')
plt.grid()
att_ax.savefig(plot_dir)
with open(fig_obs_dir, 'wb') as fid:
pickle.dump(att_ax, fid)
# Creating dirs
figure_dir = os.path.join(p.FIGS_DIR, 'BR_att')
if not os.path.exists(figure_dir):
os.mkdir(figure_dir)
plot_dir = os.path.join(figure_dir, figure_name +p.unblanaced_ext+ '.png')
fig_obs_dir = os.path.join(figure_dir, figure_name +p.unblanaced_ext+ '.pickle')
att_ax = plt.figure()
ttlc_seq = (prediction_seq-np.arange(prediction_seq))/p.FPS
# Creating Figs
plt.plot(ttlc_seq, sum_att_coef[0,:,1], label = names[0], linewidth=5)
plt.plot(ttlc_seq, sum_att_coef[1,:,1], label = names[1], linewidth=5)
plt.plot(ttlc_seq, sum_att_coef[2,:,1], label = names[2], linewidth=5)
plt.xlim(ttlc_seq[0], ttlc_seq[-1])
att_ax.legend()
plt.xlabel('TTLC (s)')
plt.ylabel('Average Attention Coefficient')
plt.grid()
att_ax.savefig(plot_dir)
with open(fig_obs_dir, 'wb') as fid:
pickle.dump(att_ax, fid)
# Creating dirs
figure_dir = os.path.join(p.FIGS_DIR, 'FL_att')
if not os.path.exists(figure_dir):
os.mkdir(figure_dir)
plot_dir = os.path.join(figure_dir, figure_name +p.unblanaced_ext+ '.png')
fig_obs_dir = os.path.join(figure_dir, figure_name +p.unblanaced_ext+ '.pickle')
att_ax = plt.figure()
ttlc_seq = (prediction_seq-np.arange(prediction_seq))/p.FPS
# Creating Figs
plt.plot(ttlc_seq, sum_att_coef[0,:,2], label = names[0], linewidth=5)
plt.plot(ttlc_seq, sum_att_coef[1,:,2], label = names[1], linewidth=5)
plt.plot(ttlc_seq, sum_att_coef[2,:,2], label = names[2], linewidth=5)
plt.xlim(ttlc_seq[0], ttlc_seq[-1])
att_ax.legend()
plt.xlabel('TTLC (s)')
plt.ylabel('Average Attention Coefficient')
plt.grid()
att_ax.savefig(plot_dir)
with open(fig_obs_dir, 'wb') as fid:
pickle.dump(att_ax, fid)
# Creating dirs
figure_dir = os.path.join(p.FIGS_DIR, 'BL_att')
if not os.path.exists(figure_dir):
os.mkdir(figure_dir)
plot_dir = os.path.join(figure_dir, figure_name +p.unblanaced_ext+ '.png')
fig_obs_dir = os.path.join(figure_dir, figure_name +p.unblanaced_ext+ '.pickle')
att_ax = plt.figure()
ttlc_seq = (prediction_seq-np.arange(prediction_seq))/p.FPS
# Creating Figs
plt.plot(ttlc_seq, sum_att_coef[0,:,3], label = names[0], linewidth=5)
plt.plot(ttlc_seq, sum_att_coef[1,:,3], label = names[1], linewidth=5)
plt.plot(ttlc_seq, sum_att_coef[2,:,3], label = names[2], linewidth=5)
plt.xlim(ttlc_seq[0], ttlc_seq[-1])
att_ax.legend()
plt.xlabel('TTLC (s)')
plt.ylabel('Average Attention Coefficient')
plt.grid()
att_ax.savefig(plot_dir)
with open(fig_obs_dir, 'wb') as fid:
pickle.dump(att_ax, fid)
def save_plot_model_prediction(p, model, dataloader, device, epoch_number, save_dir):
try:
batch_data = next(dataloader)
except:
data_iter = iter(dataloader)
batch_data = next(data_iter)
batch_data_occ = batch_data['occ']
batch_data_mask = batch_data['mask']
batch_data_v_x = batch_data['v_x']
batch_data_v_y = batch_data['v_y']
batch_label_x_fut = batch_data['x_fut']
batch_label_y_fut = batch_data['y_fut']
batch_data_x_init = batch_data['x_init']
batch_data_y_init = batch_data['y_init']
if (p.vector_field_available):
if model.model_type in ['pretrained_denseNet', 'pretrainedResnetModel', 'Resnet_LSTM', 'Prob-CNN-LSTM-v6']:
data_tuple = torch.cat((batch_data_occ + batch_data_mask, batch_data_v_x, batch_data_v_y), 1).to(device)
else:
data_tuple = torch.cat((batch_data_occ, batch_data_mask, batch_data_v_x, batch_data_v_y), 1).to(device)
data_norm = tf.Normalize((4.9260162e-02, 6.9080950e-03, -1.3276902e+00, 1.2969130e-05), (0.21641071, 0.08282737, 5.9344087, 0.00651047))
else:
if model.model_type in ['pretrained_denseNet', 'pretrainedResnetModel', 'Resnet_LSTM', 'Prob-CNN-LSTM-v6']:
data_tuple = torch.cat((batch_data_occ+batch_data_mask, batch_data_occ+batch_data_mask, batch_data_occ+batch_data_mask), 1).to(device)
else:
data_tuple = torch.cat((batch_data_occ, batch_data_mask), 1).to(device)
labels = torch.cat((batch_label_x_fut, batch_label_y_fut), 1).to(device)
label_norm = tf.Normalize((-5.6779733e+00, -1.0209690e-03), (1.0219698, 0.03529745))
if(p.input_normalization):
data_tuple = data_norm(data_tuple.permute(0,2,1,3,4)).permute(0,2,1,3,4)
if(p.output_normalization):
labels = label_norm(labels.permute(1,0,2)).permute(1,0,2)
if model.model_type in ['CNN-LSTM-v3', 'CNN-LSTM-v5', 'CNN-LSTM-v7']:
output_dict = model.test(data_tuple)
else:
output_dict = model.eval()(data_tuple, labels)
batch_traj_pred_x_all, batch_traj_pred_y = output_dict['ttlc_pred'][:2]
# if(p.output_normalization):
# batch_traj_pred_x_all = DeNormalizing(batch_traj_pred_x_all, mean=-5.6779733e+00, std=1.0219698)
# batch_traj_pred_y_all = DeNormalizing(batch_traj_pred_y_all, mean=-1.0209690e-03, std=0.03529745)
for batch_idx in range(30): #batch_data_occ.shape[0]
data_occ = batch_data_occ[batch_idx].squeeze(0)
data_mask = batch_data_mask[batch_idx].squeeze(0)
data_v_x = batch_data_v_x[batch_idx].squeeze(0)
data_v_y = batch_data_v_y[batch_idx].squeeze(0)
label_x_fut = batch_label_x_fut[batch_idx].squeeze(0)
label_y_fut = batch_label_y_fut[batch_idx].squeeze(0)
data_x_init = batch_data_x_init[batch_idx].squeeze(0)
data_y_init = batch_data_y_init[batch_idx].squeeze(0)
traj_pred_x = batch_traj_pred_x_all[batch_idx].cpu().detach().numpy()
traj_pred_y = batch_traj_pred_y[batch_idx].cpu().detach().numpy()
fig, ax = plt.subplots(3,1)
ax[0].imshow(data_occ[-1])
# ax[0].invert_yaxis()
# ax[1].imshow(data_mask)
x,y = np.meshgrid(np.arange(0, data_occ[-1].shape[1], 1), np.arange(0, data_occ[-1].shape[0], 1))
ax[2].axis('equal')
q = ax[2].quiver(x,y, data_v_x[-1].squeeze(0), data_v_y[-1].squeeze(0))
# ax[3].plot(label_x_fut*0.64, label_y_fut*4, '--', color='lightgrey', label='Ground Truth')
ax[1].plot((data_x_init[0]+np.cumsum(np.insert(label_x_fut, 0,0)))*0.64,
(data_y_init[0]+np.cumsum(np.insert(label_y_fut, 0, 0)))*4,
'-', color='lightgrey', label='Ground Truth', marker='.')
ax[1].plot((data_x_init[0]+np.cumsum(np.insert(traj_pred_x, 0,0)))*0.64,
(data_y_init[0]+np.cumsum(np.insert(traj_pred_y, 0, 0)))*4,
'-', color='red', label='Prediction', marker='.')
pred = torch.stack([
data_x_init[0]+np.cumsum(np.insert(traj_pred_x, 0,0)),
data_y_init[0]+np.cumsum(np.insert(traj_pred_y, 0,0))])
gtr = torch.stack([
data_x_init[0]+np.cumsum(np.insert(label_x_fut, 0,0)),
data_y_init[0]+np.cumsum(np.insert(label_y_fut, 0,0))])
ax[1].set_title('RMSE:{:.4f} '.format(torch.sqrt(torch.sum((pred-gtr)**2)/pred.shape[1])), fontsize=18)
# ax[3].plot((data_x_init[0]+np.cumsum(np.insert(traj_pred_x, 0,0)))*0.64,
# (data_y_init[0]+np.cumsum(np.insert(traj_pred_y, 0, 0)))*4,
# '--', color='red', label='Prediction')
ax[1].imshow(data_mask[-1])
# ax[4].plot(traj_pred_x*1.28, traj_pred_y*4, '--', color='red', label='Prediction')
custom_xlim = (0, data_occ[-1].shape[1])
custom_ylim = (0, data_occ[-1].shape[0])
# Setting the values for all axes.
plt.setp(ax, xlim=custom_xlim, ylim=custom_ylim)
plt.savefig(save_dir+'epoch_{}'.format(epoch_number)+ '_batch_{}'.format(batch_idx)+'.png')
plt.close(fig)
def calc_roc_n_prc(p, all_lc_preds, all_labels, all_ttlc_preds, prediction_seq, num_samples, figure_name, thr_type, eval_type):
if thr_type == 'thr':
thr_range = np.arange(0,101,1)/100
precision_vs_thr = np.zeros_like(thr_range)
recall_vs_thr = np.zeros_like(thr_range)
fpr_vs_thr = np.zeros_like(thr_range)
for i,thr in enumerate(thr_range):
all_lc_preds_thr = all_lc_preds>=thr
all_lc_preds_thr[:,:,0] = np.logical_not(np.logical_or(all_lc_preds_thr[:,:,1],all_lc_preds_thr[:,:,2]))
all_pred = []
all_pred.append(all_lc_preds_thr[:,:,0] * all_lc_preds[:,:,0]+ np.logical_not(all_lc_preds_thr[:,:,0]) * -1)# -1 is to make sure when thr is 0 non of lc is selected in argemax
all_pred.append(all_lc_preds_thr[:,:,1] * all_lc_preds[:,:,1])
all_pred.append(all_lc_preds_thr[:,:,2] * all_lc_preds[:,:,2])
all_pred = np.stack(all_pred, axis = -1)
all_pred = np.argmax(all_pred, axis =-1)
precision_vs_thr[i], recall_vs_thr[i], fpr_vs_thr[i] = calc_prec_recall_fpr(p, all_pred, all_labels, prediction_seq, num_samples)
else:
raise('Unknown thr type')
recall_vs_thr = np.flip(recall_vs_thr)
fpr_vs_thr = np.flip(fpr_vs_thr)