-
Notifications
You must be signed in to change notification settings - Fork 24
/
mvt_single.py
473 lines (412 loc) · 14.1 KB
/
mvt_single.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
# Copyright (c) 2022-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# Licensed under the NVIDIA Source Code License [see LICENSE for details].
from math import ceil
import torch
import torch.nn.functional as F
from torch import nn
from einops import rearrange, repeat
from rvt.mvt.attn import (
Conv2DBlock,
Conv2DUpsampleBlock,
PreNorm,
Attention,
cache_fn,
DenseBlock,
FeedForward,
)
class MVT(nn.Module):
def __init__(
self,
depth,
img_size,
add_proprio,
proprio_dim,
add_lang,
lang_dim,
lang_len,
img_feat_dim,
feat_dim,
im_channels,
attn_dim,
attn_heads,
attn_dim_head,
activation,
weight_tie_layers,
attn_dropout,
decoder_dropout,
img_patch_size,
final_dim,
self_cross_ver,
add_corr,
add_pixel_loc,
add_depth,
pe_fix,
renderer_device="cuda:0",
renderer=None,
):
"""MultiView Transfomer
:param depth: depth of the attention network
:param img_size: number of pixels per side for rendering
:param renderer_device: device for placing the renderer
:param add_proprio:
:param proprio_dim:
:param add_lang:
:param lang_dim:
:param lang_len:
:param img_feat_dim:
:param feat_dim:
:param im_channels: intermediate channel size
:param attn_dim:
:param attn_heads:
:param attn_dim_head:
:param activation:
:param weight_tie_layers:
:param attn_dropout:
:param decoder_dropout:
:param img_patch_size: intial patch size
:param final_dim: final dimensions of features
:param self_cross_ver:
:param add_corr:
:param add_pixel_loc:
:param add_depth:
:param pe_fix: matter only when add_lang is True
Either:
True: use position embedding only for image tokens
False: use position embedding for lang and image token
"""
super().__init__()
self.depth = depth
self.img_feat_dim = img_feat_dim
self.img_size = img_size
self.add_proprio = add_proprio
self.proprio_dim = proprio_dim
self.add_lang = add_lang
self.lang_dim = lang_dim
self.lang_len = lang_len
self.im_channels = im_channels
self.img_patch_size = img_patch_size
self.final_dim = final_dim
self.attn_dropout = attn_dropout
self.decoder_dropout = decoder_dropout
self.self_cross_ver = self_cross_ver
self.add_corr = add_corr
self.add_pixel_loc = add_pixel_loc
self.add_depth = add_depth
self.pe_fix = pe_fix
print(f"MVT Vars: {vars(self)}")
assert not renderer is None
self.renderer = renderer
self.num_img = self.renderer.num_img
# patchified input dimensions
spatial_size = img_size // self.img_patch_size # 128 / 8 = 16
if self.add_proprio:
# 64 img features + 64 proprio features
self.input_dim_before_seq = self.im_channels * 2
else:
self.input_dim_before_seq = self.im_channels
# learnable positional encoding
if add_lang:
lang_emb_dim, lang_max_seq_len = lang_dim, lang_len
else:
lang_emb_dim, lang_max_seq_len = 0, 0
self.lang_emb_dim = lang_emb_dim
self.lang_max_seq_len = lang_max_seq_len
if self.pe_fix:
num_pe_token = spatial_size**2 * self.num_img
else:
num_pe_token = lang_max_seq_len + (spatial_size**2 * self.num_img)
self.pos_encoding = nn.Parameter(
torch.randn(
1,
num_pe_token,
self.input_dim_before_seq,
)
)
inp_img_feat_dim = self.img_feat_dim
if self.add_corr:
inp_img_feat_dim += 3
if self.add_pixel_loc:
inp_img_feat_dim += 3
self.pixel_loc = torch.zeros(
(self.num_img, 3, self.img_size, self.img_size)
)
self.pixel_loc[:, 0, :, :] = (
torch.linspace(-1, 1, self.num_img).unsqueeze(-1).unsqueeze(-1)
)
self.pixel_loc[:, 1, :, :] = (
torch.linspace(-1, 1, self.img_size).unsqueeze(0).unsqueeze(-1)
)
self.pixel_loc[:, 2, :, :] = (
torch.linspace(-1, 1, self.img_size).unsqueeze(0).unsqueeze(0)
)
if self.add_depth:
inp_img_feat_dim += 1
# img input preprocessing encoder
self.input_preprocess = Conv2DBlock(
inp_img_feat_dim,
self.im_channels,
kernel_sizes=1,
strides=1,
norm=None,
activation=activation,
)
inp_pre_out_dim = self.im_channels
if self.add_proprio:
# proprio preprocessing encoder
self.proprio_preprocess = DenseBlock(
self.proprio_dim,
self.im_channels,
norm="group",
activation=activation,
)
self.patchify = Conv2DBlock(
inp_pre_out_dim,
self.im_channels,
kernel_sizes=self.img_patch_size,
strides=self.img_patch_size,
norm="group",
activation=activation,
padding=0,
)
# lang preprocess
if self.add_lang:
self.lang_preprocess = DenseBlock(
lang_emb_dim,
self.im_channels * 2,
norm="group",
activation=activation,
)
self.fc_bef_attn = DenseBlock(
self.input_dim_before_seq,
attn_dim,
norm=None,
activation=None,
)
self.fc_aft_attn = DenseBlock(
attn_dim,
self.input_dim_before_seq,
norm=None,
activation=None,
)
get_attn_attn = lambda: PreNorm(
attn_dim,
Attention(
attn_dim,
heads=attn_heads,
dim_head=attn_dim_head,
dropout=attn_dropout,
),
)
get_attn_ff = lambda: PreNorm(attn_dim, FeedForward(attn_dim))
get_attn_attn, get_attn_ff = map(cache_fn, (get_attn_attn, get_attn_ff))
# self-attention layers
self.layers = nn.ModuleList([])
cache_args = {"_cache": weight_tie_layers}
attn_depth = depth
for _ in range(attn_depth):
self.layers.append(
nn.ModuleList([get_attn_attn(**cache_args), get_attn_ff(**cache_args)])
)
self.up0 = Conv2DUpsampleBlock(
self.input_dim_before_seq,
self.im_channels,
kernel_sizes=self.img_patch_size,
strides=self.img_patch_size,
norm=None,
activation=activation,
)
final_inp_dim = self.im_channels + inp_pre_out_dim
# final layers
self.final = Conv2DBlock(
final_inp_dim,
self.im_channels,
kernel_sizes=3,
strides=1,
norm=None,
activation=activation,
)
self.trans_decoder = Conv2DBlock(
self.final_dim,
1,
kernel_sizes=3,
strides=1,
norm=None,
activation=None,
)
feat_out_size = feat_dim
feat_fc_dim = 0
feat_fc_dim += self.input_dim_before_seq
feat_fc_dim += self.final_dim
self.feat_fc = nn.Sequential(
nn.Linear(self.num_img * feat_fc_dim, feat_fc_dim),
nn.ReLU(),
nn.Linear(feat_fc_dim, feat_fc_dim // 2),
nn.ReLU(),
nn.Linear(feat_fc_dim // 2, feat_out_size),
)
def get_pt_loc_on_img(self, pt, dyn_cam_info):
"""
transform location of points in the local frame to location on the
image
:param pt: (bs, np, 3)
:return: pt_img of size (bs, np, num_img, 2)
"""
pt_img = self.renderer.get_pt_loc_on_img(
pt, fix_cam=True, dyn_cam_info=dyn_cam_info
)
return pt_img
def forward(
self,
img,
proprio=None,
lang_emb=None,
**kwargs,
):
"""
:param img: tensor of shape (bs, num_img, img_feat_dim, h, w)
:param proprio: tensor of shape (bs, priprio_dim)
:param lang_emb: tensor of shape (bs, lang_len, lang_dim)
:param img_aug: (float) magnitude of augmentation in rgb image
"""
bs, num_img, img_feat_dim, h, w = img.shape
num_pat_img = h // self.img_patch_size
assert num_img == self.num_img
# assert img_feat_dim == self.img_feat_dim
assert h == w == self.img_size
img = img.view(bs * num_img, img_feat_dim, h, w)
# preprocess
# (bs * num_img, im_channels, h, w)
d0 = self.input_preprocess(img)
# (bs * num_img, im_channels, h, w) ->
# (bs * num_img, im_channels, h / img_patch_strid, w / img_patch_strid) patches
ins = self.patchify(d0)
# (bs, im_channels, num_img, h / img_patch_strid, w / img_patch_strid) patches
ins = (
ins.view(
bs,
num_img,
self.im_channels,
num_pat_img,
num_pat_img,
)
.transpose(1, 2)
.clone()
)
# concat proprio
_, _, _d, _h, _w = ins.shape
if self.add_proprio:
p = self.proprio_preprocess(proprio) # [B,4] -> [B,64]
p = p.unsqueeze(-1).unsqueeze(-1).unsqueeze(-1).repeat(1, 1, _d, _h, _w)
ins = torch.cat([ins, p], dim=1) # [B, 128, num_img, np, np]
# channel last
ins = rearrange(ins, "b d ... -> b ... d") # [B, num_img, np, np, 128]
# save original shape of input for layer
ins_orig_shape = ins.shape
# flatten patches into sequence
ins = rearrange(ins, "b ... d -> b (...) d") # [B, num_img * np * np, 128]
# add learable pos encoding
# only added to image tokens
if self.pe_fix:
ins += self.pos_encoding
# append language features as sequence
num_lang_tok = 0
if self.add_lang:
l = self.lang_preprocess(
lang_emb.view(bs * self.lang_max_seq_len, self.lang_emb_dim)
)
l = l.view(bs, self.lang_max_seq_len, -1)
num_lang_tok = l.shape[1]
ins = torch.cat((l, ins), dim=1) # [B, num_img * np * np + 77, 128]
# add learable pos encoding
if not self.pe_fix:
ins = ins + self.pos_encoding
x = self.fc_bef_attn(ins)
if self.self_cross_ver == 0:
# self-attention layers
for self_attn, self_ff in self.layers:
x = self_attn(x) + x
x = self_ff(x) + x
elif self.self_cross_ver == 1:
lx, imgx = x[:, :num_lang_tok], x[:, num_lang_tok:]
# within image self attention
imgx = imgx.reshape(bs * num_img, num_pat_img * num_pat_img, -1)
for self_attn, self_ff in self.layers[: len(self.layers) // 2]:
imgx = self_attn(imgx) + imgx
imgx = self_ff(imgx) + imgx
imgx = imgx.view(bs, num_img * num_pat_img * num_pat_img, -1)
x = torch.cat((lx, imgx), dim=1)
# cross attention
for self_attn, self_ff in self.layers[len(self.layers) // 2 :]:
x = self_attn(x) + x
x = self_ff(x) + x
else:
assert False
# append language features as sequence
if self.add_lang:
# throwing away the language embeddings
x = x[:, num_lang_tok:]
x = self.fc_aft_attn(x)
# reshape back to orginal size
x = x.view(bs, *ins_orig_shape[1:-1], x.shape[-1]) # [B, num_img, np, np, 128]
x = rearrange(x, "b ... d -> b d ...") # [B, 128, num_img, np, np]
feat = []
_feat = torch.max(torch.max(x, dim=-1)[0], dim=-1)[0]
_feat = _feat.view(bs, -1)
feat.append(_feat)
x = (
x.transpose(1, 2)
.clone()
.view(
bs * self.num_img, self.input_dim_before_seq, num_pat_img, num_pat_img
)
)
u0 = self.up0(x)
u0 = torch.cat([u0, d0], dim=1)
u = self.final(u0)
# translation decoder
trans = self.trans_decoder(u).view(bs, self.num_img, h, w)
hm = F.softmax(trans.detach().view(bs, self.num_img, h * w), 2).view(
bs * self.num_img, 1, h, w
)
_feat = torch.sum(hm * u, dim=[2, 3])
_feat = _feat.view(bs, -1)
feat.append(_feat)
feat = torch.cat(feat, dim=-1)
feat = self.feat_fc(feat)
out = {"trans": trans, "feat": feat}
return out
def get_wpt(self, out, dyn_cam_info, y_q=None):
"""
Estimate the q-values given output from mvt
:param out: output from mvt
"""
nc = self.num_img
h = w = self.img_size
bs = out["trans"].shape[0]
q_trans = out["trans"].view(bs, nc, h * w)
hm = torch.nn.functional.softmax(q_trans, 2)
hm = hm.view(bs, nc, h, w)
if dyn_cam_info is None:
dyn_cam_info_itr = (None,) * bs
else:
dyn_cam_info_itr = dyn_cam_info
pred_wpt = [
self.renderer.get_max_3d_frm_hm_cube(
hm[i : i + 1],
fix_cam=True,
dyn_cam_info=dyn_cam_info_itr[i : i + 1]
if not (dyn_cam_info_itr[i] is None)
else None,
)
for i in range(bs)
]
pred_wpt = torch.cat(pred_wpt, 0)
assert y_q is None
return pred_wpt
def free_mem(self):
"""
Could be used for freeing up the memory once a batch of testing is done
"""
print("Freeing up some memory")
self.renderer.free_mem()