-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlanegcn_modules.py
504 lines (434 loc) · 15.7 KB
/
lanegcn_modules.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
import torch.nn.functional as F
from torch import nn
from torch.distributions import Normal
import torch
import math
from fractions import gcd
from fjmp_utils import *
class Linear(nn.Module):
def __init__(self, n_in, n_out, norm='GN', ng=32, act=True):
super(Linear, self).__init__()
assert(norm in ['GN', 'BN', 'SyncBN'])
self.linear = nn.Linear(n_in, n_out, bias=False)
if norm == 'GN':
self.norm = nn.GroupNorm(gcd(ng, n_out), n_out)
elif norm == 'BN':
self.norm = nn.BatchNorm1d(n_out)
else:
exit('SyncBN has not been added!')
self.relu = nn.ReLU(inplace=True)
self.act = act
self.init_weights()
def init_weights(self):
for m in self.modules():
if isinstance(m, nn.Linear):
nn.init.xavier_normal_(m.weight.data)
if m.bias is not None: nn.init.constant_(m.bias, 0.1)
def forward(self, x):
out = self.linear(x)
out = self.norm(out)
if self.act:
out = self.relu(out)
return out
class MapNet(nn.Module):
"""
Map Graph feature extractor with LaneGraphCNN
"""
def __init__(self, config):
super(MapNet, self).__init__()
self.config = config
if self.config["h_dim"] >= 256:
n_map = int(config["h_dim"] / 2)
else:
n_map = int(config["h_dim"])
norm = "GN"
ng = 1
self.input = nn.Sequential(
nn.Linear(2, n_map),
nn.ReLU(inplace=True),
Linear(n_map, n_map, norm=norm, ng=ng, act=False),
)
self.seg = nn.Sequential(
nn.Linear(2, n_map),
nn.ReLU(inplace=True),
Linear(n_map, n_map, norm=norm, ng=ng, act=False),
)
keys = ["ctr", "norm", "ctr2", "left", "right"]
for i in range(config["num_scales"]):
keys.append("pre" + str(i))
keys.append("suc" + str(i))
fuse = dict()
for key in keys:
fuse[key] = []
for i in range(self.config["n_mapnet_layers"]):
for key in fuse:
if key in ["norm"]:
fuse[key].append(nn.GroupNorm(gcd(ng, n_map), n_map))
elif key in ["ctr2"]:
fuse[key].append(Linear(n_map, n_map, norm=norm, ng=ng, act=False))
else:
fuse[key].append(nn.Linear(n_map, n_map, bias=False))
for key in fuse:
fuse[key] = nn.ModuleList(fuse[key])
self.fuse = nn.ModuleDict(fuse)
self.relu = nn.ReLU(inplace=True)
self.init_weights()
def init_weights(self):
for m in self.modules():
if isinstance(m, nn.Linear):
nn.init.xavier_normal_(m.weight.data)
if m.bias is not None: nn.init.constant_(m.bias, 0.1)
def forward(self, graph):
# this is the degenerate case -- it should not go in here
if (
len(graph["feats"]) == 0
or len(graph["pre"][self.config["num_scales"] - 1]["u"]) == 0
or len(graph["suc"][self.config["num_scales"] - 1]["u"]) == 0
):
print("Should this ever get in here, I want to know.")
temp = graph["feats"]
return (
temp.new().resize_(0),
[temp.new().long().resize_(0) for x in graph["idcs"]],
temp.new().resize_(0),
)
ctrs = torch.cat(graph["ctrs"], 0)
feat = self.input(ctrs)
feat += self.seg(graph["feats"])
feat = self.relu(feat)
"""fuse map"""
res = feat
for i in range(len(self.fuse["ctr"])):
temp = self.fuse["ctr"][i](feat)
for key in self.fuse:
if key.startswith("pre") or key.startswith("suc"):
k1 = key[:3]
k2 = int(key[3:])
temp.index_add_(
0,
graph[k1][k2]["u"],
self.fuse[key][i](feat[graph[k1][k2]["v"]]),
)
if len(graph["left"]["u"] > 0):
temp.index_add_(
0,
graph["left"]["u"],
self.fuse["left"][i](feat[graph["left"]["v"]]),
)
if len(graph["right"]["u"] > 0):
temp.index_add_(
0,
graph["right"]["u"],
self.fuse["right"][i](feat[graph["right"]["v"]]),
)
feat = self.fuse["norm"][i](temp)
feat = self.relu(feat)
feat = self.fuse["ctr2"][i](feat)
feat += res
feat = self.relu(feat)
res = feat
return feat, graph["idcs"], graph["ctrs"]
class A2A(nn.Module):
"""
The actor to actor block performs interactions among actors.
"""
def __init__(self, config):
super(A2A, self).__init__()
self.config = config
norm = "GN"
ng = 1
if self.config["h_dim"] >= 256:
n_actor = int(config["h_dim"] / 2)
else:
n_actor = int(config["h_dim"])
att = []
for i in range(self.config["n_a2a_layers"]):
att.append(Att(n_actor, n_actor))
self.att = nn.ModuleList(att)
def forward(self, actors, actor_idcs, actor_ctrs):
for i in range(len(self.att)):
actors = self.att[i](
actors,
actor_idcs,
actor_ctrs,
actors,
actor_idcs,
actor_ctrs,
self.config["actor2actor_dist"],
)
return actors
class L2A(nn.Module):
"""
The lane to actor block fuses updated
map information from lane nodes to actor nodes
"""
def __init__(self, config):
super(L2A, self).__init__()
self.config = config
norm = "GN"
ng = 1
if self.config["h_dim"] >= 256:
n_map = int(config["h_dim"] / 2)
n_actor = int(config["h_dim"] / 2)
else:
n_map = int(config["h_dim"])
n_actor = int(config["h_dim"])
att = []
for i in range(self.config["n_l2a_layers"]):
att.append(Att(n_actor, n_map))
self.att = nn.ModuleList(att)
def forward(self, actors, actor_idcs, actor_ctrs, nodes, node_idcs, node_ctrs):
for i in range(len(self.att)):
actors = self.att[i](
actors,
actor_idcs,
actor_ctrs,
nodes,
node_idcs,
node_ctrs,
self.config["map2actor_dist"],
)
return actors
class Att(nn.Module):
"""
Attention block to pass context nodes information to target nodes
This is used in Actor2Map, Actor2Actor, Map2Actor and Map2Map
"""
def __init__(self, n_agt: int, n_ctx: int) -> None:
super(Att, self).__init__()
norm = "GN"
ng = 1
self.dist = nn.Sequential(
nn.Linear(2, n_ctx),
nn.ReLU(inplace=True),
Linear(n_ctx, n_ctx, norm=norm, ng=ng),
)
self.query = Linear(n_agt, n_ctx, norm=norm, ng=ng)
self.ctx = nn.Sequential(
Linear(3 * n_ctx, n_agt, norm=norm, ng=ng),
nn.Linear(n_agt, n_agt, bias=False),
)
self.agt = nn.Linear(n_agt, n_agt, bias=False)
self.norm = nn.GroupNorm(gcd(ng, n_agt), n_agt)
self.linear = Linear(n_agt, n_agt, norm=norm, ng=ng, act=False)
self.relu = nn.ReLU(inplace=True)
self.init_weights()
def init_weights(self):
for m in self.modules():
if isinstance(m, nn.Linear):
nn.init.xavier_normal_(m.weight.data)
if m.bias is not None: nn.init.constant_(m.bias, 0.1)
def forward(self, agts, agt_idcs, agt_ctrs, ctx, ctx_idcs, ctx_ctrs, dist_th):
res = agts
if len(ctx) == 0:
print("Does it ever get in here either?")
agts = self.agt(agts)
agts = self.relu(agts)
agts = self.linear(agts)
agts += res
agts = self.relu(agts)
return agts
batch_size = len(agt_idcs)
hi, wi = [], []
hi_count, wi_count = 0, 0
for i in range(batch_size):
dist = agt_ctrs[i].view(-1, 1, 2) - ctx_ctrs[i].view(1, -1, 2)
dist = torch.sqrt((dist ** 2).sum(2))
mask = dist <= dist_th
idcs = torch.nonzero(mask, as_tuple=False)
if len(idcs) == 0:
continue
hi.append(idcs[:, 0] + hi_count)
wi.append(idcs[:, 1] + wi_count)
hi_count += len(agt_idcs[i])
wi_count += len(ctx_idcs[i])
hi = torch.cat(hi, 0)
wi = torch.cat(wi, 0)
agt_ctrs = torch.cat(agt_ctrs, 0)
ctx_ctrs = torch.cat(ctx_ctrs, 0)
dist = agt_ctrs[hi] - ctx_ctrs[wi]
dist = self.dist(dist)
query = self.query(agts[hi])
ctx = ctx[wi]
ctx = torch.cat((dist, query, ctx), 1)
ctx = self.ctx(ctx)
agts = self.agt(agts)
agts.index_add_(0, hi, ctx)
agts = self.norm(agts)
agts = self.relu(agts)
agts = self.linear(agts)
agts += res
agts = self.relu(agts)
return agts
class A2L(nn.Module):
"""
Actor to Map Fusion: fuses real-time traffic information from
actor nodes to lane nodes
"""
def __init__(self, config):
super(A2L, self).__init__()
self.config = config
if self.config["h_dim"] >= 256:
n_map = int(config["h_dim"] / 2)
n_actor = int(config["h_dim"] / 2)
else:
n_map = int(config["h_dim"])
n_actor = int(config["h_dim"])
norm = "GN"
ng = 1
"""fuse meta, static, dyn"""
# self.meta = Linear(n_map + 4, n_map, norm=norm, ng=ng)
att = []
for i in range(2):
att.append(Att(n_map, n_actor))
self.att = nn.ModuleList(att)
def forward(self, feat, graph, actors, actor_idcs, actor_ctrs, node_idcs, node_ctrs):
# """meta, static and dyn fuse using attention"""
# meta = torch.cat(
# (
# graph["turn"],
# graph["control"].unsqueeze(1),
# graph["intersect"].unsqueeze(1),
# ),
# 1,
# )
# feat = self.meta(torch.cat((feat, meta), 1))
for i in range(len(self.att)):
feat = self.att[i](
feat,
node_idcs,
node_ctrs,
actors,
actor_idcs,
actor_ctrs,
self.config["actor2map_dist"],
)
return feat
class L2L(nn.Module):
"""
The lane to lane block: propagates information over lane
graphs and updates the features of lane nodes
"""
def __init__(self, config):
super(L2L, self).__init__()
self.config = config
if self.config["h_dim"] >= 256:
n_map = int(config["h_dim"] / 2)
else:
n_map = int(config["h_dim"])
norm = "GN"
ng = 1
keys = ["ctr", "norm", "ctr2", "left", "right"]
for i in range(config["num_scales"]):
keys.append("pre" + str(i))
keys.append("suc" + str(i))
fuse = dict()
for key in keys:
fuse[key] = []
for i in range(4):
for key in fuse:
if key in ["norm"]:
fuse[key].append(nn.GroupNorm(gcd(ng, n_map), n_map))
elif key in ["ctr2"]:
fuse[key].append(Linear(n_map, n_map, norm=norm, ng=ng, act=False))
else:
fuse[key].append(nn.Linear(n_map, n_map, bias=False))
for key in fuse:
fuse[key] = nn.ModuleList(fuse[key])
self.fuse = nn.ModuleDict(fuse)
self.relu = nn.ReLU(inplace=True)
def forward(self, feat, graph):
"""fuse map"""
res = feat
for i in range(len(self.fuse["ctr"])):
temp = self.fuse["ctr"][i](feat)
for key in self.fuse:
if key.startswith("pre") or key.startswith("suc"):
k1 = key[:3]
k2 = int(key[3:])
temp.index_add_(
0,
graph[k1][k2]["u"],
self.fuse[key][i](feat[graph[k1][k2]["v"]]),
)
if len(graph["left"]["u"] > 0):
temp.index_add_(
0,
graph["left"]["u"],
self.fuse["left"][i](feat[graph["left"]["v"]]),
)
if len(graph["right"]["u"] > 0):
temp.index_add_(
0,
graph["right"]["u"],
self.fuse["right"][i](feat[graph["right"]["v"]]),
)
feat = self.fuse["norm"][i](temp)
feat = self.relu(feat)
feat = self.fuse["ctr2"][i](feat)
feat += res
feat = self.relu(feat)
res = feat
return feat
class Conv1d(nn.Module):
def __init__(self, n_in, n_out, kernel_size=3, stride=1, norm='GN', ng=32, act=True):
super(Conv1d, self).__init__()
assert(norm in ['GN', 'BN', 'SyncBN'])
self.conv = nn.Conv1d(n_in, n_out, kernel_size=kernel_size, padding=(int(kernel_size) - 1) // 2, stride=stride, bias=False)
if norm == 'GN':
self.norm = nn.GroupNorm(gcd(ng, n_out), n_out)
elif norm == 'BN':
self.norm = nn.BatchNorm1d(n_out)
else:
exit('SyncBN has not been added!')
self.relu = nn.ReLU(inplace=True)
self.act = act
def forward(self, x):
out = self.conv(x)
out = self.norm(out)
if self.act:
out = self.relu(out)
return out
class Res1d(nn.Module):
def __init__(self, n_in, n_out, kernel_size=3, stride=1, norm='GN', ng=32, act=True):
super(Res1d, self).__init__()
assert(norm in ['GN', 'BN', 'SyncBN'])
padding = (int(kernel_size) - 1) // 2
self.conv1 = nn.Conv1d(n_in, n_out, kernel_size=kernel_size, stride=stride, padding=padding, bias=False)
self.conv2 = nn.Conv1d(n_out, n_out, kernel_size=kernel_size, padding=padding, bias=False)
self.relu = nn.ReLU(inplace = True)
# All use name bn1 and bn2 to load imagenet pretrained weights
if norm == 'GN':
self.bn1 = nn.GroupNorm(gcd(ng, n_out), n_out)
self.bn2 = nn.GroupNorm(gcd(ng, n_out), n_out)
elif norm == 'BN':
self.bn1 = nn.BatchNorm1d(n_out)
self.bn2 = nn.BatchNorm1d(n_out)
else:
exit('SyncBN has not been added!')
if stride != 1 or n_out != n_in:
if norm == 'GN':
self.downsample = nn.Sequential(
nn.Conv1d(n_in, n_out, kernel_size=1, stride=stride, bias=False),
nn.GroupNorm(gcd(ng, n_out), n_out))
elif norm == 'BN':
self.downsample = nn.Sequential(
nn.Conv1d(n_in, n_out, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm1d(n_out))
else:
exit('SyncBN has not been added!')
else:
self.downsample = None
self.act = act
def forward(self, x):
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.downsample is not None:
x = self.downsample(x)
out += x
if self.act:
out = self.relu(out)
return out