PCA Image Compression With PSNR Report

Quick Overview

Compress a stack of grayscale images with PCA in NumPy: count the stored values to pick the largest k that meets a target compression ratio, reconstruct the images and report PSNR. It tests SVD-based PCA, sign conventions, storage arithmetic and how the discarded spectrum sets the reconstruction error.

PCA Image Compression With PSNR Report

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Given a grayscale stack `images` of shape `(n, h, w)`, flatten each image to `d = h*w` pixels, compress with PCA to meet `target_ratio`, reconstruct, and report quality. Implement these six functions: ```python def compression_ratio(n_images, height, width, k): ... def choose_k(n_images, height, width, target_ratio): ... def compress_images(images, target_ratio): ... def decompress_images(compressed): ... def psnr(original, reconstructed, max_val=255.0): ... def compress_report(images, target_ratio, max_val=255.0): ... ``` Requirements: - `compression_ratio` returns `n*d / (d + k*d + n*k)` as a float (mean + components + scores stored). - `choose_k` returns the largest `k` in `[1, min(n, d)]` with ratio `>= target_ratio`. If the target is unreachable, it raises `ValueError`. - `compress_images` returns a dict with keys `mean` `(d,)`, `components` `(k, d)` orthonormal, `scores` `(n, k)`, `shape` `(h, w)`, `k` (int). Non-3D input raises `ValueError`. - Sign convention: each component's largest-magnitude entry is positive. `decompress_images` returns `scores @ components + mean` reshaped to `(n, h, w)`. - `psnr` = `10*log10(max_val**2 / MSE)` over all pixels, `inf` if MSE is 0. `compress_report` returns `{"k", "ratio", "psnr"}`. For example: ```python compression_ratio(10, 4, 4, 2) # 160 / 68 ≈ 2.353 choose_k(12, 8, 8, 2.0) # 4 (k=4 → 2.087, k=5 → 1.730) choose_k(12, 8, 8, 0.01) # 12 (min(n, d), exact reconstruction up to rounding) choose_k(12, 8, 8, 100.0) # raises ValueError ``` ```hint How the ratio moves with k Look at how the stored-value count changes as `k` grows. That tells you whether `choose_k` can stop at the first `k` that fails. ``` ```hint Signs and scores The thin SVD of the mean-centred pixel matrix gives the components. A sign flip changes a component's scores too, so decide the signs before you project. ``` ### Constraints & Assumptions - `images` holds `n` grayscale images of the same size `h × w`, so `d = h*w`. - Only `numpy` is required. Reconstructions must match `sklearn` `PCA` (mean-centred, no whitening). ### What a Strong Answer Covers - The storage accounting behind the ratio, and a `choose_k` that uses the ratio's monotonic decrease in `k` and raises on an unreachable target. - Centring, the thin SVD, and the sign convention applied before the scores are computed. - A round trip between `compress_images` and `decompress_images` that matches scikit-learn, with validation of non-3D input. - PSNR over all pixels in floating point, including the zero-MSE case. - How the discarded singular values set the reconstruction error and the PSNR. ### Follow-up Questions - Why does the reconstruction MSE equal the discarded singular-value energy? - How would you pick `k` to meet a target PSNR instead of a target ratio? - How does PCA across the stack compare with a separate truncated SVD of each image? - What changes if the mean, components and scores are stored at lower precision, such as float16 or 8-bit integers?

Overview: Compress a stack of grayscale images with PCA in NumPy: count the stored values to pick the largest k that meets a target compression ratio, reconstruct the images and report PSNR. It tests SVD-based PCA, sign conventions, storage arithmetic and how the discarded spectrum sets the reconstruction error.

PracHub logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Given a grayscale stack images of shape (n, h, w), flatten each image to d = h*w pixels, compress with PCA to meet target_ratio, reconstruct, and report quality. Implement these six functions:

def compression_ratio(n_images, height, width, k): ...
def choose_k(n_images, height, width, target_ratio): ...
def compress_images(images, target_ratio): ...
def decompress_images(compressed): ...
def psnr(original, reconstructed, max_val=255.0): ...
def compress_report(images, target_ratio, max_val=255.0): ...

Requirements:

  • compression_ratio returns n*d / (d + k*d + n*k) as a float (mean + components + scores stored).
  • choose_k returns the largest k in [1, min(n, d)] with ratio >= target_ratio . If the target is unreachable, it raises ValueError .
  • compress_images returns a dict with keys mean (d,) , components (k, d) orthonormal, scores (n, k) , shape (h, w) , k (int). Non-3D input raises ValueError .
  • Sign convention: each component's largest-magnitude entry is positive. decompress_images returns scores @ components + mean reshaped to (n, h, w) .
  • psnr = 10*log10(max_val**2 / MSE) over all pixels, inf if MSE is 0. compress_report returns {"k", "ratio", "psnr"} .

For example:

compression_ratio(10, 4, 4, 2)   # 160 / 68 ≈ 2.353
choose_k(12, 8, 8, 2.0)          # 4  (k=4 → 2.087, k=5 → 1.730)
choose_k(12, 8, 8, 0.01)         # 12 (min(n, d), exact reconstruction up to rounding)
choose_k(12, 8, 8, 100.0)        # raises ValueError

Constraints & Assumptions

  • images holds n grayscale images of the same size h × w , so d = h*w .
  • Only numpy is required. Reconstructions must match sklearn PCA (mean-centred, no whitening).

What a Strong Answer Covers Guidance

  • The storage accounting behind the ratio, and a choose_k that uses the ratio's monotonic decrease in k and raises on an unreachable target.
  • Centring, the thin SVD, and the sign convention applied before the scores are computed.
  • A round trip between compress_images and decompress_images that matches scikit-learn, with validation of non-3D input.
  • PSNR over all pixels in floating point, including the zero-MSE case.
  • How the discarded singular values set the reconstruction error and the PSNR.

Follow-up Questions Guidance

  • Why does the reconstruction MSE equal the discarded singular-value energy?
  • How would you pick k to meet a target PSNR instead of a target ratio?
  • How does PCA across the stack compare with a separate truncated SVD of each image?
  • What changes if the mean, components and scores are stored at lower precision, such as float16 or 8-bit integers?