-
Notifications
You must be signed in to change notification settings - Fork 3
/
vlad.py
201 lines (143 loc) · 4.92 KB
/
vlad.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
import sys
import argparse
import re
import os
from numpy import array, zeros, mean, std, sort, add, subtract, divide, dot, sqrt, arange, random
from numpy import linalg as la
from scipy.cluster.vq import vq, kmeans, whiten
def list_files(dirname):
photos = {}
filelist = os.listdir(dirname)
for filename in filelist:
file_id = ''
m = re.search('(.*?)\..*', filename)
if (m != None):
file_id = m.group(1)
#print("Reading " + filename)
f = open(dirname + '/' + filename, 'r')
content = []
for line in f:
vec = map(float, line.split())
content.append(vec)
content = array(content).flatten()
photos[file_id] = content
f.close()
return photos
def load_clustering(filename):
f = open(filename, 'r')
clusters = []
cur_cluster = {}
for line in f:
m = re.search('\"x\"', line)
if m != None:
if cur_cluster != {}:
clusters.append(cur_cluster)
cur_cluster = {}
m = re.search('\"(.*?)\" .*', line)
if m != None:
if "exemplar" in cur_cluster:
if "member" not in cur_cluster:
cur_cluster["member"] = []
cur_cluster["member"].append(m.group(1))
else:
cur_cluster["exemplar"] = m.group(1)
cur_cluster["member"] = []
if cur_cluster != {}:
clusters.append(cur_cluster)
f.close()
return clusters
def load_list(filename):
f = open(filename, 'r')
content = []
for line in f:
content.append(line.rstrip())
f.close()
return array(content)
def do_query(photos, query):
distance = {}
for query_photo in query:
if query_photo in photos:
print("Querying with " + query_photo)
query_feature = photos[query_photo]
distance_for_query = []
for photo_id, feature in photos.iteritems():
if photo_id != query_photo:
dist = la.norm(subtract(query_feature, feature))
distance_for_query.append((photo_id, dist))
distance_for_query = array(distance_for_query, dtype = [('id', 'S20'), ('distance', float)])
distance_for_query.sort(order = 'distance')
distance[query_photo] = distance_for_query
return distance
def validate(distance, query, groundtruth):
ap = {}
map_value = 0.0
for query_photo in query:
dist_vec = distance[query_photo]
count = 1.0
hit = 0.0
query_ap = 0.0
for (photo_id, dist) in dist_vec:
if count > 10:
break
if photo_id in groundtruth:
hit = hit + 1
query_ap += (hit / count)
count = count + 1
if hit > 0:
query_ap = query_ap / hit
ap[query_photo] = query_ap
map_value += query_ap
map_value /= query.size
return (ap, map_value)
def random_sample_photos(photos, num):
photo_ids = photos.keys()
random.shuffle(photo_ids)
part = len(photo_ids) / num
samples = []
for idx in range(num):
start_idx = idx * part
end_idx = (idx + 1) * part
if idx == num - 1:
end_idx = len(photo_ids)
photo_set = {}
for hash_key in range(start_idx, end_idx):
photo_set[photo_ids[hash_key]] = photos[photo_ids[hash_key]]
samples.append(photo_set)
return samples
def write_out_vlad_matrix(photos, filename):
f = open(filename, 'w')
for photo_id, feature in photos.iteritems():
f.write(photo_id + "\t")
feature.tofile(f, sep = " ")
f.write("\n")
f.close()
def write_out_vlad_matrix_libsvm_format(photos, filename, cluster = {}, label = ''):
f = open(filename, 'w')
for photo_id, feature in photos.iteritems():
if cluster == {} or (photo_id == cluster["exemplar"] or photo_id in cluster["member"]):
if label == '':
f.write(photo_id + " ")
elif label == 'positive' or label == 1:
f.write("1 ")
else:
f.write("-1 ")
it = feature.flat
features = ""
for idx, val in enumerate(it):
if features != "":
features = features + " "
features = features + str(idx) + ":" + str(val)
f.write(features + "\n")
f.close()
def write_out_distance(distance, filename):
f = open(filename, 'w')
for photo_from_id, dist_vec in distance.iteritems():
for (photo_to_id, dist) in dist_vec:
f.write(photo_from_id + "\t" + photo_to_id + "\t" + str(dist) + "\n")
f.close()
def write_file(vlad, filename):
f = open(filename, 'w')
for vec in vlad:
vec.tofile(f, sep = " ")
f.write("\n")
f.close()