Scoring generated images with a pretrained classifier — confident predictions per image, diverse predictions overall — and why the two entropies combine into an average KL divergence.
A GAN has no likelihood, a VAE’s likelihood bound comes with blurry samples, and a WGAN critic’s estimate of W1 depends on the critic, so none of the training losses can rank finished generators. We need metrics computed from the images instead. This note and the next two cover three standard ones. The Inception Score (IS) and the Fréchet Inception Distance (FID) score a set of generated images; LPIPS is a perceptual distance between pairs of images, used as a loss and to measure the diversity of samples. The Inception Score (Salimans et al., 2016) uses only generated images, a pretrained classifier, and the entropy and KL divergence from Entropy, cross-entropy and KL divergence.
Notation.x is a generated image and y∈{1,…,n} is one of the n classes of the classifier. p(y∣x) is the classifier’s softmax output for image x, and Ex is the expectation over images produced by the generator. Logarithms are natural unless a base is shown.
The Inception Score rewards two properties at once:
Variety. The images are of many different kinds, for example many different breeds of dog, not the same breed over and over.
Distinctness. Each image distinctly looks like something: a classifier can say with confidence what it shows.
Neither property is enough alone. A generator that draws the same sharp beagle every time has distinct images and no variety. A generator that draws shapeless blobs has nothing a classifier can recognize.
Run them through a pretrained classifier, usually Inception v3 trained on ImageNet (Szegedy et al., 2016). The network is built from Inception modules and ends in a softmax, so for every image we get a class distribution p(y∣x).
Calculate two entropies from these outputs.
H(p(y∣x)) should be low: the classifier makes a confident prediction, so the image distinctly resembles a certain class. This term enters the score with a minus sign.
H(p(y)) should be high: the images are diverse, so on average the classes are close to uniformly distributed.
Figure
Computing the Inception Score
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.
The marginal class distribution is the classifier output averaged over the generated images:
p(y)=Ex[p(y∣x)],
and its entropy is
H(p(y))=−y∑p(y)logp(y).
Why the marginal is an average of the outputs. Let X∼Pg be a random generated image, where Pg is the distribution of the generator’s samples, and let Y be the class that the classifier assigns to it: given X=x, Y is drawn from p(y∣x).
Step 1. By definition, p(y)=P(Y=y).
Step 2. Condition on the image (law of total probability):
p(y)=EX∼Pg[P(Y=y∣X)].
If the images took finitely many values, this would be the familiar sum ∑xP(Y=y∣X=x)Pg(X=x). For a generator x=G(z) it is Ez[P(Y=y∣X=G(z))], an average over the noise (Salimans et al., 2016, §4). No density of Pg is needed, only samples. This matters because the GAN generator is implicit: we can sample from it, but we cannot evaluate a density.
Step 3. The conditional probability is the classifier output, P(Y=y∣X=x)=p(y∣x), so
p(y)=Ex[p(y∣x)].
In practice the expectation is empirical: the distribution of X is uniform over the N samples we took, so each image gets weight 1/N and
We want the marginal entropy to be large and the average conditional entropy to be small, so we look at their difference. It turns out to be an average KL divergence: H(p(y))−ExH(p(y∣x))=ExDKL(p(y∣x)∥p(y)). The five steps below prove it, followed by the linearity argument that step 2 uses. A reader who prefers numbers first can check the identity on the three-class example and return to the proof later.
2. Replace p(y) by Ex[p(y∣x)] in the weight of the first sum. The p(y) inside the logarithm stays as it is. The factor logp(y) does not depend on x, so it can move inside the expectation, and the sum over y can be swapped with Ex (see below):
5. Recognize the KL divergence. The inner sum is DKL(p(y∣x)∥p(y)), so
H(p(y))−Ex[H(p(y∣x))]=Ex[DKL(p(y∣x)∥p(y))].
The left-hand side is the entropy of Y minus its conditional entropy given X, which is the mutual information I(X;Y) between a generated image and its predicted class. So logIS=I(X;Y): the logarithm of the score measures how much a generated image tells us about its label (Barratt and Sharma, 2018, §3.3, eqs. 2–3 and Appendix A).
Why the sum and the expectation can be swapped.Ex[⋅] is a linear operator and ∑y is a finite, discrete sum over classes. For any linear operator and a finite sum,
y∑Ex[fy(x)]=Ex[y∑fy(x)],
where y is a class and x is an image. This is linearity of expectation: the expectation of a sum is the sum of the expectations. Step 2 uses it with fy(x)=p(y∣x)logp(y).
Figure
What each term of the identity measures
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 gives a second reading of the score. We compare each image’s class distribution with the averaged (marginal) one. If an image’s predicted class distribution differs from the average one, its prediction differs from those of the other images. The divergence is large when p(y∣x) is sharp and lies on a class that the marginal does not concentrate on.
Higher is better: more confident per-image predictions and more diversity across images.
The divergence is always finite. A KL divergence DKL(p(y∣x)∥p(y)) is finite only if p(y)>0 wherever p(y∣x)>0, and here this always holds: p(y) is the average of the p(y∣xi), so it is positive for every class that any image gives positive probability. In floating point a softmax output can still underflow to exactly 0, and the code must then use the convention 0log0=0 instead of computing it.
In code the estimator is a few lines. With the classifier outputs stacked into an array of shape (N,n), it reproduces the worked example below (2.02 for generator A, 1.52 for D). SciPy’s rel_entr computes alog(a/b) with 0log0=0, so exact zeros give no NaN. It is a tiny estimator for illustration, not a reference implementation: published scores also fix the network weights, the image preprocessing, the number of samples and the splits.
import numpy as npfrom scipy.special import rel_entrdef inception_score(probs): # probs[i] is p(y | x_i), shape (N, n) assert np.allclose(probs.sum(axis=1), 1) # rows are distributions p_y = probs.mean(axis=0) # marginal p(y) # D_KL(p(y | x_i) || p(y)) for every i, with 0 log 0 = 0 kl = rel_entr(probs, p_y).sum(axis=1) return np.exp(kl.mean())A = np.array([[0.9, 0.05, 0.05], [0.05, 0.9, 0.05], [0.05, 0.05, 0.9]])print(inception_score(A)) # 2.022...
The base of the logarithm can be anything, as long as the exponentiation matches it. With loge the score uses exp(⋅); with log2 it uses 2(⋅). Both choices give the same score: a KL value in bits is the value in nats divided by ln2, and 2a/ln2=ea.
Entropies of discrete distributions are non-negative, H(p(y∣x))≥0. By the identity, Ex[DKL]=H(p(y))−Ex[H(p(y∣x))]≤H(p(y)).
The marginal entropy is at most logn, with equality only when p(y) is uniform (Gibbs’ inequality against the uniform distribution).
Together,
0≤Ex[DKL]≤H(p(y))≤logn⟹1≤IS≤n.
In the language of mutual information this is 0≤I(X;Y)≤H(Y)≤logn (Barratt and Sharma, 2018, §3.3 and Appendix B). The maximum of the exponent is lnn in nats and log2n in bits.
IS=1 exactly when (almost) every image gets the same class distribution, because a KL divergence is zero only when its two arguments are equal. The common distribution can be peaked (mode collapse) or flat (no image is recognizable). IS=n would need every H(p(y∣x))=0 and a uniform marginal, that is one-hot predictions spread evenly over all n classes. A softmax never outputs an exact one-hot vector, so the score only approaches n. For Inception v3 on ImageNet, n=1000.
Take a classifier with n=3 classes (beagle, corgi, husky) and three generated images per generator. The columns of the table are the per-image outputs p(y∣xi) for i=1,2,3, the marginal p(y), its entropy H(p(y)), the mean conditional entropy ExH(p(y∣x)), the mean divergence ExDKL and the score. Entropies and divergences are in nats; the score has no unit.
and the marginal is uniform, so H(p(y))=ln3≈1.099. The difference is 0.704, and IS=e0.704≈2.02. Because the marginal is uniform, each per-image divergence is DKL(p(y∣xi)∥p(y))=ln3−H(p(y∣xi))=0.704, so their mean matches the difference of entropies. In general only the mean has to match. In D the three divergences are 0.245, 0.245 and 0.768 nats, and only their mean, 0.420, equals 0.814−0.394.
Generators B and C both score the minimum 1, for opposite reasons. B is confident but always draws a beagle, so p(y) is as peaked as each p(y∣x). C is “diverse” on average, but no single image looks like anything. Generator D drops the husky class and lands in between. With perfectly one-hot, evenly spread predictions the score would reach its maximum n=3.
Figure
Three classes, four generators
Blue bars are the classifier outputs p(y | x) for three generated images; green bars are their average p(y). Generator A has peaked per-image outputs and a flat marginal and scores highest; B (mode collapse) and C (unrecognizable images) both score the minimum 1; D, which never draws a husky, lands in between. Entropies H are in nats; b, c, h = beagle, corgi, husky.
The score has well-known blind spots (Barratt and Sharma, 2018; the section numbers below refer to that paper):
It never looks at real data. A generator that produces one perfect image per class gets the maximum score while ignoring everything else in the data distribution, including variety within a class. A generator that memorizes training images scores as well as the data itself (§4.2.3), and the data distribution need not maximize the score at all: in a one-dimensional example with two overlapping classes, several other distributions score higher than the true one (§4).
It can be gamed. Optimizing the score, directly or indirectly through model selection (early stopping, hyperparameter or architecture search), drifts toward adversarial examples. Small perturbations of WGAN samples reach IS≈900 out of a possible 1000 while looking no more like natural images (§4.2.2, Figure 1).
It depends on the classifier. The score is tied to the 1000 ImageNet classes and to a specific network and implementation. Keras and Torch versions of Inception v3 with almost the same accuracy give scores that differ by 3.5% on ImageNet validation images and by 11.5% on CIFAR-10 images (§4.1.1). The original code even uses a version of the network with 1008 outputs rather than 1000 (footnote 2). For data outside ImageNet’s domain, the classes are not meaningful (§4.2.1).
It depends on the sample size and protocol. The usual protocol uses 50 000 images split into 10 groups of 5000 and reports the mean and standard deviation of the per-group scores. The marginal p(y) is estimated per group, and the score changes with the number of groups, so scores computed differently are not comparable (§3.4, §4.1.2).
The metrics notebook of the HSE-LAMBDA Deep Generative Models repository (pinned commit; cell numbers count every cell, markdown included, from 1) introduces IS in cells 2–4: the purpose of the score, two illustrations of confident versus diverse predictions, the empirical formulas p(y)≈N1∑ip(y∣xi) and IS≈exp(N1∑iDKL), a remark that the score depends on the classifier and may be affected by domain mismatch, and the reading that each sample’s class distribution is compared with the averaged one. Cell 6 summarizes IS, FID and LPIPS in one comparison table. The notebook does not compute IS; its hands-on part is about LPIPS.
S. Barratt, R. Sharma. A note on the Inception Score. ICML Workshop on Theoretical Foundations and Applications of Deep Generative Models, 2018. §3.3: the mutual-information reading; §4: issues with the score.
HSE-LAMBDA, Deep Generative Models course repository: metrics.ipynb.