diff options
| -rwxr-xr-x | train.py | 20 | 
1 files changed, 13 insertions, 7 deletions
| @@ -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) | 
