def toyExample(): mat = scipy.io.loadmat('../data/toy_data.mat') data = mat['toy_data'] # TODO: Train PCA pca = PCA(-1) pca.train(data) print("Variance of the data") # TODO 1.2: Compute data variance to the S vector computed by the PCA data_variance = np.var(data, axis=1) print(data_variance) print(np.power(pca.S, 2) / data.shape[1]) # TODO 1.3: Compute data variance for the projected data (into 1D) to the S vector computed by the PCA Xout = pca.project(data, 1) print("Variance of the projected data") data_variance = np.var(Xout, axis=1) print(data_variance) print(np.power(pca.S[0], 2) / data.shape[1]) plt.figure() plt.title('PCA plot') plt.subplot(1, 2, 1) # Visualize given data and principal components # TODO 1.1: Plot original data (hint, use the plot_pca function pca.plot_pca(data) plt.subplot(1, 2, 2) # TODO 1.3: Plot data projected into 1 dimension pca.S[1] = 0 pca.plot_pca(Xout) plt.show()
gallery_labels = labels[gallery_idxs - 1] # num_of_clusters_arr = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000] num_of_clusters = 700 n = 10 # Compute K-Means baseline solution print("-----Baseline K-Means-----") compute_k_mean(num_of_clusters, query_features, gallery_features, gallery_labels) # Compute PCA result print("\n-----PCA------") pca = PCA(original_train_features, M=500) pca.fit() pca_query_features = pca.project(query_features) pca_gallery_features = pca.project(gallery_features) compute_k_mean(num_of_clusters, pca_query_features, pca_gallery_features, gallery_labels) # Compute LMNN (Large Margin Nearest Neighbour) Learning print("\n-----LMNN------") lmnn = LMNN(k=5, max_iter=20, use_pca=False, convergence_tol=1e-6, learn_rate=1e-6, verbose=True) lmnn.fit(original_train_features, original_train_labels) transformed_query_features = lmnn.transform(query_features) transformed_gallery_features = lmnn.transform(gallery_features)
def faceRecognition(): ''' Train PCA with with 25 components Project each face from 'novel' into PCA space to obtain feature coordinates Find closest face in 'gallery' according to: - Euclidean distance - Mahalanobis distance Redo with different PCA dimensionality What is the effect of reducing the dimensionality? What is the effect of different similarity measures? ''' numOfPrincipalComponents = 25 # TODO: Train a PCA on the provided face images pca = PCA(numOfPrincipalComponents) (X, data_labels, gall_faces) = data_matrix() pca.train(X) alphagal = pca.to_pca(X) # TODO: Plot the variance of each principal component - use a simple plt.plot() f = plt.figure(1) plt.plot(np.var(alphagal, 1, dtype=np.float64)) f.show() # TODO: Implement face recognition (novel, novel_labels, nov_faces) = load_novel() alphanov = pca.to_pca(novel) matches_e = [] matches_m = [] invS = np.diag(1. / pca.S) for i in range(alphanov.shape[1]): lowest_e = (sys.maxsize, 0) lowest_m = (sys.maxsize, 0) for j in range(alphagal.shape[1]): euclidean = euclideanDistance(alphanov[:, i], alphagal[:, j]) if euclidean < lowest_e[0]: lowest_e = euclidean, j maha = mahalanobisDistance(alphanov[:, i], alphagal[:, j], invS) if maha < lowest_m[0]: lowest_m = maha, j matches_e.append((i, lowest_e[1])) matches_m.append((i, lowest_m[1])) print(matches_e) print(matches_m) correct_m = 0 correct_e = 0 correct_classified = 0 correct_partner = 0 wrong_classified = 0 wrong_partner = 0 for x in range(len(matches_e)): if data_labels[matches_e[x][0]] == novel_labels[matches_e[x][1]]: correct_e += 1 correct_classified = x correct_partner = matches_e[x][1] else: wrong_classified = x wrong_partner = matches_e[x][1] if data_labels[matches_m[x][0]] == novel_labels[matches_m[x][1]]: correct_m += 1 print("Correct Euclidian Classification in percent: {}".format( correct_e / len(matches_e) * 100)) print("Correct Mahalanobis Classification in percent: {}".format( correct_m / len(matches_m) * 100)) # TODO: Visualize some of the correctly and wrongly classified images (see example in exercise sheet) # Show correct classified fig = plt.figure(2) columns = 3 rows = 2 titles = ("correct test", "wrong test", "projected test", "correct train", "wrong train", "projected train") # plt.title(titles[i]) # correct sub = fig.add_subplot(rows, columns, 1) sub.set_title("Novel to test") sub.set_ylabel("Correct Classified") plt.imshow(nov_faces.item(correct_classified)[1], cmap='gray') sub = fig.add_subplot(rows, columns, 2) sub.set_title("Closest from training") plt.imshow(gall_faces.item(correct_partner)[1], cmap='gray') sub = fig.add_subplot(rows, columns, 3) sub.set_title("Novel projected") correct_projected = pca.project( novel, numOfPrincipalComponents)[:, correct_classified].reshape( gall_faces.item(correct_partner)[1].shape) plt.imshow(correct_projected, cmap='gray') # wrong sub = fig.add_subplot(rows, columns, 4) sub.set_title("Novel to test") sub.set_ylabel("Wrong Classified") plt.imshow(nov_faces.item(wrong_classified)[1], cmap='gray') sub = fig.add_subplot(rows, columns, 5) sub.set_title("Closest from training") plt.imshow(gall_faces.item(wrong_partner)[1], cmap='gray') sub = fig.add_subplot(rows, columns, 6) sub.set_title("Novel projected") wrong_projected = pca.project( novel, numOfPrincipalComponents)[:, wrong_classified].reshape( gall_faces.item(wrong_partner)[1].shape) plt.imshow(wrong_projected, cmap='gray') fig.show()