-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmodel.py
420 lines (374 loc) · 18.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
class TransZero(nn.Module):
def __init__(self, config, att, init_w2v_att, seenclass, unseenclass,
is_bias=True, bias=1, is_conservative=True):
super(TransZero, self).__init__()
self.config = config
self.dim_f = config.dim_f
self.dim_v = config.dim_v
self.nclass = config.num_class
self.seenclass = seenclass
self.unseenclass = unseenclass
self.is_bias = is_bias
self.is_conservative = is_conservative
# class-level semantic vectors
self.att = nn.Parameter(F.normalize(att), requires_grad=False)
# GloVe features for attributes name
self.V = nn.Parameter(F.normalize(init_w2v_att), requires_grad=True)
# for self-calibration
self.bias = nn.Parameter(torch.tensor(bias), requires_grad=False)
mask_bias = np.ones((1, self.nclass))
mask_bias[:, self.seenclass.cpu().numpy()] *= -1
self.mask_bias = nn.Parameter(torch.tensor(
mask_bias, dtype=torch.float), requires_grad=False)
# mapping
self.W_1 = nn.Parameter(nn.init.normal_(
torch.empty(self.dim_v, config.tf_common_dim)), requires_grad=True)
# transformer
self.transformer = Transformer(
ec_layer=config.tf_ec_layer,
dc_layer=config.tf_dc_layer,
dim_com=config.tf_common_dim,
dim_feedforward=config.tf_dim_feedforward,
dropout=config.tf_dropout,
SAtt=config.tf_SAtt,
heads=config.tf_heads,
aux_embed=config.tf_aux_embed)
# for loss computation
self.log_softmax_func = nn.LogSoftmax(dim=1)
self.weight_ce = nn.Parameter(torch.eye(self.nclass), requires_grad=False)
def forward(self, input, from_img=False):
Fs = self.resnet101(input) if from_img else input
# transformer-based visual-to-semantic embedding
v2s_embed = self.forward_feature_transformer(Fs)
# classification
package = {'pred': self.forward_attribute(v2s_embed),
'embed': v2s_embed}
package['S_pp'] = package['pred']
return package
def forward_feature_transformer(self, Fs):
# visual
if len(Fs.shape) == 4:
shape = Fs.shape
Fs = Fs.reshape(shape[0], shape[1], shape[2] * shape[3])
Fs = F.normalize(Fs, dim=1)
# attributes
V_n = F.normalize(self.V) if self.config.normalize_V else self.V
# locality-augmented visual features
Trans_out = self.transformer(Fs, V_n)
# embedding to semantic space
embed = torch.einsum('iv,vf,bif->bi', V_n, self.W_1, Trans_out)
return embed
def forward_attribute(self, embed):
embed = torch.einsum('ki,bi->bk', self.att, embed)
self.vec_bias = self.mask_bias*self.bias
embed = embed + self.vec_bias
return embed
def compute_loss_Self_Calibrate(self, in_package):
S_pp = in_package['pred']
Prob_all = F.softmax(S_pp, dim=-1)
Prob_unseen = Prob_all[:, self.unseenclass]
assert Prob_unseen.size(1) == len(self.unseenclass)
mass_unseen = torch.sum(Prob_unseen, dim=1)
loss_pmp = -torch.log(torch.mean(mass_unseen))
return loss_pmp
def compute_aug_cross_entropy(self, in_package):
Labels = in_package['batch_label']
S_pp = in_package['pred']
if self.is_bias:
S_pp = S_pp - self.vec_bias
if not self.is_conservative:
S_pp = S_pp[:, self.seenclass]
Labels = Labels[:, self.seenclass]
assert S_pp.size(1) == len(self.seenclass)
Prob = self.log_softmax_func(S_pp)
loss = -torch.einsum('bk,bk->b', Prob, Labels)
loss = torch.mean(loss)
return loss
def compute_reg_loss(self, in_package):
tgt = torch.matmul(in_package['batch_label'], self.att)
embed = in_package['embed']
loss_reg = F.mse_loss(embed, tgt, reduction='mean')
return loss_reg
def compute_loss(self, in_package):
if len(in_package['batch_label'].size()) == 1:
in_package['batch_label'] = self.weight_ce[in_package['batch_label']]
loss_CE = self.compute_aug_cross_entropy(in_package)
loss_cal = self.compute_loss_Self_Calibrate(in_package)
loss_reg = self.compute_reg_loss(in_package)
loss = loss_CE + self.config.lambda_ * \
loss_cal + self.config.lambda_reg * loss_reg
out_package = {'loss': loss, 'loss_CE': loss_CE,
'loss_cal': loss_cal, 'loss_reg': loss_reg}
return out_package
class Transformer(nn.Module):
def __init__(self, ec_layer=1, dc_layer=1, dim_com=300,
dim_feedforward=2048, dropout=0.1, heads=1,
in_dim_cv=2048, in_dim_attr=300, SAtt=True,
aux_embed=True):
super(Transformer, self).__init__()
# input embedding
self.embed_cv = nn.Sequential(nn.Linear(in_dim_cv, dim_com))
if aux_embed:
self.embed_cv_aux = nn.Sequential(nn.Linear(in_dim_cv, dim_com))
self.embed_attr = nn.Sequential(nn.Linear(in_dim_attr, dim_com))
# transformer encoder
self.transformer_encoder = MultiLevelEncoder_woPad(N=ec_layer,
d_model=dim_com,
h=1,
d_k=dim_com,
d_v=dim_com,
d_ff=dim_feedforward,
dropout=dropout)
# transformer decoder
decoder_layer = TransformerDecoderLayer(d_model=dim_com,
nhead=heads,
dim_feedforward=dim_feedforward,
dropout=dropout,
SAtt=SAtt)
self.transformer_decoder = nn.TransformerDecoder(
decoder_layer, num_layers=dc_layer)
def forward(self, f_cv, f_attr):
# linearly map to common dim
h_cv = self.embed_cv(f_cv.permute(0, 2, 1))
h_attr = self.embed_attr(f_attr)
h_attr_batch = h_attr.unsqueeze(0).repeat(f_cv.shape[0], 1, 1)
# visual encoder
memory = self.transformer_encoder(h_cv).permute(1, 0, 2)
# attribute-visual decoder
out = self.transformer_decoder(h_attr_batch.permute(1, 0, 2), memory)
return out.permute(1, 0, 2)
class EncoderLayer(nn.Module):
def __init__(self, d_model=512, d_k=64, d_v=64, h=8, d_ff=2048,
dropout=.1, identity_map_reordering=False,
attention_module=None, attention_module_kwargs=None):
super(EncoderLayer, self).__init__()
self.identity_map_reordering = identity_map_reordering
self.mhatt = MultiHeadGeometryAttention(d_model, d_k, d_v, h, dropout,
identity_map_reordering=identity_map_reordering,
attention_module=attention_module,
attention_module_kwargs=attention_module_kwargs)
self.dropout = nn.Dropout(dropout)
self.lnorm = nn.LayerNorm(d_model)
self.pwff = PositionWiseFeedForward(
d_model, d_ff, dropout, identity_map_reordering=identity_map_reordering)
def forward(self, queries, keys, values, relative_geometry_weights,
attention_mask=None, attention_weights=None, pos=None):
q, k = (queries + pos, keys +
pos) if pos is not None else (queries, keys)
att = self.mhatt(q, k, values, relative_geometry_weights,
attention_mask, attention_weights)
att = self.lnorm(queries + self.dropout(att))
ff = self.pwff(att)
return ff
class MultiLevelEncoder_woPad(nn.Module):
def __init__(self, N, d_model=512, d_k=64, d_v=64, h=8, d_ff=2048,
dropout=.1, identity_map_reordering=False,
attention_module=None, attention_module_kwargs=None):
super(MultiLevelEncoder_woPad, self).__init__()
self.d_model = d_model
self.dropout = dropout
self.layers = nn.ModuleList([EncoderLayer(d_model, d_k, d_v, h, d_ff, dropout,
identity_map_reordering=identity_map_reordering,
attention_module=attention_module,
attention_module_kwargs=attention_module_kwargs)
for _ in range(N)])
self.WGs = nn.ModuleList(
[nn.Linear(64, 1, bias=True) for _ in range(h)])
def forward(self, input, attention_mask=None, attention_weights=None, pos=None):
relative_geometry_embeddings = BoxRelationalEmbedding(
input, grid_size=(14, 14))
flatten_relative_geometry_embeddings = relative_geometry_embeddings.view(
-1, 64)
box_size_per_head = list(relative_geometry_embeddings.shape[:3])
box_size_per_head.insert(1, 1)
relative_geometry_weights_per_head = [layer(
flatten_relative_geometry_embeddings).view(box_size_per_head) for layer in self.WGs]
relative_geometry_weights = torch.cat(
(relative_geometry_weights_per_head), 1)
relative_geometry_weights = F.relu(relative_geometry_weights)
out = input
for layer in self.layers:
out = layer(out, out, out, relative_geometry_weights,
attention_mask, attention_weights, pos=pos)
return out
class TransformerDecoderLayer(nn.TransformerDecoderLayer):
def __init__(self, d_model, nhead, dim_feedforward=2048, dropout=0.1,
activation="relu", SAtt=True):
super(TransformerDecoderLayer, self).__init__(d_model, nhead,
dim_feedforward=dim_feedforward,
dropout=dropout,
activation=activation)
self.SAtt = SAtt
def forward(self, tgt, memory, tgt_mask=None, memory_mask=None,
tgt_key_padding_mask=None, memory_key_padding_mask=None):
if self.SAtt:
tgt2 = self.self_attn(tgt, tgt, tgt, attn_mask=tgt_mask,
key_padding_mask=tgt_key_padding_mask)[0]
tgt = self.norm1(tgt + self.dropout1(tgt2))
tgt2 = self.multihead_attn(tgt, memory, memory, attn_mask=memory_mask,
key_padding_mask=memory_key_padding_mask)[0]
tgt = tgt + self.dropout2(tgt2)
tgt = self.norm2(tgt)
tgt2 = self.linear2(self.dropout(self.activation(self.linear1(tgt))))
tgt = tgt + self.dropout3(tgt2)
tgt = self.norm3(tgt)
return tgt
def get_relative_pos(x, batch_size, norm_len):
x = x.view(1, -1, 1).expand(batch_size, -1, -1)
return x / norm_len
def get_grids_pos(batch_size, seq_len, grid_size=(7, 7)):
assert seq_len == grid_size[0] * grid_size[1]
x = torch.arange(0, grid_size[0]).float().cuda()
y = torch.arange(0, grid_size[1]).float().cuda()
px_min = x.view(-1, 1).expand(-1, grid_size[0]).contiguous().view(-1)
py_min = y.view(1, -1).expand(grid_size[1], -1).contiguous().view(-1)
px_max = px_min + 1
py_max = py_min + 1
rpx_min = get_relative_pos(px_min, batch_size, grid_size[0])
rpy_min = get_relative_pos(py_min, batch_size, grid_size[1])
rpx_max = get_relative_pos(px_max, batch_size, grid_size[0])
rpy_max = get_relative_pos(py_max, batch_size, grid_size[1])
return rpx_min, rpy_min, rpx_max, rpy_max
def BoxRelationalEmbedding(f_g, dim_g=64, wave_len=1000, trignometric_embedding=True,
grid_size=(7, 7)):
batch_size, seq_len = f_g.size(0), f_g.size(1)
x_min, y_min, x_max, y_max = get_grids_pos(batch_size, seq_len, grid_size)
cx = (x_min + x_max) * 0.5
cy = (y_min + y_max) * 0.5
w = (x_max - x_min) + 1.
h = (y_max - y_min) + 1.
delta_x = cx - cx.view(batch_size, 1, -1)
delta_x = torch.clamp(torch.abs(delta_x / w), min=1e-3)
delta_x = torch.log(delta_x)
delta_y = cy - cy.view(batch_size, 1, -1)
delta_y = torch.clamp(torch.abs(delta_y / h), min=1e-3)
delta_y = torch.log(delta_y)
delta_w = torch.log(w / w.view(batch_size, 1, -1))
delta_h = torch.log(h / h.view(batch_size, 1, -1))
matrix_size = delta_h.size()
delta_x = delta_x.view(batch_size, matrix_size[1], matrix_size[2], 1)
delta_y = delta_y.view(batch_size, matrix_size[1], matrix_size[2], 1)
delta_w = delta_w.view(batch_size, matrix_size[1], matrix_size[2], 1)
delta_h = delta_h.view(batch_size, matrix_size[1], matrix_size[2], 1)
position_mat = torch.cat((delta_x, delta_y, delta_w, delta_h), -1)
if trignometric_embedding == True:
feat_range = torch.arange(dim_g / 8).cuda()
dim_mat = feat_range / (dim_g / 8)
dim_mat = 1. / (torch.pow(wave_len, dim_mat))
dim_mat = dim_mat.view(1, 1, 1, -1)
position_mat = position_mat.view(
batch_size, matrix_size[1], matrix_size[2], 4, -1)
position_mat = 100. * position_mat
mul_mat = position_mat * dim_mat
mul_mat = mul_mat.view(batch_size, matrix_size[1], matrix_size[2], -1)
sin_mat = torch.sin(mul_mat)
cos_mat = torch.cos(mul_mat)
embedding = torch.cat((sin_mat, cos_mat), -1)
else:
embedding = position_mat
return (embedding)
class ScaledDotProductGeometryAttention(nn.Module):
def __init__(self, d_model, d_k, d_v, h, dropout=.1, comment=None):
super(ScaledDotProductGeometryAttention, self).__init__()
self.fc_q = nn.Linear(d_model, h * d_k)
self.fc_k = nn.Linear(d_model, h * d_k)
self.fc_v = nn.Linear(d_model, h * d_v)
self.fc_o = nn.Linear(h * d_v, d_model)
self.dropout = nn.Dropout(dropout)
self.d_model = d_model
self.d_k = d_k
self.d_v = d_v
self.h = h
self.init_weights()
self.comment = comment
def init_weights(self):
nn.init.xavier_uniform_(self.fc_q.weight)
nn.init.xavier_uniform_(self.fc_k.weight)
nn.init.xavier_uniform_(self.fc_v.weight)
nn.init.xavier_uniform_(self.fc_o.weight)
nn.init.constant_(self.fc_q.bias, 0)
nn.init.constant_(self.fc_k.bias, 0)
nn.init.constant_(self.fc_v.bias, 0)
nn.init.constant_(self.fc_o.bias, 0)
def forward(self, queries, keys, values, box_relation_embed_matrix,
attention_mask=None, attention_weights=None):
b_s, nq = queries.shape[:2]
nk = keys.shape[1]
q = self.fc_q(queries).view(b_s, nq, self.h,
self.d_k).permute(0, 2, 1, 3)
k = self.fc_k(keys).view(b_s, nk, self.h, self.d_k).permute(0, 2, 3, 1)
v = self.fc_v(values).view(b_s, nk, self.h,
self.d_v).permute(0, 2, 1, 3)
att = torch.matmul(q, k) / np.sqrt(self.d_k)
if attention_weights is not None:
att = att * attention_weights
if attention_mask is not None:
att = att.masked_fill(attention_mask, -np.inf)
w_g = box_relation_embed_matrix
w_a = att
w_mn = - w_g + w_a
w_mn = torch.softmax(w_mn, -1)
att = self.dropout(w_mn)
out = torch.matmul(att, v).permute(
0, 2, 1, 3).contiguous().view(b_s, nq, self.h * self.d_v)
out = self.fc_o(out)
return out
class MultiHeadGeometryAttention(nn.Module):
def __init__(self, d_model, d_k, d_v, h, dropout=.1, identity_map_reordering=False,
can_be_stateful=False, attention_module=None,
attention_module_kwargs=None, comment=None):
super(MultiHeadGeometryAttention, self).__init__()
self.identity_map_reordering = identity_map_reordering
self.attention = ScaledDotProductGeometryAttention(
d_model=d_model, d_k=d_k, d_v=d_v, h=h, comment=comment)
self.dropout = nn.Dropout(p=dropout)
self.layer_norm = nn.LayerNorm(d_model)
self.can_be_stateful = can_be_stateful
if self.can_be_stateful:
self.register_state('running_keys', torch.zeros((0, d_model)))
self.register_state('running_values', torch.zeros((0, d_model)))
def forward(self, queries, keys, values, relative_geometry_weights,
attention_mask=None, attention_weights=None):
if self.can_be_stateful and self._is_stateful:
self.running_keys = torch.cat([self.running_keys, keys], 1)
keys = self.running_keys
self.running_values = torch.cat([self.running_values, values], 1)
values = self.running_values
if self.identity_map_reordering:
q_norm = self.layer_norm(queries)
k_norm = self.layer_norm(keys)
v_norm = self.layer_norm(values)
out = self.attention(q_norm, k_norm, v_norm, relative_geometry_weights,
attention_mask, attention_weights)
out = queries + self.dropout(torch.relu(out))
else:
out = self.attention(queries, keys, values, relative_geometry_weights,
attention_mask, attention_weights)
out = self.dropout(out)
out = self.layer_norm(queries + out)
return out
class PositionWiseFeedForward(nn.Module):
def __init__(self, d_model=512, d_ff=2048, dropout=.1, identity_map_reordering=False):
super(PositionWiseFeedForward, self).__init__()
self.identity_map_reordering = identity_map_reordering
self.fc1 = nn.Linear(d_model, d_ff)
self.fc2 = nn.Linear(d_ff, d_model)
self.dropout = nn.Dropout(p=dropout)
self.dropout_2 = nn.Dropout(p=dropout)
self.layer_norm = nn.LayerNorm(d_model)
def forward(self, input):
if self.identity_map_reordering:
out = self.layer_norm(input)
out = self.fc2(self.dropout_2(F.relu(self.fc1(out))))
out = input + self.dropout(torch.relu(out))
else:
out = self.fc2(self.dropout_2(F.relu(self.fc1(input))))
out = self.dropout(out)
out = self.layer_norm(input + out)
return out
if __name__ == '__main__':
pass