-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathevaluate.py
More file actions
53 lines (46 loc) · 1.66 KB
/
evaluate.py
File metadata and controls
53 lines (46 loc) · 1.66 KB
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
import numpy as np
import scipy
import scipy.spatial
def fx_calc_map_label(image, text, label, k=0, dist_method='COS'):
if dist_method == 'L2':
dist = scipy.spatial.distance.cdist(image, text, 'euclidean')
elif dist_method == 'COS':
dist = scipy.spatial.distance.cdist(image, text, 'cosine')
ord = dist.argsort()
numcases = dist.shape[0]
sim = (np.dot(label, label.T) > 0).astype(float)
tindex = np.arange(numcases, dtype=float) + 1
if k == 0:
k = numcases
res = []
for i in range(numcases):
order = ord[i]
sim[i] = sim[i][order]
num = sim[i].sum()
a = np.where(sim[i]==1)[0]
sim[i][a] = np.arange(a.shape[0], dtype=float) + 1
res += [(sim[i] / tindex).sum() / num]
return np.mean(res)
def fx_calc_map_label_per_class(image, text, label, dist_method='COS'):
if dist_method == 'L2':
dist = scipy.spatial.distance.cdist(image, text, 'euclidean')
elif dist_method == 'COS':
dist = scipy.spatial.distance.cdist(image, text, 'cosine')
ord = dist.argsort()
numcases = dist.shape[0]
sim = (np.dot(label, label.T) > 0).astype(float)
tindex = np.arange(numcases, dtype=float) + 1
res = []
for i in range(numcases):
order = ord[i]
sim[i] = sim[i][order]
num = sim[i].sum()
a = np.where(sim[i]==1)[0]
sim[i][a] = np.arange(a.shape[0], dtype=float) + 1
res += [(sim[i] / tindex).sum() / num]
categories = label.shape[1]
per_class = np.zeros([categories], dtype=np.float)
for i in range(numcases):
per_class += res[i] * label[i]
per_class /= label.sum(0)
return per_class