forked from jagjeet-singh/argoverse-forecasting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lstm_train_test.py
859 lines (703 loc) · 26.9 KB
/
lstm_train_test.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
"""lstm_train_test.py runs the LSTM baselines training/inference on forecasting dataset.
Note: The training code for these baselines is covered under the patent <PATENT_LINK>.
Example usage:
python lstm_train_test.py
--model_path saved_models/lstm.pth.tar
--test_features ../data/forecasting_data_test.pkl
--train_features ../data/forecasting_data_train.pkl
--val_features ../data/forecasting_data_val.pkl
--use_delta --normalize
"""
import os
import shutil
import tempfile
import time
from typing import Any, Dict, List, Tuple, Union
import argparse
import joblib
from joblib import Parallel, delayed
import numpy as np
import pickle as pkl
from termcolor import cprint
import torch
import torch.nn as nn
import torch.nn.functional as F
from logger import Logger
import utils.baseline_config as config
import utils.baseline_utils as baseline_utils
from utils.lstm_utils import ModelUtils, LSTMDataset
use_cuda = torch.cuda.is_available()
if use_cuda:
device = torch.device("cuda")
else:
device = torch.device("cpu")
global_step = 0
best_loss = float("inf")
np.random.seed(100)
ROLLOUT_LENS = [1, 10, 30]
def parse_arguments() -> Any:
"""Arguments for running the baseline.
Returns:
parsed arguments
"""
parser = argparse.ArgumentParser()
parser.add_argument("--test_batch_size",
type=int,
default=512,
help="Test batch size")
parser.add_argument("--model_path",
required=False,
type=str,
help="path to the saved model")
parser.add_argument("--obs_len",
default=20,
type=int,
help="Observed length of the trajectory")
parser.add_argument("--pred_len",
default=30,
type=int,
help="Prediction Horizon")
parser.add_argument(
"--normalize",
action="store_true",
help="Normalize the trajectories if non-map baseline is used",
)
parser.add_argument(
"--use_delta",
action="store_true",
help="Train on the change in position, instead of absolute position",
)
parser.add_argument(
"--train_features",
default="",
type=str,
help="path to the file which has train features.",
)
parser.add_argument(
"--val_features",
default="",
type=str,
help="path to the file which has val features.",
)
parser.add_argument(
"--test_features",
default="",
type=str,
help="path to the file which has test features.",
)
parser.add_argument(
"--joblib_batch_size",
default=100,
type=int,
help="Batch size for parallel computation",
)
parser.add_argument("--use_map",
action="store_true",
help="Use the map based features")
parser.add_argument("--use_social",
action="store_true",
help="Use social features")
parser.add_argument("--test",
action="store_true",
help="If true, only run the inference")
parser.add_argument("--train_batch_size",
type=int,
default=512,
help="Training batch size")
parser.add_argument("--val_batch_size",
type=int,
default=512,
help="Val batch size")
parser.add_argument("--end_epoch",
type=int,
default=5000,
help="Last epoch")
parser.add_argument("--lr",
type=float,
default=0.001,
help="Learning rate")
parser.add_argument(
"--traj_save_path",
required=False,
type=str,
help=
"path to the pickle file where forecasted trajectories will be saved.",
)
return parser.parse_args()
class EncoderRNN(nn.Module):
"""Encoder Network."""
def __init__(self,
input_size: int = 2,
embedding_size: int = 8,
hidden_size: int = 16):
"""Initialize the encoder network.
Args:
input_size: number of features in the input
embedding_size: Embedding size
hidden_size: Hidden size of LSTM
"""
super(EncoderRNN, self).__init__()
self.hidden_size = hidden_size
self.linear1 = nn.Linear(input_size, embedding_size)
self.lstm1 = nn.LSTMCell(embedding_size, hidden_size)
def forward(self, x: torch.FloatTensor, hidden: Any) -> Any:
"""Run forward propagation.
Args:
x: input to the network
hidden: initial hidden state
Returns:
hidden: final hidden
"""
embedded = F.relu(self.linear1(x))
hidden = self.lstm1(embedded, hidden)
return hidden
class DecoderRNN(nn.Module):
"""Decoder Network."""
def __init__(self, embedding_size=8, hidden_size=16, output_size=2):
"""Initialize the decoder network.
Args:
embedding_size: Embedding size
hidden_size: Hidden size of LSTM
output_size: number of features in the output
"""
super(DecoderRNN, self).__init__()
self.hidden_size = hidden_size
self.linear1 = nn.Linear(output_size, embedding_size)
self.lstm1 = nn.LSTMCell(embedding_size, hidden_size)
self.linear2 = nn.Linear(hidden_size, output_size)
def forward(self, x, hidden):
"""Run forward propagation.
Args:
x: input to the network
hidden: initial hidden state
Returns:
output: output from lstm
hidden: final hidden state
"""
embedded = F.relu(self.linear1(x))
hidden = self.lstm1(embedded, hidden)
output = self.linear2(hidden[0])
return output, hidden
def train(
train_loader: Any,
epoch: int,
criterion: Any,
logger: Logger,
encoder: Any,
decoder: Any,
encoder_optimizer: Any,
decoder_optimizer: Any,
model_utils: ModelUtils,
rollout_len: int = 30,
) -> None:
"""Train the lstm network.
Args:
train_loader: DataLoader for the train set
epoch: epoch number
criterion: Loss criterion
logger: Tensorboard logger
encoder: Encoder network instance
decoder: Decoder network instance
encoder_optimizer: optimizer for the encoder network
decoder_optimizer: optimizer for the decoder network
model_utils: instance for ModelUtils class
rollout_len: current prediction horizon
"""
args = parse_arguments()
global global_step
for i, (_input, target, helpers) in enumerate(train_loader):
_input = _input.to(device)
target = target.to(device)
# Set to train mode
encoder.train()
decoder.train()
# Zero the gradients
encoder_optimizer.zero_grad()
decoder_optimizer.zero_grad()
# Encoder
batch_size = _input.shape[0]
input_length = _input.shape[1]
output_length = target.shape[1]
input_shape = _input.shape[2]
# Initialize encoder hidden state
encoder_hidden = model_utils.init_hidden(
batch_size,
encoder.module.hidden_size if use_cuda else encoder.hidden_size)
# Initialize losses
loss = 0
# Encode observed trajectory
for ei in range(input_length):
encoder_input = _input[:, ei, :]
encoder_hidden = encoder(encoder_input, encoder_hidden)
# Initialize decoder input with last coordinate in encoder
decoder_input = encoder_input[:, :2]
# Initialize decoder hidden state as encoder hidden state
decoder_hidden = encoder_hidden
decoder_outputs = torch.zeros(target.shape).to(device)
# Decode hidden state in future trajectory
for di in range(rollout_len):
decoder_output, decoder_hidden = decoder(decoder_input,
decoder_hidden)
decoder_outputs[:, di, :] = decoder_output
# Update loss
loss += criterion(decoder_output[:, :2], target[:, di, :2])
# Use own predictions as inputs at next step
decoder_input = decoder_output
# Get average loss for pred_len
loss = loss / rollout_len
# Backpropagate
loss.backward()
encoder_optimizer.step()
decoder_optimizer.step()
if global_step % 1000 == 0:
# Log results
print(
f"Train -- Epoch:{epoch}, loss:{loss}, Rollout:{rollout_len}")
logger.scalar_summary(tag="Train/loss",
value=loss.item(),
step=epoch)
global_step += 1
def validate(
val_loader: Any,
epoch: int,
criterion: Any,
logger: Logger,
encoder: Any,
decoder: Any,
encoder_optimizer: Any,
decoder_optimizer: Any,
model_utils: ModelUtils,
prev_loss: float,
decrement_counter: int,
rollout_len: int = 30,
) -> Tuple[float, int]:
"""Validate the lstm network.
Args:
val_loader: DataLoader for the train set
epoch: epoch number
criterion: Loss criterion
logger: Tensorboard logger
encoder: Encoder network instance
decoder: Decoder network instance
encoder_optimizer: optimizer for the encoder network
decoder_optimizer: optimizer for the decoder network
model_utils: instance for ModelUtils class
prev_loss: Loss in the previous validation run
decrement_counter: keeping track of the number of consecutive times loss increased in the current rollout
rollout_len: current prediction horizon
"""
args = parse_arguments()
global best_loss
total_loss = []
for i, (_input, target, helpers) in enumerate(val_loader):
_input = _input.to(device)
target = target.to(device)
# Set to eval mode
encoder.eval()
decoder.eval()
# Encoder
batch_size = _input.shape[0]
input_length = _input.shape[1]
output_length = target.shape[1]
input_shape = _input.shape[2]
# Initialize encoder hidden state
encoder_hidden = model_utils.init_hidden(
batch_size,
encoder.module.hidden_size if use_cuda else encoder.hidden_size)
# Initialize loss
loss = 0
# Encode observed trajectory
for ei in range(input_length):
encoder_input = _input[:, ei, :]
encoder_hidden = encoder(encoder_input, encoder_hidden)
# Initialize decoder input with last coordinate in encoder
decoder_input = encoder_input[:, :2]
# Initialize decoder hidden state as encoder hidden state
decoder_hidden = encoder_hidden
decoder_outputs = torch.zeros(target.shape).to(device)
# Decode hidden state in future trajectory
for di in range(output_length):
decoder_output, decoder_hidden = decoder(decoder_input,
decoder_hidden)
decoder_outputs[:, di, :] = decoder_output
# Update losses for all benchmarks
loss += criterion(decoder_output[:, :2], target[:, di, :2])
# Use own predictions as inputs at next step
decoder_input = decoder_output
# Get average loss for pred_len
loss = loss / output_length
total_loss.append(loss)
if i % 10 == 0:
cprint(
f"Val -- Epoch:{epoch}, loss:{loss}, Rollout: {rollout_len}",
color="green",
)
# Save
val_loss = sum(total_loss) / len(total_loss)
if val_loss <= best_loss:
best_loss = val_loss
if args.use_map:
save_dir = "saved_models/lstm_map"
elif args.use_social:
save_dir = "saved_models/lstm_social"
else:
save_dir = "saved_models/lstm"
os.makedirs(save_dir, exist_ok=True)
model_utils.save_checkpoint(
save_dir,
{
"epoch": epoch + 1,
"rollout_len": rollout_len,
"encoder_state_dict": encoder.state_dict(),
"decoder_state_dict": decoder.state_dict(),
"best_loss": val_loss,
"encoder_optimizer": encoder_optimizer.state_dict(),
"decoder_optimizer": decoder_optimizer.state_dict(),
},
)
logger.scalar_summary(tag="Val/loss", value=val_loss.item(), step=epoch)
# Keep track of the loss to change preiction horizon
if val_loss <= prev_loss:
decrement_counter = 0
else:
decrement_counter += 1
return val_loss, decrement_counter
def infer_absolute(
test_loader: torch.utils.data.DataLoader,
encoder: EncoderRNN,
decoder: DecoderRNN,
start_idx: int,
forecasted_save_dir: str,
model_utils: ModelUtils,
):
"""Infer function for non-map LSTM baselines and save the forecasted trajectories.
Args:
test_loader: DataLoader for the test set
encoder: Encoder network instance
decoder: Decoder network instance
start_idx: start index for the current joblib batch
forecasted_save_dir: Directory where forecasted trajectories are to be saved
model_utils: ModelUtils instance
"""
args = parse_arguments()
forecasted_trajectories = {}
for i, (_input, target, helpers) in enumerate(test_loader):
_input = _input.to(device)
batch_helpers = list(zip(*helpers))
helpers_dict = {}
for k, v in config.LSTM_HELPER_DICT_IDX.items():
helpers_dict[k] = batch_helpers[v]
# Set to eval mode
encoder.eval()
decoder.eval()
# Encoder
batch_size = _input.shape[0]
input_length = _input.shape[1]
input_shape = _input.shape[2]
# Initialize encoder hidden state
encoder_hidden = model_utils.init_hidden(
batch_size,
encoder.module.hidden_size if use_cuda else encoder.hidden_size)
# Encode observed trajectory
for ei in range(input_length):
encoder_input = _input[:, ei, :]
encoder_hidden = encoder(encoder_input, encoder_hidden)
# Initialize decoder input with last coordinate in encoder
decoder_input = encoder_input[:, :2]
# Initialize decoder hidden state as encoder hidden state
decoder_hidden = encoder_hidden
decoder_outputs = torch.zeros(
(batch_size, args.pred_len, 2)).to(device)
# Decode hidden state in future trajectory
for di in range(args.pred_len):
decoder_output, decoder_hidden = decoder(decoder_input,
decoder_hidden)
decoder_outputs[:, di, :] = decoder_output
# Use own predictions as inputs at next step
decoder_input = decoder_output
# Get absolute trajectory
abs_helpers = {}
abs_helpers["REFERENCE"] = np.array(helpers_dict["DELTA_REFERENCE"])
abs_helpers["TRANSLATION"] = np.array(helpers_dict["TRANSLATION"])
abs_helpers["ROTATION"] = np.array(helpers_dict["ROTATION"])
abs_inputs, abs_outputs = baseline_utils.get_abs_traj(
_input.clone().cpu().numpy(),
decoder_outputs.detach().clone().cpu().numpy(),
args,
abs_helpers,
)
for i in range(abs_outputs.shape[0]):
seq_id = int(helpers_dict["SEQ_PATHS"][i])
forecasted_trajectories[seq_id] = [abs_outputs[i]]
with open(os.path.join(forecasted_save_dir, f"{start_idx}.pkl"),
"wb") as f:
pkl.dump(forecasted_trajectories, f)
def infer_map(
test_loader: torch.utils.data.DataLoader,
encoder: EncoderRNN,
decoder: DecoderRNN,
start_idx: int,
forecasted_save_dir: str,
model_utils: ModelUtils,
):
"""Infer function for map-based LSTM baselines and save the forecasted trajectories.
Args:
test_loader: DataLoader for the test set
encoder: Encoder network instance
decoder: Decoder network instance
start_idx: start index for the current joblib batch
forecasted_save_dir: Directory where forecasted trajectories are to be saved
model_utils: ModelUtils instance
"""
args = parse_arguments()
global best_loss
forecasted_trajectories = {}
for i, (_input, target, helpers) in enumerate(test_loader):
_input = _input.to(device)
batch_helpers = list(zip(*helpers))
helpers_dict = {}
for k, v in config.LSTM_HELPER_DICT_IDX.items():
helpers_dict[k] = batch_helpers[v]
# Set to eval mode
encoder.eval()
decoder.eval()
# Encoder
batch_size = _input.shape[0]
input_length = _input.shape[1]
# Iterate over every element in the batch
for batch_idx in range(batch_size):
num_candidates = len(
helpers_dict["CANDIDATE_CENTERLINES"][batch_idx])
curr_centroids = helpers_dict["CENTROIDS"][batch_idx]
seq_id = int(helpers_dict["SEQ_PATHS"][batch_idx])
abs_outputs = []
# Predict using every centerline candidate for the current trajectory
for candidate_idx in range(num_candidates):
curr_centerline = helpers_dict["CANDIDATE_CENTERLINES"][
batch_idx][candidate_idx]
curr_nt_dist = helpers_dict["CANDIDATE_NT_DISTANCES"][
batch_idx][candidate_idx]
_input = torch.FloatTensor(
np.expand_dims(curr_nt_dist[:args.obs_len].astype(float),
0)).to(device)
# Initialize encoder hidden state
encoder_hidden = model_utils.init_hidden(
1, encoder.module.hidden_size
if use_cuda else encoder.hidden_size)
# Encode observed trajectory
for ei in range(input_length):
encoder_input = _input[:, ei, :]
encoder_hidden = encoder(encoder_input, encoder_hidden)
# Initialize decoder input with last coordinate in encoder
decoder_input = encoder_input[:, :2]
# Initialize decoder hidden state as encoder hidden state
decoder_hidden = encoder_hidden
decoder_outputs = torch.zeros((1, args.pred_len, 2)).to(device)
# Decode hidden state in future trajectory
for di in range(args.pred_len):
decoder_output, decoder_hidden = decoder(
decoder_input, decoder_hidden)
decoder_outputs[:, di, :] = decoder_output
# Use own predictions as inputs at next step
decoder_input = decoder_output
# Get absolute trajectory
abs_helpers = {}
abs_helpers["REFERENCE"] = np.expand_dims(
np.array(helpers_dict["CANDIDATE_DELTA_REFERENCES"]
[batch_idx][candidate_idx]),
0,
)
abs_helpers["CENTERLINE"] = np.expand_dims(curr_centerline, 0)
abs_input, abs_output = baseline_utils.get_abs_traj(
_input.clone().cpu().numpy(),
decoder_outputs.detach().clone().cpu().numpy(),
args,
abs_helpers,
)
# array of shape (1,30,2) to list of (30,2)
abs_outputs.append(abs_output[0])
forecasted_trajectories[seq_id] = abs_outputs
os.makedirs(forecasted_save_dir, exist_ok=True)
with open(os.path.join(forecasted_save_dir, f"{start_idx}.pkl"),
"wb") as f:
pkl.dump(forecasted_trajectories, f)
def infer_helper(
curr_data_dict: Dict[str, Any],
start_idx: int,
encoder: EncoderRNN,
decoder: DecoderRNN,
model_utils: ModelUtils,
forecasted_save_dir: str,
):
"""Run inference on the current joblib batch.
Args:
curr_data_dict: Data dictionary for the current joblib batch
start_idx: Start idx of the current joblib batch
encoder: Encoder network instance
decoder: Decoder network instance
model_utils: ModelUtils instance
forecasted_save_dir: Directory where forecasted trajectories are to be saved
"""
args = parse_arguments()
curr_test_dataset = LSTMDataset(curr_data_dict, args, "test")
curr_test_loader = torch.utils.data.DataLoader(
curr_test_dataset,
shuffle=False,
batch_size=args.test_batch_size,
collate_fn=model_utils.my_collate_fn,
)
if args.use_map:
print(f"#### LSTM+map inference at index {start_idx} ####")
infer_map(
curr_test_loader,
encoder,
decoder,
start_idx,
forecasted_save_dir,
model_utils,
)
else:
print(f"#### LSTM+social inference at {start_idx} ####"
) if args.use_social else print(
f"#### LSTM inference at {start_idx} ####")
infer_absolute(
curr_test_loader,
encoder,
decoder,
start_idx,
forecasted_save_dir,
model_utils,
)
def main():
"""Main."""
args = parse_arguments()
if not baseline_utils.validate_args(args):
return
print(f"Using all ({joblib.cpu_count()}) CPUs....")
if use_cuda:
print(f"Using all ({torch.cuda.device_count()}) GPUs...")
model_utils = ModelUtils()
# key for getting feature set
# Get features
if args.use_map and args.use_social:
baseline_key = "map_social"
elif args.use_map:
baseline_key = "map"
elif args.use_social:
baseline_key = "social"
else:
baseline_key = "none"
# Get data
data_dict = baseline_utils.get_data(args, baseline_key)
# Get model
criterion = nn.MSELoss()
encoder = EncoderRNN(
input_size=len(baseline_utils.BASELINE_INPUT_FEATURES[baseline_key]))
decoder = DecoderRNN(output_size=2)
if use_cuda:
encoder = nn.DataParallel(encoder)
decoder = nn.DataParallel(decoder)
encoder.to(device)
decoder.to(device)
encoder_optimizer = torch.optim.Adam(encoder.parameters(), lr=args.lr)
decoder_optimizer = torch.optim.Adam(decoder.parameters(), lr=args.lr)
# If model_path provided, resume from saved checkpoint
if args.model_path is not None and os.path.isfile(args.model_path):
epoch, rollout_len, _ = model_utils.load_checkpoint(
args.model_path, encoder, decoder, encoder_optimizer,
decoder_optimizer)
start_epoch = epoch + 1
start_rollout_idx = ROLLOUT_LENS.index(rollout_len) + 1
else:
start_epoch = 0
start_rollout_idx = 0
if not args.test:
# Tensorboard logger
log_dir = os.path.join(os.getcwd(), "lstm_logs", baseline_key)
# Get PyTorch Dataset
train_dataset = LSTMDataset(data_dict, args, "train")
val_dataset = LSTMDataset(data_dict, args, "val")
# Setting Dataloaders
train_loader = torch.utils.data.DataLoader(
train_dataset,
batch_size=args.train_batch_size,
shuffle=True,
drop_last=False,
collate_fn=model_utils.my_collate_fn,
)
val_loader = torch.utils.data.DataLoader(
val_dataset,
batch_size=args.val_batch_size,
drop_last=False,
shuffle=False,
collate_fn=model_utils.my_collate_fn,
)
print("Training begins ...")
decrement_counter = 0
epoch = start_epoch
global_start_time = time.time()
for i in range(start_rollout_idx, len(ROLLOUT_LENS)):
rollout_len = ROLLOUT_LENS[i]
logger = Logger(log_dir, name="{}".format(rollout_len))
best_loss = float("inf")
prev_loss = best_loss
while epoch < args.end_epoch:
start = time.time()
train(
train_loader,
epoch,
criterion,
logger,
encoder,
decoder,
encoder_optimizer,
decoder_optimizer,
model_utils,
rollout_len,
)
end = time.time()
print(
f"Training epoch completed in {(end - start) / 60.0} mins, Total time: {(end - global_start_time) / 60.0} mins"
)
epoch += 1
if epoch % 5 == 0:
start = time.time()
prev_loss, decrement_counter = validate(
val_loader,
epoch,
criterion,
logger,
encoder,
decoder,
encoder_optimizer,
decoder_optimizer,
model_utils,
prev_loss,
decrement_counter,
rollout_len,
)
end = time.time()
print(
f"Validation completed in {(end - start) / 60.0} mins, Total time: {(end - global_start_time) / 60.0} mins"
)
# If val loss increased 3 times consecutively, go to next rollout length
if decrement_counter > 2:
break
else:
start_time = time.time()
temp_save_dir = tempfile.mkdtemp()
test_size = data_dict["test_input"].shape[0]
test_data_subsets = baseline_utils.get_test_data_dict_subset(
data_dict, args)
# test_batch_size should be lesser than joblib_batch_size
Parallel(n_jobs=-2, verbose=2)(
delayed(infer_helper)(test_data_subsets[i], i, encoder, decoder,
model_utils, temp_save_dir)
for i in range(0, test_size, args.joblib_batch_size))
baseline_utils.merge_saved_traj(temp_save_dir, args.traj_save_path)
shutil.rmtree(temp_save_dir)
end = time.time()
print(f"Test completed in {(end - start_time) / 60.0} mins")
print(f"Forecasted Trajectories saved at {args.traj_save_path}")
if __name__ == "__main__":
main()