-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtrain.py
141 lines (102 loc) · 5.13 KB
/
train.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
import argparse
import os
from tqdm import tqdm
import torch
from torch.utils.data import DataLoader
from torch import optim
from model.trajairnet import TrajAirNet
from model.utils import TrajectoryDataset, seq_collate, loss_func
from test import test
def train():
##Dataset params
parser=argparse.ArgumentParser(description='Train TrajAirNet model')
parser.add_argument('--dataset_folder',type=str,default='/dataset/')
parser.add_argument('--dataset_name',type=str,default='7days1')
parser.add_argument('--obs',type=int,default=11)
parser.add_argument('--preds',type=int,default=120)
parser.add_argument('--preds_step',type=int,default=10)
##Network params
parser.add_argument('--input_channels',type=int,default=3)
parser.add_argument('--tcn_channel_size',type=int,default=256)
parser.add_argument('--tcn_layers',type=int,default=2)
parser.add_argument('--tcn_kernels',type=int,default=4)
parser.add_argument('--num_context_input_c',type=int,default=2)
parser.add_argument('--num_context_output_c',type=int,default=7)
parser.add_argument('--cnn_kernels',type=int,default=2)
parser.add_argument('--gat_heads',type=int, default=16)
parser.add_argument('--graph_hidden',type=int,default=256)
parser.add_argument('--dropout',type=float,default=0.05)
parser.add_argument('--alpha',type=float,default=0.2)
parser.add_argument('--cvae_hidden',type=int,default=128)
parser.add_argument('--cvae_channel_size',type=int,default=128)
parser.add_argument('--cvae_layers',type=int,default=2)
parser.add_argument('--mlp_layer',type=int,default=32)
parser.add_argument('--lr',type=float,default=0.001)
parser.add_argument('--total_epochs',type=int, default=50)
parser.add_argument('--delim',type=str,default=' ')
parser.add_argument('--evaluate', type=bool, default=True)
parser.add_argument('--save_model', type=bool, default=True)
parser.add_argument('--model_pth', type=str , default="/saved_models/")
args=parser.parse_args()
##Select device
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
##Load test and train data
datapath = os.getcwd() + args.dataset_folder + args.dataset_name + "/processed_data/"
print("Loading Train Data from ",datapath + "train")
dataset_train = TrajectoryDataset(datapath + "train", obs_len=args.obs, pred_len=args.preds, step=args.preds_step, delim=args.delim)
print("Loading Test Data from ",datapath + "test")
dataset_test = TrajectoryDataset(datapath + "test", obs_len=args.obs, pred_len=args.preds, step=args.preds_step, delim=args.delim)
loader_train = DataLoader(dataset_train,batch_size=1,num_workers=4,shuffle=True,collate_fn=seq_collate)
loader_test = DataLoader(dataset_test,batch_size=1,num_workers=4,shuffle=True,collate_fn=seq_collate)
model = TrajAirNet(args)
model.to(device)
##Resume
# checkpoint = torch.load('model_11.pt',map_location=torch.device('cpu'))
# model.load_state_dict(checkpoint['model_state_dict'])
optimizer = optim.Adam(model.parameters(),lr=args.lr)
num_batches = len(loader_train)
print("Starting Training....")
for epoch in range(1, args.total_epochs+1):
model.train()
loss_batch = 0
batch_count = 0
tot_batch_count = 0
tot_loss = 0
for batch in tqdm(loader_train):
batch_count += 1
tot_batch_count += 1
batch = [tensor.to(device) for tensor in batch]
obs_traj , pred_traj, obs_traj_rel, pred_traj_rel, context, seq_start = batch
num_agents = obs_traj.shape[1]
pred_traj = torch.transpose(pred_traj,1,2)
adj = torch.ones((num_agents,num_agents))
optimizer.zero_grad()
recon_y,m,var = model(torch.transpose(obs_traj,1,2),pred_traj, adj[0],torch.transpose(context,1,2))
loss = 0
for agent in range(num_agents):
loss += loss_func(recon_y[agent],torch.transpose(pred_traj[:,:,agent],0,1).unsqueeze(0),m[agent],var[agent])
loss_batch += loss
tot_loss += loss.item()
if batch_count>8:
loss_batch.backward()
optimizer.step()
loss_batch = 0
batch_count = 0
print("EPOCH: ",epoch,"Train Loss: ",loss)
if args.save_model:
loss = tot_loss/tot_batch_count
model_path = os.getcwd() + args.model_pth + "model_" + args.dataset_name + "_" + str(epoch) + ".pt"
print("Saving model at",model_path)
torch.save({
'epoch': epoch,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'loss': loss,
}, model_path)
if args.evaluate:
print("Starting Testing....")
model.eval()
test_ade_loss, test_fde_loss = test(model,loader_test,device)
print("EPOCH: ",epoch,"Train Loss: ",loss,"Test ADE Loss: ",test_ade_loss,"Test FDE Loss: ",test_fde_loss)
if __name__=='__main__':
train()