Search

Search papers and lessons

DreamBooth: Fine-Tuning Text-to-Image Diffusion for Subject-Driven Generation

Nataniel Ruiz, Yuanzhen Li, Varun Jampani et al. · 2022

DreamBooth teaches a pretrained text-to-image diffusion model a new word for one specific subject from 3 to 5 photos, and a second loss term keeps the model's general idea of the subject's class intact.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Discrete math
  • Architectures
  • Linear algebra
  • Generative models
  • ReadReady to readNot yet

TL;DR

DreamBooth shows how to make a large text-to-image model draw one specific object, such as your dog or a particular handbag, in new scenes. It takes 3 to 5 photos of the subject, gives the subject a rare made-up word, and fine-tunes the model on those photos. A second training term, the prior preservation loss, stops the model from forgetting what the rest of its class looks like. The result is a model that keeps the subject's details while following new text prompts.

Builds on: Photorealistic Text-to-Image Diffusion Models with Deep Language Understanding, Prior Preservation Loss · Leads to: personalised image generation in general, where a single subject is added to a model that already knows the world

Why it mattered

Before this work, a text-to-image model could draw "a dog" but not your dog. Prompting harder did not help much, because words like "brown dog with a white chest" match thousands of animals. DreamBooth made a single subject a first-class thing the model can draw on request. The paper's examples include a vase in the snow, a vase on the beach, and a vase in the jungle.

The recipe spread quickly. Open-source toolkits, including the Hugging Face diffusers library, ship DreamBooth training scripts for Stable Diffusion, so people can personalise a model on a single consumer GPU with a few photos. The core idea, a rare identifier plus a class word plus a regularising loss, has been reused by later personalisation methods.

The problem

Three things were broken before this paper.

  1. Text cannot name a specific subject. A diffusion model maps a prompt to an image through a text encoder. Describing a particular clock in words is too lossy, so the model produces a clock that looks similar but not identical.
  2. Image-only methods lose the scene. Image-guided tools copy the look of the reference but cannot easily move the subject into a new context.
  3. Naive fine-tuning breaks the model. Training on 3 to 5 images for a long time makes the model overfit: every output looks like the training photos, and the general class (all dogs, all vases) drifts. The paper calls the second part language drift, a term borrowed from language models that lose general grammar when fine-tuned on a narrow task.

DreamBooth needs a method that adds a new subject to the model's vocabulary, lets the subject be combined with the words of any scene, and does not damage the class.

How it works

Take one tiny concrete case: a user has 4 photos of their dog, Rex. The model is a pretrained text-to-image diffusion model.

  1. Pick an identifier. Choose a rare token from the tokenizer's vocabulary, such as a short string that is rare in the training data. Call it [V]. The paper argues that common English words such as "unique" already carry meaning, so the model would have to unlearn that meaning first.
  2. Write the caption. Label every photo "a [V] dog". The word "dog" ties the new word to the class the model already understands. Without it, training is slower and language drift is worse.
  3. Make class images. Before training, ask the frozen original model to draw about 1000 images from the prompt "a dog", using random starting noise. These are the prior images. They show the model what dogs in general look like.
  4. Fine-tune on both sets. At each training step, compute a loss on a photo of Rex with the caption "a [V] dog", and a second loss on one of the model's own dog images with the caption "a dog". Add them with weight lambda. Update the weights of the model.
  5. Use it. Write a new prompt such as "a [V] dog on a beach at sunset". The model binds [V] to Rex, so the output shows Rex in the new scene.

Check: after step 2, the caption "a [V] dog" has 2 meaningful words in it, the identifier and the class. After step 4, a training step has two losses, so the total is their sum with the second one scaled by lambda.

flowchart LR
  A["3 to 5 subject photos"] --> C["Caption: a V dog"]
  B["Frozen model draws about 1000 a dog images"] --> D["Prior images"]
  C --> E["Subject loss"]
  D --> F["Prior preservation loss"]
  E --> G["Total loss = subject loss + lambda times prior loss"]
  F --> G
  G --> H["Update model weights"]
  H --> I["Prompt with V: new scenes of Rex"]

