Nearest neighbor face recognition in eigenspace when using dot product of test set with eigenvectors does not match the performance when using sklearn
I am trying to perform Face recognition using PCA (eigenfaces). I have a set of N
training images (of dimensions M=wxh), which I have pre-processed into a vertical stack of grayscale intensity vectors, a matrix of dimensions NxM. For the facial recognition, I am finding the single nearest neighbour of each test image in both the high-dimensional pixel space and the lower dimensional eigenspace. I am using NearestNeighbor
classifier from sklearn
. For recognition in the eigenspace, I am contrasting different usages of Python's sklearn
module's PCA
classifier to better understand it.
Here is the common setup:
pca = PCA(num_components=k)
pca.fit(X_train)
Here are the regimes:
- Use PCA to transform both the training and test sets
X_train_pca = pca.transform(X_train)
X_test_pca = pca.transform(X_test)
- Use PCA to transform the training, and perform the dot product with the eigenvectors (components) to transform the test set
X_train_pca = pca.transform(X_train)
X_test_pca = np.dot(X_test, pca.components_.T)
- Perform the dot product with the eigenvectors (components) to transform both the tthe test set
X_train_pca = np.dot(X_train, pca.components_.T)
X_test_pca = np.dot(X_test, pca.components_.T)
In all cases, the code to test the resulting transformed representation and perform the matching is as follows:
neigh = NearestNeighbors(n_neighbors=1)
neigh.fit(X_train_pca)
correct_subject_count = 0
for i in range(X_test_pca.shape[0]):
test_image = X_test_pca[i]
test_label = y_test[i]
_, neigh_ind = neigh.kneighbors([test_image])
neigh_ind = neigh_ind[0][0]
predicted_label = y_train[neigh_ind]
if test_label == predicted_label:
correct_subject_count += 1
print(float(correct_subject_count) / X_test.shape[0])
The same code is used for the matching in the pixel space (on the untransformed data), and yields an accuracy of 77%.
Here are the accuracies for each regime:
- 78%
- 0.04%
- 25%
My question is why is there a discrepancy between each regime? Is projecting a new test image (in this case the entire test matrix) onto the eigenvectors not the same exact thing as performing the dot product, which is the same exact thing as transforming the dataset using the fitted pca
classifier? Is there a mistake in the math of performing the dot product?
Topic k-nn linear-algebra pca computer-vision dimensionality-reduction
Category Data Science