VQGAN (vector-quantized generative adversarial network) turns an image into a sequence of integer codebook indices and reconstructs an image from that sequence. Its generator is a vector-quantized variational autoencoder (VQ-VAE); a discriminator helps its reconstructions look convincing. The discrete codes give transformers image tokens to predict instead of raw pixels. section 11.0 section 11.4.2 section 12.1.2
Trace an image through the model:
- Encode: the encoder compresses the image into a spatial grid of continuous feature vectors. section 11.5.2
- Quantize: replace each vector with its nearest learned codebook vector. Record that vector’s integer index; the grid becomes a token sequence. section 11.5.2
- Decode: look up the vectors by their indices, restore the quantized grid, and pass it through the decoder to reconstruct the image. section 11.5.2
- Train for appearance as well as reconstruction: the loss combines reconstruction, adversarial, and quantization terms. Perceptual reconstruction loss can use LPIPS to compare how images look, rather than only their pixel values. section 11.4.2
Figure 11.6 VQGAN architecture and training process in which VQGAN combines a VQ-VAE generator with a discriminator in a GAN framework
The architecture diagram separates the VQ-VAE generator’s encoder, codebook, and decoder from the discriminator used during adversarial training. section 11.4.2
Follow a cat image into tokens
With the illustrated pretrained model, a 384 × 384 image becomes a latent tensor of shape [1, 256, 24, 24]. Four stages of downsampling leave 576 spatial positions. Each 256-value vector is matched to one of 1,024 codebook entries, yielding 576 integers. Looking up those entries and decoding them reconstructs the image; comparing the result with the original makes the compression visible. section 11.5.2 section 11.5.1
z_q, _ , indices= model.quantize(z) #1 int_sequence = indices[2] #2 print(int_sequence)
The quantization call exposes both the selected latent vectors and the integer indices used as image tokens. section 11.5.2
z_q2 = model.quantize.embedding(int_sequence) z_q2 = z_q2.permute(1,0).view(z_q.shape) #1 z_post2 = model.post_quant_conv(z_q2) z_recon2 = model.decoder(z_post2) #2
This reverse path looks up vectors from the saved indices, reshapes them into a latent grid, and calls the decoder. section 11.5.2
In a transformer-based text-to-image pipeline, VQGAN supplies the image-token targets during training. At generation time, a text-conditioned transformer predicts tokens and the VQGAN decoder turns them into an image. This division makes sequential image generation practical without asking the transformer to predict every pixel directly. section 1.2.2 section 12.1.2
