Step-by-Step PCA Face Recognition with PaddlePaddle
This article walks through using PaddlePaddle's linear algebra API to vectorize face images, load the ORL dataset, implement PCA for dimensionality reduction, and evaluate a simple face‑recognition classifier, providing full code, installation steps, and experimental results.
Principal Component Analysis (PCA) is a dimensionality‑reduction technique that finds the most important features in high‑dimensional data while preserving most of the information. The article demonstrates how to use PaddlePaddle’s efficient linear‑algebra API to implement PCA for a face‑recognition project.
Dataset and Installation
The ORL face dataset (40 subjects, 10 images each) is downloaded from
http://www.cl.cam.ac.uk/Research/DTG/attarchive/pub/data/att_faces.tar.Z. Required libraries are installed via:
pip install opencv-python pip install paddleImage Vectorization
Each grayscale image is read with OpenCV and reshaped into a 1‑row tensor:
import cv2
import paddle
def img2vector(image):
img = cv2.imread(image, 0)
imgVector = paddle.reshape(paddle.to_tensor(img, dtype='float32'), [1, -1])
return imgVectorDataset Loader
A custom ORLDataset class loads the images, splits them into training and test sets, and returns Paddle tensors:
import os
import numpy as np
class ORLDataset:
def __init__(self, data_path, k, train=True):
self.data_path = data_path
self.k = k
self.train = train
def load_orl(self):
train_images, train_labels = [], []
test_images, test_labels = [], []
sample = np.random.permutation(10) + 1
for i in range(40):
person_id = i + 1
for j in range(10):
image_path = os.path.join(self.data_path, 's' + str(person_id), str(sample[j]) + '.pgm')
img = img2vector(image_path)
if j < self.k:
train_images.append(img)
train_labels.append(person_id)
else:
test_images.append(img)
test_labels.append(person_id)
if self.train:
return paddle.concat(train_images, axis=0), paddle.to_tensor(train_labels, dtype='int64')
else:
return paddle.concat(test_images, axis=0), paddle.to_tensor(test_labels, dtype='int64')PCA Implementation
The PCA function follows these steps:
Cast data to float32 and compute the mean.
Center the data by subtracting the mean.
Compute the covariance matrix C = A * Aᵀ.
Obtain eigenvalues and eigenvectors with paddle.linalg.eigh.
Select the top r eigenvectors and normalize them.
Project the centered data onto the selected eigenvectors to get the reduced‑dimensional representation.
def PCA(data, r):
data = paddle.cast(data, 'float32')
rows, cols = data.shape
data_mean = paddle.mean(data, axis=0)
A = data - paddle.tile(data_mean, repeat_times=[rows, 1])
C = paddle.matmul(A, A, transpose_y=True)
eig_vals, eig_vects = paddle.linalg.eigh(C)
eig_vects = paddle.matmul(A.T, eig_vects[:, :r])
for i in range(r):
eig_vects[:, i] = eig_vects[:, i] / paddle.norm(eig_vects[:, i])
final_data = paddle.matmul(A, eig_vects)
return final_data, data_mean, eig_vectsTraining and Testing
The script trains a simple nearest‑neighbor classifier by randomly selecting 7 images per person for training and evaluating on the remaining 3 images. It repeats the process for target dimensions 10, 20, 30, and 40, printing classification accuracies:
def face_recognize(data_path):
for r in range(10, 41, 10):
print(f"When reduced to {r} dimensions:")
train_set = ORLDataset(data_path, k=7, train=True)
test_set = ORLDataset(data_path, k=7, train=False)
train_data, train_labels = train_set.load_orl()
test_data, test_labels = test_set.load_orl()
train_proj, data_mean, V_r = PCA(train_data, r)
test_centered = test_data - data_mean
test_proj = paddle.matmul(test_centered, V_r)
correct = 0
for i in range(len(test_labels)):
diff = train_proj - test_proj[i]
dists = paddle.sum(paddle.square(diff), axis=1)
nearest = paddle.argsort(dists)[0]
if train_labels[nearest] == test_labels[i]:
correct += 1
accuracy = correct / len(test_labels)
print(f"Accuracy with 7 training images per person: {accuracy:.2%}")Results
10‑dimensional reduction: 67.5% accuracy
20‑dimensional reduction: 35.0% accuracy
30‑dimensional reduction: 67.5% accuracy
40‑dimensional reduction: 40.0% accuracy
Conclusion
PaddlePaddle’s paddle.linalg API provides powerful tools for data reduction and feature extraction, enabling efficient implementation of PCA and related techniques such as SVD. These capabilities are valuable for machine‑learning tasks like face recognition, data compression, and feature selection, helping to reduce computational cost and improve model generalization.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
