Search

Search papers and lessons

Unsupervised Representation Learning with Deep Convolutional GANs

Alec Radford, Luke Metz, Soumith Chintala · 2015

Radford et al. (2015) give a set of convolutional design rules that make GANs train stably on images, and show that the features a trained GAN learns can classify images it never saw.

On this page

  • Colour = topic mix
  • Probability
  • Discrete math
  • Calculus
  • Neural network basics
  • Linear algebra
  • Optimizers
  • ReadReady to readNot yet

TL;DR

A generative adversarial network trains two networks at once: a generator that turns random noise into images, and a discriminator that tries to tell those images from real ones. Such networks were hard to train on pictures. Radford, Metz and Chintala gave a set of convolutional design rules that made training stable across several image datasets. They also showed that the discriminator, once trained, can serve as a feature extractor: a linear classifier on its features beats the k-means methods on CIFAR-10, and on SVHN with 1,000 labels the same features beat a supervised CNN with the same architecture.

Builds on: Generative Adversarial Network, Convolutional Neural Network, Transposed Convolution, Batch Normalization, Adam Optimizer, Rectified Linear Unit (ReLU) · Leads to: later convolutional GAN generators, which adopt the design rules in this paper.

Why it mattered

Before this paper, the authors report, attempts to scale GANs to natural images with convolutional networks had repeatedly failed. The common failure was unstable training, in which the generator produced nonsense or collapsed to a few repeated samples. The paper's architecture rules gave a concrete recipe that others could follow.

The paper also raised the question of what GANs learn internally. Its experiments on the generator and discriminator, with the features they learn and the way the latent space behaves, became a common reference for later work on representation learning from images. The paper has about 17,000 citations on OpenAlex.

The problem

A GAN has a minimax game built into it. The generator tries to produce samples the discriminator cannot tell apart from real data, and the discriminator tries to spot the generator's samples. In principle the two reach an equilibrium where the generator's samples match the data. In practice, the paper says, GANs were unstable and hard to scale to images. Earlier attempts to use convolutional networks in GANs failed, and the authors had the same difficulties when they tried common supervised CNN designs.

A second problem concerns representation learning. Unsupervised methods such as k-means feature learning, autoencoders and deep belief networks learn features from unlabelled images, but their results on classification trailed what supervised networks could do. The paper asks whether a GAN, which has no reconstruction loss and so does not need pixel-by-pixel matching, could learn features worth reusing. The question has two parts: can the training be made stable, and do the features mean anything.

How it works

The walkthrough follows one forward pass through the generator, then the training loop, with the design rules applied in order.

  1. Draw a random code. The generator takes a 100-dimensional vector zz drawn uniformly. The paper calls the distribution ZZ.
  2. Project it to a small grid. A linear step turns the vector into a small spatial block with many feature maps. The paper notes that this first layer is a matrix multiplication, and it is the only fully connected step in the generator: no hidden fully connected layers follow.
  3. Upsample with strided convolutions. Four fractionally-strided convolutions double the spatial size each time. The paper warns that these layers are wrongly called deconvolutions. Toy check: four doublings take a 4 by 4 grid to 64 by 64, the image size in the paper.
  4. Normalise and activate. Batch normalisation runs after each layer except the generator's output and the discriminator's input. ReLU is used throughout the generator, except the output, which uses tanh to produce values between minus 1 and 1.
  5. Score with the discriminator. The discriminator is a stack of strided convolutions, not pooling layers, with leaky ReLU activations of slope 0.2. Its last convolution is flattened into one sigmoid output that estimates the probability that an input is real.
  6. Update both networks. Each training step updates the discriminator on a batch of real and generated images, then the generator to make its samples score as real. Both use Adam with a learning rate of 0.0002 and first-moment coefficient 0.5.
  7. Reuse the discriminator. After training, the discriminator's convolutional features are taken from every layer, pooled to a 4 by 4 grid, flattened, and fed to a linear classifier.

Tiny shape trace for step 3: a fractionally-strided layer with stride 2, kernel size 4 and padding 1 applied to a 4 by 4 input gives (4−1)⋅2−2+4=8(4-1)\cdot 2 - 2 + 4 = 8, so the output is 8 by 8. Applying it again to the 8 by 8 map gives 16 by 16, and four such layers from 4 by 4 give 64 by 64.

