-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec7.py
96 lines (76 loc) · 2.55 KB
/
exec7.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
import os
import numpy as np
from numpy import linalg as lala
import pylab
from PIL import Image
import matplotlib.pyplot as plt
FOLDER = os.path.join('..','..', "Machine Learning for Computer Security", "Exercises", "mlsec-exer07-pca", "images")
data = []
mean = np.zeros((1,676))
counter = 0
for filename in os.listdir(FOLDER):
# print(filename)
file = os.path.join(FOLDER, filename)
pixels = []
with Image.open(file) as image:
for x in range(image.size[0]):
for y in range(image.size[1]):
if image.mode == 'L':
b = image.getpixel((y,x)) # black channel only
pixels.append(b)
else:
r, g, b = image.getpixel((y,x)) # red, green, blue channels
pixels.append(r)
# end if
# end for
# end for
# end with
img = np.array(pixels)
mean += img
counter += 1
data.append(img)
# end for
mean /= counter # calculating average
plt.imshow(np.resize(data[0], (26, 26)), cmap=plt.cm.gray)
plt.title('original image #1')
plt.show()
plt.imshow(np.resize(mean, (26, 26)), cmap=plt.cm.gray)
plt.title('mean')
plt.show()
diff = np.zeros((213, 676))
for i in range(len(data)):
diff[i] = data[i].astype('float64') - mean
# end for
plt.imshow(np.resize(diff[0], (26, 26)), cmap=plt.cm.gray)
plt.title('diff image #1')
plt.show()
layout = pylab.GridSpec(213+1, 4)
fig = plt.figure()
subplt = fig.add_subplot(layout[0, 0])
subplt.set_title('#')
subplt = fig.add_subplot(layout[0, 1])
subplt.set_title('o')
subplt = fig.add_subplot(layout[0, 2])
subplt.set_title('m')
subplt = fig.add_subplot(layout[0, 3])
subplt.set_title('d')
for i in range(len(data))[::50]:
subplt = fig.add_subplot(layout[i + 1, 0])
subplt.set_title('#{}'.format(i))
subplt = fig.add_subplot(layout[i+1, 1])
subplt.imshow(np.resize(data[i], (26, 26)), cmap=plt.cm.gray)
subplt = fig.add_subplot(layout[i+1, 2])
subplt.imshow(np.resize(mean, (26, 26)), cmap=plt.cm.gray)
subplt = fig.add_subplot(layout[i+1, 3])
subplt.imshow(np.resize(diff[i], (26, 26)), cmap=plt.cm.gray)
# end def
plt.show()
convergence = np.cov(diff.T)
plt.imshow(np.resize(convergence[0], (26, 26)), cmap=plt.cm.gray)
plt.title('convergence #{}'.format(1))
plt.show()
eigenvalue, eigenvector = lala.eigh(convergence) # Hermitian or symmetric matrix. The latter is true.
for i in range(len(eigenvector))[:3]:
plt.imshow(np.resize(eigenvector[i], (26, 26)), cmap=plt.cm.gray)
plt.title('eigenvector #{}'.format(i))
plt.show()