Visual-Text Reference Pretraining Model for Image Captioning

People can accurately describe an image by constantly referring to the visual information and key text information of the image. Inspired by this idea, we propose the VTR-PTM (Visual-Text Reference Pretraining Model) for image captioning. First, based on the pretraining model (BERT/UNIML), we design the dual-stream input mode of image reference and text reference and use two different mask modes (bidirectional and sequence to sequence) to realize the VTR-PTM suitable for generating tasks. Second, the target dataset is used to fine tune the VTR-PTM. To the best of our knowledge, VTR-PTM is the first reported pretraining model to use visual-text references in the learning process. To evaluate the model, we conduct several experiments on the benchmark datasets of image captioning, including MS COCO and Visual Genome, and achieve significant improvements on most metrics. The code is available at https://github.com/lpfworld/VTR-PTM.


Introduction
Pretraining models based on large-scale data greatly improve the performances of cross-modal understanding tasks, in which these models are also considered as the mainstream methods to fine tune the downstream tasks (e.g., image-text retrieval, visual question answering, and video question answering) [1][2][3][4][5]. However, it is not possible to directly use the pretraining models for the generation tasks. On the one hand, the pretraining models with BERT as the benchmark models only use the encoder of the transformer. If the generation task is preferred, a decoder needs to be provided [6][7][8]. On the other hand, the mask matrix used by the pretraining models for training is mostly designed for understanding tasks but not for the cross-modal generation tasks [9,10]. We focus on a unified representation of the encoder and decoder and provide a mask design for the cross-modal generation tasks.
Typically, the input to a pretraining model includes words in the text, regions of interest in an image, and markers with special meanings. It is common to aggregate and align visual features and text features through an attention mechanism before input operation [5,8,9]. However, this method cannot make the model refer to the visual and text features of the image at all times in the learning process. In the process of adjusting the visual pretraining model, the natural language features extracted by LSTM are used as a reference to help the visual pretraining model better identify the image content [11][12][13]. Inspired by this observation, we propose that visual and textual features can be used as references for pretraining models to help them generate captions better.
In this paper, we propose the Visual-Text Reference Pretraining Model (VTR-PTM), which is used for image captioning (see Figure 1). Specifically, VTR-PTM uses the unified encoder-decoder representation architecture and integrates the visual reference module into the encoderdecoder architecture consisting of 12 layers of transformer blocks, each having a masked self-attention layer and feedforward network. e unified modeling is achieved by employing a shared transformer network and utilizing specific self-attention masks to control what context the prediction conditions on. e model is pretrained on a mass of image-text pairs and modeled for bidirectional objectives and sequence-to-sequence objectives. During the pretraining process, we use the Faster RCNN [14] to detect the image and take the results as the key text reference. ey will be input into the VTR-PTM in combination with captioning and special markers. Meanwhile, we use the ResNet101 [15] to extract image features and encode the image features as the visual reference.
ey are added to the existing and unconditional g (gain) and b (bias) of layer normalization (LN) in transformers in the form of incremental parameters. e pretraining model constantly refers to the information from the image and key text, emphasizes the feature selection related to the image captioning, and reduces the weight of irrelevant features to achieve accurate captioning of the image. We have achieved promising results on two challenging benchmark datasets (MS COCO [16] and Visual Genome [17]), and the corresponding ablation experiments also prove the effectiveness of VTR-PTM.
In summary, our main contributions include the following three aspects: (1) We propose a new pretraining model VTR-PTM with a unified encoder-decoder representation with the bidirectional objective and the sequence-to-sequence objective so that the pretraining model can be applied to the generation tasks.

