Search

Search papers and lessons

Glow: Generative Flow with Invertible 1x1 Convolutions

Diederik P. Kingma, Prafulla Dhariwal · 2018

Glow is a flow-based image model that swaps the fixed channel reversal for a learned invertible 1x1 convolution, improves log-likelihood on benchmarks and synthesises 256 by 256 faces.

On this page

  • Colour = topic mix
  • Linear algebra
  • Probability
  • Calculus
  • Discrete math
  • Generative models
  • Architectures
  • ReadReady to readNot yet

TL;DR

Glow is a generative model for images that learns an exact probability for each image, and that can also generate new images by running the model backwards. It is a flow: a stack of invertible steps that turn an image into simple Gaussian noise and back. Its two new ingredients are actnorm, a per-channel scale and shift set from the data, and a learned invertible 1x1 convolution in place of a fixed reversal of channels. The paper shows this improves log-likelihood on standard benchmarks and yields realistic 256 by 256 face images.

Builds on: Normalizing Flow, Affine Coupling Layer, Pointwise (1x1) Convolution, Change of Variables Formula, Maximum Likelihood Estimation · Leads to: later flow models for images that use learned channel mixing in place of fixed permutations

Why it mattered

The paper's authors argue that likelihood-based image models were held back by a trade-off. Autoregressive models give exact likelihoods but generate one value at a time, which is slow for large images. Variational autoencoders train in parallel but optimise only a lower bound on the likelihood. Generative adversarial networks give sharp samples but have no encoder and no likelihood. Flow models offer exact likelihoods, exact inference and parallel synthesis, but the paper says they had "gained little attention in the research community". Glow is the authors' attempt to show that a flow can match the quality of other approaches on large images, which the paper claims is the first likelihood-based model to synthesise high-resolution natural images efficiently, to the authors' knowledge.

The problem

A generative model must assign a probability to every image, and the usual way to train it is to maximise the log-likelihood of the training data. For a flow, the difficulty is computing that log-likelihood. A flow maps an image x to a latent z through invertible steps, and the density of x is the density of z times a correction for how the steps stretch space. Computing that correction, the log-determinant of a Jacobian, is cheap only when the steps are chosen with care. Earlier flows (NICE and RealNVP) used a fixed reversal of the channel order between coupling layers to let every channel affect every other one. The paper's question is whether a learned mixing step can do this better, and still keep the log-determinant cheap.

How it works

A Glow model is a stack of steps, grouped into levels. Each step has three parts, shown in the paper's Figure 2: actnorm, then an invertible 1x1 convolution, then an affine coupling layer. Here is one step traced on a tiny image.

  1. Start with a tensor. Take a 2 by 2 image with 3 channels, so there are 4 spatial positions and each has 3 numbers. Call it h.
  2. Actnorm. Scale and shift each channel by a learned value. The values are set on the first minibatch so each channel starts with zero mean and unit variance. This replaces batch normalization, which the paper says degrades with small per-device minibatches. The log-determinant of this step is simple: the sum of the log scales times the number of positions.
  3. Invertible 1x1 convolution. At each position, multiply the 3 channel values by a learned 3 by 3 matrix W. The same W is used at every position. This mixes the channels. If W is invertible, the step can be reversed by multiplying with the inverse of W.
  4. Its log-determinant. Because the same matrix acts at each of the 4 positions, the log-determinant is 4 times log of the absolute determinant of W. If det(W) = 2, the value is 4 × log 2 ≈ 2.77.
  5. Affine coupling. Split the channels into two halves. Use one half to compute a scale and a shift with a small neural network, and apply them to the other half. The half used as input is copied unchanged, so the step can be inverted: knowing the output halves, you recompute the scale and shift from the unchanged half and undo the transform.
  6. Stack and check. Repeat the three parts K times per level, and arrange the levels in the multi-scale architecture that the paper takes from RealNVP. Check that every step has an inverse, which is what makes exact inference and sampling possible.
flowchart LR
    X["image x"] --> A["actnorm: per-channel scale and shift"]
    A --> C["invertible 1x1 convolution W"]
    C --> F["affine coupling layer"]
    F --> R["repeat K times per level"]
    R --> Z["latent z: Gaussian noise"]

