Code and models for the paper "One Transformer Fits All Distributions in Multi-Modal Diffusion"
Code and models for the paper "One Transformer Fits All Distributions in Multi-Modal Diffusion"
Code and models for the paper "One Transformer Fits All Distributions in Multi-Modal Diffusion" UniDiffuser is a unified diffusion framework to fit all distributions relevant to a set of multi-modal data in one model. Its key insight is -- learning diffusion models for marginal, conditional, and joint distributions can be unified as predicting the noise in the perturbed data, where the perturbation levels (i.e. timesteps) can be different for different modalities. Inspired by the unified view, UniDiffuser learns all distributions simultaneously with a minimal modification to the original diffusion model -- perturbs data in all modalities instead of a single modality, inputs individual timesteps in different modalities, and predicts the noise of all modalities instead of a single modality. UniDiffuser is parameterized by a transformer for diffusion models to handle input types of different modalities. Implemented on large-scale paired image-text data, UniDiffuser is able to perform image, text, text-to-image, image-to-text, and image-text pair generation by setting proper timesteps without additional overhead. In particular, UniDiffuser is able to produce perceptually realistic samples in all tasks and its quantitative results (e.g., the FID and CLIP score) are not only superior to existing general-purpose models but also comparable to the bespoken models (e.g., Stable Diffusion and DALL-E 2) in representative tasks (e.g., text-to-image generation).
conda create -n unidiffuser python=3.9
conda activate unidiffuser
pip install torch torchvision --extra-index-url https://download.pytorch.org/whl/cu116 # install torch-1.13.1
pip install accelerate==0.12.0 absl-py ml_collections einops ftfy==6.1.1 transformers==4.23.1
pip install -e git+https://github.com/openai/CLIP.git@main#egg=clip
# xformers is optional, but it would greatly speed up the attention computation.
pip install -U xformers
pip install -U --pre triton
UniDiffuser employs a variation of transformer, called U-ViT, which parameterizes the joint noise prediction network. Other components perform as encoders and decoders of different modalities, including a pretrained image autoencoder from Stable Diffusion, a pretrained image ViT-B/32 CLIP encoder, a pretrained text ViT-L CLIP encoder, and a GPT-2 text decoder finetuned by ourselves.
We provide two versions of UniDiffuser, which contain U-ViT of 1B parameters and can run on a GPU with at least 10 GB memory. They can be downloaded from Hugging Face:
Both links contain three files:
autoencoder_kl.pth is the weight of the image autoencoder converted from Stable Diffusion.caption_decoder.pth is the weight of the finetuned GPT-2 text decoder.uvit_v0.pth or uvit_v1.pth is the weight of U-ViT for UniDiffuser-v0 or UniDiffuser-v1.Note that UniDiffuser-v0 and UniDiffuser-v1 share the same autoencoder_kl.pth and caption_decoder.pth. You only need to download them once.
As for other components, they will be automatically downloaded.
After downloading, create a new folder named models and put all pretrained models into this folder as follows:
├── models
│ └── autoencoder_kl.pth
│ └── caption_decoder.pth
│ └── uvit_v0.pth or uvit_v1.pth
We suggest to use UniDiffuser-v1 for a better performance. Results are put into out directory by default.
python sample_multi_v1.py --mode=t2i --prompt="an elephant under the sea"
python sample_multi_v1.py --mode=i2t --img=assets/space.jpg
python sample_multi_v1.py --mode=joint
python sample_multi_v1.py --mode=i
python sample_multi_v1.py --mode=t
python sample_multi_v1.py --mode=i2t2i --img=assets/space.jpg
python sample_multi_v1.py --mode=t2i2t --prompt="an elephant under the sea"
We provide all supported arguments below
…
The inference command of UniDiffuser-v0 is basically the same as UniDiffuser-v1, only need to change
sample_multi_v1.py to sample_multi_v0.py. For example:
python sample_multi_v0.py --mode=t2i --prompt="an elephant under the sea"
UniDiffuser is also available in diffusers. It is available in six different modes.
Here is how one can use the UniDiffuserPipeline to generate images from text:
import torch
from diffusers import UniDiffuserPipeline
device = "cuda"
model_id_or_path = "thu-ml/unidiffuser-v1"
pipe = UniDiffuserPipeline.from_pretrained(model_id_or_path, torch_dtype=torch.float16)
pipe.to(device)
# Text-to-image generation
prompt = "an elephant under the sea"
sample = pipe(prompt=prompt, num_inference_steps=20, guidance_scale=8.0)
t2i_image = sample.images[0]
t2i_image.save("unidiffuser_text2img_sample_image.png")
To learn more details, check out the official UniDiffuser documentation.
If you find the code useful for your research, please consider citing
@article{bao2022one,
title={One Transformer Fits All Distributions in Multi-Modal Diffusion at Scale},
author={Bao, Fan and Nie, Shen and Xue, Kaiwen and Li, Chongxuan and Pu, Shi and Wang, Yaole and Yue, Gang and Cao, Yue and Su, Hang and Zhu, Jun},
year={2023}
}
@inproceedings{bao2022all,
title={All are Worth Words: A ViT Backbone for Diffusion Models},
author={Bao, Fan and Nie, Shen and Xue, Kaiwen and Cao, Yue and Li, Chongxuan and Su, Hang and Zhu, Jun},
booktitle = {CVPR},
year={2023}
}
This implementation is heavily based on the U-ViT code.
About training codes
Does unidiffuser support using the peft library for lora fine-tuning?
Training uvit
can' t load tokenizer for 'gpt2', why?
May I ask what validation loss you used during training?
Unidiffuser can not be loaded in the latest version of diffusers
expected str, bytes or os.PathLike object, not NoneType
question about training
How to reproduce CLIP score
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))