Scoring restored images: PSNR, SSIM and LPIPS
Three ways to compare a restored image with its reference — pixel error in decibels, local structure, learned perceptual distance — the settings each hides, and why none of them says an image is correct.
What runs in CodeSplash
- EVA051PSNR, SSIM, and LPIPS image fidelity diagnostics Needs a data type CodeSplash doesn't load yet
You will be able to
- Compute PSNR from mean squared error and state its data range
- Explain what SSIM compares in each window
- Say what LPIPS measures and what it depends on
- Choose between full-reference scores and distribution scores such as FID
Comparing with a reference
ReadDenoising, deblurring, compression and super-resolution all have a right answer: the clean original. Full-reference metrics compare an output with that original, pixel-aligned. That makes them exact and cheap, and also narrow: they need the reference, they need alignment, and they say how close the output is, not whether it is useful or true.
Three are standard, each answering a different question.
PSNR: pixel error in decibels
ReadPSNRPeak signal-to-noise ratio (PSNR)10·log10(peak² / mean squared error), in decibels: higher means closer pixel values. It ignores how errors look. is 10·log10(peak² / MSE), where MSE is the mean squared pixel error and peak is the largest possible value. In the program, three pixel values from 0 to 1 are each off by a little; the result is 23.01 dB. Every halving of the MSE adds about 3 dB.
The peak must match the data: 1 for values scaled to 0–1, 255 for 8-bit images. Using the wrong one shifts every score by about 48 dB, so always state the data range with the number.
import numpy as np
x = np.array([0.0, 0.5, 1.0]) # reference, scaled to 0-1
y = np.array([0.05, 0.45, 0.9]) # restored
mse = np.mean((x - y) ** 2)
print(10 * np.log10(1.0 ** 2 / mse)) # peak 1 for 0-1 data
print(10 * np.log10(255.0 ** 2 / mse)) # the same images, wrong peakx <- c(0, 0.5, 1) # reference, scaled to 0-1
y <- c(0.05, 0.45, 0.9) # restored
mse <- mean((x - y)^2)
print(10 * log10(1^2 / mse)) # peak 1 for 0-1 data
print(10 * log10(255^2 / mse)) # the same images, wrong peakMove it one pixel
PredictPredict first
You shift a sharp photo one pixel to the right and compare it with the original. It looks identical to you. What happens to its PSNR?
Reveal
It drops sharply. Every edge now disagrees with the reference at the pixel level. PSNR has no idea the image merely moved; full-reference scores need careful alignment first.
SSIM: structure in windows
ReadSSIMStructural similarity (SSIM)Compares local brightness, contrast and structure in small windows; 1 means identical. Closer to perception than PSNR. slides a small window over both images and compares three things inside it: mean brightness, contrast (spread) and structure (correlation). It averages the result over all windows; 1 means identical.
Because it looks at local patterns rather than single pixels, SSIM tracks visible damage better than PSNR: blur and blockiness lower it more than a slight uniform brightening. But the window size, its weighting, the data range and how color is handled all change the value, and libraries differ. Report the settings, or compare only numbers from the same code.
LPIPS: distance a person would see
ReadLPIPSLPIPSA distance between two images measured in a pretrained network's features, tuned to match human similarity judgments. passes both images through a pretrained network, compares their features layer by layer, and combines the differences with weights fitted to human judgments of which of two distortions looks closer to the original. Lower is closer.
It agrees with people better than PSNR or SSIM, especially on textures and GAN outputs. It is also a network: results depend on the backbone (AlexNet or VGG) and version, and images unlike its training data can fool it. It still needs aligned pairs.
Pairs or distributions?
ReadGenerated images have no reference: a new face has no original. Then compare distributions instead: FIDFréchet inception distance (FID)Distance between Gaussians fit to real and generated images' features; lower is closer, and it depends on the feature network. measures how far the feature statistics of many generated images are from those of many real ones. It needs thousands of images and says nothing about any single one.
DExplorable · Scoring generators: FID, precision and recall
Can one number say how good generated samples are?
How will this generator's precision and recall come out?
Generator
The real data is a ring of 8 clusters. Each generator gets something different wrong.
Feature extractor
FID and the k-NN scores compare features, not raw samples. Real FID uses a vision network; here, four simple maps.
How many real and how many generated samples are compared.
Each sample's ball reaches its k-th nearest neighbor in its own set.
Commit your prediction above to see the result
Make your prediction to see the result.
Check: image fidelity
CheckpointCheck yourself
Images are scaled to 0–1 and the mean squared error is 0.001. What is the PSNR in dB?
Show the answer
Answer: 30
10 · log10(1 / 0.001) = 10 · 3 = 30 dB.
Check yourself
Paper A reports PSNR 32 dB on 8-bit images; your code reports 32 dB on images scaled to 0–1 but with peak 255. Are the numbers comparable?
Show the answer
Answer: b
PSNR's peak must be the data's maximum. Mixing ranges is one of the most common errors in published comparisons.
Check yourself
What does SSIM compare inside each window?
Show the answer
Answer: a
The three terms are combined per window and averaged, which makes SSIM sensitive to blur and blockiness.
Check yourself
You want to score a model that generates new product photos from text. Which metric fits?
Show the answer
Answer: c
PSNR, SSIM and LPIPS need an aligned original. Generated images need distribution scores and checks of prompt adherence.
Check yourself
A restored medical scan has excellent LPIPS against the original in your test set. What does that tell you about scans in clinical use?
Show the answer
Answer: b
Fidelity scores measure closeness under one network's view, not whether clinically important detail is right.
Recap
Recap- PSNR = 10·log10(peak²/MSE): state the data range; it punishes misalignment and favors blur.
- SSIM compares brightness, contrast and structure in windows; its settings change the number.
- LPIPS compares pretrained features weighted to human judgments; it depends on its network.
- No reference, no full-reference metric: use distribution scores like FID and human review for generation.
Concepts in this lesson