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.
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 and a useless one that outputs white noise, and mix them:
Ninety-nine samples in a hundred from are noise. Its log-likelihood barely moves, because dropping the second term can only lower the logarithm:
For a colour image, with values, the log-likelihood is a sum over thousands of dimensions, and 4.61 nats is 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, , 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 be the distance from a test image to its nearest training image. Every term of the mixture is a Gaussian density whose exponent is at most , and an average is at most its largest term, so
As the first term falls like and the second grows only like , so the test log-likelihood tends to 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 it outputs a softmax distribution over the classes .
Computing the Inception Score
Confidence on one image is low entropy of its output:
A one-hot output has entropy 0; a flat output has the maximum . Logarithms are natural throughout.
Variety across images is high entropy of the marginal class distribution, the classifier’s output averaged over generated images. Let be a random generated image and the class the classifier assigns to it. By the law of total probability,
where 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 large and the average small, so we look at their difference. Expand both entropies, write the weight of the first sum as , and use linearity of expectation to pull both sums under one :
Only the second equality needs care. The factor does not depend on , and the sum over classes is finite, so the expectation can move outside:
What each term of the identity measures
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 minus its conditional entropy given is the mutual information 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:
Higher is better. Splitting the exponent into its two entropies gives a reading in numbers of classes:
The exponential of an entropy is an effective number of classes: 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 wherever , and is an average that includes itself. In floating point a softmax output can underflow to exactly 0, so the code must use .
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 with is non-negative, so by the identity the exponent is at most . And the entropy of a distribution over classes is at most , with equality only for the uniform distribution (Gibbs’ inequality). Together,
The equality cases follow from the same facts. 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 . So exactly when every image gets the same class distribution, peaked (mode collapse) or flat (nothing recognizable).
needs both inequalities on the right to be equalities: the marginal must be uniform, and every must be 0, which means one-hot outputs. A softmax is never exactly one-hot, so the score only approaches for Inception v3.
The base of the logarithm does not matter as long as the exponential matches it. An exponent of nats is bits, and gives the same score.
A worked example with three classes#
Take a classifier with classes (beagle, corgi, husky) and three generated images per generator. Entropies and divergences are in nats.
| Generator | Outputs | Marginal | mean | IS | |
|---|---|---|---|---|---|
| A: confident, diverse | |||||
| B: mode collapse | three times | ||||
| C: not confident | three times | ||||
| D: partial collapse | twice, |
Each confident output has the same entropy:
For A the marginal is uniform, , and the score is . In the effective-number reading, A covers 3 classes and each image looks like classes: .
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 , and ; only their mean, , equals .
Three classes, four generators
The estimator is a few lines; SciPy’s rel_entr computes with the convention :
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 , 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.
- ; .
- The exponent is the mutual information ; the score is classes covered divided by classes per image.
- ; 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#
- L. Theis, A. van den Oord, M. Bethge. A note on the evaluation of generative models. ICLR, 2016. Section 3.2: samples and log-likelihood, the 1%-good mixture.
- T. Salimans, I. Goodfellow, W. Zaremba, V. Cheung, A. Radford, X. Chen. Improved techniques for training GANs. NeurIPS, 2016. Section 4: human annotators and the Inception Score; Section 6.2: the caution against optimizing it.
- C. Szegedy, V. Vanhoucke, S. Ioffe, J. Shlens, Z. Wojna. Rethinking the Inception architecture for computer vision. CVPR, 2016.
- S. Barratt, R. Sharma. A note on the Inception Score. ICML Workshop on Theoretical Foundations and Applications of Deep Generative Models, 2018. Section 3: the mutual-information reading and the usual protocol; Section 4: sensitivity to the implementation (Section 4.1.1, Table 1), adversarial optimization (Section 4.2.2), memorization.
- K. P. Murphy. Probabilistic Machine Learning: Advanced Topics. MIT Press, 2023. Section 20.4: evaluating generative models.
- S. Ermon. CS236 Deep Generative Models, Stanford University, Fall 2023. Lecture 15, Evaluating generative models.
- HSE-LAMBDA, Deep Generative Models course repository:
metrics.ipynb.