liveBook · Manning

What you will learn in Build a Text-to-Image Generator (from Scratch)

With transformers and diffusions

by Mark Liu

Chapter by chapter

Chapter 1: A tale of two models: Transformers and diffusions

1.1 What is a text-to-image generation model? · 1.1.1 Unimodal vs. multimodal models · 1.1.2 Practical use cases of text-to-image models · 1.2 Transformer-based text-to-image generation · 1.2.1 Converting an image into a sequence of integers and then back · 1.2.2 Training and using a transformer-based text-to-image model · 1.3 Text-to-image generation with diffusion models · 1.3.1 Forward and reverse diffusions · 1.3.2 Latent diffusion models and Stable Diffusion · 1.4 How to build text-to-image models from scratch · 1.5 Challenges for text-to-image generation models · 1.5.1 Are generative AI models stealing from artists? · 1.5.2 The geometric inconsistency problem · 1.6 Social, environmental, and ethical concerns · 1.7 Summary

What you learn in chapter 1 →

Chapter 2: Build a transformer

2.1 An overview of attention and transformers · 2.1.1 How the attention mechanism works · 2.1.2 How to create a transformer · 2.2 Word embedding and positional encoding · 2.2.1 Word tokenization with the Spacy library · 2.2.2 A sequence padding function · 2.2.3 Input embedding from word embedding and positional encoding · 2.3 Creating an encoder–decoder transformer · 2.3.1 Coding the attention mechanism · 2.3.2 Defining the Transformer() class · 2.3.3 Creating a language translator · 2.4 Training and using the German-to-English translator · 2.4.1 Training the encoder–decoder transformer · 2.4.2 Translating German to English with the trained model · 2.5 Summary

What you learn in chapter 2 →

Chapter 3: Classify images with a vision transformer

3.1 The blueprint to train a ViT · 3.1.1 Converting images to sequences · 3.1.2 Training a ViT for classification · 3.2 The CIFAR-10 dataset · 3.2.1 Downloading and visualizing CIFAR-10 images · 3.2.2 Preparing datasets for training and testing · 3.3 Building a ViT from scratch · 3.3.1 Dividing images into patches · 3.3.2 Modeling the positions of different patches in an image · 3.3.3 Using the multi-head self-attention mechanism · 3.3.4 Building an encoder-only transformer · 3.3.5 Using the ViT to create a classifier · 3.4 Training and using the ViT to classify images · 3.4.1 Choosing the optimizer and the loss function · 3.4.2 Training the ViT for image classification · 3.4.3 Classifying images using the trained ViT · 3.5 Summary

What you learn in chapter 3 →

Chapter 4: Add captions to images

4.1 Training and using a transformer to add captions · 4.1.1 Preparing data and the causal attention mask · 4.1.2 Creating and training a transformer · 4.2 Preparing the training dataset · 4.2.1 Downloading and visualizing Flickr 8k images · 4.2.2 Building a vocabulary of tokens · 4.2.3 Preparing the training dataset · 4.3 Creating a multimodal transformer to add captions · 4.3.1 Defining a ViT as the image encoder · 4.3.2 Creating the decoder to generate text · 4.4 Training and using the image-to-text transformer · 4.4.1 Training the encoder–decoder transformer · 4.4.2 Adding captions to images with the trained model · 4.5 Summary

What you learn in chapter 4 →

Chapter 5: Generate images with diffusion models

5.1 The forward diffusion process · 5.1.1 How diffusion models work · 5.1.2 Visualizing the forward diffusion process · 5.1.3 Different diffusion schedules · 5.2 The reverse diffusion process · 5.3 A blueprint to train the U-Net model · 5.3.1 Steps in training a denoising U-Net model · 5.3.2 Preprocessing the training data · 5.4 Training and using the diffusion model · 5.4.1 The Denoising Diffusion Probabilistic Model noise scheduler · 5.4.2 Inference using the U-Net denoising model · 5.4.3 Training and using the denoising U-Net model · 5.5 Summary

What you learn in chapter 5 →

Chapter 6: Control what images to generate in diffusion models

6.1 Classifier-free guidance in diffusion models · 6.1.1 An overview of classifier-free guidance · 6.1.2 A blueprint to implement CFG · 6.2 Different components of a denoising U-Net model · 6.2.1 Time step embedding and label embedding · 6.2.2 The U-Net denoising model architecture · 6.2.3 Down blocks and up blocks in the U-Net · 6.3 Building and training the denoising U-Net model · 6.3.1 Building the denoising U-Net · 6.3.2 The Denoising Diffusion Probabilistic Model · 6.3.3 Training the diffusion model · 6.4 Generating images with the trained diffusion model · 6.4.1 Visualizing generated images · 6.4.2 How the guidance parameter affects generated images · 6.5 Summary

What you learn in chapter 6 →

Chapter 7: Generate high-resolution images with diffusion models

