Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
D

DALLE2-pytorch

> DevOps
Open source

Implementation of DALL-E 2, OpenAI's updated text-to-image synthesis neural network, in Pytorch

11.3K stars0 likes0 views
WebsiteGitHub

About

Implementation of DALL-E 2, OpenAI's updated text-to-image synthesis neural network, in Pytorch

DALL-E 2 - Pytorch

Implementation of DALL-E 2, OpenAI's updated text-to-image synthesis neural network, in Pytorch.

Yannic Kilcher summary | AssemblyAI explainer

The main novelty seems to be an extra layer of indirection with the prior network (whether it is an autoregressive transformer or a diffusion network), which predicts an image embedding based on the text embedding from CLIP. Specifically, this repository will only build out the diffusion prior network, as it is the best performing variant (but which incidentally involves a causal transformer as the denoising network )

This model is SOTA for text-to-image for now.

Please join if you are interested in helping out with the replication with the LAION community | Yannic Interview

As of 5/23/22, it is no longer SOTA. SOTA will be here. Jax versions as well as text-to-video project will be shifted towards the Imagen architecture, as it is way simpler.

Status

  • A research group has used the code in this repository to train a functional diffusion prior for their CLIP generations. Will share their work once they release their preprint. This, and Katherine's own experiments, validate OpenAI's finding that the extra prior increases variety of generations.

  • Decoder is now verified working for unconditional generation on my experimental setup for Oxford flowers. 2 researchers have also confirmed Decoder is working for them.

ongoing at 21k steps

  • Justin Pinkney successfully trained the diffusion prior in the repository for his CLIP to Stylegan2 text-to-image application

  • Romain has scaled up training to 800 GPUs with the available scripts without any issues

Pre-Trained Models

  • LAION is training prior models. Checkpoints are available on huggingface and the training statistics are available on WANDB.
  • Decoder - In-progress test run
  • Decoder - Another test run with sparse attention
  • DALL-E 2 - DALL-E 2 Laion repository

Appreciation

This library would not have gotten to this working state without the help of

  • Zion for the distributed training code for the diffusion prior
  • Aidan for the distributed training code for the decoder as well as the dataloaders
  • Kumar for working on the initial diffusion training script
  • Romain for the pull request reviews and project management
  • He Cao and xiankgx for the Q&A and for identifying of critical bugs
  • Marunine for identifying issues with resizing of the low resolution conditioner, when training the upsampler, in addition to various other bug fixes
  • MalumaDev for proposing the use of pixel shuffle upsampler for fixing checkboard artifacts
  • Katherine for her advice
  • Stability AI for the generous sponsorship
  • Huggingface and in particular Sylvain for the Accelerate library
  • Alex for einops, indispensable tool for tensor manipulation

... and many others. Thank you!

Install

bash
$ pip install dalle2-pytorch

Usage

To train DALLE-2 is a 3 step process, with the training of CLIP being the most important

To train CLIP, you can either use x-clip package, or join the LAION discord, where a lot of replication efforts are already underway.

This repository will demonstrate integration with x-clip for starters

…

Then, you will need to train the decoder, which learns to generate images based on the image embedding coming from the trained CLIP above

…

Finally, the main contribution of the paper. The repository offers the diffusion prior network. It takes the CLIP text embeddings and tries to generate the CLIP image embeddings. Again, you will need the trained CLIP from the first step

…

In the paper, they actually used a recently discovered technique, from Jonathan Ho himself (original author of DDPMs, the core technique used in DALL-E v2) for high resolution image synthesis.

This can easily be used within this framework as so

…

Finally, to generate the DALL-E2 images from text. Insert the trained DiffusionPrior as well as the Decoder (which wraps CLIP, the causal transformer, and unet(s))

python
from dalle2_pytorch import DALLE2

dalle2 = DALLE2(
    prior = diffusion_prior,
    decoder = decoder
)

# send the text as a string if you want to use the simple tokenizer from DALLE v1
# or you can do it as token ids, if you have your own tokenizer

texts = ['glistening morning dew on a flower petal']
images = dalle2(texts) # (1, 3, 256, 256)

That's it!

Let's see the whole script below

…

Everything in this readme should run without error

You can also train the decoder on images of greater than the size (say 512x512) at which CLIP was trained (256x256). The images will be resized to CLIP image resolution for the image embeddings

For the layperson, no worries, training will all be automated into a CLI tool, at least for small scale training.

Training on Preprocessed CLIP Embeddings

It is likely, when scaling up, that you would first preprocess your images and text into corresponding embeddings before training the prior network. You can do so easily by simply passing in image_embed, text_embed, and optionally text_encodings

Working example below

…