The math

Each step in the walkthrough has a formula. Symbols follow the paper: x is the data, h are intermediate tensors, z is the latent, and c is the number of channels.

Step 2 in symbols: actnorm

Actnorm applies yi,j=s⊙xi,j+b\mathbf{y}_{i,j} = \mathbf{s} \odot \mathbf{x}_{i,j} + \mathbf{b} at each spatial position (i,j)(i, j), where ⊙\odot is element-wise multiplication, s\mathbf{s} is a per-channel scale and b\mathbf{b} a per-channel shift. Its log-determinant is h⋅w⋅sum(log⁡∣s∣)h \cdot w \cdot \mathrm{sum}(\log |\mathbf{s}|) for an image of height hh and width ww.

Step 3 in symbols: the invertible 1x1 convolution

The layer applies yi,j=Wxi,j\mathbf{y}_{i,j} = \mathbf{W}\mathbf{x}_{i,j} with W\mathbf{W} a c×cc \times c matrix. Its inverse is W−1\mathbf{W}^{-1}. The log-determinant is

log⁡∣det⁡(d conv2D(h;W)d h)∣=h⋅w⋅log⁡∣det⁡(W)∣\log \left| \det \left( \frac{d\,\mathrm{conv2D}(\mathbf{h}; \mathbf{W})}{d\,\mathbf{h}} \right) \right| = h \cdot w \cdot \log |\det(\mathbf{W})|

Computing or differentiating det⁡(W)\det(\mathbf{W}) costs O(c3)\mathcal{O}(c^{3}), while the convolution itself costs O(h⋅w⋅c2)\mathcal{O}(h \cdot w \cdot c^{2}). The paper initialises W as a random rotation, whose determinant has absolute value 1 and log-determinant 0.

To make the determinant cheap for large cc, the paper parameterises W through its LU decomposition:

W=PL(U+diag(s))\mathbf{W} = \mathbf{P}\mathbf{L}(\mathbf{U} + \mathrm{diag}(\mathbf{s}))

where P is a fixed permutation matrix, L is lower triangular with ones on the diagonal, U is upper triangular with zeros on the diagonal, and s is a vector. The log-determinant then reduces to sum(log⁡∣s∣)\mathrm{sum}(\log |\mathbf{s}|), which costs O(c)\mathcal{O}(c). The paper notes that for its networks it did not measure a large difference in wall-clock time.

Step 5 in symbols: the affine coupling layer

Split x\mathbf{x} into halves xa,xb\mathbf{x}_{a}, \mathbf{x}_{b}. A neural network computes (log⁡s,t)=NN(xb)(\log \mathbf{s}, \mathbf{t}) = \mathrm{NN}(\mathbf{x}_{b}). Then

ya=s⊙xa+t,yb=xb\mathbf{y}_{a} = \mathbf{s} \odot \mathbf{x}_{a} + \mathbf{t}, \qquad \mathbf{y}_{b} = \mathbf{x}_{b}

The inverse recomputes (log⁡s,t)(\log \mathbf{s}, \mathbf{t}) from yb\mathbf{y}_{b}, which equals xb\mathbf{x}_{b}, and then sets xa=(ya−t)/s\mathbf{x}_{a} = (\mathbf{y}_{a} - \mathbf{t}) / \mathbf{s}. The log-determinant is sum(log⁡∣s∣)\mathrm{sum}(\log |\mathbf{s}|), because the Jacobian is triangular.

The full log-likelihood

Stacking the steps f=f1∘⋯∘fK\mathbf{f} = \mathbf{f}_{1} \circ \cdots \circ \mathbf{f}_{K} with h0=x\mathbf{h}_{0} = \mathbf{x} and hK=z\mathbf{h}_{K} = \mathbf{z}, the change of variables gives

log⁡pθ(x)=log⁡pθ(z)+∑i=1Klog⁡∣det⁡(dhi/dhi−1)∣\log p_{\boldsymbol{\theta}}(\mathbf{x}) = \log p_{\boldsymbol{\theta}}(\mathbf{z}) + \sum_{i=1}^{K} \log |\det(d\mathbf{h}_{i}/d\mathbf{h}_{i-1})|

