-
Notifications
You must be signed in to change notification settings - Fork 15
/
utils.py
436 lines (341 loc) · 13.8 KB
/
utils.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#encoding:utf-8
# -----------------------------------------------------------
# "Remote Sensing Cross-Modal Text-Image Retrieval Based on Global and Local Information"
# Yuan, Zhiqiang and Zhang, Wenkai and Changyuan Tian and Xuee, Rong and Zhengyuan Zhang and Wang, Hongqi and Fu, Kun and Sun, Xian
# Writen by YuanZhiqiang, 2021. Our code is depended on AMFMN
# ------------------------------------------------------------
import torch
import numpy as np
import sys
import math
from torch.autograd import Variable
from collections import OrderedDict
import torch.nn as nn
import shutil
import time
# 从npy中读取
def load_from_npy(filename):
info = np.load(filename, allow_pickle=True)
return info
# 保存结果到txt文件
def log_to_txt( contexts=None,filename="save.txt", mark=False,encoding='UTF-8',mode='a'):
f = open(filename, mode,encoding=encoding)
if mark:
sig = "------------------------------------------------\n"
f.write(sig)
elif isinstance(contexts, dict):
tmp = ""
for c in contexts.keys():
tmp += str(c)+" | "+ str(contexts[c]) +"\n"
contexts = tmp
f.write(contexts)
else:
if isinstance(contexts,list):
tmp = ""
for c in contexts:
tmp += str(c)
contexts = tmp
else:
contexts = contexts + "\n"
f.write(contexts)
f.close()
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=0):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / (.0001 + self.count)
def __str__(self):
"""String representation for logging
"""
# for values that should be recorded exactly e.g. iteration number
if self.count == 0:
return str(self.val)
# for stats
return '%.4f (%.4f)' % (self.val, self.avg)
class LogCollector(object):
"""A collection of logging objects that can change from train to val"""
def __init__(self):
# to keep the order of logged variables deterministic
self.meters = OrderedDict()
def update(self, k, v, n=0):
# create a new meter if previously not recorded
if k not in self.meters:
self.meters[k] = AverageMeter()
self.meters[k].update(v, n)
def __str__(self):
"""Concatenate the meters in one log line
"""
s = ''
for i, (k, v) in enumerate(self.meters.items()):
if i > 0:
s += ' '
s += k + ' ' + str(v)
return s
def tb_log(self, tb_logger, prefix='', step=None):
"""Log using tensorboard
"""
for k, v in self.meters.items():
tb_logger.log_value(prefix + k, v.val, step=step)
def update_values(dict_from, dict_to):
for key, value in dict_from.items():
if isinstance(value, dict):
update_values(dict_from[key], dict_to[key])
elif value is not None:
dict_to[key] = dict_from[key]
return dict_to
def params_count(model):
count = 0
for p in model.parameters():
c = 1
for i in range(p.dim()):
c *= p.size(i)
count += c
return count
def collect_match(input):
"""change the model output to the match matrix"""
image_size = input.size(0)
text_size = input.size(1)
# match_v = torch.zeros(image_size, text_size, 1)
# match_v = match_v.view(image_size*text_size, 1)
input_ = nn.LogSoftmax(2)(input)
output = torch.index_select(input_, 2, Variable(torch.LongTensor([1])).cuda())
return output
def collect_neg(input):
""""collect the hard negative sample"""
if input.dim() != 2:
return ValueError
batch_size = input.size(0)
mask = Variable(torch.eye(batch_size)>0.5).cuda()
output = input.masked_fill_(mask, 0)
output_r = output.max(1)[0]
output_c = output.max(0)[0]
loss_n = torch.mean(output_r) + torch.mean(output_c)
return loss_n
def calcul_loss(scores, size, margin, max_violation=False):
diagonal = scores.diag().view(size, 1)
d1 = diagonal.expand_as(scores)
d2 = diagonal.t().expand_as(scores)
# compare every diagonal score to scores in its column
# caption retrieval
cost_s = (margin + scores - d1).clamp(min=0)
# compare every diagonal score to scores in its row
# image retrieval
cost_im = (margin + scores - d2).clamp(min=0)
mask = torch.eye(scores.size(0)) > .5
I = Variable(mask)
if torch.cuda.is_available():
I = I.cuda()
cost_s = cost_s.masked_fill_(I, 0)
cost_im = cost_im.masked_fill_(I, 0)
if max_violation:
cost_s = cost_s.max(1)[0]
cost_im = cost_im.max(0)[0]
return cost_s.sum() + cost_im.sum()
def acc_train(input):
predicted = input.squeeze().numpy()
batch_size = predicted.shape[0]
predicted[predicted > math.log(0.5)] = 1
predicted[predicted < math.log(0.5)] = 0
target = np.eye(batch_size)
recall = np.sum(predicted * target) / np.sum(target)
precision = np.sum(predicted * target) / np.sum(predicted)
acc = 1 - np.sum(abs(predicted - target)) / (target.shape[0] * target.shape[1])
return acc, recall, precision
def acc_i2t(input):
"""Computes the precision@k for the specified values of k of i2t"""
#input = collect_match(input).numpy()
image_size = input.shape[0]
ranks = np.zeros(image_size)
# ranks_ = np.zeros(image_size//5)
top1 = np.zeros(image_size)
for index in range(image_size):
inds = np.argsort(input[index])[::-1]
# Score
rank = 1e20
# index_ = index // 5
for i in range(5 * index, 5 * index + 5, 1):
tmp = np.where(inds == i)[0][0]
if tmp < rank:
rank = tmp
if rank == 1e20:
print('error')
ranks[index] = rank
top1[index] = inds[0]
# Compute metrics
r1 = 100.0 * len(np.where(ranks < 1)[0]) / len(ranks)
r5 = 100.0 * len(np.where(ranks < 5)[0]) / len(ranks)
r10 = 100.0 * len(np.where(ranks < 10)[0]) / len(ranks)
medr = np.floor(np.median(ranks)) + 1
meanr = ranks.mean() + 1
return (r1, r5, r10, medr, meanr), (ranks, top1)
def acc_t2i(input):
"""Computes the precision@k for the specified values of k of t2i"""
#input = collect_match(input).numpy()
image_size = input.shape[0]
ranks = np.zeros(5*image_size)
top1 = np.zeros(5*image_size)
# ranks_ = np.zeros(image_size // 5)
# --> (5N(caption), N(image))
input = input.T
for index in range(image_size):
for i in range(5):
inds = np.argsort(input[5 * index + i])[::-1]
ranks[5 * index + i] = np.where(inds == index)[0][0]
top1[5 * index + i] = inds[0]
# Compute metrics
r1 = 100.0 * len(np.where(ranks < 1)[0]) / len(ranks)
r5 = 100.0 * len(np.where(ranks < 5)[0]) / len(ranks)
r10 = 100.0 * len(np.where(ranks < 10)[0]) / len(ranks)
medr = np.floor(np.median(ranks)) + 1
meanr = ranks.mean() + 1
return (r1, r5, r10, medr, meanr), (ranks, top1)
def shard_dis(images, captions, model, shard_size=128, lengths=None):
"""compute image-caption pairwise distance during validation and test"""
n_im_shard = (len(images) - 1) // shard_size + 1
n_cap_shard = (len(captions) - 1) // shard_size + 1
d = np.zeros((len(images), len(captions)))
for i in range(n_im_shard):
im_start, im_end = shard_size*i, min(shard_size*(i+1), len(images))
# print("======================")
# print("im_start:",im_start)
# print("im_end:",im_end)
for j in range(n_cap_shard):
sys.stdout.write('\r>> shard_distance batch (%d,%d)' % (i,j))
cap_start, cap_end = shard_size * j, min(shard_size * (j + 1), len(captions))
im = Variable(torch.from_numpy(images[im_start:im_end]), volatile=True).float().cuda()
s = Variable(torch.from_numpy(captions[cap_start:cap_end]), volatile=True).cuda()
l = lengths[cap_start:cap_end]
sim = model(im, s,l)
sim = sim.squeeze()
d[im_start:im_end, cap_start:cap_end] = sim.data.cpu().numpy()
sys.stdout.write('\n')
return d
def acc_i2t2(input):
"""Computes the precision@k for the specified values of k of i2t"""
#input = collect_match(input).numpy()
image_size = input.shape[0]
ranks = np.zeros(image_size)
top1 = np.zeros(image_size)
for index in range(image_size):
inds = np.argsort(input[index])[::-1]
# Score
rank = 1e20
for i in range(5 * index, 5 * index + 5, 1):
tmp = np.where(inds == i)[0][0]
if tmp < rank:
rank = tmp
ranks[index] = rank
top1[index] = inds[0]
# Compute metrics
r1 = 100.0 * len(np.where(ranks < 1)[0]) / len(ranks)
r5 = 100.0 * len(np.where(ranks < 5)[0]) / len(ranks)
r10 = 100.0 * len(np.where(ranks < 10)[0]) / len(ranks)
medr = np.floor(np.median(ranks)) + 1
meanr = ranks.mean() + 1
return (r1, r5, r10, medr, meanr), (ranks, top1)
def acc_t2i2(input):
"""Computes the precision@k for the specified values of k of t2i"""
#input = collect_match(input).numpy()
image_size = input.shape[0]
ranks = np.zeros(5*image_size)
top1 = np.zeros(5*image_size)
# --> (5N(caption), N(image))
input = input.T
for index in range(image_size):
for i in range(5):
inds = np.argsort(input[5 * index + i])[::-1]
ranks[5 * index + i] = np.where(inds == index)[0][0]
top1[5 * index + i] = inds[0]
# Compute metrics
r1 = 100.0 * len(np.where(ranks < 1)[0]) / len(ranks)
r5 = 100.0 * len(np.where(ranks < 5)[0]) / len(ranks)
r10 = 100.0 * len(np.where(ranks < 10)[0]) / len(ranks)
medr = np.floor(np.median(ranks)) + 1
meanr = ranks.mean() + 1
return (r1, r5, r10, medr, meanr), (ranks, top1)
def shard_dis_reg(images, captions, model, shard_size=128, lengths=None):
"""compute image-caption pairwise distance during validation and test"""
n_im_shard = (len(images) - 1) // shard_size + 1
n_cap_shard = (len(captions) - 1) // shard_size + 1
d = np.zeros((len(images), len(captions)))
for i in range(len(images)):
# im_start, im_end = shard_size*i, min(shard_size*(i+1), len(images))
im_index = i
for j in range(n_cap_shard):
sys.stdout.write('\r>> shard_distance batch (%d,%d)' % (i,j))
cap_start, cap_end = shard_size * j, min(shard_size * (j + 1), len(captions))
s = Variable(torch.from_numpy(captions[cap_start:cap_end]), volatile=True).cuda()
im = Variable(torch.from_numpy(images[i]), volatile=True).float().unsqueeze(0).expand(len(s), 3, 256, 256).cuda()
l = lengths[cap_start:cap_end]
sim = model(im, s, l)[:, 1]
sim = sim.squeeze()
d[i, cap_start:cap_end] = sim.data.cpu().numpy()
sys.stdout.write('\n')
return d
def shard_dis_GaLR(images, input_local_rep, input_local_adj, captions, model, shard_size=128, lengths=None):
"""compute image-caption pairwise distance during validation and test"""
n_im_shard = (len(images) - 1) // shard_size + 1
n_cap_shard = (len(captions) - 1) // shard_size + 1
d = np.zeros((len(images), len(captions)))
all = []
for i in range(n_im_shard):
im_start, im_end = shard_size*i, min(shard_size*(i+1), len(images))
print("======================")
print("im_start:",im_start)
print("im_end:",im_end)
for j in range(n_cap_shard):
sys.stdout.write('\r>> shard_distance batch (%d,%d)' % (i,j))
cap_start, cap_end = shard_size * j, min(shard_size * (j + 1), len(captions))
im = Variable(torch.from_numpy(images[im_start:im_end]), volatile=True).float().cuda()
local_rep = Variable(torch.from_numpy(input_local_rep[im_start:im_end]), volatile=True).float().cuda()
local_adj = Variable(torch.from_numpy(input_local_adj[im_start:im_end]), volatile=True).float().cuda()
s = Variable(torch.from_numpy(captions[cap_start:cap_end]), volatile=True).cuda()
l = lengths[cap_start:cap_end]
t1 = time.time()
sim = model(im, local_rep, local_adj, s, l)
t2 = time.time()
all.append(t2-t1)
sim = sim.squeeze()
d[im_start:im_end, cap_start:cap_end] = sim.data.cpu().numpy()
sys.stdout.write('\n')
print("infer time:",np.average(all))
return d
def save_checkpoint(state, is_best, filename, prefix='', model_name = None):
tries = 15
error = None
# deal with unstable I/O. Usually not necessary.
while tries:
try:
# torch.save(state, prefix + filename)
if is_best:
torch.save(state, prefix +model_name +'_best.pth.tar')
except IOError as e:
error = e
tries -= 1
else:
break
print('model save {} failed, remaining {} trials'.format(filename, tries))
if not tries:
raise error
def adjust_learning_rate(options, optimizer, epoch):
"""Sets the learning rate to the initial LR
decayed by 10 every 30 epochs"""
for param_group in optimizer.param_groups:
lr = param_group['lr']
if epoch % options['optim']['lr_update_epoch'] == options['optim']['lr_update_epoch'] - 1:
lr = lr * options['optim']['lr_decay_param']
param_group['lr'] = lr
print("Current lr: {}".format(optimizer.state_dict()['param_groups'][0]['lr']))
def load_from_txt(filename, encoding="utf-8"):
f = open(filename,'r' ,encoding=encoding)
contexts = f.readlines()
return contexts