-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.py
132 lines (102 loc) · 3.99 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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
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, device, 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, device):
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, device, 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, device)
x = x.view(N, FR, -1)
x = self.graph(x, device)
y = self.graph(feat_global, device)
x = torch.cat([x, y], dim=-1)
x = self.cls(x, device)
return x
else:
x, adjobj = self.graph(feats, device, get_adj)
adjobj = adjobj.cpu()
wids_objects = adjobj.numpy().sum(axis=1)
x = x.view(N, FR, -1)
x, adjframelocal = self.graph(x, device, get_adj)
adjframelocal = adjframelocal.cpu()
wids_frame_local = adjframelocal.numpy().sum(axis=1)
y, adjframeglobal = self.graph(feat_global, device, get_adj)
adjframeglobal = adjframeglobal.cpu()
wids_frame_global = adjframeglobal.numpy().sum(axis=1)
x = torch.cat([x, y], dim=-1)
x = self.cls(x, device)
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, feats, feat_global, device):
x = self.graph(feat_global, device)
x = self.cls(x, device)
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, device):
N, FR, B, NF = feats.shape
feats = feats.view(N * FR, B, NF)
x = self.graph(feats, device)
x = x.view(N, FR, -1)
x = self.graph(x, device)
x = self.cls(x, device)
return x