Course contents 18 / 33

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.

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 rr marks real images and gg marks generated ones. φ(x)\varphi(x) is the Inception feature vector of an image xx, and ∥⋅∥2\|\cdot\|_2 is the Euclidean norm. As in the Gaussian note, mean vectors and matrices are set in bold: μ\boldsymbol\mu is a mean vector, Σ\boldsymbol\Sigma a covariance matrix and I\mathbf I the identity matrix.

The formula#

FID=∥μr−μg∥22+Tr⁡ ⁣(Σr+Σg−2 (ΣrΣg)1/2)\begin{aligned} \mathrm{FID} &= \|\boldsymbol\mu_r - \boldsymbol\mu_g\|_2^2 \\ &\quad + \operatorname{Tr}\!\Big(\boldsymbol\Sigma_r + \boldsymbol\Sigma_g - 2\,(\boldsymbol\Sigma_r \boldsymbol\Sigma_g)^{1/2}\Big) \end{aligned}
  • μr,μg\boldsymbol\mu_r, \boldsymbol\mu_g are the average feature vectors of the real and generated images.
  • Σr,Σg\boldsymbol\Sigma_r, \boldsymbol\Sigma_g are their covariance matrices.

This is the definition of Heusel et al. (2017), Eq. (6). With NrN_r real images x1,…,xNrx_1, \dots, x_{N_r} (and the same for the generated set), the two summaries are the usual sample estimates:

μr=1Nr∑i=1Nrφ(xi),Σr=1Nr−1∑i=1Nr(φ(xi)−μr)(φ(xi)−μr)⊤.\begin{gathered} \boldsymbol\mu_r = \tfrac{1}{N_r}\sum_{i=1}^{N_r} \varphi(x_i), \\ \boldsymbol\Sigma_r = \tfrac{1}{N_r - 1}\sum_{i=1}^{N_r} (\varphi(x_i) - \boldsymbol\mu_r)(\varphi(x_i) - \boldsymbol\mu_r)^{\top}. \end{gathered}

In effect we approximate each feature distribution by a Gaussian, N(μr,Σr)\mathcal N(\boldsymbol\mu_r, \boldsymbol\Sigma_r) for real images and N(μg,Σg)\mathcal N(\boldsymbol\mu_g, \boldsymbol\Sigma_g) 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 Rd\mathbb R^d 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).

Figure

How FID is computed

FID pipelineReal images and generated images both enter one pretrained Inception v3 network. Its pool3 layer, before the softmax classifier, gives a 2048-dimensional embedding per image; the fully connected softmax head is crossed out as not used. The real embeddings form a red point cloud summarised by mean mu r and covariance Sigma r; the generated embeddings form a blue cloud summarised by mu g and Sigma g. Both summaries feed into FID, where lower means closer and zero means equal means and equal covariances.real imagesgenerated imagesInception v3pretrained, frozenpool3FC + softmaxreal featuresgenerated featuresfit a Gaussianμr, Σrμg, ΣgFIDlower = closer0 = same μ, Σtake embeddings here: before softmaxFID pipeline (stacked)Real images and generated images both enter one pretrained Inception v3 network. Its pool3 layer, before the softmax classifier, gives a 2048-dimensional embedding per image; the fully connected softmax head is crossed out as not used. The real embeddings form a red point cloud summarised by mu r and Sigma r; the generated embeddings form a blue cloud summarised by mu g and Sigma g. Both feed into FID, where lower means closer and zero means equal means and equal covariances.real imagesgenerated imagesInception v3pretrained, frozenpool3FC + softmaxembeddingsbefore softmaxfeaturesfit a Gaussianμr, Σrμg, ΣgFIDlower = closer0 = same μ and Σ
Both image sets go through the same frozen Inception network. The activations are taken before the softmax classification head (in the standard implementation, the 2048-dimensional pool3 layer), one Gaussian is fitted to each feature cloud, and FID compares the two Gaussians.

