Lecture Note: Multimodal Transformers - Part 1

11-777 Multimodal Machine Learning CMU Fall 2023
Instructor: Dr. Louis-Philippe Morency | Co-lecturer: Dr. Paul Pu Liang
Author: Yi-Jing Sie

Overview

Building on the previous post's introduction to the Transformer architecture, its core components, and how it jointly performs alignment and representation learning on unimodal text data, this post examines how multimodal Transformers extend these ideas to jointly model language and vision.

Multimodal Transformers

Real-world communication rarely comes from text alone. Take a video, for example. It combines spoken or written language, facial and gestural visual cues, and acoustic tone of voice. That raises the question of how to build contextualized representations that draw on more than one modality at once. Two broad strategies show up here:

  • Concatenate Modalities and Learn BERT Transformer
  • Pairwise Cross-Modal Attention

Modality Concatenation

The simplest approach concatenates tokens from all modalities into a single sequence and runs that through a shared self-attention stack, letting cross-modal relationships emerge implicitly from self-attention.

  • VisualBERT: concatenates word tokens and region-based visual tokens into a single joint sequence, and then it pretrains the Transformer model using the same objectives as BERT over that sequence:
    1. Masked Language Modeling (MLM): Some text tokens are masked and must be predicted.
    2. Next Sentence Prediction (NSP): In the multimodal setting, it's a sentence-image prediction task, where model predicts if the text tokens correspond to the visual tokens,
  • UNITER: also uses the transformer architecture for Vision-and-Language tasks but with a different combination of pre-training objectives:
    1. Masked Language Modeling (MLM): masking text tokens.
    2. Masked Region Modeling (MRM): masking visual tokens.
    3. Image Text Matching (ITM) : A binary classification task indicating whether a sampled pair of image tokens and text tokens is a match
    4. Word Region Alignment (WRA): optimizing the transport cost between image embeddings and word embeddings in both directions through Optimal Transport.
Grid Image
Grid Image

Pairwise Cross-Modal Attention

Rather than concatenating everything into one sequence, this option keeps modalities separate and contextualize each other through dedicated cross-modal attention blocks.
Multimodal Transformer (MulT) introduced a crossmodal attention module. When modeling the information flowing from modality $B$ into modality $A$, the queries come from $A$ while the keys and values come from $B$.
A scaled dot-product attention between the queries and keys produces crossmodal attention weights, which are then used to pull correlated information out of $B$'s values. The output is combined with a residual connection from $A$ to produce a $B$-contextualized representation of $A$, such as a visually-contextualized representation of language.

Grid Image

Several follow-up works include:

  • ViLBERT introduced separate streams for vision and language representation learning that communicate through their proposed co-attention Transformer layers.
    Each stream takes either image region embeddings or word embeddings as input and consists of a series of standard Transformer (TRM) layers interleaved with co-attention Transformer (Co-TRM) layers. This ultimately produces separate visual and word representations that are contextualized by each other.
  • LXMERT similarly encodes image regions and words separately at first, and then processes their embeddings through separate streams. Each stream consists of multiple cross-modality blocks, where each block is a series of a cross-attention (Co-TRM from ViLBERT) layer, a standard self-attention layer and a feed-forward layer.
    Finally, the model produces 3 output representations for language, vision, and cross-modality, respectively. The cross-modality representation is obtained from the hidden state of the special [CLS] token prepended to the text sequence before entering the model and can be used for downstream multimodal classification tasks.
Grid Image
Grid Image

Modality-Shifting Fusion

A different way to combine modalities treats one modality as primary and the others as secondary, and shifts the primary representation through the signals from the secondary modalities. In a language example, where language is the primary modality while acoustic and visual signals are the secondary modalities, a gate applied to the secondary modalities determines how much to shift the primary representation, so the same word: "expectations", for instance, can end up closer to a negative-shifted or a positive-shifted representation depending on the accompanying nonverbal cues.

MAG-BERT (MAG + BERT) pushed this idea inside BERT with its Multimodal Adaptation Gate (MAG). At each token position, the language, acoustic, and visual features are passed through the MAG to derive the non-verbal cues that shift the language representation, conditioned on the acoustic and visual modalities. The shifting is performed by adding a non-verbal displacement vector $H$ to the original language representation, where the displacement vector $H$ is defined as follows:

"A vector with trajectory and magnitude that shifts the language-only position of the word to the new position in light of nonverbal behaviors."

The following describes how the Multimodal Adaptation Gate (MAG) works:
Let $Z_i$, $A_i$, and $V_i$ denote the $i$th language, acoustic, and visual input, respectively. Concating $Z_i$ with $A_i$ and $V_i$, respectively, gives two gating vectors $g^a_i$ and $ g^v_i $as follows: $$\begin{align*} g^a_i &= R(W_{ga}[Z_i;A_i] + b_a) \\ g^v_i &= R(W_{gv}[Z_i;V_i] + b_v)\end{align*}$$ where $R(x)$ is a non-linear activation function, $W_{ga}$ and $W_{gv}$ are linear layer weights, and $b_a$ and $b_v$ are linear layer biases. Then, a non-verbal displacement $H_i$ is defined as: $$H_i = g^a_i \otimes (W_aA_i) + g^v_i \otimes (W_vV_i) + b_{H}$$ where $W_a$ and $W_v$ are linear layer weights, $b_H$ is the bias, and $\otimes$ represents elementwise multiplication.
Finally, the nonverbal displacement $H_i$ is added to $Z_i$ to perform modality-shifting: $$\begin{align*}Z_i &= Z_i + \alpha H_i\\ \alpha &= \min \left( \frac{\|Z_i\|_2}{\|H_i\|_2} \beta, 1 \right) \end{align*}$$ where $\beta$ is a hyperparameter, and $\|\cdot\|_2$ denotes the $L_2$ norm function. The scaling factor $\alpha$ can be viewed as controlling the effect of the non-verbal shift $H_i$ by limiting it to no greater than one.

Grid Image
Grid Image

Key Takeaways

Contextualized Representations via Multimodal Transformers

Transformers are the most widely-used architecture for learning contextualized representations. Through the attention mechanism, they jointly perform alignment and representation learning on multimodal data.


Modality Concatenation

By concatenating unimodal embeddings from different modalities and passing them through a shared self-attention layer, cross-modal relationships emerge implicitly in the resulting representations.


Pairwise Cross-Modal Attention

Instead of implicitly contextualizing information across modalities, Pairwise Cross-Modal Attention explicitly allows information to flow in both directions by letting each modality alternately serve as the query, while the other as the keys and values in dedicated cross-modal attention blocks.


Modality Shifting

Unlike "Modality Concatenation" and "Pairwise Cross-Modal Attention", Modality Shifting uses a gating mechanism to shift the primary modality's representation based on cross-modal information with the secondary modalities. This achieves attention-like modulation on the primary modality without performing self-attention.