forked from fmaglia/keras_rmac_plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
366 lines (305 loc) · 10.7 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
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
356
357
358
359
360
361
362
363
364
365
366
from keras.applications.vgg16 import VGG16, preprocess_input
from keras.applications.resnet50 import ResNet50, preprocess_input
from keras.preprocessing import image
from keras.models import Model
import os
import os.path
from PIL import Image, ImageFile
import sys
import numpy as np
from numpy import linalg as LA
import cv2
import operator
from sklearn.decomposition import PCA
import scipy
import math
import time
from sys import getsizeof
from sklearn.preprocessing import normalize
from sklearn.decomposition import PCA
from collections import defaultdict
from sklearn.metrics.pairwise import euclidean_distances
from tqdm import tqdm
from glob import glob
import shutil
def str_to_class(str):
return getattr(sys.modules[__name__], str)
def readTraining(dataset, rotated=True, nFiles=0, debug=False):
if (dataset == 'oxford5k' or dataset == 'paris6k'):
path = 'data/'+dataset+'/jpg/*.jpg'
elif (dataset == 'Flickr1M'):
path = 'data/Flickr1M/im*/*/*.jpg'
DbImages = np.sort(glob(path)) #da capire se funziona con Flickr1M
if (dataset == 'Flickr1M'):
DbImages = DbImages[0:int(nFiles*1000)]
return DbImages
def readTest(dataset, full=False, debug=False):
bBox = []
if (dataset == 'oxford5k' or dataset == 'paris6k'):
path = 'dataset/' + dataset + '/query'
if (not full):
path += '_reduced/*.jpg'
else:
path +='/*.jpg'
queryImages = np.sort(queryImages)
if ((dataset=="oxford5k" or dataset=="paris6k") and full):
print("Creation of bBox list")
#insert elements in bBox list
url = 'dataset/'+dataset+'/gt_files/'
lab_filenames = np.sort(os.listdir(url))
for e in lab_filenames:
if e.endswith('_query.txt'):
q_name = e[:-len('_query.txt')]
q_data = open("{0}/{1}".format(url, e)).readline().split(" ")
q_filename = q_data[0][5:] if q_data[0].startswith('oxc1_') else q_data[0]
q_final = [s.rstrip() for s in q_data]
bBox.append(q_final[1:])
for i,q in enumerate(queryImages,0):
img = cv2.imread(q)
h = img.shape[0]
w = img.shape[1]
bBox[i][0] = float(bBox[i][0]) / w
bBox[i][2] = float(bBox[i][2]) / w
bBox[i][1] = float(bBox[i][1]) / h
bBox[i][3] = float(bBox[i][3]) / h
return queryImages,bBox
def calculateMAC(featureVector, listData): #max-pooling and l2-norm
rows = featureVector.shape[1] * featureVector.shape[2]
cols = featureVector.shape[3]
features1 = np.reshape(featureVector, (rows, cols))
features2 = np.amax(features1, axis = 0)
features2 /= np.linalg.norm(features2, 2)
listData.append(features2)
return
def calculateRMAC(features, listData, L):
W = features.shape[1]
H = features.shape[2]
# print("W",W,"H",H)
for l in range(1,L+1):
if (l==1):
heightRegion = widthRegion = min(W,H)
if (W<H):
xRegions = 1
yRegions = 2
else:
xRegions = 2
yRegions = 1
else:
widthRegion = heightRegion = math.ceil(2*min(W,H)/(l+1))
if (l==2):
xRegions = 2
yRegions = 3
elif (l==3):
xRegions = 3
yRegions = 2
if (widthRegion*xRegions < W): #not covered the image along width
widthRegion = math.ceil(W/xRegions)
if (heightRegion*yRegions < H):
heightRegion = math.ceil(H/yRegions)
coefW = W / xRegions
coefH = H / yRegions
# print("L:",l," w:",widthRegion," h:",heightRegion,"xRegions",xRegions,"yRegions",yRegions)
for x in range(0,xRegions):
for y in range(0,yRegions):
initialX = round(x*coefW)
initialY = round(y*coefH)
finalX = initialX + widthRegion
finalY = initialY + heightRegion
if (finalX > W):
finalX = W
initialX = finalX - widthRegion
if (finalY > H):
finalY = H
initialY = finalY - heightRegion
# print(" X ",initialX,":", finalX," Y ", initialY,":", finalY)
featureRegion = features[:,initialX:finalX,initialY:finalY,:] #(old implementation)
calculateMAC(featureRegion, listData)
return
def resizeImg (img, i, delta):
if delta != 0:
w = img.size[0]
h = img.size[1]
newWidth = round(w + w*delta)
newHeight = round(h + h*delta)
img = img.resize((newWidth,newHeight))
return img
def extractFeatures(imgs, model, RMAC, L, resolutionLevel, bBox=[], croppedActivations = False):
listData = []
deltas = [0, -0.25, 0.25]
for j in tqdm(range(0,len(imgs))):
for i in range(0, resolutionLevel):
img = image.load_img(imgs[j])
img = resizeImg(img,i, deltas[i])
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
features = model.predict(x)
if (croppedActivations):
startDim1 = math.floor(bBox[j][1]*features.shape[1])
endDim1 = math.ceil(bBox[j][3]*features.shape[1])
startDim2 = math.floor(bBox[j][0]*features.shape[2])
endDim2 = math.floor(bBox[j][2]*features.shape[2])
features = np.copy(features[:,startDim1:endDim1,startDim2:endDim2,:])
# print(features.shape,"->", features2.shape)
calculateMAC(features, listData)
if (RMAC):
calculateRMAC(features, listData, L)
return listData
def learningPCA(listData):
fudge = 1E-18
X = np.matrix(listData)
mean = X.mean(axis=0)
# subtract the mean
X = np.subtract(X, mean)
# calc covariance matrix
Xcov = np.dot(X.T,X)
d,V = np.linalg.eigh(Xcov)
D = np.diag(1. / np.sqrt(d+fudge))
W = np.dot(np.dot(V, D), V.T)
return W, mean
def apply_whitening(listData, Xm, W) :
X = np.matrix(listData)
X = np.subtract(X, Xm)
Xnew = np.dot(X,W)
Xnew /= LA.norm(Xnew,axis=1).reshape(Xnew.shape[0],1)
return Xnew
def sumPooling(listData, numberImages, largeScaleRetrieval=False):
newListData = []
value = 0
regions = listData.shape[0] // numberImages
for i, elem in enumerate(listData, 1):
value = np.add(value,elem)
if (i%regions==0):
value /= LA.norm(value, 2)
newListData.append(value)
value = 0
if (not largeScaleRetrieval):
print("Sum pooling of",regions,"regions. The descriptors are",len(newListData),"of shape",newListData[0].shape)
return newListData
def extractAndWhiteningNEW(imgs, model, RMAC, L, resolutionLevel,Xm,W, limits=1000, pca=None):
ImageFile.LOAD_TRUNCATED_IMAGES = True
tmpList = []
finalList = []
delta = 0.25
for j in tqdm(range(0,len(imgs))):
for i in range(0, resolutionLevel):
img = image.load_img(imgs[j])
img = resizeImg(img, i, delta)
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
features = model.predict(x)
calculateMAC(features, tmpList)
if (RMAC):
calculateRMAC(features, tmpList, L)
if ((j+1)%limits==0):
tmpList = apply_whitening(tmpList, Xm, W)
tmpList = sumPooling(tmpList, limits, True)
finalList.extend(tmpList)
tmpList = []
print("Features len",len(finalList))
return finalList
def write_results(url, queryImages,i, distances, DbImages, dataset, largeScaleRetrieval=False):
if (dataset=='oxford5k' or dataset=='paris6k'):
if not os.path.exists(url):
os.makedirs(url)
file_query = open(url+"/"+os.path.basename(queryImages[i])[:-4], "w")
for elem in distances:
if ((elem[0]>5062 and dataset=='oxford5k') or (elem[0]>6391 and dataset=='paris6k')):
file_query.write("Flickr1M")
else:
file_query.write(os.path.basename(DbImages[elem[0]])[:-4])
file_query.write("\n")
file_query.close()
return
def calcResults(dataset, url):
if (dataset=='paris6k' or dataset=='oxford5k'):
os.system("results/"+dataset+"/compute_ap_all_2 "+url)
return
def retrieve(queryMAC, DbMAC, topResultsQE, url, queryImages, DbImages, dataset, largeScaleRetrieval=False):
if (os.path.exists(url) and (dataset=='paris6k' or dataset=='oxford5k')):
shutil.rmtree(url)
reRank = []
for i,q in enumerate(queryMAC,0):
distances = {}
qNP = np.asarray(q)
for j,dbElem in enumerate(DbMAC,0):
dbNP = np.asarray(dbElem)
distances[j] = np.linalg.norm(qNP-dbNP)
finalDict = sorted(distances.items(), key=operator.itemgetter(1))
reRank.extend(list(finalDict)[:topResultsQE])
write_results(url, queryImages, i, finalDict, DbImages, dataset, largeScaleRetrieval)
calcResults(dataset, url)
return reRank
def retrieveRegionsNEW(queryMAC, regions, topResultsQE,url, queryImages, DbImages, dataset, largeScaleRetrieval=False):
if (os.path.exists(url) and (dataset=='paris6k' or dataset=='oxford5k')):
shutil.rmtree(url)
reRank = []
nRegions = regions.shape[0]//len(DbImages)
for i,q in enumerate(queryMAC,0):
distances = {}
bestRegions = []
qNP = np.asarray(q)
for j,dbElem in enumerate(regions,0):
dbNP = np.asarray(dbElem)
indexDb = j//nRegions
d = np.linalg.norm(qNP-dbNP)
if (indexDb in distances):
if (distances[indexDb][0]>d):
distances[indexDb] = [d,j]
else:
distances[indexDb] = [d,j]
finalDict = sorted(distances.items(), key=operator.itemgetter(1))
reRank.extend(list(finalDict)[:topResultsQE])
write_results(url, queryImages, i, finalDict, DbImages, dataset, largeScaleRetrieval)
calcResults(dataset, url)
return reRank
def retrieveQE(queryMAC, DbMAC, topResultsQE, url, queryImages, DbImages, reRank, dataset, largeScaleRetrieval=False):
url += '_avgQE'
if (os.path.exists(url) and (dataset=='paris6k' or dataset=='oxford5k')):
shutil.rmtree(url)
finalReRank = []
for i,q in enumerate(queryMAC,0):
distances2 = {}
qNewNP = np.asarray(q)
for top_results in range(0,int(topResultsQE)):
index = top_results+(topResultsQE*i)
dbOLD = np.asarray(DbMAC[reRank[index][0]])
qNewNP += dbOLD
qNewNP = np.divide(qNewNP,float(topResultsQE))
for j,dbElem in enumerate(DbMAC,0):
dbNP = np.asarray(dbElem)
distances2[j] = np.linalg.norm(qNewNP-dbNP)
finalDict = sorted(distances2.items(), key=operator.itemgetter(1))
finalReRank.extend(list(finalDict))
write_results(url, queryImages, i, finalDict, DbImages, dataset, largeScaleRetrieval)
calcResults(dataset, url)
return finalReRank
def retrieveQERegionsNEW(queryMAC, regions, topResultsQE, url, queryImages, DbImages, reRank, dataset, largeScaleRetrieval=False):
url += '_avgQE'
if (os.path.exists(url) and (dataset=='paris6k' or dataset=='oxford5k')):
shutil.rmtree(url)
finalReRank = []
nRegions = regions.shape[0]//len(DbImages)
for i,q in enumerate(queryMAC,0):
distances2 = {}
qNewNP = np.asarray(q)
for top_results in range(0,int(topResultsQE)):
index = top_results+(topResultsQE*i)
dbOLD = np.asarray(regions[reRank[index][1][1]])
qNewNP += dbOLD
qNewNP = np.divide(qNewNP,float(topResultsQE))
for j,dbElem in enumerate(regions,0):
dbNP = np.asarray(dbElem)
indexDb = j//nRegions
d = np.linalg.norm(qNewNP-dbNP)
if (indexDb in distances2):
if (distances2[indexDb]>d):
distances2[indexDb] = d
else:
distances2[indexDb] = d
finalDict = sorted(distances2.items(), key=operator.itemgetter(1))
finalReRank.extend(list(finalDict))
write_results(url, queryImages, i, finalDict, DbImages, dataset, largeScaleRetrieval)
calcResults(dataset, url)
return finalReRank