Course contents 09 / 34

09 / 34 · Evaluation

Inception Score

Scoring generated images with a pretrained classifier, which should be confident on each image and spread its predictions over many classes, and why the two entropies combine into one average KL divergence.

Prerequisites

Entropy · KL divergence · Expectation

On this page 7

You have trained two VAE variants and drawn a grid of samples from each. One grid looks sharper, the other more varied. Which model is better, and by how much?

For a VAE the first answer is the likelihood: compare the ELBO of the two models on held-out images. That answer has two problems. The generative adversarial networks later in the course define no likelihood at all; they only produce samples. And even where a likelihood exists, it can disagree with what the samples look like (Theis, van den Oord and Bethge, 2016).

Why the likelihood is not enough#

Take an excellent model q0q_0 and a useless one q1q_1 that outputs white noise, and mix them:

q2(x)=0.01 q0(x)+0.99 q1(x).q_2(x) = 0.01\, q_0(x) + 0.99\, q_1(x) .

Ninety-nine samples in a hundred from q2q_2 are noise. Its log-likelihood barely moves, because dropping the second term can only lower the logarithm:

log⁡q2(x)≥log⁡(0.01 q0(x))=log⁡q0(x)−4.61.\begin{aligned} \log q_2(x) &\ge \log\big(0.01\, q_0(x)\big) \\ &= \log q_0(x) - 4.61 . \end{aligned}

For a 32×3232 \times 32 colour image, with D=3072D = 3072 values, the log-likelihood is a sum over thousands of dimensions, and 4.61 nats is 0.00150.0015 nats per dimension. The model got far worse to look at; its likelihood is almost the same.

The opposite also happens. A mixture of narrow Gaussians centred on the training images, q(x)=1N∑nN(x∣xn,ϵ2I)q(x) = \frac1N \sum_n \mathcal N(x \mid x_n, \epsilon^2 I), produces perfect-looking samples, because they are the training images plus invisible noise. Its likelihood on test images is terrible, because the test images lie far from every centre (Murphy, 2023, Section 20.4.1).

To see how terrible, let δ>0\delta > 0 be the distance from a test image xx to its nearest training image. Every term of the mixture is a Gaussian density whose exponent is at most −δ2/(2ϵ2)-\delta^2/(2\epsilon^2), and an average is at most its largest term, so

log⁡q(x)≤−δ22ϵ2−D2log⁡(2πϵ2).\log q(x) \le -\frac{\delta^2}{2\epsilon^2} - \frac D2 \log(2\pi\epsilon^2) .

As ϵ→0\epsilon \to 0 the first term falls like 1/ϵ21/\epsilon^2 and the second grows only like log⁡(1/ϵ)\log(1/\epsilon), so the test log-likelihood tends to −∞-\infty while the samples get ever sharper.

So sample quality has to be measured on samples. People can do it: Salimans et al. (2016) asked crowd workers to tell generated images from real ones, and found the answers depended on the interface and on whether workers saw their mistakes. The last note of this section returns to people. The Inception Score was proposed in the same paper as an automatic stand-in for them.

What the Inception Score asks for#

A good set of generated images should have two properties. Each image should clearly show something: a classifier looking at it should be confident. And the set should contain many different things: across the images, the classifier’s answers should be spread over many classes.

Neither property is enough alone. A generator that draws the same sharp beagle every time passes the first test and fails the second. A generator that draws shapeless blobs might spread the classifier’s guesses evenly, yet no single image is recognizable.

The two entropies#

The score uses a pretrained classifier, by convention Inception v3 trained on the 1000 ImageNet classes (Szegedy et al., 2016). For a generated image xx it outputs a softmax distribution p(y∣x)p(y \mid x) over the classes y∈{1,…,n}y \in \{1, \dots, n\}.

Figure

Computing the Inception Score

