Image as a Foreign Language: BEiT Pretraining for All Vision and Vision-Language Tasks — part 1/2
Cleaned-text code points [0, 15582). Bibliography omitted here; source.md includes it.
# Image as a Foreign Language: BEiT Pretraining for All Vision and Vision-Language Tasks
###### Abstract
A big convergence of language, vision, and multimodal pretraining is emerging. In this work, we introduce a general-purpose multimodal foundation model BEiT-3 , which achieves state-of-the-art transfer performance on both vision and vision-language tasks. Specifically, we advance the big convergence from three aspects: backbone architecture, pretraining task, and model scaling up. We introduce Multiway Transformers for general-purpose modeling, where the modular architecture enables both deep fusion and modality-specific encoding. Based on the shared backbone, we perform masked “language” modeling on images ( Imglish ), texts (English), and image-text pairs (“parallel sentences”) in a unified manner. Experimental results show that BEiT-3 obtains state-of-the-art performance on object detection (COCO), semantic segmentation (ADE20K), image classification (ImageNet), visual reasoning (NLVR2), visual question answering (VQAv2), image captioning (COCO), and cross-modal retrieval (Flickr30K, COCO).
## 1 Introduction: The Big Convergence
Recent years have featured a trend toward the big convergence of language [ 44 , 14 , 17 ] , vision [ 3 , 40 ] , and multimodal [ 53 , 43 , 62 ] pretraining. By performing large-scale pretraining on massive data, we can easily transfer the models to various downstream tasks. It is appealing that we can pretrain a general-purpose foundation model that handles multiple modalities. In this work, we advance the convergence trend for vision-language pretraining from the following three aspects.
First, the success of Transformers [ 52 ] is translated from language to vision [ 13 ] and multimodal [ 26 , 53 ] problems. The unification of network architectures enables us to seamlessly handle multiple modalities. For vision-language modeling, there are various ways to apply Transformers due to the different natures of downstream tasks. For example, the dual-encoder architecture is used for efficient retrieval [ 43 ] , encoder-decoder networks for generation tasks [ 58 ] , and the fusion-encoder architecture for image-text encoding [ 26 ] . However, most foundation models have to manually convert the end-task formats according to the specific architectures. Moreover, the parameters are usually not effectively shared across modalities. In this work, we adopt Multiway Transformers [ 53 ] for general-purpose modeling, i.e., one unified architecture shared for various downstream tasks. The modular network also comprehensively considers modality-specific encoding and cross-modality fusion.
Second, the pretraining task based on masked data modeling has been successfully applied to various modalities, such as texts [ 14 ] , images [ 3 , 40 ] , and image-text pairs [ 7 ] . Current vision-language foundation models usually multitask other pretraining objectives (such as image-text matching), rendering scaling-up unfriendly and inefficient. In contrast, we only use one pretraining task, i.e., mask-then-predict, to train a general-purpose multimodal foundation model. By regarding the image as a foreign language (i.e., Imglish ), we handle texts and images in the same manner without fundamental modeling differences. Consequentially, image-text pairs are utilized as “parallel sentences” in order to learn the alignments between modalities. We also show that the simple yet effective method learns strong transferable representations, achieving state-of-the-art performance on both vision and vision-language tasks. The prominent success demonstrates the superiority of generative pretraining [ 14 , 3 ] .
Third, scaling up the model size and data size universally improves the generalization quality of foundation models, so that we can transfer them to various downstream tasks. We follow the philosophy and scale up the model size to billions of parameters. Moreover, we scale up the pretraining data size in our experiments while only using publicly accessible resources for academic reproducibility. Although without using any private data, our method outperforms state-of-the-art foundation models that rely on in-house data by a decent margin. In addition, the scaling up benefits from treating images as a foreign language, as we can directly reuse the pipeline developed for large-scale language model pretraining.
In this work, we take advantage of the above ideas to pretrain a general-purpose multimodal foundation model BEiT-3 . We pretrain a Multiway Transformer by performing masked data modeling on images, texts, and image-text pairs. During pretraining, we randomly mask some proportion of text tokens or image patches. The self-supervised learning objective is to recover the original tokens (i.e., text tokens, or visual tokens) given corrupted inputs. The model is general-purpose in the sense that it can be repurposed for various tasks regardless of input modalities, or output formats.
As shown in Figure 1 and Table 1 , BEiT-3 achieves state-of-the-art transfer performance across a broad range of vision and vision-language tasks. We evaluate BEiT-3 on extensive downstream tasks and datasets, i.e., object detection (COCO), instance segmentation (COCO), semantic segmentation (ADE20K), image classification (ImageNet), visual reasoning (NLVR2), visual question answering (VQAv2), image captioning (COCO), and cross-modal retrieval (Flickr30K, COCO). Specifically, our model outperforms previous strong foundation models [ 62 , 1 , 60 ] despite that we only use public resources for pretraining and finetuning. The model also obtains better results than specialized models. Moreover, BEiT-3 not only performs well on vision-language tasks but also on vision tasks (such as object detection, and semantic segmentation).
## 2 BEiT-3 : A General-Purpose Multimodal Foundation Model
As shown in Figure 2 , BEiT-3 is pretrained by masked data modeling on monomodal and multimodal data, using a shared Multiway Transformer network. The model can be transferred to various vision and vision-language downstream tasks.
### 2.1 Backbone Network: Multiway Transformers
We use Multiway Transformers [ 53 ] as the backbone model to encode different modalities. As shown in Figure 2 , each Multiway Transformer block consists of a shared self-attention module, and a pool of feed-forward networks (i.e., modality experts) used for different modalities. We route each input token to the experts depending on its modality. In our implementation, each layer contains a vision expert and a language expert. Moreover, the top three layers have vision-language experts designed for fusion encoders. Refer to Figure 3 (a)(b)(c) for more detailed modeling layouts. Using a pool of modality experts encourages the model to capture more modality-specific information. The shared self-attention module learns the alignment between different modalities and enables deep fusion for multimodal (such as vision-language) tasks.
As shown in Figure 3 , the unified architecture enables BEiT-3 to support a wide range of downstream tasks. For example, BEiT-3 can be used as an image backbone for various vision tasks, including image classification, object detection, instance segmentation, and semantic segmentation. It can also be finetuned as a dual encoder for efficient image-text retrieval, and a fusion model for multimodal understanding and generation tasks.
### 2.2 Pretraining Task: Masked Data Modeling
We pretrain BEiT-3 via a unified masked data modeling [ 7 ] objective on monomodal (i.e., images, and texts) and multimodal data (i.e., image-text pairs). During pretraining, we randomly mask some percentage of text tokens or image patches and train the model to recover the masked tokens. The unified mask-then-predict task not only learns representations but also learns the alignment of different modalities. Specifically, text data is tokenized by a SentencePiece tokenizer [ 25 ] . Image data is tokenized by the tokenizer of BEiT v2 [ 40 ] to obtain the discrete visual tokens as the reconstructed targets. We randomly mask 15 15 % tokens of monomodal texts and 50 50 % tokens of texts from image-text pairs. For images, we mask 40 40 % of image patches using a block-wise masking strategy as in BEiT [ 3 , 40 ] .
We only use one pretraining task, which makes the training process scaling-up friendly. In contrast, previous vision-language models [ 36 , 65 , 26 , 34 , 53 , 30 , 62 ] usually employ multiple pretraining tasks, such as image-text contrast, image-text matching, and word-patch/region alignment. We show that a much smaller pretraining batch size can be used with the mask-then-predict task. In comparison, contrastive-based models [ 43 , 23 , 60 , 62 ] usually need a very large batch size 1 1 1 For example, CoCa [ 62 ] uses 65 65 k batch size, CLIP [ 43 ] uses 32 32 k batch size, and Florence [ 60 ] uses 24 24 k batch size. BEiT-3 uses a much smaller 6 6 k batch size for pretraining. for pretraining, which brings more engineering challenges, such as GPU memory cost.
### 2.3 Scaling Up: BEiT-3 Pretraining
#### Backbone Network
BEiT-3 is a giant-size foundation model following the setup of ViT-giant [ 63 ] . As shown in Table 2 , the model consists of a 40 40 -layer Multiway Transformer with 1408 1408 hidden size, 6144 6144 intermediate size, and 16 16 attention heads. All layers contain both vision experts and language experts. Vision-language experts are also employed in the top three Multiway Transformer layers. The self-attention module is shared across different modalities. BEiT-3 consists of 1.9 1.9 B parameters in total, including 692 692 M parameters for vision experts, 692 692 M parameters for language experts, 52 52 M parameters for vision-language experts, and 317 317 M parameters for the shared self-attention module. Notice that only vision-related parameters (i.e., comparable size as ViT-giant; about 1B) are activated when the model is used as a vision encoder.
#### Pretraining Data
BEiT-3 is pretrained on both monomodal and multimodal data shown in Table 3 . For multimodal data, there are about 15 15 M images and 21 21 M image-text pairs collected from five public datasets: Conceptual 12M (CC12M) [ 11 ] , Conceptual Captions (CC3M) [ 46 ] , SBU Captions (SBU) [ 39 ] , COCO [ 31 ] and Visual Genome (VG) [ 27 ] . For monomodal data, we use 14 14 M images from ImageNet-21K and 160 160 GB text corpora [ 4 ] from English Wikipedia, BookCorpus [ 64 ] , OpenWebText 2 2 2 http://skylion007.github.io/OpenWebTextCorpus , CC-News [ 33 ] , and Stories [ 50 ] .
#### Pretraining Settings
We pretrain BEiT-3 for 1 1 M steps. Each batch contains 6144 6144 samples in total, including 2048 2048 images, 2048 2048 texts and 2048 2048 image-text pairs. The batch size is much smaller than contrastive models [ 43 , 23 , 62 ] . BEiT-3 uses 14 × 14 14\times 14 patch size and is pretrained at resolution 224 × 224 224\times 224 . We use the same image augmentation as in BEiT [ 3 ] , including random resized cropping, horizontal flipping, and color jittering [ 56 ] . A SentencePiece tokenizer [ 25 ] with 64 64 k vocab size is employed to tokenize the text data. We use the AdamW [ 28 ] optimizer with β 1 = 0.9 \beta_{1}=0.9 , β 2 = 0.98 \beta_{2}=0.98 and ϵ = \epsilon= 1e-6 for optimization. We use a cosine learning rate decay scheduler with a peak learning rate of 1e-3 and a linear warmup of 10 10 k steps. The weight decay is 0.05 0.05 . Stochastic depth [ 21 ] with a rate of 0.1 0.1 is used. The BEiT initialization algorithm 3 3 3 We first randomly initialize the parameters within a small range, e.g., [ − 0.02 , 0.02 ] [-0.02,0.02] . Next, we rescale the l l -th Transformer layer’s output matrices (i.e., the last linear projection within each sublayer) of self-attention and FFN by 1 2 l \frac{1}{\sqrt{2l}} . [ 3 ] is used to stabilize Transformer training.
## 3 Experiments on Vision and Vision-Language Tasks
We extensively evaluate BEiT-3 on major public benchmarks for both vision-language and vision tasks. Table 1 presents the overview of results. BEiT-3 obtains state-of-the-art performance on a wide range of vision and vision-language tasks.
### 3.1 Vision-Language Downstream Tasks
We evaluate the capabilities of BEiT-3 on the widely used vision-language understanding and generation benchmarks, including visual question answering [ 19 ] , visual reasoning [ 49 ] , image-text retrieval [ 41 , 31 ] , and image captioning [ 31 ] .
#### Visual Question Answering (VQA)
The task requires the model to answer natural language questions about input images. Following previous work [ 2 , 65 , 26 ] , we conduct finetuning experiments on the VQA v2.0 dataset [ 19 ] and formulate the task as a classification problem. The model is trained to predict answers from the 3129 3129 most frequent answer candidates in the training set. BEiT-3 is finetuned as a fusion encoder to model deep interactions of images and questions for the VQA task. We concatenate the embeddings of a given question and an image, and then feed the input embeddings into Multiway Transformers to jointly encode the image-question pair. The final pooled output is fed into a classifier layer to predict the answer. The results are present in Table 4 , BEiT-3 outperforms all previous models by a large margin (more than 1.7 1.7 points), pushing the state of the art to 84.03 84.03 with a single model.
#### Visual Reasoning
The task needs models to perform joint reasoning about images and natural language descriptions. We evaluate the model on the popular NLVR2 [ 49 ] benchmark, which is to determine whether a textual description is true about a pair of images. Following previous work [ 65 , 26 ] , we construct two image-text pairs based on the triplet input. We finetune BEiT-3 as a fusion encoder to jointly encode the image-text pairs. The final pooled outputs of the two pairs are concatenated and then fed into a classifier layer to predict the label. As shown in Table 4 , BEiT-3 achieves a new state-of-the-art result for visual reasoning, outperforming CoCa by about 5.6 5.6 points. The performance on NLVR2 reaches above 90 90 % for the first time.
#### Image Captioning
The task aims to generate a natural language caption for the given image. We use the COCO [ 31 ] benchmark, finetune and evaluate the model on Karpathy split [ 24 ] . Following UniLM [ 17 ] and s2s-ft [ 5 ] , BEiT-3 is used as a conditional generation model via masked finetuning. To be more specific, a special self-attention mask is employed for the image captioning task. Image tokens (i.e., image patches) can only attend to each other bidirectionally within the image sequence. Tokens of the caption can attention to image tokens, their leftward caption tokens, and themselves. During finetuning, we randomly mask some percentage of caption tokens. The model is trained to recover these tokens based on the clues of the image and its leftward caption context. We also mask the special boundary token [SEP] to help the model learn to terminate the generation. For simplicity, BEiT-3 is trained with simple cross-entropy loss, without using CIDEr optimization. During inference, we generate the caption tokens one by one in an autoregressive manner. Table 4 presents the results on COCO captioning. BEiT-3 outperforms all previous models trained with cross-entropy loss, creating a new state-of-the-art image captioning result. The results demonstrate the superiority of BEiT-3 for vision-language generation.
#### Image-Text Retrieval