-
Notifications
You must be signed in to change notification settings - Fork 17
/
similarity.py
258 lines (209 loc) · 10.2 KB
/
similarity.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
from recognitionModel import RecognitionModel
from utilities import loadImage,removeBorder,showImage,saveMatrixAsImage
import random
import numpy as np
class DummyArguments():
def __init__(self):
self.noisy = True
self.distance = True
self.architecture = "original"
self.dropout = False
self.learningRate = 1
self.showParticles = False
def learnedDistanceMatrix(images):
from calculate_distances import distanceMatrix
return np.array(distanceMatrix)
# worker = RecognitionModel(DummyArguments())
# worker.loadDistanceCheckpoint("checkpoints/distance.checkpoint")
# jobs = [(x,y) for j,x in enumerate(images)
# for k,y in enumerate(images)
# if j != k ]
# distances = []
# while jobs != []:
# batch = jobs[:100]
# jobs = jobs[100:]
# print "%d jobs remaining"%len(jobs)
# result = worker.learnedDistances(np.array([x for x,_ in batch ]),
# np.array([x for _,x in batch ]))
# for j in range(len(batch)):
# distances.append(result[j,1] + result[j,0])
# matrix = np.zeros((len(images),len(images)))
# indexes = [(x,y) for x in range(len(images))
# for y in range(len(images))
# if x != y]
# for (x,y),d in zip(indexes, distances):
# matrix[x,y] += d
# matrix = matrix + matrix.T
# print "MATRIX:"
# print matrix.tolist()
# return matrix
if __name__ == '__main__':
worker = RecognitionModel(DummyArguments())
worker.loadDistanceCheckpoint("checkpoints/distance.checkpoint")
imageNames = ['drawings/expert-%d.png'%j
for j in range(100) ]
images = [ loadImage(n)
for n in imageNames ]
jobs = [(x,y) for j,x in enumerate(images)
for k,y in enumerate(images)
if j != k ]
distances = []
while jobs != []:
batch = jobs[:100]
jobs = jobs[100:]
print "%d jobs remaining"%len(jobs)
result = worker.learnedDistances(np.array([x for x,_ in batch ]),
np.array([x for _,x in batch ]))
for j in range(len(batch)):
distances.append(result[j,1] + result[j,0])
matrix = np.zeros((len(images),len(images)))
indexes = [(x,y) for x in range(len(images))
for y in range(len(images))
if x != y]
for (x,y),d in zip(indexes, distances):
matrix[x,y] += d
matrix = matrix + matrix.T
print "MATRIX:"
print matrix.tolist()
def analyzeFeatures(featureMaps):
from sklearn.decomposition import PCA,NMF
from sklearn import preprocessing
from sklearn.manifold import MDS
import matplotlib.pyplot as plot
import matplotlib.image as image
# collect together a whole of the different names for features
featureNames = list(set([ k for f in featureMaps.values() for k in f ]))
imageNames = featureMaps.keys()
# Convert feature maps into vectors
featureVectors = [ [ featureMaps[k].get(name,0) for name in featureNames ]
for k in imageNames ]
print "Feature vectors:"
for j,n in enumerate(imageNames):
print n
print featureMaps[n]
print featureVectors[j]
# Figure out things that are close / far as measured by different metrics
percentile = 80
imageDistances = learnedDistanceMatrix(None)
numberOfPrograms = len(featureVectors)
programDistances = np.zeros((numberOfPrograms,numberOfPrograms))
featureVectors = preprocessing.scale(np.array(featureVectors))
for j in range(numberOfPrograms):
for k in range(numberOfPrograms):
programDistances[j,k] = ((featureVectors[j,:] - featureVectors[k,:])*(featureVectors[j,:] - featureVectors[k,:])).sum()
smallDistance = np.percentile(programDistances,100 - percentile)
bigDistance = np.percentile(programDistances,percentile)
closePrograms = set([(n1,n2) for j,n1 in enumerate(imageNames) for k,n2 in enumerate(imageNames)
if n1 < n2 and programDistances[j,k] < smallDistance])
farPrograms = set([(n1,n2) for j,n1 in enumerate(imageNames) for k,n2 in enumerate(imageNames)
if n1 < n2 and programDistances[j,k] > bigDistance])
smallDistance = np.percentile(imageDistances,100 - percentile)
bigDistance = np.percentile(imageDistances,percentile)
imageNames = ["drawings/expert-%d.png"%j for j in range(100) ]
closeImages = set([(n1,n2) for j,n1 in enumerate(imageNames) for k,n2 in enumerate(imageNames)
if n1 < n2 and imageDistances[j,k] < smallDistance])
farImages = set([(n1,n2) for j,n1 in enumerate(imageNames) for k,n2 in enumerate(imageNames)
if n1 < n2 and imageDistances[j,k] > bigDistance])
programOptions = [(closePrograms,'close in program space'),(farPrograms,'distant in program space')]
imageOptions = [(closeImages,'close in image space'),(farImages,'far in image space')]
for programSet,programName in programOptions:
for imageSet,imageName in imageOptions:
overlap = programSet&imageSet
print programName,'&',imageName,'have overlap',len(overlap)
overlap = list(sorted(list(overlap)))
indices = np.random.choice(range(len(overlap)),size = min(100,len(overlap)),replace = False)
overlap = [overlap[j] for j in indices ]
matrix = 1 - np.concatenate([ np.concatenate((loadImage(n1),loadImage(n2)), axis = 0) for n1,n2 in overlap ],axis = 1)
saveMatrixAsImage(matrix*255,"similarity/%s%s.png"%(programName,imageName))
assert False
for algorithm in [0,1,2]:
if algorithm == 0:
learner = PCA()
transformedFeatures = learner.fit_transform(preprocessing.scale(np.array(featureVectors)))
print learner.explained_variance_ratio_
if algorithm == 1:
learner = NMF(2)
transformedFeatures = learner.fit_transform(preprocessing.scale(np.array(featureVectors),
with_mean = False))
if algorithm == 2:
imageNames = ["drawings/expert-%d.png"%j for j in range(100) ]
distances = learnedDistanceMatrix(map(loadImage,imageNames))
learner = MDS(dissimilarity = 'precomputed')
transformedFeatures = learner.fit_transform(distances)
print transformedFeatures
maximumExtent = max([transformedFeatures[:,0].max() - transformedFeatures[:,0].min(),
transformedFeatures[:,1].max() - transformedFeatures[:,1].min()])
print maximumExtent
w = 0.1*maximumExtent
if algorithm < 2:
print learner.components_
for dimension in range(2):
coefficients = learner.components_[dimension]
print "Dimension %d:"%(dimension+1)
for j,n in enumerate(featureNames):
print n,'\t',learner.components_[dimension,j]
print
showProbability = []
for j in range(len(imageNames)):
overlapping = 0
for k in range(len(imageNames)):
if j == k: continue
d = abs(transformedFeatures[j,:2] - transformedFeatures[k,:2])
if d[0] < 2*w and d[1] < 2*w:
overlapping += 1
showProbability.append(1.0/(1 + overlapping*0.3))
for index in range(50):
f,a = plot.subplots()
imageIsShown = [random.random() < sp for sp in showProbability ]
coolImages = [38,39,12,26,46,47,76,71,75,80,89]
for coolImage in coolImages:
coolImage = 'drawings/expert-%d.png'%coolImage
for j, imageName in enumerate(imageNames):
if imageName == coolImage:
imageIsShown[j] = True
break
imageCoordinates = [ transformedFeatures[j,:2] for j in range(len(imageNames)) ]
imageCoordinates = diffuseImagesOutwards(imageCoordinates,w,imageIsShown)
for j, imageName in enumerate(imageNames):
if not imageIsShown[j]: continue
i = 1 - image.imread(imageName)
i = 1 - removeBorder(i)
x = imageCoordinates[j][0]
y = imageCoordinates[j][1]
a.imshow(i, aspect = 'auto',
extent = (x - w,x + w,
y - w,y + w),
zorder = -1,
cmap = plot.get_cmap('Greys'))
a.arrow(x, y,
transformedFeatures[j,0] - x, transformedFeatures[j,1] - y,
head_width=0.04, head_length=0.1, fc='k', ec='k',
zorder = -2)
a.scatter(transformedFeatures[:,0],
transformedFeatures[:,1])
a.get_yaxis().set_visible(False)
a.get_xaxis().set_visible(False)
n = ['PCA','NMF','MDS'][algorithm]
plot.savefig('%s/%s%d.png'%(n,n,index),bbox_inches = 'tight')
# plot.show()
def diffuseImagesOutwards(coordinates, w, mask):
cp = coordinates
for _ in range(10):
forces = []
for j,p1 in enumerate(cp):
F = np.array([0.0,0.0])
for k,p2 in enumerate(cp):
if (not mask[k]) or (not mask[j]): continue
if j == k: continue
distance = ((p1 - p2)*(p1 - p2)).sum()
if distance == 0: continue
difference = abs(p1 - p2)
if difference[0] > 2*w or difference[1] > 2*w: continue
F += (p1 - p2)/distance
forces.append(F)
cp = [ c + f for c,f in zip(cp,forces) ]
allowedDistance = 1.3
cp = [ np.array([ min(original[0] + allowedDistance*w, max(original[0] - allowedDistance*w, c[0])),
min(original[1] + allowedDistance*w, max(original[1] - allowedDistance*w, c[1]))])
for c,original in zip(cp,coordinates) ]
return cp