-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel_jrdb.py
281 lines (201 loc) · 10.9 KB
/
model_jrdb.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
import numpy as np
import argparse
from datetime import datetime
class AuxilliaryEncoderCMT(nn.TransformerEncoder):
def __init__(self, encoder_layer_local, num_layers, norm=None):
super(AuxilliaryEncoderCMT, self).__init__(encoder_layer=encoder_layer_local,
num_layers=num_layers,
norm=norm)
def forward(self, src, mask=None, src_key_padding_mask=None, get_attn=False):
output = src
attn_matrices = []
for i, mod in enumerate(self.layers):
output = mod(output, src_mask=mask, src_key_padding_mask=src_key_padding_mask)
if self.norm is not None:
output = self.norm(output)
return output
class AuxilliaryEncoderST(nn.TransformerEncoder):
def __init__(self, encoder_layer_local, num_layers, norm=None):
super(AuxilliaryEncoderST, self).__init__(encoder_layer=encoder_layer_local,
num_layers=num_layers,
norm=norm)
def forward(self, src, mask=None, src_key_padding_mask=None, get_attn=False):
output = src
attn_matrices = []
for i, mod in enumerate(self.layers):
output = mod(output, src_mask=mask, src_key_padding_mask=src_key_padding_mask)
if self.norm is not None:
output = self.norm(output)
return output
class LearnedIDEncoding(nn.Module):
def __init__(self, d_model: int, dropout: float = 0.1, max_len: int = 5000, seq_len=21, device='cuda:0'):
super().__init__()
self.dropout = nn.Dropout(p=dropout)
self.device = device
self.person_encoding = nn.Embedding(1000, d_model, max_norm=True).to(device)
def forward(self, x: torch.Tensor, num_people=1) -> torch.Tensor:
seq_len = 21
x = x + self.person_encoding(torch.arange(num_people).repeat_interleave(seq_len, dim=0).to(self.device)).unsqueeze(1)
return self.dropout(x)
class LearnedTrajandIDEncoding(nn.Module):
def __init__(self, d_model: int, dropout: float = 0.1, max_len: int = 5000, seq_len=21, device='cuda:0'):
super().__init__()
self.dropout = nn.Dropout(p=dropout)
self.device = device
self.learned_encoding = nn.Embedding(seq_len, d_model//2, max_norm=True).to(device)
self.person_encoding = nn.Embedding(1000, d_model//2, max_norm=True).to(device)
def forward(self, x: torch.Tensor, num_people=1) -> torch.Tensor:
seq_len = 21
half = x.size(3)//2 ## 124
x[:,:,:,0:half*2:2] = x[:,:,:,0:half*2:2] + self.learned_encoding(torch.arange(seq_len).to(self.device)).unsqueeze(1).unsqueeze(0)
x[:,:,:,1:half*2:2] = x[:,:,:,1:half*2:2] + self.person_encoding(torch.arange(num_people).unsqueeze(0).repeat_interleave(seq_len, dim=0).to(self.device)).unsqueeze(0)
return self.dropout(x)
class Learnedbb3dEncoding(nn.Module):
def __init__(self, d_model: int, dropout: float = 0.1, max_len: int = 5000, seq_len=9, device='cuda:0'):
super().__init__()
self.dropout = nn.Dropout(p=dropout)
self.device = device
self.learned_encoding = nn.Embedding(seq_len, d_model, max_norm=True).to(device)
def forward(self, x: torch.Tensor) -> torch.Tensor:
seq_len = 9
x = x + self.learned_encoding(torch.arange(seq_len).to(self.device)).unsqueeze(1).unsqueeze(0)
return self.dropout(x)
class Learnedbb2dEncoding(nn.Module):
def __init__(self, d_model: int, dropout: float = 0.1, max_len: int = 5000, seq_len=9, device='cuda:0'):
super().__init__()
self.dropout = nn.Dropout(p=dropout)
self.device = device
self.learned_encoding = nn.Embedding(seq_len, d_model, max_norm=True).to(device)
def forward(self, x: torch.Tensor) -> torch.Tensor:
seq_len = 9
x = x + self.learned_encoding(torch.arange(seq_len).to(self.device)).unsqueeze(1).unsqueeze(0)
return self.dropout(x)
class Learnedpose3dEncoding(nn.Module):
def __init__(self, d_model: int, dropout: float = 0.1, max_len: int = 5000, seq_len=198, device='cuda:0'):
super().__init__()
self.dropout = nn.Dropout(p=dropout)
self.device = device
self.learned_encoding = nn.Embedding(seq_len, d_model, max_norm=True).to(device)
def forward(self, x: torch.Tensor) -> torch.Tensor:
seq_len = x.size(1)
x = x + self.learned_encoding(torch.arange(seq_len).to(self.device)).unsqueeze(1).unsqueeze(0)
return self.dropout(x)
class Learnedpose2dEncoding(nn.Module):
def __init__(self, d_model: int, dropout: float = 0.1, max_len: int = 5000, seq_len=198, device='cuda:0'):
super().__init__()
self.dropout = nn.Dropout(p=dropout)
self.device = device
self.learned_encoding = nn.Embedding(seq_len, d_model, max_norm=True).to(device)
def forward(self, x: torch.Tensor) -> torch.Tensor:
seq_len = x.size(1)
x = x + self.learned_encoding(torch.arange(seq_len).to(self.device)).unsqueeze(1).unsqueeze(0)
return self.dropout(x)
class TransMotion(nn.Module):
def __init__(self, tok_dim=21, nhid=256, nhead=4, dim_feedfwd=1024, nlayers_local=2, nlayers_global=4, dropout=0.1, activation='relu', output_scale=1, obs_and_pred=21, num_tokens=47, device='cuda:0'):
super(TransMotion, self).__init__()
self.seq_len = tok_dim
self.nhid = nhid
self.output_scale = output_scale
self.token_num = num_tokens
self.joints_pose = 22
self.obs_and_pred = 21
self.device = device
self.fc_in_traj = nn.Linear(2,nhid)
self.fc_out_traj = nn.Linear(nhid, 2)
self.double_id_encoder = LearnedTrajandIDEncoding(nhid, dropout, seq_len=21, device=device)
self.id_encoder = LearnedIDEncoding(nhid, dropout, seq_len=21, device=device)
self.scale = torch.sqrt(torch.FloatTensor([nhid])).to(device)
self.fc_in_3dbb = nn.Linear(4,nhid)
self.bb3d_encoder = Learnedbb3dEncoding(nhid, dropout, device=device)
self.fc_in_2dbb = nn.Linear(4,nhid)
self.bb2d_encoder = Learnedbb2dEncoding(nhid, dropout, device=device)
self.fc_in_3dpose = nn.Linear(3, nhid)
self.pose3d_encoder = Learnedpose3dEncoding(nhid, dropout, device=device)
self.fc_in_2dpose = nn.Linear(2, nhid)
self.pose2d_encoder = Learnedpose2dEncoding(nhid, dropout, device=device)
encoder_layer_local = nn.TransformerEncoderLayer(d_model=nhid,
nhead=nhead,
dim_feedforward=dim_feedfwd,
dropout=dropout,
activation=activation)
self.local_former = AuxilliaryEncoderCMT(encoder_layer_local, num_layers=nlayers_local)
encoder_layer_global = nn.TransformerEncoderLayer(d_model=nhid,
nhead=nhead,
dim_feedforward=dim_feedfwd,
dropout=dropout,
activation=activation)
self.global_former = AuxilliaryEncoderST(encoder_layer_global, num_layers=nlayers_global)
def forward(self, tgt, padding_mask,metamask=None):
B, in_F, NJ, K = tgt.shape
F = self.obs_and_pred
J = self.token_num
out_F = F - in_F
N = NJ // J
## keep padding
pad_idx = np.repeat([in_F - 1], out_F)
i_idx = np.append(np.arange(0, in_F), pad_idx)
tgt = tgt[:,i_idx]
tgt = tgt.reshape(B,F,N,J,K)
## add mask
mask_ratio_traj = 0.0
mask_ratio_modality = 0.0
tgt_traj = tgt[:,:,:,0,:2].to(self.device)
traj_mask = torch.rand((B,F,N)).float().to(self.device) > mask_ratio_traj
traj_mask = traj_mask.unsqueeze(3).repeat_interleave(2,dim=-1)
tgt_traj = tgt_traj*traj_mask
tgt_2dbb = tgt[:,:,:,1,:4].to(self.device)
## mask for specific modality for whole observation horizon
modality_selection_2dbb = (torch.rand((B,1,N)).float().to(self.device) > mask_ratio_modality).unsqueeze(3).repeat(1,F,1,4)
tgt_vis = tgt_2dbb*modality_selection_2dbb
tgt_2dbb = tgt_vis.to(self.device)
############
# Transformer
###########
tgt_traj = self.fc_in_traj(tgt_traj)
tgt_traj = self.double_id_encoder(tgt_traj, num_people=N)
tgt_2dbb = self.fc_in_2dbb(tgt_2dbb[:,:9])
tgt_2dbb = self.bb2d_encoder(tgt_2dbb)
tgt_padding_mask_global = padding_mask.repeat_interleave(F, dim=1)
tgt_padding_mask_local = padding_mask.reshape(-1).unsqueeze(1).repeat_interleave(self.seq_len,dim=1)
tgt_traj = torch.transpose(tgt_traj,0,1).reshape(F,-1,self.nhid)
tgt_2dbb = torch.transpose(tgt_2dbb,0,1).reshape(in_F,-1,self.nhid)
tgt = torch.cat((tgt_traj,tgt_2dbb),0)
out_local = self.local_former(tgt, mask=None, src_key_padding_mask=tgt_padding_mask_local)
##### local residual ######
out_local = out_local * self.output_scale + tgt
out_local = out_local[:21].reshape(21,B,N,self.nhid).permute(2,0,1,3).reshape(-1,B,self.nhid)
out_global = self.global_former(out_local, mask=None, src_key_padding_mask=tgt_padding_mask_global)
##### global residual ######
out_global = out_global * self.output_scale + out_local
out_primary = out_global.reshape(N,F,out_global.size(1),self.nhid)[0]
out_primary = self.fc_out_traj(out_primary)
out = out_primary.transpose(0, 1).reshape(B, F, 1, 2)
return out
def create_model(config, logger):
seq_len = config["MODEL"]["seq_len"]
token_num = config["MODEL"]["token_num"]
nhid=config["MODEL"]["dim_hidden"]
nhead=config["MODEL"]["num_heads"]
nlayers_local=config["MODEL"]["num_layers_local"]
nlayers_global=config["MODEL"]["num_layers_global"]
dim_feedforward=config["MODEL"]["dim_feedforward"]
if config["MODEL"]["type"] == "transmotion":
logger.info("Creating bert model.")
model = TransMotion(tok_dim=seq_len,
nhid=nhid,
nhead=nhead,
dim_feedfwd=dim_feedforward,
nlayers_local=nlayers_local,
nlayers_global=nlayers_global,
output_scale=config["MODEL"]["output_scale"],
obs_and_pred=config["TRAIN"]["input_track_size"] + config["TRAIN"]["output_track_size"],
num_tokens=token_num,
device=config["DEVICE"]
).to(config["DEVICE"]).float()
else:
raise ValueError(f"Model type '{config['MODEL']['type']}' not found")
return model