-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathevaluate_vistex.py
108 lines (77 loc) · 2.4 KB
/
evaluate_vistex.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
import re
import pandas as pd
import numpy as np
from irma_reader import *
from auc_ap import auc
import os
from autoencoder import autoencoder
from co_occurence import *
def euclidean(x,y):
return np.sqrt(np.sum(np.square(x-y)))
vistex = get_vistex()
ipath_cache_file = os.path.join('cache', 'paths.pkl')
if os.path.isfile(ipath_cache_file):
# print('Loading image paths from : ' + ipath_cache_file)
with open(ipath_cache_file, 'rb') as f:
ipath = cPickle.load(f)
def vistex_query():
idx = np.random.randint(0,len(vistex))
query_feature = vistex[idx]
result = []
for i,feature in enumerate(vistex):
dis = euclidean(feature,query_feature)
result.append((dis,ipath[i][:-4]))
result = sorted(result)
# print result
return result,ipath[idx][:-4]
def evaluate(num_queries=10):
class_info = pd.read_csv("ImageCLEFmed2009_train_codes.02.csv")
class_i = np.array(class_info["05_class"])
print class_i.shape
class_count = {}
for c in class_i:
if not class_count.get(c):
class_count[c] = 1
else:
class_count[c] += 1
# print class_count
get_class = {}
path = "dataset/ImageCLEFmed2009_train.02/"
for img_id,img_class in zip(class_info["image_id"],class_info["05_class"]):
get_class[path+str(img_id)] = img_class
# print get_class
# model = ae_model()
mAP = 0.0
count = 0
for i in range(num_queries):
result,query = vistex_query()
try:
query_class = get_class[query]
except:
i = i-1
continue
count+=1
print 'query class ',query_class
print 'total relavant: ', class_count[query_class]
simplified_result = []
for x in result:
# print x[1], get_class[x[1]]
try:
if(get_class[x[1]]==query_class):
simplified_result.append(1)
else:
simplified_result.append(0)
except:
continue
# print simplified_result
AP = auc(simplified_result,class_count[query_class])
print 'query ', i, ' AP : ', AP
mAP+=AP
mAP /= count
print 'mAP : ', mAP
return mAP
if __name__ == '__main__':
# os.environ['CUDA_VISIBLE_DEVICES'] = ''
# model = ae_model()
# print model.query()
evaluate(100)