Skip to content
BIASED

Inception Score

How to score a generative model from its samples alone with a frozen classifier, why the Inception Score is the average KL between each image's prediction and the marginal label distribution, and what the score cannot see.

One yardstick for every family

The topic Autoregressive transformers completed the map of this course. VAEs, GANs, normalizing flows, energy-based models, diffusion and autoregressive models all learn to produce images. Which one is good? Is today's checkpoint better than yesterday's? The training losses do not say. A GAN's two losses pull against each other, and different families minimize different things.

The yardstick from Entropy, cross-entropy and KL is the average log-likelihood of held-out images. Not every model can report it.

  • Flows and autoregressive models compute log⁡Qθ(x)\log Q_\theta(x) exactly.
  • A VAE gives only a lower bound, the ELBO. Diffusion gives a bound too, or an exact value at the price of solving the probability-flow ODE for every image.
  • An energy-based model knows its density only up to an unknown normalizing constant.
  • A GAN has no density at all. It can only sample.

And where a likelihood exists, it can still disagree with how the samples look (Theis, van den Oord and Bethge, 2016).

The final judge is a person looking at samples. Salimans et al. (2016) asked crowd workers to tell real images from generated ones. The answers depended on the setup, for example on whether workers got feedback on their mistakes, and people are slow and expensive anyway. So the same paper proposed an automatic substitute that needs nothing but samples: the Inception Score.

Two tests for the samples

The score borrows a judge that has already seen a million photos: Inception v3 (Szegedy et al., 2016), a classifier trained on the 1000 classes of ImageNet and then frozen. Give it an image xx, resized to 299×299299 \times 299, and it returns a softmax, one probability per class yy:

p(y∣x),y∈{1,…,n},n=1000.p(y \mid x), \qquad y \in \{1, \dots, n\}, \quad n = 1000 .

A good generator should pass two tests with this judge.

Each image is recognizable. The classifier is confident: most of the mass of p(y∣x)p(y \mid x) sits on one class. A blob that looks a little like everything gets a flat output.

The set is varied. Over many samples, the predicted classes spread across many classes instead of piling up on one.

Neither test is enough on its own. A generator that draws the same perfect beagle again and again passes the first test and fails the second. A generator of shapeless blobs gives every image a flat output. Overall no class is favored, yet no single image is recognizable.

Two entropies

Both tests talk about uncertainty, and Entropy, cross-entropy and KL gave us a number for it. A confident output has low entropy:

H(p(y∣x))=−∑yp(y∣x)log⁡p(y∣x).H(p(y \mid x)) = -\sum_{y} p(y \mid x) \log p(y \mid x).

Logarithms are natural, so entropies are in nats. We use 0log⁡0=00 \log 0 = 0, which matters when a softmax underflows to zero.

For variety we need the distribution of predicted classes over the whole set. Average the classifier's outputs over generated images. The result is the marginal label distribution:

p(y)=Ex∼Qθ p(y∣x)≈1N∑i=1Np(y∣xi).p(y) = \mathbb E_{x \sim Q_\theta}\, p(y \mid x) \approx \frac1N \sum_{i=1}^N p(y \mid x_i).

It needs only the samples xix_i and the classifier, never the generator's density. So it works for a GAN as well as for a flow. A varied set has a spread-out marginal, with entropy close to the maximum log⁡n\log n.

We want each H(p(y∣x))H(p(y \mid x)) to be low and H(p(y))H(p(y)) to be high.

One KL instead of two entropies

We could report the two entropies separately. Salimans et al. folded them into one number. For each image, take the KL divergence of its prediction from the marginal, and average over images. Expanding the definition of KL, we get

Ex DKL(p(y∣x) ∥ p(y))=Ex∑yp(y∣x)log⁡p(y∣x)  −  ∑y(Ex p(y∣x))log⁡p(y).\mathbb E_x\, D_{\mathrm{KL}}\big(p(y \mid x) \,\|\, p(y)\big) = \mathbb E_x \sum_y p(y \mid x) \log p(y \mid x) \;-\; \sum_y \Big(\mathbb E_x\, p(y \mid x)\Big) \log p(y).

The sum over classes is finite, so the expectation moves inside it freely. The first term is −ExH(p(y∣x))-\mathbb E_x H(p(y \mid x)). In the second, the bracket is exactly p(y)p(y), so the term is +H(p(y))+H(p(y)):

