-
Notifications
You must be signed in to change notification settings - Fork 34
/
mile.py
433 lines (368 loc) · 18.2 KB
/
mile.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
import torch
import torch.nn as nn
import timm
from mile.constants import CARLA_FPS, DISPLAY_SEGMENTATION
from mile.utils.network_utils import pack_sequence_dim, unpack_sequence_dim, remove_past
from mile.models.common import BevDecoder, Decoder, RouteEncode, Policy
from mile.models.frustum_pooling import FrustumPooling
from mile.layers.layers import BasicBlock
from mile.models.transition import RSSM
class Mile(nn.Module):
def __init__(self, cfg):
super().__init__()
self.cfg = cfg
self.receptive_field = cfg.RECEPTIVE_FIELD
# Image feature encoder
if self.cfg.MODEL.ENCODER.NAME == 'resnet18':
self.encoder = timm.create_model(
cfg.MODEL.ENCODER.NAME, pretrained=True, features_only=True, out_indices=[2, 3, 4],
)
feature_info = self.encoder.feature_info.get_dicts(keys=['num_chs', 'reduction'])
self.feat_decoder = Decoder(feature_info, self.cfg.MODEL.ENCODER.OUT_CHANNELS)
if not self.cfg.EVAL.NO_LIFTING:
# Frustum pooling
bev_downsample = cfg.BEV.FEATURE_DOWNSAMPLE
self.frustum_pooling = FrustumPooling(
size=(cfg.BEV.SIZE[0] // bev_downsample, cfg.BEV.SIZE[1] // bev_downsample),
scale=cfg.BEV.RESOLUTION * bev_downsample,
offsetx=cfg.BEV.OFFSET_FORWARD / bev_downsample,
dbound=cfg.BEV.FRUSTUM_POOL.D_BOUND,
downsample=8,
)
# mono depth head
self.depth_decoder = Decoder(feature_info, self.cfg.MODEL.ENCODER.OUT_CHANNELS)
self.depth = nn.Conv2d(self.depth_decoder.out_channels, self.frustum_pooling.D, kernel_size=1)
# only lift argmax of depth distribution for speed
self.sparse_depth = cfg.BEV.FRUSTUM_POOL.SPARSE
self.sparse_depth_count = cfg.BEV.FRUSTUM_POOL.SPARSE_COUNT
backbone_bev_in_channels = self.cfg.MODEL.ENCODER.OUT_CHANNELS
# Route map
if self.cfg.MODEL.ROUTE.ENABLED:
self.backbone_route = RouteEncode(cfg.MODEL.ROUTE.CHANNELS, cfg.MODEL.ROUTE.BACKBONE)
backbone_bev_in_channels += self.backbone_route.out_channels
# Measurements
if self.cfg.MODEL.MEASUREMENTS.ENABLED:
self.command_encoder = nn.Sequential(
nn.Embedding(6, self.cfg.MODEL.MEASUREMENTS.COMMAND_CHANNELS),
nn.Linear(self.cfg.MODEL.MEASUREMENTS.COMMAND_CHANNELS, self.cfg.MODEL.MEASUREMENTS.COMMAND_CHANNELS),
nn.ReLU(True),
nn.Linear(self.cfg.MODEL.MEASUREMENTS.COMMAND_CHANNELS, self.cfg.MODEL.MEASUREMENTS.COMMAND_CHANNELS),
nn.ReLU(True),
)
self.command_next_encoder = nn.Sequential(
nn.Embedding(6, self.cfg.MODEL.MEASUREMENTS.COMMAND_CHANNELS),
nn.Linear(self.cfg.MODEL.MEASUREMENTS.COMMAND_CHANNELS, self.cfg.MODEL.MEASUREMENTS.COMMAND_CHANNELS),
nn.ReLU(True),
nn.Linear(self.cfg.MODEL.MEASUREMENTS.COMMAND_CHANNELS, self.cfg.MODEL.MEASUREMENTS.COMMAND_CHANNELS),
nn.ReLU(True),
)
self.gps_encoder = nn.Sequential(
nn.Linear(2*2, self.cfg.MODEL.MEASUREMENTS.GPS_CHANNELS),
nn.ReLU(True),
nn.Linear(self.cfg.MODEL.MEASUREMENTS.GPS_CHANNELS, self.cfg.MODEL.MEASUREMENTS.GPS_CHANNELS),
nn.ReLU(True),
)
backbone_bev_in_channels += 2*self.cfg.MODEL.MEASUREMENTS.COMMAND_CHANNELS
backbone_bev_in_channels += self.cfg.MODEL.MEASUREMENTS.GPS_CHANNELS
# Speed as input
self.speed_enc = nn.Sequential(
nn.Linear(1, cfg.MODEL.SPEED.CHANNELS),
nn.ReLU(True),
nn.Linear(cfg.MODEL.SPEED.CHANNELS, cfg.MODEL.SPEED.CHANNELS),
nn.ReLU(True),
)
backbone_bev_in_channels += cfg.MODEL.SPEED.CHANNELS
self.speed_normalisation = cfg.SPEED.NORMALISATION
# Bev network
self.backbone_bev = timm.create_model(
cfg.MODEL.BEV.BACKBONE,
in_chans=backbone_bev_in_channels,
pretrained=True,
features_only=True,
out_indices=[3],
)
feature_info_bev = self.backbone_bev.feature_info.get_dicts(keys=['num_chs', 'reduction'])
embedding_n_channels = self.cfg.MODEL.EMBEDDING_DIM
self.final_state_conv = nn.Sequential(
BasicBlock(feature_info_bev[-1]['num_chs'], embedding_n_channels, stride=2, downsample=True),
BasicBlock(embedding_n_channels, embedding_n_channels),
nn.AdaptiveAvgPool2d(output_size=(1, 1)),
nn.Flatten(start_dim=1),
)
# Recurrent model
self.receptive_field = self.cfg.RECEPTIVE_FIELD
if self.cfg.MODEL.TRANSITION.ENABLED:
# Recurrent state sequence module
self.rssm = RSSM(
embedding_dim=embedding_n_channels,
action_dim=self.cfg.MODEL.ACTION_DIM,
hidden_state_dim=self.cfg.MODEL.TRANSITION.HIDDEN_STATE_DIM,
state_dim=self.cfg.MODEL.TRANSITION.STATE_DIM,
action_latent_dim=self.cfg.MODEL.TRANSITION.ACTION_LATENT_DIM,
receptive_field=self.receptive_field,
use_dropout=self.cfg.MODEL.TRANSITION.USE_DROPOUT,
dropout_probability=self.cfg.MODEL.TRANSITION.DROPOUT_PROBABILITY,
)
# Policy
if self.cfg.MODEL.TRANSITION.ENABLED:
state_dim = self.cfg.MODEL.TRANSITION.HIDDEN_STATE_DIM + self.cfg.MODEL.TRANSITION.STATE_DIM
else:
state_dim = embedding_n_channels
self.policy = Policy(in_channels=state_dim)
# Bird's-eye view semantic segmentation
if self.cfg.SEMANTIC_SEG.ENABLED:
self.bev_decoder = BevDecoder(
latent_n_channels=state_dim,
semantic_n_channels=self.cfg.SEMANTIC_SEG.N_CHANNELS,
)
# RGB reconstruction
if self.cfg.EVAL.RGB_SUPERVISION:
self.rgb_decoder = BevDecoder(
latent_n_channels=state_dim,
semantic_n_channels=3,
constant_size=(5, 13),
is_segmentation=False,
)
# Used during deployment to save last state
self.last_h = None
self.last_sample = None
self.last_action = None
self.count = 0
def forward(self, batch, deployment=False):
"""
Parameters
----------
batch: dict of torch.Tensor
keys:
image: (b, s, 3, h, w)
route_map: (b, s, 3, h_r, w_r)
speed: (b, s, 1)
intrinsics: (b, s, 3, 3)
extrinsics: (b, s, 4, 4)
throttle_brake: (b, s, 1)
steering: (b, s, 1)
"""
# Encode RGB images, route_map, speed using intrinsics and extrinsics
# to a 512 dimensional vector
b, s = batch['image'].shape[:2]
embedding = self.encode(batch) # dim (b, s, 512)
output = dict()
if self.cfg.MODEL.TRANSITION.ENABLED:
# Recurrent state sequence module
if deployment:
action = batch['action']
else:
action = torch.cat([batch['throttle_brake'], batch['steering']], dim=-1)
state_dict = self.rssm(embedding, action, use_sample=not deployment, policy=self.policy)
if deployment:
state_dict = remove_past(state_dict, s)
s = 1
output = {**output, **state_dict}
state = torch.cat([state_dict['posterior']['hidden_state'], state_dict['posterior']['sample']], dim=-1)
else:
state = embedding
state = pack_sequence_dim(state)
output_policy = self.policy(state)
throttle_brake, steering = torch.split(output_policy, 1, dim=-1)
output['throttle_brake'] = unpack_sequence_dim(throttle_brake, b, s)
output['steering'] = unpack_sequence_dim(steering, b, s)
if self.cfg.SEMANTIC_SEG.ENABLED:
if (not deployment) or (deployment and DISPLAY_SEGMENTATION):
bev_decoder_output = self.bev_decoder(state)
bev_decoder_output = unpack_sequence_dim(bev_decoder_output, b, s)
output = {**output, **bev_decoder_output}
if self.cfg.EVAL.RGB_SUPERVISION:
rgb_decoder_output = self.rgb_decoder(state)
rgb_decoder_output = unpack_sequence_dim(rgb_decoder_output, b, s)
output = {**output, **rgb_decoder_output}
return output
def encode(self, batch):
b, s = batch['image'].shape[:2]
image = pack_sequence_dim(batch['image'])
speed = pack_sequence_dim(batch['speed'])
intrinsics = pack_sequence_dim(batch['intrinsics'])
extrinsics = pack_sequence_dim(batch['extrinsics'])
# Image encoder, multiscale
xs = self.encoder(image)
# Lift features to bird's-eye view.
# Aggregate features to output resolution (H/8, W/8)
x = self.feat_decoder(xs)
if not self.cfg.EVAL.NO_LIFTING:
# Depth distribution
depth = self.depth(self.depth_decoder(xs)).softmax(dim=1)
if self.sparse_depth:
# only lift depth for topk most likely depth bins
topk_bins = depth.topk(self.sparse_depth_count, dim=1)[1]
depth_mask = torch.zeros(depth.shape, device=depth.device, dtype=torch.bool)
depth_mask.scatter_(1, topk_bins, 1)
else:
depth_mask = torch.zeros(0, device=depth.device)
x = (depth.unsqueeze(1) * x.unsqueeze(2)).type_as(x) # outer product
# Add camera dimension
x = x.unsqueeze(1)
x = x.permute(0, 1, 3, 4, 5, 2)
x = self.frustum_pooling(x, intrinsics.unsqueeze(1), extrinsics.unsqueeze(1), depth_mask)
if self.cfg.MODEL.ROUTE.ENABLED:
route_map = pack_sequence_dim(batch['route_map'])
route_map_features = self.backbone_route(route_map)
route_map_features = route_map_features.unsqueeze(2).unsqueeze(3).expand(-1, -1, x.shape[2], x.shape[3])
x = torch.cat([x, route_map_features], dim=1)
if self.cfg.MODEL.MEASUREMENTS.ENABLED:
route_command = pack_sequence_dim(batch['route_command'])
gps_vector = pack_sequence_dim(batch['gps_vector'])
route_command_next = pack_sequence_dim(batch['route_command_next'])
gps_vector_next = pack_sequence_dim(batch['gps_vector_next'])
command_features = self.command_encoder(route_command)
command_features = command_features.unsqueeze(2).unsqueeze(3).expand(-1, -1, x.shape[2], x.shape[3])
x = torch.cat([x, command_features], dim=1)
command_next_features = self.command_next_encoder(route_command_next)
command_next_features = command_next_features.unsqueeze(2).unsqueeze(3).expand(-1, -1, x.shape[2], x.shape[3])
x = torch.cat([x, command_next_features], dim=1)
gps_features = self.gps_encoder(torch.cat([gps_vector, gps_vector_next], dim=-1))
gps_features = gps_features.unsqueeze(2).unsqueeze(3).expand(-1, -1, x.shape[2], x.shape[3])
x = torch.cat([x, gps_features], dim=1)
speed_features = self.speed_enc(speed / self.speed_normalisation)
speed_features = speed_features.unsqueeze(2).unsqueeze(3).expand(-1, -1, x.shape[2], x.shape[3])
x = torch.cat((x, speed_features), 1)
embedding = self.backbone_bev(x)[-1]
embedding = self.final_state_conv(embedding)
embedding = unpack_sequence_dim(embedding, b, s)
return embedding
def observe_and_imagine(self, batch, predict_action=False, future_horizon=None):
""" This is only used for visualisation of future prediction"""
assert self.cfg.MODEL.TRANSITION.ENABLED and self.cfg.SEMANTIC_SEG.ENABLED
if future_horizon is None:
future_horizon = self.cfg.FUTURE_HORIZON
b, s = batch['image'].shape[:2]
if not predict_action:
assert batch['throttle_brake'].shape[1] == s + future_horizon
assert batch['steering'].shape[1] == s + future_horizon
# Observe past context
output_observe = self.forward(batch)
# Imagine future states
output_imagine = {
'action': [],
'state': [],
'hidden': [],
'sample': [],
}
h_t = output_observe['posterior']['hidden_state'][:, -1]
sample_t = output_observe['posterior']['sample'][:, -1]
for t in range(future_horizon):
if predict_action:
action_t = self.policy(torch.cat([h_t, sample_t], dim=-1))
else:
action_t = torch.cat([batch['throttle_brake'][:, s+t], batch['steering'][:, s+t]], dim=-1)
prior_t = self.rssm.imagine_step(
h_t, sample_t, action_t, use_sample=True, policy=self.policy,
)
sample_t = prior_t['sample']
h_t = prior_t['hidden_state']
output_imagine['action'].append(action_t)
output_imagine['state'].append(torch.cat([h_t, sample_t], dim=-1))
output_imagine['hidden'].append(h_t)
output_imagine['sample'].append(sample_t)
for k, v in output_imagine.items():
output_imagine[k] = torch.stack(v, dim=1)
bev_decoder_output = self.bev_decoder(pack_sequence_dim(output_imagine['state']))
bev_decoder_output = unpack_sequence_dim(bev_decoder_output, b, future_horizon)
output_imagine = {**output_imagine, **bev_decoder_output}
return output_observe, output_imagine
def imagine(self, batch, predict_action=False, future_horizon=None):
""" This is only used for visualisation of future prediction"""
assert self.cfg.MODEL.TRANSITION.ENABLED and self.cfg.SEMANTIC_SEG.ENABLED
if future_horizon is None:
future_horizon = self.cfg.FUTURE_HORIZON
# Imagine future states
output_imagine = {
'action': [],
'state': [],
'hidden': [],
'sample': [],
}
h_t = batch['hidden_state'] #(b, c)
sample_t = batch['sample'] #(b, s)
b = h_t.shape[0]
for t in range(future_horizon):
if predict_action:
action_t = self.policy(torch.cat([h_t, sample_t], dim=-1))
else:
action_t = torch.cat([batch['throttle_brake'][:, t], batch['steering'][:, t]], dim=-1)
prior_t = self.rssm.imagine_step(
h_t, sample_t, action_t, use_sample=True, policy=self.policy,
)
sample_t = prior_t['sample']
h_t = prior_t['hidden_state']
output_imagine['action'].append(action_t)
output_imagine['state'].append(torch.cat([h_t, sample_t], dim=-1))
output_imagine['hidden'].append(h_t)
output_imagine['sample'].append(sample_t)
for k, v in output_imagine.items():
output_imagine[k] = torch.stack(v, dim=1)
bev_decoder_output = self.bev_decoder(pack_sequence_dim(output_imagine['state']))
bev_decoder_output = unpack_sequence_dim(bev_decoder_output, b, future_horizon)
output_imagine = {**output_imagine, **bev_decoder_output}
return output_imagine
def deployment_forward(self, batch, is_dreaming):
"""
Keep latent states in memory for fast inference.
Parameters
----------
batch: dict of torch.Tensor
keys:
image: (b, s, 3, h, w)
route_map: (b, s, 3, h_r, w_r)
speed: (b, s, 1)
intrinsics: (b, s, 3, 3)
extrinsics: (b, s, 4, 4)
throttle_brake: (b, s, 1)
steering: (b, s, 1)
"""
assert self.cfg.MODEL.TRANSITION.ENABLED
b = batch['image'].shape[0]
if self.count == 0:
# Encode RGB images, route_map, speed using intrinsics and extrinsics
# to a 512 dimensional vector
s = batch['image'].shape[1]
action_t = batch['action'][:, -2] # action from t-1 to t
batch = remove_past(batch, s)
embedding_t = self.encode(batch)[:, -1] # dim (b, 1, 512)
# Recurrent state sequence module
if self.last_h is None:
h_t = action_t.new_zeros(b, self.cfg.MODEL.TRANSITION.HIDDEN_STATE_DIM)
sample_t = action_t.new_zeros(b, self.cfg.MODEL.TRANSITION.STATE_DIM)
else:
h_t = self.last_h
sample_t = self.last_sample
if is_dreaming:
rssm_output = self.rssm.imagine_step(
h_t, sample_t, action_t, use_sample=False, policy=self.policy,
)
else:
rssm_output = self.rssm.observe_step(
h_t, sample_t, action_t, embedding_t, use_sample=False, policy=self.policy,
)['posterior']
sample_t = rssm_output['sample']
h_t = rssm_output['hidden_state']
self.last_h = h_t
self.last_sample = sample_t
game_frequency = CARLA_FPS
model_stride_sec = self.cfg.DATASET.STRIDE_SEC
n_image_per_stride = int(game_frequency * model_stride_sec)
self.count = n_image_per_stride - 1
else:
self.count -= 1
s = 1
state = torch.cat([self.last_h, self.last_sample], dim=-1)
output_policy = self.policy(state)
throttle_brake, steering = torch.split(output_policy, 1, dim=-1)
output = dict()
output['throttle_brake'] = unpack_sequence_dim(throttle_brake, b, s)
output['steering'] = unpack_sequence_dim(steering, b, s)
output['hidden_state'] = self.last_h
output['sample'] = self.last_sample
if self.cfg.SEMANTIC_SEG.ENABLED and DISPLAY_SEGMENTATION:
bev_decoder_output = self.bev_decoder(state)
bev_decoder_output = unpack_sequence_dim(bev_decoder_output, b, s)
output = {**output, **bev_decoder_output}
return output