Architecture overview

The notion of pre-training a large-scale machine learning model and then using it for downstream tasks first appeared in natural language processing (NLP). As shown in Supplementary Note Fig. S1c, large-scale NLP models like GPT50, BERT51, and their variants, take Transformers52 as text encoders to encode input texts into text embeddings, and then design pre-training objectives on top of these embeddings (e.g., generative loss and masked language modeling loss). In computer vision (CV), large-scale pre-training also becomes popular. Models like BiT53 and ViT54 use convolutional neural networks (CNNs) or Transformers as image encoders to obtain embeddings of input images. Similarly, pre-training losses are computed using the image embeddings (e.g., image classification loss and masked image patch prediction loss). However, these models are single-modal and thus only benefit downstream tasks in one modality. For multimodal (e.g., image, text, and audio) pre-training12,13,14,15,16,17,18,32, existing works can be divided into two groups according to their network architectures: single-tower models (e.g., UNITER17, OSCAR12, and M618) and two-tower ones (e.g., CLIP13 and ALIGN32). Our BriVL can also be categorized into the two-tower models since we use separate image and text encoders. But note that we actually adopt two additional momentum encoders to help with the pre-training process (i.e., to dynamically maintain negative sample queues across training batches), resulting in a four-tower pre-training architecture.

The pre-training goal of our BriVL is to learn two encoders that can embed image and text inputs into the same semantic space for effective image-text retrieval. To enforce the image and text encoders to learn better representations in the same embedding space, we introduce cross-modal contrastive learning with the InfoNCE loss23 into our BriVL. Specifically, our learning objective is to find the corresponding image embedding from a batch of them for a given text embedding and vice versa. By maximizing the cosine similarity of the image and text embeddings for each ground-truth pair while minimizing the cosine similarities of the embeddings from negative pairs, we jointly train the image and text encoders to learn an aligned cross-modal embedding space.

Formally, for the image-text retrieval task, we denote the training set as \({{{{{{{\mathcal{D}}}}}}}}=\{({x}_{i}^{(i)},{x}_{i}^{(t)})| i=1,\cdots \,,N\}\), where \(({x}_{i}^{(i)},{x}_{i}^{(t)})\) is a matched image-text pair, and N is the size of \({{{{{{{\mathcal{D}}}}}}}}\). Our BriVL leverages contrastive learning by applying MoCo29 into the cross-modal scenario, as illustrated in Supplementary Note Fig. S1a. Each image \({x}_{i}^{(i)}\) (or each text \({x}_{i}^{(t)}\)) is encoded by the image encoder f(i) (or the text encoder f(t)) to obtain its d-dimensional embedding \({{{{{{{{\bf{z}}}}}}}}}_{i}^{(i)}\) (or \({{{{{{{{\bf{z}}}}}}}}}_{i}^{(t)}\)). The image encoder (see Supplementary Note Fig. S1b) contains a CNN backbone, a successive self-attention (SA) block, and a multi-layer perceptron (MLP). A sequence of patch embeddings are first obtained by applying multi-scale patch pooling (MSPP) to the feature map from CNN. They are then fused/encoded by the SA block. The text encoder, on the other hand, is stacked by several SA blocks such as BERT51 and RoBERTa4. A two-layer MLP block with a ReLU55 activation layer is used for mapping each encoder’s representation into the joint cross-modal embedding space. The parameters of f(i) and f(t) are denoted as θ(i) and θ(t), respectively.

Image encoder

To obtain better performance in the image-text retrieval task, most previous methods19,20,21,56 utilize a bottom-up attention mechanism57 with object features extracted by the Faster R-CNN detector58. However, extracting region/object features with a heavy detector is computationally expensive, e.g., a Faster R-CNN detector typically costs 0.064s (15.6 fps) to extract fine-grained region information from an image of moderate size. Meanwhile, the image-text retrieval would be inevitably limited by the detector’s performance, which is not adaptable to real-world applications. In this paper, we thus introduce a simple yet effective module named Multi-Scale Patch Pooling (MSPP) to address this problem.