Ex DKL(p(y∣x) ∥ p(y))=H(p(y))−Ex H(p(y∣x)).\mathbb E_x\, D_{\mathrm{KL}}\big(p(y \mid x) \,\|\, p(y)\big) = H(p(y)) - \mathbb E_x\, H(p(y \mid x)).

Both tests now push one number up: a varied set raises the first term, confident images shrink the second. An image scores well when its prediction is far from the average prediction. The difference on the right is the mutual information I(X;Y)I(X; Y) between a generated image and its predicted class (Barratt and Sharma, 2018): how much the image tells us about the class.

The Inception Score exponentiates it:

IS=exp⁡(Ex∼Qθ DKL(p(y∣x) ∥ p(y))).\mathrm{IS} = \exp\Big(\mathbb E_{x \sim Q_\theta}\, D_{\mathrm{KL}}\big(p(y \mid x) \,\|\, p(y)\big)\Big).

Higher is better. In code, with the outputs for NN samples stacked into an N×nN \times n array:

import numpy as np
from scipy.special import rel_entr      # p * log(p / q), with 0 log 0 = 0
 
def inception_score(probs):             # probs: N x n softmax outputs
    p_y = probs.mean(axis=0)            # the marginal
    kl = rel_entr(probs, p_y).sum(axis=1)
    return np.exp(kl.mean())

Reading the number

Back to three classes: beagle, corgi, husky. A confident output puts 0.90.9 on one class and 0.050.05 on each of the others; its entropy is 0.3940.394 nats. Each generator below produced three images.

Generator Outputs H(p(y))H(p(y)) ExH(p(y∣x))\mathbb E_x H(p(y \mid x)) IS
A: confident and varied one peaked output per class ln⁡3≈1.099\ln 3 \approx 1.099 0.3940.394 2.022.02
B: mode collapse three outputs peaked on beagle 0.3940.394 0.3940.394 1.001.00
C: not confident three flat outputs 1.0991.099 1.0991.099 1.001.00

B and C fail in opposite ways and land on the same score. In both, every image gets the same output, so each one's KL to the marginal is zero. That is the bottom of the scale: IS =1= 1 exactly when all images get the same class distribution.

The exponential turns entropies into counts. A uniform distribution over kk classes has entropy log⁡k\log k, so eHe^{H} is the effective number of classes. Splitting the exponential of the difference gives

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

This is the number of classes the set covers, divided by the number of classes per image. A covers 33 classes, each of its images looks like about 1.481.48 classes, and 3/1.48≈2.023 / 1.48 \approx 2.02.

The bounds come from the entropy topic. KL is never negative, so IS≥1\mathrm{IS} \ge 1. In the difference of entropies, the first is at most log⁡n\log n and the second is at least 00, so IS≤n\mathrm{IS} \le n. The base of the logarithm does not matter as long as the exponential uses the same base.

Where the score breaks

A single number from a frozen network is convenient. It is also fragile in three ways.

It can be gamed. The score is a differentiable function of the image. Optimize it directly and you get adversarial examples: tiny changes that make the classifier confident without making the image look any more real. Barratt and Sharma (2018) pushed the score of such images to about 900900. So the IS must never become a training objective, even indirectly.

It depends on the exact network. Keras and PyTorch ports of Inception v3 have almost the same ImageNet accuracy. Yet their scores differ by about 3.5%3.5\% on ImageNet validation images and by about 11.5%11.5\% on CIFAR-10 (Barratt and Sharma, 2018). And ImageNet's classes describe dogs, cars and teapots. For faces, handwritten digits or medical scans, the score measures almost nothing.

It depends on the protocol. Variety is a property of the set, so the marginal must be estimated from many images. Papers usually follow Salimans et al. (2016): the mean and standard deviation of the score over 10 splits of 50005000 images, 50 00050\,000 samples in total, each split with its own marginal. A score computed with another sample size or another split is not comparable.

It never sees real data

Look at the formula once more. It contains the generator's samples and the classifier, and nothing else. No real image ever enters it.

So a generator that returns its training images unchanged gets the score of real data: memorization is invisible. And the score rewards a uniform marginal over ImageNet's 1000 classes, whatever the data looks like. Train on dogs only and a perfect generator covers about 120 dog breeds, so its IS is at most about 120120. A worse generator that wanders over cars and teapots can beat it.

The fix keeps what works. A frozen network stays as a fixed ruler, the same for every model, and the score still needs nothing but samples. What changes is that a set of real images enters the comparison: we ask how far the generated set is from the real one. That is the next topic, Fréchet Inception Distance.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.