<jupyter_start><jupyter_text>DALL-E - Generating Images from Text * [DALLE-pytorch](https://github.com/lucidrains/DALLE-pytorch)<jupyter_code>COLAB = True
# --- Remember to change the runtime to use GPU for better performance
if COLAB:
    !nvidia-smi
  
!pip install -Uqq dalle-pytorch
from pathlib import Path
from tqdm import tqdm
import torch
from einops import repeat
from PIL import Image
from torchvision.utils import make_grid, save_image
from dalle_pytorch import DiscreteVAE, OpenAIDiscreteVAE, VQGanVAE1024, DALLE
from dalle_pytorch.simple_tokenizer import tokenize, tokenizer, VOCAB_SIZE
vae = OpenAIDiscreteVAE()
dalle = DALLE(vae = vae, dim=1024, depth=32).cuda()
image_text = "fireflies in a field under a full moon"
batch_size = 4
output_dir = "/content/"
num_images = 1
top_k = 0.9
image_size = vae.image_size
text = tokenize([image_text], dalle.text_seq_len).cuda()
text = repeat(text, '() n -> b n', b = num_images)
outputs = []
for text_chunk in tqdm(text.split(batch_size), desc = 'generating images'):
    output = dalle.generate_images(text_chunk, filter_thres = top_k)
    outputs.append(output)

outputs = torch.cat(outputs)
outputs_dir = Path(output_dir) / image_text.replace(' ', '_')
outputs_dir.mkdir(parents = True, exist_ok = True)

for i, image in tqdm(enumerate(outputs), desc = 'saving images'):
    save_image(image, outputs_dir / f'{i}.jpg')

print(f'created {num_images} images at "{str(outputs_dir)}"')
import glob
from IPython.display import Image 

images = glob.glob("/content/*/*.jpg")
for image in images:
   img = Image(filename=image)
   display(img)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1163076923076923, 'fraction_numerical': 0.008615384615384615, 'mean_word_length': 5.376470588235295, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '350769', 'license': 'MIT', 'n_tokens_mistral': 589, 'n_tokens_neox': 577, 'n_words': 150, 'path': 'nbs/07_dalle.ipynb', 'repo_name': 'stordopoulos/jupyter-notebooks', 'size': '1625'}