with pθ(z)=N(z;0,I)p_{\boldsymbol{\theta}}(\mathbf{z}) = \mathcal{N}(\mathbf{z}; 0, \mathbf{I}) in the standard setup. Training minimises the average negative log-likelihood over the data. For images, the paper reports results in bits per dimension, the standard conversion: the negative log-likelihood divided by the number of dimensions and by log 2, which turns nats into bits.

Results

The paper's first experiment is an ablation on CIFAR-10. It compares three ways to permute channels, RealNVP's reversal, a fixed random permutation, and the learned 1x1 convolution, with additive and affine coupling. The 1x1 convolution gives lower negative log-likelihood and converges faster in both cases, and the affine models converge faster than the additive ones. The authors report a 0.2% increase in parameters and about a 7% increase in wall-clock time.

The main comparison is with RealNVP, in bits per dimension (lower is better) from Table 2:

  • CIFAR-10: RealNVP 3.49, Glow 3.35.
  • ImageNet 32 by 32: RealNVP 4.28, Glow 4.09.
  • ImageNet 64 by 64: RealNVP 3.98, Glow 3.81.
  • LSUN bedroom: RealNVP 2.72, Glow 2.38.
  • LSUN tower: RealNVP 2.81, Glow 2.46.
  • LSUN church outdoor: RealNVP 3.08, Glow 2.67.

Glow is better on every dataset in the table. The paper's Table 3 gives Glow's results on 5-bit images: CIFAR-10 1.67, ImageNet 32 by 32 1.99, ImageNet 64 by 64 1.76 and CelebA HQ 256 by 256 1.03. These use a different bit depth, so they are not directly comparable with Table 2. The paper also reports that sampling a 256 by 256 image with its largest models takes under a second on current hardware, with a footnote giving about 130 ms on one 1080 Ti GPU at batch size 1.

Limits. The paper gives no formal guarantee that the learned permutation is better in general, only the experiments above. Sampling uses a temperature, and the authors chose 0.7 for the faces as "a sweet spot for diversity and quality", which means the reported sample quality depends on a tuned setting. The face experiments use the CelebA HQ dataset with a split of 27000 training and 3000 validation images, and 5-bit inputs, which trade a little colour fidelity for visual quality, as the paper states. The attribute editing in Figure 6 relies on averaged latent codes from CelebA labels, a small amount of supervision applied after training.

Reading guide

The paper has a short background section, so read it in this order with the lessons it leans on.

Check yourself

QuestionIn the 1x1 convolution step, the image has h = 4 and w = 4, and det(W) = 3. What is the log-determinant of this layer, in natural log, to two decimal places?Show answer

The log-determinant is h · w · log |det(W)| = 16 × log 3. Since log 3 ≈ 1.0986, the value is about 17.58.

QuestionWhy does the paper parameterise W through its LU decomposition?Show answer

The log-determinant of a general c by c matrix costs O(c³) to compute, while the LU form reduces it to the sum of log absolute values of the diagonal vector s, which costs O(c). The gain grows with c.

QuestionWhy is an affine coupling layer invertible even though the scale and shift come from a neural network?Show answer

The copied half x_b passes through unchanged, so the inverse can recompute the same scale and shift from y_b, which equals x_b. Then it undoes the affine map on the other half. The network itself never needs to be inverted.

QuestionOn the CIFAR-10 numbers in Table 2, how much lower is Glow's bits per dimension than RealNVP's, and in percent of RealNVP's value?Show answer

The difference is 3.49 − 3.35 = 0.14 bits per dimension. As a fraction of RealNVP's 3.49, that is 0.14 / 3.49 ≈ 0.040, or about 4%.

QuestionScenario: a team wants a generative model that gives exact likelihoods and can encode an image into a latent code in one pass. Why might a flow such as Glow fit better than a GAN for this goal?Show answer

A flow is invertible, so it gives exact likelihoods and exact latent inference. The paper points out in its introduction that GANs have no encoder at all to infer latents. Glow therefore fits when a team needs both likelihoods and encoding, though it still has to be trained and checked against the data.

Sources

Read the full paper

Diederik P. Kingma, Prafulla Dhariwal, 2018. arXiv:1807.03039