Search

Search papers and lessons

MaskGIT: Masked Generative Image Transformer

Huiwen Chang, Han Zhang, Lu Jiang et al. · 2022

MaskGIT generates an image by filling a grid of hidden tokens over a few rounds, keeping the most confident guesses each round, far fewer passes than one token at a time.

On this page

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

TL;DR

MaskGIT is a way to generate images with a transformer. Instead of writing an image one token at a time, left to right, it starts with every token hidden and fills them in over about 8 to 12 rounds, keeping the guesses it is most sure of in each round. A model trained to fill in hidden tokens from both sides makes this possible. The result is higher image quality than the autoregressive transformer it was compared with, and much less work per image.

Builds on: Masked Language Modeling, Vector Quantization, Autoregressive Model, Transformer · Leads to: Masked Autoencoder

Why it mattered

Most strong image generators in 2022 were one of two kinds: GANs, which are fast but hard to train, and autoregressive transformers, which train stably but generate slowly. MaskGIT showed that a masked-token model could match or beat the autoregressive transformer on ImageNet at 256 by 256 and 512 by 512 pixels, and could match some GANs and diffusion models, while taking 8 or 12 neural-network passes per image instead of 256 or 1024. The same paper showed that one trained model could be used for inpainting and outpainting, which are image-editing tasks that autoregressive models handle poorly. Later masked-token image models, such as Google's MUSE (2023), build on the same decoding idea.

The problem

A generative transformer for images works in two stages. First, an image is compressed into a grid of discrete tokens by a Vector Quantization tokenizer (a VQGAN-style encoder and decoder). Second, a transformer learns the statistics of those token grids. The second stage was almost always an Autoregressive Model: it predicts token 1, then token 2 given token 1, and so on in raster-scan order. Two things go wrong with this:

  • Speed. A 16 by 16 token grid needs 256 sequential passes through the transformer. A 32 by 32 grid needs 1024. The passes cannot run in parallel because each one needs the previous output.
  • Shape. Raster order treats the image as a line of text. Early tokens cannot see later ones, so the model commits to the top of an image before it knows what the bottom will be. The authors compare this to a painter who must paint one line at a time, when a painter normally sketches the whole picture first and refines it.

MaskGIT keeps the first stage unchanged and replaces the second.

How it works

The walkthrough below uses a tiny grid of 16 tokens (a 4 by 4 grid) and 4 rounds. The real model uses the same steps on a 16 by 16 or 32 by 32 grid.

  1. Start blank. Every one of the N = 16 token positions holds a special [MASK] token. Nothing is known yet.
  2. Predict everywhere at once. One forward pass of a bidirectional transformer outputs a probability over the codebook for every masked position. Each position can see all the other positions, so it uses context from every direction. Check: 16 positions, each gets a probability list; the total work is one pass, not 16.
  3. Sample and score. At each masked position, draw one token from its probability list. Record that token's probability as its confidence. Positions that were already filled keep confidence 1.0. Check: a position where the model says "token 37 with probability 0.9" gets confidence 0.9; a position that says "0.2" gets confidence 0.2.
  4. Keep the confident ones, re-mask the rest. Decide how many tokens should still be hidden after this round. Keep the rest, the ones with the highest confidence, and hide the others again. This is where the mask schedule enters (next step). Check: if 12 tokens should stay hidden and 16 are masked, you keep the 4 most confident draws.
  5. Repeat with fewer hidden tokens. The schedule says how many tokens remain hidden after round t out of T rounds. With the cosine schedule that number is ⌈cos⁡(πt/2T) N⌉\lceil \cos(\pi t / 2T)\, N \rceil. The hidden count falls to zero at round T, so every token has been fixed. The paper's default is T = 8 for ImageNet at 256 by 256.
  6. Output. The final token grid goes through the first-stage decoder, which turns tokens into pixels.

The mask schedule matters most. If too many tokens stay hidden early, the model has to make many guesses that are mostly wrong. If too few stay hidden, it commits too early. MaskGIT uses a schedule that hides most tokens in the first rounds and reveals many in the last rounds. The worked example at the end of "The math" traces the counts.

flowchart TD
  A["All N tokens masked"] --> B["One pass: predict a distribution at every masked position"]
  B --> C["Sample a token at each masked position, keep its confidence"]
  C --> D["Keep the most confident draws; re-mask the rest per schedule"]
  D --> E{"Round t equals T?"}
  E -- "no" --> B
  E -- "yes" --> F["Full token grid, decode to pixels"]

The math

Definition: Masked visual token modeling

Let Y = (y_1, ..., y_N) be the N tokens of an image's grid, each taking one of K values from the codebook, and let M = (m_1, ..., m_N) be a binary mask with m_i = 1 meaning "hidden". Let Y_M̄ denote Y with the hidden positions replaced by [MASK]. A bidirectional transformer is trained to predict the hidden tokens from the visible ones.

Training objective

Training samples a random mask ratio, hides that fraction of tokens, and asks the model to predict the hidden ones. The loss is the negative log-likelihood of the hidden tokens:

Lmask=−EY[∑i: mi=1log⁡p(yi∣YM‾)]\mathcal{L}_{\text{mask}} = -\mathbb{E}_{Y}\left[\sum_{i:\, m_i = 1} \log p(y_i \mid Y_{\overline{M}})\right]

Each term is the log of the probability the model gave to the true token at a hidden position. Summing over hidden positions and averaging over images gives the loss. The sum is only over hidden positions, so the model is never rewarded for copying visible tokens.

Step 2 in symbols: predict and sample

At round t, the model outputs p^(t), an N by K table where row i is a distribution over the K codebook entries. For each hidden position i, draw y_i^(t) from row i of p^(t). Its confidence is c_i = p_i^(t)(y_i^(t)), the probability of the draw. Visible positions get c_i = 1.0.

Step 3 in symbols: the schedule

The mask schedule is a function gamma from [0, 1] to [0, 1] that is continuous, decreasing, with gamma(0) = 1 and gamma(1) = 0. At round t of T, the number of tokens that remain hidden after the round is

nt=⌈γ(t/T) N⌉n_t = \lceil \gamma(t/T)\, N \rceil

The paper's default is the cosine schedule, gamma(r) = cos(pi r / 2). It satisfies both requirements: cos(0) = 1 and cos(pi/2) = 0.

00.20.40.60.8100.20.40.60.81progress r = t / Tfraction still hidden
  • cosine (default)
  • linear
The cosine schedule hides more tokens in the early rounds than the linear one, so the model reveals most of the image late.

Step 4 in symbols: keep and re-mask

Sort the confidences. Keep the N - n_t positions with the highest confidence, set them to their sampled tokens, and mark the other n_t positions as hidden for the next round. Any position already kept stays fixed in later rounds.

Worked numbers for the walkthrough

With N = 16 and T = 4:

  • Round 1: r = 1/4, gamma = cos(pi/8) = 0.9239. n_1 = ceil(0.9239 times 16) = ceil(14.78) = 15. Keep 1 token.
  • Round 2: r = 2/4, gamma = cos(pi/4) = 0.7071. n_2 = ceil(11.31) = 12. Keep 4 tokens.
  • Round 3: r = 3/4, gamma = cos(3 pi/8) = 0.3827. n_3 = ceil(6.12) = 7. Keep 9 tokens.
  • Round 4: r = 1, gamma = 0. n_4 = 0. Keep all 16.

Worked examples

Example
Example 1: counting hidden tokens with a linear schedule

Take N = 4 tokens and T = 2 rounds, with the linear schedule gamma(r) = 1 - r. Find how many tokens stay hidden.

  1. Round 1: r = 1/2, so gamma = 1 - 0.5 = 0.5.
  2. Hidden after round 1: ceil(0.5 times 4) = ceil(2) = 2. So 2 tokens are kept and 2 are re-masked.
  3. Round 2: r = 2/2 = 1, so gamma = 0.
  4. Hidden after round 2: 0. All 4 tokens are fixed.

Interpretation: the linear schedule keeps half the tokens in round 1 and the rest in round 2. Every round fixes the same number of tokens, which is the simplest possible plan.

Example
Example 2: the cosine schedule on the 4 by 4 grid

Use N = 16 and T = 4, as in the walkthrough.

  1. Round 1 hides ceil(cos(pi/8) times 16) = ceil(14.78) = 15 tokens, so only 1 token is fixed. Only one guess is kept.
  2. Round 2 hides ceil(cos(pi/4) times 16) = ceil(11.31) = 12 tokens, so 4 are fixed in total. Three new tokens are kept.
  3. Round 3 hides ceil(cos(3 pi/8) times 16) = ceil(6.12) = 7 tokens, so 9 are fixed in total. Five new tokens are kept.
  4. Round 4 hides ceil(0) = 0 tokens, so all 16 are fixed.

Interpretation: the fixed count goes 1, 4, 9, 16. The gaps grow each round, so the model makes its most confident commitments early when the picture is still vague, and the last rounds fill in many tokens at once. The linear schedule from Example 1 fixes the same number every round, and the paper finds linear schedules worse than concave ones.

Example
Example 3: realistic cost on ImageNet

Use the paper's setting: a 256 by 256 image is compressed by a factor of 16 into a 16 by 16 grid, so N = 256 tokens. A 512 by 512 image gives a 32 by 32 grid, so N = 1024.

  1. A sequential (autoregressive) transformer needs one pass per token. For 256 by 256 that is 256 passes.
  2. MaskGIT uses 8 passes for 256 by 256 in the paper's main table. The ratio is 256 / 8 = 32.
  3. For 512 by 512, the sequential VQGAN baseline in the table needs 1024 passes, and MaskGIT needs 12. The ratio is 1024 / 12, which is about 85.
  4. These are counts of network passes, not wall-clock time. The paper's abstract reports that MaskGIT speeds up autoregressive decoding by up to 64 times. That wall-clock figure is smaller than the pass ratio, so the pass count is not a timing measure.
  5. Quality at the same time: FID 6.18 at 256 by 256 against 15.78 for the published VQGAN number. Lower FID is better.