For each input image x(i), we first split it into multiple patches at different scales and record the patch coordinates. In all experiments, we take two scales as 1 × 1 and 6 × 6, resulting in a total of 37 patches. Next, we project each set of patch coordinates onto the feature map that is obtained by the CNN backbone (e.g., EfficientNet43) and generate a sequence of 37 region feature maps. Finally, we apply average pooling to each region feature map and obtain a sequence of patch features \({{{{{{{\bf{S}}}}}}}}\in {{\mathbb{R}}}^{c\times {N}_{p}}\), where each column corresponds to a patch, Np is the number of patches (i.e., Np = 37 in this paper), and c is the number of channels in the feature map.

To better capture the relationship of image patch features, we deploy a self-attention (SA) block containing multiple Transformer52 encoder layers. Each Transformer encoder layer consists of a multi-head attention (MultiHeadAttn) layer and a feed forward network (FFN) layer:

$${{{{{{{\bf{S}}}}}}}}^{\prime} ={{{{{{{\rm{LayerNorm}}}}}}}}({{{{{{{\bf{S}}}}}}}}+{{{{{{{\rm{MultiHeadAttn}}}}}}}}({{{{{{{\bf{S}}}}}}}}))$$

(1)

$${{{{{{{\bf{S}}}}}}}}={{{{{{{\rm{LayerNorm}}}}}}}}({{{{{{{\bf{S}}}}}}}}^{\prime} +{{{{{{{\rm{FFN}}}}}}}}({{{{{{{\bf{S}}}}}}}}^{\prime} )).$$

(2)

We then fuse the extracted patch features by applying an average pooling layer:

$${{{{{{{{\bf{r}}}}}}}}}^{(i)}=\frac{1}{{N}_{p}}\mathop{\sum }\limits_{j=1}^{{N}_{p}}{{{{{{{{\bf{S}}}}}}}}}_{j}\in {{\mathbb{R}}}^{c},$$

(3)

where Sj is the j-th column of S. A two-layer MLP block with a ReLU activation layer is adopted to project r(i) to the joint cross-modal embedding space, resulting in the final d-dimensional image embedding \({{{{{{{{\bf{z}}}}}}}}}^{(i)}\in {{\mathbb{R}}}^{d}\).

Text encoder

Given a sentence x(t), we first tokenize it to obtain a sequence of tokens \({{{{{{{\mathcal{T}}}}}}}}=\{{{{{{{{{\bf{t}}}}}}}}}_{j}| j=1,…,l\}\), where l denotes the length of the sentence (e.g., the number of words) and tj denotes the j-th token of \({{{{{{{\mathcal{T}}}}}}}}\). A pre-trained Transformer encoder (e.g., RoBERTa42) is then used to map text tokens to a sequence of feature vectors (each feature vector corresponds to a word). Similarly, to better capture the relationship between words, we use the same self-attention mechanism as in the image encoder to extract the text representation r(t). A two-layer MLP block with a ReLU activation layer is also used for mapping the text representation r(t) to the joint cross-modal embedding space, resulting in the final d-dimensional text embedding \({{{{{{{{\bf{z}}}}}}}}}^{(t)}\in {{\mathbb{R}}}^{d}\).

Contrastive loss

The cross-modal contrastive loss in our BriVL is defined based on MoCo29, which provides a mechanism of building dynamic sample queues for contrastive learning. Since the two negative queues used in our BriVL decouple the queue size from the mini-batch size, we can have a much larger negative sample size than the mini-batch size (thus GPU-resource-saving).

To maintain large queues of samples coming from different mini-batches and address the problem that sample features are extracted by encoders with very different parameters, we need two more smoothly updated encoders, that is, momentum encoders. The parameters \({\theta }_{m}^{(i)}\) (or \({\theta }_{m}^{(t)}\)) of the momentum image encoder \({f}_{m}^{(i)}\) (or the momentum text encoder \({f}_{m}^{(t)}\)) are updated in each training iteration with a momentum hyper-parameter m:

$${\theta }_{m}^{(i)}=m\cdot {\theta }_{m}^{(i)}+(1-m)\cdot {\theta }^{(i)},$$

(4)

$${\theta }_{m}^{(t)}=m\cdot {\theta }_{m}^{(t)}+(1-m)\cdot {\theta }^{(t)}.$$

(5)

