18 / 33 · Evaluation
Fréchet Inception Distance
FID compares real and generated images through the mean and covariance of their Inception features. It is the squared 2-Wasserstein distance between two fitted Gaussians.
Inception Score · Multivariate Gaussian distributions · Wasserstein GANs
On this page 11
The Inception Score looks only at generated images: it asks whether a classifier is confident on each image and whether the predicted classes are diverse. It never checks whether the images resemble the training data. The Fréchet Inception Distance (FID) of Heusel et al. (2017) closes that gap. It passes real and generated images through the same pretrained Inception network, summarises each set of features by a mean vector and a covariance matrix, and measures how far apart the two summaries are.
Notation: the subscript marks real images and marks generated ones. is the Inception feature vector of an image , and is the Euclidean norm. As in the Gaussian note, mean vectors and matrices are set in bold: is a mean vector, a covariance matrix and the identity matrix.
The formula#
- are the average feature vectors of the real and generated images.
- are their covariance matrices.
This is the definition of Heusel et al. (2017), Eq. (6). With real images (and the same for the generated set), the two summaries are the usual sample estimates:
In effect we approximate each feature distribution by a Gaussian, for real images and for generated ones, and compare the two Gaussians. Heusel et al. motivate this choice by maximum entropy (Heusel et al., 2017, text before Eq. 6). Among all densities on with a given mean and covariance, the Gaussian has the largest differential entropy (Cover and Thomas, 2006, Theorem 8.6.5). It is therefore the least committal distribution consistent with the two moments that FID keeps: the fit adds no structure beyond them.
Take the embeddings before the softmax. The features are not the class probabilities that IS uses, but activations from inside the network. Heusel et al. use the last pooling layer of Inception v3 (Szegedy et al., 2016) as the coding layer. In their TTUR code this is the pool3 layer, which feeds the classification head: 2048 numbers per image (Figure 1). Class probabilities would discard most of what the network sees in an image, while the penultimate layer keeps a richer description than the 1000 class probabilities, although it is still shaped by the ImageNet classes (see Drawbacks).
How FID is computed
Reading the two terms#
- The mean term measures how far apart the centres of the two feature clouds are.
- The covariance term measures how differently the clouds are spread and oriented. It is never negative and vanishes only when . Both facts follow from the Wasserstein view below: with equal means the formula reduces to this term, so the term is the squared distance between and . is a metric (Peyré and Cuturi, 2019, Proposition 2.3), so it is non-negative and zero only for equal distributions, here for equal covariances.
So lower FID means closer Gaussian fits, that is, closer means and covariances of the two feature clouds. FID = 0 means equal means and equal covariances, not identical feature distributions (see Drawbacks).
The two terms of FID on a 2-D example
A one-dimensional check#
For scalar features, becomes a variance , the matrix square root is an ordinary square root, because standard deviations are non-negative, and the trace disappears:
The covariance term is the squared difference of the standard deviations. Take real features and generated features :
A generator whose samples are too spread out is penalised just like a shifted one. So is a generator that is too narrow. A model that produces nearly the same image every time (mode collapse) has , and the covariance term tends to even when the mean is right.
The same holds in dimensions for isotropic covariances and . Then , and each of the diagonal entries contributes , so the covariance term is . Figure 3 shows the case with , and a shift of the mean, so that .
FID as the generated Gaussian shifts and shrinks
The two-dimensional example in numbers#
Figure 2 uses
Both covariances are positive definite here (, , positive diagonals). The only non-trivial step is the matrix square root. The rest of this paragraph and the next display justify its closed form, ; this is optional mathematical depth, and a reader who takes the formula on trust can go straight to the table. The product is not symmetric. Because is invertible, it is similar to the symmetric positive-definite matrix :
So is diagonalisable with positive eigenvalues , and its principal square root has the same eigenvectors and eigenvalues . For a matrix this gives a closed form. Squaring the sum of the two roots gives , and the sum and product of the eigenvalues are the trace and the determinant:
| Quantity | Value |
|---|---|
| FID |
In practice the features are 2048-dimensional and the square root is computed numerically, for example with scipy.linalg.sqrtm. Unlike the symmetric root of the Gaussian note, is the principal square root of a non-symmetric matrix. In floating point it can come back with tiny imaginary parts, which implementations discard when they are negligible (TTUR raises an error otherwise). When the computed root is not finite because the product is nearly singular, they add a small to both covariances and compute the root again (see calculate_frechet_distance in the TTUR code).
FID is a Wasserstein distance between two Gaussians#
The 2-Wasserstein distance between distributions and on with finite second moments is
where is the set of transport plans with marginals and , as in the WGAN note; the finite second moments keep the infimum finite. It is the same family of transport distances as , with squared rather than plain Euclidean cost. For two Gaussians the infimum has a closed form (Dowson and Landau, 1982), and that closed form is the FID formula:
FID is the squared distance, so it has no outer square root. Maurice Fréchet studied this distance between probability laws (Fréchet, 1957), so it is also called the Fréchet distance; the “F” in FID refers to it.
Compared with the Inception Score#
Unlike IS, FID compares the generated images with real data.
| Inception Score | FID | |
|---|---|---|
| Aim | confidence and diversity of generated images | closeness of generated images to real ones |
| Data used | generated images only | real and generated images |
| What is compared | each with the marginal | Gaussian fit to real features vs. Gaussian fit to generated features |
| Distance | KL divergence | squared Fréchet (2-Wasserstein) distance |
| Better is | higher | lower |
Drawbacks#
- Gaussian approximation. Inception features are not normally distributed: the embeddings of a typical image set have many modes (Jayasumana et al., 2024, §3.3). Two feature distributions with the same mean and covariance but different shapes get FID 0, because only the first two moments are compared. Jayasumana et al. build such a pair from a Gaussian and a mixture of Gaussians with the same overall mean and covariance (§3.2, Table 2).
- Data unlike ImageNet. The network was trained on ImageNet photographs, so for data far from them, such as medical images, sketches or spectrograms, nothing guarantees that its features capture what makes an image good. One remedy is to compute the same distance in the features of an encoder trained on the domain (below). Even where FID is the standard metric the features stay tied to the ImageNet classes. On FFHQ faces, FID is most sensitive to regions outside the face, where the classifier recognises ImageNet objects such as bow ties, seat belts or mortarboards (Kynkäänniemi et al., 2023, §2, Figure 4). Resampling the generated images so that their ImageNet class statistics match the real ones lowers FID without a visible improvement in the images: by about 10% when only the Top-1 class histograms are matched, and by about two thirds on FFHQ (from 5.30 to 1.78) when the pool3 features themselves are matched (§3, Tables 1–2).
- One number for two properties. FID conflates how realistic the samples are with how much of the data’s variation they cover (Kynkäänniemi et al., 2023, §1). A worse score can come from unrealistic samples, which move and reshape the feature cloud, or from too little variety, which shrinks it (Figure 3), so two generators with the same FID can fail in different ways. Precision and recall metrics report the two properties separately (Sajjadi et al., 2018).
- Blind to memorization. Heusel et al. compute the real statistics over the whole training set. A generator that returns randomly drawn training images reproduces their mean and covariance up to sampling noise, so its FID is close to 0 although it has learned nothing beyond the data. The same weakness affects the Inception Score. Detecting copies needs a per-image comparison, such as a nearest-neighbour search in the training set under a perceptual distance like LPIPS.
- Needs thousands of images. A 2048 × 2048 covariance matrix has free entries. With images the sample covariance is singular (its rank is at most ), and the matrix square root becomes numerically unstable. With a few thousand it is still noisy. The estimate is biased upward, and the bias depends on the generator, so a fixed sample size reduces the problem but does not remove it: two generators evaluated with the same can be ranked by their bias rather than by their quality (Chong and Forsyth, 2020, §3.1, Figure 2). Chong and Forsyth extrapolate in to an effectively unbiased (§3.2). Heusel et al. used 50 000 generated images; 10 000 or 50 000 are common.
- Sensitive to preprocessing. Resizing to the network’s input size (the resizing filter and its antialiasing), 8-bit quantisation and JPEG compression all change the features, and FIDs computed with different pipelines are not comparable (Parmar, Zhang and Zhu, 2022, §3).
Over which images is FID computed?#
Standard FID is computed once, over all generated and all real images, not per class and not per pair of images. We pool every real image into one feature cloud and every generated image into another, fit one Gaussian to each, and compare the two Gaussians. FID needs no class labels, and no generated image is paired with a particular real image. (Class-conditional generators are sometimes scored with intra-FID, an FID computed separately within each class, between the generated and the real images of that class (Miyato and Koyama, 2018, §5.1), but that is a separate variant.) Because FID compares distributions rather than pairs, it needs the large samples described above. A pairwise, per-image comparison is what LPIPS does.
FID with a custom encoder#
Nothing in the formula depends on Inception: it needs a feature vector per image, and any encoder can supply one. For data unlike ImageNet, an encoder trained on the domain itself is the natural choice, for example the encoder of an autoencoder trained on that data; Bińkowski et al. (2018), §4, score MNIST models with the features of a LeNet-like MNIST classifier instead of Inception. The price is comparability. A distance computed with one encoder can be compared only with distances computed with the same encoder, the same layer and the same preprocessing. Strictly, the result is no longer FID, since the I stands for Inception: it is a Fréchet distance in the features of the named encoder and layer. Standard FID means the Inception protocol, and a custom-encoder value must never be compared with published FIDs. Below, “FID” inside formulas is shorthand for this feature distance.
Distances between classes as a check. Before trusting a new encoder, apply it to real images only. Split a labelled dataset by class, fit a Gaussian to the features of each class, and compute the Fréchet distance between every pair of classes. For classes this gives a matrix with three properties to check:
- Zero diagonal. A class compared with itself has equal means and covariances, so both terms vanish. The numerical matrix square root leaves small values of either sign, so a check needs a small tolerance.
- Symmetry. is a metric, so .
- Semantic order. If the features carry meaning, similar classes are closer than dissimilar ones. On CIFAR-10 one expects and .
The sample size matters here more than for standard FID. CIFAR-10 has 1000 test images per class, so each class covariance is estimated from 1000 vectors and has rank at most . An encoder with a code of 1000 numbers or more therefore gives singular covariances, and the matrix square root becomes unstable (Needs thousands of images). Adding to both covariances, with a small such as , keeps the computation stable at the cost of a small bias; a smaller code avoids the problem.
The same encoder can also be applied to images it was not trained on, for example two classes of photographs from another dataset. The result is then a distance in features learned on the training domain, which may miss what separates the new classes: the same limitation as Inception features on data unlike ImageNet, now in the other direction.
Kernel Inception Distance: an unbiased alternative#
The Kernel Inception Distance (KID) of Bińkowski et al. (2018), §4, keeps the Inception features but drops the Gaussian fit. It is the squared maximum mean discrepancy (MMD) between the real and the generated features with the cubic polynomial kernel
where is the feature dimension ( for pool3). For distributions and of features,
with and independent. Expanding the cube, is a sum of the inner products , , with positive weights, and is the inner product of the -fold tensor products of and . So is a weighted sum of the squared differences between the moments of and of orders 1 to 3, and it is zero exactly when these moments agree. FID compares the first two moments; KID also compares the third moments, skewness among them, and assumes no parametric form.
With features of real and of generated images, KID is estimated by
Leaving out the terms makes each average an unbiased estimate of its expectation (Gretton et al., 2012, Lemma 6), so is unbiased for any sample size. This is its main advantage over FID, whose estimate is biased upward, by an amount that depends on the generator (Needs thousands of images); no unbiased estimator of FID exists (Bińkowski et al., Appendix D.3). Comparing the CIFAR-10 training and test sets, two samples of the same distribution, the KID estimate is centred on at every sample size shown, while the FID estimate is still about with all 10 000 test images (Bińkowski et al., §4, Figure 1). The price of unbiasedness is that the estimate can be slightly negative when the two distributions are close.
The double sums cost for images per set, against for FID’s covariances and matrix square root. Bińkowski et al. therefore recommend averaging the estimate over several subsets of moderate size: their experiments average 100 subsets of 1000 images (§5). Like FID, KID depends on what the ImageNet-trained features capture, and lower is better.
Notebook#
The metrics notebook of the HSE-LAMBDA Deep Generative Models repository (pinned commit; cell numbers count every cell, markdown included, from 1) describes FID in cell 5: features from intermediate layers of a pretrained network, “often Inception v3”, a Gaussian fitted to the real and to the generated features (written and there), and the Fréchet distance between them. Standard FID uses a single layer, the 2048-dimensional pool3 output described above. Cell 6 compares IS, FID and LPIPS in a table; the IS-versus-FID table above is based on it. The metrics notebook itself does not compute FID. KID appears in the repository only by name, in the closing discussion of the GAN notebook (cell 35), next to IS and FID.
A Fréchet feature distance, called FID there, is computed in Homework 1 of the same repository (same cell convention), with the encoder of an autoencoder trained on CIFAR-10 in place of Inception (the autoencoder part is described in the autoencoder note). The notebook has no saved outputs and its solution cells are empty, so it documents a procedure only.
- Cells 35–38: the FID formula, and
get_representations, which maps each class of the validation set to the list of its 1000 encoder outputs. - Cell 39:
calculate_fid(repr1, repr2, eps=1e-6), to be written by the student; the defaultepsplays the role of the added to the covariances above. - Cells 40–41: the class-to-class FID matrix and its heatmap. The checks: all entries above , and . The matrix is computed in full, although symmetry would halve the work and the diagonal is known.
- Cell 42: FID between two classes of any other dataset (cats and dogs, for example) with the same CIFAR-trained encoder.
Next#
IS, FID and KID score a whole set of images. Sometimes we need a distance between two individual images that agrees with human perception, for example to compare a generated or restored image with its reference, or to train a network with a perceptual loss. That is LPIPS.
References#
- M. Heusel, H. Ramsauer, T. Unterthiner, B. Nessler, S. Hochreiter. GANs trained by a two time-scale update rule converge to a local Nash equilibrium. NeurIPS, 2017. Introduces FID: Experiments section, paragraph “Performance Measure” (maximum-entropy motivation and Eq. (6)); paragraph “Model Selection and Evaluation” (last pooling layer as coding layer, real statistics over the whole training set, 50 000 generated images). Code:
fid.pyin the TTUR repository. - D. C. Dowson, B. V. Landau. The Fréchet distance between multivariate normal distributions. Journal of Multivariate Analysis, 12(3), pp. 450–455, 1982. Closed form of the Fréchet () distance between two Gaussians.
- M. Fréchet. Sur la distance de deux lois de probabilité. Comptes rendus hebdomadaires des séances de l’Académie des sciences, Paris, 244, pp. 689–692, 1957. The volume is digitised in the Gallica collection of the Comptes rendus (1957, tome 244, first part).
- T. M. Cover, J. A. Thomas. Elements of Information Theory, 2nd ed. Wiley, 2006. Theorem 8.6.5: the Gaussian maximises differential entropy for a given covariance.
- G. Peyré, M. Cuturi. Computational optimal transport. Foundations and Trends in Machine Learning, 11(5–6), 2019. Proposition 2.3: is a distance.
- C. Szegedy, V. Vanhoucke, S. Ioffe, J. Shlens, Z. Wojna. Rethinking the Inception architecture for computer vision. CVPR, 2016.
- M. J. Chong, D. Forsyth. Effectively unbiased FID and Inception Score and where to find them. CVPR, 2020. §3.1 and Figure 2: the bias of depends on the generator; §3.2: extrapolation to .
- G. Parmar, R. Zhang, J.-Y. Zhu. On aliased resizing and surprising subtleties in GAN evaluation. CVPR, 2022. §3.2 (resizing), §3.3 (quantisation and compression).
- S. Jayasumana, S. Ramalingam, A. Veit, D. Glasner, A. Chakrabarti, S. Kumar. Rethinking FID: Towards a better evaluation metric for image generation. CVPR, 2024. §3.2–3.3: the normality assumption and why it fails.
- T. Kynkäänniemi, T. Karras, M. Aittala, T. Aila, J. Lehtinen. The role of ImageNet classes in Fréchet Inception Distance. ICLR, 2023. §1: FID conflates fidelity and variation; §2 and Figure 4: what FID is sensitive to in FFHQ images; §3, Tables 1–2: resampling to match ImageNet class statistics lowers FID.
- M. S. M. Sajjadi, O. Bachem, M. Lucic, O. Bousquet, S. Gelly. Assessing generative models via precision and recall. NeurIPS, 2018.
- T. Miyato, M. Koyama. cGANs with projection discriminator. ICLR, 2018. §5.1: intra-FID.
- M. Bińkowski, D. J. Sutherland, M. Arbel, A. Gretton. Demystifying MMD GANs. ICLR, 2018. §4: the Kernel Inception Distance, its cubic kernel and unbiased estimator, Figure 1 (bias of the FID estimate on CIFAR-10), footnote 12 (cost), and LeNet features for MNIST; §5: KID averaged over 100 subsets of 1000 images; Appendix D.3: no unbiased estimator of FID exists.
- A. Gretton, K. M. Borgwardt, M. J. Rasch, B. Schölkopf, A. Smola. A kernel two-sample test. JMLR, 13, pp. 723–773, 2012. Lemma 6: the squared MMD and its unbiased estimate.
- HSE-LAMBDA, Deep Generative Models course repository:
metrics.ipynb,GANs.ipynband01_hw_AE-and-FID.ipynb.