-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
206 lines (162 loc) · 6.22 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
import torch
import torch.nn as nn
import torch.nn.functional as F
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 tokengraph_with_global_part_sharing(nn.Module):
def __init__(self, gcn_layers, num_feats, num_class):
super().__init__()
self.graph = GraphModule(gcn_layers, num_feats)
self.graph_omega = GraphModule(gcn_layers, num_feats)
self.cls = ClassifierSimple(2*num_feats, num_feats, num_class)
def forward(self, feats, feats_global):
N, FR, B, NF = feats.shape
feats = feats.view(N * FR, B, NF)
x = self.graph(feats)
x = x.view(N, FR, NF)
x = self.graph_omega(x)
y = self.graph_omega(feats_global)
x = torch.cat([x, y], dim=-1)
x = self.cls(x)
return x
class cls_only(nn.Module):
def __init__(self, gcn_layers, num_feats, num_class):
super().__init__()
self.cls = ClassifierSimple(2*num_feats, num_feats, num_class)
def forward(self, feats, feats_global):
x = feats.mean(dim=-2)
x = x.mean(dim=-2)
y = feats_global.mean(dim=-2)
x = torch.cat([x, y], dim=-1)
x = self.cls(x)
return x
class tokens_as_extra_Graph_mean(nn.Module):
def __init__(self, gcn_layers, num_feats, num_class):
super().__init__()
self.graph = GraphModule(gcn_layers, num_feats)
self.graph_omega = GraphModule(gcn_layers, num_feats)
self.cls = ClassifierSimple(3*num_feats, num_feats, num_class)
def forward(self, feats, feats_global):
N, FR, B, NF = feats.shape
feats = feats.view(N * FR, B, NF)
x = self.graph_omega(feats)
x = x.view(N, FR, NF)
x = self.graph_omega(x)
x_tokens = self.graph(feats)
x_tokens = x_tokens.view(N, FR, NF)
x_tokens = x_tokens.mean(dim=-2)
y = self.graph_omega(feats_global)
x = torch.cat([x, x_tokens, y], dim=-1)
x = self.cls(x)
return x
class tokenGraph_and_Graph(nn.Module):
def __init__(self, gcn_layers, num_feats, num_class):
super().__init__()
self.graph = GraphModule(gcn_layers, num_feats)
self.graph_omega3 = GraphModule(gcn_layers, num_feats)
self.cls = ClassifierSimple(num_feats, int(num_feats/2), num_class)
def forward(self, feats):
N, FR, B, NF = feats.shape
feats = feats.view(N * FR, B, NF)
x_tokens = self.graph(feats)
x_tokens = x_tokens.view(N, FR, NF)
x_tokens = self.graph_omega3(x_tokens)
x = self.cls(x_tokens)
return x
class tokenGraph_and_Graph_shared(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):
N, FR, B, NF = feats.shape
feats = feats.view(N * FR, B, NF)
x_tokens = self.graph(feats)
x_tokens = x_tokens.view(N, FR, NF)
x_tokens = self.graph(x_tokens)
x = self.cls(x_tokens)
return x
class tokenGraph_and_mean(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):
N, FR, B, NF = feats.shape
feats = feats.view(N * FR, B, NF)
x_tokens = self.graph(feats)
x_tokens = x_tokens.view(N, FR, NF)
x_tokens = x_tokens.mean(dim=-2)
x = self.cls(x_tokens)
return x
class Graph_and_tokenGraph(nn.Module):
def __init__(self, gcn_layers, num_feats, num_class):
super().__init__()
self.graph = GraphModule(gcn_layers, num_feats)
self.graph_omega3 = GraphModule(gcn_layers, num_feats)
self.cls = ClassifierSimple(num_feats, int(num_feats/2), num_class)
def forward(self, feats):
N, FR, B, NF = feats.shape
feats = feats.view(N * FR, B, NF)
x_tokens = self.graph_omega3(feats)
x_tokens = x_tokens.view(N, FR, NF)
x_tokens = self.graph(x_tokens)
x = self.cls(x_tokens)
return x
class mean_and_tokenGraph(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):
N, FR, B, NF = feats.shape
feats = feats.view(N * FR, B, NF)
x_tokens = feats.mean(dim=-2)
x_tokens = x_tokens.view(N, FR, NF)
x_tokens = self.graph(x_tokens)
x = self.cls(x_tokens)
return x