Sub topic 14
Attention as the Building Block of DETR
Assume you know convolutional neural networks, feature maps, matrix multiplication, and gradient-based training. Basic familiarity with Transformers helps, but the key ideas can be understood independently.
Why DETR needs attention
A convolutional backbone converts an image into a feature map. Each location in that map describes visual information from part of the image. A detector must then combine local evidence with global context. For example, a small region may look like a wheel, but the surrounding image helps determine whether it belongs to a car.
Attention is a learned way to combine information from different locations. Instead of giving every location the same fixed neighborhood, the model learns which locations should influence one another.
DETR uses a Transformer encoder and decoder:
- The encoder lets image features interact with one another.
- The decoder lets learned object queries search those image features.
- The resulting query representations are used to predict objects.
Queries, keys, and values
An attention layer receives a sequence of feature vectors. It creates three transformed versions:
- A query describes what a position is looking for.
- A key describes what information a position contains.
- A value contains the information that can be passed to another position.
For a query matrix Q, key matrix K, and value matrix V, scaled dot-product attention is:
Attention(Q, K, V) = softmax(QKᵀ / √d) V
Here, d is the key-vector size. The dot product between a query and each key produces relevance scores. The softmax converts those scores into weights that sum to one. The weighted values form the output.
The division by √d prevents large dot products from making the softmax too sharp, which would make optimization difficult.
Self-attention in the encoder
The backbone feature map is flattened into a sequence. If the map has height H, width W, and channel size C, the sequence contains H × W vectors of length C.
Self-attention allows every image location to compare itself with every other location. A feature describing a person can therefore use information from nearby objects, the ground, or the rest of the scene. Because flattening removes the two-dimensional layout, DETR adds positional encodings so the model can distinguish locations.
Cross-attention in the decoder
The decoder contains a fixed number of learned object queries. A query is not tied to an anchor or a particular pixel. During cross-attention, each query compares its current representation with encoded image features and gathers information from the regions that best match it.
Different queries can specialize in different objects. Their final representations feed prediction heads that output a class and a bounding box. The later matching step assigns predictions to ground-truth objects.
Exercise
What is the main difference between encoder self-attention and decoder cross-attention in DETR?
Show answer
Self-attention compares image features with other image features. Cross-attention compares an object query with encoded image features so the query can gather evidence for one predicted object.