Related Work
2.1. Image Captioning. Image captioning is a comprehensive task combining the fields of computer vision and natural language processing. is task is challenging, and it needs to accurately find the salient objects in an image, the properties of the objects, the relationship between objects and the scene, and correct articulation in natural language. In the early years, operators were used to extract the features of an image and obtain the possible targets and their attributes by SVM. en, CRF or some rules were considered to generate captions of an image [18,19]. rough machine translation, the encoder-decoder structures were used for image captioning, which encode images by a convolutional neural network and decode the encoding features by the recurrent neural network [20,21]. After the introduction of the attention mechanism, performances have been further improved [22,23]. In addition, some methods establish relationships among the objects in an image and use them to construct a scene graph. By analyzing the scene graph, image captions are obtained [24][25][26][27]. anks to the success of the models such as transformer [28] and BERT in the field of natural language processing, studies on image captioning have been further advanced. NG-SAN [29] proposes a normalized self-attention mechanism and demonstrates the benefits of internal normalization. To address the transformer's inability to model the geometry of the input objects, geometric awareness-based self-attention is proposed, which enables to clearly and effectively consider the relative geometric relations among the objects in the images. U-VLP [7] proposes a unified visual language pretraining model, which uses two unsupervised learning objectives of bidirectional and sequence-to-sequence to conduct unified pretraining for lots of image-text pairs and to fine tune the pretraining model to adapt to the downstream tasks.

Pretrained Language Models.
e pretraining models trained on a large corpus can learn common language representations and help complete downstream tasks. e pretraining models provide a better model initialization, which can bring in a better generalization performance and accelerate the convergence speed of the target task. A detailed introduction of the pretraining models is provided in literature [27]. Here, we provide a specific review related to our model. BERT is of great significance to NLP, which is  mainly based on two core ideas: transformer architecture and unsupervised learning. It increases the generalization ability of the word vector model and fully describes the features at the character level, word level, sentence level, and even at the level for intersentence relationships. Unified Language Model Pretraining for Natural Language Understanding and Generation (UNILM) [30] also proposes a pretraining method, which is very simple and directly multiplexes the structure and parameters of BERT. Specifically, it uses a shared multilayer transformer that is pretrained with three language modeling goals: one way, bidirectional, and sequence to sequence. Each target specifies a different binary value in the self-attention mask to control the content available to the language model. Both BERT and UNILM can be fine-tuned to handle a variety of downstream tasks. BERT is mostly used in natural language understanding tasks, while UNILM aggregates context for different types of language models by configuring different self-attention masks. erefore, UNILM can be used in both the natural language understanding task and the natural language generation task.

Cross-Modal Pretraining
Models. According to the model architectures, the solutions to the cross-modal pretraining models can be divided into one-stream models and two-stream models. e one-stream models integrate text information and visual information into combined models, while the two-stream models process text and visual information, respectively. Video BERT [5] processes video-language tasks using BERT, which changes BERT processing "sentence pair" to "sentence-video pair," and then masks the video frame randomly. Similar to Video BERT, Visual BERT [31] serializes text features and visual features. e difference is that its visual features use region features without a mask. Unicoder VL [7] also fuses data at the beginning of training. e difference is that it uses Faster RCNN to process images and encodes the extracted image regions and positions. VL BERT [9] uses regions as visual features and adds the features of the complete image to the input part of the model. In ViL BERT [3], images and texts are sent into two different streams into the co-attentional transformer layer, where images are generated by Faster R-CNN to extract features from candidate regions to generate embedding, while texts are generated by several additional transformer layers. LXMERT [4] uses a two-stream model. e texts and the images are encoded separately through an independent coding layer, and then, the texts and the images are aligned and fused semantically through a modal interaction coding layer. e above studies use datasets of different sizes to adjust the pretraining model during pretraining. e pretraining process is based on large datasets and self-supervised learning [3-5, 7, 9, 31]. Some studies on the combination of semisupervised learning and pretraining model give us some new enlightenment [32][33][34][35][36]. Semisupervised learning is a learning method that uses annotated and unannotated data comprehensively, which can reduce the workload of manual annotation data and bring high accuracy. It is widely used in image classification, text classification, and other fields [32][33][34]. Sun et al. [35] studied how to combine semisupervised learning and pretraining models more effectively, that is, how to make better use of annotated and unannotated corpora in large-scale domains under the premise of large-scale general domain pretraining so as to maximize the effect of the model. Chen et al. [36] attempted to use the typical paradigm of semisupervised learning on the ImageNet dataset with superior results.