Reading the two terms#

  • The mean term ∥μr−μg∥22\|\boldsymbol\mu_r - \boldsymbol\mu_g\|_2^2 measures how far apart the centres of the two feature clouds are.
  • The covariance term Tr⁡(⋅)\operatorname{Tr}(\cdot) measures how differently the clouds are spread and oriented. It is never negative and vanishes only when Σr=Σg\boldsymbol\Sigma_r = \boldsymbol\Sigma_g. Both facts follow from the Wasserstein view below: with equal means the formula reduces to this term, so the term is the squared W2W_2 distance between N(0,Σr)\mathcal N(\mathbf 0, \boldsymbol\Sigma_r) and N(0,Σg)\mathcal N(\mathbf 0, \boldsymbol\Sigma_g). W2W_2 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).

Figure

The two terms of FID on a 2-D example

Real and generated feature clouds with their FID termsA red cloud of real features centred at the origin, tilted, and a smaller, rounder blue cloud of generated features centred at (1.2, 0.6). An amber arrow between the two means is labelled squared norm of mu r minus mu g, equal to 1.80. A callout pointing at both ellipse outlines is labelled covariance term, the trace, equal to 0.484. A summary panel adds them: FID equals 2.284.2 of the 2048 feature dimensionsreal, 𝒩(μr, Σr)generated,𝒩(μg, Σg)‖μr − μg‖² = 1.80covariance term Tr(…) = 0.484different spread and tiltThis examplemean term1.800Tr Σr1.800Tr Σg0.800−2 Tr[(ΣrΣg)½]−2.116cov. term0.484FID2.284Real and generated feature clouds with their FID termsA red cloud of real features centred at the origin, tilted, and a smaller, rounder blue cloud of generated features centred at (1.2, 0.6). An amber arrow between the two means is labelled squared norm of mu r minus mu g, equal to 1.80. A callout pointing at both ellipse outlines is labelled covariance term, the trace, equal to 0.484. A summary panel adds them: FID equals 2.284.2 of the 2048 feature dimensionsreal, 𝒩(μr, Σr)generated,𝒩(μg, Σg)‖μr − μg‖² = 1.80covariance term Tr(…) = 0.484different spread and tiltThis examplemean term1.800Tr Σr1.800Tr Σg0.800−2 Tr[(ΣrΣg)½]−2.116covariance term0.484FID2.284
Seeded samples (140 drawn from each Gaussian; the few outside the frame are not shown), with 2σ ellipses. The mean term measures how far apart the clouds sit; the covariance term measures how differently they are spread and oriented. Numbers use the true parameters: real mean (0, 0) and covariance [[1, 0.6], [0.6, 0.8]]; generated mean (1.2, 0.6) and covariance [[0.45, −0.1], [−0.1, 0.35]].

A one-dimensional check#

For scalar features, Σ\boldsymbol\Sigma becomes a variance σ2\sigma^2, the matrix square root is an ordinary square root, σr2σg2=σrσg\sqrt{\sigma_r^2\sigma_g^2} = \sigma_r\sigma_g because standard deviations are non-negative, and the trace disappears:

σr2+σg2−2σrσg=(σr−σg)2,FID1D=(μr−μg)2+(σr−σg)2.\begin{aligned} \sigma_r^2 + \sigma_g^2 - 2\sigma_r\sigma_g &= (\sigma_r - \sigma_g)^2 , \\ \mathrm{FID}_{1\text{D}} &= (\mu_r - \mu_g)^2 + (\sigma_r - \sigma_g)^2 . \end{aligned}

The covariance term is the squared difference of the standard deviations. Take real features N(0,1)\mathcal N(0, 1) and generated features N(0.5,22)\mathcal N(0.5, 2^2):

FID=(0−0.5)2+(1+4−2⋅1⋅2)=0.25+1=1.25.\begin{aligned} \mathrm{FID} &= (0 - 0.5)^2 + \big(1 + 4 - 2\cdot 1\cdot 2\big) \\ &= 0.25 + 1 = 1.25 . \end{aligned}

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 σg→0\sigma_g \to 0, and the covariance term tends to σr2\sigma_r^2 even when the mean is right.