Worked examples

Example
1. A single loss on one photo

Take one photo of Rex and one class image. Suppose the model's squared error on the photo is 0.20 and on the class image is 0.10, with λ=1\lambda = 1 and weights wt=wt′=1w_t = w_{t'} = 1.

  1. The subject term is 0.200.20.
  2. The prior term is λ⋅0.10=0.10\lambda \cdot 0.10 = 0.10.
  3. The total is 0.20+0.10=0.300.20 + 0.10 = 0.30.

Interpretation: the update that lowers this total improves both how Rex looks and how well the model still draws dogs in general.

Example
2. The paper's setting

The paper trains Imagen for about 1000 iterations with λ=1\lambda = 1 and learning rate 10−510^{-5}. For Stable Diffusion it uses learning rate 5×10−65 \times 10^{-6}. It generates about 1000 class images, though fewer can be used. Training takes about 5 minutes on one TPUv4 for Imagen, and about 5 minutes on one NVIDIA A100 for Stable Diffusion.

  1. The 1000 prior images give the model a broad sample of the class at the start.
  2. The learning rate is small, so the weights move only a little per step, which limits overfitting to the 3 to 5 photos.
  3. The prior term runs at every step, so the model is pulled back toward the class even when the subject loss is still falling.
  4. Result: the subject is learned in about 1000 steps, and the model still generates varied dogs.

Interpretation: the paper's numbers show that DreamBooth is cheap to run, which is why it became practical.

Example
3. Try it: pick lambda

A training run reports the subject term 0.15 and the prior term 0.30. You want the prior term to count for half as much as the current setting. What lambda should you use, and what is the new total?

Answer: the current lambda is 1. Half as much weight means lambda = 0.5. The new total is 0.15+0.5×0.30=0.15+0.15=0.300.15 + 0.5 \times 0.30 = 0.15 + 0.15 = 0.30.

The math

Let x\mathbf{x} be a photo of the subject and c\mathbf{c} the embedding of its caption "a [V] dog". A diffusion model is trained to denoise: it is given a noisy version xt=αtx+σtϵ\mathbf{x}_t = \alpha_t \mathbf{x} + \sigma_t \boldsymbol{\epsilon} of the image, where ϵ\boldsymbol{\epsilon} is Gaussian noise, and it predicts the clean image x^θ(xt,c)\hat{\mathbf{x}}_\theta(\mathbf{x}_t, \mathbf{c}). The usual denoising loss is a weighted squared error between that prediction and the clean image.

Step 2 in symbols. The caption embedding is c=Γ(f("a [V] dog"))\mathbf{c} = \Gamma(f(\text{"a [V] dog"})). Here ff is the tokenizer that turns text into tokens, and Γ\Gamma is the text encoder. The class prompt for the prior images uses the same encoder: cpr=Γ(f("a dog"))\mathbf{c}_{\text{pr}} = \Gamma(f(\text{"a dog"})).

Step 3 in symbols. Each prior image comes from sampling the frozen model: xpr=x^(zt1,cpr)\mathbf{x}_{\text{pr}} = \hat{\mathbf{x}}(\mathbf{z}_{t_1}, \mathbf{c}_{\text{pr}}), where zt1\mathbf{z}_{t_1} is random noise drawn from a standard Gaussian, N(0,I)\mathcal{N}(\mathbf{0}, \mathbf{I}). The frozen model is never updated, so these images are a fixed record of its prior.

Step 4 in symbols. The full objective is

L=E[wt∥x^θ(αtx+σtϵ,c)−x∥22+λ wt′∥x^θ(αt′xpr+σt′ϵ′,cpr)−xpr∥22]\mathcal{L} = \mathbb{E}\left[ w_t \left\| \hat{\mathbf{x}}_\theta(\alpha_t \mathbf{x} + \sigma_t \boldsymbol{\epsilon}, \mathbf{c}) - \mathbf{x} \right\|_2^2 + \lambda\, w_{t'} \left\| \hat{\mathbf{x}}_\theta(\alpha_{t'} \mathbf{x}_{\text{pr}} + \sigma_{t'} \boldsymbol{\epsilon}', \mathbf{c}_{\text{pr}}) - \mathbf{x}_{\text{pr}} \right\|_2^2 \right]