Inception Score pipelineGenerated images x1 to xN enter an Inception v3 network pretrained on ImageNet, built from Inception modules and ending in a softmax. For each image the network outputs a class distribution p of y given x; the three examples are each peaked on a different class, meaning low conditional entropy and confident predictions. Averaging them gives the marginal p of y, which is flat, meaning high entropy and diverse images. The expected KL divergence between each p of y given x and p of y is exponentiated to give the Inception Score.generatedimagesx₁, …, xNInception v3softmaxInception modulespretrained onImageNet, frozenone output per imagep(y | x₁)p(y | x₂)p(y | xN)⋮averageover imagesp(y)marginalpeaked: low H(p(y | x))“confident”flat: high H(p(y))“diverse”𝔼x DKL(p(y | x) ‖ p(y))expISInception Score pipelineGenerated images x1 to xN enter an Inception v3 network pretrained on ImageNet, built from Inception modules and ending in a softmax. For each image the network outputs a class distribution p of y given x; the three examples are each peaked on a different class, meaning low conditional entropy and confident predictions. Averaging them gives the marginal p of y, which is flat, meaning high entropy and diverse images. The expected KL divergence between each p of y given x and p of y is exponentiated to give the Inception Score.generated imagesx₁, …, xNInception v3softmaxInceptionmodulespretrained on ImageNet, frozenp(y | x₁)p(y | x₂)p(y | xN)…peaked: low H(p(y | x)), “confident”averagep(y)flat: high H(p(y)), “diverse”𝔼x DKL(p(y | x) ‖ p(y))expIS
Each generated image goes through a frozen ImageNet classifier; its softmax output p(y | x) should be peaked (confident). Averaging these outputs over all images gives the marginal p(y), which should be flat (diverse). The score compares every p(y | x) with p(y) by KL divergence.

Confidence on one image is low entropy of its output:

H(p(y∣x))=−∑yp(y∣x)log⁡p(y∣x).H\big(p(y \mid x)\big) = -\sum_y p(y \mid x) \log p(y \mid x) .

A one-hot output has entropy 0; a flat output has the maximum log⁡n\log n. Logarithms are natural throughout.

Variety across images is high entropy of the marginal class distribution, the classifier’s output averaged over generated images. Let XX be a random generated image and YY the class the classifier assigns to it. By the law of total probability,

p(y)=P(Y=y)=EX[P(Y=y∣X)]=Ex[p(y∣x)]≈1N∑i=1Np(y∣xi),\begin{aligned} p(y) = P(Y = y) &= \mathbb E_X\big[P(Y = y \mid X)\big] \\ &= \mathbb E_x\big[p(y \mid x)\big] \\ &\approx \frac1N \sum_{i=1}^N p(y \mid x_i) , \end{aligned}

where x1,…,xNx_1, \dots, x_N are samples from the generator. Only samples are needed, never a density of the generator, which is what makes the score usable for models without a likelihood.

From two entropies to one KL divergence#

We want H(p(y))H(p(y)) large and the average H(p(y∣x))H(p(y \mid x)) small, so we look at their difference. Expand both entropies, write the weight p(y)p(y) of the first sum as Ex[p(y∣x)]\mathbb E_x[p(y \mid x)], and use linearity of expectation to pull both sums under one Ex\mathbb E_x:

H(p(y))−Ex[H(p(y∣x))]=−∑yp(y)log⁡p(y)+Ex[∑yp(y∣x)log⁡p(y∣x)]=Ex[∑yp(y∣x)log⁡p(y∣x)]−Ex[∑yp(y∣x)log⁡p(y)]=Ex[∑yp(y∣x)log⁡p(y∣x)p(y)]=Ex[DKL(p(y∣x) ∥ p(y))].\begin{aligned} &H\big(p(y)\big) - \mathbb E_x\Big[H\big(p(y \mid x)\big)\Big] \\ &\quad= -\sum_y p(y) \log p(y) \\ &\qquad + \mathbb E_x\Big[\sum_y p(y \mid x) \log p(y \mid x)\Big] \\ &\quad= \mathbb E_x\Big[\sum_y p(y \mid x) \log p(y \mid x)\Big] \\ &\qquad - \mathbb E_x\Big[\sum_y p(y \mid x) \log p(y)\Big] \\ &\quad= \mathbb E_x\Big[\sum_y p(y \mid x) \log \frac{p(y \mid x)}{p(y)}\Big] \\ &\quad= \mathbb E_x\Big[D_{\mathrm{KL}}\big(p(y \mid x) \,\|\, p(y)\big)\Big] . \end{aligned}

