Skip to content

Taming Transformers for High-Resolution Image Synthesis (VQ-GAN)

Authors: Patrick Esser, Robin Rombach, Björn Ommer (Heidelberg University)

Published: December 2020 (CVPR 2021 oral)

Paper Link: https://arxiv.org/abs/2012.09841


Why This Paper Matters

VQ-GAN is one of the most quietly influential image-generation papers of the 2020s. It showed how to combine the discrete tokenization of VQ-VAE with the perceptual sharpness of GANs, producing a compact "image alphabet" that a Transformer could model autoregressively. The same authors then went on to build Stable Diffusion (#7) using essentially this autoencoder as the latent space. Almost every modern image, video, and even audio model that operates on a compressed latent grid - from Stable Diffusion to MaskGIT to Sora - traces its perceptual encoder back to the recipe introduced here.

Beyond architecture, VQ-GAN demonstrated a powerful principle: rather than forcing Transformers to model every pixel directly, first learn a good vocabulary of visual concepts, then let the Transformer reason at that level. This idea - compress, tokenize, then model - is now standard.


The Problem Before

By 2020 two camps were producing high-quality images:

  • GANs (#2) generated sharp images quickly but were unstable to train and had poor mode coverage.
  • Autoregressive Transformers (PixelCNN, Image GPT) modeled likelihood honestly and gave good diversity but were brutally slow: 256x256 RGB has 196,608 pixel values to predict one at a time, so high-resolution generation was effectively out of reach.

VQ-VAE (#89) had already proposed compressing an image into a small grid of discrete codes (e.g., 32x32 indices instead of 256x256 pixels) so a Transformer only had to predict 1,024 tokens instead of 196k. But standalone VQ-VAE reconstructions were blurry - they minimized pixel-wise L2, which averages over plausible details and gives mushy textures.

The gap: how do you get a discrete latent space that is both compact (Transformer-friendly) and sharp (perceptually realistic)?


The Core Innovation

VQ-GAN keeps the VQ-VAE two-stage recipe but completely upgrades the autoencoder training:

  1. Perceptual loss (LPIPS) replaces pure L2 - it compares deep CNN features instead of raw pixels, so the model is rewarded for matching textures rather than averaging them.
  2. Patch-based adversarial loss - a small PatchGAN discriminator judges whether local 16x16 patches of the reconstruction look real, forcing the decoder to produce crisp, locally plausible details.
  3. Same VQ bottleneck as VQ-VAE: a learned codebook of ~1,024 to 16,384 vectors; each encoder output is snapped to its nearest codebook entry, yielding integer indices.

The result is an autoencoder that compresses a 256x256 image down to a 16x16 grid of integer tokens - a 256x reduction in sequence length - while reconstructing images that are nearly indistinguishable from the originals.

Then, a vanilla GPT-style Transformer is trained to predict these tokens in raster-scan order. Conditioning (class labels, segmentation maps, depth, partial images) is just prepended to the sequence.

Image (256x256x3)
       v
   Encoder (CNN)
       v
   16x16 grid of feature vectors
       v
   Vector quantization -> nearest codebook entry
       v
   16x16 grid of integer codes  (e.g., [42, 1337, 88, ...])
       v
   GPT-style Transformer models p(code_t | code_<t>, condition)
       v
   Decoder (CNN, trained with L1 + LPIPS + GAN loss)
       v
   Reconstructed / generated image

How the Two Stages Work

Stage 1: Train the VQ-GAN autoencoder

The training loss is a weighted sum:

L_total = L_recon (L1)
        + lambda_perc * L_LPIPS (perceptual)
        + lambda_GAN  * L_adv   (PatchGAN)
        + L_VQ        (codebook commitment, from VQ-VAE)

A clever trick: lambda_GAN is computed adaptively from the ratio of perceptual-loss gradients to GAN-loss gradients in the decoder's last layer. This keeps adversarial training stable - if the discriminator starts dominating, lambda_GAN shrinks; if it falls behind, it grows.

The PatchGAN discriminator only looks at small regions, so it focuses on local texture realism rather than global structure (global structure comes from perceptual + reconstruction losses).

Stage 2: Train the Transformer

With the autoencoder frozen, every image in the dataset becomes a sequence of ~256 to ~1024 integer tokens. A GPT-style decoder-only Transformer learns:

p(z_1, z_2, ..., z_N) = product_i  p(z_i | z_<i>, c)

where c is optional conditioning. Because there are only ~1024 tokens (not 196k pixels), Transformers can finally generate megapixel-class images. For very high resolutions the paper uses a sliding attention window, generating tokens locally while attending to a neighborhood - restoring the locality that pixel CNNs naturally have.


A Worked Mental Model

Think of VQ-GAN as constructing a "Lego set" for images:

  • Each codebook vector is a Lego piece - a small chunk of visual concept (a patch of grass, a slant of sky, a corner of an eye).
  • The encoder is the "inverse Lego instructions": given an image, decide which piece goes in each 16x16 grid cell.
  • The decoder is the "Lego assembler": given a grid of pieces, render the final image.
  • The Transformer is a player learning the rules of the Lego set: given the pieces seen so far, predict which piece comes next.

The discriminator's role is to make sure the Lego pieces look like real visual textures rather than mushy averages - and it works on small patches, so it can complain about any local region that looks fake even if the overall structure is right.


Practical Notes

A few details that practitioners care about:

  • Codebook collapse is a known failure mode where most codes go unused and the model relies on a few. The paper uses EMA codebook updates and codebook resets to mitigate this; later work (e.g., Improved VQGAN, FSQ) revisits the discrete bottleneck design.
  • Downsampling factor f trades reconstruction quality for sequence length. f=16 gives 16x16 tokens for a 256-image (manageable for Transformers); f=8 gives 32x32 (much higher fidelity but 4x more tokens to model).
  • Two-stage training is essential: training the autoencoder and the Transformer jointly is unstable. Freeze the autoencoder before Stage 2.
  • The Transformer dominates inference cost: the autoencoder runs once per generation, but the Transformer runs N times (once per token). Most engineering effort in successor systems went into faster Stage-2 models (parallel decoding in MaskGIT, latent diffusion in Stable Diffusion).
  • Why discrete and not continuous? VAEs with continuous latents work too (Stable Diffusion's released VAE is continuous + KL-regularized). Discrete codes were chosen here because Transformers natively model categorical sequences and because discretization acts as a strong regularizer on the latent space.

Limitations

  • Slow autoregressive sampling. Predicting tokens one at a time means generation latency scales linearly with sequence length - generating a 1024x1024 image is painfully slow.
  • Raster-scan order is arbitrary. Forcing the model to predict tokens left-to-right, top-to-bottom doesn't match how humans perceive images; later work (MaskGIT) generates tokens in parallel from most-confident to least-confident.
  • Fixed codebook capacity. The codebook size caps the information density. Too small and reconstructions suffer; too large and many codes go unused.
  • Reconstruction-vs-perception tradeoff. Heavy compression (f=16) gives nicer Transformer training but visible reconstruction artifacts in fine details (text, faces, hands).

Key Results

  • Class-conditional ImageNet at 256x256: FID 15.78, beating prior autoregressive approaches and competitive with strong GANs of the era, with notably better diversity.
  • High-resolution synthesis up to megapixel scale using sliding-window sampling - the first time a Transformer convincingly generated such large images.
  • Conditional generation across many modalities with one architecture: semantic segmentation to image, depth to image, edges to image, pose to image, and image completion. Everything is just "tokens in, tokens out."
  • Codebook usage analyses showed the model genuinely uses thousands of distinct codes - different codes specialize in different visual concepts (grass, brick, sky gradients, eyes, etc.), behaving like a learned visual vocabulary.

Impact and Legacy

VQ-GAN's perceptual + adversarial autoencoder became infrastructure. The two clearest descendants:

  • DALL-E 1 (OpenAI, 2021) used a discrete VAE almost identical in spirit and trained a Transformer over text + image tokens - the first viral text-to-image model.
  • Stable Diffusion (#7) uses a near-identical VQ-GAN-style autoencoder as its latent space. The diffusion U-Net does the heavy lifting instead of a Transformer, but the perceptual encoder is essentially this paper's contribution.

Other lines that flow from VQ-GAN:

  • MaskGIT, Muse, Parti - non-autoregressive or parallel decoding over VQ-GAN tokens.
  • VideoGPT, Phenaki, MagViT, Sora-style video tokenizers - extend the discrete-latent recipe to spatiotemporal grids.
  • Audio LMs (AudioLM, MusicLM, EnCodec) borrow the same "neural codec + Transformer" structure for waveforms.
  • Multimodal LLMs that tokenize images into discrete codes so a single Transformer handles text and images (Chameleon, early Gemini).

The deeper lesson - separate perception from generation by training a strong tokenizer first - is now the default architectural pattern for generative modeling across modalities.


Connections to Other Papers

  • VQ-VAE (#89): Direct predecessor - VQ-GAN inherits the discrete codebook bottleneck and straight-through gradient estimator.
  • GANs (#2): Provides the adversarial loss that gives VQ-GAN its perceptual sharpness, without using GANs for the global generation task.
  • Vision Transformer (#11) / Attention Is All You Need (#1): The Stage-2 model is a standard decoder-only Transformer applied to image tokens.
  • Stable Diffusion (#7): Directly built by overlapping authors; its autoencoder is essentially a VQ-GAN variant (KL-regularized rather than vector-quantized in the final SD release, but architecturally identical).
  • DALL-E 3 (#48): Inherits the "tokenize-then-model" lineage; modern multimodal models still echo this two-stage pattern.
  • Sora / DiT (#44): Uses a learned spatiotemporal tokenizer with the same conceptual structure.

Key Takeaways

  • Compression first, modeling second: train a strong perceptual autoencoder, then let a powerful sequence model work in the compact latent space.
  • Pixel-wise losses give blurry reconstructions; perceptual + adversarial losses give sharp ones - even when the bottleneck is heavily discretized.
  • A 16x16 grid of discrete tokens is enough to represent a 256x256 image well, turning a 196k-step generation problem into a ~256-step one.
  • Adaptive loss weighting (balancing perceptual vs. adversarial via gradient norms) is what makes GAN-augmented autoencoders trainable.
  • Architectural ancestor of Stable Diffusion and most modern tokenized-image / tokenized-video / tokenized-audio systems.