Search

Search papers and lessons

Show and Tell: A Neural Image Caption Generator

Oriol Vinyals, Alexander Toshev, Samy Bengio et al. · 2014

Vinyals and colleagues trained one network that reads an image with a CNN and writes a caption word by word with an LSTM, and it beat the prior systems on several benchmarks.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Neural network basics
  • Linear algebra
  • Probability
  • Architectures
  • ReadReady to readNot yet

TL;DR

Vinyals, Toshev, Bengio and Erhan built one neural network that looks at a picture and writes a sentence about it. A convolutional network turns the image into a fixed list of numbers, and a recurrent LSTM network, started from those numbers, writes the words one at a time. The whole system is trained end to end to maximise the probability of the correct caption. On several benchmarks it scored higher than the systems that came before it.

Builds on: Convolutional Neural Network, Long Short-Term Memory, Word Embedding, Encoder-Decoder Architecture · Leads to: Image Captioning

Why it mattered

Before this work, captioning systems were built from separate parts: a detector found objects, a rule set arranged them, and a template produced the sentence. The paper showed that a single trained model could replace that pipeline and do better on the standard scores. On the Pascal dataset its BLEU-1 score was 59, against a previous best of 25 and a human figure of 69. On Flickr30k it reached 66 against 56, and on SBU it reached 28 against 19. The design, a CNN encoder followed by a language-model decoder that starts from the image, is the pattern that later captioning work built on.

The problem

Describing an image needs three things at once: recognising the objects, working out how they relate and what they are doing, and writing that in fluent English. Hand-built systems handled each step with a separate rule, so they were brittle outside their domains. Retrieval systems, which pick an existing caption from a database, could not write a new sentence for a new combination of objects. The authors wanted a model that generates sentences, and that can be trained from image and caption pairs alone.

How it works

The pipeline runs in one direction: image in, words out.

  1. Encode the image. A CNN pretrained on ImageNet classification reads the image and produces a fixed-length vector. The paper feeds this vector into the decoder once, at the first step. Check: the image is a single vector, not a sequence, so the decoder receives one fixed-size summary of it.
  2. Start the decoder. A special start token is fed in next, and the LSTM, having seen the image vector, produces a probability for every word in the dictionary. Check: the probabilities sum to 1.
  3. Pick or score the next word. Using the ground-truth caption during training, the true next word is the target. At inference, the model chooses among the likely words. Check: the target is one specific word, so the loss looks only at that word's probability.
  4. Feed the word back in. The chosen word is turned into an embedding vector and fed to the LSTM at the next step. Check: the embedding's size does not depend on the dictionary size.
  5. Repeat until the stop token. The LSTM keeps going until it emits a stop token, which marks a complete sentence. Check: a caption of 8 words takes about 9 steps including the stop.
  6. Search for the best sentence. At inference, the model keeps the 20 best partial sentences at each step (a beam of size 20) and returns the best complete one. Check: using a beam of size 1, which is greedy search, lowered the paper's scores by about 2 BLEU points on average.
flowchart LR
  I[Image] --> C[CNN encoder]
  C --> V[Image vector]
  V --> L1[LSTM step -1]
  S0[START] --> L2[LSTM step 0]
  L1 --> L2
  L2 --> P1[Word probabilities]
  P1 --> W1[Chosen word]
  W1 --> L3[LSTM step 1]
  L3 --> P2[More probabilities]
  P2 --> E[STOP ends caption]

A tiny example traced by hand. Use toy numbers, not the paper's. Suppose the true caption is "a dog" followed by stop, and the LSTM gives these probabilities for the true words: 0.6 for "a" at the first step, 0.5 for "dog" at the second, and 0.9 for stop at the third. The loss adds the negative logs: 1.3093=−(ln⁡0.6+ln⁡0.5+ln⁡0.9)1.3093 = -(\ln 0.6+\ln 0.5+\ln 0.9). Training pushes all three probabilities up, which lowers this number.

Training choices that mattered. The authors trained every weight with stochastic gradient descent at a fixed learning rate and no momentum. The image CNN started from ImageNet weights and was left unchanged, because updating it hurt results. Word embeddings started random, since initialising them from a news corpus gave no clear gain. The vocabulary kept only words that appeared at least five times in the training captions. Overfitting was the main difficulty, since the caption datasets are small, so dropout and averaging several models together added a few BLEU points, and the paper reports its numbers with both in place.

The math

The training objective maximises the probability of the correct caption SS for each training image II, summed over the training set, with parameters θ\theta:

θ⋆=arg⁡max⁡θ∑(I,S)log⁡p(S∣I;θ).\theta^{\star}=\arg\max_{\theta}\sum_{(I,S)}\log p(S\mid I;\theta).

Step 2 in symbols: the chain rule over words

Because a caption has no fixed length, the paper factors its probability word by word:

log⁡p(S∣I)=∑t=0Nlog⁡p ⁣(St∣I,S0,…,St−1).\log p(S\mid I)=\sum_{t=0}^{N}\log p\!\left(S_t\mid I,S_0,\ldots,S_{t-1}\right).

Here S0S_0 is the start token and SNS_N is the stop token, and NN is the length of this caption.

Step 3 in symbols: the recurrent state

The recurrent state hth_t summarises everything seen so far and is updated by a function ff:

ht+1=f(ht,xt).h_{t+1}=f(h_t,x_t).

