tensorfork / DALLE-pytorch

Implementation / replication of DALL-E, OpenAI's Text to Image Transformer, in Pytorch

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DALL-E Pytorch (wip)

Implementation / replication of DALL-E, OpenAI's Text to Image Transformer, in Pytorch. It will also contain CLIP for ranking the generations.

Install

$ pip install dalle-pytorch

Usage

Train VAE

import torch
from dalle_pytorch import DiscreteVAE

vae = DiscreteVAE(
    num_tokens = 2000,
    dim = 512,
    hidden_dim = 64
)

x = torch.randn(8, 3, 256, 256)
loss = vae(x, return_recon_loss)
loss.backward()

Train CLIP

import torch
from dalle_pytorch import CLIP

clip = CLIP(
    dim = 512,
    num_text_tokens = 10000,
    num_visual_tokens = 512,
    text_enc_depth = 6,
    visual_enc_depth = 6,
    text_seq_len = 256,
    visual_seq_len = 1024,
    text_heads = 8,
    visual_heads = 8
)

text = torch.randint(0, 10000, (2, 256))
images = torch.randint(0, 512, (2, 1024))
mask = torch.ones_like(text).bool()

loss = clip(text, images, text_mask = mask, return_loss = True)
loss.backward()

Train DALL-E

import torch
from dalle_pytorch import DALLE

dalle = DALLE(
    dim = 512,
    num_text_tokens = 10000,
    num_image_tokens = 512,
    text_seq_len = 256,
    image_seq_len = 1024,
    depth = 6, # should be 64
    heads = 8
)

text = torch.randint(0, 10000, (2, 256))
images = torch.randint(0, 512, (2, 1024))
mask = torch.ones_like(text).bool()

loss = dalle(text, images, mask = mask, return_loss = True)
loss.backward()

Combine pretrained VAE with DALL-E, and pass in raw images

import torch
from dalle_pytorch import DiscreteVAE, DALLE

vae = DiscreteVAE(
    num_tokens = 512,
    dim = 512
)

dalle = DALLE(
    dim = 512,
    vae = vae,
    num_text_tokens = 10000,
    num_image_tokens = 512,
    text_seq_len = 256,
    image_seq_len = 1024,
    depth = 6, # should be 64
    heads = 8
)

text = torch.randint(0, 10000, (2, 256))
images = torch.randn(2, 3, 256, 256) # train directly on raw images, VAE converts to proper embeddings
mask = torch.ones_like(text).bool()

loss = dalle(text, images, return_loss = True)
loss.backward()

Citations

@misc{unpublished2021dalle,
    title   = {DALL·E: Creating Images from Text},
    author  = {Aditya Ramesh, Mikhail Pavlov, Gabriel Goh, Scott Gray},
    year    = {2021}
}
@misc{unpublished2021clip,
    title  = {CLIP: Connecting Text and Images},
    author = {Alec Radford, Ilya Sutskever, Jong Wook Kim, Gretchen Krueger, Sandhini Agarwal},
    year   = {2021}
}

About

Implementation / replication of DALL-E, OpenAI's Text to Image Transformer, in Pytorch

License:MIT License


Languages

Language:Python 100.0%