-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathevaluate.py
75 lines (63 loc) · 1.56 KB
/
evaluate.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
import math
import tensorflow as tf
import prettytensor as pt
import numpy as np
from deconv import deconv2d
import IPython.display
import matplotlib.pyplot as plt
import ipywidgets as widgets
from ipywidgets import interact, interactive, fixed
import skimage
import skimage.io
import skimage.transform
import tqdm # making loops prettier
import h5py
import scipy
import cv2
import scipy.io as sio
def cal_map(rel,K=None):
shot = 0.
ans = 0.
if K is not None:
rel= rel[:K]
pos = 1.
for i in rel:
shot += i
ans +=(shot/pos)*i
pos += 1.0
return ans / shot
#binary data dataset
feat = np.load('codes/32_00020_beta.npz')
mat = sio.loadmat('cifar-10.mat')
dataset = np.sign(feat['dataset'])
dataset_L = mat['dataset_L']
test_L = mat['test_L']
test = np.sign(feat['test'])
'''
nms= 'codes/48_3_00030'#"SphericalH_12_resnet_cifar"
data = np.load(nms+'.npz')
dataset=data['B_dataset']
dataset_L=data['dataset_L']
test_L=data['test_L']
test = data['B_test']
'''
print dataset.shape,test.shape,dataset_L.shape,test_L.shape,'DSDD'
s = 0.
for i in xrange(len(test_L)):
query = test[i]
sim=[]
query_L=test_L[i]
for j in xrange(len(dataset)):
a = np.sum(query*dataset[j]) / (np.sqrt(np.sum(query**2))*np.sqrt(np.sum(dataset[j]**2)))
sim.append(a)
sim = np.array(sim)
indx = np.argsort(-sim)
rel=[]
for j in indx:
if query_L==dataset_L[j]:
rel.append(1)
else:
rel.append(0)
mp1 = cal_map(rel,len(dataset))
s +=mp1
print i,'ap=',mp1,"Map=",s/(i+1.)