-
Notifications
You must be signed in to change notification settings - Fork 10
/
train.py
195 lines (142 loc) · 5.3 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
import torch
from torch.utils.data import DataLoader
import time
import math
import argparse
from model.full_model import Model
# Initialize device:
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', default="eth")
parser.add_argument('--device', default="cuda:0")
args = parser.parse_args()
device = torch.device(args.device)
dataset = args.dataset
eval_every=1000
val_type="test"
if dataset == "ind":
horizon = 30
fut_len = 30
grid_extent = 25
train_batch_size = 16
nei_dim = 0
step1 = 50
step2 = 100
step3 = 200
step4 = 800
gamma = 0.9996
from data.IND.inD import inD as DS
else:
horizon = 20
fut_len = 12
grid_extent = 20
train_batch_size = 32
nei_dim = 2
gamma = 0.9996
# if dataset=="trajnet":#tain 67952 val :2829
step1 = 15
step2 = 25
step3 = 50
step4 = 100
from data.SDD.sdd import sdd as DS
if dataset=="sdd":
val_type="val"
# Initialize datasets:
tr_set = DS(dataset, horizon=horizon, fut_len=fut_len, grid_extent=grid_extent)
val_set = DS(dataset, horizon=horizon, fut_len=fut_len, type=val_type, grid_extent=grid_extent)
tr_dl = DataLoader(tr_set,
batch_size=train_batch_size,
shuffle=True,
num_workers=8
)
val_dl = DataLoader(val_set,
batch_size=64,
shuffle=False,
num_workers=8
)
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
net = Model(horizon, fut_len, nei_dim, grid_extent).float().to(device)
temp = 1
parameters = list(net.parameters())
t_optimizer = torch.optim.Adam(parameters, lr=1e-3) #
st_time = time.time()
start_epoch = 1
min_val_loss = math.inf
min_epoch = 0
type = "omgs"
net.train()
for epoch in range(step4):
if epoch == step1:
checkpoint = torch.load(dataset + "omgs.tar")
net.load_state_dict(checkpoint['model_state_dict'])
type = "dist"
t_optimizer = torch.optim.Adam(parameters, lr=1e-3)
min_val_loss = math.inf
elif epoch == step2:
checkpoint = torch.load(dataset + "dist.tar")
net.load_state_dict(checkpoint['model_state_dict'])
temp = checkpoint['temp']
min_val_loss = math.inf
type = "cluster"
t_optimizer = torch.optim.Adam(parameters, lr=1e-3) # , weight_decay=1e-4
elif epoch == step3:
checkpoint = torch.load(dataset + "cluster.tar")
net.load_state_dict(checkpoint['model_state_dict'])
temp = checkpoint['temp']
type = "end"
t_optimizer = torch.optim.Adam(parameters, lr=1e-4) # , weight_decay=1e-4
for i, data in enumerate(tr_dl):
loss, _, _, _, _, _, count = net(data, temp=temp, type=type, device=device)
t_optimizer.zero_grad()
loss.backward()
a = torch.nn.utils.clip_grad_norm_(parameters, 10)
t_optimizer.step()
if type == "dist" :
temp = temp * gamma
if i % eval_every == 0:
net.eval()
with torch.no_grad():
agg_val_loss = 0
policy_loss = 0
traj_loss = 0
ogms_rce_loss = 0
ogms_ce_loss = 0
l_batch_loss = 0
val_batch_count = 0
# Load batch
for k, data_val in enumerate(val_dl):
loss, policy, traj, ogms_rce, ogms_ce, min_ade, count = net(data_val, temp=temp, type=type,
device=device)
agg_val_loss += loss.item() * count
policy_loss += policy.item()
traj_loss += traj.item()
ogms_rce_loss += ogms_rce.item()
ogms_ce_loss += ogms_ce.item()
l_batch_loss += min_ade.item() * count
val_batch_count += count
val_loss = agg_val_loss / val_batch_count
if val_loss < min_val_loss:
min_val_loss = val_loss
min_epoch = epoch
model_path = dataset + type + '.tar'
torch.save({
'epoch': epoch,
'model_state_dict': net.state_dict(),
'temp': temp,
'loss': val_loss,
'min_val_loss': min_val_loss
}, model_path)
print("Epoch no:", epoch,
"| temp", format(temp, '0.5f'),
"| val", format(agg_val_loss / val_batch_count, '0.3f'),
"| ade", format(l_batch_loss / val_batch_count, '0.3f'),
"| policy", format(policy_loss / val_batch_count, '0.3f'),
"| traj", format(traj_loss / val_batch_count, '0.3f'),
"|ce", format((traj_loss + policy_loss) / val_batch_count, '0.3f'),
"| ogms_rce", format(ogms_rce_loss / val_batch_count, '0.3f'),
"| ogms_ce", format(ogms_ce_loss / val_batch_count, '0.3f'),
"| Min epoch", min_epoch,
"| Min val loss", format(min_val_loss, '0.3f'),
"| T(s):", int(time.time() - st_time))
st_time = time.time()
net.train()