Skip to content

Latest commit

 

History

History
218 lines (167 loc) · 6.64 KB

File metadata and controls

218 lines (167 loc) · 6.64 KB

Part 1 Explained: Eigenfaces and PCA for Face Recognition

High-Level Overview

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.

Key Concept

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.


Workflow Diagram

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]
Loading

Step-by-Step Explanation

1. Loading Images

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)

2. Data Representation

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

3. Mathematical Foundation

Mean Face Calculation

The mean face represents the average appearance:

μ = (1/N) Σ xᵢ

where N is the number of images and xᵢ is each face vector.

Centering the Data

Φᵢ = xᵢ - μ

Subtracting the mean centers the data around the origin, which is necessary for PCA.

Covariance Matrix

C = (1/N) Σ Φᵢ Φᵢᵀ

The covariance matrix captures the variance and correlations between different pixel positions.

Eigenfaces

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)

Visual Interpretation

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
Loading

What are Eigenfaces?

  • 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

Key Mathematical Concepts for Oral Exam

1. Principal Component Analysis (PCA)

  • Purpose: Dimensionality reduction while preserving maximum variance
  • Method: Find orthogonal directions (principal components) of maximum variance
  • Output: Eigenfaces ordered by importance (eigenvalue magnitude)

2. Dimensionality Reduction

  • 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

3. Face Recognition Process

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
Loading

Steps:

  1. Subtract mean face from new image
  2. Project onto eigenface space: w = vᵀ Φ
  3. Calculate distance to all known faces
  4. Return closest match (minimum Euclidean distance)

Advantages and Limitations

✅ Advantages

  • Fast: Once eigenfaces are computed, recognition is quick
  • Efficient: Stores faces in compressed form
  • Robust: Works well with controlled lighting and pose

❌ Limitations

  • Lighting Sensitive: Performance degrades with varying illumination
  • Pose Dependent: Requires frontal face images
  • Global Method: Uses entire face region, not local features

Code Flow Diagram

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
Loading

Key Takeaways for Oral Exam

🎯 Core Concepts

  1. Eigenfaces are eigenvectors of the face covariance matrix
  2. PCA finds directions of maximum variance in the data
  3. Dimensionality reduction enables efficient face recognition
  4. Mean face represents the average appearance

📊 Mathematical Understanding

  • 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)

💡 Practical Applications

  • Face recognition systems
  • Data compression
  • Feature extraction
  • Noise reduction

🔍 Why It Works

  • 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

Quick Reference: Key Equations

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

Dataset Information

  • 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

Summary

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.