The first term is the subject loss. The second term is the prior preservation loss. Its weight is λ\lambda, and the paper uses λ=1\lambda = 1. The weights wtw_t and wt′w_{t'} come from the noise schedule, and the two noise levels tt and t′t' are sampled independently.

In the tiny case, suppose the subject term is 0.20 and the prior term is 0.10 for one step. With λ=1\lambda = 1 the total is 0.20+0.10=0.300.20 + 0.10 = 0.30. With λ=0.5\lambda = 0.5 it would be 0.20+0.05=0.250.20 + 0.05 = 0.25, which gives the prior term less say in the update.

Rare identifier. The paper does not give a formula for the identifier, but for Imagen it samples tokens at random from those of at most 3 Unicode characters (without spaces) in the T5-XXL token range 5000 to 10000, so the model has little prior for them.

Results

The paper evaluates two things: subject fidelity, how much the generated subject looks like the real one, and prompt fidelity, how well the image follows the text. It uses DINO and CLIP-I for the first, and CLIP-T for the second. DINO is preferred because it is not trained to ignore differences between subjects of the same class, whereas CLIP-I can score two different subjects of one class as close.

MethodDINO (higher is better)CLIP-I (higher is better)CLIP-T (higher is better)
Real images0.7740.885not applicable
DreamBooth (Imagen)0.6960.8120.306
DreamBooth (Stable Diffusion)0.6680.8030.305
Textual Inversion (Stable Diffusion)0.5690.7800.255

In a user study with 72 participants (1800 answers in total), DreamBooth on Stable Diffusion was preferred over Textual Inversion for subject fidelity 68% of the time and for prompt fidelity 81% of the time.

The ablation on Imagen with 15 subjects isolates the prior term. With the prior preservation loss, the prior metric PRES (the DINO similarity between generated class images and the real subject, so lower means less of the subject leaks into the class) falls from 0.664 to 0.493, and the diversity metric DIV rises from 0.371 to 0.391. Subject fidelity drops slightly, DINO from 0.712 to 0.684. This is the trade the loss is designed to make: a little less snapping to the training photos for more variety and a class that survives.

Limits. The paper reports failure modes. For rare objects such as backpacks, the subject can clash with the class prior and come out misshapen. The context and the subject's appearance can become entangled, so a prompt can change the subject's colour. And with a few photos it can overfit and reproduce the training shots. Common subjects such as dogs and cats were easier to learn than rare objects. Two photos were enough for a common dog, while a backpack needed at least three.

Reading guide

Check yourself

QuestionWhy does the caption contain the class word 'dog' as well as the identifier?Show answer
The class word ties the new identifier to the class the model already knows, so training is faster and language drift is smaller. Without it, the identifier must be learned from nothing, which the paper found slower and worse.
QuestionWith lambda = 1, a subject term of 0.25 and a prior term of 0.05, what is the total loss?Show answer
The total is 0.25+1×0.05=0.300.25 + 1 \times 0.05 = 0.30.
QuestionWhy does the paper generate the class images with the frozen model instead of using a dataset of dogs?Show answer
The frozen model's own samples are exactly the distribution the model had before training. Training against them keeps that distribution in place. A fixed external dataset would pull the model toward a different distribution and would not reflect what the model already believes about the class.
QuestionA user fine-tunes on 4 photos for 20000 steps with no prior term and gets images that look just like the photos. Which failure is this and what does the paper suggest?Show answer
This is overfitting, which a long run without regularisation makes worse. The paper's remedy is the prior-preservation loss, and its recipe uses about 1000 iterations at a small learning rate.
QuestionWhich metric would you use to check that a generated dog is your dog, and why not the other?Show answer
Use DINO, because it is self-supervised to separate individual instances. CLIP-I can score two different dogs of the same breed as similar, so it is weaker for identity.

Sources

Read the full paper

Nataniel Ruiz, Yuanzhen Li, Varun Jampani et al., 2022. arXiv:2208.12242