This notebook demonstrates Eigenfaces, a classic face recognition technique that uses Principal Component Analysis (PCA) to reduce the dimensionality of face images. The method treats face images as high-dimensional vectors and finds the principal components (eigenfaces) that best represent the variation in the dataset.
Eigenfaces are the eigenvectors of the covariance matrix of face images. They represent the directions of maximum variance in the face space and can be used to reconstruct and recognize faces with reduced dimensionality.
flowchart TD
A[Load Face Images] --> B[Convert to Grayscale]
B --> C[Flatten Images to Vectors]
C --> D[Calculate Mean Face]
D --> E[Center Data by Subtracting Mean]
E --> F[Compute Covariance Matrix]
F --> G[Calculate Eigenvalues \u0026 Eigenvectors]
G --> H[Sort Eigenfaces by Eigenvalue]
H --> I[Select Top K Eigenfaces]
I --> J[Project Faces to Lower Dimension]
J --> K[Face Recognition/Reconstruction]
faces = load_images('./Data/Dataset/att_faces/'+imageSet)- Loads face images from the specified directory
- Each image is 112×92 pixels
- Dataset contains 10 images per person
- Images are converted to grayscale (luminance)
Dataset shape: (10, 112, 92)- 10 images of size 112×92 pixels
- Each image is treated as a vector of length 112 × 92 = 10,304 dimensions
- The face recognition problem becomes a high-dimensional pattern recognition task
The mean face represents the average appearance:
μ = (1/N) Σ xᵢ
where N is the number of images and xᵢ is each face vector.
Φᵢ = xᵢ - μ
Subtracting the mean centers the data around the origin, which is necessary for PCA.
C = (1/N) Σ Φᵢ Φᵢᵀ
The covariance matrix captures the variance and correlations between different pixel positions.
The eigenfaces are the eigenvectors of the covariance matrix:
C vᵢ = λᵢ vᵢ
where:
- vᵢ are the eigenfaces (eigenvectors)
- λᵢ are the eigenvalues (representing variance along each eigenface)
graph LR
A[Original Face Space\n10,304 dimensions] --PCA--> B[Eigenface Space\nK dimensions]
B --Reconstruction--> C[Reconstructed Face]
style A fill:#e1f5ff
style B fill:#fff4e1
style C fill:#e8f5e9
- Eigenfaces look like ghostly face images that capture common facial features
- The first few eigenfaces capture the most variance (most important features)
- Higher-order eigenfaces capture finer details and noise
- Purpose: Dimensionality reduction while preserving maximum variance
- Method: Find orthogonal directions (principal components) of maximum variance
- Output: Eigenfaces ordered by importance (eigenvalue magnitude)
- Original: Each face is 10,304-dimensional vector
- Reduced: Can be represented using K eigenfaces (typically K = 10-100)
- Benefit: Faster computation, noise reduction, better generalization
flowchart LR
A[New Face] --> B[Subtract Mean]
B --> C[Project onto Eigenfaces]
C --> D[Compare with Database]
D --> E[Find Closest Match]
style A fill:#ffebee
style E fill:#c8e6c9
Steps:
- Subtract mean face from new image
- Project onto eigenface space:
w = vᵀ Φ - Calculate distance to all known faces
- Return closest match (minimum Euclidean distance)
- Fast: Once eigenfaces are computed, recognition is quick
- Efficient: Stores faces in compressed form
- Robust: Works well with controlled lighting and pose
- Lighting Sensitive: Performance degrades with varying illumination
- Pose Dependent: Requires frontal face images
- Global Method: Uses entire face region, not local features
sequenceDiagram
participant User
participant LoadImages
participant Numpy
participant PCA
User->>LoadImages: Specify image set
LoadImages->>Numpy: Convert to arrays
Numpy->>PCA: Flatten images
PCA->>PCA: Compute mean face
PCA->>PCA: Center data
PCA->>PCA: Compute covariance
PCA->>PCA: Calculate eigenvectors
PCA->>User: Return eigenfaces
- Eigenfaces are eigenvectors of the face covariance matrix
- PCA finds directions of maximum variance in the data
- Dimensionality reduction enables efficient face recognition
- Mean face represents the average appearance
- Know how to compute: mean face, centered data, covariance matrix
- Understand: eigenvalues represent variance, eigenvectors are directions
- Can explain: why we use only top K eigenfaces (they capture most variance)
- Face recognition systems
- Data compression
- Feature extraction
- Noise reduction
- Faces are similar: Most variation captured in low-dimensional space
- Eigenfaces are basis vectors: Any face can be approximated as linear combination
- Top eigenfaces: Capture the most significant facial features
| Concept | Equation | Meaning |
|---|---|---|
| Mean Face | μ = (1/N) Σ xᵢ | Average of all faces |
| Centered Data | Φᵢ = xᵢ - μ | Remove mean from each face |
| Covariance | C = (1/N) Σ Φᵢ Φᵢᵀ | Variance-covariance matrix |
| Eigenfaces | C vᵢ = λᵢ vᵢ | Principal components |
| Projection | wᵢ = vᵢᵀ Φ | Coordinates in eigenface space |
- Source: AT\u0026T Face Database (formerly ORL)
- Images per person: 10
- Image size: 112 × 92 pixels
- Format: Grayscale (8-bit)
- Total dimensions: 10,304 per image
Eigenfaces is a powerful technique that demonstrates how linear algebra and statistics come together in machine learning. By treating faces as high-dimensional vectors and using PCA, we can efficiently recognize faces using only a small number of principal components (eigenfaces). This technique laid the foundation for modern face recognition systems.