-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetric.py
438 lines (385 loc) · 14.4 KB
/
metric.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
437
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Function
import torchvision.datasets as dsets
from torchvision import transforms
from torch.autograd import Variable
import torchvision
import math
import numpy as np
from tools import build_G_from_S, cal_sim
import settings
def compress_ab(train_loader, test_loader, model_I, model_T, train_dataset, test_dataset):
re_BI = list([])
re_BT = list([])
re_L = list([])
for _, (data_I, data_T, _, _) in enumerate(train_loader):
with torch.no_grad():
var_data_I = Variable(data_I.cuda())
_,_,code_I = model_I(var_data_I)
code_I = torch.sign(code_I)
re_BI.extend(code_I.cpu().data.numpy())
var_data_T = Variable(torch.FloatTensor(data_T.numpy()).cuda())
_,_,code_T = model_T(var_data_T)
code_T = torch.sign(code_T)
re_BT.extend(code_T.cpu().data.numpy())
qu_BI = list([])
qu_BT = list([])
qu_L = list([])
for _, (data_I, data_T, _, _) in enumerate(test_loader):
with torch.no_grad():
var_data_I = Variable(data_I.cuda())
_,_,code_I = model_I(var_data_I)
code_I = torch.sign(code_I)
qu_BI.extend(code_I.cpu().data.numpy())
var_data_T = Variable(torch.FloatTensor(data_T.numpy()).cuda())
_,_,code_T = model_T(var_data_T)
code_T = torch.sign(code_T)
qu_BT.extend(code_T.cpu().data.numpy())
re_BI = np.array(re_BI)
re_BT = np.array(re_BT)
re_L = train_dataset.train_labels
qu_BI = np.array(qu_BI)
qu_BT = np.array(qu_BT)
qu_L = test_dataset.train_labels
return re_BI, re_BT, re_L, qu_BI, qu_BT, qu_L
def compress_wiki(train_loader, test_loader, modeli, modelt, train_dataset, test_dataset, classes=10):
re_BI = list([])
re_BT = list([])
re_L = list([])
for _, (data_I, data_T, target, _) in enumerate(train_loader):
with torch.no_grad():
var_data_I = Variable(data_I.cuda())
_,_,code_I,_ = modeli(var_data_I)
code_I = torch.sign(code_I)
re_BI.extend(code_I.cpu().data.numpy())
re_L.extend(target)
var_data_T = Variable(torch.FloatTensor(data_T.numpy()).cuda())
_,_,code_T,_ = modelt(var_data_T)
code_T = torch.sign(code_T)
re_BT.extend(code_T.cpu().data.numpy())
qu_BI = list([])
qu_BT = list([])
qu_L = list([])
for _, (data_I, data_T, target, _) in enumerate(test_loader):
with torch.no_grad():
var_data_I = Variable(data_I.cuda())
_,_,code_I,_ = modeli(var_data_I)
code_I = torch.sign(code_I)
qu_BI.extend(code_I.cpu().data.numpy())
qu_L.extend(target)
var_data_T = Variable(torch.FloatTensor(data_T.numpy()).cuda())
_,_,code_T,_ = modelt(var_data_T)
code_T = torch.sign(code_T)
qu_BT.extend(code_T.cpu().data.numpy())
re_BI = np.array(re_BI)
re_BT = np.array(re_BT)
re_L = np.eye(classes)[np.array(re_L)]
qu_BI = np.array(qu_BI)
qu_BT = np.array(qu_BT)
qu_L = np.eye(classes)[np.array(qu_L)]
return re_BI, re_BT, re_L, qu_BI, qu_BT, qu_L
def compress_nus(train_loader, test_loader, model_I, model_T, train_dataset, test_dataset, G_test):
re_BI = list([])
re_BT = list([])
re_L = list([])
for _, (data_I, data_T, target, batch_ind) in enumerate(train_loader):
data_I = Variable(data_I.cuda())
F_T = Variable(torch.FloatTensor(data_T.numpy()).cuda())
feature_I, _, _= model_I(data_I)
F_I = F.normalize(feature_I)
F_T = F.normalize(F_T)
S_I = torch.mm(F_I, F_I.t())
S_T = torch.mm(F_T, F_T.t())
S_high_crs = F.normalize(S_I).mm(F.normalize(S_T).t())
if settings.DATASET == "MSCOCO":
sim1_database = 0.4 * S_I + 0.2 * S_T + 0.4 * (S_high_crs + S_high_crs.t()) / 2
sim1_database = sim1_database * 1.4
# sim1_database = cal_sim(S_I=S_I, S_T=S_T, S_F=S_high_crs)* 1.4
# elif settings.DATASET == "NUSWIDE":
else:
sim1_database = 0.4 * S_I + 0.3 * S_T + 0.3 * (S_high_crs + S_high_crs.t()) / 2
sim1_database = sim1_database * 1.4
# else:
# sim1_database = 0.4 * S_I + 0.3 * S_T + 0.3 * (S_high_crs + S_high_crs.t()) / 2
# sim1_database = sim1_database * 1.4
S = sim1_database
# H = load_feature_construct_H(S)
# G = generate_G_from_H(H)
# G = torch.tensor(G, dtype=torch.float).cuda()
G = build_G_from_S(S, settings.K)
with torch.no_grad():
var_data_I = Variable(data_I.cuda())
_,_,code_I = model_I(var_data_I, G)
code_I = torch.sign(code_I)
re_BI.extend(code_I.cpu().data.numpy())
var_data_T = Variable(torch.FloatTensor(data_T.numpy()).cuda())
_,_,code_T = model_T(var_data_T, G)
code_T = torch.sign(code_T)
re_BT.extend(code_T.cpu().data.numpy())
qu_BI = list([])
qu_BT = list([])
qu_L = list([])
for _, (data_I, data_T, _, batch_ind) in enumerate(test_loader):
_S = G_test[batch_ind, :]
S = _S[:, batch_ind]
# H = load_feature_construct_H(S)
# G = generate_G_from_H(H)
# G = torch.tensor(G, dtype=torch.float).cuda()
G = build_G_from_S(S, settings.K)
with torch.no_grad():
var_data_I = Variable(data_I.cuda())
_,_,code_I = model_I(var_data_I, G)
code_I = torch.sign(code_I)
qu_BI.extend(code_I.cpu().data.numpy())
var_data_T = Variable(torch.FloatTensor(data_T.numpy()).cuda())
_,_,code_T = model_T(var_data_T, G)
code_T = torch.sign(code_T)
qu_BT.extend(code_T.cpu().data.numpy())
re_BI = np.array(re_BI)
re_BT = np.array(re_BT)
re_L = train_dataset.train_labels
qu_BI = np.array(qu_BI)
qu_BT = np.array(qu_BT)
qu_L = test_dataset.train_labels
return re_BI, re_BT, re_L, qu_BI, qu_BT, qu_L
def compress(train_loader, test_loader, model_I, model_T, train_dataset, test_dataset, G_train, G_test):
re_BI = list([])
re_BT = list([])
re_L = list([])
re_L_wiki = list([])
for _, (data_I, data_T, target, batch_ind) in enumerate(train_loader):
S_ = G_train[batch_ind, :]
S = S_[:, batch_ind]
# H = load_feature_construct_H(S)
# G = generate_G_from_H(H)
# G = torch.tensor(G, dtype=torch.float).cuda()
G = build_G_from_S(S, settings.K)
with torch.no_grad():
var_data_I = Variable(data_I.cuda())
_,_,code_I = model_I(var_data_I, G)
code_I = torch.sign(code_I)
re_BI.extend(code_I.cpu().data.numpy())
var_data_T = Variable(torch.FloatTensor(data_T.numpy()).cuda())
_,_,code_T = model_T(var_data_T, G)
code_T = torch.sign(code_T)
re_BT.extend(code_T.cpu().data.numpy())
qu_BI = list([])
qu_BT = list([])
qu_L = list([])
for _, (data_I, data_T, target, batch_ind) in enumerate(test_loader):
_S = G_test[batch_ind, :]
S = _S[:, batch_ind]
# H = load_feature_construct_H(S)
# G = generate_G_from_H(H)
# G = torch.tensor(G, dtype=torch.float).cuda()
G = build_G_from_S(S, settings.K)
with torch.no_grad():
var_data_I = Variable(data_I.cuda())
_,_,code_I = model_I(var_data_I, G)
code_I = torch.sign(code_I)
qu_BI.extend(code_I.cpu().data.numpy())
var_data_T = Variable(torch.FloatTensor(data_T.numpy()).cuda())
_,_,code_T = model_T(var_data_T, G)
code_T = torch.sign(code_T)
qu_BT.extend(code_T.cpu().data.numpy())
re_BI = np.array(re_BI)
re_BT = np.array(re_BT)
re_L = train_dataset.train_labels
qu_L = test_dataset.train_labels
qu_BI = np.array(qu_BI)
qu_BT = np.array(qu_BT)
return re_BI, re_BT, re_L, qu_BI, qu_BT, qu_L
def calculate_hamming(B1, B2):
"""
:param B1: vector [n]
:param B2: vector [r*n]
:return: hamming distance [r]
"""
leng = B2.shape[1] # max inner product value
distH = 0.5 * (leng - np.dot(B1, B2.transpose()))
return distH
def calculate_map(qu_B, re_B, qu_L, re_L):
"""
:param qu_B: {-1,+1}^{mxq} query bits
:param re_B: {-1,+1}^{nxq} retrieval bits
:param qu_L: {0,1}^{mxl} query label
:param re_L: {0,1}^{nxl} retrieval label
:return:
"""
num_query = qu_L.shape[0]
map = 0
for iter in range(num_query):
gnd = (np.dot(qu_L[iter, :], re_L.transpose()) > 0).astype(np.float32)
tsum = np.sum(gnd).astype(int)
if tsum == 0:
continue
hamm = calculate_hamming(qu_B[iter, :], re_B)
ind = np.argsort(hamm)
gnd = gnd[ind]
count = np.linspace(1, tsum, tsum) # [1,2, tsum]
tindex = np.asarray(np.where(gnd == 1)) + 1.0
map_ = np.mean(count / (tindex))
map = map + map_
map = map / num_query
return map
def calculate_top_map(qu_B, re_B, qu_L, re_L, topk):
"""
:param qu_B: {-1,+1}^{mxq} query bits
:param re_B: {-1,+1}^{nxq} retrieval bits
:param qu_L: {0,1}^{mxl} query label
:param re_L: {0,1}^{nxl} retrieval label
:param topk:
:return:
"""
num_query = qu_L.shape[0]
topkmap = 0
for iter in range(num_query):
gnd = (np.dot(qu_L[iter, :], re_L.transpose()) > 0).astype(np.float32)
hamm = calculate_hamming(qu_B[iter, :], re_B)
ind = np.argsort(hamm)
gnd = gnd[ind]
tgnd = gnd[0:topk]
tsum = np.sum(tgnd).astype(int)
if tsum == 0:
continue
count = np.linspace(1, tsum, tsum)
tindex = np.asarray(np.where(tgnd == 1)) + 1.0
topkmap_ = np.mean(count / (tindex))
topkmap = topkmap + topkmap_
topkmap = topkmap / num_query
return topkmap
def construct_H_with_KNN(X, K_neigs=[10], split_diff_scale=False, is_probH=True, m_prob=1):
"""
init multi-scale hypergraph Vertex-Edge matrix from original node feature matrix
:param X: N_object x feature_number
:param K_neigs: the number of neighbor expansion
:param split_diff_scale: whether split hyperedge group at different neighbor scale
:param is_probH: prob Vertex-Edge matrix or binary
:param m_prob: prob
:return: N_object x N_hyperedge
"""
if len(X.shape) != 2:
X = X.reshape(-1, X.shape[-1])
if type(K_neigs) == int:
K_neigs = [K_neigs]
dis_mat = Eu_dis(X)
H = []
for k_neig in K_neigs:
H_tmp = construct_H_with_KNN_from_distance(dis_mat, k_neig, is_probH, m_prob)
if not split_diff_scale:
H = hyperedge_concat(H, H_tmp)
else:
H.append(H_tmp)
return H
def hyperedge_concat(*H_list):
"""
Concatenate hyperedge group in H_list
:param H_list: Hyperedge groups which contain two or more hypergraph incidence matrix
:return: Fused hypergraph incidence matrix
"""
H = None
for h in H_list:
if h is not None and h != []:
# for the first H appended to fused hypergraph incidence matrix
if H is None:
H = h
else:
if type(h) != list:
H = np.hstack((H, h))
else:
tmp = []
for a, b in zip(H, h):
tmp.append(np.hstack((a, b)))
H = tmp
return H
def load_feature_construct_H(feature, m_prob=1, K_neigs=[10], is_probH=True, split_diff_scale=False):
H = None
tmp = construct_H_with_KNN(feature, K_neigs=K_neigs, split_diff_scale=split_diff_scale, is_probH=is_probH, m_prob=m_prob)
H = hyperedge_concat(H, tmp)
return H
def Eu_dis(x):
"""
Calculate the distance among each raw of x
:param x: N X D
N: the object number
D: Dimension of the feature
:return: N X N distance matrix
"""
x = np.mat(x.cpu())
aa = np.sum(np.multiply(x, x), 1)
ab = x * x.T
dist_mat = aa + aa.T - 2 * ab
dist_mat[dist_mat < 0] = 0
dist_mat = np.sqrt(dist_mat)
dist_mat = np.maximum(dist_mat, dist_mat.T)
return dist_mat
def construct_H_with_KNN_from_distance(dis_mat, k_neig, is_probH=True, m_prob=1):
"""
construct hypregraph incidence matrix from hypergraph node distance matrix
:param dis_mat: node distance matrix
:param k_neig: K nearest neighbor
:param is_probH: prob Vertex-Edge matrix or binary
:param m_prob: prob
:return: N_object X N_hyperedge
"""
# dis_mat = dis_mat.cpu()
n_obj = dis_mat.shape[0]
# construct hyperedge from the central feature space of each node
n_edge = n_obj
H = np.zeros((n_obj, n_edge))
for center_idx in range(n_obj):
dis_mat[center_idx, center_idx] = 0
dis_vec = dis_mat[center_idx]
nearest_idx = np.array(np.argsort(dis_vec)).squeeze()
avg_dis = np.average(dis_vec)
if not np.any(nearest_idx[:k_neig] == center_idx):
nearest_idx[k_neig - 1] = center_idx
for node_idx in nearest_idx[:k_neig]:
if is_probH:
H[node_idx, center_idx] = np.exp(-dis_vec[0, node_idx] ** 2 / (m_prob * avg_dis) ** 2)
else:
H[node_idx, center_idx] = 1.0
return H
def generate_G_from_H(H, variable_weight=False):
"""
calculate G from hypgraph incidence matrix H
:param H: hypergraph incidence matrix H
:param variable_weight: whether the weight of hyperedge is variable
:return: G
"""
if type(H) != list:
return _generate_G_from_H(H, variable_weight)
else:
G = []
for sub_H in H:
G.append(generate_G_from_H(sub_H, variable_weight))
return G
def _generate_G_from_H(H, variable_weight=False):
"""
calculate G from hypgraph incidence matrix H
:param H: hypergraph incidence matrix H
:param variable_weight: whether the weight of hyperedge is variable
:return: G
"""
H = np.array(H)
n_edge = H.shape[1]
# the weight of the hyperedge
W = np.ones(n_edge)
# the degree of the node
DV = np.sum(H * W, axis=1)
# the degree of the hyperedge
DE = np.sum(H, axis=0)
invDE = np.mat(np.diag(np.power(DE, -1)))
DV2 = np.mat(np.diag(np.power(DV, -0.5)))
W = np.mat(np.diag(W))
H = np.mat(H)
HT = H.T
if variable_weight:
DV2_H = DV2 * H
invDE_HT_DV2 = invDE * HT * DV2
return DV2_H, W, invDE_HT_DV2
else:
G = DV2 * H * W * invDE * HT * DV2
return G