You can also completely go CLIP-less, in which case you will need to pass in the image_embed_dim into the DiffusionPrior on initialization

…

OpenAI CLIP

Although there is the possibility they are using an unreleased, more powerful CLIP, you can use one of the released ones, if you do not wish to train your own CLIP from scratch. This will also allow the community to more quickly validate the conclusions of the paper.

To use a pretrained OpenAI CLIP, simply import OpenAIClipAdapter and pass it into the DiffusionPrior or Decoder like so

…

Alternatively, you can also use Open Clip

bash
$ pip install open-clip-torch

Ex. using the SOTA Open Clip model trained by Romain

python
from dalle2_pytorch import OpenClipAdapter

clip = OpenClipAdapter('ViT-H/14')

Now you'll just have to worry about training the Prior and the Decoder!

Inpainting

Inpainting is also built into the Decoder. You simply have to pass in the inpaint_image and inpaint_mask (boolean tensor where True indicates which regions of the inpaint image to keep)

This repository uses the formulation put forth by Lugmayr et al. in Repaint

…

Experimental

DALL-E2 with Latent Diffusion

This repository decides to take the next step and offer DALL-E v2 combined with latent diffusion, from Rombach et al.

You can use it as follows. Latent diffusion can be limited to just the first U-Net in the cascade, or to any number you wish.

The repository also comes equipped with all the necessary settings to recreate ViT-VQGan from the Improved VQGans paper. Furthermore, the vector quantization library also comes equipped to do residual or multi-headed quantization, which I believe will give an even further boost in performance to the autoencoder.

…

Training wrapper

Decoder Training

Training the Decoder may be confusing, as one needs to keep track of an optimizer for each of the Unet(s) separately. Each Unet will also need its own corresponding exponential moving average. The DecoderTrainer hopes to make this simple, as shown below

…

Diffusion Prior Training

Similarly, one can use the DiffusionPriorTrainer to automatically instantiate and keep track of an exponential moving averaged prior.

…

Bonus

Unconditional Training

The repository also contains the means to train unconditional DDPM model, or even cascading DDPMs. You simply have to set unconditional = True in the Decoder

ex.

…

Dataloaders

Decoder Dataloaders

In order to make loading data simple and efficient, we include some general dataloaders that can be used to train portions of the network.

Decoder: Image Embedding Dataset

When training the decoder (and up samplers if training together) in isolation, you will need to load images and corresponding image embeddings. This dataset can read two similar types of datasets. First, it can read a webdataset that contains .jpg and .npy files in the .tars that contain the images and associated image embeddings respectively. Alternatively, you can also specify a source for the embeddings outside of the webdataset. In this case, the path to the embeddings should contain .npy files with the same shard numbers as the webdataset and there should be a correspondence between the filename of the .jpg and the index of the embedding in the .npy. So, for example, 0001.tar from the webdataset with image 00010509.jpg (the first 4 digits are the shard number and the last 4 are the index) in it should be paralleled by a img_emb_0001.npy which contains a NumPy array with the embedding at index 509.

Generating a dataset of this type:

  1. Use img2dataset to generate a webdataset.
  2. Use clip-retrieval to convert the images to embeddings.
  3. Use embedding-dataset-reordering to reorder the embeddings into the expected format.

Usage:

…

Scripts

train_diffusion_prior.py

For detailed information on training the diffusion prior, please refer to the dedicated readme

Todo

  • finish off gaussian diffusion class for latent embedding - allow for prediction of epsilon
  • add what was proposed in the paper, where DDPM objective for image latent embedding predicts x0 directly (reread vq-diffusion paper and get caught up on that line of work)
  • make sure it works end

Issues· 73 open

View all issuesOpen on GitHub
  • #282

    can not generate normal image with pretrained model

    Updated Jan 9, 2025
  • #219

    how to use pretrained clip model in train_decoder.py

    Updated Jan 8, 2025
  • #230

    dataset for training???

    Updated Jan 8, 2025
  • #322

    Noisy output image

    Updated Dec 19, 2024
  • #311

    "Nan" values in loss.

    Updated Oct 8, 2024
  • #316

    The resulting images are of poor quality

    Updated Sep 25, 2024
  • #298

    Custom Dalle-2 trained decoder generating random noise

    Updated Aug 12, 2024
  • #300

    About the weight file of prior

    Updated Jul 23, 2024
  • #319

    How to eval diffusion prior?

    Updated Jul 17, 2024
  • #164

    decoder generates noise images

    Updated Apr 17, 2024

> Tags

Pythonartificial-intelligencedeep-learningtext-to-image

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 28, 2026
CategoryDevOps
PricingOpen source

> Related tools

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理