A Pytorch Reproduction of Masked Generative Image Transformer

Victor Besnier, Mickaël ChenarXiv 2023

Abstract

In this technical report, we present a reproduction of MaskGIT: Masked Generative Image Transformer, using PyTorch. The approach involves leveraging a masked bidirectional transformer architecture, enabling image generation with only few steps (8~16 steps) for 512 x 512 resolution images, i.e., ~64x faster than an auto-regressive approach. Through rigorous experimentation and optimization, we achieved results that closely align with the findings presented in the original paper. We match the reported FID of 7.32 with our replication and obtain 7.59 with similar hyperparameters on ImageNet at resolution 512 x 512. Moreover, we improve over the official implementation with some minor hyperparameter tweaking, achieving FID of 7.26. At the lower resolution of 256 x 256 pixels, our reimplementation scores 6.80, in comparison to the original paper's 6.18. To promote further research on Masked Generative Models and facilitate their reproducibility, we released our code and pre-trained weights openly at https://github.com/valeoai/MaskGIT-pytorch/

Introduction

In the landscape of high-fidelity image synthesis, Generative Adversarial Networks (GANs) and Diffusion Models have long been the primary focus. However, Masked Generative Models (MGMs) like MaskGIT have emerged as a powerful alternative. By utilizing a bidirectional transformer to predict masked visual tokens in parallel, MaskGIT offers a significant speed advantage—approximately 64x faster than traditional auto-regressive models that generate images token-by-token.

Despite its potential, the original MaskGIT implementation was released in JAX and lacked the necessary training and evaluation scripts for the broader PyTorch community. This report details a successful PyTorch reproduction by researchers at Valeo.ai, filling this gap and providing the community with a high-performance, open-source framework.

Methodology

The Two-Stage Pipeline

The MaskGIT architecture operates in two distinct stages:

  1. VQGAN (Vector Quantized GAN): The model first compresses an input image into a grid of discrete visual tokens. For instance, a $512 \times 512$ image is reduced to a $32 \times 32$ grid of tokens.
  2. Bidirectional Transformer: Unlike unidirectional models, this transformer observes the entire grid simultaneously. During training, it learns to predict randomly masked tokens. At inference, it begins with a fully masked grid and iteratively "unmasks" tokens based on confidence scores over 8 to 32 steps.

Code Archaeology and the "Gumbel Trick"

A significant portion of this reproduction involved "code archaeology" of the original JAX implementation. The authors discovered that simply selecting the most confident tokens during inference led to low-diversity, repetitive results (yielding a poor FID of 66.7). By implementing Gumbel noise injection into the confidence scores—a detail present in the JAX code but omitted from the original paper—the FID score improved dramatically to 7.7.

Additionally, the choice of scheduler for token unmasking proved critical. The "arccos" scheduler was found to be superior to linear or square alternatives, maintaining high image quality across various step counts.

Results and Performance

The PyTorch reproduction successfully matches and, in some cases, exceeds the benchmarks set by the original paper. On ImageNet at $512 \times 512$ resolution, the model achieved an FID of 7.26, surpassing the original reported score of 7.32.

A grid of diverse images generated by the MaskGIT PyTorch reproduction, including animals, objects, and landscapes.

Efficiency and Speed

One of the primary benefits of this architecture is its inference speed. On an Nvidia A100 GPU, generating a $512 \times 512$ image takes only 0.44 seconds using 15 steps. The model remains relatively lightweight with approximately 248 million parameters.

Resolution FID (Original) FID (Reproduction)
$256 \times 256$ 6.18 6.80
$512 \times 512$ 7.32 7.26

Conclusion and Future Work

This reproduction demonstrates that Masked Generative Models are a robust and efficient alternative for high-resolution image synthesis. While the current implementation focuses on class-conditional generation on ImageNet, the open-sourcing of this code provides a foundation for future extensions into text-to-image (e.g., MUSE) and video generation (e.g., MAGVIT). Future work may also explore advanced sampling techniques like Token Critic to further enhance generation diversity and quality.

The complete codebase and pre-trained weights are available for the research community at: https://github.com/valeoai/MaskGIT-pytorch/

Summary updated

BibTeX
@misc{besnier2023pytorch,
  author = {Victor Besnier and Mickaël Chen},
  title = {{A Pytorch Reproduction of Masked Generative Image Transformer}},
  year = {2023},
  eprint = {2310.14400},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2310.14400},
}
Figure