Further, we maintain two negative sample queues \({{{{{{{{\mathcal{Q}}}}}}}}}^{(i)}\) and \({{{{{{{{\mathcal{Q}}}}}}}}}^{(t)}\), which contain Nq image negatives and Nq text negatives for contrastive learning, respectively. In each pre-training iteration with the batch size Nb, all Nb image negatives and Nb text negatives are separately pushed into these two queues. Meanwhile, there are Nb earliest samples being popped out of each queue. Concretely, at iteration t, the image and text negatives from the current data batch \(({{{{{{{{\mathcal{B}}}}}}}}}_{t}^{(i)},{{{{{{{{\mathcal{B}}}}}}}}}_{t}^{(t)})\) are computed by respectively forwarding the momentum encoders \({f}_{m}^{(i)}\) and \({f}_{m}^{(t)}\):

$${{{{{{{{\mathcal{N}}}}}}}}}_{t}^{(i)}=\left\{{f}_{m}^{(i)}({x}_{i}^{(i)})| {x}_{i}^{(i)}\in {{{{{{{{\mathcal{B}}}}}}}}}_{t}^{(i)}\right\},$$

(6)

$${{{{{{{{\mathcal{N}}}}}}}}}_{t}^{(t)}=\left\{{f}_{m}^{(t)}({x}_{i}^{(t)})| {x}_{i}^{(t)}\in {{{{{{{{\mathcal{B}}}}}}}}}_{t}^{(t)}\right\},$$

(7)

where \(| {{{{{{{{\mathcal{B}}}}}}}}}_{t}^{(i)}| =| {{{{{{{{\mathcal{B}}}}}}}}}_{t}^{(t)}| ={N}_{b}\). The obtained \({{{{{{{{\mathcal{N}}}}}}}}}_{t}^{(i)}\) and \({{{{{{{{\mathcal{N}}}}}}}}}_{t}^{(t)}\) are then pushed into \({{{{{{{{\mathcal{Q}}}}}}}}}^{(i)}\) and \({{{{{{{{\mathcal{Q}}}}}}}}}^{(t)}\), respectively. Note that although we generally call \({{{{{{{{\mathcal{N}}}}}}}}}_{t}^{(i)}\) (or \({{{{{{{{\mathcal{N}}}}}}}}}_{t}^{(t)}\)) image negatives (or text negatives), there is still one sample being positive to each text (or image). Here, we denote the positive image sample (or text sample) for the j-th input text \({x}_{j}^{(t)}\) (or the j-th input image \({x}_{j}^{(i)}\)) of the current mini-batch as:

$${{{{{{{{\bf{p}}}}}}}}}_{j}^{(i)}={f}_{m}^{(i)}({x}_{j}^{(i)})\in {{{{{{{{\mathcal{N}}}}}}}}}_{t}^{(i)},$$

(8)

$${{{{{{{{\bf{p}}}}}}}}}_{j}^{(t)}={f}_{m}^{(t)}({x}_{j}^{(t)})\in {{{{{{{{\mathcal{N}}}}}}}}}_{t}^{(t)}.$$

(9)

With the two negative queues, the loss function in each training iteration is thus computed as follows. For each input image \({x}_{i}^{(i)}\), we define the contrastive loss between its image embedding \({{{{{{{{\bf{z}}}}}}}}}_{i}^{(i)}\) and all positive/negative texts in the queue \({{{{{{{{\mathcal{Q}}}}}}}}}^{(t)}\) as an InfoNCE loss23:

$${{{{{{{{\mathcal{L}}}}}}}}}_{i2t}=-\frac{1}{{N}_{b}}\mathop{\sum }\limits_{i}^{{N}_{b}}\log \frac{\exp \left({{{{{{{{\bf{z}}}}}}}}}_{i}^{(i)}\cdot {{{{{{{{\bf{p}}}}}}}}}_{i}^{(t)}/\tau \right)}{\exp \left({{{{{{{{\bf{z}}}}}}}}}_{i}^{(i)}\cdot {{{{{{{{\bf{p}}}}}}}}}_{i}^{(t)}/\tau \right)+\mathop{\sum}\limits_{{{{{{{{{\bf{n}}}}}}}}}^{(t)}}\exp \left({{{{{{{{\bf{z}}}}}}}}}_{i}^{(i)}\cdot {{{{{{{{\bf{n}}}}}}}}}^{(t)}/\tau \right)},$$

