-
Notifications
You must be signed in to change notification settings - Fork 2
/
prism.py
355 lines (270 loc) · 9.28 KB
/
prism.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
'''
Project: Prism
Author: Kain Liu
'''
from PIL import Image
from cossim import cossim
from sketch import sketch
from numpy import *
import time
import os, sys
import scipy.spatial.distance as dis
# 24k pictures in total
population = 24000
# random vector
rv_number = 256
# sample id of images
samples = [ 2158, 7418, 7757, 9824, 22039,
16336, 7463, 4595, 20159, 17348,
19166, 23112, 16678, 2084, 11398,
19557, 14867, 5437, 13122, 20811]
'''
Generate a signature based on colour information
'''
def color_sig(file, seg = 4):
print file
try:
im = Image.open(file)
print(im.format, im.size, im.mode)
except:
print "Unable to load image!"
w, h = im.size
colors = im.getcolors(w*h)
color_counter = {}
def cut(x, n=16):
return x / (256 / n)
for color in colors:
key = []
for x in color[1]:
key.append(cut(x, seg))
key = str(key)
color_counter.setdefault(key, []).append(color[0])
hash_result = []
# loop throught rgb colors
for r in range(0, seg):
for g in range(0, seg):
for b in range(0, seg):
key = str([r, g, b])
if key in color_counter:
val = sum(color_counter[key])
else:
val = 0
# optional: ignore background color which is black
'''
if r == 0 and g == 0 and b == 0:
val = 0
'''
# optional: ignore the color takes up too much weight
'''
if val > 10000:
val = 0
'''
hash_result.append(val)
return hash_result
'''
calculate which size is the best choice for bins
'''
def bin_size():
for i in (2, 4, 8, 16, 32, 64):
# compare image collections of two objects
a1 = color_sig('dataset/251_l3c1.png', i)
a2 = color_sig('dataset/251_l3c2.png', i)
a3 = color_sig('dataset/251_l3c3.png', i)
b1 = color_sig('dataset/255_l3c1.png', i)
b2 = color_sig('dataset/255_l3c2.png', i)
b3 = color_sig('dataset/255_l3c3.png', i)
# generate a latex table
print "====== i:", i, " ======"
print '& $A_1$ &',cossim_3(a1, a1), '&',cossim_3(a1, a2), '&',cossim_3(a1, a3), '&',cossim_3(a1, b1), '&',cossim_3(a1, b2), '&',cossim_3(a1, b3), '\\\\ \cline{2-8}'
print '& $A_2$ &',cossim_3(a2, a1), '&',cossim_3(a2, a2), '&',cossim_3(a2, a3), '&',cossim_3(a2, b1), '&',cossim_3(a2, b2), '&',cossim_3(a2, b3), '\\\\ \cline{2-8}'
print '& $A_3$ &',cossim_3(a3, a1), '&',cossim_3(a3, a2), '&',cossim_3(a3, a3), '&',cossim_3(a3, b1), '&',cossim_3(a3, b2), '&',cossim_3(a3, b3), '\\\\ \cline{2-8}'
print '& $B_1$ &',cossim_3(b1, a1), '&',cossim_3(b1, a2), '&',cossim_3(b1, a3), '&',cossim_3(b1, b1), '&',cossim_3(b1, b2), '&',cossim_3(b1, b3), '\\\\ \cline{2-8}'
print '& $B_2$ &',cossim_3(b2, a1), '&',cossim_3(b2, a2), '&',cossim_3(b2, a3), '&',cossim_3(b2, b1), '&',cossim_3(b2, b2), '&',cossim_3(b2, b3), '\\\\ \cline{2-8}'
print '& $B_3$ &',cossim_3(b3, a1), '&',cossim_3(b3, a2), '&',cossim_3(b3, a3), '&',cossim_3(b3, b1), '&',cossim_3(b3, b2), '&',cossim_3(b3, b3), '\\\\ \cline{2-8}'
def sig(start = 1, end = 1000):
file = open("result/sig.txt", "w")
t0 = time.clock()
for i in range(start, end + 1):
for j in range(1, 9):
for k in range(1, 4):
h = color_sig(id2path(i, j, k))
file.write(str(h).replace(",","").replace("[","").replace("]",""))
file.write("\n")
print "{0} of {1}".format(i, end - start + 1)
file.close()
print "sig.txt finish."
print time.clock() - t0, "seconds in generating signatures"
def matrix():
t0 = time.clock()
sketches = open_sketch()
# sketch has #vectors rows and #image columns
# every row is result multipied by one random vector
result = dot(sketches.transpose(), sketches)
# save result
print time.clock() - t0, "seconds in generating matrix"
m = zeros([len(samples), population])
for i in range(len(samples)):
m[i] = result[samples[i]]
savetxt('result/matrix-sample.txt', m, fmt='%i')
def cos():
sig = open_sig()
s = zeros([len(samples), population])
for i in range(len(samples)):
for j in range(0, population):
s[i][j] = cossim_3(sig[samples[i]], sig[j])
savetxt('result/similarity-sample.txt', s, fmt='%.3f')
def sketch():
t0 = time.clock()
m = open_sig()
print "signature matrix size is {0} x {1}".format(m.shape[0], m.shape[1])
sketches = sketch(m, rv_number)
print "sketch matrix size is {0} x {1}".format(sketches.shape[0], sketches.shape[1])
print time.clock() - t0, "seconds in generating sketches"
savetxt('result/sketch.txt', sketches, fmt='%d')
def similar(i, j, k):
# only calculate all pairs of given image with rest images
line = id2line(i, j, k)
sketches = open_sketch()
t0 = time.clock()
'''
def nested_loop(sketches):
h = len(sketches)
w = len(sketches[0])
_r = []
for i in range(0, w):
intersection = 0
for k in range(0, h):
if sketches[k][i] == sketches[k][line]:
intersection += 1
_r.append(round(
float(intersection) / float(w),
4
))
return _r
pre_sim = nested_loop(sketches)
'''
def transpose_dot(sketches):
result = dot(sketches.transpose()[line], sketches)
return result
pre_sim = transpose_dot(sketches)
# get top n
# argsort(line)[-n:] #last n elements
# [::-1] # reverse
n = 32
top_n = argsort(pre_sim)[-n:][::-1]
result = []
path = []
for top in top_n:
di = line2id(top)
result.append( di )
path.append( id2path(di[0],di[1],di[2]) )
print time.clock() - t0, "seconds in finding similar items"
print result
def similar_all():
def transpose_dot(_sketches, _line):
result = dot(_sketches.transpose()[_line], _sketches)
return result
# only calculate all pairs of given image with rest images
file = open("result/all-to-mongodb.txt", "w")
sketches = open_sketch()
t00 = time.clock()
for i in range(0, population):
t0 = time.clock()
pre_sim = transpose_dot(sketches, i)
# get top n
# argsort(line)[-n:] #last n elements
# [::-1] # reverse
n = 32
top_n = argsort(pre_sim)[-n:][::-1]
result = []
path = []
for top in top_n:
di = line2id(top)
result.append( di )
path.append( id2path(di[0],di[1],di[2]) )
print i, ' : ', time.clock() - t0, "s"
# print result
# Mongodb insert similar neighbors for each picture
# print(i, path)
file.write("db.similarPic.insert({{ id: {} , neighbors: {} }})".format(i, path))
file.write("\n")
print "Total {}s".format(time.clock() - t00)
file.close()
'''
loader functions
'''
def open_sig():
t0 = time.clock()
m = loadtxt("result/sig.txt")
print time.clock() - t0, "seconds in opening signatures"
return m
# def open_matrix():
# t0 = time.clock()
# m = loadtxt("('result/matrix.txt")
# print time.clock() - t0, "seconds in opening signatures"
# return m.shape
def open_sketch():
t0 = time.clock()
m = loadtxt("result/sketch.txt")
print time.clock() - t0, "seconds in opening sketches"
return m
'''
helper functions
'''
def id2path(i, j, k):
return "dataset/{0}/{0}_l{1}c{2}.png".format(i, j, k)
def id2line(i, j, k):
line = (i - 1) * 24 + (j - 1) * 3 + (k - 1)
return line
def line2id(line):
a = line / 24 + 1
b = line % 24 / 3 + 1
c = line % 24 % 3 + 1
return a, b, c
def cossim_3(x, y):
return round(cossim(x, y), 3)
'''
main function
'''
if __name__ == "__main__":
c = sys.argv[1] if len(sys.argv) > 1 else ""
if c == "sig":
sig()
elif c == "sketch":
if len(sys.argv) > 2:
rv = int(sys.argv[2])
else:
rv = 256
print 'INFO: ', rv, ' random vectors'
sketch(rv)
elif c == "cos":
cos()
elif c == "matrix":
matrix()
elif c == "similar":
if len(sys.argv) > 4:
similar(
int(sys.argv[2]),
int(sys.argv[3]),
int(sys.argv[4])
)
else:
print 'ERROR: Please identify the picture id.'
elif c == "all":
similar_all()
elif c == 'lsh':
lsh_all()
elif c == 'bin_size':
bin_size()
else:
print '''
Welcome to Prism.
Options:
* sig : generate Signatures based on the colours distribution.
* sketch : generate Sketches based on Signatures.
* cos : calculate the Cosine Similarity between samples and all population.
* matrix : calculate the similarity matrix based on Sketeches
* similar : find similar candidates for one image
* all : find similar candidates for all images, generate a mongdb sql as output
* bin_size : experiments to optimize bin size
'''