-
Notifications
You must be signed in to change notification settings - Fork 1
/
VCSR_Modules.py
305 lines (238 loc) · 10.3 KB
/
VCSR_Modules.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
# -----------------------------------------------------------
# "Exploring a Fine-Grained Multiscale Method for Cross-Modal Remote Sensing Image Retrieval"
# Yuan, Zhiqiang and Zhang, Wenkai and Fu, Kun and Li, Xuan and Deng, Chubo and Wang, Hongqi and Sun, Xian
# IEEE Transactions on Geoscience and Remote Sensing 2021
# Writen by YuanZhiqiang, 2021. Our code is depended on MTFN
# ------------------------------------------------------------
import torch
import torch.nn as nn
import torch.distributed as dist
import torch.nn.init
import torchvision.models as models
from torch.autograd import Variable
from torch.nn.utils.clip_grad import clip_grad_norm
import numpy as np
from collections import OrderedDict
from torchvision.models.resnet import resnet18,resnet50
import torch.nn.functional as F
from layers import seq2vec
import math
import copy
def l2norm(X, dim, eps=1e-8):
"""L2-normalize columns of X
"""
norm = torch.pow(X, 2).sum(dim=dim, keepdim=True).sqrt() + eps
X = torch.div(X, norm)
return X
class ExtractFeature(nn.Module):
def __init__(self, opt = {}, finetune=True):
super(ExtractFeature, self).__init__()
self.embed_dim = opt['embed']['embed_dim']
self.resnet = resnet50(pretrained=True)
for param in self.resnet.parameters():
param.requires_grad = finetune
self.pool_2x2 = nn.MaxPool2d(4)
self.up_sample_2 = nn.Upsample(scale_factor=2, mode='nearest')
self.up_sample_4 = nn.Upsample(scale_factor=4, mode='nearest')
self.linear = nn.Linear(in_features=512, out_features=self.embed_dim)
def forward(self, img):
x = self.resnet.conv1(img)
x = self.resnet.bn1(x)
x = self.resnet.relu(x)
x = self.resnet.maxpool(x)
f1 = self.resnet.layer1(x)
f2 = self.resnet.layer2(f1)
f3 = self.resnet.layer3(f2)
f4 = self.resnet.layer4(f3)
# Lower Feature
f2_up = self.up_sample_2(f2)
lower_feature = torch.cat([f1, f2_up], dim=1)
# Higher Feature
f4_up = self.up_sample_2(f4)
higher_feature = torch.cat([f3, f4_up], dim=1)
# higher_feature = self.up_sample_4(higher_feature)
# batch * 512
feature = f4.view(f4.shape[0], 512, -1)
solo_feature = self.linear(torch.mean(feature,dim=-1))
# torch.Size([10, 192, 64, 64])
# torch.Size([10, 768, 64, 64])
# torch.Size([10, 512])
return lower_feature, higher_feature, solo_feature
class VSA_Module(nn.Module):
def __init__(self, opt = {}):
super(VSA_Module, self).__init__()
# extract value
channel_size = opt['multiscale']['multiscale_input_channel']
out_channels = opt['multiscale']['multiscale_output_channel']
embed_dim = opt['embed']['embed_dim']
# sub sample
self.LF_conv = nn.Conv2d(in_channels=192, out_channels=channel_size, kernel_size=3, stride=4)
self.HF_conv = nn.Conv2d(in_channels=768, out_channels=channel_size, kernel_size=1, stride=1)
# visual attention
self.conv1x1_1 = nn.Conv2d(in_channels=channel_size*2, out_channels=out_channels, kernel_size=1)
self.conv1x1_2 = nn.Conv2d(in_channels=channel_size*2, out_channels=out_channels, kernel_size=1)
# solo attention
self.solo_attention = nn.Linear(in_features=256, out_features=embed_dim)
def forward(self, lower_feature, higher_feature, solo_feature):
# b x channel_size x 16 x 16
lower_feature = self.LF_conv(lower_feature)
higher_feature = self.HF_conv(higher_feature)
# concat
concat_feature = torch.cat([lower_feature, higher_feature], dim=1)
# residual
concat_feature = higher_feature.mean(dim=1,keepdim=True).expand_as(concat_feature) + concat_feature
# attention
main_feature = self.conv1x1_1(concat_feature)
attn_feature = torch.sigmoid(self.conv1x1_2(concat_feature).view(concat_feature.shape[0],1,-1)).view(concat_feature.shape[0], 1, main_feature.shape[2], main_feature.shape[3])
atted_feature = (main_feature*attn_feature).squeeze(dim=1).view(attn_feature.shape[0], -1)
# solo attention
solo_att = torch.sigmoid(self.solo_attention(atted_feature))
solo_feature1 = solo_feature*solo_att
return solo_feature
class Skipthoughts_Embedding_Module(nn.Module):
def __init__(self, vocab, opt, out_dropout=-1):
super(Skipthoughts_Embedding_Module, self).__init__()
self.opt = opt
self.vocab_words = vocab
self.seq2vec = seq2vec.factory(self.vocab_words, self.opt['seq2vec'], self.opt['seq2vec']['dropout'])
self.to_out = nn.Linear(in_features=2400, out_features=self.opt['embed']['embed_dim'])
self.dropout = out_dropout
def forward(self, input_text ):
x_t_vec = self.seq2vec(input_text)
out = F.relu(self.to_out(x_t_vec))
if self.dropout >= 0:
out = F.dropout(out, self.dropout)
return out
def cosine_similarity(x1, x2, dim=-1, eps=1e-8):
"""Returns cosine similarity between x1 and x2, computed along dim."""
w12 = torch.sum(x1 * x2, dim)
w1 = torch.norm(x1, 2, dim)
w2 = torch.norm(x2, 2, dim)
return (w12 / (w1 * w2).clamp(min=eps)).squeeze()
def sim(img,text):
w12=img*text
w1=torch.norm(img,2)
w2=torch.norm(text,2)
return w12/(w1*w2)
def cosine_sim(im, s):
"""Cosine similarity between all the image and sentence pairs
"""
im = l2norm(im, dim=-1)
s = l2norm(s, dim=-1)
w12 = im.mm(s.t())
return w12
# cross attention
class CrossAttention(nn.Module):
def __init__(self, opt={}):
super(CrossAttention, self).__init__()
self.att_type = opt['cross_attention']['att_type']
dim = opt['embed']['embed_dim']
self.softmax = nn.Softmax(-1)
if self.att_type == "soft_att":
self.cross_attention = nn.Sequential(
nn.Linear(dim, dim),
nn.Sigmoid()
)
elif self.att_type == "fusion_att":
self.cross_attention_fc1 = nn.Sequential(
nn.Linear(2*dim, dim),
nn.Sigmoid()
)
self.cross_attention_fc2 = nn.Sequential(
nn.Linear(2*dim, dim),
)
self.cross_attention = lambda x:self.cross_attention_fc1(x)*self.cross_attention_fc2(x)
elif self.att_type == "similarity_att":
self.fc_visual = nn.Sequential(
nn.Linear(dim, dim),
)
self.fc_text = nn.Sequential(
nn.Linear(dim, dim),
)
elif self.att_type == "sim_att":
self.fc_visual = nn.Sequential(
nn.Linear(dim, dim),
)
self.fc_text = nn.Sequential(
nn.Linear(dim, dim),
)
elif self.att_type == "ls_att":
self.fc_visual = nn.Sequential(
nn.Linear(2*dim, dim),
nn.Sigmoid()
)
self.fc_text = nn.Sequential(
nn.Linear(2*dim, dim),
)
self.cross_attention = lambda x:self.fc_text(x)*self.fc_visual(x)
else:
raise Exception
def forward(self, visual, text):
batch_v = visual.shape[0]
batch_t = text.shape[0]
if self.att_type == "soft_att":
visual_gate = self.cross_attention(visual)
# mm
visual = visual.unsqueeze(dim=1).expand(-1, batch_t, -1)
visual_gate = visual_gate.unsqueeze(dim=1).expand(-1, batch_t, -1)
text = text.unsqueeze(dim=0).expand(batch_v, -1, -1)
return visual_gate*text,visual
elif self.att_type == "fusion_att":
visual = visual.unsqueeze(dim=1).expand(-1, batch_t, -1)
text = text.unsqueeze(dim=0).expand(batch_v, -1, -1)
fusion_vec = torch.cat([visual,text], dim=-1)
return self.cross_attention(fusion_vec),visual
elif self.att_type == "similarity_att":
visual = self.fc_visual(visual)
text = self.fc_text(text)
visual = visual.unsqueeze(dim=1).expand(-1, batch_t, -1)
text = text.unsqueeze(dim=0).expand(batch_v, -1, -1)
sims = visual*text
return F.sigmoid(sims) * text,visual
elif self.att_type == "sim_att":
#visual = self.fc_visual(visual)
visual = visual.unsqueeze(dim=1).expand(-1, batch_t, -1)
text = text.unsqueeze(dim=0).expand(batch_v, -1, -1)
visual=sim(visual,text)
#visual=F.relu(visual)/torch.norm(visual,2)
return F.sigmoid(visual)*text,visual
elif self.att_type=="ls_att":
visual = visual.unsqueeze(dim=1).expand(-1, batch_t, -1)
text = text.unsqueeze(dim=0).expand(batch_v, -1, -1)
text1=torch.sigmoid(visual*text)*text
visual1=torch.sigmoid(visual*text1)*visual
return text1, visual1
class VGMF_Fusion(nn.Module):
def __init__(self, opt = {}):
super(VGMF_Fusion, self).__init__()
self.gate = nn.Linear(1024, opt['embed']['embed_dim'])
def forward(self, sv, kv):
# l2 norm
sv = l2norm(sv, dim=-1)
kv = l2norm(kv, dim=-1)
# concat fc
sw_s = F.sigmoid(self.gate(torch.cat([sv, kv], dim=-1)))
ones = torch.ones(sw_s.shape).cuda()
sw_k = ones - sw_s
out = sw_s*sv + sw_k*kv
return out
class VGMF_Fusion1(nn.Module):
def __init__(self, opt = {}):
super(VGMF_Fusion1, self).__init__()
# dynamic fusion
self.dynamic_weight = nn.Sequential(
nn.Linear(512, opt['embed']['embed_dim']),
nn.Sigmoid(),
nn.Dropout(0.2),
nn.Linear(opt['embed']['embed_dim'], 2),
nn.Softmax()
)
def forward(self, sv, kv):
global_feature = F.sigmoid(kv) * sv
local_feature = kv + sv
feature_gl = global_feature + local_feature
dynamic_weight = self.dynamic_weight(feature_gl)
weight_global = dynamic_weight[:, 0].reshape(feature_gl.shape[0], -1).expand_as(sv)
weight_local = dynamic_weight[:, 0].reshape(feature_gl.shape[0], -1).expand_as(sv)
visual_feature = weight_global*sv + weight_local*kv
return visual_feature