Only the second equality needs care. The factor log⁡p(y)\log p(y) does not depend on xx, and the sum over classes is finite, so the expectation can move outside:

∑yEx[p(y∣x)]log⁡p(y)=Ex[∑yp(y∣x)log⁡p(y)].\sum_y \mathbb E_x\big[p(y \mid x)\big] \log p(y) = \mathbb E_x\Big[\sum_y p(y \mid x) \log p(y)\Big] .
Figure

What each term of the identity measures

The Inception Score identity, annotatedThe identity H of p of y, minus the expectation over x of H of p of y given x, equals the expectation over x of the KL divergence of p of y given x from p of y. An arrow labels the first term as diversity, which we want high: high when the generated classes are spread evenly, at most log n. An arrow labels the second term as uncertainty, which we want low: low when each image is clearly one class, at least 0. An arrow labels the right-hand side as how far each image's prediction is from the average prediction; exponentiating it gives IS, between 1 and n.H(p(y))−𝔼x[H(p(y | x))]=𝔼x[DKL(p(y | x) ‖ p(y))]diversity (want high)high when classes are usedevenly; at most log nuncertainty (want low)low when each image is clearlyone class; at least 0how far each image's predictionis from the average predictionexp(·)1 ≤ IS ≤ nThe Inception Score identity, annotatedThe identity H of p of y, minus the expectation over x of H of p of y given x, equals the expectation over x of the KL divergence of p of y given x from p of y. The first term is diversity, which we want high: high when the generated classes are spread evenly, at most log n. The second term is uncertainty, which we want low: low when each image is clearly one class, at least 0. The right-hand side measures how far each image's prediction is from the average prediction; exponentiating it gives IS, between 1 and n.diversity (want high)high when classes areused evenly; ≤ log nuncertainty (want low)low when each image isclearly one class; ≥ 0H(p(y))−𝔼x[H(p(y | x))]=𝔼x[DKL(p(y | x) ‖ p(y))]how far each image's predictionis from the average predictionexp(·)1 ≤ IS ≤ n
The two entropies are the two goals of the score: a high marginal entropy (variety) and a low conditional entropy (each image distinctly looks like something). Their difference is the average KL divergence that the Inception Score exponentiates.

The KL form says what the score compares: each image’s class distribution against the average one. An image scores high when its prediction is sharp and sits on a class the average does not concentrate on, so it differs from the other images.

The left-hand side is also a familiar quantity. The entropy of YY minus its conditional entropy given XX is the mutual information I(X;Y)I(X; Y) between a generated image and its predicted class: how much seeing the image tells us about the label (Barratt and Sharma, 2018).

The score#

The Inception Score exponentiates the average divergence:

IS=exp⁡(Ex[DKL(p(y∣x) ∥ p(y))])≈exp⁡(1N∑i=1NDKL(p(y∣xi) ∥ p(y))).\begin{aligned} \mathrm{IS} &= \exp\Big(\mathbb E_x\big[D_{\mathrm{KL}}\big(p(y \mid x) \,\|\, p(y)\big)\big]\Big) \\ &\approx \exp\Big(\frac1N \sum_{i=1}^N D_{\mathrm{KL}}\big(p(y \mid x_i) \,\|\, p(y)\big)\Big) . \end{aligned}

Higher is better. Splitting the exponent into its two entropies gives a reading in numbers of classes:

IS=eH(p(y))e ExH(p(y∣x)).\mathrm{IS} = \frac{e^{H(p(y))}}{e^{\,\mathbb E_x H(p(y \mid x))}} .

