TL;DR
Textual Inversion teaches a text-to-image model a new word for one specific object or style, using only 3 to 5 pictures of it. The model itself is frozen; the only thing learned is one new embedding vector, which is the list of numbers the model uses for that word. Once learned, the new word can go into any sentence, such as "a painting of S* on the beach", and the model still draws on everything else it knows.
Builds on: Latent Diffusion Model, Word Embedding, Classifier-Free Guidance · Leads to: DreamBooth: Fine-Tuning Text-to-Image Diffusion for Subject-Driven Generation, which asks the same question but changes the model's weights as well
Why it mattered
Before this paper, a text-to-image model could draw "a cat" but could not draw your cat. The paper's motivating examples are a user who wants "our cat" turned into a painting, and a designer who wants a new product based on a favourite toy. Describing a specific toy in words is too lossy: "a brown plush rabbit with a red bow" fits many different toys.
The paper showed that a single learned word can carry a specific object or a specific art style into new scenes, and that the text interface stays usable for editing. The authors also note that the approach avoids the cost of retraining a large model on each new concept. That made personalised generation a question of learning a small amount of data, not building a new model. Later methods such as DreamBooth: Fine-Tuning Text-to-Image Diffusion for Subject-Driven Generation, which also changes the model's weights, took the idea further.
The problem
Three things were hard before this work.
- Naming a concept needs a word the model already knows. A model that has never seen your object has no word for it. Describing it with common words loses the details that make it yours.
- Retraining or fine-tuning a big model on a few images is costly and damaging. The paper says that re-training with an expanded dataset for each concept is too expensive, and that fine-tuning on few examples tends to cause catastrophic forgetting, where the model loses what it knew before.
- Frozen-model methods were either forgetful or inflexible. Adapter-style methods freeze the model and train extra transformation modules, but they still forget prior knowledge or struggle to use it alongside new concepts.
The paper's answer is to keep the model frozen and search for a new vocabulary entry instead, which is the idea of inversion: find the input that makes the model reproduce a given example.
How it works
Take one tiny concrete case: 3 photos of one ceramic cat, and a frozen text-to-image model. We want the word S* to mean this cat.
flowchart LR A["Prompt: A photo of S*"] --> B["Tokenizer: S* becomes one token"] B --> C["Embedding lookup: S* uses the learned vector v"] C --> D["Text encoder builds the conditioning code"] D --> E["Denoiser predicts the noise in a noisy latent"] E --> F["Loss: squared error against the true noise"] F -. "gradient changes only v" .-> C
- Pick a placeholder and start its vector. Call the new word S*. Give it an embedding vector v, the row the model would look up for a word. The paper starts v from the embedding of a coarse word for the object, such as "cat". In our toy example v has two numbers, v = (0.5, -1.0).
- Build a training sentence. Use a template such as "A photo of S*". The tokenizer turns the sentence into tokens, and the token for S* is swapped for the learned vector v. The rest of the sentence is unchanged.
- Encode the sentence. The text encoder reads the tokens and produces a conditioning code. This code is what steers the image model.
- Add noise to one photo and ask for the noise. Encode a photo into a small latent code, add random noise at a random step, and ask the denoiser to predict the noise. Suppose the true noise is (0.2, 1.0) and the prediction is (0.0, 0.5).
- Measure the error. Square each difference and add them up. The differences are 0.2 and 0.5, so the squared errors are 0.04 and 0.25, and the loss is 0.04 + 0.25 = 0.29.
- Change only v. Compute how the loss changes when v changes, and move v a small step in the direction that lowers it. The denoiser, the text encoder and every other weight stay fixed. Check: a smaller loss means the model predicts the noise better for this photo.
- Repeat and use. Repeat steps 2 to 6 with random photos from the set and random templates for 5,000 steps. The learned v is now the meaning of S*. Writing "a painting of S* on the beach" sends the same vector through the same frozen model, so the cat appears in the new scene.
The math
Definition. A pseudo-word is a placeholder string S* whose row in the token embedding table is replaced by a learned vector v. Everything else in the text encoder and the image model is frozen. The learned vector is the whole personalisation.
The paper's starting point is the loss for latent diffusion. Let be the encoder of a pretrained autoencoder, which maps an image to a spatial latent , and let be its decoder, so that is close to . A diffusion model is trained on latents: is the latent with noise added up to time step , is the unscaled noise sample, is the denoising network, and is the conditioning code for a text prompt .
In words: draw a photo, a prompt and a noise level at random, add that noise, and measure how far the predicted noise is from the true noise.
Step 1 in symbols: the placeholder and its vector
Write y_v for the prompt in which S* is represented by the vector v. The embedding table of the text encoder has one row per token. The row for S* is set to v, and the rest of the table is left alone. Only v is treated as a free parameter.
Step 2 in symbols: the textual inversion objective
The paper finds v by minimising the same loss, with the model fixed:
Here and are not changed. The expectation is over the 3 to 5 training photos and over randomly chosen templates such as "A photo of S*". Because the training goal is to reconstruct the photos, the learned vector has to hold visual detail. The paper argues that a discriminative objective, as used by PALAVRA, only needs to separate concepts, which is one reason its outputs show visual corruption.
Step 3 in symbols: one gradient step
Let be the loss above for one sampled photo, template and noise level. A gradient step moves v against the gradient:
Here is the learning rate and is the vector of how the loss changes with each number of v. The paper's base learning rate is 0.005, and it scales this by the number of GPUs and the batch size, for an effective rate of 0.04 on its setup of 2 GPUs and batch size 4. The text does not name the exact optimiser, so the lesson shows the simplest update form.
Worked examples
Let the embedding be one number v, with loss L(v) = (v - 2)^2. The loss is smallest at v = 2. Start at v = 0 with learning rate .
- The gradient is . At v = 0 it is .
- Update: .
- The loss fell from to .
- At v = 0.4 the gradient is . Update: .
- The loss is now .
Interpretation: each step lowers the loss and moves v toward the value that fits best. Textual Inversion does the same thing, except v has many numbers and L is a diffusion loss over photos.
Take one latent with two noise entries. The true noise is (0.2, 1.0). Two candidate embeddings give two denoiser predictions.
- Candidate A predicts (0.0, 0.5). Squared errors: and . Sum: .
- Candidate B predicts (0.1, 0.8). Squared errors: and . Sum: .
- Compare the sums: 0.29 for A, 0.05 for B. B is about 6 times smaller.
- Interpretation: an embedding that makes the denoiser predict the noise better on this photo is the kind of embedding the optimisation searches for. Training pushes v toward the choice that gives low loss across all photos and templates, not only this one.
The paper also tests a progressive version that adds extra pseudo-words during training. It starts with one vector, adds a second vector after 2,000 steps, and adds a third after 4,000 steps. The total run is 5,000 steps.
- The first vector trains alone for steps 0 to 1,999, which is 2,000 steps.
- The second vector joins at step 2,000 and trains until step 4,999, which is steps.
- The third vector joins at step 4,000 and trains until step 4,999, which is steps.
- Interpretation: the schedule lets the core shape of the concept settle first, and the extra vectors add finer detail on top. The paper's multi-word setups aim at more accurate reconstruction, and its single-word model has considerably better editability than every multi-word baseline it tests.
Try it: in the same 5,000-step run, how many steps does the third vector train for?
Answer: 1,000 steps, because it joins at step 4,000 and the run ends at step 5,000.
Results
The paper evaluates reconstruction (how close generated images are to the training photos, in CLIP space) and editability (how well generated images match a new text prompt). It compares the single-word method with multi-word and human-caption baselines.
- The single word is an appealing trade-off. Its reconstruction is comparable to the baselines and to random real images from the training set, and its editability is considerably better than every multi-word baseline. The paper's numbers are in its figures; this lesson does not quote them.
- A distortion-editability trade-off is visible. Embeddings that stay close to ordinary words are easier to edit but miss the concept's details. Embeddings that move far from ordinary words capture more detail but are harder to edit. Changing the learning rate moves the single-word model along this curve: the paper's high learning rate is 2e-2 and its low one is 1e-4.
- Human captions do worse. Captions of 12 words or fewer, and of 30 words or fewer, failed to capture the concept's likeness and reduced editability. A user study with 600 responses per questionnaire, 1,200 in total, agreed with the CLIP metrics on the same trade-off.
- Baselines fail in known ways. PALAVRA, a discriminative personalisation method, produced visible corruption because its objective only needed to separate concepts. Image-guided methods looked natural but did not generalise to new text.
- The number of images matters. The paper's sweep from one image up to 25 found the method works best with about 5 images. More images pull the embedding away from real words and hurt editing.
Limits
It is tempting to think that more images always give a better personal word. In fact the paper finds about 5 images is the sweet spot, because extra images push the embedding away from the words the model understands, which hurts editing.
It is tempting to think that the method changes the model. In fact the model is frozen, which is why the same word works in new prompts; the cost is that the method can only shape the model's output through that one word.
The paper is candid about limits. Learning one concept takes roughly two hours. The method tends to capture the semantic essence of an object rather than its exact shape. The paper also reports that composition is limited for complex relational prompts, such as placing two concepts side by side. Guidance scale matters too: the paper notes that typical latent-diffusion guidance values of 5 to 10 can stop the denoiser from keeping an object's structure when the prompt changes, while the state-of-the-art models it cites use scales near 2, where structure is preserved.
Reading guide
The paper is short in its main text and most of the detail is in its appendices. Read it in this order.
- Abstract and Section 1. The idea of learning a new pseudo-word from 3 to 5 images. This lesson's TL;DR follows it.
- Section 2, related work. GAN inversion, diffusion inversion and personalisation. The inversion ideas need Autoencoder and the framing of DDIM Sampler; the CLIP-based baselines need Learning Transferable Visual Models From Natural Language Supervision (CLIP).
- Section 3, method. The pipeline of tokens, embeddings and the conditioning code, and the textual inversion objective. Needs Latent Diffusion Model for the loss, Word Embedding for the embedding table and Classifier-Free Guidance for the guidance used at sampling time. The optimisation step needs Gradient Descent.
- Section 4, applications. Image variations, text-guided synthesis, style transfer, composition, bias reduction and downstream use with Blended Latent Diffusion. Read these as examples of the same vector used in new prompts.
- Section 5, quantitative analysis. The evaluation metrics, the setups (multi-vector, regularisation, per-image tokens) and the results. Needs the distortion-editability idea from GAN inversion, which this lesson defines in Results.
- Sections 6 and 7, limitations and social impact. Read these before using the method.
- Appendices A and B. Bipartite inversion, pivotal tuning and the training-set size sweep. These are alternatives the authors tested, not the main method.
Check yourself
QuestionWhat is frozen during Textual Inversion, and what is learned?Show answer
The denoising network, the text encoder and the autoencoder are all frozen, along with every other weight of the model. The only learned quantity is the embedding vector v for the placeholder S*. This is why the model keeps its general knowledge, and why the new word can be placed in any prompt.
QuestionA run uses 2 GPUs and batch size 4, with a base learning rate of 0.005 and the paper's scaling rule. What is the effective learning rate, and how many steps does a full run take?Show answer
The effective learning rate is . The paper's runs use 5,000 optimisation steps, so a full run is 5,000 steps.
QuestionWhy does the method optimise a reconstruction loss on the photos, rather than a CLIP-based score or a language-completion loss?Show answer
Generation is a visual task, so the word has to carry visual detail. A reconstruction loss on the photos asks the model to draw the concept well. The paper argues that PALAVRA's discriminative goal only needs to separate concepts, and reports visual corruption in its outputs.
QuestionYou want the learned word to make the model draw your friend's dog in a new scene. You have 4 photos, and you want to keep the model's general idea of dogs intact. What does the paper suggest, and why?Show answer
Use Textual Inversion with the 4 photos, which is within the 3 to 5 range the paper uses. Keep the model frozen so that its general knowledge of dogs is not changed. The paper recommends around 5 images as the best point, so 4 photos is reasonable, and adding many more would push the embedding away from ordinary words and hurt editing. The concept is then placed in prompts such as "a painting of S* on a beach".
Sources
- Gal et al., "An Image is Worth One Word: Personalizing Text-to-Image Generation using Textual Inversion," arXiv:2208.01618 (https://arxiv.org/abs/2208.01618)
- Project page for Textual Inversion (https://textual-inversion.github.io)
- Rombach et al., "High-Resolution Image Synthesis with Latent Diffusion Models," arXiv:2112.10752 (https://arxiv.org/abs/2112.10752)