[init]
Browse files
README.md
CHANGED
@@ -1,3 +1,179 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Marrying Autoregressive Transformer and Diffusion with Multi-Reference Autoregression <br><sub>Official PyTorch Implementation</sub>
|
2 |
+
|
3 |
+
[](https://arxiv.org/pdf/2506.09482)
|
4 |
+
[](https://huggingface.co/zhendch/Transdiff)
|
5 |
+
|
6 |
+
|
7 |
+
<p align="center">
|
8 |
+
<img src="figs/visual.png" width="720">
|
9 |
+
</p>
|
10 |
+
|
11 |
+
This is a PyTorch/GPU implementation of the paper [Marrying Autoregressive Transformer and Diffusion with Multi-Reference Autoregression](https://arxiv.org/pdf/2506.09482):
|
12 |
+
|
13 |
+
```
|
14 |
+
@article{zhen2025marrying,
|
15 |
+
title={Marrying Autoregressive Transformer and Diffusion with Multi-Reference Autoregression},
|
16 |
+
author={Zhen, Dingcheng and Qiao, Qian and Yu, Tan and Wu, Kangxi and Zhang, Ziwei and Liu, Siyuan and Yin, Shunshun and Tao, Ming},
|
17 |
+
journal={arXiv preprint arXiv:2506.09482},
|
18 |
+
year={2025}
|
19 |
+
}
|
20 |
+
```
|
21 |
+
|
22 |
+
This repo contains:
|
23 |
+
|
24 |
+
* 🪐 A simple PyTorch implementation of [TransDiff Model](models/transdiff.py) and [TransDiff Model with MRAR](models/transdiff_mrar.py)
|
25 |
+
* ⚡️ Pre-trained class-conditional TransDiff models trained on ImageNet 256x256 and 512x512
|
26 |
+
* 💥 A self-contained [notebook](demo.ipynb) for running various pre-trained TransDiff models
|
27 |
+
* 🛸 An TransDiff [training and evaluation script](main.py) using PyTorch DDP
|
28 |
+
|
29 |
+
## Preparation
|
30 |
+
|
31 |
+
### Dataset
|
32 |
+
Download [ImageNet](http://image-net.org/download) dataset, and place it in your `IMAGENET_PATH`.
|
33 |
+
|
34 |
+
### VAE Model
|
35 |
+
We adopt the VAE model from [MAR](https://github.com/LTH14/mar) , you can also get it [here](https://huggingface.co/zhendch/Transdiff/resolve/main/vae/checkpoint-last.pth?download=true).
|
36 |
+
### Installation
|
37 |
+
|
38 |
+
Download the code:
|
39 |
+
```
|
40 |
+
git clone https://github.com/TransDiff/TransDiff
|
41 |
+
cd TransDiff
|
42 |
+
```
|
43 |
+
|
44 |
+
A suitable [conda](https://conda.io/) environment named `transdiff` can be created and activated with:
|
45 |
+
|
46 |
+
```
|
47 |
+
conda env create -f environment.yaml
|
48 |
+
conda activate transdiff
|
49 |
+
```
|
50 |
+
|
51 |
+
For convenience, our pre-trained TransDiff models can be downloaded directly here as well:
|
52 |
+
|
53 |
+
| TransDiff Model | FID-50K | Inception Score | #params |
|
54 |
+
|--------------------------------------------------------------------------------------------------------------------------------|---------|-----------------|---------|
|
55 |
+
| [TransDiff-B](https://huggingface.co/zhendch/Transdiff/resolve/main/transdiff_b/checkpoint-last.pth?download=true) | 2.47 | 244.2 | 290M |
|
56 |
+
| [TransDiff-L](https://huggingface.co/zhendch/Transdiff/resolve/main/transdiff_l/checkpoint-last.pth?download=true) | 2.25 | 244.3 | 683M |
|
57 |
+
| [TransDiff-H](https://huggingface.co/zhendch/Transdiff/resolve/main/transdiff_h/checkpoint-last.pth?download=true) | 1.69 | 282.0 | 1.3B |
|
58 |
+
| [TransDiff-B MRAR](https://huggingface.co/zhendch/Transdiff/resolve/main/transdiff_b_mrar/checkpoint-last.pth?download=true) | 1.49 | 282.2 | 290M |
|
59 |
+
| [TransDiff-L MRAR](https://huggingface.co/zhendch/Transdiff/resolve/main/transdiff_l_mrar/checkpoint-last.pth?download=true) | 1.61 | 293.4 | 683M |
|
60 |
+
| [TransDiff-H MRAR](https://huggingface.co/zhendch/Transdiff/resolve/main/transdiff_h_mrar/checkpoint-last.pth?download=true) | 1.42 | 301.2 | 1.3B |
|
61 |
+
| [TransDiff-L 512x512](https://huggingface.co/zhendch/Transdiff/resolve/main/transdiff_l_512/checkpoint-last.pth?download=true) | 2.51 | 286.6 | 683M |
|
62 |
+
|
63 |
+
### (Optional) Download Other Files
|
64 |
+
Download necessary [file](https://huggingface.co/zhendch/Transdiff/resolve/main/VIRTUAL_imagenet512.npz?download=true) and put it into folder `fid_stats/`, if you want to run evaluation on ImageNet 512x512.
|
65 |
+
Download [MRAR index file](https://huggingface.co/zhendch/Transdiff/resolve/main/Imagenet2012_mrar_files.txt?download=true) and put it into root of project, if you want to train TransDiff with MRAR.
|
66 |
+
|
67 |
+
### (Optional) Caching VAE Latents
|
68 |
+
|
69 |
+
Given that our data augmentation consists of simple center cropping and random flipping,
|
70 |
+
the VAE latents can be pre-computed and saved to `CACHED_PATH` to save computations during TransDiff training:
|
71 |
+
|
72 |
+
```
|
73 |
+
torchrun --nproc_per_node=8 --nnodes=1 --node_rank=0 \
|
74 |
+
main_cache.py \
|
75 |
+
--img_size 256 --vae_path ckpt/vae/kl16.ckpt --vae_embed_dim 16 \
|
76 |
+
--batch_size 128 \
|
77 |
+
--data_path ${IMAGENET_PATH} --cached_path ${CACHED_PATH}
|
78 |
+
```
|
79 |
+
|
80 |
+
## Usage
|
81 |
+
|
82 |
+
### Demo
|
83 |
+
Run our interactive visualization [demo](demo.ipynb).
|
84 |
+
|
85 |
+
### Training
|
86 |
+
Script for the TransDiff-L 1StepAR setting (Pretrain TransDiff-L with a width of 1024 channels, 800 epochs):
|
87 |
+
```
|
88 |
+
torchrun --nproc_per_node=8 --nnodes=8 --node_rank=${NODE_RANK} --master_addr=${MASTER_ADDR} --master_port=${MASTER_PORT} \
|
89 |
+
main.py \
|
90 |
+
--img_size 256 --vae_path ckpt/vae/kl16.ckpt --vae_embed_dim 16 --patch_size 1 \
|
91 |
+
--model transdiff_large --diffloss_w 1024 \
|
92 |
+
--diffusion_batch_mul 4 \
|
93 |
+
--epochs 800 --warmup_epochs 100 --blr 1.0e-4 --batch_size 32 \
|
94 |
+
--output_dir ${OUTPUT_DIR} --resume ${OUTPUT_DIR} \
|
95 |
+
--data_path ${IMAGENET_PATH}
|
96 |
+
```
|
97 |
+
- Training time is ~115h on 64 A100 GPUs with `--batch_size 32`.
|
98 |
+
- Add `--online_eval` to evaluate FID during training (every 50 epochs).
|
99 |
+
- (Optional) To train with cached VAE latents, add `--use_cached --cached_path ${CACHED_PATH}` to the arguments.
|
100 |
+
- (Optional) If the error 'Loss is nan, stopping training' frequently occurs during training when using mixed precision training with 'torch.cuda.amp.autocast()', you can add `--bf16` to the arguments.
|
101 |
+
- (Optional) If necessary, you can use gradient accumulation by setting `--gradient_accumulation_steps n`.
|
102 |
+
|
103 |
+
Script for the TransDiff-L MRAR setting (Finetune TransDiff-L MRAR with a width of 1024 channels, 40 epochs):
|
104 |
+
```
|
105 |
+
torchrun --nproc_per_node=8 --nnodes=8 --node_rank=${NODE_RANK} --master_addr=${MASTER_ADDR} --master_port=${MASTER_PORT} \
|
106 |
+
main.py \
|
107 |
+
--img_size 256 --vae_path ckpt/vae/kl16.ckpt --vae_embed_dim 16 --patch_size 1 \
|
108 |
+
--model transdiff_large --diffloss_w 1024 --mrar --bf16 \
|
109 |
+
--diffusion_batch_mul 2 \
|
110 |
+
--epochs 40 --warmup_epochs 10 --lr 5.0e-5 --batch_size 16 --gradient_accumulation_steps 2 \
|
111 |
+
--output_dir ${OUTPUT_DIR} --resume ${Transdiff-L_1StepAR_DIR} \
|
112 |
+
--data_path ${IMAGENET_PATH}
|
113 |
+
```
|
114 |
+
Script for the TransDiff-L 512x512 setting (Finetune TransDiff-L 512x512 with a width of 1024 channels, 150 epochs):
|
115 |
+
```
|
116 |
+
torchrun --nproc_per_node=8 --nnodes=8 --node_rank=${NODE_RANK} --master_addr=${MASTER_ADDR} --master_port=${MASTER_PORT} \
|
117 |
+
main.py \
|
118 |
+
--img_size 512 --vae_path ckpt/vae/kl16.ckpt --vae_embed_dim 16 --patch_size 1 \
|
119 |
+
--model transdiff_large --diffloss_w 1024 --ema_rate 0.999 --bf16 \
|
120 |
+
--diffusion_batch_mul 4 \
|
121 |
+
--epochs 150 --warmup_epochs 10 --lr 1.0e-4 --batch_size 16 --gradient_accumulation_steps 2 \
|
122 |
+
--only_train_diff \
|
123 |
+
--output_dir ${OUTPUT_DIR} --resume ${Transdiff-L_1StepAR_DIR} \
|
124 |
+
--data_path ${IMAGENET_PATH}
|
125 |
+
```
|
126 |
+
|
127 |
+
### Evaluation (ImageNet 256x256 and 512x512)
|
128 |
+
|
129 |
+
Evaluate TransDiff-L 1StepAR with classifier-free guidance:
|
130 |
+
```
|
131 |
+
torchrun --nproc_per_node=8 --nnodes=1 --node_rank=0 \
|
132 |
+
main.py \
|
133 |
+
--img_size 256 --vae_path ckpt/vae/kl16.ckpt --vae_embed_dim 16 --patch_size 1 \
|
134 |
+
--model transdiff_large --diffloss_w 1024 \
|
135 |
+
--output_dir ${OUTPUT_DIR} --resume ckpt/transdiff_l/ \
|
136 |
+
--evaluate --eval_bsz 256 --num_images 50000 \
|
137 |
+
--cfg 1.3 --scale_0 0.89 --scale_1 0.95
|
138 |
+
```
|
139 |
+
|
140 |
+
Evaluate TransDiff-L MRAR with classifier-free guidance:
|
141 |
+
```
|
142 |
+
torchrun --nproc_per_node=8 --nnodes=1 --node_rank=0 \
|
143 |
+
main.py \
|
144 |
+
--img_size 256 --vae_path ckpt/vae/kl16.ckpt --vae_embed_dim 16 --patch_size 1 \
|
145 |
+
--model transdiff_large --diffloss_w 1024 \
|
146 |
+
--output_dir ${OUTPUT_DIR} --resume ckpt/transdiff_l_mrar/ \
|
147 |
+
--evaluate --eval_bsz 256 --num_images 50000 \
|
148 |
+
--cfg 1.3 --scale_0 0.91 --scale_1 0.93
|
149 |
+
```
|
150 |
+
|
151 |
+
Evaluate TransDiff-L 512x512 with classifier-free guidance:
|
152 |
+
```
|
153 |
+
torchrun --nproc_per_node=8 --nnodes=1 --node_rank=0 \
|
154 |
+
main.py \
|
155 |
+
--img_size 512 --vae_path ckpt/vae/kl16.ckpt --vae_embed_dim 16 --patch_size 1 \
|
156 |
+
--model transdiff_large --diffloss_w 1024 \
|
157 |
+
--output_dir ${OUTPUT_DIR} --resume ckpt/transdiff_l_512/ \
|
158 |
+
--evaluate --eval_bsz 64 --num_images 50000 \
|
159 |
+
--cfg 1.3 --scale_0 0.87 --scale_1 0.87
|
160 |
+
```
|
161 |
+
|
162 |
+
More settings for Benchmark in paper:
|
163 |
+
|
164 |
+
| TransDiff Model | cfg | scale_0 | scale_1 |
|
165 |
+
|---------------------|------|---------|---------|
|
166 |
+
| TransDiff-B | 1.30 | 0.87 | 0.91 |
|
167 |
+
| TransDiff-L | 1.30 | 0.89 | 0.95 |
|
168 |
+
| TransDiff-H | 1.23 | 0.87 | 0.93 |
|
169 |
+
| TransDiff-B MRAR | 1.30 | 0.87 | 0.91 |
|
170 |
+
| TransDiff-L MRAR | 1.30 | 0.91 | 0.93 |
|
171 |
+
| TransDiff-H MRAR | 1.28 | 0.87 | 0.91 |
|
172 |
+
| TransDiff-L 512x512 | 1.30 | 0.87 | 0.87 |
|
173 |
+
|
174 |
+
## Acknowledgements
|
175 |
+
A large portion of codes in this repo is based on [MAR](https://github.com/LTH14/mar), [diffusers](https://github.com/huggingface/diffusers) and [timm](https://github.com/huggingface/pytorch-image-models).
|
176 |
+
|
177 |
+
## Contact
|
178 |
+
|
179 |
+
If you have any questions, feel free to contact me through email ([email protected]). Enjoy!
|