-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodel.py
375 lines (304 loc) · 13.7 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
import torch
import math
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Function
from torch.nn import Parameter
from torchvision import models
from util import *
class GraphConvolution(nn.Module):
"""
Simple GCN layer, similar to https://arxiv.org/abs/1609.02907
"""
def __init__(self, in_features, out_features, bias=False):
super(GraphConvolution, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.weight = Parameter(torch.Tensor(in_features, out_features))
if bias:
self.bias = Parameter(torch.Tensor(1, 1, out_features))
else:
self.register_parameter('bias', None)
self.reset_parameters()
def reset_parameters(self):
stdv = 1. / math.sqrt(self.weight.size(1))
self.weight.data.uniform_(-stdv, stdv)
if self.bias is not None:
self.bias.data.uniform_(-stdv, stdv)
def forward(self, input, adj):
support = torch.matmul(input, self.weight)
output = torch.matmul(adj, support)
if self.bias is not None:
return output + self.bias
else:
return output
def __repr__(self):
return self.__class__.__name__ + ' (' \
+ str(self.in_features) + ' -> ' \
+ str(self.out_features) + ')'
class GraphAttentionLayer(nn.Module):
"""
Simple GAT layer, similar to https://arxiv.org/abs/1710.10903
"""
def __init__(self, in_features, out_features, dropout=0, alpha=0.2, concat=True):
super(GraphAttentionLayer, self).__init__()
self.dropout = dropout
self.in_features = in_features
self.out_features = out_features
self.alpha = alpha
self.concat = concat
self.W = nn.Parameter(torch.zeros(size=(in_features, out_features)))
nn.init.xavier_uniform_(self.W.data, gain=1.414)
self.a = nn.Parameter(torch.zeros(size=(2 * out_features, 1)))
nn.init.xavier_uniform_(self.a.data, gain=1.414)
self.leakyrelu = nn.LeakyReLU(self.alpha)
def forward(self, input, adj):
h = torch.mm(input, self.W)
N = h.size()[0]
a_input = torch.cat([h.repeat(1, N).view(N * N, -1), h.repeat(N, 1)], dim=1).view(N, -1, 2 * self.out_features)
e = self.leakyrelu(torch.matmul(a_input, self.a).squeeze(2))
zero_vec = -9e15 * torch.ones_like(e)
attention = torch.where(adj > 0, e, zero_vec)
attention = F.softmax(attention, dim=1)
attention = F.dropout(attention, self.dropout, training=self.training)
h_prime = torch.matmul(attention, h)
if self.concat:
return F.elu(h_prime)
else:
return h_prime
def __repr__(self):
return self.__class__.__name__ + ' (' + str(self.in_features) + ' -> ' + str(self.out_features) + ')'
class VGGNet(nn.Module):
def __init__(self):
"""Select conv1_1 ~ conv5_1 activation maps."""
super(VGGNet, self).__init__()
self.vgg = models.vgg19_bn(pretrained=True)
self.vgg_features = self.vgg.features
self.fc_features = nn.Sequential(*list(self.vgg.classifier.children())[:-2])
def forward(self, x):
"""Extract multiple convolutional feature maps."""
features = self.vgg_features(x).view(x.shape[0], -1)
features = self.fc_features(features)
return features
class TxtMLP(nn.Module):
def __init__(self, code_len=300, txt_bow_len=1386, num_class=24):
super(TxtMLP, self).__init__()
self.fc1 = nn.Linear(txt_bow_len, 4096)
self.fc2 = nn.Linear(4096, code_len)
self.classifier = nn.Linear(code_len, num_class)
def forward(self, x):
feat = F.leaky_relu(self.fc1(x), 0.2)
feat = F.leaky_relu(self.fc2(feat), 0.2)
predict = self.classifier(feat)
return feat, predict
class ImgNN(nn.Module):
"""Network to learn image representations"""
def __init__(self, input_dim=4096, output_dim=1024):
super(ImgNN, self).__init__()
self.bn = nn.BatchNorm1d(input_dim)
self.denseL1 = nn.Linear(input_dim, output_dim)
def forward(self, x):
x = self.bn(x)
out = F.relu(self.denseL1(x))
return out
class TextNN(nn.Module):
"""Network to learn text representations"""
def __init__(self, input_dim=1024, output_dim=1024):
super(TextNN, self).__init__()
self.bn = nn.BatchNorm1d(input_dim)
self.denseL1 = nn.Linear(input_dim, output_dim)
def forward(self, x):
x = self.bn(x)
out = F.relu(self.denseL1(x))
return out
class ReverseLayerF(Function):
@staticmethod
def forward(ctx, x, alpha):
ctx.alpha = alpha
return x.view_as(x)
@staticmethod
def backward(ctx, grad_output):
output = grad_output.neg() * ctx.alpha
return output, None
class ModalClassifier(nn.Module):
"""Network to discriminate modalities"""
def __init__(self, input_dim=40):
super(ModalClassifier, self).__init__()
self.bn = nn.BatchNorm1d(input_dim)
self.denseL1 = nn.Linear(input_dim, input_dim // 4)
self.denseL2 = nn.Linear(input_dim // 4, input_dim // 16)
self.denseL3 = nn.Linear(input_dim // 16, 2)
def forward(self, x):
x = ReverseLayerF.apply(x, 1.0)
x = self.bn(x)
out = F.relu(self.denseL1(x))
out = F.relu(self.denseL2(out))
out = self.denseL3(out)
return out
class ImgDec(nn.Module):
"""Network to decode image representations"""
def __init__(self, input_dim=1024, output_dim=4096, hidden_dim=2048):
super(ImgDec, self).__init__()
self.bn = nn.BatchNorm1d(input_dim)
self.denseL1 = nn.Linear(input_dim, hidden_dim)
self.denseL2 = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
x = self.bn(x)
out = F.relu(self.denseL1(x))
out = F.relu(self.denseL2(out))
return out
class TextDec(nn.Module):
"""Network to decode image representations"""
def __init__(self, input_dim=1024, output_dim=300, hidden_dim=512):
super(TextDec, self).__init__()
self.bn = nn.BatchNorm1d(input_dim)
self.denseL1 = nn.Linear(input_dim, hidden_dim)
self.denseL2 = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
x = self.bn(x)
out = F.leaky_relu(self.denseL1(x), 0.2)
out = F.leaky_relu(self.denseL2(out), 0.2)
return out
class P_GNN(nn.Module):
def __init__(self, img_input_dim=4096, text_input_dim=1024, minus_one_dim=1024, num_classes=10, in_channel=300, t=0,
adj_file=None, inp=None, GNN='GAT', n_layers=4):
super(P_GNN, self).__init__()
self.img_net = ImgNN(img_input_dim, minus_one_dim)
self.text_net = TextNN(text_input_dim, minus_one_dim)
self.img2text_net = TextDec(minus_one_dim, text_input_dim)
self.text2img_net = ImgDec(minus_one_dim, img_input_dim)
self.img_md_net = ModalClassifier(img_input_dim)
self.text_md_net = ModalClassifier(text_input_dim)
self.num_classes = num_classes
if GNN == 'GAT':
self.gnn = GraphAttentionLayer
elif GNN == 'GCN':
self.gnn = GraphConvolution
else:
raise NameError("Invalid GNN name!")
self.n_layers = n_layers
self.relu = nn.LeakyReLU(0.2)
self.lrn = [self.gnn(in_channel, minus_one_dim)]
for i in range(1, self.n_layers):
self.lrn.append(self.gnn(minus_one_dim, minus_one_dim))
for i, layer in enumerate(self.lrn):
self.add_module('lrn_{}'.format(i), layer)
self.hypo = nn.Linear(self.n_layers * minus_one_dim, minus_one_dim)
_adj = torch.FloatTensor(gen_A(num_classes, t, adj_file))
if GNN == 'GAT':
self.adj = Parameter(_adj, requires_grad=False)
else:
self.adj = Parameter(gen_adj(_adj), requires_grad=False)
if inp is not None:
self.inp = Parameter(inp, requires_grad=False)
else:
self.inp = Parameter(torch.rand(num_classes, in_channel))
# image normalization
self.image_normalization_mean = [0.485, 0.456, 0.406]
self.image_normalization_std = [0.229, 0.224, 0.225]
def forward(self, feature_img, feature_text):
view1_feature = self.img_net(feature_img)
view2_feature = self.text_net(feature_text)
layers = []
x = self.inp
for i in range(self.n_layers):
x = self.lrn[i](x, self.adj)
if self.gnn == GraphConvolution:
x = self.relu(x)
layers.append(x)
x = torch.cat(layers, -1)
x = self.hypo(x)
norm_img = torch.norm(view1_feature, dim=1)[:, None] * torch.norm(x, dim=1)[None, :] + 1e-6
norm_txt = torch.norm(view2_feature, dim=1)[:, None] * torch.norm(x, dim=1)[None, :] + 1e-6
x = x.transpose(0, 1)
y_img = torch.matmul(view1_feature, x)
y_text = torch.matmul(view2_feature, x)
y_img = y_img / norm_img
y_text = y_text / norm_txt
view1_feature_view2 = self.img2text_net(view1_feature)
view2_feature_view1 = self.text2img_net(view2_feature)
view1_modal_view1 = self.img_md_net(feature_img)
view2_modal_view1 = self.img_md_net(view2_feature_view1)
view1_modal_view2 = self.text_md_net(view1_feature_view2)
view2_modal_view2 = self.text_md_net(feature_text)
return view1_feature, view2_feature, y_img, y_text, x.transpose(0, 1), \
view1_modal_view1, view2_modal_view1, view1_modal_view2, view2_modal_view2
class I_GNN(nn.Module):
def __init__(self, img_input_dim=4096, text_input_dim=1024, minus_one_dim=1024, num_classes=10, in_channel=300,
t=0.4, k=3, inp=None, GNN='GAT', n_layers=4):
super(I_GNN, self).__init__()
self.img_net = ImgNN(img_input_dim, minus_one_dim)
self.text_net = TextNN(text_input_dim, minus_one_dim)
self.img2text_net = TextDec(minus_one_dim, text_input_dim)
self.text2img_net = ImgDec(minus_one_dim, img_input_dim)
self.img_md_net = ModalClassifier(img_input_dim)
self.text_md_net = ModalClassifier(text_input_dim)
self.num_classes = num_classes
if GNN == 'GAT':
self.gnn = GraphAttentionLayer
elif GNN == 'GCN':
self.gnn = GraphConvolution
else:
raise NameError("Invalid GNN name!")
self.n_layers = n_layers
self.relu = nn.LeakyReLU(0.2)
self.lrn = [self.gnn(in_channel, minus_one_dim)]
for i in range(1, self.n_layers):
self.lrn.append(self.gnn(minus_one_dim, minus_one_dim))
for i, layer in enumerate(self.lrn):
self.add_module('lrn_{}'.format(i), layer)
self.hypo = nn.Linear(self.n_layers * minus_one_dim, minus_one_dim)
if inp is None:
raise NotImplementedError("Category embeddings are missing!")
self.inp = Parameter(inp, requires_grad=True)
normalized_inp = F.normalize(inp, dim=1)
self.A0 = Parameter(torch.matmul(normalized_inp, normalized_inp.T), requires_grad=False)
self.A0[self.A0 < t] = 0
self.t = t
self.Wk = list()
self.k = k
for i in range(k):
Wk_temp = Parameter(torch.zeros(in_channel))
torch.nn.init.uniform_(Wk_temp.data, 0.3, 0.6)
self.Wk.append(Wk_temp)
for i, Wk_temp in enumerate(self.Wk):
self.register_parameter('W_{}'.format(i), Wk_temp)
self.lambdda = 0.5
# image normalization
self.image_normalization_mean = [0.485, 0.456, 0.406]
self.image_normalization_std = [0.229, 0.224, 0.225]
def forward(self, feature_img, feature_text):
view1_feature = self.img_net(feature_img)
view2_feature = self.text_net(feature_text)
S = torch.zeros_like(self.A0)
for Wk_temp in self.Wk:
normalized_imp_mul_Wk = F.normalize(self.inp * Wk_temp[None, :], dim=1)
S += torch.matmul(normalized_imp_mul_Wk, normalized_imp_mul_Wk.T)
S /= self.k
S[S < self.t] = 0
A = self.lambdda * self.A0 + (1-self.lambdda) * S
adj = gen_adj(A)
layers = []
x = self.inp
for i in range(self.n_layers):
x = self.lrn[i](x, adj)
if self.gnn == GraphConvolution:
x = self.relu(x)
layers.append(x)
x = torch.cat(layers, -1)
x = self.hypo(x)
norm_img = torch.norm(view1_feature, dim=1)[:, None] * torch.norm(x, dim=1)[None, :] + 1e-6
norm_txt = torch.norm(view2_feature, dim=1)[:, None] * torch.norm(x, dim=1)[None, :] + 1e-6
x = x.transpose(0, 1)
y_img = torch.matmul(view1_feature, x)
y_text = torch.matmul(view2_feature, x)
y_img = y_img / norm_img
y_text = y_text / norm_txt
view1_feature_view2 = self.img2text_net(view1_feature)
view2_feature_view1 = self.text2img_net(view2_feature)
view1_modal_view1 = self.img_md_net(feature_img)
view2_modal_view1 = self.img_md_net(view2_feature_view1)
view1_modal_view2 = self.text_md_net(view1_feature_view2)
view2_modal_view2 = self.text_md_net(feature_text)
return view1_feature, view2_feature, y_img, y_text, x.transpose(0, 1), \
view1_modal_view1, view2_modal_view1, view1_modal_view2, view2_modal_view2