(10)

where \({{{{{{{{\bf{n}}}}}}}}}^{(t)}\in {{{{{{{{\mathcal{Q}}}}}}}}}^{(t)}\setminus \{{{{{{{{{\bf{p}}}}}}}}}_{i}^{(t)}\}\) denotes a text negative for each image, τ is the temperature hyper-parameter, and the vector similarity is measured by dot product (⋅). Similarly, for each input text \({x}_{i}^{(t)}\), the InfoNCE loss is given by:

$${{{{{{{{\mathcal{L}}}}}}}}}_{t2i}=-\frac{1}{{N}_{b}}\mathop{\sum }\limits_{i}^{{N}_{b}}\log \frac{\exp \left({{{{{{{{\bf{z}}}}}}}}}_{i}^{(t)}\cdot {{{{{{{{\bf{p}}}}}}}}}_{i}^{(i)}/\tau \right)}{\exp \left({{{{{{{{\bf{z}}}}}}}}}_{i}^{(t)}\cdot {{{{{{{{\bf{p}}}}}}}}}_{i}^{(i)}/\tau \right)+\mathop{\sum}\limits_{{{{{{{{{\bf{n}}}}}}}}}^{(i)}}\exp \left({{{{{{{{\bf{z}}}}}}}}}_{i}^{(t)}\cdot {{{{{{{{\bf{n}}}}}}}}}^{(i)}/\tau \right)},$$

(11)

where \({{{{{{{{\bf{n}}}}}}}}}^{(i)}\in {{{{{{{{\mathcal{Q}}}}}}}}}^{(i)}\setminus \{{{{{{{{{\bf{p}}}}}}}}}_{i}^{(i)}\}\) denotes an image negative for each text.

The total loss function for pre-training our BriVL is then defined as:

$${{{{{{{{\mathcal{L}}}}}}}}}_{total}={{{{{{{{\mathcal{L}}}}}}}}}_{i2t}+{{{{{{{{\mathcal{L}}}}}}}}}_{t2i}.$$

(12)

In the test/evaluation stage, given each query image (or text), the cross-modal retrieval results are obtained simply by the dot product defined over the outputs of the text (or image) encoder.

Implementation details

Over the input images, we adopt random graying and random color jittering for data augmentation. All images are resized to 600 × 600 pixels. We adopt EfficientNet-B743 as the CNN backbone in the image encoder and RoBERTa-Large42 as the basis Transformer in the text encoder. For both image and text encoders, the self-attention block consists of 4 Transformer encoder layers and the MLP block has two fully-connected layers with a ReLU activation layer. The final embedding size of the joint cross-modal space is 2,560. We select the hyper-parameters heuristically for pre-training our BriVL model due to the computational constraint: the temperature hyper-parameter τ = 0.07, momentum m = 0.99, and the queue size Nq = 13, 440. We adopt the Adam optimizer59, with the weight decay 1e-5 and the learning rate 1e-4. We use a mini-batch size of 192 for each of the 14 machines (each machine has 8 NVIDIA A100 GPUs), resulting in a total batch size of 2688 (far smaller than Nq). The resource-saving advantages of such batch setting are shown by the ablation study results in Supplementary Note Fig. S2. We also deploy the latest distributed-training framework DeepSpeed26 to accelerate the pre-training process and save the GPU memories. With 112 NVIDIA A100 GPUs in total, it takes about 10 days to pre-train our BriVL model over our WSCD of 650 million image-text pairs.

Differences from CLIP/ALIGN

We have stated two main differences between our BriVL and CLIP/ALIGN in the Introduction section. Below we give more detailed differences technically. (1) We adopt a four-tower network architecture (see Supplementary Note Fig. S1a) for pre-training. By extending the original single-modal contrastive learning (CL) algorithm MoCo29, we introduce momentum encoders and negative sample queues for multimodal pre-training in a more GPU-resource-saving way. In contrast, both CLIP and ALIGN employ the standard two-tower architecture, which requires large batch size (thus enough negative samples) to be effective, taking up a mass of GPU memories. (2) We additionally devise a multi-scale patch pooling (MSPP) module (see Supplementary Note Fig. S1b) to capture fine-grained image region representations without using object detectors. While CLIP and ALIGN only consider global-level image embeddings, which impedes their ability to learn fine-grained/local image features.

