Skip to content
BIASED

Precision and recall for generative models

How to split the fidelity of samples from their diversity — precision and recall via nearest-neighbour balls, density and coverage, and where these metrics break.

One number, two failures

The topic Fréchet Inception Distance warned that FID mixes two failures in one number. Here is how completely it can mix them.

Let the real features be a 2-D standard Gaussian, P=N(0,I)P = \mathcal N(0, I), and let a model draw from Qs=N(0,s2I)Q_s = \mathcal N(0, s^2 I). The means agree, and (ΣrΣg)1/2=sI(\Sigma_r \Sigma_g)^{1/2} = sI, so the FID formula reduces to

FID=Tr⁡(I+s2I−2sI)=2(1−s)2.\mathrm{FID} = \operatorname{Tr}\big(I + s^2 I - 2sI\big) = 2(1 - s)^2 .

Take s=0.3s = 0.3 and s=1.7s = 1.7. Both give 2⋅0.72=0.982 \cdot 0.7^2 = 0.98. Yet the two models fail in opposite ways.

The collapsed model, s=0.3s = 0.3, puts about 99% of its samples within radius 0.90.9 of the center. Each of them is a perfectly plausible real point. But that disc holds only a third of the real data, and the rest is never drawn. High fidelity, poor diversity.

The spread-out model, s=1.7s = 1.7, reaches every region of the data. But half of its samples land beyond radius 22, where only 14% of the real data lives. Good diversity, poor fidelity.

We want two numbers instead of one, one for each failure.

Where the mass lands

The names come from search engines. Precision: of the results you returned, how many are relevant? Recall: of the relevant documents, how many did you return?

For distributions, Sajjadi et al. (2018) ask the same two questions about supports. The support supp⁡P\operatorname{supp} P is the region where PP puts its mass. Then

precision=Q(supp⁡P),recall=P(supp⁡Q).\text{precision} = Q(\operatorname{supp} P), \qquad \text{recall} = P(\operatorname{supp} Q).

Precision is the share of the model's samples that land where real data occurs. Recall is the share of the real data that lands where the model generates.

For our Gaussians we must read "support" loosely, as the region that holds most of the mass. A Gaussian's literal support is the whole plane, and both numbers would be 11 for every model. Read loosely, the collapsed model has precision near 11 and recall near a third.

The definitions become exact in a discrete picture. Split the images into clusters, and each distribution turns into a vector of cluster probabilities. Sajjadi et al. cluster the features into 20 clusters with k-means and work in exactly this picture.

Balls around neighbours

Features have 2048 dimensions, and we know neither support. Kynkäänniemi et al. (2019) estimate them from the samples themselves, as the topic Fréchet Inception Distance briefly sketched. Here are the details.

Write x1,…,xNrx_1, \dots, x_{N_r} for the real features and y1,…,yNgy_1, \dots, y_{N_g} for the generated ones. For a real point, let rk(xi)r_k(x_i) be the distance to its kk-th nearest neighbour among the other real points. The union of balls

M^r=⋃i=1NrB(xi, rk(xi))\widehat{\mathcal M}_r = \bigcup_{i=1}^{N_r} B\big(x_i,\, r_k(x_i)\big)

is the estimated real manifold, a stand-in for supp⁡P\operatorname{supp} P. The radius adapts to the data: crowded regions get small balls, sparse regions get large ones. The generated manifold M^g\widehat{\mathcal M}_g is built the same way from the yjy_j.

Precision counts samples inside the real manifold. Recall counts real points inside the generated one:

precision=1Ng∑j=1Ng1[yj∈M^r],recall=1Nr∑i=1Nr1[xi∈M^g].\text{precision} = \frac{1}{N_g} \sum_{j=1}^{N_g} \mathbb 1\big[y_j \in \widehat{\mathcal M}_r\big], \qquad \text{recall} = \frac{1}{N_r} \sum_{i=1}^{N_r} \mathbb 1\big[x_i \in \widehat{\mathcal M}_g\big].

The paper uses k=3k = 3, 50 00050\,000 images per side and features of VGG-16, another frozen ImageNet classifier. Any fixed feature network works, as with FID. The number kk is a knob: it decides how generously the balls bridge the gaps between samples.

Precision and recall by hand

Let us compute both numbers by hand. One dimension, k=1k = 1, four points per side:

X={0, 1, 2, 3},Y={0.2, 0.8, 1.1, 7}.X = \{0,\ 1,\ 2,\ 3\}, \qquad Y = \{0.2,\ 0.8,\ 1.1,\ 7\}.

Precision. Every real point's nearest real neighbour is 11 away, so each real ball has radius 11. Together they cover [−1,4][-1, 4]. The samples 0.20.2, 0.80.8 and 1.11.1 fall inside; 77 does not. Precision is 3/43/4.

Recall. Now the balls sit around the generated points, with radii from the generated set:

Sample Nearest other sample Radius Ball
0.20.2 0.80.8 0.60.6 [−0.4, 0.8][-0.4,\ 0.8]
0.80.8 1.11.1 0.30.3 [0.5, 1.1][0.5,\ 1.1]
1.11.1 0.80.8 0.30.3 [0.8, 1.4][0.8,\ 1.4]
77 1.11.1 5.95.9 [1.1, 12.9][1.1,\ 12.9]

Recall is the share of real points 0,1,2,30, 1, 2, 3 that fall into at least one of these balls. Count them yourself.

