Search

Search papers and lessons

CapFilt

Level 4 · 11 min read

CapFilt cleans up noisy web image-text pairs: one model writes a new caption for each picture, another deletes the pairs whose text does not match the image.

On this page

  • Colour = topic mix
  • Architectures
  • Neural network basics
  • Calculus
  • Discrete math
  • Linear algebra
  • Probability
  • ReadReady to readNot yet

TL;DR

CapFilt is a recipe for making a large image-text training set cleaner before a model learns from it. Pictures scraped from the web come with alt text, the short description a page author typed, and much of that text does not describe the picture. CapFilt starts from one pre-trained network and makes two copies. The captioner writes a new caption for each web image. The filter decides, for each image and text pair, whether the text really matches the image, and removes the pairs that do not. The cleaned set then trains a new vision-language model.

Builds on: Vision-Language Model, Image Captioning · Leads to: BLIP, which introduced CapFilt

Where it's used

BLIP pre-training. The BLIP paper (Li et al., 2022) pre-trains one model on a set of about 14 million images. That set mixes two human-annotated datasets, COCO and Visual Genome, with two web datasets, Conceptual Captions and SBU. The web part is where CapFilt is applied. The bootstrapped data is then used to train the vision-language model that is finetuned for image-text retrieval, captioning and visual question answering. Its image side is a ViT.

A larger web source. The paper's largest set adds LAION, which its dataset table lists at 115 million images, to make 129 million. With CapFilt applied, that model reaches 81.9 text-retrieval recall at 1 on COCO after finetuning, against 80.6 for the 14-million-image model (Table 1).

Any project with many images and few captions. A photo archive, a product catalogue or a museum collection often has pictures with thin or missing descriptions. The same two-model idea applies: a captioner adds text, and a filter keeps only the text that fits. The filter needs a modest set of human-labeled pairs to learn what a good match looks like.

How it works

Here is one tiny instance. The four web pairs below are invented for illustration. They are not from the paper.

  1. Start with web pairs. We have four images, each with its alt text: a dog on a beach (alt text "IMG_2041.jpg"), a red bicycle against a brick wall ("red bicycle against a brick wall"), a bowl of soup ("click here to buy now"), and a cat on a sofa ("a cat sleeping on a grey sofa"). Two of the four alt texts are useless. Check: 4 images, 4 texts, 2 of them describe nothing.

  2. The captioner writes one new caption per image. The captioner looks only at the image and writes a sentence. For the four images it writes "a dog lying on sand", "a red bike next to a wall", "a bowl of tomato soup" and "a cat on a sofa". Now there are 8 candidate texts: 4 from the web and 4 synthetic. Check: 4 images times 1 caption each is 4 new texts, 4 + 4 = 8 in all.

  3. The filter checks every pair. The filter sees an image and a text and answers "match" or "no match". Suppose it answers "no match" for the dog's alt text "IMG_2041.jpg" and for the soup's "click here to buy now". It answers "match" for the other six. Check: 8 pairs in, 2 removed, 6 kept.

  4. Combine the kept pairs with the human-labeled pairs. The six kept pairs join the small human-labeled set. This new, cleaner set is what trains a new model. Check: the new set has no pair the filter rejected.

flowchart LR
  A["Web pairs: image and alt text"] --> B["Captioner writes one new caption per image"]
  B --> C["Filter checks every pair"]
  A --> C
  C --> D["Kept pairs"]
  H["Human-labeled pairs"] --> E["Combined dataset"]
  D --> E
  E --> F["Train a new model"]

The math

Definition: CapFilt

Let the web set be pairs (Iw,Tw)(I_w, T_w), where IwI_w is an image and TwT_w its alt text. CapFilt builds a captioner gg that maps an image to a synthetic text, Ts=g(Iw)T_s = g(I_w), and a filter ff that maps an image and a text to a yes or no decision, "match" or "no match". The new dataset keeps every pair the filter calls a match, from both the web texts and the synthetic texts, and adds the human-labeled pairs.

Step 1 in symbols: the web pairs

Each web pair is (Iw,Tw)(I_w, T_w): one image and its alt text. The four pairs in the walkthrough are four such pairs, and the filter is applied to each pair it sees.

Step 2 in symbols: the captioner

