PCA Color Augmentation

Hard
Computer Vision

Implement the PCA color distortion technique used in AlexNet for data augmentation. This method applies PCA to the RGB pixel values of natural images, then adds multiples of the principal components with magnitudes proportional to the corresponding eigenvalues times a random variable. Given an RGB image (H, W, 3), compute the principal components of RGB values and apply the color distortion with specified alpha values.

Examples

Example 1:
Input: np.random.seed(42) image = np.random.randint(0, 256, (2, 2, 3)).astype(np.uint8) alpha = np.array([0.1, -0.05, 0.03]) result = pca_color_augmentation(image, alpha) print(result)
Output: [[[ 97 98 99] [144 145 146]] [[243 244 245] [ 32 33 34]]]
Explanation: The function computes PCA on the RGB pixel values, then applies a color distortion based on the principal components weighted by alpha values. The distortion is added to all pixels and the result is clamped to [0, 255].

Starter Code

import numpy as np

def pca_color_augmentation(image: np.ndarray, alpha: np.ndarray) -> np.ndarray:
    """
    Apply PCA color augmentation to an RGB image.
    
    Args:
        image: RGB image of shape (H, W, 3) with values in [0, 255]
        alpha: Array of 3 random coefficients for principal components
    
    Returns:
        Augmented image of shape (H, W, 3) with values clamped to [0, 255]
    """
    # Your code here
    pass
Lines: 1Characters: 0
Ready
The AI Interview - Master AI/ML Interviews