The exponential of an entropy is an effective number of classes: elog⁡3=3e^{\log 3} = 3 for a uniform distribution over three. The numerator counts how many classes the generator covers; the denominator, how many classes a typical single image looks like. Stanford’s CS236 writes the same product as diversity times sharpness (Ermon, 2023).

The divergence is always finite. A KL divergence is finite when p(y)>0p(y) > 0 wherever p(y∣x)>0p(y \mid x) > 0, and p(y)p(y) is an average that includes p(y∣x)p(y \mid x) itself. In floating point a softmax output can underflow to exactly 0, so the code must use 0log⁡0=00 \log 0 = 0.

The range#

Three facts from Entropy, cross-entropy and KL divergence bound the exponent. A KL divergence is never negative, and it is 0 only when its two arguments are equal (the KL section). A discrete entropy is never negative, because every term −plog⁡p-p \log p with 0≤p≤10 \le p \le 1 is non-negative, so by the identity the exponent is at most H(p(y))H(p(y)). And the entropy of a distribution over nn classes is at most log⁡n\log n, with equality only for the uniform distribution (Gibbs’ inequality). Together,

0≤Ex[DKL]≤H(p(y))≤log⁡n,1≤IS≤n.\begin{aligned} 0 &\le \mathbb E_x\big[D_{\mathrm{KL}}\big] \le H\big(p(y)\big) \le \log n , \\ 1 &\le \mathrm{IS} \le n . \end{aligned}

The equality cases follow from the same facts. IS=1\mathrm{IS} = 1 means the average divergence is 0. An average of non-negative numbers is 0 only when each of them is 0, and each KL is 0 only when p(y∣x)=p(y)p(y \mid x) = p(y). So IS=1\mathrm{IS} = 1 exactly when every image gets the same class distribution, peaked (mode collapse) or flat (nothing recognizable).

IS=n\mathrm{IS} = n needs both inequalities on the right to be equalities: the marginal must be uniform, and every H(p(y∣x))H(p(y \mid x)) must be 0, which means one-hot outputs. A softmax is never exactly one-hot, so the score only approaches n=1000n = 1000 for Inception v3.

The base of the logarithm does not matter as long as the exponential matches it. An exponent of aa nats is a/ln⁡2a / \ln 2 bits, and 2a/ln⁡2=ea2^{a / \ln 2} = e^{a} gives the same score.

A worked example with three classes#

Take a classifier with n=3n = 3 classes (beagle, corgi, husky) and three generated images per generator. Entropies and divergences are in nats.

GeneratorOutputs p(y∣xi)p(y \mid x_i)Marginal p(y)p(y)H(p(y))H(p(y))mean H(p(y∣x))H(p(y \mid x))IS
A: confident, diverse(0.9,0.05,0.05),(0.9, 0.05, 0.05), (0.05,0.9,0.05),(0.05, 0.9, 0.05), (0.05,0.05,0.9)(0.05, 0.05, 0.9)(13,13,13)(\frac13, \frac13, \frac13)1.0991.0990.3940.3942.022.02
B: mode collapse(0.9,0.05,0.05)(0.9, 0.05, 0.05) three times(0.9,0.05,0.05)(0.9, 0.05, 0.05)0.3940.3940.3940.3941.001.00
C: not confident(13,13,13)(\frac13, \frac13, \frac13) three times(13,13,13)(\frac13, \frac13, \frac13)1.0991.0991.0991.0991.001.00
D: partial collapse(0.9,0.05,0.05)(0.9, 0.05, 0.05) twice, (0.05,0.9,0.05)(0.05, 0.9, 0.05)(0.617,0.333,0.05)(0.617, 0.333, 0.05)0.8140.8140.3940.3941.521.52

Each confident output has the same entropy:

H=−(0.9ln⁡0.9+2⋅0.05ln⁡0.05)=0.0948+0.2996=0.394.\begin{aligned} H &= -\big(0.9 \ln 0.9 + 2 \cdot 0.05 \ln 0.05\big) \\ &= 0.0948 + 0.2996 \\ &= 0.394 . \end{aligned}

