-
Notifications
You must be signed in to change notification settings - Fork 9
/
model_baseline.py
335 lines (218 loc) · 12.3 KB
/
model_baseline.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
# Baseline model for "SGCN:Sparse Graph Convolution Network for Pedestrian Trajectory Prediction"
# Source-code referred from SGCN at https://github.com/shuaishiliu/SGCN/tree/0ff25cedc04852803787196e83c0bb941d724fc2/model.py
import torch
import torch.nn as nn
from torch.nn import functional as F
class AsymmetricConvolution(nn.Module):
def __init__(self, in_cha, out_cha):
super(AsymmetricConvolution, self).__init__()
self.conv1 = nn.Conv2d(in_cha, out_cha, kernel_size=(3, 1), padding=(1, 0), bias=False)
self.conv2 = nn.Conv2d(in_cha, out_cha, kernel_size=(1, 3), padding=(0, 1))
self.shortcut = lambda x: x
if in_cha != out_cha:
self.shortcut = nn.Sequential(
nn.Conv2d(in_cha, out_cha, 1, bias=False)
)
self.activation = nn.PReLU()
def forward(self, x):
shortcut = self.shortcut(x)
x1 = self.conv1(x)
x2 = self.conv2(x)
x2 = self.activation(x2 + x1)
return x2 + shortcut
class InteractionMask(nn.Module):
def __init__(self, number_asymmetric_conv_layer=7, spatial_channels=4, temporal_channels=4):
super(InteractionMask, self).__init__()
self.number_asymmetric_conv_layer = number_asymmetric_conv_layer
self.spatial_asymmetric_convolutions = nn.ModuleList()
self.temporal_asymmetric_convolutions = nn.ModuleList()
for i in range(self.number_asymmetric_conv_layer):
self.spatial_asymmetric_convolutions.append(
AsymmetricConvolution(spatial_channels, spatial_channels)
)
self.temporal_asymmetric_convolutions.append(
AsymmetricConvolution(temporal_channels, temporal_channels)
)
self.spatial_output = nn.Sigmoid()
self.temporal_output = nn.Sigmoid()
def forward(self, dense_spatial_interaction, dense_temporal_interaction, threshold=0.5):
assert len(dense_temporal_interaction.shape) == 4
assert len(dense_spatial_interaction.shape) == 4
for j in range(self.number_asymmetric_conv_layer):
dense_spatial_interaction = self.spatial_asymmetric_convolutions[j](dense_spatial_interaction)
dense_temporal_interaction = self.temporal_asymmetric_convolutions[j](dense_temporal_interaction)
spatial_interaction_mask = self.spatial_output(dense_spatial_interaction)
temporal_interaction_mask = self.temporal_output(dense_temporal_interaction)
spatial_zero = torch.zeros_like(spatial_interaction_mask, device='cuda')
temporal_zero = torch.zeros_like(temporal_interaction_mask, device='cuda')
spatial_interaction_mask = torch.where(spatial_interaction_mask > threshold, spatial_interaction_mask,
spatial_zero)
temporal_interaction_mask = torch.where(temporal_interaction_mask > threshold, temporal_interaction_mask,
temporal_zero)
return spatial_interaction_mask, temporal_interaction_mask
class ZeroSoftmax(nn.Module):
def __init__(self):
super(ZeroSoftmax, self).__init__()
def forward(self, x, dim=0, eps=1e-5):
x_exp = torch.pow(torch.exp(x) - 1, exponent=2)
x_exp_sum = torch.sum(x_exp, dim=dim, keepdim=True)
x = x_exp / (x_exp_sum + eps)
return x
class SelfAttention(nn.Module):
def __init__(self, in_dims=2, d_model=64, num_heads=4):
super(SelfAttention, self).__init__()
self.embedding = nn.Linear(in_dims, d_model)
self.query = nn.Linear(d_model, d_model)
self.key = nn.Linear(d_model, d_model)
self.scaled_factor = torch.sqrt(torch.Tensor([d_model])).cuda()
self.softmax = nn.Softmax(dim=-1)
self.num_heads = num_heads
def split_heads(self, x):
# x [batch_size seq_len d_model]
x = x.reshape(x.shape[0], -1, self.num_heads, x.shape[-1] // self.num_heads).contiguous()
return x.permute(0, 2, 1, 3) # [batch_size nun_heads seq_len depth]
def forward(self, x, mask=False, multi_head=False):
# batch_size seq_len 2
assert len(x.shape) == 3
embeddings = self.embedding(x) # batch_size seq_len d_model
query = self.query(embeddings) # batch_size seq_len d_model
key = self.key(embeddings) # batch_size seq_len d_model
if multi_head:
query = self.split_heads(query) # B num_heads seq_len d_model
key = self.split_heads(key) # B num_heads seq_len d_model
attention = torch.matmul(query, key.permute(0, 1, 3, 2)) # (batch_size, num_heads, seq_len, seq_len)
else:
attention = torch.matmul(query, key.permute(0, 2, 1)) # (batch_size, seq_len, seq_len)
attention = self.softmax(attention / self.scaled_factor)
if mask is True:
mask = torch.ones_like(attention)
attention = attention * torch.tril(mask)
return attention, embeddings
class SpatialTemporalFusion(nn.Module):
def __init__(self, obs_len=8):
super(SpatialTemporalFusion, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(obs_len, obs_len, 1),
nn.PReLU()
)
self.shortcut = nn.Sequential()
def forward(self, x):
x = self.conv(x) + self.shortcut(x)
return x.squeeze(dim=0)
class SparseWeightedAdjacency(nn.Module):
def __init__(self, spa_in_dims=2, tem_in_dims=3, embedding_dims=64, obs_len=8, dropout=0,
number_asymmetric_conv_layer=7):
super(SparseWeightedAdjacency, self).__init__()
# dense interaction
self.spatial_attention = SelfAttention(spa_in_dims, embedding_dims)
self.temporal_attention = SelfAttention(tem_in_dims, embedding_dims)
# attention fusion
self.spa_fusion = SpatialTemporalFusion(obs_len=obs_len)
# interaction mask
self.interaction_mask = InteractionMask(
number_asymmetric_conv_layer=number_asymmetric_conv_layer
)
self.dropout = dropout
self.zero_softmax = ZeroSoftmax()
def forward(self, graph, identity, mask=None):
assert len(graph.shape) == 3
spatial_graph = graph[:, :, 1:] # (T N 2)
temporal_graph = graph.permute(1, 0, 2) # (N T 3)
# (T num_heads N N) (T N d_model)
dense_spatial_interaction, spatial_embeddings = self.spatial_attention(spatial_graph, multi_head=True)
# (N num_heads T T) (N T d_model)
dense_temporal_interaction, temporal_embeddings = self.temporal_attention(temporal_graph, multi_head=True)
# attention fusion
st_interaction = self.spa_fusion(dense_spatial_interaction.permute(1, 0, 2, 3)).permute(1, 0, 2, 3)
ts_interaction = dense_temporal_interaction
spatial_mask, temporal_mask = self.interaction_mask(st_interaction, ts_interaction)
# self-connected
spatial_mask = spatial_mask + identity[0].unsqueeze(1)
temporal_mask = temporal_mask + identity[1].unsqueeze(1)
# Masking if needed
spatial_mask = spatial_mask if mask is None else spatial_mask * mask
normalized_spatial_adjacency_matrix = self.zero_softmax(dense_spatial_interaction * spatial_mask, dim=-1)
normalized_temporal_adjacency_matrix = self.zero_softmax(dense_temporal_interaction * temporal_mask, dim=-1)
return normalized_spatial_adjacency_matrix, normalized_temporal_adjacency_matrix,\
spatial_embeddings, temporal_embeddings
class GraphConvolution(nn.Module):
def __init__(self, in_dims=2, embedding_dims=16, dropout=0):
super(GraphConvolution, self).__init__()
self.embedding = nn.Linear(in_dims, embedding_dims, bias=False)
self.activation = nn.PReLU()
self.dropout = dropout
def forward(self, graph, adjacency):
# graph [batch_size 1 seq_len 2]
# adjacency [batch_size num_heads seq_len seq_len]
gcn_features = self.embedding(torch.matmul(adjacency, graph))
gcn_features = F.dropout(self.activation(gcn_features), p=self.dropout)
return gcn_features # [batch_size num_heads seq_len hidden_size]
class SparseGraphConvolution(nn.Module):
def __init__(self, in_dims=16, embedding_dims=16, dropout=0):
super(SparseGraphConvolution, self).__init__()
self.dropout = dropout
self.spatial_temporal_sparse_gcn = nn.ModuleList()
self.temporal_spatial_sparse_gcn = nn.ModuleList()
self.spatial_temporal_sparse_gcn.append(GraphConvolution(in_dims, embedding_dims))
self.spatial_temporal_sparse_gcn.append(GraphConvolution(embedding_dims, embedding_dims))
self.temporal_spatial_sparse_gcn.append(GraphConvolution(in_dims, embedding_dims))
self.temporal_spatial_sparse_gcn.append(GraphConvolution(embedding_dims, embedding_dims))
def forward(self, graph, normalized_spatial_adjacency_matrix, normalized_temporal_adjacency_matrix):
# graph [1 seq_len num_pedestrians 3]
# _matrix [batch num_heads seq_len seq_len]
graph = graph[:, :, :, 1:]
spa_graph = graph.permute(1, 0, 2, 3) # (seq_len 1 num_p 2)
tem_graph = spa_graph.permute(2, 1, 0, 3) # (num_p 1 seq_len 2)
gcn_spatial_features = self.spatial_temporal_sparse_gcn[0](spa_graph, normalized_spatial_adjacency_matrix)
gcn_spatial_features = gcn_spatial_features.permute(2, 1, 0, 3)
# [num_p num_heads seq_len d]
gcn_spatial_temporal_features = self.spatial_temporal_sparse_gcn[1](gcn_spatial_features, normalized_temporal_adjacency_matrix)
gcn_temporal_features = self.temporal_spatial_sparse_gcn[0](tem_graph,
normalized_temporal_adjacency_matrix)
gcn_temporal_features = gcn_temporal_features.permute(2, 1, 0, 3)
gcn_temporal_spatial_features = self.temporal_spatial_sparse_gcn[1](gcn_temporal_features,
normalized_spatial_adjacency_matrix)
return gcn_spatial_temporal_features, gcn_temporal_spatial_features.permute(2, 1, 0, 3)
class TrajectoryModel(nn.Module):
def __init__(self,
number_asymmetric_conv_layer=7, embedding_dims=64, number_gcn_layers=1, dropout=0,
obs_len=8, pred_len=12, n_tcn=5,
out_dims=5, num_heads=4):
super(TrajectoryModel, self).__init__()
self.number_gcn_layers = number_gcn_layers
self.n_tcn = n_tcn
self.dropout = dropout
# sparse graph learning
self.sparse_weighted_adjacency_matrices = SparseWeightedAdjacency(
number_asymmetric_conv_layer=number_asymmetric_conv_layer, obs_len=obs_len
)
# graph convolution
self.stsgcn = SparseGraphConvolution(
in_dims=2, embedding_dims=embedding_dims // num_heads, dropout=dropout
)
self.fusion_ = nn.Conv2d(num_heads, num_heads, kernel_size=1, bias=False)
self.tcns = nn.ModuleList()
self.tcns.append(nn.Sequential(
nn.Conv2d(obs_len, pred_len, 3, padding=1),
nn.PReLU()
))
for j in range(1, self.n_tcn):
self.tcns.append(nn.Sequential(
nn.Conv2d(pred_len, pred_len, 3, padding=1),
nn.PReLU()
))
self.output = nn.Linear(embedding_dims // num_heads, out_dims)
def forward(self, graph, identity, mask=None):
# graph 1 obs_len N 3
normalized_spatial_adjacency_matrix, normalized_temporal_adjacency_matrix, spatial_embeddings, temporal_embeddings = \
self.sparse_weighted_adjacency_matrices(graph.squeeze(dim=0), identity, mask)
gcn_temporal_spatial_features, gcn_spatial_temporal_features = self.stsgcn(
graph, normalized_spatial_adjacency_matrix, normalized_temporal_adjacency_matrix
)
gcn_representation = self.fusion_(gcn_temporal_spatial_features) + gcn_spatial_temporal_features
gcn_representation = gcn_representation.permute(0, 2, 1, 3)
features = self.tcns[0](gcn_representation)
for k in range(1, self.n_tcn):
features = F.dropout(self.tcns[k](features) + features, p=self.dropout)
prediction = torch.mean(self.output(features), dim=-2)
return prediction.permute(1, 0, 2).contiguous()