flowchart LR
  Z["Random code z, 100 numbers"] --> P["Project to small grid, no hidden dense layers"]
  P --> U["Four strided upsampling convolutions"]
  U --> G["Generated image, 64 by 64"]
  G --> D["Discriminator: strided convolutions, leaky ReLU"]
  R["Real image"] --> D
  D --> S["Probability the input is real"]

The math

Step 1: the code

The generator GG maps a code zz to an image, x=G(z)x = G(z). The paper draws zz from a uniform distribution, so each component is equally likely anywhere in its range. Nothing in the paper says the code must be Gaussian, though many later papers use one.

Step 2 and 3: the upsampling arithmetic

A strided convolution with input size nn, kernel size kk, stride ss and padding pp gives an output of size

⌊n+2p−ks⌋+1.\left\lfloor \frac{n + 2p - k}{s} \right\rfloor + 1.

A fractionally-strided (transposed) convolution with stride ss gives

nout=(n−1)s−2p+k.n_{\text{out}} = (n - 1)s - 2p + k.

With s=2s = 2, k=4k = 4 and p=1p = 1, each layer doubles the size, which is the setting the walkthrough used.

Step 4: batch normalisation

For a mini-batch of values x1,…,xmx_1, \dots, x_m in one feature map, batch normalisation computes

x^i=xi−μBσB2+ϵ,yi=γx^i+β,\hat{x}_i = \frac{x_i - \mu_B}{\sqrt{\sigma_B^{2} + \epsilon}}, \qquad y_i = \gamma \hat{x}_i + \beta,

where μB\mu_B and σB2\sigma_B^2 are the batch mean and variance, ϵ\epsilon is a small constant for stability, and γ\gamma and β\beta are learned scale and shift. The paper notes that applying it to every layer caused oscillation, so it skips the generator output and the discriminator input.

Step 5: the activations

Leaky ReLU with slope a=0.2a = 0.2 is