For A the marginal is uniform, H(p(y))=ln⁡3=1.099H(p(y)) = \ln 3 = 1.099, and the score is e1.099−0.394=e0.704=2.02e^{1.099 - 0.394} = e^{0.704} = 2.02. In the effective-number reading, A covers 3 classes and each image looks like e0.394=1.48e^{0.394} = 1.48 classes: 3/1.48=2.023 / 1.48 = 2.02.

B and C both get the minimum 1, for opposite reasons. B is confident but always draws a beagle, so its marginal is as peaked as each output. C spreads its guesses but no image looks like anything. D never draws a husky and lands between. Its three divergences are 0.2450.245, 0.2450.245 and 0.7680.768; only their mean, 0.4200.420, equals 0.814−0.3940.814 - 0.394.

Figure

Three classes, four generators

Inception Score for four toy generatorsGenerator A produces three images, each classified with probability 0.9 as a different class; the marginal is uniform, the mean KL divergence is 0.70 nats and IS is 2.02. Generator B produces three images all classified as beagle with probability 0.9; the marginal equals each per-image distribution, KL is 0 and IS is 1. Generator C produces images with uniform class probabilities; the marginal is also uniform, KL is 0 and IS is 1. Generator D produces two beagles and one corgi with probability 0.9 each and never a husky; the marginal is 0.62, 0.33, 0.05, the mean KL is 0.42 and IS is 1.52.p(y | x₁)p(y | x₂)p(y | x₃)p(y)scoreA · confident and diversebchH = 0.39bchH = 0.39bchH = 0.39averagebchH = 1.10mean DKL = 0.70IS = 2.02B · confident, mode collapsebchH = 0.39bchH = 0.39bchH = 0.39averagebchH = 0.39mean DKL = 0.00IS = 1.00C · diverse on average, not confidentbchH = 1.10bchH = 1.10bchH = 1.10averagebchH = 1.10mean DKL = 0.00IS = 1.00D · partial collapse (never a husky)bchH = 0.39bchH = 0.39bchH = 0.39averagebchH = 0.81mean DKL = 0.42IS = 1.52Inception Score for four toy generatorsGenerator A produces three images, each classified with probability 0.9 as a different class; the marginal is uniform, the mean KL divergence is 0.70 nats and IS is 2.02. Generator B produces three images all classified as beagle with probability 0.9; the marginal equals each per-image distribution, KL is 0 and IS is 1. Generator C produces images with uniform class probabilities; the marginal is also uniform, KL is 0 and IS is 1. Generator D produces two beagles and one corgi with probability 0.9 each and never a husky; the marginal is 0.62, 0.33, 0.05, the mean KL is 0.42 and IS is 1.52.A · confident and diversep(y | x₁)bchH = 0.39p(y | x₂)bchH = 0.39p(y | x₃)bchH = 0.39averagebchp(y)H = 1.10mean DKL = 0.70IS = 2.02B · confident, mode collapsep(y | x₁)bchH = 0.39p(y | x₂)bchH = 0.39p(y | x₃)bchH = 0.39averagebchp(y)H = 0.39mean DKL = 0.00IS = 1.00C · diverse on average, not confidentp(y | x₁)bchH = 1.10p(y | x₂)bchH = 1.10p(y | x₃)bchH = 1.10averagebchp(y)H = 1.10mean DKL = 0.00IS = 1.00D · partial collapse (never a husky)p(y | x₁)bchH = 0.39p(y | x₂)bchH = 0.39p(y | x₃)bchH = 0.39averagebchp(y)H = 0.81mean DKL = 0.42IS = 1.52
Blue bars: classifier outputs p(y | x) for three generated images; green bars: their average p(y). A has peaked outputs and a flat marginal and scores highest; B (mode collapse) and C (unrecognizable images) score the minimum 1; D lands between. Entropies in nats; b, c, h = beagle, corgi, husky.