Approach
In this section, we first introduce the architecture of VTR-PTM, including preprocessing details for images and texts, and adjustments to LN layers in the transformer layers of the pretraining model. en, pretraining objectives including bidirectional and sequence-to-sequence are introduced.

Visual-Text Reference Pretraining Model.
Our crossmodel transformer network unifies the transformer encoder and decoder into a single model. Its backbone is the same as the BERT base. In contrast to the existing pretraining model, we design a dual-stream input mode for image reference and text reference, and we design a novel LN layer for transformer, called "VRLN" (Visual Reference Layer Normalization).

Text Reference.
e content of the text reference is different during model training and testing. During training, the text reference includes the results of object detection, target captions, and special markers. During testing, only results from object detection are included. We define the input image as I and the target sequence as S. We use the pretrained Faster RCNN [14] on Visual Genome [17] to detect objects in the images and obtain n object detection boxes B � b i , i � 1, . . . , n . e corresponding region label is C � {C 1 , C 2 ,. . ., C n }, C i ∈ R d . C i represents the category of the final prediction, and d represents the total number of the prediction categories. e calculation process is shown in as follows: e content of the visual reference is obtained by coding the image through the visual reference network. For a given image I, we adopt two different encoding methods, i.e., the single-channel visual reference network (VRN-SC) and the dual-channel visual reference network (VRN-DC) (see Figures 2(a) and 2(b)).
Our backbone network uses the pretrained ResNet on ImageNet to obtain the image feature I resnet , with a dimensionality of 2048. e process is formulated as In the single-channel visual reference network, I resnet is encoded to 512-dimensional vectors through two fully connected networks and two Swish layers, and then up to 768-dimensional vectors to ensure that it can be input into transformer as increments (e.g., in Figure 2(a)), where g ′ and b ′ are expressed as In the dual-channel visual reference network, the calculation process of I resnet is the same as that of the singlechannel visual reference network, but the coding of g ′ and b ′ is independent (e.g., in Figure 2(b)), and their expressions are where f VRN−DC1 and f VRN−DC2 represent two different columns. Although the calculations of I resnet are the same, the adjustment of the two columns of the parameters is gradually different in the training process of the model, and the final g ′ and b ′ are different. In the ablation experiments, we also compare the effects of these two coding methods.

Visual Reference Layer Normalization.
Layer normalization can normalize the results of a hidden layer in a neural network into standard normal distribution to accelerate convergence. Its input is a matrix X, and the mean μ and variance σ are calculated by the column unit of the matrix: where i is the row of X, j is the column of X, andε is a small decimal to prevent the denominator from being 0. e normalized value can be obtained by subtracting the mean value of this column from each element of each column and dividing it by the standard deviation of this column. e output of LN is In LN, we also need a set of parameters to ensure that the normalization does not destroy the existing information.
e process is formulated in equation (9). ere are already readymade, unconditional, fixed length g (gain) and b (bias) (equivalent to c and β in BN).
VRN encodes image features into the same dimensions as those of g and b and then adds the two coding results g ′ and b ′ to g and b in the form of increments, respectively. To prevent disturbing the original pretraining weight, the two transformation matrices can be zero initialized so that in the initial state, the model remains consistent with the original pretraining model.
en, the features can be obtained from VRLN: where X is the output of the previous model. e calculation process is shown in Figure 3.

