forked from Wangt-CN/MTFN-RR-PyTorch-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrerank.py
147 lines (112 loc) · 4.45 KB
/
rerank.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
# -----------------------------------------------------------
# "Matching Images and Text with Multi-modal Tensor Fusion and Re-ranking"
# WangTan, XingXu, YangYang, Alan Hanjalic, HengtaoShen, JingkuanSong
# ACM Multimedia 2019, Nice, France
# Writen by WangTan, 2019
# ------------------------------------------------------------
# The core cross-modal re-ranking module without supervision
# and can be added into other image-text matching methods
import numpy as np
import utils
def i2t_rerank(sim, K1, K2):
size_i = sim.shape[0]
size_t = sim.shape[1]
sort_i2t = np.argsort(-sim, 1)
sort_t2i = np.argsort(-sim, 0)
sort_i2t_re = np.copy(sort_i2t)[:, :K1]
address = np.array([])
for i in range(size_i):
for j in range(K1):
result_t = sort_i2t[i][j]
query = sort_t2i[:, result_t]
# query = sort_t2i[:K2, result_t]
address = np.append(address, np.where(query == i)[0][0])
sort = np.argsort(address)
sort_i2t_re[i] = sort_i2t_re[i][sort]
address = np.array([])
sort_i2t[:,:K1] = sort_i2t_re
return sort_i2t
def t2i_rerank(sim, K1, K2):
size_i = sim.shape[0]
size_t = sim.shape[1]
sort_i2t = np.argsort(-sim, 1)
sort_t2i = np.argsort(-sim, 0)
sort_t2i_re = np.copy(sort_t2i)[:K1, :]
address = np.array([])
for i in range(size_t):
for j in range(K1):
result_i = sort_t2i[j][i]
query = sort_i2t[result_i, :]
# query = sort_t2i[:K2, result_t]
ranks = 1e20
for k in range(5):
tmp = np.where(query == i//5 * 5 + k)[0][0]
if tmp < ranks:
ranks = tmp
address = np.append(address, ranks)
sort = np.argsort(address)
sort_t2i_re[:, i] = sort_t2i_re[:, i][sort]
address = np.array([])
sort_t2i[:K1, :] = sort_t2i_re
return sort_t2i
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 = input[index]
# Score
if index == 197:
print('s')
rank = 1e20
for i in range(5 * index, min(5 * index + 5, image_size*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 = input[5 * index + i]
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)
# The accuracy computing
# Input the prediction similarity score matrix (d * 5d)
d = np.load('flickr1_stage1_d.npy')
# calculate the i2t score after rerank
sort_rerank = i2t_rerank(d, 15, 1)
(r1i, r5i, r10i, medri, meanri), _ = acc_i2t2(np.argsort(-d, 1))
(r1i2, r5i2, r10i2, medri2, meanri2), _ = acc_i2t2(sort_rerank)
print(r1i, r5i, r10i, medri, meanri)
print(r1i2, r5i2, r10i2, medri2, meanri2)
# calculate the t2i score after rerank
# sort_rerank = t2i_rerank(d, 20, 1)
# (r1t, r5t, r10t, medrt, meanrt), _ = acc_t2i2(np.argsort(-d, 0))
# (r1t2, r5t2, r10t2, medrt2, meanrt2), _ = acc_t2i2(sort_rerank)
#
# print(r1t, r5t, r10t, medrt, meanrt)
# print(r1t2, r5t2, r10t2, medrt2, meanrt2)