-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmodel2_seq.py
483 lines (394 loc) · 21.1 KB
/
model2_seq.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
import math
from collections import deque
import numpy as np
import torch
from torch import nn
import torch.nn.functional as F
from torchvision import models
class ImageCNN(nn.Module):
"""
Encoder network for image input list.
Args:
c_dim (int): output dimension of the latent embedding
normalize (bool): whether the input images should be normalized
"""
def __init__(self, c_dim, normalize=True):
super().__init__()
self.normalize = normalize
self.features = models.resnet34(pretrained =True)
self.features.fc = nn.Sequential()
# for param in self.features.parameters():
# param.requires_grad = False
def forward(self, inputs):
c = 0
for x in inputs:
if self.normalize:
x = normalize_imagenet(x)
c += self.features(x)
return c
def normalize_imagenet(x):
""" Normalize input images according to ImageNet standards.
Args:
x (tensor): input images
"""
x = x.clone()
x[:, 0] = (x[:, 0]/255.0 - 0.485) / 0.229
x[:, 1] = (x[:, 1]/255.0 - 0.456) / 0.224
x[:, 2] = (x[:, 2]/255.0 - 0.406) / 0.225
return x
class LidarEncoder(nn.Module):
"""
Encoder network for LiDAR input list
Args:
num_classes: output feature dimension
in_channels: input channels
"""
def __init__(self, num_classes=512, in_channels=2):
super().__init__()
self._model = models.resnet18(pretrained =True)
self._model.fc = nn.Sequential()
_tmp = self._model.conv1
self._model.conv1 = nn.Conv2d(in_channels, out_channels=_tmp.out_channels,
kernel_size=_tmp.kernel_size, stride=_tmp.stride, padding=_tmp.padding, bias=_tmp.bias)
# for param in self._model.parameters():
# param.requires_grad = False
def forward(self, inputs):
features = 0
for lidar_data in inputs:
lidar_feature = self._model(lidar_data)
features += lidar_feature
return features
class SelfAttention(nn.Module):
"""
A vanilla multi-head masked self-attention layer with a projection at the end.
"""
def __init__(self, n_embd, n_head, attn_pdrop, resid_pdrop):
super().__init__()
assert n_embd % n_head == 0
# key, query, value projections for all heads
self.key = nn.Linear(n_embd, n_embd)
self.query = nn.Linear(n_embd, n_embd)
self.value = nn.Linear(n_embd, n_embd)
# regularization
self.attn_drop = nn.Dropout(attn_pdrop)
self.resid_drop = nn.Dropout(resid_pdrop)
# output projection
self.proj = nn.Linear(n_embd, n_embd)
self.n_head = n_head
def forward(self, x):
B, T, C = x.size()
# calculate query, key, values for all heads in batch and move head forward to be the batch dim
k = self.key(x).view(B, T, self.n_head, C // self.n_head).transpose(1, 2) # (B, nh, T, hs)
q = self.query(x).view(B, T, self.n_head, C // self.n_head).transpose(1, 2) # (B, nh, T, hs)
v = self.value(x).view(B, T, self.n_head, C // self.n_head).transpose(1, 2) # (B, nh, T, hs)
# self-attend: (B, nh, T, hs) x (B, nh, hs, T) -> (B, nh, T, T)
att = (q @ k.transpose(-2, -1)) * (1.0 / math.sqrt(k.size(-1)))
att = F.softmax(att, dim=-1)
att = self.attn_drop(att)
y = att @ v # (B, nh, T, T) x (B, nh, T, hs) -> (B, nh, T, hs)
y = y.transpose(1, 2).contiguous().view(B, T, C) # re-assemble all head outputs side by side
# output projection
y = self.resid_drop(self.proj(y))
return y
class Block(nn.Module):
""" an unassuming Transformer block """
def __init__(self, n_embd, n_head, block_exp, attn_pdrop, resid_pdrop):
super().__init__()
self.ln1 = nn.LayerNorm(n_embd)
self.ln2 = nn.LayerNorm(n_embd)
self.attn = SelfAttention(n_embd, n_head, attn_pdrop, resid_pdrop)
self.mlp = nn.Sequential(
nn.Linear(n_embd, block_exp * n_embd),
nn.ReLU(True), # changed from GELU
nn.Linear(block_exp * n_embd, n_embd),
nn.Dropout(resid_pdrop),
)
def forward(self, x):
B, T, C = x.size()
x = x + self.attn(self.ln1(x))
x = x + self.mlp(self.ln2(x))
return x
class GPT(nn.Module):
""" the full GPT language model, with a context size of block_size """
def __init__(self, n_embd, n_head, block_exp, n_layer,
vert_anchors, horz_anchors, seq_len,
embd_pdrop, attn_pdrop, resid_pdrop, config):
super().__init__()
self.n_embd = n_embd
self.seq_len = seq_len
self.vert_anchors = vert_anchors
self.horz_anchors = horz_anchors
self.config = config
# positional embedding parameter (learnable), image + lidar
self.pos_emb = nn.Parameter(torch.zeros(1, (self.config.n_views + 2) * seq_len * vert_anchors * horz_anchors+2, n_embd))
self.drop = nn.Dropout(embd_pdrop)
# transformer
self.blocks = nn.Sequential(*[Block(n_embd, n_head,
block_exp, attn_pdrop, resid_pdrop)
for layer in range(n_layer)])
# decoder head
self.ln_f = nn.LayerNorm(n_embd)
self.block_size = seq_len
self.apply(self._init_weights)
def get_block_size(self):
return self.block_size
def _init_weights(self, module):
if isinstance(module, nn.Linear):
module.weight.data.normal_(mean=0.0, std=0.02)
if module.bias is not None:
module.bias.data.zero_()
elif isinstance(module, nn.LayerNorm):
module.bias.data.zero_()
module.weight.data.fill_(1.0)
def configure_optimizers(self):
# separate out all parameters to those that will and won't experience regularizing weight decay
decay = set()
no_decay = set()
whitelist_weight_modules = (torch.nn.Linear, torch.nn.Conv2d)
blacklist_weight_modules = (torch.nn.LayerNorm, torch.nn.BatchNorm2d)
for mn, m in self.named_modules():
for pn, p in m.named_parameters():
fpn = '%s.%s' % (mn, pn) if mn else pn # full param name
if pn.endswith('bias'):
# all biases will not be decayed
no_decay.add(fpn)
elif pn.endswith('weight') and isinstance(m, whitelist_weight_modules):
# weights of whitelist modules will be weight decayed
decay.add(fpn)
elif pn.endswith('weight') and isinstance(m, blacklist_weight_modules):
# weights of blacklist modules will NOT be weight decayed
no_decay.add(fpn)
# special case the position embedding parameter in the root GPT module as not decayed
no_decay.add('pos_emb')
# create the pytorch optimizer object
param_dict = {pn: p for pn, p in self.named_parameters()}
optim_groups = [
{"params": [param_dict[pn] for pn in sorted(list(decay))], "weight_decay": 0.01},
{"params": [param_dict[pn] for pn in sorted(list(no_decay))], "weight_decay": 0.0},
]
return optim_groups
def forward(self, image_tensor, lidar_tensor, radar_tensor, gps):
"""
Args:
image_tensor (tensor): B*4*seq_len, C, H, W
lidar_tensor (tensor): B*seq_len, C, H, W
gps (tensor): ego-gps
"""
bz = lidar_tensor.shape[0] // self.seq_len
h, w = lidar_tensor.shape[2:4]
# print('transfo',self.config.n_views , self.seq_len)
# forward the image model for token embeddings
image_tensor = image_tensor.view(bz, self.config.n_views * self.seq_len, -1, h, w)
lidar_tensor = lidar_tensor.view(bz, self.seq_len, -1, h, w)
radar_tensor = radar_tensor.view(bz, self.seq_len, -1, h, w)
# pad token embeddings along number of tokens dimension
token_embeddings = torch.cat([image_tensor, lidar_tensor, radar_tensor], dim=1).permute(0,1,3,4,2).contiguous()
# print(token_embeddings.shape)
token_embeddings = token_embeddings.view(bz, -1, self.n_embd) # (B, an * T, C)
token_embeddings = torch.cat([token_embeddings,gps], dim=1)
# add (learnable) positional embedding and gps embedding for all tokens
x = self.drop(self.pos_emb + token_embeddings ) # (B, an * T, C)
x = self.blocks(x) # (B, an * T, C)
x = self.ln_f(x) # (B, an * T, C)
pos_tensor_out = x[:, (self.config.n_views + 2) * self.seq_len * self.vert_anchors * self.horz_anchors:, :]
x = x[:,:(self.config.n_views + 2) * self.seq_len * self.vert_anchors * self.horz_anchors,:]
x = x.view(bz, (self.config.n_views + 2) * self.seq_len, self.vert_anchors, self.horz_anchors, self.n_embd)
x = x.permute(0,1,4,2,3).contiguous() # same as token_embeddings
image_tensor_out = x[:, :self.config.n_views*self.seq_len, :, :, :].contiguous().view(bz * self.config.n_views * self.seq_len, -1, h, w)
lidar_tensor_out = x[:, self.config.n_views*self.seq_len:(self.config.n_views+1)*self.seq_len, :, :, :].contiguous().view(bz * self.seq_len, -1, h, w)
radar_tensor_out = x[:, (self.config.n_views+1)*self.seq_len:, :, :, :].contiguous().view(bz * self.seq_len, -1, h, w)
return image_tensor_out, lidar_tensor_out, radar_tensor_out, pos_tensor_out
class Encoder(nn.Module):
"""
Multi-scale Fusion Transformer for image + LiDAR feature fusion
"""
def __init__(self, config):
super().__init__()
self.config = config
self.avgpool = nn.AdaptiveAvgPool2d((self.config.vert_anchors, self.config.horz_anchors))
self.image_encoder = ImageCNN(512, normalize=True)
self.lidar_encoder = LidarEncoder(num_classes=512, in_channels=1)
if config.add_velocity:
self.radar_encoder = LidarEncoder(num_classes=512, in_channels=2)
else:
self.radar_encoder = LidarEncoder(num_classes=512, in_channels=1)
self.vel_emb1 = nn.Linear(2, 64)
self.vel_emb2 = nn.Linear(64, 128)
self.vel_emb3 = nn.Linear(128, 256)
self.vel_emb4 = nn.Linear(256, 512)
self.transformer1 = GPT(n_embd=64,
n_head=config.n_head,
block_exp=config.block_exp,
n_layer=config.n_layer,
vert_anchors=config.vert_anchors,
horz_anchors=config.horz_anchors,
seq_len=config.seq_len,
embd_pdrop=config.embd_pdrop,
attn_pdrop=config.attn_pdrop,
resid_pdrop=config.resid_pdrop,
config=config)
self.transformer2 = GPT(n_embd=128,
n_head=config.n_head,
block_exp=config.block_exp,
n_layer=config.n_layer,
vert_anchors=config.vert_anchors,
horz_anchors=config.horz_anchors,
seq_len=config.seq_len,
embd_pdrop=config.embd_pdrop,
attn_pdrop=config.attn_pdrop,
resid_pdrop=config.resid_pdrop,
config=config)
self.transformer3 = GPT(n_embd=256,
n_head=config.n_head,
block_exp=config.block_exp,
n_layer=config.n_layer,
vert_anchors=config.vert_anchors,
horz_anchors=config.horz_anchors,
seq_len=config.seq_len,
embd_pdrop=config.embd_pdrop,
attn_pdrop=config.attn_pdrop,
resid_pdrop=config.resid_pdrop,
config=config)
self.transformer4 = GPT(n_embd=512,
n_head=config.n_head,
block_exp=config.block_exp,
n_layer=config.n_layer,
vert_anchors=config.vert_anchors,
horz_anchors=config.horz_anchors,
seq_len=config.seq_len,
embd_pdrop=config.embd_pdrop,
attn_pdrop=config.attn_pdrop,
resid_pdrop=config.resid_pdrop,
config=config)
def forward(self, image_list, lidar_list, radar_list, gps):
'''
Image + LiDAR feature fusion using transformers
Args:
image_list (list): list of input images
lidar_list (list): list of input LiDAR BEV
gps (tensor): input gps
'''
if self.image_encoder.normalize:
image_list = [normalize_imagenet(image_input) for image_input in image_list]
bz, _, h, w = lidar_list[0].shape
img_channel = image_list[0].shape[1]
lidar_channel = lidar_list[0].shape[1]
radar_channel = radar_list[0].shape[1]
self.config.n_views = len(image_list) // self.config.seq_len
image_tensor = torch.stack(image_list, dim=1).view(bz * self.config.n_views * self.config.seq_len, img_channel, h, w)
lidar_tensor = torch.stack(lidar_list, dim=1).view(bz * self.config.seq_len, lidar_channel, h, w)
radar_tensor = torch.stack(radar_list, dim=1).view(bz * self.config.seq_len, radar_channel, h, w)
image_features = self.image_encoder.features.conv1(image_tensor)
image_features = self.image_encoder.features.bn1(image_features)
image_features = self.image_encoder.features.relu(image_features)
image_features = self.image_encoder.features.maxpool(image_features)
lidar_features = self.lidar_encoder._model.conv1(lidar_tensor)
lidar_features = self.lidar_encoder._model.bn1(lidar_features)
lidar_features = self.lidar_encoder._model.relu(lidar_features)
lidar_features = self.lidar_encoder._model.maxpool(lidar_features)
radar_features = self.radar_encoder._model.conv1(radar_tensor)
radar_features = self.radar_encoder._model.bn1(radar_features)
radar_features = self.radar_encoder._model.relu(radar_features)
radar_features = self.radar_encoder._model.maxpool(radar_features)
image_features = self.image_encoder.features.layer1(image_features)
lidar_features = self.lidar_encoder._model.layer1(lidar_features)
radar_features = self.radar_encoder._model.layer1(radar_features)
# fusion at (B, 64, 64, 64)
image_embd_layer1 = self.avgpool(image_features)
lidar_embd_layer1 = self.avgpool(lidar_features)
radar_embd_layer1 = self.avgpool(radar_features)
gps_embd_layer1 = self.vel_emb1(gps)
image_features_layer1, lidar_features_layer1, radar_features_layer1, gps_features_layer1 = self.transformer1(image_embd_layer1, lidar_embd_layer1, radar_embd_layer1, gps_embd_layer1)
image_features_layer1 = F.interpolate(image_features_layer1, scale_factor=8, mode='bilinear')
lidar_features_layer1 = F.interpolate(lidar_features_layer1, scale_factor=8, mode='bilinear')
radar_features_layer1 = F.interpolate(radar_features_layer1, scale_factor=8, mode='bilinear')
image_features = image_features + image_features_layer1
lidar_features = lidar_features + lidar_features_layer1
radar_features = radar_features + radar_features_layer1
image_features = self.image_encoder.features.layer2(image_features)
lidar_features = self.lidar_encoder._model.layer2(lidar_features)
radar_features = self.radar_encoder._model.layer2(radar_features)
# fusion at (B, 128, 32, 32)
image_embd_layer2 = self.avgpool(image_features)
lidar_embd_layer2 = self.avgpool(lidar_features)
radar_embd_layer2 = self.avgpool(radar_features)
gps_embd_layer2 = self.vel_emb2(gps_features_layer1)
image_features_layer2, lidar_features_layer2, radar_features_layer2, gps_features_layer2 = self.transformer2(image_embd_layer2, lidar_embd_layer2, radar_embd_layer2, gps_embd_layer2)
image_features_layer2 = F.interpolate(image_features_layer2, scale_factor=4, mode='bilinear')
lidar_features_layer2 = F.interpolate(lidar_features_layer2, scale_factor=4, mode='bilinear')
radar_features_layer2 = F.interpolate(radar_features_layer2, scale_factor=4, mode='bilinear')
image_features = image_features + image_features_layer2
lidar_features = lidar_features + lidar_features_layer2
radar_features = radar_features + radar_features_layer2
image_features = self.image_encoder.features.layer3(image_features)
lidar_features = self.lidar_encoder._model.layer3(lidar_features)
radar_features = self.radar_encoder._model.layer3(radar_features)
# fusion at (B, 256, 16, 16)
image_embd_layer3 = self.avgpool(image_features)
lidar_embd_layer3 = self.avgpool(lidar_features)
radar_embd_layer3 = self.avgpool(radar_features)
gps_embd_layer3 = self.vel_emb3(gps_features_layer2)
image_features_layer3, lidar_features_layer3, radar_features_layer3, gps_features_layer3 = self.transformer3(image_embd_layer3, lidar_embd_layer3, radar_embd_layer3, gps_embd_layer3)
image_features_layer3 = F.interpolate(image_features_layer3, scale_factor=2, mode='bilinear')
lidar_features_layer3 = F.interpolate(lidar_features_layer3, scale_factor=2, mode='bilinear')
radar_features_layer3 = F.interpolate(radar_features_layer3, scale_factor=2, mode='bilinear')
image_features = image_features + image_features_layer3
lidar_features = lidar_features + lidar_features_layer3
radar_features = radar_features + radar_features_layer3
image_features = self.image_encoder.features.layer4(image_features)
lidar_features = self.lidar_encoder._model.layer4(lidar_features)
radar_features = self.radar_encoder._model.layer4(radar_features)
# fusion at (B, 512, 8, 8)
image_embd_layer4 = self.avgpool(image_features)
lidar_embd_layer4 = self.avgpool(lidar_features)
radar_embd_layer4 = self.avgpool(radar_features)
gps_embd_layer4 = self.vel_emb4(gps_features_layer3)
image_features_layer4, lidar_features_layer4, radar_features_layer4, gps_features_layer4 = self.transformer4(image_embd_layer4, lidar_embd_layer4, radar_embd_layer4, gps_embd_layer4)
image_features = image_features + image_features_layer4
lidar_features = lidar_features + lidar_features_layer4
radar_features = radar_features + radar_features_layer4
image_features = self.image_encoder.features.avgpool(image_features)
image_features = torch.flatten(image_features, 1)
image_features = image_features.view(bz, self.config.n_views * self.config.seq_len, -1)
lidar_features = self.lidar_encoder._model.avgpool(lidar_features)
lidar_features = torch.flatten(lidar_features, 1)
lidar_features = lidar_features.view(bz, self.config.seq_len, -1)
radar_features = self.radar_encoder._model.avgpool(radar_features)
radar_features = torch.flatten(radar_features, 1)
radar_features = radar_features.view(bz, self.config.seq_len, -1)
gps_features = gps_features_layer4
fused_features = torch.cat([image_features, lidar_features, radar_features, gps_features], dim=1)
# fused_features = torch.cat([image_features, lidar_features, radar_features], dim=1)
fused_features = torch.sum(fused_features, dim=1)
return fused_features
class TransFuser(nn.Module):
'''
Transformer-based feature fusion followed by GRU-based waypoint prediction network and PID controller
'''
def __init__(self, config, device):
super().__init__()
self.device = device
self.config = config
self.pred_len = config.pred_len
self.encoder = Encoder(config).to(self.device)
self.join = nn.Sequential(
nn.Linear(512, 256),
nn.ReLU(inplace=True),
nn.Linear(256, 128),
nn.ReLU(inplace=True),
nn.Linear(128, 64),
).to(self.device)
# self.decoder = nn.GRUCell(input_size=2, hidden_size=64).to(self.device)
# self.output = nn.Linear(64, 2).to(self.device)
def forward(self, image_list, lidar_list, radar_list, gps):
'''
Predicts waypoint from geometric feature projections of image + LiDAR input
Args:
image_list (list): list of input images
lidar_list (list): list of input LiDAR BEV
target_point (tensor): goal location registered to ego-frame
gps (tensor): input gps
'''
fused_features = self.encoder(image_list, lidar_list, radar_list, gps)
z = self.join(fused_features)
return z