-
Notifications
You must be signed in to change notification settings - Fork 5
/
model_lstm.py
318 lines (258 loc) · 10 KB
/
model_lstm.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
"""
README: batch + loss_mask version of model
Author: He Zhao
Date: 14/10/2020 (dd/mm/yy)
"""
import os
import math
import sys
import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as Func
from torch.nn import init
from torch.nn.parameter import Parameter
from torch.nn.modules.module import Module
from torch.autograd import Variable
import torch.optim as optim
from gmm2d import *
class ConvTemporalGraphical(nn.Module):
# Source : https://github.com/yysijie/st-gcn/blob/master/net/st_gcn.py
r"""The basic module for applying a graph convolution.
Args:
in_channels (int): Number of channels in the input sequence data
out_channels (int): Number of channels produced by the convolution
kernel_size (int): Size of the graph convolving kernel
t_kernel_size (int): Size of the temporal convolving kernel
t_stride (int, optional): Stride of the temporal convolution. Default: 1
t_padding (int, optional): Temporal zero-padding added to both sides of
the input. Default: 0
t_dilation (int, optional): Spacing between temporal kernel elements.
Default: 1
bias (bool, optional): If ``True``, adds a learnable bias to the output.
Default: ``True``
Shape:
- Input[0]: Input graph sequence in :math:`(N, in_channels, T_{in}, V)` format
- Input[1]: Input graph adjacency matrix in :math:`(K, V, V)` format
- Output[0]: Outpu graph sequence in :math:`(N, out_channels, T_{out}, V)` format
- Output[1]: Graph adjacency matrix for output data in :math:`(K, V, V)` format
where
:math:`N` is a batch size,
:math:`K` is the spatial kernel size, as :math:`K == kernel_size[1]`,
:math:`T_{in}/T_{out}` is a length of input/output sequence,
:math:`V` is the number of graph nodes.
"""
def __init__(
self,
in_channels,
out_channels,
kernel_size,
t_kernel_size=1,
t_stride=1,
t_padding=0,
t_dilation=1,
bias=True,
):
super(ConvTemporalGraphical, self).__init__()
self.kernel_size = kernel_size
self.conv = nn.Conv2d(
in_channels,
out_channels,
kernel_size=(t_kernel_size, 1),
padding=(t_padding, 0),
stride=(t_stride, 1),
dilation=(t_dilation, 1),
bias=bias,
)
def forward(self, x, A, mask=None):
x = self.conv(x)
if mask is not None:
x = torch.einsum("nctv, ntv->nctv", (x, mask))
# x = torch.einsum("nctv,ntvw->nctw", (x, A))
return x.contiguous(), A
class st_gcn(nn.Module):
r"""Applies a spatial temporal graph convolution over an input graph sequence.
Args:
in_channels (int): Number of channels in the input sequence data
out_channels (int): Number of channels produced by the convolution
kernel_size (tuple): Size of the temporal convolving kernel and graph convolving kernel
stride (int, optional): Stride of the temporal convolution. Default: 1
dropout (int, optional): Dropout rate of the final output. Default: 0
residual (bool, optional): If ``True``, applies a residual mechanism. Default: ``True``
Shape:
- Input[0]: Input graph sequence in :math:`(N, in_channels, T_{in}, V)` format
- Input[1]: Input graph adjacency matrix in :math:`(K, V, V)` format
- Output[0]: Outpu graph sequence in :math:`(N, out_channels, T_{out}, V)` format
- Output[1]: Graph adjacency matrix for output data in :math:`(K, V, V)` format
where
:math:`N` is a batch size,
:math:`K` is the spatial kernel size, as :math:`K == kernel_size[1]`,
:math:`T_{in}/T_{out}` is a length of input/output sequence,
:math:`V` is the number of graph nodes.
"""
def __init__(
self,
in_channels,
out_channels,
kernel_size,
use_mdn=False,
stride=1,
dropout=0,
residual=True,
):
super(st_gcn, self).__init__()
assert len(kernel_size) == 2
assert kernel_size[0] % 2 == 1
padding = ((kernel_size[0] - 1) // 2, 0)
self.use_mdn = use_mdn
self.bn1 = torch.nn.LayerNorm([8, out_channels])
self.gcn = ConvTemporalGraphical(in_channels, out_channels, kernel_size[1])
self.bn0 = torch.nn.LayerNorm([8, out_channels])
self.prelu = nn.PReLU()
self.tcn = nn.Conv2d(
out_channels,
out_channels,
(kernel_size[0], 1),
(stride, 1),
padding,
)
self.dropout = nn.Dropout(dropout, inplace=True)
if not residual:
self.residual = lambda x: 0
elif (in_channels == out_channels) and (stride == 1):
self.residual = lambda x: x
else:
self.residual = nn.Conv2d(
in_channels,
out_channels,
kernel_size=1,
stride=(stride, 1),
)
self.bn = torch.nn.LayerNorm([8, out_channels])
self.prelu = nn.PReLU()
def forward(self, x, A, mask=None, if_bn=True):
res = self.residual(x)
res = torch.einsum("nctv, ntv->nctv", (res, mask))
if if_bn:
res = res.permute(0, 3, 2, 1)
res = self.bn(res)
res = res.permute(0, 3, 2, 1)
x, A = self.gcn(x, A, mask)
if if_bn:
x = x.permute(0, 3, 2, 1)
x = self.bn0(x)
x = x.permute(0, 3, 2, 1)
x = self.prelu(x)
x = self.tcn(x)
x = torch.einsum("nctv, ntv->nctv", (x, mask))
if if_bn:
x = x.permute(0, 3, 2, 1)
x = self.bn(x)
x = x.permute(0, 3, 2, 1)
x = self.dropout(x) + res
if not self.use_mdn:
x = self.prelu(x)
return x, A
class Goal_Example_Model(nn.Module):
def __init__(
self,
n_stgcnn=1,
n_txpcnn=1,
input_feat=2,
output_feat=5,
seq_len=8,
pred_seq_len=12,
inter_feat=32,
kernel_size=3,
):
super(Goal_Example_Model, self).__init__()
self.n_stgcnn = n_stgcnn
self.n_txpcnn = n_txpcnn
self.rnn_type = "LSTM"
self.nlayers = 1
self.pred_seq_len = pred_seq_len
self.st_gcns = nn.ModuleList()
self.st_gcns.append(st_gcn(input_feat, output_feat, (kernel_size, seq_len)))
for j in range(1, self.n_stgcnn):
self.st_gcns.append(
st_gcn(output_feat, output_feat, (kernel_size, seq_len))
)
self.enc_lstm = torch.nn.LSTM(output_feat, output_feat)
self.state_start = torch.nn.Linear(output_feat, 2) # manully define as 2 dim;
self.dec_lstm = torch.nn.LSTM(output_feat + 2, output_feat)
self.out_mus = nn.Linear(output_feat, 2)
self.out_sigma = nn.Linear(output_feat, 2)
self.out_corr = nn.Linear(output_feat, 1)
self.leakyrelu = torch.nn.LeakyReLU()
def forward(self, v, a, mask=None, out_mask=None):
# use the last observed as input, rather than inferring it with other nets;
a_0 = v[:, :2, -1, :].clone() # extract (x, y)
for k in range(self.n_stgcnn):
v, a = self.st_gcns[k](v, a, mask)
v = v.permute(0, 2, 3, 1) # [B, T, N, C]
# v = self.enc_mlp(v)
v = v.permute(0, 3, 1, 2) # [B, C, T, N]
B, C, T, N = v.shape
# transform to shape [T, B*N, C]
v = v.permute(2, 0, 3, 1).reshape(T, B * N, C)
h_0, c_0 = self.init_hidden(B * N, C)
out, (h_inp, c_inp) = self.enc_lstm(v, (h_0, c_0))
""" Transform the state to start_action """
# Should I use the last observed coords as a_0?
# a_0 = self.state_start(h_inp)
a_0 = a_0.permute(0, 2, 1)
a_0 = a_0.reshape(1, B * N, 2)
a_i = torch.zeros(a_0.shape)
""" Init some stats """
V_pred = []
a_list = [a_0]
""" Start Decoding Stage """
for i in range(self.pred_seq_len):
if i == 0:
(h_t, c_t) = self.init_hidden(B * N, C)
inp = torch.cat([h_inp, a_0], -1)
_, (h_t, c_t) = self.dec_lstm(inp, (h_t, c_t))
else:
inp = torch.cat([h_inp, a_i], -1)
_, (h_t, c_t) = self.dec_lstm(inp, (h_t, c_t))
v_mus = self.out_mus(h_t)
v_sigma = self.out_sigma(h_t)
v_corr = self.out_corr(h_t)
v = torch.cat([v_mus, v_sigma, v_corr], -1)
v = v.reshape(B, N, 5)
V_pred.append(v.clone())
log_pis = torch.ones(v[..., -2:-1].shape)
gmm2d = GMM2D(
log_pis, v[..., 0:2], v[..., 2:4], torch.tanh(v[..., -1]).unsqueeze(-1)
)
a_i = gmm2d.rsample().squeeze()
a_i = a_i.reshape(1, B * N, 2)
a_list.append(a_i.clone())
V_pred = torch.stack(V_pred, dim=1)
a_pred = torch.stack(a_list, dim=1)
return V_pred, a_pred
def residual_block(self, index, x, out_mask=None):
residual = x
out = self.tpcnns[index](x)
if out_mask is not None:
out = torch.einsum("ntcv, ntv->ntcv", out, out_mask)
out = self.prelus[index](out)
out += residual
return out
def init_hidden(self, bsz, nhid):
weight = next(self.parameters()).data
if self.rnn_type == "LSTM":
return (
Variable(weight.new(self.nlayers, bsz, nhid).zero_()),
Variable(weight.new(self.nlayers, bsz, nhid).zero_()),
)
else:
return Variable(weight.new(self.nlayers, bsz, hid).zero_())
if __name__ == "__main__":
inp = torch.randn(64, 2, 8, 10)
inp_adj = torch.randn(64, 8, 10, 10)
inp_maks = torch.randn(64, 8, 10)
out_maks = torch.randn(64, 12, 10)
model = social_stgcnn()
out = model(inp, inp_adj, mask=inp_maks, out_mask=out_maks)
print(out[0].shape)