f(x)={xx>00.2 xx≤0.f(x) = \begin{cases} x & x > 0 \\ 0.2\,x & x \le 0. \end{cases}

The generator's output uses tanh, which maps values into (−1,1)(-1, 1). The paper scales every training image to that same range.

Step 6: the training game

The GAN objective is the minimax game

min⁡Gmax⁡D  Ex[log⁡D(x)]+Ez[log⁡(1−D(G(z)))],\min_G \max_D \; \mathbb{E}_{x}\big[\log D(x)\big] + \mathbb{E}_{z}\big[\log\big(1 - D(G(z))\big)\big],

where D(x)D(x) is the discriminator's probability that xx is real. Toy check: if D(x)=0.9D(x) = 0.9 for a real image and D(G(z))=0.2D(G(z)) = 0.2 for a generated one, the bracketed terms are log⁡0.9+log⁡0.8≈−0.105−0.223=−0.328\log 0.9 + \log 0.8 \approx -0.105 - 0.223 = -0.328. The discriminator pushes this value up and the generator pushes it down.

The paper trains with mini-batches of 128 and with Adam at learning rate 2×10−42 \times 10^{-4} and β1=0.5\beta_1 = 0.5. The authors found that the usual β1=0.9\beta_1 = 0.9 made training oscillate. Weights are initialised from a zero-centred normal distribution with standard deviation 0.020.02.

Step 7: the feature vector

The feature vector for classification is built from every discriminator layer, pooled to a 4×44 \times 4 grid. Toy check: if the top layer has 512 maps pooled to 4 by 4, it contributes 512×16=8192512 \times 16 = 8192 numbers. The paper's full vector has 28,672 numbers, so the lower layers contribute the rest. A regularised linear L2-SVM is trained on that vector.

Results

Classification with learned features. The discriminator is trained on ImageNet-1k images at 32 by 32 pixels, then its features classify CIFAR-10 with a linear L2-SVM:

ModelAccuracyAccuracy with 400 labels per classLargest feature count
1-layer k-means80.6%63.7%4800
3-layer k-means with learned receptive fields82.0%70.7%3200
View-invariant k-means81.9%72.6%6400
Exemplar CNN84.3%77.4%1024
DCGAN features with L2-SVM82.8%73.8%512

The DCGAN beats every k-means method, while using fewer feature maps in its top layer. It remains behind the exemplar CNN, which uses aggressive augmentation. The paper notes that the DCGAN features form a larger vector, 28,672 numbers, from fewer feature maps, which is a trade-off rather than a free gain.

SVHN digits with 1,000 labels. With 1,000 labelled digits, the DCGAN features with an L2-SVM reach 22.48% test error. The paper's supervised CNN with the same architecture, tuned by random search, reaches 28.87% on the validation set. The supervised figure is a validation result, so the two numbers are not measured on the same split.

MNIST nearest neighbours. A conditional DCGAN trained on MNIST and used as a generator for a nearest-neighbour classifier reaches 1.48% test error with 10 million generated samples, against 3.1% for real data with 50,000 examples. The paper reports that its GAN baseline reached 5.65% at the same size.

Latent space. Interpolating between random codes produces smooth transitions. Averaging the codes of three exemplars of a concept, then doing vector arithmetic on those averages, produces images that follow the arithmetic. The paper also shows that face pose changes along a linear direction in the code, which it calls a turn vector.

Limits and misreadings. The paper admits that instability remains, that filters can collapse, and that the features were not tuned on CIFAR-10. Several misreadings are worth avoiding:

  • "Strided layers that grow an image are deconvolutions, the inverse of convolution." They are not. A transposed convolution is a different linear map that produces a larger output, which is why the paper objects to the name deconvolution.
  • "Stable GAN training comes from a clever loss." The paper's gains came from architecture rules: strided convolutions in place of pooling, no hidden dense layers, batch normalisation with its exceptions, and specific activations. The loss was the standard GAN loss.
  • "A faster learning rate speeds up the GAN." The paper found 0.001 too high and used 0.0002, and it found the default momentum unstable.
  • "Good samples mean the model has learned useful features." The paper tests features through classification, because a model can produce convincing samples while its features are still weak.

The SVHN comparison is against a supervised CNN with the same architecture, not against the best tuned supervised methods. The vector-arithmetic results are shown on selected examples. The paper also states that it does not use nearest-neighbour search or log-likelihood to assess the model, since both are poor measures for image models.

Reading guide

The paper is short, and the appendix and figures carry much of the detail. Read it in this order.

Paper sectionWhat it doesRead first
1 IntroductionMotivates GANs as a route to representationsGenerative Adversarial Network
2 Related WorkSurveys clustering, autoencoders, and generative modelsAutoencoder, Variational Autoencoder
3 Approach and Model ArchitectureThe architecture rules, Figure 1, the activationsConvolutional Neural Network, Transposed Convolution, Batch Normalization, Rectified Linear Unit (ReLU)
4 Details of Adversarial TrainingDatasets, batch size, initialisation, AdamAdam Optimizer, Stochastic Gradient Descent, He (Kaiming) Initialization
5 Empirical ValidationCIFAR-10 and SVHN featuresFine-Tuning
6 Internals of the NetworksLatent walks, filter visualisation, dropping windows, vector arithmeticGaussian (Normal) Distribution
7 ConclusionThe remaining instability and future worknone

The Section 4 training details are short but matter for reproduction, so read them before attempting the experiments.

Check yourself

QuestionName the three changes the paper makes to ordinary CNNs so that GANs train stably.Show answer

Replace pooling with strided convolutions, so the network learns its own downsampling and upsampling. Remove fully connected hidden layers, keeping only the input projection of the generator and the flattened output of the discriminator. Use batch normalisation in both networks, except at the generator output and the discriminator input.

QuestionAn 8 by 8 feature map goes through a fractionally-strided convolution with stride 2, kernel size 4 and padding 1. What is the output size?Show answer

nout=(n−1)s−2p+k=(8−1)⋅2−2+4=16n_{\text{out}} = (n - 1)s - 2p + k = (8 - 1)\cdot 2 - 2 + 4 = 16. The output is 16 by 16.

QuestionThe paper uses tanh at the generator output rather than ReLU, and scales training images to the range minus 1 to 1. Why does the choice of range matter?Show answer

A bounded output activation lets the generator saturate and cover the colour range of the training data quickly, and the images are scaled to match the range of tanh. If the outputs and the data used different ranges, the generator would be asked to produce values it cannot output.

QuestionOn SVHN with 1,000 labels, the DCGAN features give 22.48% error and the supervised CNN with the same architecture gives 28.87%. What does that suggest, and what is the caveat?Show answer

It suggests that features learned from unlabelled images, by the discriminator, can help when labels are scarce, and that the architecture alone does not account for the gain. The caveat is that the supervised figure is a validation result, while the DCGAN figure is a test result, and the baseline is not tuned to the level of the best supervised methods.

Sources

Read the full paper

Alec Radford, Luke Metz, Soumith Chintala, 2015. arXiv:1511.06434