The official implementation of Autoregressive Image Generation using Residual Quantization (CVPR '22)
Jupyter Notebook
1,029
5 commits
updated Jan 3, 2024
The official implementation of "Autoregressive Image Generation using Residual Quantization"
Doyup Lee*, Chiheon Kim*, Saehoon Kim, Minsu Cho, Wook-Shin Han (* Equal contribution)
CVPR 2022
The examples of generated images by RQ-Transformer using class conditions and text conditions.
Note that the text conditions of the examples are not used in training time.
TL;DR For autoregressive (AR) modeling of high-resolution images, we propose the two-stage framework, which consists of RQ-VAE and RQ-Transformer. Our framework can precisely approximate a feature map of an image and represent an image as a stack of discrete codes to effectively generate high-quality images.
We have tested our codes on the environment below
Python 3.7.10 / Pytorch 1.9.0 / torchvision 0.10.0 / CUDA 11.1 / Ubuntu 18.04 .Please run the following command to install the necessary dependencies
pip install -r requirements.txt
We provide pretrained checkpoints of RQ-VAEs and RQ-Transformers to reproduce the results in the paper. Please use the links below to download tar.gz files and unzip the pretrained checkpoints. Each link contains pretrained checkpoints of RQ-VAE and RQ-Transformer and their model configurations.
| Dataset | RQ-VAE & RQ-Transformer | # params of RQ-Transformer | FID |
|---|---|---|---|
| FFHQ | link | 355M | 10.38 |
| LSUN-Church | link | 370M | 7.45 |
| LSUN-Cat | link | 612M | 8.64 |
| LSUN-Bedroom | link | 612M | 3.04 |
| ImageNet (cIN) | link | 480M | 15.72 |
| ImageNet (cIN) | link | 821M | 13.11 |
| ImageNet (cIN) | link | 1.4B | 11.56 (4.45) |
| ImageNet (cIN) | link | 1.4B | 8.71 (3.89) |
| ImageNet (cIN) | link | 3.8B | 7.55 (3.80) |
| CC-3M | link | 654M | 12.33 |
FID scores above are measured based on original samples and generated images, and the scores in brackets are measured using 5% rejection sampling via pretrained ResNet-101. We do not provide the pipeline of rejection sampling in this repository.
We also provide the pretrained checkpoint of large-scale RQ-Transformer for text-to-image (T2I) generation. Our paper does not include the results of this large-scale RQ-Transformer for T2I generation, since we trained RQ-Transformer with 3.9B parameters on about 30 millions of text-to-image pairs from CC-3M, CC-12M, and YFCC-subset after the paper submission. Please use the link below to download the checkpoints of large-scale T2I model. We emphasize that any commercial use of our checkpoints is strictly prohibited.
| Dataset. | RQ-VAE & RQ-Transformer | # params |
|---|---|---|
| CC-3M + CC-12M + YFCC-subset | link | 3.9B |
In this repository, we evaluate the pretrained RQ-Transformer with 3.9B parameters on MS-COCO. According to the evaluation protocol of DALL-Eval, we randomly select 30K text captions in val2014 split of MS-COCO and generate 256x256 images using the selected captions. We use (1024, 0.95) for top-(k, p) sampling, and FID scores of other models are from Table 2 in DALL-Eval paper.
| Model | # params | # data | Image / Grid Size | FID on 2014val |
|---|---|---|---|---|
| X-LXMERT | 228M | 180K | 256x256 / 8x8 | 37.4 |
| DALL-E small | 120M | 15M | 256x256 / 16x16 | 45.8 |
| ruDALL-E-XL | 1.3B | 120M | 256x256 / 32x32 | 18.6 |
| minDALL-E | 1.3B | 15M | 256x256 / 16x16 | 24.6 |
| RQ-Transformer (ours) | 3.9B | 30M | 256x256 / 8x8x4 | 16.9 |
Note that some text captions in MS-COCO are also included in the YFCC-subset, but the FIDs are not much different whether the duplicated captions are removed in the evaluation or not. See this paper for more details.
We provide a jupyter notebook for you to easily enjoy text-to-image (T2I) generation of pretrained RQ-Transformers and the results ! After you download the pretrained checkpoints for T2I generation, open notebooks/T2I_sampling.ipynb and follows the instructions in the notebook file. We recommend to use a GPU such as NVIDIA V100 or A100, which has enough memory size over 32GB, considering the model size.
We attach some examples of T2I generation from the provided Jupyter notebook.
Examples of Generated Images from Text Conditions
Our implementation uses DistributedDataParallel in Pytorch for efficient training with multi-node and multi-GPU environments. Four NVIDIA A100 GPUs are used to train all RQ-VAEs in our paper. You can also adjust -nr, -np, and -nr according to your GPU setting.
Training 8x8x4 RQ-VAE on ImageNet 256x256 with a single node having four GPUs
python -m torch.distributed.launch \
--master_addr=$MASTER_ADDR \
--master_port=$PORT \
--nnodes=1 --nproc_per_node=4 --node_rank=0 \
main_stage1.py \
-m=configs/imagenet256/stage1/in256-rqvae-8x8x4.yaml -r=$SAVE_DIR
If you want to train 8x8x4 RQ-VAE on ImageNet using four nodes, where each node has one GPU, run the following scripts at each node with $RANK being the node rank (0, 1, 2, 3). Here, we assume that the master node corresponds to the node with rank 0.
python -m torch.distributed.launch \
--master_addr=$MASTER_ADDR \
--master_port=$PORT \
--nnodes=4 --nproc_per_node=1 --node_rank=$RANK \
main_stage1.py \
-m=configs/imagenet256/stage1/in256-rqvae-8x8x4.yaml -r=$SAVE_DIR
-l=$RQVAE_CKPT argument.python -m torch.distributed.launch \
--master_addr=$MASTER_ADDR \
--master_port=$PORT \
--nnodes=1 --nproc_per_node=4 --node_rank=0 \
main_stage1.py \
-m=configs/lsun-church/stage1/church256-rqvae-8x8x4.yaml -r=$SAVE_DIR -l=$RQVAE_CKPT
Run compute_rfid.py to evaluate the reconstruction FID (rFID) of learned RQ-VAEs.
python compute_rfid.py --split=val --vqvae=$RQVAE_CKPT
compute_rfid.py evaluates rFID of RQ-VAE on the dataset in the configuration file.--batch-size as the memory size of your GPU environment.In this repository, the quantitative results in the paper can be reproduced by the codes for the evaluation of RQ-Transformer. Before the evaluation of RQ-Transformer on a dataset, the dataset has to be prepared for computing the feature vectors of its samples. To reproduce the results in the paper, we provide the statistics of feature vectors of each dataset, since extracting feature vectors accompanies computational costs and a long time. You can also prepare the datasets, which are used in our paper, as you follow the instructions of data/READMD.md.
cd assets
wget https://twg.kakaocdn.net/brainrepo/etc/RQVAE/8b325b628f49bf60a3094fcf9419398c/fid_stats.tar.gz
tar -zxvf fid_stats.tar.gz
--save-dir to specify directory where the generated images are saved. If --save-dir is not given, the generated images are saved at the directory of the checkpoint.python -m torch.distributed.launch \
--master_addr=$MASTER_ADDR \
--master_port=$PORT \
--nnodes=1 --nproc_per_node=4 --node_rank=0 \
main_sampling_fid.py \
-v=$RQVAE_CKPT -a=$RQTRANSFORMER_CKPT --save-dir=$SAVE_IMG_DIR
data/READMD.md and prepare the dataset first.python -m torch.distributed.launch \
--master_addr=$MASTER_ADDR \
--master_port=$PORT \
--nnodes=1 --nproc_per_node=4 --node_rank=0 \
main_sampling_txt2img.py \
-v=$RQVAE_CKPT -a=$RQTRANSFORMER_CKPT --dataset="cc3m" --save-dir=$SAVE_IMG_DIR
2014val split, and provide the sampled samples as json file.data/READMD.md and prepare the dataset first.python -m torch.distributed.launch \
--master_addr=$MASTER_ADDR \
--master_port=$PORT \
--nnodes=1 --nproc_per_node=4 --node_rank=0 \
main_sampling_txt2img.py \
-v=$RQVAE_CKPT -a=$RQTRANSFORMER_CKPT --dataset="coco_2014val" --save-dir=$SAVE_IMG_DIR
NOTE
.pkl files in the directory $DIR_SAVED_IMG.--top-k and --top-p in running the sampling scripts.compute_metrics.py can be used to evaluate the images again as follows:python compute_metrics.py fake_path=$DIR_SAVED_IMG ref_dataset=$DATASET_NAME
We provide the codes to measure the sampling speed of RQ-Transformer according to the code shape of RQ-VAEs, such as 8x8x4 or 16x16x1, as shown in Figure 4 in the paper. To reproduce the figure, run the following commands on NVIDIA A100 GPU:
# RQ-Transformer (1.4B) on 16x16x1 RQ-VAE (corresponds to VQ-GAN 1.4B model)
python -m measure_throughput f=16 d=1 c=16384 model=huge batch_size=100
python -m measure_throughput f=16 d=1 c=16384 model=huge batch_size=200
python -m measure_throughput f=16 d=1 c=16384 model=huge batch_size=500 # this will result in OOM.
# RQ-Transformer (1.4B) on 8x8x4 RQ-VAE
python -m measure_throughput f=32 d=4 c=16384 model=huge batch_size=100
python -m measure_throughput f=32 d=4 c=16384 model=huge batch_size=200
python -m measure_throughput f=32 d=4 c=16384 model=huge batch_size=500
@inproceedings{lee2022autoregressive,
title={Autoregressive Image Generation using Residual Quantization},
author={Lee, Doyup and Kim, Chiheon and Kim, Saehoon and Cho, Minsu and Han, Wook-Shin},
booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
pages={11523--11532},
year={2022}
}
If you would like to collaborate with us or provide us a feedback, please contaus us,contact@kakaobrain.com
Our transformer-related implementation is inspired by minGPT and minDALL-E. We appreciate the authors of VQGAN for making their codes available to public.
Since RQ-Transformer is trained on publicly available datasets, some generated images can include socially unacceptable contents according to the text conditions. When the problem occurs, please let us know the pair of "text condition" and "generated images".
549 followers · starred May 2024
141 followers · starred Apr 2024
123 followers · starred Jul 2023
13 followers · starred Nov 2025
Jupyter Notebook
89.8%
Python
10.2%
The official implementation of Autoregressive Image Generation using Residual Quantization (CVPR '22)
Jupyter Notebook
1,029
5 commits
updated Jan 3, 2024
The official implementation of "Autoregressive Image Generation using Residual Quantization"
Doyup Lee*, Chiheon Kim*, Saehoon Kim, Minsu Cho, Wook-Shin Han (* Equal contribution)
CVPR 2022
The examples of generated images by RQ-Transformer using class conditions and text conditions.
Note that the text conditions of the examples are not used in training time.
TL;DR For autoregressive (AR) modeling of high-resolution images, we propose the two-stage framework, which consists of RQ-VAE and RQ-Transformer. Our framework can precisely approximate a feature map of an image and represent an image as a stack of discrete codes to effectively generate high-quality images.
We have tested our codes on the environment below
Python 3.7.10 / Pytorch 1.9.0 / torchvision 0.10.0 / CUDA 11.1 / Ubuntu 18.04 .Please run the following command to install the necessary dependencies
pip install -r requirements.txt
We provide pretrained checkpoints of RQ-VAEs and RQ-Transformers to reproduce the results in the paper. Please use the links below to download tar.gz files and unzip the pretrained checkpoints. Each link contains pretrained checkpoints of RQ-VAE and RQ-Transformer and their model configurations.
| Dataset | RQ-VAE & RQ-Transformer | # params of RQ-Transformer | FID |
|---|---|---|---|
| FFHQ | link | 355M | 10.38 |
| LSUN-Church | link | 370M | 7.45 |
| LSUN-Cat | link | 612M | 8.64 |
| LSUN-Bedroom | link | 612M | 3.04 |
| ImageNet (cIN) | link | 480M | 15.72 |
| ImageNet (cIN) | link | 821M | 13.11 |
| ImageNet (cIN) | link | 1.4B | 11.56 (4.45) |
| ImageNet (cIN) | link | 1.4B | 8.71 (3.89) |
| ImageNet (cIN) | link | 3.8B | 7.55 (3.80) |
| CC-3M | link | 654M | 12.33 |
FID scores above are measured based on original samples and generated images, and the scores in brackets are measured using 5% rejection sampling via pretrained ResNet-101. We do not provide the pipeline of rejection sampling in this repository.
We also provide the pretrained checkpoint of large-scale RQ-Transformer for text-to-image (T2I) generation. Our paper does not include the results of this large-scale RQ-Transformer for T2I generation, since we trained RQ-Transformer with 3.9B parameters on about 30 millions of text-to-image pairs from CC-3M, CC-12M, and YFCC-subset after the paper submission. Please use the link below to download the checkpoints of large-scale T2I model. We emphasize that any commercial use of our checkpoints is strictly prohibited.
| Dataset. | RQ-VAE & RQ-Transformer | # params |
|---|---|---|
| CC-3M + CC-12M + YFCC-subset | link | 3.9B |
In this repository, we evaluate the pretrained RQ-Transformer with 3.9B parameters on MS-COCO. According to the evaluation protocol of DALL-Eval, we randomly select 30K text captions in val2014 split of MS-COCO and generate 256x256 images using the selected captions. We use (1024, 0.95) for top-(k, p) sampling, and FID scores of other models are from Table 2 in DALL-Eval paper.
| Model | # params | # data | Image / Grid Size | FID on 2014val |
|---|---|---|---|---|
| X-LXMERT | 228M | 180K | 256x256 / 8x8 | 37.4 |
| DALL-E small | 120M | 15M | 256x256 / 16x16 | 45.8 |
| ruDALL-E-XL | 1.3B | 120M | 256x256 / 32x32 | 18.6 |
| minDALL-E | 1.3B | 15M | 256x256 / 16x16 | 24.6 |
| RQ-Transformer (ours) | 3.9B | 30M | 256x256 / 8x8x4 | 16.9 |
Note that some text captions in MS-COCO are also included in the YFCC-subset, but the FIDs are not much different whether the duplicated captions are removed in the evaluation or not. See this paper for more details.
We provide a jupyter notebook for you to easily enjoy text-to-image (T2I) generation of pretrained RQ-Transformers and the results ! After you download the pretrained checkpoints for T2I generation, open notebooks/T2I_sampling.ipynb and follows the instructions in the notebook file. We recommend to use a GPU such as NVIDIA V100 or A100, which has enough memory size over 32GB, considering the model size.
We attach some examples of T2I generation from the provided Jupyter notebook.
Examples of Generated Images from Text Conditions
Our implementation uses DistributedDataParallel in Pytorch for efficient training with multi-node and multi-GPU environments. Four NVIDIA A100 GPUs are used to train all RQ-VAEs in our paper. You can also adjust -nr, -np, and -nr according to your GPU setting.
Training 8x8x4 RQ-VAE on ImageNet 256x256 with a single node having four GPUs
python -m torch.distributed.launch \
--master_addr=$MASTER_ADDR \
--master_port=$PORT \
--nnodes=1 --nproc_per_node=4 --node_rank=0 \
main_stage1.py \
-m=configs/imagenet256/stage1/in256-rqvae-8x8x4.yaml -r=$SAVE_DIR
If you want to train 8x8x4 RQ-VAE on ImageNet using four nodes, where each node has one GPU, run the following scripts at each node with $RANK being the node rank (0, 1, 2, 3). Here, we assume that the master node corresponds to the node with rank 0.
python -m torch.distributed.launch \
--master_addr=$MASTER_ADDR \
--master_port=$PORT \
--nnodes=4 --nproc_per_node=1 --node_rank=$RANK \
main_stage1.py \
-m=configs/imagenet256/stage1/in256-rqvae-8x8x4.yaml -r=$SAVE_DIR
-l=$RQVAE_CKPT argument.python -m torch.distributed.launch \
--master_addr=$MASTER_ADDR \
--master_port=$PORT \
--nnodes=1 --nproc_per_node=4 --node_rank=0 \
main_stage1.py \
-m=configs/lsun-church/stage1/church256-rqvae-8x8x4.yaml -r=$SAVE_DIR -l=$RQVAE_CKPT
Run compute_rfid.py to evaluate the reconstruction FID (rFID) of learned RQ-VAEs.
python compute_rfid.py --split=val --vqvae=$RQVAE_CKPT
compute_rfid.py evaluates rFID of RQ-VAE on the dataset in the configuration file.--batch-size as the memory size of your GPU environment.In this repository, the quantitative results in the paper can be reproduced by the codes for the evaluation of RQ-Transformer. Before the evaluation of RQ-Transformer on a dataset, the dataset has to be prepared for computing the feature vectors of its samples. To reproduce the results in the paper, we provide the statistics of feature vectors of each dataset, since extracting feature vectors accompanies computational costs and a long time. You can also prepare the datasets, which are used in our paper, as you follow the instructions of data/READMD.md.
cd assets
wget https://twg.kakaocdn.net/brainrepo/etc/RQVAE/8b325b628f49bf60a3094fcf9419398c/fid_stats.tar.gz
tar -zxvf fid_stats.tar.gz
--save-dir to specify directory where the generated images are saved. If --save-dir is not given, the generated images are saved at the directory of the checkpoint.python -m torch.distributed.launch \
--master_addr=$MASTER_ADDR \
--master_port=$PORT \
--nnodes=1 --nproc_per_node=4 --node_rank=0 \
main_sampling_fid.py \
-v=$RQVAE_CKPT -a=$RQTRANSFORMER_CKPT --save-dir=$SAVE_IMG_DIR
data/READMD.md and prepare the dataset first.python -m torch.distributed.launch \
--master_addr=$MASTER_ADDR \
--master_port=$PORT \
--nnodes=1 --nproc_per_node=4 --node_rank=0 \
main_sampling_txt2img.py \
-v=$RQVAE_CKPT -a=$RQTRANSFORMER_CKPT --dataset="cc3m" --save-dir=$SAVE_IMG_DIR
2014val split, and provide the sampled samples as json file.data/READMD.md and prepare the dataset first.python -m torch.distributed.launch \
--master_addr=$MASTER_ADDR \
--master_port=$PORT \
--nnodes=1 --nproc_per_node=4 --node_rank=0 \
main_sampling_txt2img.py \
-v=$RQVAE_CKPT -a=$RQTRANSFORMER_CKPT --dataset="coco_2014val" --save-dir=$SAVE_IMG_DIR
NOTE
.pkl files in the directory $DIR_SAVED_IMG.--top-k and --top-p in running the sampling scripts.compute_metrics.py can be used to evaluate the images again as follows:python compute_metrics.py fake_path=$DIR_SAVED_IMG ref_dataset=$DATASET_NAME
We provide the codes to measure the sampling speed of RQ-Transformer according to the code shape of RQ-VAEs, such as 8x8x4 or 16x16x1, as shown in Figure 4 in the paper. To reproduce the figure, run the following commands on NVIDIA A100 GPU:
# RQ-Transformer (1.4B) on 16x16x1 RQ-VAE (corresponds to VQ-GAN 1.4B model)
python -m measure_throughput f=16 d=1 c=16384 model=huge batch_size=100
python -m measure_throughput f=16 d=1 c=16384 model=huge batch_size=200
python -m measure_throughput f=16 d=1 c=16384 model=huge batch_size=500 # this will result in OOM.
# RQ-Transformer (1.4B) on 8x8x4 RQ-VAE
python -m measure_throughput f=32 d=4 c=16384 model=huge batch_size=100
python -m measure_throughput f=32 d=4 c=16384 model=huge batch_size=200
python -m measure_throughput f=32 d=4 c=16384 model=huge batch_size=500
@inproceedings{lee2022autoregressive,
title={Autoregressive Image Generation using Residual Quantization},
author={Lee, Doyup and Kim, Chiheon and Kim, Saehoon and Cho, Minsu and Han, Wook-Shin},
booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
pages={11523--11532},
year={2022}
}
If you would like to collaborate with us or provide us a feedback, please contaus us,contact@kakaobrain.com
Our transformer-related implementation is inspired by minGPT and minDALL-E. We appreciate the authors of VQGAN for making their codes available to public.
Since RQ-Transformer is trained on publicly available datasets, some generated images can include socially unacceptable contents according to the text conditions. When the problem occurs, please let us know the pair of "text condition" and "generated images".
549 followers · starred May 2024
141 followers · starred Apr 2024
123 followers · starred Jul 2023
13 followers · starred Nov 2025
Jupyter Notebook
89.8%
Python
10.2%