DifuseIt

Built by members

Project Overview

DifuseIt is a text-to-image diffusion model written from scratch in PyTorch by Entropic member Lucas Germinari, with the goal of understanding these models by implementing every piece of them. Its design follows Google's Imagen (Saharia et al., 2022) and covers the whole pipeline: variance scheduling, noise conditioning, a multi-scale U-Net with cross-attention, and scripts for training and sampling.

How diffusion models work

A diffusion model is built from two opposite processes:

  1. Forward diffusion gradually corrupts an image with Gaussian noise over T discrete timesteps, with step sizes set by a linear variance schedule (beta_1 to beta_T). Because every step adds Gaussian noise, the noisy image at any step t can be computed directly from the original image x_0 using cumulative products of the schedule (alphas_cumprod), without simulating the steps in between.
  2. Reverse denoising is the learned part: a neural network is trained to predict the noise that was added at step t. Removing that predicted noise one step at a time turns pure noise into an image that matches a text caption.
# the linear variance schedule used by GaussianDiffusion
scale = 1000 / timesteps
beta_start = scale * 0.0001
beta_end = scale * 0.02
betas = torch.linspace(beta_start, beta_end, timesteps, dtype=torch.float64)
alphas = 1.0 - betas
alphas_cumprod = torch.cumprod(alphas, axis=0)

Architecture

  • Denoising U-Net: a convolutional backbone whose downsampling and upsampling paths are joined by residual skip connections. Each level stacks ResNet blocks with group normalization and SiLU activations.
  • Timestep embeddings: the current timestep is encoded as a sinusoidal positional embedding and added to the feature maps of every ResNet block, so the network knows how far along the schedule it is.
  • Frozen T5 text encoder: instead of training a text encoder alongside the image model, captions go through a frozen t5-small. Its token embeddings reach the U-Net through spatial cross-attention layers.
  • Dynamic thresholding: strong classifier-free guidance tends to push pixel values into saturation. Clipping activations at percentile-based bounds keeps contrast stable at higher guidance scales.
  • Cascaded resolution: a base model generates a low-resolution image, and a separate super-resolution diffusion model then refines the detail.

Training and generation

  • main.py: a quick start that builds a compact model, runs a training step on a small test dataset, and generates a sample.
  • train.py: the full training pipeline, configurable from the command line (batch size, learning rate, epochs, gradient accumulation, checkpoint interval).
  • inference.py: loads checkpoints from a training directory and samples images for a single prompt or for a file of captions.