-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
268 lines (198 loc) · 7.15 KB
/
utils.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
import imagehash
import skimage.measure as ssim
from numbers import Number
import random
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import glob
from scipy import sum, average
from scipy.spatial.distance import directed_hausdorff
# feature map for the query image
def index_query(image_path, cnn_model):
img = image.load_img(image_path, target_size=(224, 224))
x = image.img_to_array(img)
x1 = np.expand_dims(x, axis=0)
x2 = preprocess_input(x1)
print(x2.shape)
query = cnn_model.predict(x2)
print(query.shape)
query = query.flatten()
print(query.shape)
imgplot = plt.imshow(img)
plt.show()
return query,img,x
# helper function to plot the topn results retrieved
def plot_results(selected_images, highlight = None):
n = len(selected_images)
fig = plt.figure(figsize=(20,10))
for i in range(n):
x = round(n / 5)
a = fig.add_subplot(x,5,i+1)
img = selected_images[i][0]
#img = image.load_img(image_path, target_size=(224, 224))
plt.imshow(img)
if i == highlight:
a.set_title("RELEVANT")
else:
a.set_title(str(selected_images[i][1]))
plt.show()
def ssims(x,y):
'''
return similarity score
'''
ssimValue = ssim.compare_ssim(x,y,multichannel=True )
#hashes=imagehash.dhash(i)-imagehash.dhash(j)
return ssimValue
def simHash(x,y):
'''
return similarity score using Hash function
'''
hashes=imagehash.dhash(x)-imagehash.dhash(y)
return hashes
def ssims2(x,y):
'''
return similarity similarity score between pairs of feature vector
'''
ssimValue = ssim.compare_ssim(x,y)
#hashes=imagehash.dhash(i)-imagehash.dhash(j)
return ssimValue
### similarity measure on feature space
# for the purpose of comparison, I use the Euclidean Distance between the feature vectors
def find_query_l2(query_index, indexes, topn):
'''
return euclidean distacnce between pairs of feature vector
'''
retrieved = []
for indx in indexes:
search_image = indx[0]
search_indx = indx[1]
diff = query_index - search_indx
diff = diff**2
match_score = sum(diff)
retrieved.append((search_image, match_score))
sorted_retreival = sorted(retrieved, key=lambda x: x[1])
selected = sorted_retreival[:topn]
return selected
# helper function to do the retreival
# inputs - the query feature vector computed from the cnn_model,
# the indexes image list
# and the number of results to retreive (topn)
# outputs - a list of tuples containing the retrieved image paths and their respective scores
## similarity measure on feature vector space
def find_query_ssim2(query_index, indexes, topn):
'''
input : query_index, indexes, topn.
output: structural_similarity_index_value for top-n images
'''
retrieved = []
for indx in indexes:
search_image = indx[0]
search_indx = indx[1]
match_score = ssims2(query_index, search_indx)
retrieved.append((search_image, match_score))
sorted_retreival = sorted(retrieved, key=lambda x: x[1])
selected = sorted_retreival[-topn:]
selected = sorted(selected, key=lambda x: x[1], reverse=True)
return selected
def find_query_ssim3(query_index, indexes, topn):
'''
output : structural_similarity_index/euclidean_distance for top-n images
input : query_index, indexes, topn.
'''
retrieved = []
for indx in indexes:
search_image = indx[0]
search_indx = indx[1]
score1 = ssims2(query_index, search_indx)
diff = query_index - search_indx
diff = diff**2
score2 = sum(diff)
match_score = score1/score2
retrieved.append((search_image, match_score))
sorted_retreival = sorted(retrieved, key=lambda x: x[1])
selected = sorted_retreival[-topn:]
selected = sorted(selected, key=lambda x: x[1], reverse=True)
return selected
def find_query(query_index,model, indexes, topn):
'''
input : query image vector,neural model, indexes, topn.
output: similarity score for top-n images
'''
retrieved = []
for indx in indexes:
search_image = indx[0]
search_indx = indx[1]
diff = query_index - search_indx
diff = diff**2
diff = np.reshape(diff,(1,2048))
match_score =model.predict(diff.reshape(1,-1))# model.predict(diff.reshape(-1,1))
retrieved.append((search_image, match_score))
sorted_retreival = sorted(retrieved, key=lambda x: x[1])
selected = sorted_retreival[-topn:]
selected = sorted(selected, key=lambda x: x[1], reverse=True)
return selected
## similarity measure on raw image input
def find_query_hash(query_index, indexes, topn):
'''
input: query image feature vector,indexed image data base, number of similar images to be retrieved
out: similarity score for topn using differencehash
'''
retrieved = []
for indx in indexes:
search_image = indx[0]
search_indx = indx[1]
match_score = simHash(query_index, search_image)
retrieved.append((search_image, match_score))
sorted_retreival = sorted(retrieved, key=lambda x: x[1])
selected = sorted_retreival[:topn]
return selected
def find_query_ssim(query_index, indexes, topn):
'''
input: query image feature vector,indexed image data base, number of similar images to be retrieved
out: similarity score for topn
'''
retrieved = []
for indx in indexes:
search_image = indx[0]
search_indx = indx[1]
x = image.img_to_array(search_image)
match_score = ssims(x,query_index)
#diff = query_index - search_indx
#diff = diff**2
#match_score = sum(diff)
retrieved.append((search_image, match_score))
sorted_retreival = sorted(retrieved, key=lambda x: x[1])
selected = sorted_retreival[-topn:]
return selected
def find_query_l2raw(query_index, indexes, topn):
'''
input: query image feature vector,indexed image data base, number of similar images to be retrieved
out: euclidean distance for topn
'''
retrieved = []
for indx in indexes:
search_image = indx[0]
search_indx = indx[1]
x = image.img_to_array(search_image)
#match_score = ssims(x,query_index)
diff = query_index - x
diff = diff**2
match_score = sum(diff)
retrieved.append((search_image, match_score))
sorted_retreival = sorted(retrieved, key=lambda x: x[1])
selected = sorted_retreival[:topn]
return selected
def similarity_score(x,y,i,j):
'''
return similarity score
'''
ssimValue = ssim.compare_ssim(x,y,multichannel=True )
hashes=imagehash.dhash(i)-imagehash.dhash(j)
if hashes != 0: # it is an integer or a float
return ssimValue/hashes
else:
return ssimValue