-
Notifications
You must be signed in to change notification settings - Fork 14
/
train.py
322 lines (281 loc) · 10.4 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
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
import argparse
import logging
import os
import random
import shutil
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.tensorboard import SummaryWriter
import utils
from data.loader import data_loader
from models import TrajectoryGenerator
from utils import (
displacement_error,
final_displacement_error,
get_dset_path,
int_tuple,
l2_loss,
relative_to_abs,
)
parser = argparse.ArgumentParser()
parser.add_argument("--log_dir", default="./", help="Directory containing logging file")
parser.add_argument("--dataset_name", default="zara2", type=str)
parser.add_argument("--delim", default="\t")
parser.add_argument("--loader_num_workers", default=4, type=int)
parser.add_argument("--obs_len", default=8, type=int)
parser.add_argument("--pred_len", default=12, type=int)
parser.add_argument("--skip", default=1, type=int)
parser.add_argument("--seed", type=int, default=72, help="Random seed.")
parser.add_argument("--batch_size", default=64, type=int)
parser.add_argument("--num_epochs", default=400, type=int)
parser.add_argument("--noise_dim", default=(16,), type=int_tuple)
parser.add_argument("--noise_type", default="gaussian")
parser.add_argument(
"--traj_lstm_input_size", type=int, default=2, help="traj_lstm_input_size"
)
parser.add_argument("--traj_lstm_hidden_size", default=32, type=int)
parser.add_argument(
"--heads", type=str, default="4,1", help="Heads in each layer, splitted with comma"
)
parser.add_argument(
"--hidden-units",
type=str,
default="16",
help="Hidden units in each hidden layer, splitted with comma",
)
parser.add_argument(
"--graph_network_out_dims",
type=int,
default=32,
help="dims of every node after through GAT module",
)
parser.add_argument("--graph_lstm_hidden_size", default=32, type=int)
parser.add_argument(
"--dropout", type=float, default=0, help="Dropout rate (1 - keep probability)."
)
parser.add_argument(
"--alpha", type=float, default=0.2, help="Alpha for the leaky_relu."
)
parser.add_argument(
"--lr",
default=1e-3,
type=float,
metavar="LR",
help="initial learning rate",
dest="lr",
)
parser.add_argument(
"--start-epoch",
default=0,
type=int,
metavar="N",
help="manual epoch number (useful on restarts)",
)
parser.add_argument("--best_k", default=20, type=int)
parser.add_argument("--print_every", default=10, type=int)
parser.add_argument("--use_gpu", default=1, type=int)
parser.add_argument("--gpu_num", default="0", type=str)
parser.add_argument(
"--resume",
default="",
type=str,
metavar="PATH",
help="path to latest checkpoint (default: none)",
)
best_ade = 100
def main(args):
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_num
train_path = get_dset_path(args.dataset_name, "train")
val_path = get_dset_path(args.dataset_name, "test")
logging.info("Initializing train dataset")
train_dset, train_loader = data_loader(args, train_path)
logging.info("Initializing val dataset")
_, val_loader = data_loader(args, val_path)
writer = SummaryWriter()
n_units = (
[args.traj_lstm_hidden_size]
+ [int(x) for x in args.hidden_units.strip().split(",")]
+ [args.graph_lstm_hidden_size]
)
n_heads = [int(x) for x in args.heads.strip().split(",")]
model = TrajectoryGenerator(
obs_len=args.obs_len,
pred_len=args.pred_len,
traj_lstm_input_size=args.traj_lstm_input_size,
traj_lstm_hidden_size=args.traj_lstm_hidden_size,
n_units=n_units,
n_heads=n_heads,
graph_network_out_dims=args.graph_network_out_dims,
dropout=args.dropout,
alpha=args.alpha,
graph_lstm_hidden_size=args.graph_lstm_hidden_size,
noise_dim=args.noise_dim,
noise_type=args.noise_type,
)
model.cuda()
optimizer = optim.Adam(
[
{"params": model.traj_lstm_model.parameters(), "lr": 1e-2},
{"params": model.traj_hidden2pos.parameters()},
{"params": model.gatencoder.parameters(), "lr": 3e-2},
{"params": model.graph_lstm_model.parameters(), "lr": 1e-2},
{"params": model.traj_gat_hidden2pos.parameters()},
{"params": model.pred_lstm_model.parameters()},
{"params": model.pred_hidden2pos.parameters()},
],
lr=args.lr,
)
global best_ade
if args.resume:
if os.path.isfile(args.resume):
logging.info("Restoring from checkpoint {}".format(args.resume))
checkpoint = torch.load(args.resume)
args.start_epoch = checkpoint["epoch"]
model.load_state_dict(checkpoint["state_dict"])
logging.info(
"=> loaded checkpoint '{}' (epoch {})".format(
args.resume, checkpoint["epoch"]
)
)
else:
logging.info("=> no checkpoint found at '{}'".format(args.resume))
training_step = 1
for epoch in range(args.start_epoch, args.num_epochs + 1):
if epoch < 150:
training_step = 1
elif epoch < 250:
training_step = 2
else:
if epoch == 250:
for param_group in optimizer.param_groups:
param_group["lr"] = 5e-3
training_step = 3
train(args, model, train_loader, optimizer, epoch, training_step, writer)
if training_step == 3:
ade = validate(args, model, val_loader, epoch, writer)
is_best = ade < best_ade
best_ade = min(ade, best_ade)
save_checkpoint(
{
"epoch": epoch + 1,
"state_dict": model.state_dict(),
"best_ade": best_ade,
"optimizer": optimizer.state_dict(),
},
is_best,
f"./checkpoint/checkpoint{epoch}.pth.tar",
)
writer.close()
def train(args, model, train_loader, optimizer, epoch, training_step, writer):
losses = utils.AverageMeter("Loss", ":.6f")
progress = utils.ProgressMeter(
len(train_loader), [losses], prefix="Epoch: [{}]".format(epoch)
)
model.train()
for batch_idx, batch in enumerate(train_loader):
batch = [tensor.cuda() for tensor in batch]
(
obs_traj,
pred_traj_gt,
obs_traj_rel,
pred_traj_gt_rel,
non_linear_ped,
loss_mask,
seq_start_end,
) = batch
optimizer.zero_grad()
loss = torch.zeros(1).to(pred_traj_gt)
l2_loss_rel = []
loss_mask = loss_mask[:, args.obs_len :]
if training_step == 1 or training_step == 2:
model_input = obs_traj_rel
pred_traj_fake_rel = model(
model_input, obs_traj, seq_start_end, 1, training_step
)
l2_loss_rel.append(
l2_loss(pred_traj_fake_rel, model_input, loss_mask, mode="raw")
)
else:
model_input = torch.cat((obs_traj_rel, pred_traj_gt_rel), dim=0)
for _ in range(args.best_k):
pred_traj_fake_rel = model(model_input, obs_traj, seq_start_end, 0)
l2_loss_rel.append(
l2_loss(
pred_traj_fake_rel,
model_input[-args.pred_len :],
loss_mask,
mode="raw",
)
)
l2_loss_sum_rel = torch.zeros(1).to(pred_traj_gt)
l2_loss_rel = torch.stack(l2_loss_rel, dim=1)
for start, end in seq_start_end.data:
_l2_loss_rel = torch.narrow(l2_loss_rel, 0, start, end - start)
_l2_loss_rel = torch.sum(_l2_loss_rel, dim=0) # [20]
_l2_loss_rel = torch.min(_l2_loss_rel) / (
(pred_traj_fake_rel.shape[0]) * (end - start)
)
l2_loss_sum_rel += _l2_loss_rel
loss += l2_loss_sum_rel
losses.update(loss.item(), obs_traj.shape[1])
loss.backward()
optimizer.step()
if batch_idx % args.print_every == 0:
progress.display(batch_idx)
writer.add_scalar("train_loss", losses.avg, epoch)
def validate(args, model, val_loader, epoch, writer):
ade = utils.AverageMeter("ADE", ":.6f")
fde = utils.AverageMeter("FDE", ":.6f")
progress = utils.ProgressMeter(len(val_loader), [ade, fde], prefix="Test: ")
model.eval()
with torch.no_grad():
for i, batch in enumerate(val_loader):
batch = [tensor.cuda() for tensor in batch]
(
obs_traj,
pred_traj_gt,
obs_traj_rel,
pred_traj_gt_rel,
non_linear_ped,
loss_mask,
seq_start_end,
) = batch
loss_mask = loss_mask[:, args.obs_len :]
pred_traj_fake_rel = model(obs_traj_rel, obs_traj, seq_start_end)
pred_traj_fake_rel_predpart = pred_traj_fake_rel[-args.pred_len :]
pred_traj_fake = relative_to_abs(pred_traj_fake_rel_predpart, obs_traj[-1])
ade_, fde_ = cal_ade_fde(pred_traj_gt, pred_traj_fake)
ade_ = ade_ / (obs_traj.shape[1] * args.pred_len)
fde_ = fde_ / (obs_traj.shape[1])
ade.update(ade_, obs_traj.shape[1])
fde.update(fde_, obs_traj.shape[1])
if i % args.print_every == 0:
progress.display(i)
logging.info(
" * ADE {ade.avg:.3f} FDE {fde.avg:.3f}".format(ade=ade, fde=fde)
)
writer.add_scalar("val_ade", ade.avg, epoch)
return ade.avg
def cal_ade_fde(pred_traj_gt, pred_traj_fake):
ade = displacement_error(pred_traj_fake, pred_traj_gt)
fde = final_displacement_error(pred_traj_fake[-1], pred_traj_gt[-1])
return ade, fde
def save_checkpoint(state, is_best, filename="checkpoint.pth.tar"):
if is_best:
torch.save(state, filename)
logging.info("-------------- lower ade ----------------")
shutil.copyfile(filename, "model_best.pth.tar")
if __name__ == "__main__":
args = parser.parse_args()
utils.set_logger(os.path.join(args.log_dir, "train.log"))
checkpoint_dir = "./checkpoint"
if os.path.exists(checkpoint_dir) is False:
os.mkdir(checkpoint_dir)
main(args)