Formalization of neural network visualization

Neural network visualization is developed to directly show the visual response/imagination of BriVL w.r.t. the semantic input. Formally, given the pre-trained image and text encoders f(i) and f(t) of BriVL, we first input a piece of text x(t) and obtain its text embedding \({{{{{{{{\bf{z}}}}}}}}}^{(t)}={f}^{(t)}({x}^{(t)})\in {{\mathbb{R}}}^{d}\). In the mean time, we randomly initialize a noisy image \({x}^{(i)}\in {{\mathbb{R}}}^{600\times 600\times 3}\), which contains all the learnable parameters throughout the entire visualization process. Further, we obtain the image embedding \({{{{{{{{\bf{z}}}}}}}}}^{(i)}={f}^{(i)}({x}^{(i)})\in {{\mathbb{R}}}^{d}\) and define the learning objective by matching the two embeddings:

$${{{{{{{{\mathcal{L}}}}}}}}}_{vis}=-\cos \left({{{{{{{{\bf{z}}}}}}}}}^{(i)},{{{{{{{{\bf{z}}}}}}}}}^{(t)}\right),$$

(13)

where \(\cos (\cdot ,\cdot )\) computes the cosine similarity between two vectors. With the resultant gradients, we are able to update the input image x(i) by back-propagation. After repeating the above updating step with multiple iterations, we finally obtain an image x(i), which can be regarded as BriVL’s response/imagination about the input text. The algorithm for neural network visualization is summarized in Algorithm 1.

Algorithm 1

Neural Network Visualization

Input: The pre-trained image and text encoder f(i) and f(t) of our BriVL

   A piece of text x(t)

   A randomly initialized image x(i)

   A learning rate parameter λ

Output: The updated input image

1: Obtain the text embedding z(t) = f(t)(x(t));

2: for all iteration = 1, 2, ⋯  , MaxIteration do

3:    Obtain the image embedding z(i) = f(i)(x(i));

4:    Compute \({{{{{{{{\mathcal{L}}}}}}}}}_{vis}\) with Eq. (13);

5:    Compute the gradients \({\nabla }_{{x}^{(i)}}{{{{{{{{\mathcal{L}}}}}}}}}_{vis}\);

6:    Update x(i) using gradient descent with λ;

7: end for

8: return the updated input image.

Formalization of text-to-image generation

To make BriVL’s response/imagination on input texts better understood, we further adopt VQGAN34 to help generate more photo-realistic images. The reason of utilizing VQGAN instead of other GANs60 is as follows. Although classic GANs are able to generate high quality images under specific domains (e.g., natural sceneries or human faces), they tend to fail when complex scenarios are involved. In contrast, VQGAN alleviates this problem and performs better under complex scenarios by combining VQVAE61 and GAN. For our text-to-image generation, we only need a codebook \({{{{{{{\mathcal{C}}}}}}}}\) and a CNN generator g of the VQGAN pre-trained on ILSVRC-201235. The pre-trained codebook \({{{{{{{\mathcal{C}}}}}}}}=\{{{{{{{{{\bf{c}}}}}}}}}_{k}\in {{\mathbb{R}}}^{{d}_{c}}| k=1,2,\cdots ,{N}_{c}\}\) is a collection of tokens/codes, where dc is the dimension of each code and Nc is the number of codes in the codebook (dc = 256 and Nc = 1, 024 in our case). The pre-trained CNN generator g takes a spatial collection of codebook entries \({{{{{{{\bf{U}}}}}}}}\in {{\mathbb{R}}}^{h\times w\times {d}_{c}}\) as input to generate an image (U can also be regarded as a sequence of hw codes, h = w = 16 in our case), where each element \({{{{{{{{\bf{u}}}}}}}}}_{ij}\in {{\mathbb{R}}}^{{d}_{c}}\) (i = 1, 2, \(\cdots,\) h and j = 1, 2, \(\cdots,\) w) must come from the codebook \({{{{{{{\mathcal{C}}}}}}}}\) (i.e., \({{{{{{{{\bf{u}}}}}}}}}_{ij}\in {{{{{{{\mathcal{C}}}}}}}}\)). With the pre-trained image and text encoders f(i) and f(t) of BriVL, we first input a piece of text x(t) and obtain its text embedding \({{{{{{{{\bf{z}}}}}}}}}^{(t)}={f}^{(t)}({x}^{(t)})\in {{\mathbb{R}}}^{d}\). Meanwhile, we randomly initialize an input code collection U, which is the only parameter matrix to be learned. Afterwards, we generate an image from the generator x(i) = g(U) and further obtain its image embedding \({{{{{{{{\bf{z}}}}}}}}}^{(i)}={f}^{(i)}({x}^{(i)})\in {{\mathbb{R}}}^{d}\). The learning objective is to maximize the similarity between two embeddings:

$${{{{{{{{\mathcal{L}}}}}}}}}_{t2i}=-\cos \left({{{{{{{{\bf{z}}}}}}}}}^{(i)},{{{{{{{{\bf{z}}}}}}}}}^{(t)}\right).$$

(14)

After updating the input U and obtaining \({{{{{{{\bf{U}}}}}}}}^{\prime}\) by back-propagation, we need to perform an element-wise quantization of each spatial code \({{{{{{{{\bf{u}}}}}}}}}_{ij}^{\prime}\in {{\mathbb{R}}}^{{d}_{c}}\) in \({{{{{{{\bf{U}}}}}}}}^{\prime}\) onto its closest codebook entry ck:

$${{{{{{{{\bf{u}}}}}}}}}_{ij}=\arg \mathop{\min }\limits_{{{{{{{{{\bf{c}}}}}}}}}_{k}\in {{{{{{{\mathcal{C}}}}}}}}}{\parallel} {{{{{{{{\bf{u}}}}}}}}}_{ij}^{\prime}-{{{{{{{{\bf{c}}}}}}}}}_{k}{\parallel} .$$

(15)

By repeating the above updating step with multiple iterations, we finally obtain an image x(i) generated with the updated U. The algorithm for text-to-image generation is summarized in Algorithm 2.

Algorithm 2

Text-to-Image Generation

Input: The pre-trained image and text encoder f(i) and f(t) of our BriVL

   The codebook \({{{{{{{\mathcal{C}}}}}}}}\) and the CNN generator g of the pre-trained VQGAN

   A piece of text x(t)

   A randomly initialized collection of codebook entries U

   A learning rate parameter λ

Output: The image generated with the updated U

1: Obtain the text embedding z(t) = f(t)(x(t));

2: for all iteration = 1, 2, ⋯  , MaxIteration do

3:   Generate an image x(i) = g(U);

4:   Obtain the image embedding z(i) = f(i)(x(i));

5:   Compute \({{{{{{{{\mathcal{L}}}}}}}}}_{t2i}\) with Eq. (14);

6:   Compute the gradients \({\nabla }_{{{{{{{{\bf{U}}}}}}}}}{{{{{{{{\mathcal{L}}}}}}}}}_{t2i}\);

7:   Obtain \({{{{{{{\bf{U}}}}}}}}^{\prime}\) by updating U using gradient descent with λ;

8:   Obtain U by performing element-wise quantization on \({{{{{{{\bf{U}}}}}}}}^{\prime}\) with Eq. (15);

9:  end for

10: return the image generated with the updated U.

Neural network visualization vs. text-to-image generation

The intrinsic difference between neural network visualization and text-to-image generation lies in that they produce images following different data distributions. Not utilizing extra modules or data, neural network visualization exhibits BriVL’s primitive visual understanding of a given piece of text. However, the VQGAN34 used for text-to-image generation is pre-trained on ILSVRC-201235 (i.e., the classic ImageNet dataset), which generates images following the data distribution of ImageNet and thus being more photo-realistic. Due to such an intrinsic difference, we present the visualization results of these two tasks for different purposes in this paper. Specifically, neural network visualization allows us to see what exactly a pre-trained multi-modal foundation model imagines about semantic concepts and sentences, while text-to-image generation is used to generate images matched with given texts in a more human-friendly way.