The paper chooses ff to be an LSTM, whose state is the output mtm_t and whose memory is a cell ctc_t. It has an input gate iti_t, a forget gate ftf_t (not the function ff) and an output gate oto_t. Writing σ\sigma for the sigmoid and ⊙\odot for elementwise product:

it=σ(Wixxt+Wimmt−1),ft=σ(Wfxxt+Wfmmt−1),ot=σ(Woxxt+Wommt−1),i_t=\sigma(W_{ix}x_t+W_{im}m_{t-1}),\quad f_t=\sigma(W_{fx}x_t+W_{fm}m_{t-1}),\quad o_t=\sigma(W_{ox}x_t+W_{om}m_{t-1}), ct=ft⊙ct−1+it⊙h(Wcxxt+Wcmmt−1),mt=ot⊙ct.c_t=f_t\odot c_{t-1}+i_t\odot h(W_{cx}x_t+W_{cm}m_{t-1}),\qquad m_t=o_t\odot c_t.

The word distribution is a softmax over mtm_t. The gates let the cell keep or drop information, which is what makes long sentences trainable.

Step 1 and 4 in symbols: the inputs

The image enters once, as

x−1=CNN(I),x_{-1}=\mathrm{CNN}(I),

and each word enters through a learned embedding of its one-hot vector StS_t:

xt=WeSt,t∈{0,…,N−1}.x_t=W_e S_t,\qquad t\in\{0,\ldots,N-1\}.

Step 5 in symbols: the loss

The loss is the negative log likelihood of the correct word at each step, summed over the caption:

L(I,S)=−∑t=1Nlog⁡pt(St).L(I,S)=-\sum_{t=1}^{N}\log p_t(S_t).

This is the cross-entropy loss between the predicted distribution and the correct word, summed over steps. Minimising LL is the same as maximising the caption's probability.

Results

The paper reports BLEU, the standard overlap score for machine translation, along with METEOR and CIDEr, and it also runs human ratings. The main numbers, all from the paper:

  • MSCOCO development set: BLEU-4 of 27.7, METEOR 23.7, CIDEr 85.5. A nearest-neighbour baseline scored BLEU-4 9.9 and the human caption comparison scored 21.7 on the same measure.
  • Official COCO test set: BLEU-4 of 27.2.
  • Pascal (BLEU-1, transfer from COCO): 59, against a previous best of 25 and a human score of 69.
  • Flickr30k: 66, against 56. Flickr8k: 63, against 58. SBU: 28, against 19.
  • Human ratings on a 1 to 4 scale (Flickr8k): NIC averaged 2.37, a reference system 2.08, and the human ground-truth captions 3.89.

What the numbers mean. BLEU rewards word overlap, so a fluent but different caption scores low. The human ratings show the gap: the model is clearly better than the reference system, but well below the human captions. The authors say so directly, and they argue that better metrics are needed.

Limits. Transfer across datasets costs a lot. A model trained on COCO and tested on SBU fell from 28 to 16 BLEU-1. Transfer from Flickr30k to Pascal gave 53 against 59 from COCO. Most of the time the top candidate was a sentence already in the training set (about 80 percent), which suggests the model learned to reuse exemplar captions for the small training sets used. The paper reports that about half of the top 15 candidates were novel sentences with similar BLEU.

Reading guide

Read the paper in this order, with the lessons it needs:

  • Abstract and Section 1 (Introduction): the joint model idea, and why it follows machine translation. Needs Encoder-Decoder Architecture.
  • Section 2 (Related Work): the older pipeline systems and retrieval approaches. Useful context only; no new prerequisite.
  • Section 3 (Model), the equations: maximising the caption likelihood and the chain rule. Needs Maximum Likelihood Estimation and Language Model.
  • Section 3.1 (the LSTM): the gate equations. Needs Long Short-Term Memory, and Vanishing Gradient Problem for why gates help.
  • Section 3 (Training and Inference): the unrolled model and the loss, then sampling and beam search. Needs Word Embedding, Cross-Entropy Loss and Beam Search.
  • Section 4 (Experiments): the metrics and datasets. Needs BLEU Score. Read the results tables after the metric section.
  • Section 5 (Conclusion): the authors' summary of NIC and their closing view on evaluation.

Check yourself

QuestionRecall: what does the CNN contribute, and at which step of the LSTM does the image enter?Show answer

The CNN turns the image into a fixed-length vector. The paper feeds that vector into the LSTM once, at the first step, before any word, so the decoder knows the image content from the start.

QuestionToy numbers: the true caption's word probabilities are 0.5, 0.8 and 0.4 at three steps. What is the loss LL?Show answer

L=−(ln⁡0.5+ln⁡0.8+ln⁡0.4)=−(−0.6931−0.2231−0.9163)=1.8325L=-(\ln 0.5+\ln 0.8+\ln 0.4)=-(-0.6931-0.2231-0.9163)=1.8325. Training lowers this value by raising each probability.

QuestionWhy did the authors find that feeding the image at every step gave worse results?Show answer

They report that feeding the image at each time step yields inferior results because the network can exploit noise in the image and overfits more easily. Showing it once, at the start, worked better.

QuestionWhy do the BLEU scores and the human ratings disagree about how close the model is to people?Show answer

BLEU measures word overlap with reference captions, so a fluent caption with different wording scores low. Human ratings judge whether the description is correct and useful, and they show NIC well below the human captions, so BLEU does not fully capture the gap.

Sources

Read the full paper

Oriol Vinyals, Alexander Toshev, Samy Bengio et al., 2014. arXiv:1411.4555