The captioner is trained to predict a text one word at a time, given the image. This is an autoregressive model. Write the caption as words t1,t2,…,tLt_1, t_2, \dots, t_L. The usual training loss sums, over the words, the negative log probability the model gives to the correct next word:

LLM=−∑i=1Llog⁡pθ(ti∣I,t<i)\mathcal{L}_{\text{LM}} = -\sum_{i=1}^{L} \log p_\theta(t_i \mid I, t_{\lt i})

Here pθp_\theta is the model with parameters θ\theta, and t<it_{\lt i} means all the words before word ii.

To write a caption, the paper uses nucleus sampling with threshold p=0.9p = 0.9. At each word, sort the candidate words by probability and keep the smallest group whose probabilities add up to at least 0.90.9. Then sample one word from that group, with probabilities rescaled to add to 1.

Step 3 in symbols: the filter

The filter answers a yes or no question for each pair. Its training uses labeled examples, where the label is 1 for a matching pair and 0 for a non-matching one. It keeps a pair when its decision is "match":

keep(I,T)={1if the filter predicts match for (I,T)0otherwise\text{keep}(I, T) = \begin{cases} 1 & \text{if the filter predicts match for } (I, T) \\ 0 & \text{otherwise} \end{cases}

Step 4 in symbols: the new dataset

Dnew=Dh  ∪  {(Iw,Tw):keep(Iw,Tw)=1}  ∪  {(Iw,Ts):keep(Iw,Ts)=1}\mathcal{D}_{\text{new}} = \mathcal{D}_h \;\cup\; \{(I_w, T_w) : \text{keep}(I_w, T_w) = 1\} \;\cup\; \{(I_w, T_s) : \text{keep}(I_w, T_s) = 1\}

Here Dh\mathcal{D}_h is the human-labeled set. The three parts are the human pairs, the kept web pairs and the kept synthetic pairs.

Worked examples

Example
1. Counting the kept pairs (tiny)
  1. Start with 4 images and 4 web texts. The captioner adds 4 synthetic texts, so there are 8 candidate pairs.
  2. The filter removes 2 of the 8 pairs: the dog's alt text and the soup's alt text.
  3. Kept pairs: 8−2=68 - 2 = 6.
  4. Share of pairs kept: 6/8=0.756 / 8 = 0.75, or 75 percent. The filter removed 25 percent.
Example
2. One nucleus-sampling step (standard)

Suppose the captioner, after the words "a cat on a", gives these probabilities for the next word:

WordProbability
sofa0.50
bed0.30
mat0.15
car0.05
  1. Sort by probability: sofa 0.50, bed 0.30, mat 0.15, car 0.05.
  2. Add them up in order: 0.50, then 0.80, then 0.95. The total first reaches 0.9 at "mat", so the group is {sofa, bed, mat}. "car" is left out.
  3. Rescale so the group adds to 1. The group's total is 0.95, so divide each probability by 0.95.
  4. sofa: 0.50/0.95≈0.5260.50 / 0.95 \approx 0.526. bed: 0.30/0.95≈0.3160.30 / 0.95 \approx 0.316. mat: 0.15/0.95≈0.1580.15 / 0.95 \approx 0.158.
  5. Check: 0.526+0.316+0.158=1.0000.526 + 0.316 + 0.158 = 1.000. The captioner now samples one of these three words. It never picks "car".
Example
3. The gain from both modules (realistic, from the paper)

This uses Table 1 of the BLIP paper, for the 14-million-image pre-training set, and retrieval on COCO with finetuning.

  1. Baseline with the original web texts only: text-retrieval recall at 1 is 78.4.
  2. Captioner only: 79.7. Filter only: 79.1.
  3. Captioner and filter together: 80.6.
  4. Gain from both modules over the baseline: 80.6−78.4=2.280.6 - 78.4 = 2.2 points.
  5. Captioning on COCO, measured by CIDEr: baseline 127.8, both modules 129.7. Gain: 129.7−127.8=1.9129.7 - 127.8 = 1.9.
  6. Interpretation: each module helps on its own, and together they help more, as the paper's text says.

Try it: In the paper's Table 2, beam search gives text-retrieval recall at 1 of 79.6, and nucleus sampling gives 80.6. Which decoding method is better, and by how many points? Answer: nucleus sampling, by 80.6−79.6=1.080.6 - 79.6 = 1.0 point.

Common mistakes and tradeoffs

It is tempting to think the captioner should write the most probable caption. In fact the most probable caption is often a safe, common phrase like "a man riding a wave", which adds little new information. Beam search finds those high-probability captions. Nucleus sampling picks more varied words, the captions are noisier, and the paper finds they help more (80.6 against 79.6 for beam search, in Table 2). The reason given is that varied captions carry more new information.

It is tempting to think the filter can be trusted to catch every bad caption. In fact the filter is also a model trained on a small set, and it makes mistakes in both directions. CapFilt does not claim to produce perfect text. It claims that the cleaned set is better on average than the raw web text.

It is tempting to think sharing weights between the captioner and the filter saves effort and therefore helps. In fact the paper finds the opposite. When the two share parameters, the filter keeps more of the captioner's own mistakes, so its noise ratio falls to 8 percent against 25 percent for the decoupled version. The paper calls this confirmation bias, and the downstream score is lower (79.8 against 80.6 for text-retrieval recall at 1).

When to use it. Use CapFilt when you have many web pairs, a limited set of human-labeled pairs, and a model that needs text to learn from.

When not to use it. If you already have a large, clean, human-labeled set, the cleaning adds cost and little benefit. If the captioner's errors are systematic, the filter may not catch them, so a human check on a sample is still worth doing.

Alternatives. Other ways to deal with noisy text include training on the web text as it is, filtering with a fixed rule such as keeping only pairs with long captions, or using only human-labeled data. CapFilt is different because it adds new text as well as removing bad text.

History

Early image captioning systems, such as the encoder-decoder model in Show and Tell (Vinyals et al., 2014), needed human-written captions for training. Human labeling is slow and expensive, so the research community turned to web pictures with the text found near them. Those texts are often short, off-topic or missing, which made noise a central problem.

Large web datasets such as Conceptual Captions, one of the sources the BLIP paper uses, made it practical to pre-train on noisy pairs at scale. BLIP (arXiv 2201.12086, 2022, by Junnan Li, Dongxu Li, Caiming Xiong and Steven Hoi) proposed CapFilt as a way to reuse that web data. The paper reports state-of-the-art results on image-text retrieval, image captioning and visual question answering at the time of publication.

Recap

  • A web image comes with alt text that is often wrong or empty.
  • The captioner writes a new caption for each image, one word at a time, using nucleus sampling so the captions are varied.
  • The filter, trained to say match or no match, removes the pairs whose text does not fit the image.
  • The kept pairs, plus a human-labeled set, form a cleaner training set for a new model.
  • Keep the captioner and filter as separate models; sharing their weights lets the filter pass the captioner's mistakes.

Read next: BLIP: Bootstrapping Language-Image Pre-training for Unified Vision-Language for the full model that uses CapFilt, and Learning Transferable Visual Models From Natural Language Supervision (CLIP) for a different way to learn from web image-text pairs, using contrastive learning.

Check yourself

QuestionWhat are the two modules in CapFilt, and what does each one do?Show answer

The captioner takes an image and writes a new caption for it. The filter takes an image and a text and decides whether they match; it removes the pairs it judges as not matching. Both are initialised from the same pre-trained model and finetuned separately on a small human-labeled set.

QuestionA bootstrapped set has 40 synthetic captions, and the filter removes 10 of them. What share of the synthetic captions is kept?Show answer

The filter keeps 40−10=3040 - 10 = 30 captions. The share kept is 30/40=0.7530 / 40 = 0.75, or 75 percent.

QuestionWhy does the paper keep the captioner and the filter as separate models rather than sharing their parameters?Show answer

If the two share parameters, the filter is more likely to accept the captions the captioner produced, including its mistakes. The paper calls this confirmation bias. The noise ratio falls to 8 percent with sharing, against 25 percent without, and the downstream results are lower with sharing.

QuestionA team has 50 million scraped image-caption pairs and 5,000 human-labeled pairs. Would CapFilt help them, and what does it need?Show answer

It probably would, because most of the data is noisy web text and CapFilt cleans it. It needs the human-labeled pairs, because both the captioner and the filter are finetuned on a small labeled set first. The 5,000 pairs are a modest set, so the team should check a sample of the filtered output by hand before training on it.

Sources