From 9842746888fee666d416ee90150defc58dfab8ea Mon Sep 17 00:00:00 2001 From: nunzip Date: Tue, 30 Oct 2018 14:36:31 +0000 Subject: Fix Reduced PCA and face Reconstruction --- train.py | 20 +++++++++++++------- 1 file 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) -- cgit v1.2.3-54-g00ecf