-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmodel.py
258 lines (182 loc) · 8.63 KB
/
model.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
#!/usr/bin/env python
# coding: utf-8
import torch
import torch.nn as nn
import math
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: Input graph sequence in :math:`(N, in_channels, T_{in}, V)` format
- Output: Outpu graph sequence in :math:`(N, out_channels, T_{out}, 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,
time_dim,
joints_dim
):
super(ConvTemporalGraphical,self).__init__()
self.A=nn.Parameter(torch.FloatTensor(time_dim, joints_dim,joints_dim)) #learnable, graph-agnostic 3-d adjacency matrix(or edge importance matrix)
stdv = 1. / math.sqrt(self.A.size(1))
self.A.data.uniform_(-stdv,stdv)
self.T=nn.Parameter(torch.FloatTensor(joints_dim , time_dim, time_dim))
stdv = 1. / math.sqrt(self.T.size(1))
self.T.data.uniform_(-stdv,stdv)
'''
self.prelu = nn.PReLU()
self.Z=nn.Parameter(torch.FloatTensor(joints_dim, joints_dim, time_dim, time_dim))
stdv = 1. / math.sqrt(self.Z.size(2))
self.Z.data.uniform_(-stdv,stdv)
'''
def forward(self, x):
x = torch.einsum('nctv,vtq->ncqv', (x, self.T))
## x=self.prelu(x)
x = torch.einsum('nctv,tvw->nctw', (x, self.A))
## x = torch.einsum('nctv,wvtq->ncqw', (x, self.Z))
return x.contiguous()
class ST_GCNN_layer(nn.Module):
"""
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
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.
:in_channels= dimension of coordinates
: out_channels=dimension of coordinates
+
"""
def __init__(self,
in_channels,
out_channels,
kernel_size,
stride,
time_dim,
joints_dim,
dropout,
bias=True):
super(ST_GCNN_layer,self).__init__()
self.kernel_size = kernel_size
assert self.kernel_size[0] % 2 == 1
assert self.kernel_size[1] % 2 == 1
padding = ((self.kernel_size[0] - 1) // 2,(self.kernel_size[1] - 1) // 2)
self.gcn=ConvTemporalGraphical(time_dim,joints_dim) # the convolution layer
self.tcn = nn.Sequential(
nn.Conv2d(
in_channels,
out_channels,
(self.kernel_size[0], self.kernel_size[1]),
(stride, stride),
padding,
),
nn.BatchNorm2d(out_channels),
nn.Dropout(dropout, inplace=True),
)
if stride != 1 or in_channels != out_channels:
self.residual=nn.Sequential(nn.Conv2d(
in_channels,
out_channels,
kernel_size=1,
stride=(1, 1)),
nn.BatchNorm2d(out_channels),
)
else:
self.residual=nn.Identity()
self.prelu = nn.PReLU()
def forward(self, x):
# assert A.shape[0] == self.kernel_size[1], print(A.shape[0],self.kernel_size)
res=self.residual(x)
x=self.gcn(x)
x=self.tcn(x)
x=x+res
x=self.prelu(x)
return x
class CNN_layer(nn.Module): # This is the simple CNN layer,that performs a 2-D convolution while maintaining the dimensions of the input(except for the features dimension)
def __init__(self,
in_channels,
out_channels,
kernel_size,
dropout,
bias=True):
super(CNN_layer,self).__init__()
self.kernel_size = kernel_size
padding = ((kernel_size[0] - 1) // 2, (kernel_size[1] - 1) // 2) # padding so that both dimensions are maintained
assert kernel_size[0] % 2 == 1 and kernel_size[1] % 2 == 1
self.block= [nn.Conv2d(in_channels,out_channels,kernel_size=kernel_size,padding=padding)
,nn.BatchNorm2d(out_channels),nn.Dropout(dropout, inplace=True)]
self.block=nn.Sequential(*self.block)
def forward(self, x):
output= self.block(x)
return output
# In[11]:
class Model(nn.Module):
"""
Shape:
- Input[0]: Input sequence in :math:`(N, in_channels,T_in, V)` format
- Output[0]: Output sequence in :math:`(N,T_out,in_channels, V)` format
where
:math:`N` is a batch size,
:math:`T_{in}/T_{out}` is a length of input/output sequence,
:math:`V` is the number of graph nodes.
:in_channels=number of channels for the coordiantes(default=3)
+
"""
def __init__(self,
input_channels,
input_time_frame,
output_time_frame,
st_gcnn_dropout,
joints_to_consider,
n_txcnn_layers,
txc_kernel_size,
txc_dropout,
bias=True):
super(Model,self).__init__()
self.input_time_frame=input_time_frame
self.output_time_frame=output_time_frame
self.joints_to_consider=joints_to_consider
self.st_gcnns=nn.ModuleList()
self.n_txcnn_layers=n_txcnn_layers
self.txcnns=nn.ModuleList()
self.st_gcnns.append(ST_GCNN_layer(input_channels,64,[1,1],1,input_time_frame,
joints_to_consider,st_gcnn_dropout))
self.st_gcnns.append(ST_GCNN_layer(64,32,[1,1],1,input_time_frame,
joints_to_consider,st_gcnn_dropout))
self.st_gcnns.append(ST_GCNN_layer(32,64,[1,1],1,input_time_frame,
joints_to_consider,st_gcnn_dropout))
self.st_gcnns.append(ST_GCNN_layer(64,input_channels,[1,1],1,input_time_frame,
joints_to_consider,st_gcnn_dropout))
# at this point, we must permute the dimensions of the gcn network, from (N,C,T,V) into (N,T,C,V)
self.txcnns.append(CNN_layer(input_time_frame,output_time_frame,txc_kernel_size,txc_dropout)) # with kernel_size[3,3] the dimensinons of C,V will be maintained
for i in range(1,n_txcnn_layers):
self.txcnns.append(CNN_layer(output_time_frame,output_time_frame,txc_kernel_size,txc_dropout))
self.prelus = nn.ModuleList()
for j in range(n_txcnn_layers):
self.prelus.append(nn.PReLU())
def forward(self, x):
for gcn in (self.st_gcnns):
x = gcn(x)
x= x.permute(0,2,1,3) # prepare the input for the Time-Extrapolator-CNN (NCTV->NTCV)
x=self.prelus[0](self.txcnns[0](x))
for i in range(1,self.n_txcnn_layers):
x = self.prelus[i](self.txcnns[i](x)) +x # residual connection
return x