Fusion encoders: VisualBERT, ViLBERT, LXMERT, UNITER and OSCAR
Models that let words and image regions attend to each other: one shared stream or two streams with co-attention, pretraining task mixtures, word–region alignment by optimal transport, and object tags as anchors.
What runs in CodeSplash
- MM004VisualBERT Explained here
- MM005ViLBERT Explained here
- MM006LXMERT Explained here
- MM007UNITER Explained here
- MM008OSCAR Explained here
You will be able to
- Contrast single-stream and two-stream fusion encoders
- Trace co-attention between a text stream and a visual stream
- List the pretraining tasks fusion encoders combine and why their weights matter
- Explain how optimal transport and object tags align words with regions
Let the two sides talk
ReadA dual encoder compares an image and a text only as two finished vectors. A fusion encoderFusion encoderA Transformer that lets text tokens and image regions attend to each other, in one stream or two streams with co-attention. lets every word attend to every image region inside one Transformer, so it can check that "the red cup" refers to the cup that is red, not to some red thing and some cup.
Between 2019 and 2020 a family of such models appeared. Most read the image as region featuresRegion featuresImage features taken from an object detector's boxes and used as visual tokens, often with the detected class names.: the boxes and feature vectors of a pretrained object detector, so the detector's vocabulary and mistakes come along.
Attention weights, masks and heads, the mechanism the two sides talk through.
DExplorable · Attention: scores, masks and heads
Where does each word look, and what do the scaling and the masks change?
With this mask, how much of each word's attention lands on <pad>?
How many of the positions are <pad>.
Width d
Heads
Mask
Without masks or positions the output rows shuffle the same way.
- Little weight
- Most weight
- Masked: score set to −∞
Commit your prediction above to see the result
Make your prediction to see the result.
One stream: VisualBERT
ReadVisualBERT puts word tokens and region features into one sequence and runs a BERT-style Transformer over it. Plain self-attentionSelf-attentionEach token builds its next value as a weighted mix of all tokens, with weights from how well their keys match its query. then mixes the two modalities from the first layer: early fusion.
In the program, two word tokens and two region tokens attend to all four. The first word's output becomes the mix 0.654 and 0.346 of the two feature directions: it has drawn on the region that resembles it. Pretraining used captions with masked words and image–caption matching.
Two streams: ViLBERT
ReadViLBERT keeps a text stream and a visual stream, each with its own layers, and connects them with co-attention: in those layers each stream's queries attend to the other stream's keys and values, a form of cross-attentionCross-attentionAttention whose queries come from one sequence and keys and values from another, such as text reading image features..
In the program, three visual tokens read two words. The middle visual token resembles both words equally, so it splits its attention 0.500 and 0.500; the outer ones lean toward their look-alike word, 0.587 against 0.413. Separate streams let each modality keep its own depth before mixing.
Which wins for search?
PredictPredict first
You must find the best caption match among a million images for each new query. Which design can do it in under a second?
Reveal
The dual encoder. A fusion encoder needs a full forward pass for every image–query pair, a million passes per query. The usual answer is both: a dual encoder retrieves the top 100, and a fusion model reranks those.
LXMERT: a mixture of tasks
ReadLXMERT has three encoders: language, objects, and a cross-modal one with co-attention. It pretrained on five tasks at once: masked words, masked region features (regress them), masked region classes (predict the detector's label), image–text matching, and visual question answering.
The program lists five task losses and combines them with weights into 3.35. Those weights are design choices, not facts: change them and the model learns different things. Report them when comparing models.
UNITER: align words and regions
ReadUNITER is single-stream like VisualBERT, with two changes. It masks only one modality at a time, so a masked word is always predicted from the complete image. And it adds word–region alignment by optimal transport: find the cheapest plan that moves the words' "mass" onto the regions, and train to lower that cost for matching pairs.
In the program two words and two regions have a cost matrix favoring the diagonal; the best plan sends half the mass along each diagonal pair, at transport cost 0.15.
OSCAR: object tags as anchors
ReadWords and region features live in different spaces, so aligning them is hard to learn. OSCAR adds a third input: the detector's object tags written as words ("dog", "bicycle"). A caption word and a tag are both text, so they align easily, and the tag points at its region.
In the program the caption "a dog beside a bicycle" is checked against the tags dog, bicycle and person: the second and fifth words each match one tag, and "person" matches nothing. Real models learn softer matches than string equality, but the anchor idea is the same, and so is the limit: an object the detector didn't tag gets no anchor.
What runs here
ReadCheck: fusion encoders
CheckpointCheck yourself
How does VisualBERT combine text and image?
Show the answer
Answer: a
That's single-stream, early fusion. Every layer can relate any word to any region.
Check yourself
VisualBERT reads images as region features from a pretrained detector. What follows?
Show the answer
Answer: a
The detector's vocabulary and errors become the fusion model's. Later models moved to patch features to avoid this.
Check yourself
In ViLBERT's co-attention layers, where do the text stream's keys and values come from?
Show the answer
Answer: b
Each stream asks questions of the other: that's co-attention, two cross-attentions in opposite directions.
Check yourself
What does a two-stream design like ViLBERT allow that a single stream doesn't?
Show the answer
Answer: a
Region features and words have very different statistics; separate streams let each be processed in its own way.
Check yourself
Which of these was NOT one of LXMERT's pretraining tasks?
Show the answer
Answer: d
LXMERT is an encoder: it predicts words, regions, matches and answers, but doesn't generate images.
Check yourself
Two papers pretrain the same architecture with the same five tasks but different task weights. What should you expect?
Show the answer
Answer: b
A task mixture's weights decide what the shared encoder prioritizes. Without them, results can't be compared fairly.
Check yourself
What does UNITER's word–region alignment loss compute?
Show the answer
Answer: a
A low transport cost means words and regions pair up cheaply, which training encourages for matching image–text pairs.
Check yourself
Why does UNITER mask only one modality at a time during pretraining?
Show the answer
Answer: a
If words and regions were masked together, the model might lose the very evidence that would let it use the other modality.
Check yourself
What role do object tags play in OSCAR?
Show the answer
Answer: a
A word and a tag share the text space, so they align easily; the tag then points at its region.
Check yourself
A caption mentions a "kite", but the detector found no kite. What does OSCAR's anchor mechanism provide for that word?
Show the answer
Answer: b
Anchors only help for objects the detector tags. Missing tags are a quiet source of error.
Recap
Recap- Fusion encoders let words and regions attend to each other; they check details dual encoders blur, at one forward pass per pair.
- Single stream (VisualBERT, UNITER, OSCAR) mixes everything early; two streams (ViLBERT, LXMERT) exchange information by co-attention.
- Pretraining mixes tasks (masked words and regions, matching, QA); the task weights are design choices.
- UNITER aligns words and regions by optimal transport; OSCAR anchors them with detector tags, which inherit the detector's blind spots.
Concepts in this lesson