Density and coverage

In the example, 77 is the worst sample, yet it raised recall from 1/21/2 to 11. Its huge ball swallowed the real points 22 and 33. It works the other way too: a real outlier gets a huge ball, and bad samples inside it count toward precision. Unions of balls trust every point, outliers included.

Naeem et al. (2020) keep only the real balls. We choose the real set, so we can trust it more than the model's output. Write Bi=B(xi,rk(xi))B_i = B\big(x_i, r_k(x_i)\big).

Density counts how many real balls contain each sample:

density=1kNg∑j=1Ng∑i=1Nr1[yj∈Bi].\text{density} = \frac{1}{k N_g} \sum_{j=1}^{N_g} \sum_{i=1}^{N_r} \mathbb 1\big[y_j \in B_i\big].

A sample in a crowded real region counts several times; an isolated one counts zero times. For a perfect model, density is 11 on average in any dimension. A fresh sample lands in BiB_i when it is closer to xix_i than xix_i's kk-th real neighbour, which happens with probability k/Nrk / N_r. Density can exceed 11 when samples pile up in dense regions.

Coverage asks, for every real point, whether its own ball caught at least one sample:

coverage=1Nr∑i=1Nr1[∃j: yj∈Bi].\text{coverage} = \frac{1}{N_r} \sum_{i=1}^{N_r} \mathbb 1\big[\exists j:\ y_j \in B_i\big].

A sample far from the data lands in no real ball, so it cannot raise coverage. In the example the ball of the real point 33 is [2,4][2, 4], it catches nothing, and coverage is 3/43/4.

Even a perfect model does not reach coverage 11. Pool the Nr−1N_r - 1 other real points with the NgN_g samples and sort them by distance to xix_i. Then xix_i stays uncovered exactly when its kk nearest pooled points are all real. For a perfect model every point comes from the same distribution, so every order is equally likely. With Nr=NgN_r = N_g large, each nearest point is real with probability about 1/21/2, and coverage is about 1−2−k1 - 2^{-k}. The paper takes k=5k = 5, which gives about 0.970.97.

The knob between precision and recall

Back to the two Gaussians. We take 20002000 points per side, k=3k = 3 for precision and recall, k=5k = 5 for density and coverage, and average 20 runs:

Model FID Precision Recall Density Coverage
Collapsed, s=0.3s = 0.3 0.980.98 0.980.98 0.490.49 1.001.00 0.360.36
Matched, s=1s = 1 00 0.980.98 0.980.98 1.001.00 0.970.97
Spread out, s=1.7s = 1.7 0.980.98 0.900.90 0.980.98 0.850.85 0.890.89

The pairs separate the two models that FID could not. Even the matched model scores a bit below 11 on precision, recall and coverage: the samples are finite.

Many generators have a knob that slides along this table. In Latent diffusion, a larger guidance scale ww traded diversity for fidelity. A VAE can sample with temperature τ<1\tau < 1, drawing z∼N(0,τ2I)z \sim \mathcal N(0, \tau^2 I). Large GANs use the truncation trick: draw z∼N(0,I)z \sim \mathcal N(0, I), but redraw every coordinate whose magnitude exceeds a threshold. Turned toward "safe", each knob moves the model from s=1s = 1 toward s=0.3s = 0.3.

So report the pair, or a curve along the knob, never one average of the two. Which end matters depends on the use. A tool that makes stock photos needs precision. A generator of training data for another model needs recall.

Where the balls fail

Now take a perfect model, Q=P=N(0,Id)Q = P = \mathcal N(0, I_d), with 20002000 points per side and k=3k = 3. Precision and recall come out at about 0.980.98 for d=2d = 2, 0.80.8 for d=16d = 16 and 0.60.6 for d=64d = 64. Density and coverage stay near 11 and 0.970.97. Real features have hundreds or thousands of dimensions.

The cause is distance concentration. For two independent points of N(0,Id)\mathcal N(0, I_d), ∥x−y∥2\|x - y\|^2 is a sum of dd independent terms, with mean 2d2d and standard deviation 8d\sqrt{8d}. Its relative spread is

8d2d=2d.\frac{\sqrt{8d}}{2d} = \sqrt{\frac{2}{d}} .

It equals 11 for d=2d = 2 and 0.180.18 for d=64d = 64. All distances crowd around one value. Expand ∥xi−y∥2=∥xi∥2+∥y∥2−2 xi⊤y\|x_i - y\|^2 = \|x_i\|^2 + \|y\|^2 - 2\, x_i^\top y. The term ∥y∥2\|y\|^2 is the same for every ball. A sample a little farther from the center than usual is a little too far from every real point and misses all balls at once. A sample a little closer lands in many. Precision counts each sample once, so the misses cost it. Density counts every ball, so the extra hits make up for the misses.

The fine print from FID applies here too. All four numbers depend on kk, on the sample size and on the feature network, so compare them only under the same settings. The cost is quadratic: 50 00050\,000 points per side means 2.5⋅1092.5 \cdot 10^9 distances.

Every metric so far scores whole sets of images. None says whether one reconstruction is close to its original, or whether two samples for the same prompt really differ. For that we need a distance between two single images that agrees with a human eye. Pixels fail, as a one-pixel shift showed in Fréchet Inception Distance. The next topic builds such a distance from deep features and calibrates it on human judgements: LPIPS.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.