TL;DR
Deformable attention is an attention layer for grids of image features in which each query reads only a few points, and it learns where those points go. For every query it predicts small offsets from a reference location, samples the feature map at the shifted points with Bilinear Interpolation, and averages the samples with learned weights. Because a query touches a handful of points rather than every position, the cost stays small even for large feature maps, and the pattern adapts to each image.
Builds on: Multi-Head Attention, Bilinear Interpolation · Leads to: Deformable DETR, Sparse Attention
Where it's used
Object detection with transformers. DETR (2020) turned Object Detection into predicting the full set of boxes in one pass with a Transformer, and it removed several hand-designed steps that earlier detectors depended on. Its dense attention over every feature position converged slowly, and the original paper trained for hundreds of epochs. Deformable DETR (2020) replaced the dense attention over image features with deformable attention, and the paper reports that it outperforms DETR, especially on small objects, with about ten times fewer training epochs.
Multi-scale feature maps in detectors. Detectors usually compute features at several resolutions, from coarse maps that see large objects to fine maps that see small ones. Deformable attention reads all of these levels with one set of sampling points per query, so a single layer can gather evidence across scales. The Deformable DETR paper uses this in its encoder and decoder.
Dense prediction tasks. Segmentation and pose estimation also need features at many positions and at precise locations. Here the sampling points let each query find the parts of an image that belong to its object, even when the object is not aligned with a fixed grid.
How it works
Take the smallest case: one head, one query, one feature map, and two sampling points. The feature map is a 3 by 3 grid of numbers. Each grid cell has a position , with running left to right and running top to bottom. The value at position is
| row y, column x | 0 | 1 | 2 |
|---|---|---|---|
| 0 | 1 | 2 | 3 |
| 1 | 4 | 5 | 6 |
| 2 | 7 | 8 | 9 |
The query sits at the reference point , which is the centre cell with value 5. Between grid points, the map is read with bilinear interpolation.
- Predict an offset for each point. The query is turned into two offsets, and . These say where the two points go relative to . Check: the offsets are small, so both points stay near the reference point.
- Place the sampling points. The first point is and the second is . Check: neither point is a grid point, so bilinear interpolation is needed.
- Sample each point. The first point lies on the row halfway between 5 and 6, so its value is 5.5. The second lies on the column halfway between 2 and 5, so its value is 3.5. Check: each sample lies between the two grid values it came from.
- Predict the weights. The query also gives one raw score per point (a logit), and a Softmax turns the two scores into weights. Here the scores are equal, so the weights are . Check: the weights add to 1.
- Weighted sum. The output is . Check: the output lies between the two samples, as an average must.
- Several heads and levels. Each head repeats steps 1 to 5 with its own offsets and weights, and the heads are concatenated and mixed exactly as in Multi-Head Attention. With several feature levels, each head also samples on each level.
The flow of one head is shown below.
flowchart LR Q[Query and reference point] --> O[Predict offsets] Q --> W[Predict weights, softmax over points] O --> S[Sample the feature map at each shifted point] S --> A[Weighted sum of samples] W --> A A --> H[Head output]
The math
Let be a feature map with values at grid positions, and let be the feature vector of query . The reference point is a position on the feature map's grid, in the same coordinates as the walkthrough. There are heads, indexed by , and sampling points per head, indexed by .
For each head and point , the query predicts an offset and a weight logit. The weights are normalised with a Softmax over the points, giving that sum to 1 for each head. The output is
where means the feature map read at a real-valued position by Bilinear Interpolation, and is a learned matrix that mixes the head output back into the model width.
Read it from the inside out: is the sample in step 3, is the weight in step 4, and the sum over is step 5. The outer sum over and the matrix are the Multi-Head Attention combination from step 6.
Steps 1 and 2 in symbols
The offsets and the weight logits are both produced by linear maps of the query, and the logits are another linear map of . Because they are computed from the query, the sampling pattern changes with the image. A convolution uses the same fixed grid of offsets at every position. Deformable attention keeps the shape of that idea but learns it per query. The idea of sampling at moved positions was used in deformable convolution (Dai and colleagues, 2017), where the offsets are learned per position but the sampled values are then combined with fixed weights.
Step 3 in symbols
The sampling positions are real numbers, so at those points is found by bilinear interpolation of the four grid values around them. For the example, has and , which gives .
Steps 4 and 5 in symbols
The weights come from the softmax over the points, so for each head . The inner sum is a weighted average of the samples. For the example, .
Shapes and cost
In dense attention each query compares with all positions of the feature map, so one head costs about comparisons per query. In deformable attention each query uses points per level and levels, so one head costs samples per query, and over all heads. The cost per query no longer grows with the size of the map, which is the reason the method scales to high-resolution features.
Worked examples
Use the grid above with , , and the reference point . Both points get weight .
- Point 1 is . On the row the values are 5 at and 6 at . Halfway gives .
- Point 2 is . On the column the values are 2 at and 5 at . Halfway gives .
- Weighted sum: .
Interpretation: the output is the average of two nearby samples, one to the right and one above. The query never looked at the other six cells.
Use the top-left block of the grid: , , , . Let , with two offsets and . Suppose the softmax gives .
- Point 1 is . On the column the values are 2 and 5, so the sample is .
- Point 2 is . Here . Across the top: . Across the bottom: . Up: .
- Check point 2 with the four weights: . Both routes agree.
- Weighted sum: .
Interpretation: the query trusts the first point more, and the output sits between the two sampled values. Changing the offsets moves the samples, and changing the logits changes the weights, so the same layer can read different patterns on different images.
Take a single feature map, so positions. Compare one query in dense attention with one query in deformable attention using the Deformable DETR default setting: heads, levels and points per level.
- Dense attention for one head compares the query with all positions: scores. Over 8 heads that is scores.
- Deformable attention uses points on each of levels, so samples per head.
- Over 8 heads: samples per query.
- The ratio is . A dense query does about 156 times as many comparisons on this map.
Interpretation: the saving comes from the number of points, not from a smaller map. The sampled values also need bilinear interpolation, which costs a few multiplications each, so the real speed-up depends on the implementation. This is a comparison of counts, not a benchmark.
Try it: a map, one level, heads and points per head. How many points does one query sample in total, and how many scores would dense attention compute over the same heads? Answer: deformable attention samples points. Dense attention computes scores. The ratio is .
Common mistakes and tradeoffs
It is tempting to think that deformable attention looks at the whole image. In fact each query reads only points per head, and the whole-image view comes from stacking layers and from the offsets moving across the image over several layers.
It is tempting to think that the offsets are a fixed kernel, as in a convolution. In fact they are predicted from the query, so the pattern differs for each query and each image.
It is tempting to think that the sampling positions cannot be learned because they are not differentiable. In fact bilinear interpolation is piecewise linear in the position, so the gradient (the signal that tells each value which way to move) flows back into the offsets, and the network learns where to look. The gradient is only defined within each cell, so the learning signal is local.
When to use it: dense image features where the relevant evidence is local but its location varies, such as detection, segmentation and tracking, and where dense attention is too expensive.
When not to use it: language, where tokens do not sit on a grid and the relevant context may be anywhere in the sequence; and small inputs, where dense attention is cheap and the extra machinery adds nothing. A fixed local pattern such as Sparse Attention with a window is simpler when the relevant positions are known in advance.
| Situation | Problem | Better choice |
|---|---|---|
| Large image feature maps | Dense scores grow with the number of positions | Deformable attention with a few learned points |
| Relevant context is a fixed neighbourhood | Learned offsets add complexity | A fixed window or a convolution |
| Tokens with no grid position | There is no spatial sample to move | Standard Multi-Head Attention or Sparse Attention |
| Need to run on hardware with no custom kernel | The sampling is slower without a specialised implementation | Dense attention on a small map, or a convolution |
History
Convolutional networks use a fixed grid of weights at every position, and they were the default for vision for many years. Dai and colleagues introduced deformable convolution in 2017, which learns offsets for the grid so that the kernel can follow object shape. A later version added learned weights for each sample.
The transformer brought dense attention to vision. ViT split images into patches and attended over all of them, and DETR in 2020 used dense attention in an encoder-decoder for detection. DETR removed hand-designed detection steps but converged slowly, and dense attention over high-resolution features was costly. Zhu and colleagues published Deformable DETR in 2020, combining the sampling of deformable convolution with the attention of the transformer, and it became a common way to make detection with transformers faster to train.
Recap
- Each query predicts a few offsets and weights, samples the feature map at the shifted points, and takes a weighted sum.
- Sampling at real-valued positions uses Bilinear Interpolation, which gives a smooth and differentiable read.
- Learned offsets let the pattern change with the query and the image, which is the main difference from a convolution.
- The cost per query depends on the number of points, not on the size of the map, so high-resolution features are affordable.
- It is the core of Deformable DETR, and related ideas appear in Sparse Attention.
What to read next: End-to-End Object Detection with Transformers shows the detection setting that motivated the design, and Object Detection explains the task it serves.
Check yourself
QuestionIn deformable attention, what does the query predict, and what two things are then computed from those predictions?Show answer
The query predicts offsets for each sampling point and a logit for each point. The offsets place the points near the reference location, and the bilinear samples are read at those positions. The softmax of the logits gives the weights, and the output is the weighted sum of the samples, combined across heads.
QuestionA query uses one head, two points with weights , and the samples are 8 and 4. What is the output?Show answer
. The output is a weighted average of the samples, so it lies between 4 and 8, and the point with the larger weight pulls it closer to its sample.
QuestionWhy does the cost per query in deformable attention not grow with the number of positions in the feature map, while dense attention cost does?Show answer
Dense attention compares the query with every position, so its score count equals the number of positions. Deformable attention samples a fixed number of points per head and level, chosen by the offsets, so its count depends only on the number of heads, levels and points. The map can grow without adding samples per query.
QuestionApplied: a designer replaces a convolution with a fixed grid by deformable attention, but the new model is slower on a small feature map. Explain why this is expected, and what would make the choice worthwhile.Show answer
On an map there are only 64 positions, so dense attention is already cheap, and the learned offsets, the bilinear sampling and the extra weights add overhead. The benefit grows with the map size and with the need for spatially varying patterns. On large feature maps, where dense attention would need thousands of scores per query, the few sampled points become the cheaper option.
Sources
- Zhu et al., Deformable DETR: Deformable Transformers for End-to-End Object Detection (2020): https://arxiv.org/abs/2010.04159
- Carion et al., End-to-End Object Detection with Transformers (2020): https://arxiv.org/abs/2005.12872
- Dai et al., Deformable Convolutional Networks (2017): https://arxiv.org/abs/1703.06211