Repository navigation
Expand file tree
/
Copy pathcode.py
More file actions
237 lines (206 loc) · 8.1 KB
/
Copy pathcode.py
File metadata and controls
237 lines (206 loc) · 8.1 KB
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
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import argparse
import utils
import cv2
import random
import numpy as np
from sklearn.metrics import silhouette_samples, silhouette_score
import matplotlib.image as mpimg
import os
import math
import libpylshbox
try:
os.remove("./database/.DS_Store")
os.remove("./query/.DS_Store")
except OSError:
pass
Hash_count = 0
hasSeenImageName = {}
def Maphash(l):
global Hash_count
Hash_count += 1
print "Vector " , Hash_count," Getting started to hashed and reduced"
StartSliceIndex = 0
StopSliceIndex=0
HashedResults = []
while StopSliceIndex < len(l):
StopSliceIndex = min(StartSliceIndex+1000,len(l))
l_temp = l[StartSliceIndex:StopSliceIndex]
StartSliceIndex = StartSliceIndex +1000
l_temp = hash(tuple(l_temp))%32416190071
HashedResults.append(l_temp)
print "Done getting hashed and reduced size"
return HashedResults
'''
A formula to determine the id of starting point of a left top most corner
of a cube
'''
def formula(cubeX,cubeY,cubeZ,sidelen,xshift,yshift,zshift):
xc = math.ceil(((xshift%sidelen) + cubeX + 1)*1.0/sidelen)
yc = math.ceil(((yshift%sidelen) + cubeY + 1)*1.0/sidelen)
zc = math.ceil(((zshift%sidelen) + cubeZ + 1)*1.0/sidelen)
yd = math.ceil((256+yshift%sidelen)*1.0/sidelen)
zd = math.ceil((256+zshift%sidelen)*1.0/sidelen)
xd = math.ceil((256+xshift%sidelen)*1.0/sidelen)
offset = 0
for side in range(0,int(math.log(sidelen,2))):
yp = math.ceil((256+yshift%(2**side))*1.0/((2**side)))
zp = math.ceil((256+zshift%(2**side))*1.0/(2**side))
xp = math.ceil((256+xshift%(2**side))*1.0/(2**side))
offset += xp*yp*zp
#offset = int(math.log(sidelen,2))*xd*yd*zd
return int(offset + (xc - 1)*yd*zd + (yc-1)*zd + zc - 1)
'''
Gets the image and filename from a particular folder 'folder'
and stores the rgb values of that image and its name in a vector
'''
def load_images(folder):
images = []
imageFileNames = []
for filename in os.listdir(folder):
img = mpimg.imread(os.path.join(folder, filename))
if img is not None:
imageFileNames.append(filename)
images.append(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
return images,imageFileNames
'''
creates a mapping from image to number of clusters
where its silhoute score of optimum
and clusters that image with those number of clusters
Another approach could be to store clt objects also
in a file , but it all depends on tradeoff
'''
def createMappingOfImageToNClusters():
fil = open('database.txt','r')
fileClusterMap = {}
#for dataset images
for line in fil:
fileClusterMap[line.split(' ')[0]] = int(line.split(' ')[1])
#for query images
fil = open('query.txt','r')
for line in fil:
fileClusterMap[line.split(' ')[0]] = int(line.split(' ')[1])
return fileClusterMap
'''
gets some of the stats as mentioned in the paper like
average number of clusters per image, but this can be changed on
the basis of how and when elbow point is chosen
or other approaches like density based clustering may solve the issue
'''
def getStats(fileClusterMap):
meanNumberOfClusters = np.mean(np.array(fileClusterMap.values()))
print "Average clusters per image: ",meanNumberOfClusters
return
'''
This determines the randomshifting of grids
'''
def getRandomShifts():
return (int(random.uniform(0,255)),int(random.uniform(0,255)),int(random.uniform(0,255)))
'''
Get the flabel weights and ImageClt objects for all images
'''
def getFlablesAndImageClt(images,imageFileNames,fileClusterMap):
imageCount = 0
flabels =[]
#store clt(object) of every image in this:
ImgClt = {}
global hasSeenImageName
for image in images:
if imageFileNames[imageCount] in hasSeenImageName.keys():
ImgClt[imageCount] = hasSeenImageName[imageFileNames[imageCount]]
labelc = [0]*(len(ImgClt[imageCount].cluster_centers_))
for i in ImgClt[imageCount].labels_:
labelc[i]+=1
flabels.append(labelc)
print imageFileNames[imageCount],'n_cluster:',len(ImgClt[imageCount].cluster_centers_),\
'imageCount:',imageCount,'/',len(images)
imageCount+=1
continue
image = image.reshape((image.shape[0] * image.shape[1], 3))
if imageFileNames[imageCount] not in fileClusterMap.keys():
print imageFileNames[imageCount] ," doen't have a mapping in pre processed data"
imageCount+=1
continue
n_cluster = fileClusterMap[imageFileNames[imageCount]]
ImgClt[imageCount] = KMeans(n_clusters = n_cluster)
ImgClt[imageCount].fit(image)
print imageFileNames[imageCount],'n_cluster:',n_cluster,'imageCount:',imageCount,'/',len(images)
#finding the approximate diameter
labelc = [0]*(n_cluster)
for i in ImgClt[imageCount].labels_:
labelc[i]+=1
flabels.append(labelc)
hasSeenImageName[imageFileNames[imageCount]] = ImgClt[imageCount]
imageCount+=1
return flabels,ImgClt
'''
Get Appended list after embedding as mentioned in the paper
and get the number of points of each image in a particular cube
'''
def getAppendedVeList(images,ImgClt,xshift,yshift,zshift):
imageCount = 0
velist = []
for image in images:
if imageFileNames[imageCount] not in fileClusterMap.keys():
print imageFileNames[imageCount] ," doen't have a mapping in pre processed data"
imageCount+=1
continue
sidelen = 1
mappingPointsToCube = [0]*(3*(10**7))
while sidelen<=255:
for points in ImgClt[imageCount].cluster_centers_:
[x,y,z] = points
CubeX = int(x/sidelen)*sidelen + (sidelen-(xshift%sidelen))%sidelen
CubeY = int(y/sidelen)*sidelen + (sidelen-(yshift%sidelen))%sidelen
CubeZ = int(z/sidelen)*sidelen + (sidelen-(zshift%sidelen))%sidelen
mappingPointsToCube[formula(CubeX,CubeY,CubeZ,sidelen,xshift,yshift,zshift)]+=(sidelen)
sidelen*=2
mappingPointsHash = Maphash(mappingPointsToCube)
velist.append(mappingPointsHash)
imageCount+=1
return velist
#load dataset images
images,imageFileNames = load_images("./database/")
#load query images
queryImages, queryImageNames = load_images("./query/")
#get mapping
fileClusterMap = createMappingOfImageToNClusters()
#get average number of cluster per image
getStats(fileClusterMap)
#get flables and ImgClt
flabels,ImgClt = getFlablesAndImageClt(images,imageFileNames,fileClusterMap)
#get flables and ImgClt for query images
flabelsQuery,ImgCltQuery = getFlablesAndImageClt(queryImages,queryImageNames,fileClusterMap)
#this contains the vector generated as discussed in paper after embedding
velist=[]
#finding the random shift for each axis
(xshift,yshift,zshift) = getRandomShifts()
print "Done Creating Cube Embedding, moving on to finding number of points in each cube"
velist = getAppendedVeList(images,ImgClt,xshift,yshift,zshift)
velistQuery = getAppendedVeList(queryImages,ImgCltQuery,xshift,yshift,zshift)
print "Embedding for each image complete.! check once for speed"
'''
Finally get f(p)-f(q) for each query image
Uncomment this to test
'''
# queryImageCount = 0
# for queryImageVector in velistQuery:
# imageCount = 0
# for imageVector in velist:
# print 'diff between ',queryImageNames[queryImageCount], 'and',imageFileNames[imageCount],'is',\
# np.sum(np.absolute(np.subtract(np.array(imageVector),np.array(queryImageVector))))
# imageCount+=1
# queryImageCount+=1
'''
LSH
'''
psdL1_mat = libpylshbox.psdlsh()
psdL1_mat.init_mat(velist, '', 20, 4, 1, 5)
queryImageCount = 0
for queryImageVector in velistQuery:
result = psdL1_mat.query(queryImageVector, 2, 10)
indices, dists = result[0], result[1]
for i in range(len(indices)):
print "for query image: ",queryImageNames[queryImageCount]," datesetImage",imageFileNames[indices[i]], '\t', dists[i]
queryImageCount+=1