Classic design · Mid
Image captioning
Classic interview design. Key points: visual encoder + text decoder, cross-attention, and CIDEr.
Short answer
Classic interview design. Key points: visual encoder + text decoder, cross-attention, and CIDEr. Captioning an image means generating a sentence that describes what it shows. It is a problem of ML system design that combines vision and language. According to the Show and Tell paper, a good description needs to capture the objects in the image, the relationships between them…
01 · CONTEXT
What it means to caption an image
Captioning an image means generating a sentence that describes what it shows. It is a problem of ML system design that combines vision and language. According to the Show and Tell paper, a good description needs to capture the objects in the image, the relationships between them, their attributes, and the activities they are involved in, and also express this in natural language; that is why a language model is needed in addition to visual understanding.
02 · BASE ARCHITECTURE
Visual encoder and text decoder
The approach in the Show and Tell paper, called NIC, is a single model: it takes the image and is trained to maximize the likelihood of the sequence of words that describes it. The vision and language blocks can be pretrained on larger corpora.
The diagram starts with the input image, which goes to the visual encoder.
The encoder is a CNN pretrained on image classification, and its last hidden layer feeds the decoder.
The image is fed in only once, to inform the decoder about the content; in the authors' tests, feeding it at every step gave worse results.
The decoder generates the sentence one word at a time, until it emits the stop word, which signals that the sentence is complete.
The diagram ends at the application that consumes the finished caption.
At inference, the paper compares sampling with beam search: the latter keeps the k best partial sentences as candidates at each step, and using a beam of size one (greedy search) worsened the results on average.
| Abordagem | What the source says |
|---|---|
| Detections and sentence templates | Detections become a triple of scene elements, converted to text by fixed models (templates); the source describes them as limited in expressiveness. |
| Ranking existing descriptions | Retrieves descriptions close to the image in a joint embedding space; it does not describe compositions of objects never seen before. |
| Single model (NIC) | A single model trained to maximize the likelihood of the word sequence given the image. |
03 · VISUAL ATTENTION
Looking at the right region for each word
The Show, Attend and Tell paper points out a limit of using only the convnet's top-layer representation: it may lose information useful for richer captions. The way out is to extract features from a lower convolutional layer, which lets the decoder focus on specific parts of the image.
The diagram starts with the image, which goes to the spatial feature extractor.
The extractor uses a lower convolutional layer, not a fully connected layer, so that the decoder can focus on parts of the image.
The attention module gives a weight to each region, read as the probability that it is the right place to focus for the next word, and summarizes the result in a context vector, a dynamic representation of the relevant part of the image.
The decoder generates one word per step, conditioned on the context vector, the previous hidden state, and the words already generated.
The edge going back from the decoder to the attention module represents the attention that changes as each word is generated.
| Variante | How it is trained |
|---|---|
| Soft attention | Deterministic mechanism, trainable by standard backpropagation. |
| Hard attention | Stochastic mechanism, trained by maximizing an approximate variational lower bound, or equivalently by REINFORCE. |
04 · EVALUATION
How to measure whether the caption is good
Evaluation by human raters, who give each description a usefulness score, is described in the Show and Tell paper as the most reliable but time-consuming. Among automatic metrics, BLEU is a form of word n-gram precision between the generated sentence and the reference ones.
The sources disagree about BLEU: the NIC authors report that it correlates well with human evaluations, while the CIDEr authors state that BLEU and ROUGE correlate weakly with human judgment. Hence the CIDEr proposal, which measures a sentence's similarity to the consensus of how most people describe the image.
The diagram starts with the evaluation images, which feed the captioning model.
The evaluator receives the generated caption and the human reference captions, because CIDEr measures a sentence's similarity to the consensus of how most people describe the image.
Five reference sentences per image are not enough, according to the CIDEr authors, to measure how most people would describe the image; that is why they collected evaluation sets with many more descriptions per image.
CIDEr weights each n-gram by TF-IDF: the IDF discounts popular words, which are visually uninformative.
CIDEr-D is available on the MS COCO evaluation server and is more robust to gaming than the original version.
05 · PRODUCTION
Taking the model to a service
This section is a study proposal, not a company's architecture. Three decisions deserve attention when taking the model to production: whether the caption comes back immediately or later, where the images and captions are stored, and how quality is tracked over time. See also message queues e monitoramento.
The diagram starts with the client, which reaches the captioning service through the API gateway.
In the diagram, the captioning service connects to the image storage and the caption cache.
In the diagram, the service also connects to the inference queue, from which the inference worker consumes.
In the diagram, the worker connects to the storage, the cache, and the quality monitoring.
Frequently asked questions
What is Image captioning?
Classic interview design. Key points: visual encoder + text decoder, cross-attention, and CIDEr.
What does the Image captioning design cover?
It is organized in these sections: What it means to caption an image, Visual encoder and text decoder, Looking at the right region for each word, How to measure whether the caption is good, and Taking the model to a service.
How long is Image captioning and what level is it?
It takes about 14 minutes to read and is rated Mid level.
Now design it yourself
Run this case as an interview in the System Design school: you make the decisions, the tutor challenges them and you see how your design scores.