aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornunzip <np.scarh@gmail.com>2018-10-30 14:36:31 +0000
committerVasil Zlatanov <v@skozl.com>2018-10-30 14:38:26 +0000
commit9842746888fee666d416ee90150defc58dfab8ea (patch)
treecbcce01d7835293e7e6ede34991b774d602de107
parentb5dee391a29e05a22e3c4eb62f6801c297869428 (diff)
downloadvz215_np1915-9842746888fee666d416ee90150defc58dfab8ea.tar.gz
vz215_np1915-9842746888fee666d416ee90150defc58dfab8ea.tar.bz2
vz215_np1915-9842746888fee666d416ee90150defc58dfab8ea.zip
Fix Reduced PCA and face Reconstruction
-rwxr-xr-xtrain.py20
1 files changed, 13 insertions, 7 deletions
diff --git a/train.py b/train.py
index 0927943..fd03220 100755
--- a/train.py
+++ b/train.py
@@ -69,7 +69,6 @@ parser.add_argument("-q", "--pca_r", help="Use Reduced PCA", action='store_true'
args = parser.parse_args()
-
M = args.eigen
raw_faces = genfromtxt(args.data, delimiter=',')
@@ -83,6 +82,7 @@ faces_train, faces_test, target_train, target_test = test_split(n_faces, raw_fac
sc = StandardScaler()
#faces_train = sc.fit_transform(faces_train)
#faces_test = sc.transform(faces_test)
+raw_faces_train = faces_train
explained_variances = ()
@@ -93,9 +93,12 @@ if args.pca or args.pca_r:
faces_train = normalise_faces(average_face, faces_train)
faces_test = normalise_faces(average_face, faces_test)
if (args.pca_r):
- e_vals, e_vecs = LA.eigh(np.cov(faces_train))
+ print('Reduced PCA')
+ e_vals, e_vecs = LA.eigh(np.dot(faces_train, faces_train.T))
e_vecs = np.dot(faces_train.T, e_vecs)
+ e_vecs = e_vecs/LA.norm(e_vecs, axis = 0)
else:
+ print('Standard PCA')
e_vals, e_vecs = LA.eigh(np.cov(faces_train.T))
# e_vecs = normalise_faces(np.mean(e_vecs,axis=0), e_vecs)
# e_vecs = sc.fit_transform(e_vecs)
@@ -108,12 +111,15 @@ if args.pca or args.pca_r:
faces_test = np.dot(faces_test, e_vecs.T)
if (args.reconstruct):
- for face in range(args.reconstruct):
- rec_vec = np.add(average_face, np.dot(faces_train[face], e_vecs) * deviations)
- ar = plt.subplot(2, args.reconstruct/2, face + 1)
- ar.imshow(rec_vec.reshape([46,56]).T, cmap = 'gist_gray')
+ rec_vec = np.add(average_face, np.dot(faces_train[args.reconstruct], e_vecs) * deviations)
+ rec_error = LA.norm(np.subtract(raw_faces_train[args.reconstruct], rec_vec))
+ print(rec_error)
+ ar = plt.subplot(2, 1, 1)
+ ar.imshow(rec_vec.reshape([46,56]).T, cmap = 'gist_gray')
+ ar = plt.subplot(2, 1, 2)
+ ar.imshow(raw_faces_train[args.reconstruct].reshape([46,56]).T, cmap = 'gist_gray')
plt.show()
-
+
if args.lda or (args.pca and args.lda):
lda = LinearDiscriminantAnalysis(n_components=M)
faces_train = lda.fit_transform(faces_train, target_train)