7.1 Attention in U-Net, DDIM, and image interpolation · 7.1.1 Incorporating the attention mechanism in the U-Net model · 7.1.2 Denoising Diffusion Implicit Models · 7.1.3 Image interpolation in diffusion models · 7.2 High-resolution flower images as training data · 7.2.1 Visualizing images in the training dataset · 7.2.2 Applying forward diffusion on flower images · 7.3 Building and training a U-Net for high-resolution images · 7.3.1 Building the denoising U-Net model · 7.3.2 Training the denoising U-Net model · 7.4 Image generation and interpolation · 7.4.1 Using the trained denoising U-Net to generate images · 7.4.2 Transition from one image to another · 7.5 Summary

What you learn in chapter 7 →

Chapter 8: CLIP: A model to measure the similarity between image and text

8.1 The CLIP model · 8.1.1 How the CLIP model works · 8.1.2 Selecting an image from Flickr 8k based on a text description · 8.2 Preparing the training dataset · 8.2.1 Image-caption pairs in Flickr 8k · 8.2.2 The DistilBERT tokenizer · 8.2.3 Preprocess captions and images for training · 8.3 Creating a CLIP model · 8.3.1 Creating a text encoder · 8.3.2 Creating an image encoder · 8.3.3 Building a CLIP model · 8.4 Training and using the CLIP model · 8.4.1 Training the CLIP model · 8.4.2 Using the trained CLIP model to select images · 8.4.3 Using the OpenAI pretrained CLIP model to select images · 8.5 Summary

What you learn in chapter 8 →

Chapter 9: Text-to-image generation with latent diffusion

9.1 What is a latent diffusion model? · 9.1.1 How variational autoencoders work · 9.1.2 Combining a latent diffusion model with a variational autoencoder · 9.2 Compressing and reconstructing images with VAEs · 9.2.1 Downloading the pretrained VAE · 9.2.2 Encoding and decoding images with the pretrained VAE · 9.3 Text-to-image generation with latent diffusion · 9.3.1 Guidance by the CLIP model · 9.3.2 Diffusion in the latent space · 9.3.3 Converting latent images to high-resolution ones · 9.4 Modifying existing images with text prompts · 9.5 Summary

What you learn in chapter 9 →

Chapter 10: A deep dive into Stable Diffusion

10.1 Generating images with Stable Diffusion · 10.2 The Stable Diffusion architecture · 10.2.1 Generating images from text with Stable Diffusion · 10.2.2 Text embedding interpolation · 10.3 Creating text embeddings · 10.4 Image generation in the latent space · 10.5 Converting latent images to high-resolution ones · 10.6 Summary

What you learn in chapter 10 →

Chapter 11: VQGAN: Convert images into sequences of integers

11.1 Converting images into sequences of integers and back · 11.2 Variational autoencoders · 11.2.1 What is an autoencoder? · 11.2.2 The need for VAEs and their training methodology · 11.3 Vector quantized variational autoencoders · 11.3.1 The need for VQ-VAEs · 11.3.2 The VQ-VAE model architecture and training process · 11.4 Vector quantized generative adversarial networks · 11.4.1 Generative adversarial networks · 11.4.2 VQGAN: A GAN with a VQ-VAE generator · 11.5 A pretrained VQGAN model · 11.5.1 Reconstructing images with the pretrained VQGAN · 11.5.2 Converting images into sequences of integers · 11.6 Summary

What you learn in chapter 11 →

Chapter 12: A minimal implementation of DALL-E

12.1 How min-DALL-E works · 12.1.1 Training min-DALL-E · 12.1.2 From prompt to pixels: Image generation at inference time · 12.2 Tokenizing and encoding the text prompt · 12.2.1 Tokenizing the text prompt · 12.2.2 Encoding the text prompt · 12.3 Iterative prediction of image tokens · 12.3.1 Loading the pretrained BART decoder · 12.3.2 Predicting image tokens using the BART decoder · 12.4 Converting image tokens to high-resolution images · 12.4.1 Loading the pretrained VQGAN detokenizer · 12.4.2 Visualizing the intermediate and final high-resolution outputs · 12.5 Summary

What you learn in chapter 12 →

Chapter 13: New developments and challenges in text-to-image generation

13.1 State-of-the-art text-to-image generators · 13.1.1 DALL-E series · 13.1.2 Google’s Imagen · 13.1.3 Latent diffusion models: Stable Diffusion and Midjourney · 13.2 Challenges and concerns · 13.3 A blueprint to fine-tune ResNet50 · 13.3.1 The history and architecture of ResNet50 · 13.3.2 A plan to fine-tune ResNet50 for classification · 13.3.3 Using ResNet50 to classify images · 13.4 Fine-tuning ResNet50 to detect fake images · 13.4.1 Downloading and preprocessing real and fake face images · 13.4.2 Fine-tuning ResNet50 · 13.4.3 Detecting deepfakes using the fine-tuned ResNet50 · 13.5 Summary

What you learn in chapter 13 →

Concepts explained in this book

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.