Interpretation: the speed comes from doing many tokens per pass, and the quality comes from letting every token see the whole image. The pass count gives the first; the FID scores give the second.

Try it: a 512 by 512 image has N = 1024 tokens and MaskGIT uses 12 passes. If every pass fixes the same number of tokens, how many does each pass fix? Work it out before reading on.

Answer: 1024 / 12 is about 85.3 tokens per pass on average. Real schedules fix few tokens early and many late, so the count in any single pass differs from this average.

Results

The paper evaluates on class-conditional ImageNet generation, where the model is given a class label, and on three editing tasks. The main numbers, with the paper's own reported values:

  • ImageNet 256 by 256. MaskGIT reaches FID 6.18 and IS 182.1 in 8 steps. BigGAN-deep reaches FID 6.95 and IS 198.2 but in 1 step with a GAN. ADM, a diffusion model, reaches FID 10.94 in 250 steps. VQGAN reaches FID 15.78 in 256 steps.
  • ImageNet 512 by 512. MaskGIT reaches FID 7.32 in 12 steps, better than BigGAN-deep's 8.43 in the same table.
  • Class-conditional diversity. The paper reports classification accuracy score (CAS), a measure of how well a classifier recognises the generated images' classes. MaskGIT has the highest CAS of the methods with CAS values at both resolutions (63.14 Top-1 at 256, 63.43 at 512).
  • Inpainting (center 50 percent of the image, Places2): FID 7.92, IS 22.95. This beats DeepFill and HiFill but not CoModGAN, which reaches FID 7.13.
  • Outpainting (extending the right half): FID 6.78, IS 11.69, which beats In&Out and Boundless.
  • Mask schedules (ablation, ImageNet 256 by 256): cosine FID 6.06, square 6.35, linear 7.51, logarithmic 29.17. The cosine row here reads 6.06, not the 6.18 of the main table for the same schedule; the paper does not explain the gap. Schedules that hide more tokens early do better, and cosine is best.

Limits. The first-stage tokenizer is the VQGAN one and is not changed by this method. BigGAN-deep is still faster per image in the table, because it is a single pass. FID and IS measure statistics over a set of samples and do not show that any single image is correct.

Reading guide

The paper is short. Read it in this order, with the lesson open beside it.

  1. Abstract and introduction. The problem with raster order and the two-stage recipe. Needs Autoregressive Model and Vector Quantization.
  2. The masked-modeling section. Connects to BERT: Pre-training of Deep Bidirectional Transformers, which trains with the same masked objective on text. Needs Masked Language Modeling.
  3. The training section. The loss in "The math" above. Needs Softmax for the output probabilities and Transformer for the architecture.
  4. The iterative decoding section. The four-step loop from "How it works". No new prerequisites beyond Softmax Temperature, which is used in sampling.
  5. The masking design section. The schedule family and the cosine choice. Compare with Cosine Noise Schedule, which uses a cosine curve for the forward noising process in diffusion.
  6. The experiments section. The tables. Read the FID definition in Fréchet Inception Distance before them.

Check yourself

QuestionRecall: what does MaskGIT predict at a masked position during training, and in what order does it see the other tokens?Show answer

It predicts the true token at that position, given the visible tokens. The model sees tokens in every direction, not only those before it, because it is a bidirectional transformer. That is the key difference from an autoregressive model, which conditions only on earlier tokens.

QuestionComputation: with N = 16 tokens and T = 4 rounds, the cosine schedule gives n_2 = ceil(cos(pi/4) times 16). What is n_2, and how many tokens are fixed after round 2?Show answer

cos(pi/4) = 0.7071, and 0.7071 times 16 = 11.31. The ceiling is 12, so 12 tokens are still hidden after round 2. The number fixed is 16 - 12 = 4.

QuestionWhy: why does the schedule hide most tokens in the early rounds rather than spreading the reveals evenly?Show answer

Early in decoding the model is least certain, so its guesses are mostly wrong. Hiding many tokens forces it to commit only to the few it is most confident about, and it keeps training harder examples through the larger mask ratios. Late in decoding the image has much more context, so many tokens can be fixed at once. The ablation shows concave schedules (like cosine) beat linear and convex ones.

QuestionApplied: a team wants to generate 512 by 512 images with a sequential transformer and a 1024-token grid. They ask why MaskGIT would be faster. Give the pass counts and say what the comparison does not show.Show answer

The sequential model needs 1024 passes, one per token. MaskGIT needs 12 passes in the paper's setup, a ratio of about 85. The comparison does not show wall-clock speed directly, since each MaskGIT pass is heavier than one sequential step. It also does not show that the two models give the same image quality on the same tokenizer; the paper reports quality separately.

Sources

Read the full paper

Huiwen Chang, Han Zhang, Lu Jiang et al., 2022. arXiv:2202.04200