Pretraining Tasks.
Our pretraining process refers to U-VLP [8] and UNILM [30], using the design of the two different language objectives for VTR-PTM generation. In BERT, 15% of the input text is replaced by [MASK], random text, or original text, and the replacement probabilities are 80%, 10%, and 10%, respectively. We adopt similar replacement principles. We input the corresponding output vector calculated by the model into SoftMax to predict possible words. e only difference between the two language objectives is the self-attention mask. e input to the where U � N + T + 3, N is the number of keywords, and T is the length of the target caption. e output of the encoder at different layers of the L-layer transformer can be expressed as H l , as shown in the following equation: We define the mask matrix of self-attention as M ∈ R U * U , which is shown in the following equation: where M jk represents the elements in the mask matrix, j, k ∈ [1, . . . , U]. e self-attention head can be expressed as A l , and the calculation process is shown in the following equation: where W l V , W l V , and W l K are the embedding weights. e intermediate variables V, Q, and K indicate values, queries, and keys. A l is further encoded by the feedforward layer and combined with the residual network to form the output H l . e bidirectional objective: the model allows all tokens to focus on each other in the prediction and encodes the context information of the left and right directions of the masked token in the training process. e operation is to set M in equation (14) to a zero matrix, and each token is allowed to appear in all the locations in the input sequence, indicating that the global information can be involved in the prediction of the token. e sequence-to-sequence objective: the input of this model consists of the source segment and the target segment. During the training process, tokens in the two segments are randomly masked off, and then, the model is asked to predict the words masked off. In the source segment prediction process, mutual attention is allowed from two directions in the source segment, while in the target segment prediction process, only the left context in the target segment, the current word, and all the tokens in the source segment can be used. In this way, the model can implicitly learn a bidirectional encoder and a unidirectional decoder.

Fine Tuning for Image Captioning.
We fine tune the pretrained VTR-PTM on the target dataset using the sequence-to-sequence objective and use beam search for the generated sequence. If not specifically specified, the beam size is 3. We use [CLS]

Datasets.
We conduct pretraining on Flickr30K [37]. e dataset includes 31,783 images, each of which has the corresponding 5 descriptions. Compared with large datasets such as Conceptual Captions [38], our pretraining process uses a smaller cost. To evaluate the performance of the model proposed in this paper, it is verified on the datasets of MS COCO and Visual Genome. MS COCO is widely used in the field of image captioning. e training set, verification set, and test set contain 82,783, 40,775, and 40,504 images, respectively, with 5 descriptions for each image. To get the keyword input of the pretrained model, we use Faster RCNN to extract the entities of each image and obtain the keyword information. For the verification of the experimental effect, we follow Karpathy's segmentation method and divide the data into 113.2K/5K/5K [39]. Visual Genome includes 108,077 images, and each image contains 35 objects on average, 26 attributes, and 21 relationships among objects. e region descriptions of the dataset are marked, with each region having a bounding box. e verification and testing methods are the same as those of using MS COCO.

Implementation Details.
Our backbone network uses the BERT base and is initialized using UNILM parameters. e pretrained Faster RCNN is used to extract the category information in an image as the keywords and the pretrained ResNet101 to extract the global image features as the visual reference input. Specifically, the keywords extracted by Faster RCNN are transformed into the word vector of 768, and the image information extracted by ResNet101 is obtained by using two VPNs; the image features are transformed from 2,048 dimensions to 768 dimensions. In the training process, our epoch length is set as 10, the learning rate is 0.00001, the batch size is 16, and the Adam optimizer [45] is used to progressively adjust the learning rate. In the inference phase, we use a beam search with a beam size of 3 if not specifically specified. We train our model using the standard cross-entropy loss; we minimize the following cross-entropy loss: where y 1:t is the true captioning, and θ is the model parameter. Within the scope of our calculation, we have carried out experiments on epoch, batch size, beam size, learning rate, etc. e settings described above are under optimal conditions.

Image Captioning.
To comprehensively evaluate the performance of VTR-PTM, we compare our model with two types of the existing methods on MS COCO. (1) No pretraining models are used. e methods introduce attention or graph network in the structure of encoder and decoder, such as ADAPTIVE [24], UP-DOWN [45], CAVP [46], SGAE [26], and ASG [27]; (2) pretraining models are used. e methods are based on multilayer transformers, such as ORT [47], AOANET [48], NG-SAN [29], and U-VLP [7]. e evaluation results are shown in Table 1. VTR-PTM outperformed all the baselines on multiple metrics, except that C and S are 130.2 and 28.5, which are lower than 204.2 and 42.1 of ASG [27].
We visualize the results in the test set (see Figure 4). Line 1: the same image gets different keywords by object detection. Lines 2, 3, and 4: different image captions are generated by keywords in Line1. Line 5: different images get different keywords through object detection. Line 6, 7, and 8: different image captions are generated according to the keywords and image features in Line 5. VTR-PTM generated accurate captions according to keywords and image information. In terms of syntax and content, we have achieved promising results. In the first line, three different captions are generated for the same image, and the results are consistent with the image content, indicating that VTR-PTM can generate diverse expressions.