The estimator is a few lines; SciPy’s rel_entr computes alog⁡(a/b)a \log(a / b) with the convention 0log⁡0=00 \log 0 = 0:

import numpy as np
from scipy.special import rel_entr

def inception_score(probs):              # probs[i] = p(y | x_i), shape (N, n)
    p_y = probs.mean(axis=0)              # the marginal p(y)
    kl = rel_entr(probs, p_y).sum(axis=1) # KL(p(y | x_i) || p(y)) for every i
    return np.exp(kl.mean())

a, b, c = [0.9, 0.05, 0.05], [0.05, 0.9, 0.05], [0.05, 0.05, 0.9]
print(round(inception_score(np.array([a, b, c])), 2))   # generator A
print(round(inception_score(np.array([a, a, b])), 2))   # generator D
2.02
1.52

Where it breaks#

The score never looks at real data, and the worked example already shows what that costs. Replace generator A by one that outputs exactly three fixed images, one perfect beagle, one perfect corgi and one perfect husky, each with the outputs of A. Its marginal is uniform and its score is 2.022.02, the same as A’s. It has no variety within any class, and its three images could be copied straight from the training set. A generator that memorizes training images scores as well as the data itself (Barratt and Sharma, 2018). Within-class variety, faithfulness to the training distribution and memorization are all invisible to IS.

Other failures follow from the classifier.

It can be gamed. Salimans et al. themselves caution that the score is a rough guide for models trained by some other criterion: optimizing it directly produces adversarial examples. Barratt and Sharma show this concretely: small perturbations of generated images push the score to 900.15, close to its maximum of 1000, without making them look more like natural images (their Section 4.2.2 and Figure 1).

It depends on the network and its implementation. The Keras and the PyTorch ports of Inception v3 have almost the same ImageNet accuracy, yet on the same 50,000 images their mean scores differ by 3.5% for ImageNet validation images and by 11.5% for CIFAR-10 images (Barratt and Sharma, 2018, Section 4.1.1, Table 1). For data far from ImageNet, faces, digits or medical images, the 1000 classes do not describe what varies in the data at all.

It depends on the protocol. Diversity is measured through the marginal, which needs many samples: Salimans et al. evaluate on 50,000 images. The common protocol splits them into 10 groups of 5,000, estimates the marginal in each group and reports the mean and standard deviation of the 10 scores. Scores computed with other sample sizes or splits are not comparable.

What you should remember#

  • A likelihood can be fine while samples are bad, and some models have none: sample quality is measured on samples.
  • p(y)=1N∑ip(y∣xi)p(y) = \frac1N \sum_i p(y \mid x_i); log⁡IS=H(p(y))−ExH(p(y∣x))=ExDKL(p(y∣x) ∥ p(y))\log \mathrm{IS} = H(p(y)) - \mathbb E_x H(p(y \mid x)) = \mathbb E_x D_{\mathrm{KL}}(p(y \mid x) \,\|\, p(y)).
  • The exponent is the mutual information I(X;Y)I(X; Y); the score is classes covered divided by classes per image.
  • 1≤IS≤n1 \le \mathrm{IS} \le n; 1 means every image gets the same class distribution.
  • IS never sees real data: one perfect image per class, or copied training images, score as well as a good generator.

Notebook#

The metrics notebook of the HSE-LAMBDA Deep Generative Models course opens the evaluation block. Cell numbers count every cell, markdown included, from 1; quoted numbers come from the run saved with the course.

  • The IS part (cells 2–4) is text only: the purpose of the score, the empirical marginal and the exponentiated average KL, the reading that each image’s class distribution is compared with the averaged one, and the caveat that the score depends on the classifier. The notebook does not compute IS.
  • A comparison table of IS, FID and LPIPS (cell 6) closes the theory part; its hands-on part is LPIPS.

Next#

IS judges generated images by themselves, so a generator that copies three training images can match a good one. The Fréchet Inception Distance uses the same network but compares the generated images with real ones.

References#