The same holds in dd dimensions for isotropic covariances Σr=σr2I\boldsymbol\Sigma_r = \sigma_r^2\mathbf I and Σg=σg2I\boldsymbol\Sigma_g = \sigma_g^2\mathbf I. Then (ΣrΣg)1/2=σrσgI(\boldsymbol\Sigma_r\boldsymbol\Sigma_g)^{1/2} = \sigma_r\sigma_g\mathbf I, and each of the dd diagonal entries contributes (σr−σg)2(\sigma_r - \sigma_g)^2, so the covariance term is d(σr−σg)2d(\sigma_r - \sigma_g)^2. Figure 3 shows the case d=2d = 2 with σr=1\sigma_r = 1, σg=s\sigma_g = s and a shift mm of the mean, so that FID=m2+2(1−s)2\mathrm{FID} = m^2 + 2(1 - s)^2.

Figure

FID as the generated Gaussian shifts and shrinks

Four cases of FID between two isotropic Gaussiansidentical: shift 0, scale 1, mean term 0, covariance term 0, FID 0. shifted: shift 1.5, scale 1, mean term 2.25, covariance term 0, FID 2.25. shrunk: shift 0, scale 0.4, mean term 0, covariance term 0.72, FID 0.72. shifted + shrunk: shift 1.5, scale 0.4, mean term 2.25, covariance term 0.72, FID 2.97.identicalm = 0, s = 10 + 0FID = 0shiftedm = 1.5, s = 12.25 + 0FID = 2.25shrunkm = 0, s = 0.40 + 0.72FID = 0.72shifted + shrunkm = 1.5, s = 0.42.25 + 0.72FID = 2.97Four cases of FID between two isotropic Gaussiansidentical: shift 0, scale 1, mean term 0, covariance term 0, FID 0. shifted: shift 1.5, scale 1, mean term 2.25, covariance term 0, FID 2.25. shrunk: shift 0, scale 0.4, mean term 0, covariance term 0.72, FID 0.72. shifted + shrunk: shift 1.5, scale 0.4, mean term 2.25, covariance term 0.72, FID 2.97.identicalm = 0, s = 10 + 0FID = 0shiftedm = 1.5, s = 12.25 + 0FID = 2.25shrunkm = 0, s = 0.40 + 0.72FID = 0.72shifted + shrunkm = 1.5, s = 0.42.25 + 0.72FID = 2.97
Real features N(0, I) in red, generated features N((m, 0), s²I) in blue (2σ circles). In two dimensions FID = m² + 2(1 − s)², twice the one-dimensional covariance term (1 − s)², printed under each panel as mean term (amber) + covariance term: a shift only moves the mean term, a narrower (less diverse) generator only moves the covariance term, and the two add up.

The two-dimensional example in numbers#

Figure 2 uses

μr=(00),Σr=(10.60.60.8),μg=(1.20.6),Σg=(0.45−0.1−0.10.35).\begin{aligned} \boldsymbol\mu_r &= \begin{pmatrix}0\\0\end{pmatrix}, & \boldsymbol\Sigma_r &= \begin{pmatrix}1 & 0.6\\ 0.6 & 0.8\end{pmatrix}, \\ \boldsymbol\mu_g &= \begin{pmatrix}1.2\\0.6\end{pmatrix}, & \boldsymbol\Sigma_g &= \begin{pmatrix}0.45 & -0.1\\ -0.1 & 0.35\end{pmatrix}. \end{aligned}

