← All articles

languagelanguage generationimage captioning

Turning a few keywords into a sentence with two word predictors that meet in the middle

Many systems know what is in a picture or a conversation, but only as a handful of keywords, such as the objects, places, actions or attributes in an image. Turning those keywords into a natural sentence is harder than it looks, especially when there is no ready-made schema or language model to lean on. We trained two word-prediction networks, one reading sentences forwards and one backwards, and built sentences from the places where their guesses meet.

The problem

Systems such as virtual assistants, chatbots, question answering systems and caption generators are often built on top of other systems that hand over information as keywords. Without a proper schema, it can be hard to predict the sentence that should reach the user. Older approaches paired language models with probabilistic graphical models or statistical models of word sequences. We wanted a method that learns from caption text and does not need a schema describing the sentence in advance.

The idea

We use a type of network called an LSTM (long short-term memory), which reads a sentence word by word and keeps a memory of what came before. Given the words so far, it gives a probability for each possible next word. We trained two of them on the same sentences. The forward LSTM learned the sentences as written. The reverse LSTM learned them back to front. The paper stresses that this Dual-LSTM should not be confused with the common bidirectional LSTM. Both use two networks, one per direction, but they differ in implementation: ours are trained separately and then used to build word trees that are matched against each other.

Take two keywords, Ws and We. We want a short phrase that starts with the first and ends with the second. The forward LSTM starts from Ws and, at each step, keeps its K most probable next words. For each of those it again keeps K words, and so on, up to a depth L. That gives a tree of possible continuations. The reverse LSTM grows a similar tree backwards from We.

Two branching trees of words: the forward tree grows to the right from the start keyword, and the reverse tree grows to the left from the end keyword, each with branching probabilities.
Each LSTM grows a tree of likely next words, one forward from the first keyword and one backward from the last.Source: Fig. 1 of the paper.

A phrase is then found wherever a word in one tree matches a word in the other. We prune these matches by also requiring one extra neighbouring word on each side to overlap, so three words in total appear in both chains. The phrase is scored by multiplying the probabilities of its words, and for the overlapping words we use the average of the forward and reverse probabilities. A full sentence is made by chaining the phrases for each neighbouring keyword pair, and its score is the average of the phrase scores. We try every possible order of the keywords and rank the resulting sentences.

A diagram of a forward word chain and a reverse word chain overlapping in the middle, with the score formula below.
Where the forward and reverse chains overlap, the two probabilities are averaged, and the score multiplies the probabilities along the chain.Source: Fig. 4 of the paper.

What we found

We trained on 404,207 sentences from the COCO caption data set, with two stacked LSTM layers of 650 units and 650-dimensional Word2Vec word vectors, over 23,000 unique words. We tested on 500 randomly selected images from a different collection, the SBU data set, which we annotated by hand with keywords for objects and scenes. We chose a different training set on purpose, to show that the method scales and is not tied to one corpus.

Three judges independently looked at the top five sentences for each keyword order and picked the best one. They scored four things out of 5. The first score asks how good the sentence is given the keywords. The second asks how well it describes the image. The third compares it with the image's actual caption, and the fourth rates how well the original caption describes the image.

Mean judge scores for the generated sentences were 4.90 for fit to the keywords and 4.89 for describing the image. The original captions scored 2.72 (Table I).

Results in more detail

The two LSTMs were trained for 200 epochs (passes over the training sentences). We kept the epoch with the lowest perplexity, a standard score of how well a language model predicts text, where lower is better. That was epoch 88 for the forward network and epoch 77 for the reverse one. Before training, the sentences were split into words and stray whitespace and punctuation were removed.

At test time we gave the program each image's hand-written keywords, which mostly name things like objects, places, actions or attributes in the image. It tried every possible keyword order and returned ranked sentences. The judges looked at the top five sentences for each order and picked the best. The table below lists what each of the three judges gave, then the mean and the standard deviation (how far the judges' marks spread around the mean). Score 1 is how good the sentence is given the keywords, score 2 how well the best sentence describes the image, score 3 how the top sentence fares against the actual caption, and score 4 how well the original caption describes the image. All are out of 5.

