TL;DR
Xu et al. (2015) build an image captioner that writes one word at a time and, before each word, decides which part of the image to look at. The image is split into a grid of feature vectors, and a weighting over the grid is computed from the caption so far. The authors train this in two ways: a "soft" version that is a smooth function and can be trained by backpropagation, and a "hard" version that samples one location and is trained with a variance-reduced policy-gradient rule. The attention maps also show which region produced each word.
Builds on: Convolutional Neural Network, Long Short-Term Memory, Attention Mechanism · Leads to: Vision Transformer (ViT)
Why it mattered
Before this paper, most caption generators squeezed the whole image into one vector from the top of a convolutional network and fed it to a recurrent decoder once or at every step. The paper showed that letting the decoder pick its own image regions gives state-of-the-art BLEU on Flickr8k, Flickr30k and MS COCO with a single model, and that the learned alignments correspond strongly with human intuition. It made attention over image regions an ordinary tool. Later vision models such as Vision Transformer (ViT) apply attention to image patches in a related way.
The problem
Captioning needs two abilities at once. The model must recognise what is in the image, and it must express the relations between objects in a sentence. A single global feature vector throws away spatial detail, which the caption may need. The paper's answer is to keep a grid of local features and let the decoder learn which cells matter for each word. The open question is how to train that choice. A hard choice of one cell is not differentiable, and a soft average is differentiable but blurs the signal.
How it works
The model has three parts: an encoder that turns the image into a grid of vectors, an attention step that turns the grid and the current decoder state into one context vector, and an LSTM decoder that emits the next word.
flowchart LR I[image 224 by 224] --> C[VGG conv features: 14 by 14 by 512] C --> A[196 annotation vectors a_i] H[decoder state h_t minus 1] --> F[attention scores e_ti] A --> F F --> S[softmax weights alpha_ti] S --> Z[context vector z_t] A --> Z Z --> L[LSTM step] L --> W[next word] W --> H
- Encode the image. A convolutional network (the paper uses the 19-layer Oxford VGG network, pretrained on ImageNet) gives a feature map. Flatten it into annotation vectors , each of length . Check: .
- Score each location. An attention network scores each against the previous hidden state , giving one number per location. Toy case: scores for three locations.
- Normalise the scores with a softmax. Toy case: the exponentials are , which sum to , so the weights are . Check: the weights sum to 1.
- Form the context vector. Toy case, with , , : . The context leans towards the third location, which had the highest score.
- Update the LSTM with the previous word embedding, the previous hidden state and the context. The new state gives a distribution over the vocabulary, and the most likely (or a sampled) word is emitted.
- Repeat from step 2 with the new hidden state. Because changes, the attention moves as the caption unfolds. The paper's Figure 2 shows this shift.
Training differs only in step 4. The soft model uses the weighted average, which is smooth. The hard model samples one location from the weights and is trained with REINFORCE.
The math
Given annotation vectors and a caption , the model scores locations with , normalises them to weights that sum to one, forms a context , and predicts each word from the LSTM state and the context.
Step 2 in symbols: scores and weights
, where is a small multilayer perceptron. The weights are , a softmax over locations, so .
Step 4 in symbols: the context
Soft attention uses . The soft model also learns a gate and uses . Hard attention instead draws a location from the categorical distribution and sets .
Step 5 in symbols: the decoder
The LSTM uses the standard gates, with the input, forget and output gates and the candidate memory: , then and . Here is the word embedding, is an affine map, and is element-wise multiplication. The initial state comes from the mean of the annotation vectors through two MLPs: and . The word distribution is .
Training the hard model
The hard model maximises a variational lower bound on . Its gradient is estimated by sampling from the attention weights: . The baseline is a moving average, updated as . The term with is an entropy bonus. The paper notes that this is the REINFORCE rule with the log-likelihood as the reward.
Training the soft model and the doubly stochastic penalty
The soft model is differentiable end to end, and it is trained on . The penalty pushes each location's total attention over the caption towards 1, so the model looks at every part of the image at some point.
Results
All numbers below are from the paper's Table 1 and Sections 4 to 5.
- Flickr8k. BLEU-4 is 19.5 for the soft model and 21.3 for the hard model, against 17.7 for a log-bilinear baseline. The hard model's METEOR is 20.30.
- Flickr30k. BLEU-4 is 19.1 for soft and 19.9 for hard, against 18.3 for Google NIC (Vinyals et al.) in the same table.
- MS COCO. BLEU-4 is 24.3 for soft and 25.0 for hard. BLEU-1 is 70.7 and 71.8. The authors also report a METEOR improvement on COCO, which they speculate is connected to their regularisation and lower-level image features.
- Single model. All of these are single models, without an ensemble. Several competing entries use ensembles.
- Training cost. On COCO the soft model trained in under three days on one Titan Black GPU. Mini-batches are grouped by caption length, which the authors say speeds convergence without hurting quality.
- Visualisation. The 14 by 14 attention grid is upsampled by a factor of 16 and smoothed, which yields the maps in Figures 2 and 3.
Limits. The comparison uses BLEU and METEOR, which score n-gram overlap with reference captions rather than whether a caption is correct. Results use different feature extractors and dataset splits across rows, and the paper says so. Hard attention needs sampling and variance reduction, and its two weights ( and ) were set by cross-validation. The attention maps are offered as an aid to understanding mistakes, not as a score of caption quality.
Reading guide
- Section 1 (Introduction). The task and the two attention variants. Needs Attention Mechanism only as a concept.
- Section 2 (Related Work). The encoder-decoder view of captioning, from Encoder-Decoder Architecture and the earlier models it cites, such as Learning Phrase Representations using RNN Encoder-Decoder for SMT and Neural Machine Translation by Jointly Learning to Align and Translate.
- Section 3.1. The encoder and decoder equations. Needs Convolutional Neural Network, Long Short-Term Memory and Word Embedding.
- Sections 3.1.1 and 3.1.2. The attention scores, the softmax and the LSTM update. Needs Softmax.
- Section 4.1. The hard model and its gradient estimator. Needs REINFORCE, Variational Lower Bound (ELBO) and Policy Gradient.
- Section 4.2. The soft model and the expectation argument. Needs Categorical Distribution.
- Section 4.3 and Section 5. Training and results. Needs RMSProp, Adam Optimizer, Dropout, Early Stopping, VGG Network and BLEU Score.
- Section 5.4. Visualisation. Needs nothing new beyond the grid picture.
Check yourself
QuestionThree locations have scores 0, 0 and ln 2. What are the attention weights?Show answer
The exponentials are 1, 1 and 2, which sum to 4. The weights are 0.25, 0.25 and 0.5.
QuestionWhy does the soft model need no sampling, and what does the hard model give up in exchange for picking one location?Show answer
The soft model uses the weighted average of the annotation vectors, which is differentiable, so ordinary backpropagation works. The hard model samples a single location, which is not differentiable, so its gradient must be estimated with a score-function (REINFORCE) rule. That estimator has high variance. The paper uses a moving-average baseline to reduce it, and adds an entropy term with its own weight.
QuestionWhy does the paper use a 14 by 14 grid of features rather than one global vector, and where does 196 come from?Show answer
Each cell of the grid corresponds to a region of the input, so the decoder can attend to parts of the image. The grid is 14 by 14 because the 224 by 224 crop is downsampled four times by max pooling, and 14 times 14 is 196 locations.
QuestionThe soft model reports BLEU-4 of 19.5 on Flickr8k and the hard model reports 21.3. Compute the difference and say whether a single difference like this settles which variant is better.Show answer
The difference is 1.8 BLEU-4 points. This is one comparison on one dataset, so it shows the hard model scored higher on that split but does not by itself settle which variant is better in general.
QuestionIn the penalty L_d, what does the term sum over t of alpha_ti approaching 1 encourage?Show answer
It encourages each image location to receive roughly total attention of 1 across all the words of the caption, so the caption covers the image rather than fixating on a few regions.
Sources
- Xu, Ba, Kiros, Cho, Courville, Salakhutdinov, Zemel, Bengio (2015). Show, Attend and Tell: Neural Image Caption Generation with Visual Attention. arXiv:1502.03044. https://arxiv.org/abs/1502.03044
- Long short-term memory. Wikipedia. https://en.wikipedia.org/wiki/Long_short-term_memory