LPIPS: a perceptual distance
Why pixel error fails to compare two images, and how LPIPS compares them through normalized features of a frozen network, with weights fitted to human judgements.
Pairs, not sets
The topic Precision and recall for generative models split FID's single number in two: are the samples realistic, and is the data covered? Both answers, like FID itself, describe sets of images. Neither says how close one particular image is to another.
Many everyday questions are about exactly such a pair.
- Reconstruction. The autoencoder of Latent diffusion and the VQGAN tokenizer of Autoregressive transformers must return an image close to the one they encoded.
- Restoration and editing. A super-resolved photo should match the sharp original. An edit should leave everything it did not touch alone.
- Variety. A model that turns one sketch into many shoes should give shoes that differ from each other.
- Copies. The FID topic warned that a model returning its training images scores well. Catching that needs a nearest-neighbor search, and a search needs a distance between two images.
All four need a function that is small when a person would call two images alike and large when a person would not. This topic builds one.
Pixels mislead
The obvious candidate is the mean squared error over pixels. For two images with values,
Here is a small test. Take an image of vertical stripes with period 4, so every row reads 0 0 1 1 0 0 1 1.
- Shift it one pixel to the right, wrapping around the edge. Every row becomes
1 0 0 1 1 0 0 1. Half of the pixels flip from to or back, so . A person sees the same stripes. - Blur it along each row with the kernel , again wrapping. Every pixel moves by exactly toward grey. A person sees a visibly worse image.
Pixel MSE ranks the blurred copy far closer to the original than the shifted one. People rank them the other way round.
This is not a quirk of one example. Squared error rewards averages (see GANs as a two-player game). When several sharp images are plausible, their mean has the smallest expected error, and the mean of sharp edges in slightly different places is a blur. A model trained or judged by MSE learns that blur is safe.
Features on a grid
FID already showed the way out: compare images through the features of a pretrained network, not through pixels. LPIPS, the Learned Perceptual Image Patch Similarity (Zhang et al., 2018), does this for a single pair.
Both images go through the same frozen convolutional network, AlexNet or VGG trained on ImageNet classification (convolutions are covered in Batch normalization, gradients and convolutions). From several layers, five for AlexNet, we take the feature map
that is, channels on an grid. At each position of the grid sits a vector that describes the patch of the image this layer sees there.
Early layers respond to edges and colors in small patches. Deeper layers respond to textures and object parts, on coarser grids. A one-pixel shift moves a deep map by a fraction of a grid cell, so it barely changes. A blur wipes out the edges that many channels respond to, so it changes them a lot.
There is one difference from FID. FID pooled the last layer into one vector per image and compared clouds of many images. LPIPS keeps the grid of every layer and compares two images position by position.
The LPIPS formula
Comparing at a position starts with normalization across channels. Each feature vector is divided by its length:
Only the direction survives: which channels fire and in what proportion, not how strongly overall. The reference code adds to the length so that a zero vector stays zero.
Next, the normalized vectors of the two images are subtracted, each channel is multiplied by a weight, the squared length is taken, and the result is averaged over the grid. The weights are non-negative, one per channel:
Finally, the layers are added up:
Each piece has a job. Normalization makes positions comparable, whatever the scale of their activations. Spatial averaging puts layers with different grid sizes on the same footing. Skip it, and you get a map of where the two images differ. The weights pick out the channels that matter to people. They are the only learned part of LPIPS, and the network itself stays frozen.
One layer by hand
Let us run one toy layer by hand. It has channels on a grid, and weights . The four positions hold these feature vectors.
| Position | ||
|---|---|---|
| A | ||
| B | ||
| C | ||
| D |
Position A, step by step. Normalization gives and . The difference is . The weights turn it into . Its squared length is .
Note that the 3 and the 2 played no part: only the directions did. The weight made the disagreement in channel 2 count for less than the one in channel 1.
Fitted to people
The weights are fitted to people. The data set is BAPPS (Berkeley-Adobe Perceptual Patch Similarity). Each item is a triplet: a reference patch and two distorted versions of it. People answer one question: which version is closer to the reference? This format is called a two-alternative forced choice, 2AFC. The distortions include noise, blur, JPEG, color shifts and the outputs of neural networks. The test sets add real algorithm outputs: super-resolution, frame interpolation, deblurring, colorization.
The network stays frozen. A small head turns the two distances, reference to version 1 and reference to version 2, into the probability that people pick version 1. It is trained with cross-entropy against the share of people who actually did.
To score a metric on held-out triplets: if a share of people picked version 1 and the metric also says version 1 is closer, the metric scores on that triplet; otherwise it scores . The average over triplets is its agreement with people. People disagree with each other, so even a person does not reach . A person is scored the same way, as if a random rater were the metric and the rest of the crowd the judges.
| Metric | Agreement with people |
|---|---|
| L2 on pixels | |
| SSIM, a classic hand-made similarity index | |
| Deep features of supervised networks, all weights | – |
| LPIPS on AlexNet, learned weights | |
| One person |
On the two test sets with the same kinds of distortion as in training, AlexNet goes from unweighted to with learned weights. On the four sets of real algorithm outputs, from to .
Where LPIPS is used
LPIPS shows up wherever a model must produce one specific image, or two images must be told apart.
Reconstruction and restoration. Papers report LPIPS next to MSE or PSNR. MSE forgives blur; LPIPS punishes it.
A training loss. LPIPS is differentiable, so it can replace or join MSE in the loss. Such perceptual losses go back to Johnson, Alahi and Fei-Fei (2016). The autoencoder of Latent diffusion and VQGAN train with a perceptual loss of this kind plus a patch discriminator. That is why their reconstructions stay sharp where a VAE's turn blurry.
Variety. Draw several outputs for the same input, such as one sketch or one map, and average LPIPS over all pairs of them (Zhu et al., 2017). A value near zero means the model ignores its noise and draws the same picture every time: mode collapse for a conditional model. For scale, compute the same average over random pairs of real images.
Copies. For a generated image, find its nearest neighbor in the training set under LPIPS. A very small distance flags a candidate copy to look at. It is not proof of copying, and an empty search is not an audit either. Report which set you searched and with what threshold.
Where LPIPS breaks
LPIPS is a network's opinion, and it inherits a network's weak spots.
- Adversarial fragility. Changes too small for a person to see can move LPIPS a lot (Kettunen, Härkönen and Lehtinen, 2019). A generator trained only to minimize LPIPS may find such directions instead of better images. Keep a second metric and look at the samples.
- Partial shift tolerance. Early layers still see the pixel grid, so LPIPS reacts to misalignments smaller than a pixel (Ghildyal and Liu, 2022).
- Local view. It was fitted on patches. On a whole image it averages local judgements, and global composition, such as two eyes in the right places, lies outside its view.
- Conventions. The number depends on the network (AlexNet or VGG), the version of the weights and the input range, which is for the reference package. Scores are comparable only under the same settings.
- Cost. Every comparison is a forward pass through a deep network. A nearest-neighbor search over millions of training images adds up.
Step back and look at the whole evaluation part of the course. Inception Score, FID, precision and recall, LPIPS: each one is a network standing in for a person. LPIPS went furthest and was fitted to people's choices, yet only on small patches and only up to how much people agree with each other. The final judge of a generated image is a person. The next topic, Human evaluation, asks how to ask people well: how to design the study, how to turn pairwise preferences into a ranking and how far to trust the answers.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.