Both covariances are positive definite here (det⁡Σr=0.44\det\boldsymbol\Sigma_r = 0.44, det⁡Σg=0.1475\det\boldsymbol\Sigma_g = 0.1475, 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, Tr⁡M1/2=Tr⁡M+2det⁡M\operatorname{Tr} \mathbf M^{1/2} = \sqrt{\operatorname{Tr} \mathbf M + 2\sqrt{\det \mathbf M}}; this is optional mathematical depth, and a reader who takes the formula on trust can go straight to the table. The product M=ΣrΣg\mathbf M = \boldsymbol\Sigma_r\boldsymbol\Sigma_g is not symmetric. Because Σr\boldsymbol\Sigma_r is invertible, it is similar to the symmetric positive-definite matrix Σr1/2ΣgΣr1/2\boldsymbol\Sigma_r^{1/2}\boldsymbol\Sigma_g\boldsymbol\Sigma_r^{1/2}:

M=Σr1/2(Σr1/2ΣgΣr1/2)Σr−1/2.\mathbf M = \boldsymbol\Sigma_r^{1/2}\big(\boldsymbol\Sigma_r^{1/2}\boldsymbol\Sigma_g\boldsymbol\Sigma_r^{1/2}\big)\boldsymbol\Sigma_r^{-1/2} .

So M\mathbf M is diagonalisable with positive eigenvalues λ1,λ2\lambda_1, \lambda_2, and its principal square root M1/2\mathbf M^{1/2} has the same eigenvectors and eigenvalues λ1,λ2\sqrt{\lambda_1}, \sqrt{\lambda_2}. For a 2×22\times 2 matrix this gives a closed form. Squaring the sum of the two roots gives λ1+λ2+2λ1λ2\lambda_1 + \lambda_2 + 2\sqrt{\lambda_1\lambda_2}, and the sum and product of the eigenvalues are the trace and the determinant:

Tr⁡M1/2=λ1+λ2=λ1+λ2+2λ1λ2=Tr⁡M+2det⁡M.\begin{aligned} \operatorname{Tr} \mathbf M^{1/2} &= \sqrt{\lambda_1} + \sqrt{\lambda_2} \\ &= \sqrt{\lambda_1 + \lambda_2 + 2\sqrt{\lambda_1\lambda_2}} \\ &= \sqrt{\operatorname{Tr} \mathbf M + 2\sqrt{\det \mathbf M}} . \end{aligned}
QuantityValue
∥μr−μg∥22=1.22+0.62\lVert\boldsymbol\mu_r - \boldsymbol\mu_g\rVert_2^2 = 1.2^2 + 0.6^21.801.80
M=ΣrΣg\mathbf M = \boldsymbol\Sigma_r\boldsymbol\Sigma_g(0.390.110.190.22)\begin{pmatrix}0.39 & 0.11\\ 0.19 & 0.22\end{pmatrix}
Tr⁡M, det⁡M\operatorname{Tr} \mathbf M,\ \det \mathbf M0.61, 0.06490.61,\ 0.0649
Tr⁡M1/2=0.61+20.0649\operatorname{Tr} \mathbf M^{1/2} = \sqrt{0.61 + 2\sqrt{0.0649}}1.0581.058
Tr⁡Σr+Tr⁡Σg−2Tr⁡M1/2=1.8+0.8−2.116\operatorname{Tr}\boldsymbol\Sigma_r + \operatorname{Tr}\boldsymbol\Sigma_g - 2\operatorname{Tr} \mathbf M^{1/2} = 1.8 + 0.8 - 2.1160.4840.484
FID2.2842.284

In practice the features are 2048-dimensional and the square root is computed numerically, for example with scipy.linalg.sqrtm. Unlike the symmetric root Σ1/2\boldsymbol\Sigma^{1/2} of the Gaussian note, (ΣrΣg)1/2(\boldsymbol\Sigma_r\boldsymbol\Sigma_g)^{1/2} 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 εI\varepsilon\mathbf I 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 PP and QQ on Rd\mathbb R^d with finite second moments is

W22(P,Q)=inf⁡γ∈Π(P,Q)E(x,y)∼γ ∥x−y∥22,W_2^2(P, Q) = \inf_{\gamma \in \Pi(P, Q)} \mathbb{E}_{(x, y)\sim\gamma}\,\|x - y\|_2^2 ,

where Π(P,Q)\Pi(P, Q) is the set of transport plans with marginals PP and QQ, as in the WGAN note; the finite second moments keep the infimum finite. It is the same family of transport distances as W1W_1, 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=W22(N(μr,Σr), N(μg,Σg)).\mathrm{FID} = W_2^2\big(\mathcal N(\boldsymbol\mu_r, \boldsymbol\Sigma_r),\ \mathcal N(\boldsymbol\mu_g, \boldsymbol\Sigma_g)\big).

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 ScoreFID
Aimconfidence and diversity of generated imagescloseness of generated images to real ones
Data usedgenerated images onlyreal and generated images
What is comparedeach p(y∣x)p(y \mid x) with the marginal p(y)p(y)Gaussian fit to real features vs. Gaussian fit to generated features
DistanceKL divergencesquared Fréchet (2-Wasserstein) distance
Better ishigherlower

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 2048⋅2049/2=2 098 1762048\cdot 2049/2 = 2\,098\,176 free entries. With N≤2048N \le 2048 images the sample covariance is singular (its rank is at most N−1N - 1), 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 NN can be ranked by their bias rather than by their quality (Chong and Forsyth, 2020, §3.1, Figure 2). Chong and Forsyth extrapolate in 1/N1/N to an effectively unbiased FID∞\mathrm{FID}_\infty (§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 φ(x)\varphi(x) 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 KK classes this gives a K×KK \times K 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. W2W_2 is a metric, so FID(a,b)=FID(b,a)\mathrm{FID}(a, b) = \mathrm{FID}(b, a).
  • Semantic order. If the features carry meaning, similar classes are closer than dissimilar ones. On CIFAR-10 one expects FID(airplane,ship)<FID(airplane,frog)\mathrm{FID}(\text{airplane}, \text{ship}) < \mathrm{FID}(\text{airplane}, \text{frog}) and FID(truck,automobile)<FID(truck,dog)\mathrm{FID}(\text{truck}, \text{automobile}) < \mathrm{FID}(\text{truck}, \text{dog}).

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 999999. 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 εI\varepsilon\mathbf I to both covariances, with a small ε\varepsilon such as 10−610^{-6}, 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

k(u,v)=(1d u⊤v+1)3,k(u, v) = \Big(\tfrac1d\, u^{\top} v + 1\Big)^{3},

where dd is the feature dimension (20482048 for pool3). For distributions PP and QQ of features,

MMD2(P,Q)=E k(u,u′)+E k(v,v′)−2 E k(u,v),\mathrm{MMD}^2(P, Q) = \mathbb E\,k(u, u') + \mathbb E\,k(v, v') - 2\,\mathbb E\,k(u, v),

with u,u′∼Pu, u' \sim P and v,v′∼Qv, v' \sim Q independent. Expanding the cube, kk is a sum of the inner products (u⊤v)j(u^\top v)^j, j=0,…,3j = 0, \dots, 3, with positive weights, and (u⊤v)j(u^\top v)^j is the inner product of the jj-fold tensor products of uu and vv. So MMD2\mathrm{MMD}^2 is a weighted sum of the squared differences between the moments of PP and QQ 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 u1,…,umu_1, \dots, u_m of real and v1,…,vnv_1, \dots, v_n of generated images, KID is estimated by

KID^=1m(m−1)∑i≠jk(ui,uj)+1n(n−1)∑i≠jk(vi,vj)−2mn∑i=1m∑j=1nk(ui,vj).\begin{aligned} \widehat{\mathrm{KID}} = {}& \frac{1}{m(m-1)}\sum_{i \ne j} k(u_i, u_j) + \frac{1}{n(n-1)}\sum_{i \ne j} k(v_i, v_j) \\ &- \frac{2}{mn}\sum_{i=1}^{m}\sum_{j=1}^{n} k(u_i, v_j) . \end{aligned}

Leaving out the terms i=ji = j makes each average an unbiased estimate of its expectation (Gretton et al., 2012, Lemma 6), so KID^\widehat{\mathrm{KID}} 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 00 at every sample size shown, while the FID estimate is still about 8.18.1 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 O(n2d)O(n^2 d) for nn images per set, against O(nd2+d3)O(nd^2 + d^3) 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 N(μ1,Σ1)\mathcal N(\mu_1, \Sigma_1) and N(μ2,Σ2)\mathcal N(\mu_2, \Sigma_2) 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 default eps plays the role of the ε\varepsilon added to the covariances above.
  • Cells 40–41: the 10×1010 \times 10 class-to-class FID matrix and its heatmap. The checks: all entries above −10−5-10^{-5}, FID(airplane,ship)<FID(airplane,frog)\mathrm{FID}(\text{airplane}, \text{ship}) < \mathrm{FID}(\text{airplane}, \text{frog}) and FID(truck,automobile)<FID(truck,dog)\mathrm{FID}(\text{truck}, \text{automobile}) < \mathrm{FID}(\text{truck}, \text{dog}). 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#