Average scores (out of 5) given by the judges for the generated sentences
Score 1: fit to keywordsScore 2: describes imageScore 3: vs actual captionScore 4: original caption describes image
Judge 14.984.974.963.42
Judge 24.994.974.963.43
Judge 34.734.744.041.30
Mean score4.904.894.652.72
Standard deviation0.140.140.521.23
Higher is better in every column. Source: Table I of the paper.

The first two judges gave nearly the same marks, and the third was stricter on every score, most of all on score 4, where the printed values are 3.42, 3.43 and 1.30. The original captions got the lowest mean (2.72) and the widest spread (1.23), so the judges disagreed most about them. The generated sentences were rated 4.90 for fit to the keywords and 4.89 for describing the image, with a spread of 0.14 for both (Table I).

The paper also shows sample results for three test images. Each image has two or three keywords, an original caption and five of the generated sentences. They change only in small ways, such as “with a bottle” against “holding a bottle”, or “in a room” against “in a living room”.

Sample results: keywords given to the system, the original caption of the image, and the five sentences generated
KeywordsOriginal captionGenerated sentences
person, bottle, roommad at my water bottlea person with a bottle in a room / a person with a bottle in a living room / a person with a bottle in a small room / a person holding a bottle in a room / a person holding a bottle in a living room
cat, boxcat in boxa cat sitting in a box / a cat in a box / a cat sitting in the box / a black cat sitting in a box / a black cat in a box
woman, babies, hospitalLarkin in the lavendar hat and Ruby in the pinka woman and two babies in a hospital / a woman and two babies are in a hospital / a woman and two babies in the hospital / a woman and her babies in a hospital / a woman and her babies are in a hospital
The images of the paper's Table II are not reproduced here. Sentences are separated by slashes. Source: Table II of the paper.

In the third example, the original caption names two people by first name, and says nothing about the hospital or the babies. The generated sentences are built from the three keywords (Table II).

Discussion

We conclude that the algorithm is quite effective for the task. The generated sentences scored well with human judges, and in our conclusion they are much better logically and grammatically than the original captions. They also describe the image in a more generic way than the original captions, which “suffer from the whims of the human mind”.

We think two ingredients matter. The Word2Vec vectors take similarities between words into account, and the LSTMs can hold long contexts, so together they let the algorithm work out the intermediate phrases between two keywords. A further point is that the method is not bound to one corpus. We trained on COCO captions and tested on a different data set, SBU, and read that as evidence of its general nature.

The test itself was small. It used 500 images, keywords annotated by hand, and three human judges instead of an automatic score. We evaluated only image captioning. For the future we plan to extend the algorithm to other tasks, such as human-computer dialogue and question answering, to see how well it works there, and to try it in other languages.

Sources

The figures and tables listed here are taken from the paper cited at the top of this page; the numbers quoted in the text come from it too.

  • Figures: Fig. 1, Fig. 4
  • Tables: Table I: scores from each human judge, with the mean and standard deviation, for the generated sentences and for the original captions; Table II: sample results with keywords, original caption and generated sentences for three images

Acknowledgements

This work was partially supported by the project sponsored by SERB, Government of India (order no. SB/S3/EECE/054/2016, dated 25/11/2016). The work was carried out at the Centre for Microprocessor Application for Training Education and Research, CSE Department, Jadavpur University.

Cite the paper

S Dubey, Swarnendu Ghosh, S Chowdhury, N Das, T Gonçalves, P Quaresma (2019). Caption Generation from Keywords using Probabilistic Dual-LSTM Networks and Dynamic Programming. International Conference on Emerging Technologies for Sustainable Development (ICETSD).

BibTeX
@inproceedings{dubey2019caption,
  author    = {Dubey, S and Ghosh, Swarnendu and Chowdhury, S and Das, N and Gon{\c c}alves, T and Quaresma, P},
  title     = {Caption Generation from Keywords using Probabilistic {Dual-LSTM} Networks and Dynamic Programming},
  booktitle = {International Conference on Emerging Technologies for Sustainable Development (ICETSD)},
  year      = {2019}
}