-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbai28_Principal_Component_Analysis.py
136 lines (117 loc) · 3.85 KB
/
bai28_Principal_Component_Analysis.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
# -*- coding: utf-8 -*-
"""
Created on Sun May 31 09:11:29 2020
@author: phamk
"""
'''
import numpy as np
from scipy import misc # for loading image
import imageio
path = 'E:/AI/example/YALE/unpadded' # path to the database
ids = range(1, 16) # 15 persons
states = ['centerlight', 'glasses', 'happy', 'leftlight',
'noglasses', 'normal', 'rightlight','sad',
'sleepy', 'surprised', 'wink' ]
prefix = 'subject'
surfix = '.pgm'
im = imageio.imread(fn)
h = 116 # hight
w = 98 # width
D = h * w
N = len(states)*15
X = np.zeros((D, N))
# collect all data
cnt = 0
for person_id in range(1, 16):
for state in states:
fn = path + prefix + str(person_id).zfill(2) + '.' + state + surfix
X[:, cnt] = misc.imread(fn).reshape(D)
cnt += 1
'''
import numpy as np
from scipy import misc # for loading image
import imageio
np.random.seed(1)
# filename structure
path = 'E:/AI/example/YALE/unpadded/' # path to the database
ids = range(1, 16) # 15 persons
states = ['centerlight', 'glasses', 'happy', 'leftlight',
'noglasses', 'normal', 'rightlight','sad',
'sleepy', 'surprised', 'wink' ]
prefix = 'subject'
surfix = '.pgm'
# data dimension
h = 116 # hight
w = 98 # width
D = h * w
N = len(states)*15
K = 100
# collect all data
X = np.zeros((D, N))
cnt = 0
for person_id in range(1, 16):
for state in states:
fn = path + prefix + str(person_id).zfill(2) + '.' + state + surfix
X[:, cnt] = imageio.imread(fn).reshape(D)
cnt += 1
# Doing PCA, note that each row is a datapoint
from sklearn.decomposition import PCA
pca = PCA(n_components=K) # K = 100
pca.fit(X.T)
# projection matrix
U = pca.components_.T
import matplotlib.pyplot as plt
for i in range(U.shape[1]):
plt.axis('off')
f1 = plt.imshow(U[:, i].reshape(116, 98), interpolation='nearest')
f1.axes.get_xaxis().set_visible(False)
f1.axes.get_yaxis().set_visible(False)
# f2 = plt.imshow(, interpolation='nearest' )
plt.gray()
fn = 'eigenface' + str(i).zfill(2) + '.png'
plt.savefig(fn, bbox_inches='tight', pad_inches=0)
# plt.show()
# See reconstruction of first 6 persons
for person_id in range(1, 7):
for state in ['centerlight']:
fn = path + prefix + str(person_id).zfill(2) + '.' + state + surfix
im = imageio.imread(fn)
plt.axis('off')
# plt.imshow(im, interpolation='nearest' )
f1 = plt.imshow(im, interpolation='nearest')
f1.axes.get_xaxis().set_visible(False)
f1.axes.get_yaxis().set_visible(False)
plt.gray()
fn = 'ori' + str(person_id).zfill(2) + '.png'
plt.savefig(fn, bbox_inches='tight', pad_inches=0)
plt.show()
# reshape and subtract mean, don't forget
x = im.reshape(D, 1) - pca.mean_.reshape(D, 1)
# encode
z = U.T.dot(x)
#decode
x_tilde = U.dot(z) + pca.mean_.reshape(D, 1)
# reshape to orginal dim
im_tilde = x_tilde.reshape(116, 98)
plt.axis('off')
# plt.imshow(im_tilde, interpolation='nearest' )
f1 = plt.imshow(im_tilde, interpolation='nearest')
f1.axes.get_xaxis().set_visible(False)
f1.axes.get_yaxis().set_visible(False)
plt.gray()
fn = 'res' + str(person_id).zfill(2) + '.png'
plt.savefig(fn, bbox_inches='tight', pad_inches=0)
plt.show()
cnt = 0
for person_id in [10]:
for ii, state in enumerate(states):
fn = path + prefix + str(person_id).zfill(2) + '.' + state + surfix
im = imageio.imread(fn)
f1 = plt.imshow(im, interpolation='nearest')
f1.axes.get_xaxis().set_visible(False)
f1.axes.get_yaxis().set_visible(False)
fn = 'ex' + str(ii).zfill(2) + '.png'
plt.axis('off')
plt.savefig(fn, bbox_inches='tight', pad_inches=0)
plt.show()
# cnt += 1