-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
427 lines (334 loc) · 14.5 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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import torch
import torch.nn as nn
import torch.nn.functional as F
from utils import gumbel_sigmoid
class GCNLayer(nn.Module):
def __init__(self, in_feats, out_feats):
super().__init__()
self.in_feats = in_feats
self.out_feats = out_feats
self.weight = nn.Parameter(torch.FloatTensor(in_feats, out_feats))
self.norm = nn.LayerNorm(out_feats)
nn.init.xavier_uniform_(self.weight.data)
def forward(self, x, adj):
x = x.matmul(self.weight)
x = adj.matmul(x)
x = self.norm(x)
x = F.relu(x)
return x
class GraphModule(nn.Module):
def __init__(self, num_layers, num_feats):
super().__init__()
self.wq = nn.Linear(num_feats, num_feats)
self.wk = nn.Linear(num_feats, num_feats)
layers = []
for i in range(num_layers):
layers.append(GCNLayer(num_feats, num_feats))
self.gcn = nn.ModuleList(layers)
def forward(self, x, get_adj=False):
qx = self.wq(x)
kx = self.wk(x)
dot_mat = qx.matmul(kx.transpose(-1, -2))
adj = F.normalize(dot_mat.square(), p=1, dim=-1)
for layer in self.gcn:
x = layer(x, adj)
x = x.mean(dim=-2)
if get_adj is False:
return x
else:
return x, adj
class ClassifierSimple(nn.Module):
def __init__(self, num_feats, num_hid, num_class):
super().__init__()
self.fc1 = nn.Linear(num_feats, num_hid)
self.fc2 = nn.Linear(num_hid, num_class)
self.drop = nn.Dropout()
def forward(self, x):
x = F.relu(self.fc1(x))
x = self.drop(x)
x = self.fc2(x)
return x
class ModelGCNConcAfter(nn.Module):
def __init__(self, gcn_layers, num_feats, num_class):
super().__init__()
self.graph = GraphModule(gcn_layers, num_feats)
self.cls = ClassifierSimple(2 * num_feats, num_feats, num_class)
def forward(self, feats, feat_global, get_adj=False):
N, FR, B, NF = feats.shape
feats = feats.view(N * FR, B, NF)
if get_adj is False:
x = self.graph(feats)
x = x.view(N, FR, -1)
x = self.graph(x)
y = self.graph(feat_global)
x = torch.cat([x, y], dim=-1)
x = self.cls(x)
return x
else:
x, adjobj = self.graph(feats, get_adj)
adjobj = adjobj.cpu()
wids_objects = adjobj.numpy().sum(axis=1)
x = x.view(N, FR, -1)
x, adjframelocal = self.graph(x, get_adj)
adjframelocal = adjframelocal.cpu()
wids_frame_local = adjframelocal.numpy().sum(axis=1)
y, adjframeglobal = self.graph(feat_global, get_adj)
adjframeglobal = adjframeglobal.cpu()
wids_frame_global = adjframeglobal.numpy().sum(axis=1)
x = torch.cat([x, y], dim=-1)
x = self.cls(x)
return x, wids_objects, wids_frame_local, wids_frame_global
class ModelGCNConcAfterGlobalOnly(nn.Module):
def __init__(self, gcn_layers, num_feats, num_class):
super().__init__()
self.graph = GraphModule(gcn_layers, num_feats)
self.cls = ClassifierSimple(num_feats, int(num_feats/2), num_class)
def forward(self, feat_global): #(,feats,)
x = self.graph(feat_global)
x = self.cls(x)
return x
class ModelGCNConcAfterLocalOnly(nn.Module):
def __init__(self, gcn_layers, num_feats, num_class):
super().__init__()
self.graph = GraphModule(gcn_layers, num_feats)
self.cls = ClassifierSimple(num_feats, int(num_feats/2), num_class)
def forward(self, feats): #,feat_global
N, FR, B, NF = feats.shape
feats = feats.view(N * FR, B, NF)
x = self.graph(feats)
x = x.view(N, FR, -1)
x = self.graph(x)
x = self.cls(x)
return x
class ModelGATPolicyDeterministic(nn.Module):
def __init__(self, gcn_layers, num_feats, num_class):
super().__init__()
self.graph = GraphModule(gcn_layers, num_feats)
self.cls = ClassifierSimple(2 * num_feats, num_feats, num_class)
def forward(self, feat_global, get_adj=True):
y, adjframeglobal = self.graph(feat_global, get_adj)
adjframeglobal = adjframeglobal.cpu()
wids_frame_global = adjframeglobal.detach().numpy().sum(axis=1)
# y = self.cls(y, device)
return wids_frame_global
class ModelGATPolicy(nn.Module):
def __init__(self, gcn_layers, num_feats, num_class):
super().__init__()
self.graph = GraphModule(gcn_layers, num_feats)
self.cls = ClassifierSimple(2 * num_feats, num_feats, num_class)
def forward(self, feat_global, get_adj=True):
y, adjframeglobal = self.graph(feat_global, get_adj)
wids_frame_global = adjframeglobal.sum(axis=1)
return wids_frame_global
class ModelTotalGATPolicy(nn.Module):
def __init__(self, gcn_layers, num_feats, num_class):
super().__init__()
self.vigat = ModelGCNConcAfter(gcn_layers, num_feats, num_class)
self.ModelGATPolicy = ModelGATPolicy(gcn_layers, num_feats, num_class)
def forward(self, feats, feat_global, temp, gs_thresh):
wids_frame_global = self.ModelGATPolicy(feat_global)
# Policy Function
# TODO CHECK NON-ZERO Wids
mask = gumbel_sigmoid(logits=torch.log(wids_frame_global), temperature=temp, thresh=gs_thresh, hard=True)
# Index Selection
kept_feats = feats * mask.unsqueeze(-1).unsqueeze(-1)
out_data = self.vigat(kept_feats, feat_global)
return out_data, mask
class ModelTotalGATPolicyHead(nn.Module):
def __init__(self, gcn_layers, num_feats, num_class, num_frames):
super().__init__()
self.vigat = ModelGCNConcAfter(gcn_layers, num_feats, num_class)
self.ModelGATPolicy = ModelGATPolicy(gcn_layers, num_feats, num_class)
self.fc = nn.ReLU(nn.Linear(num_frames, num_frames))
self.drop = nn.Dropout()
def forward(self, feats, feat_global, temp, gs_thresh):
wids_frame_global = self.ModelGATPolicy(feat_global)
wids_frame_global = self.fc(wids_frame_global)
wids_frame_global = self.drop(wids_frame_global)
# Policy Function
# TODO CHECK NON-ZERO Wids
mask = gumbel_sigmoid(logits=wids_frame_global, temperature=temp, thresh=gs_thresh, hard=True)
# Index Selection
kept_feats = feats * mask.unsqueeze(-1).unsqueeze(-1)
out_data = self.vigat(kept_feats, feat_global)
return out_data, mask
class ModelGCNConcAfterGlobal(nn.Module):
def __init__(self, gcn_layers, num_feats, num_class):
super().__init__()
self.graph = GraphModule(gcn_layers, num_feats)
self.cls = ClassifierSimple(num_feats, int(num_feats/2), num_class)
def forward(self, feat_global):
y = self.graph(feat_global)
x = self.cls(y)
return x, y
class ModelGCNConcAfterFrame(nn.Module):
def __init__(self, gcn_layers, num_feats, num_frame_classifiers, num_class):
super().__init__()
self.graph = GraphModule(gcn_layers, num_feats)
self.classifiers = nn.ModuleList()
self.num_frame_classifiers = num_frame_classifiers
for m in range(0, self.num_frame_classifiers):
self.classifiers.append(ClassifierSimple(2 * num_feats, num_feats, num_class))
def forward(self, feats, feat_global_single, feat_single_previous, t_c=torch.tensor(0), get_adj=False):
N, FR, B, NF = feats.shape
feats = feats.reshape(N * FR, B, NF) # .view .reshape .contiguous()
if get_adj is False:
x = self.graph(feats)
x = x.view(N, FR, -1)
x = self.graph(x)
y = torch.cat([x, feat_global_single], dim=-1)
x = self.classifiers[t_c](y)
return x, y
else:
x, adjobj = self.graph(feats, get_adj)
adjobj = adjobj.cpu()
wids_objects = adjobj.numpy().sum(axis=1)
x = x.view(N, FR, -1)
x, adjframelocal = self.graph(x, get_adj)
adjframelocal = adjframelocal.cpu()
wids_frame_local = adjframelocal.numpy().sum(axis=1)
y = torch.cat([x, feat_global_single], dim=-1)
x = self.classifiers[t_c](y)
return x, y, wids_objects, wids_frame_local
class ExitingGate(nn.Module):
def __init__(self, in_planes):
super(ExitingGate, self).__init__()
self.relu = nn.ReLU(inplace=True)
self.conv1 = nn.Conv2d(in_planes, 128, kernel_size=1, stride=1, bias=True)
self.conv2 = nn.Conv2d(128, 64, kernel_size=1, stride=1, bias=True)
self.bn1 = nn.BatchNorm2d(128)
self.bn2 = nn.BatchNorm2d(64)
self.sigmoid = nn.Sigmoid()
self.linear = nn.Linear(128, 1, bias=True)
def forward(self, x0, x1, force_hard=True, prev_features=None):
x0 = F.relu(self.bn1(self.conv1(x0)))
x0 = F.relu(self.bn2(self.conv2(x0)))
x0 = torch.flatten(x0, 1)
x1 = F.relu(self.bn1(self.conv1(x1)))
x1 = F.relu(self.bn2(self.conv2(x1)))
x1 = torch.flatten(x1, 1)
x = torch.cat([x0, x1], dim=1)
out = self.linear(x)
out = self.sigmoid(out)
# out[out >= 0.5] = 1
# out[out < 0.5] = 0
return out
class ExitingGates(nn.Module):
def __init__(self, in_planes, num_gates):
super(ExitingGates, self).__init__()
self.exiting_gates = nn.ModuleList()
self.num_gates = num_gates
for m in range(0, self.num_gates):
self.exiting_gates.append(ExitingGate(in_planes))
def forward(self, x0, x1, gate_num, force_hard=True, prev_features=None):
out = self.exiting_gates[gate_num](x0, x1)
return out
class ExitingGateGAT(nn.Module):
def __init__(self, gcn_layers, num_feats):
super(ExitingGateGAT, self).__init__()
self.graph = GraphModule(gcn_layers, num_feats)
self.cls = ClassifierSimple(num_feats, int(num_feats/2), 1)
self.sigmoid = nn.Sigmoid()
def forward(self, feat):
y = self.graph(feat)
x = self.cls(y)
out = self.sigmoid(x)
return out
class ExitingGatesGAT(nn.Module):
def __init__(self, gcn_layers, num_feats, num_gates):
super(ExitingGatesGAT, self).__init__()
self.exiting_gates = nn.ModuleList()
self.num_gates = num_gates
for m in range(0, self.num_gates):
self.exiting_gates.append(ExitingGateGAT(gcn_layers, num_feats))
def forward(self, feat, gate_num):
out = self.exiting_gates[gate_num](feat)
return out
class ModelClassifier(nn.Module):
def __init__(self, num_feats, num_class, num_frame_classifiers):
super(ModelClassifier, self).__init__()
self.classifiers = nn.ModuleList()
self.num_frame_classifiers = num_frame_classifiers
for m in range(0, self.num_frame_classifiers):
self.classifiers.append(ClassifierSimple(2 * num_feats, num_feats, num_class))
def forward(self, y, t_c=torch.tensor(0)):
x = self.classifiers[t_c](y)
return x
class ExitingGateGATCNN(nn.Module):
def __init__(self, gcn_layers, in_planes, out_planes=256, inter_planes=512):
super(ExitingGateGATCNN, self).__init__()
self.in_planes = in_planes
self.out_planes = out_planes
self.inter_planes = inter_planes
self.graph = GraphModule(gcn_layers, self.out_planes)
self.cls = ClassifierSimple(self.out_planes, int(self.out_planes / 2), 1)
self.sigmoid = nn.Sigmoid()
self.relu = nn.ReLU(inplace=True)
self.conv1 = nn.Conv2d(self.in_planes, self.inter_planes, kernel_size=1, stride=1, bias=True)
self.conv2 = nn.Conv2d(self.inter_planes, self.out_planes, kernel_size=1, stride=1, bias=True)
self.bn1 = nn.BatchNorm2d(self.inter_planes)
self.bn2 = nn.BatchNorm2d(self.out_planes)
def forward(self, feat):
N, FR, NF = feat.shape
feat = feat.view(N * FR, NF, 1, 1)
feat = F.relu(self.bn1(self.conv1(feat)))
feat = F.relu(self.bn2(self.conv2(feat)))
feat = feat.view(N, FR, self.out_planes)
y = self.graph(feat)
x = self.cls(y)
out = self.sigmoid(x)
return out
class ExitingGatesGATCNN(nn.Module):
def __init__(self, gcn_layers, num_feats, num_gates):
super(ExitingGatesGATCNN, self).__init__()
self.exiting_gates = nn.ModuleList()
self.num_gates = num_gates
for m in range(0, self.num_gates):
self.exiting_gates.append(ExitingGateGATCNN(gcn_layers, in_planes=num_feats))
def forward(self, feat, gate_num):
out = self.exiting_gates[gate_num](feat)
return out
class ModelGCNConcAfterGlobalFrame(nn.Module):
def __init__(self, gcn_layers, num_feats, num_class):
super().__init__()
self.graph = GraphModule(gcn_layers, num_feats)
self.cls = ClassifierSimple(2 * num_feats, num_feats, num_class)
def forward(self, feat_global, get_adj=False):
if get_adj is False:
y = self.graph(feat_global)
return y
else:
y, adjframeglobal = self.graph(feat_global, get_adj)
adjframeglobal = adjframeglobal.cpu()
wids_frame_global = adjframeglobal.detach().numpy().sum(axis=1)
return y, wids_frame_global
class ModelGCNConcAfterLocalFrame(nn.Module):
def __init__(self, gcn_layers, num_feats, num_class):
super().__init__()
self.graph = GraphModule(gcn_layers, num_feats)
self.cls = ClassifierSimple(2 * num_feats, num_feats, num_class)
def forward(self, feats, get_adj=False):
N, FR, B, NF = feats.shape
feats = feats.view(N * FR, B, NF)
if get_adj:
x, adjobj = self.graph(feats, get_adj)
adjobj = adjobj.cpu()
wids_objects = adjobj.numpy().sum(axis=1)
x = x.view(N, FR, -1)
x, adjframelocal = self.graph(x, get_adj)
adjframelocal = adjframelocal.cpu()
wids_frame_local = adjframelocal.numpy().sum(axis=1)
return x, wids_objects, wids_frame_local
else:
x = self.graph(feats)
x = x.view(N, FR, -1)
x = self.graph(x)
return x
class ModelGCNConcAfterClassifier(nn.Module):
def __init__(self, gcn_layers, num_feats, num_class):
super().__init__()
self.graph = GraphModule(gcn_layers, num_feats)
self.cls = ClassifierSimple(2 * num_feats, num_feats, num_class)
def forward(self, feats):
x = self.cls(feats)
return x