-
Notifications
You must be signed in to change notification settings - Fork 15
/
models.py
306 lines (229 loc) · 9.24 KB
/
models.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import temporal_structure_filter as tsf
import tgm.TGM as TGM
class SuperEvent(nn.Module):
def __init__(self, classes=65):
super(SuperEvent, self).__init__()
self.classes = classes
self.dropout = nn.Dropout(0.7)
self.add_module('d', self.dropout)
self.super_event = tsf.TSF(3)#, False)
self.add_module('sup', self.super_event)
self.super_event2 = tsf.TSF(3)#, False)
self.add_module('sup2', self.super_event2)
# we have 2xD*3
# we want to learn a per-class weighting
# to take 2xD*3 to D*3
self.cls_wts = nn.Parameter(torch.Tensor(classes))
self.sup_mat = nn.Parameter(torch.Tensor(1, classes, 1024))
stdv = 1./np.sqrt(1024+1024*3)
self.sup_mat.data.uniform_(-stdv, stdv)
self.per_frame = nn.Conv3d(1024, classes, (1,1,1))
self.per_frame.weight.data.uniform_(-stdv, stdv)
self.per_frame.bias.data.uniform_(-stdv, stdv)
self.add_module('pf', self.per_frame)
def forward(self, inp):
inp[0] = self.dropout(inp[0])
val = False
dim = 1
if inp[0].size()[0] == 1:
val = True
dim = 0
super_event = self.dropout(torch.stack([self.super_event(inp).squeeze(), self.super_event2(inp).squeeze()], dim=dim))
if val:
super_event = super_event.unsqueeze(0)
# we have B x 2 x D*3
# we want B x C x D*3
# now we have C x 2 matrix
cls_wts = torch.stack([torch.sigmoid(self.cls_wts), 1-torch.sigmoid(self.cls_wts)], dim=1)
# now we do a bmm to get B x C x D*3
super_event = torch.bmm(cls_wts.expand(inp[0].size()[0], -1, -1), super_event)
del cls_wts
# apply the super-event weights
super_event = torch.sum(self.sup_mat * super_event, dim=2)
#super_event = self.sup_mat(super_event.view(-1, 1024)).view(-1, self.classes)
super_event = super_event.unsqueeze(2).unsqueeze(3).unsqueeze(4)
cls = self.per_frame(inp[0])
return super_event+cls
def compute_pad(stride, k, s):
if s % stride == 0:
return max(k - stride, 0)
else:
return max(k - (s % stride), 0)
class SubConv(tsf.TSF):
"""
Subevents as temporal conv
"""
def __init__(self, inp, num_f, length):
super(SubConv, self).__init__(num_f)
self.inp = inp
self.length = length
def forward(self, x):
# overwrite the forward pass to get the TSF as conv kernels
t = x.size(2)
k = super(SubConv, self).get_filters(torch.tanh(self.delta), torch.tanh(self.gamma), torch.tanh(self.center), self.length, self.length)
k = k.squeeze().unsqueeze(1).unsqueeze(1)#.repeat(1, 1, self.inp, 1)
p = compute_pad(1, self.length, t)
pad_f = p // 2
pad_b = p - pad_f
x = F.pad(x, (pad_f, pad_b)).unsqueeze(1)
return F.conv2d(x, k).squeeze(1)
class SubConv2(tsf.TSF):
"""
Subevents as temporal conv
"""
def __init__(self, inp, num_f, length, c=1):
super(SubConv2, self).__init__(num_f)
self.inp = inp
self.length = length
self.c = c
self.soft_attn = nn.Parameter(torch.Tensor(c, num_f))
def forward(self, x):
# overwrite the forward pass to get the TSF as conv kernels
t = x.size(2)
k = super(SubConv2, self).get_filters(torch.tanh(self.delta), torch.tanh(self.gamma), torch.tanh(self.center), self.length, self.length)
# k is shape 1xNxL
k = k.squeeze()
# is k now NxL
# apply soft attention to conver (CxN)*(NxL) to CxL
# make attn sum to 1 along the num_gaussians
soft_attn = F.softmax(self.soft_attn, dim=1)
#print soft_attn
k = torch.mm(soft_attn, k)
# make k Cx1x1xL
k = k.unsqueeze(1).unsqueeze(1)
p = compute_pad(1, self.length, t)
pad_f = p // 2
pad_b = p - pad_f
# x is shape CxDxT
x = F.pad(x, (pad_f, pad_b))
if len(x.size()) == 3:
x = x.unsqueeze(1)
if x.size(1) == 1:
x = x.expand(-1, self.c, -1, -1)
#print x.size(), k.size(), self.c
# use groups to separate the class channels
return F.conv2d(x, k, groups=self.c).squeeze(1)
class StackSub(nn.Module):
def __init__(self, inp, num_f, length, classes):
super(StackSub, self).__init__()
# each return T*num_f
self.sub_event1 = SubConv(inp, num_f, length)
self.sub_event2 = SubConv(inp, num_f, length)
self.sub_event3 = SubConv(inp, num_f, length)
self.classify = nn.Conv1d(num_f*inp, classes, 1)
self.dropout = nn.Dropout()
self.inp = inp
self.num_f = num_f
self.classes = classes
def forward(self, inp):
val = False
dim = 1
f = inp[0].squeeze()
if inp[0].size()[0] == 1:
val = True
dim = 0
f = f.unsqueeze(0)
sub_event = torch.max(F.relu(self.sub_event1(f)), dim=1)[0]
sub_event = torch.max(F.relu(self.sub_event2(sub_event)), dim=1)[0]
sub_event = self.sub_event3(sub_event)
sub_event = self.dropout(sub_event).view(-1, self.num_f*self.inp, f.size(2))
cls = F.relu(sub_event)
return self.classify(cls)
class Hierarchy(nn.Module):
def __init__(self, inp, classes=8):
super(Hierarchy, self).__init__()
self.classes = classes
self.dropout = nn.Dropout()
self.add_module('d', self.dropout)
self.super_event = tsf.TSF(3)
self.add_module('sup', self.super_event)
self.super_event2 = tsf.TSF(3)
self.add_module('sup2', self.super_event2)
# we have 2xD
# we want to learn a per-class weighting
# to take 2xD to D
self.cls_wts = nn.Parameter(torch.Tensor(classes))
self.sup_mat = nn.Parameter(torch.Tensor(1, classes, inp))
stdv = 1./np.sqrt(inp+inp)
self.sup_mat.data.uniform_(-stdv, stdv)
self.sub_event1 = TGM(inp, 16, 5, c_in=1, c_out=8, soft=False)
self.sub_event2 = TGM(inp, 16, 5, c_in=8, c_out=8, soft=False)
self.sub_event3 = TGM(inp, 16, 5, c_in=8, c_out=8, soft=False)
self.h = nn.Conv1d(inp+1*inp+classes, 512, 1)
self.classify = nn.Conv1d(512, classes, 1)
self.inp = inp
def forward(self, inp):
val = False
dim = 1
if inp[0].size()[0] == 1:
val = True
dim = 0
super_event = torch.stack([self.super_event(inp).squeeze(), self.super_event2(inp).squeeze()], dim=dim)
f = inp[0].squeeze()
if val:
super_event = super_event.unsqueeze(0)
f = f.unsqueeze(0)
# we have B x 2 x D
# we want B x C x D
# now we have C x 2 matrix
cls_wts = torch.stack([torch.sigmoid(self.cls_wts), 1-torch.sigmoid(self.cls_wts)], dim=1)
# now we do a bmm to get B x C x D*3
super_event = torch.bmm(cls_wts.expand(inp[0].size()[0], -1, -1), super_event)
del cls_wts
# apply the super-event weights
super_event = torch.sum(self.sup_mat * super_event, dim=2)
super_event = self.dropout(super_event).view(-1,self.classes,1).expand(-1,self.classes,f.size(2))
sub_event = self.sub_event1(f)
sub_event = self.sub_event2(sub_event)
sub_event = self.dropout(torch.max(self.sub_event3(sub_event), dim=1)[0])
cls = F.relu(torch.cat([self.dropout(f), sub_event, super_event], dim=1))
cls = F.relu(self.h(cls))
return self.classify(cls)
class StackTGM(nn.Module):
def __init__(self, inp, classes=8):
super(StackTGM, self).__init__()
self.classes = classes
self.dropout = nn.Dropout()
self.add_module('d', self.dropout)
self.sub_event1 = TGM(inp, 16, 5, c_in=1, c_out=8, soft=False)
self.sub_event2 = TGM(inp, 16, 5, c_in=8, c_out=8, soft=False)
self.sub_event3 = TGM(inp, 16, 5, c_in=8, c_out=8, soft=False)
self.h = nn.Conv1d(inp+1*inp, 512, 1)
self.classify = nn.Conv1d(512, classes, 1)
self.inp = inp
def forward(self, inp):
val = False
dim = 1
if inp[0].size()[0] == 1:
val = True
dim = 0
f = inp[0].squeeze()
if val:
f = f.unsqueeze(0)
sub_event = self.sub_event1(f)
sub_event = self.sub_event2(sub_event)
sub_event = self.sub_event3(sub_event)
sub_event = self.dropout(torch.max(sub_event, dim=1)[0])
cls = F.relu(torch.cat([self.dropout(f), sub_event], dim=1))
cls = F.relu(self.h(cls))
return self.classify(cls)
def get_baseline_model(gpu, classes=65):
model = nn.Sequential(
nn.Dropout(0.5),
nn.Conv3d(1024, classes, (1,1,1)))
return model.cuda()
def get_super_model(gpu, classes=65):
model = SuperEvent(classes)
return model.cuda()
def get_hier(classes):
model = Hierarchy(1024, classes)
model.cuda()
return model
def get_tgm(classes):
model = StackTGM(1024, classes)
model.cuda()
return model