How to implement threshold in PCA for face recognition?

4 views (last 30 days)
Hello, i'm now working with PCA for recognition. i get confused to get and implement threshold in PCA, can someone help me?

Answers (1)

TED MOSBY
TED MOSBY on 12 Oct 2024
Hi Alvindra,
Principal Component Analysis (PCA) is a dimensionality reduction technique used in face recognition. Setting a threshold in PCA is important for decision-making processes like classification or anomaly detection. I have written some basic steps for you to get started on implementing threshold in PCA:
  • Preprocess the data: Collect face images and convert them into a suitable format (grayscale, normalized, etc). Flatten the images into 1D vectors if they are 2D matrices.
  • Compute the mean face, then center the data
  • Calculate covariance variance, then perform eigen decomposition
  • Select principal components: Sort the eigenvectors by their corresponding eigenvalues in descending order. Choose the top k eigenvectors where k is determined by a threshold on the cumulative explained variance. For example, you might choose enough components to explain 95% of the variance.
  • Project the centered data onto the selected eigenvectors to obtain the PCA-transformed data.
  • Calculate Reconstruction Error: For each data point, calculate the reconstruction error. This is the difference between the original data point and its reconstruction from the PCA components. Reconstruction error can be calculated using the Euclidean distance or another appropriate metric.
  • There are some methods to determine the threshold:
Empirical Method: Analyze the distribution of reconstruction errors for known samples (e.g., training dataset) and set
a threshold that separates classes effectively.
Statistical Method: Use statistical measures (e.g., mean ± k*standard deviation) to set a threshold.
Cross-validation: Use cross-validation to determine a threshold that maximizes performance metrics like accuracy,
precision, or recall.
  • Compare the reconstruction error of a new sample against the threshold. If the error is below the threshold, classify the sample as recognized; otherwise, classify it as unrecognized or anomalous.
Below I have written an example MATLAB pseudocode of the above-mentioned steps:
% Load and preprocess face data
faces = loadFaceData(); % Function to load and preprocess data - your own written function
% Compute mean face
meanFace = mean(faces, 2);
% Center the data
centeredFaces = faces - meanFace;
% Compute covariance matrix
covMatrix = cov(centeredFaces');
% Eigen decomposition
[eigenvectors, eigenvalues] = eig(covMatrix);
% Sort eigenvectors by eigenvalues
[~, sortedIndices] = sort(diag(eigenvalues), 'descend');
eigenvectors = eigenvectors(:, sortedIndices);
% Choose number of components based on variance threshold
explainedVariance = cumsum(diag(eigenvalues(sortedIndices))) / sum(diag(eigenvalues));
k = find(explainedVariance >= 0.95, 1); % 95% variance threshold
% Project data onto principal components
projectedFaces = eigenvectors(:, 1:k)' * centeredFaces;
% Recognition process
testFace = loadTestFace(); % Load a test face - your own written function
centeredTestFace = testFace - meanFace;
projectedTestFace = eigenvectors(:, 1:k)' * centeredTestFace;
% Calculate distances and apply threshold
distances = vecnorm(projectedFaces - projectedTestFace, 2, 1);
recognitionThreshold = 10; % Example threshold
isRecognized = any(distances < recognitionThreshold);
Refer to the PCA documentation here:
Hope this helps!

Categories

Find more on Dimensionality Reduction and Feature Extraction in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!