-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfaceIsomap.py
More file actions
89 lines (75 loc) · 2.73 KB
/
faceIsomap.py
File metadata and controls
89 lines (75 loc) · 2.73 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
#This was the code run to get the isomap embedding of facespace
import os
import time
from sklearn import manifold
from PIL import Image
import math
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from matplotlib.offsetbox import TextArea, DrawingArea, OffsetImage, AnnotationBbox
from sklearn.metrics.pairwise import euclidean_distances
import random
import csv
with open('faceMatrix.csv') as csvfile:
csvreader = csv.reader(csvfile)
rows = []
for row in csvreader:
rows.append(row)
def generateDeterministicSwissRoll(num_points):
data= []
for i in range(num_points):
phi = (.1*i+1);
for j in range(3):
temp = []
temp.append(10*phi*(math.cos(phi)))
temp.append(10*phi*(math.sin(phi)))
temp.append(j*8)
data.append(temp)
return data
def readcsv():
return rows
def distance(x,y):
x=np.array(x)
y=np.array(y)
p=np.sum((x-y)**2)
d=np.sqrt(p)
return d
def generatePseudoRandomList(embedded_array, n, maxiters, mindist):
indices = []
iters = 0
while(len(indices) < n and iters < maxiters):
choice = random.sample(range(0, len(embedded_array)), 1)
tooClose = False
for point in indices:
if(distance([embedded_array[point][0][0], embedded_array[point][0][1]], [embedded_array[choice][0][0], embedded_array[choice][0][1]]) < mindist):
tooClose = True
if not tooClose:
indices.append(choice)
return indices
final_directory = "dim_recution(imagetest)_"+str(round(time.time()))
os.chdir("code_output")
os.chdir("isomap")
os.makedirs(final_directory)
os.chdir(final_directory)
data = readcsv()
ndata=np.array([np.array(xi) for xi in data]).astype(np.float64)
ndata_preimage = np.copy(ndata)
ndata = np.transpose(ndata)
#defaults to 5 neighbors, in kclosest neighbors graph
embedded_array = manifold.Isomap(n_neighbors=5, n_components=2).fit_transform(ndata)
im = []
randomlist = generatePseudoRandomList(embedded_array, 50, 1000000, 10)
for i in randomlist: #20
test = ndata_preimage[:,i]
test = np.transpose(np.resize(test, (64,64)))
im.append(Image.fromarray((test*255).astype(np.uint8)))
fig = plt.figure()
ax= plt.axes()
dat_array= np.transpose(embedded_array);
ax.scatter(dat_array[0], dat_array[1])
for imageindex in range(len(im)):
imagebox = OffsetImage(im[imageindex], zoom=.2, cmap="Greys", norm=plt.Normalize(0,255))
ab = AnnotationBbox(imagebox,(embedded_array[randomlist[imageindex]][0][0], embedded_array[randomlist[imageindex]][0][1]), xycoords='data', pad=0.0)
ax.add_artist(ab)
plt.savefig('isomap_embedding_space.png')