Dense Image Captioning.
e dense image captioning is to generate the captions of different regions in an image.
Relatively speaking, it means generating shorter sentences and more concrete caption content. We test the performance of our model on Visual Genome. Each image of the dataset contains the caption of 35 objects, and the results are shown in Table 2. VTR-PTM beats all the baselines substantially in all the metrics, except that C is 185.9, which is lower than 202.4 of ASG [27]. Our method is relatively more flexible because ASG needs to control the number of entities and then generate captions.
We show some of the visualization results in Figure 5. Line 1 and Line 5: different regions in the same image get different keywords by object detection. Lines 2, 3, 4 and 6, 7, 8: image captions are generated for different regions based on keywords in Lines 1 and 3. In the first line of images, our model generates accurate captions for bicycles, wheels, etc.

Impact of Different References.
To analyze the effects of different references, we compare three different methods that are as follows: (1) only the visual reference section is taken as input, and the keywords are set to an empty string, which is called VTR-PTM0, and the corresponding training loss is loss0; (2) only the keywords extracted are taken as input, and the visual reference is set to an empty string, which is called VTR-PTM1, and the corresponding training loss is loss1; and (3) the dual-stream input is taken, which is called VTR-PTM2, and the corresponding training loss is loss2. Experiments are carried out on MS COCO, and the results are shown in Table 3.
VTR-PTM2 is significantly better than VTR-PTM0 and VTR-PTM1 alone. Although VTR-PTM1 is better than VTR-PTM0, VTR-PTM0 still plays a certain role, which also proves that the visual reference embedding proposed here is effective. We show the training losses under the same conditions for three different input methods (see Figure 6). Training losses show the same downward trend. However, the convergence trend is VTR-PTM2 > VTR-PTM1 > VTR-PTM0, and the final losses are 1.74, 2.06, and 2.67, respectively. It can be seen that the input of the two parts has a certain effect. When the pretraining model selects features based on text features, image features can help the model further learn and express according to the image content.

Impact of Different Coding Methods.
To study the influence of the reference features generated by different coding methods on the results, we design two coding methods: (1) single-channel network VRN-SC and (2) dual-channel network VRN-DC. e results are shown in Table 4. VRN-DC is superior to VRN-SC in all the evaluation metrics. In a transformer, g and b have different functions in the normalization layer. g is the scaling of the features, b is the bias of the features. erefore, in the process of training, the influence on model learning is also different. e training loss is shown in Figure 7. During the training process, the loss decreases almost uniformly, and the final loss of VRN-SC is 6 Computational Intelligence and Neuroscience   e results are shown in Table 5. e results show that using UNILM parameters to initialize our model is better than using random initialization or BERT parameter initialization. Although UNILM does not specifically train image captions, the three mask designs contain to include the design of sequence generation. is is more conducive to the learning process of our model.

Conclusion
In this paper, we propose a novel pretraining model VTR-PTM for image captioning. We design the dual-stream input mode of image reference and text reference and use two different mask modes (bidirectional and sequence to sequence) to realize the VTR-PTM suitable for generating tasks. To the best of our knowledge, VTR-PTM is the first reported pretraining model to use visual-text reference in      e corresponding ablation experiments also show the effectiveness of the proposed model. In addition, our model can also be extended to audiotext or video-text tasks. In future research, we will try to combine semisupervised learning and pretraining models, which may bring better results for image captioning.

Data Availability
e data used to support the findings of this study are available from the corresponding author upon request.

Conflicts of Interest
e authors declare that there are no conflicts of interest regarding this study.