Foundations to Frontier
Tests
E. Deep Learning › E7 CNN Building Blocks

Deformable Convolution

A deformable convolution adds a learned 2-D offset to each tap of the kernel, predicted from the input by an ordinary convolution, and samples the input at the shifted positions by bilinear interpolation. The kernel's footprint can then bend, stretch and rotate to follow objects, which helps detection and segmentation of objects with varied shapes and poses. This page covers the operation, bilinear sampling, how offsets are predicted, and modulated deformable convolution (DCNv2). In the exercises you show zero offsets give an ordinary convolution and grid offsets give dilation, then learn offsets for a horizontal-line detector from six images of 45° lines and find its response to diagonals rose from 0.25 to 0.58, against 0.72 for an exactly rotated grid.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Let the kernel bend

An ordinary 3 × 3 kernel always samples a rigid square grid. Objects are not square: a bird’s wing, a tilted car, a curved road. A deformable convolution lets each of the nine sampling points shift by a small learned amount, decided separately at each position from the image itself, so the kernel can stretch along an object or rotate with it. Positions between pixels are read by blending the four nearest pixels.

CNN building blocks
input pixels that affect the centre output (25 × 25 window shown)
receptive field of one output: 7 × 7 input pixels · total stride 1
Deep-learning "convolution" slides a small learned kernel over the input (no flip). Stride shrinks the output, padding preserves borders, dilation spreads the kernel to see further without more weights. Pooling summarises windows. Stacking layers grows the receptive field. Depthwise-separable and grouped convolutions cut cost by not connecting every input channel to every output channel. Transposed convolution upsamples, but uneven kernel overlap leaves checkerboard patterns. Squeeze-and-excitation rescales whole channels by gates computed from their global averages; CBAM adds a per-pixel spatial gate. (Here the gates are fixed functions of the averages so you can see their effect; in a network the excitation is learned.)
Why engineers care

Deformable convolutions improve detection and segmentation of objects in unusual poses and shapes, common in aerial imagery, driving scenes and robotic grasping. Deformable attention (E9-13) carries the idea into transformer detectors.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A deformable convolution learns…
  2. 2. Points between pixels are read by…
  3. 3. Offsets are predicted…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Output: y(p) = Σₙ w_n·x(p + gₙ + Δpₙ), where gₙ runs over the 3 × 3 grid and Δpₙ are learned offsets.
  • Bilinear sampling: for the 2 × 2 image [[0, 1], [2, 3]], the value at (0.5, 0.5) is 1.5; at (0, 0.25) it is 0.25.
  • Special cases:
    • zero offsets give a standard convolution;
    • offsets (d − 1)·gₙ give a dilation-d convolution;
    • rotating the grid by θ gives a rotated filter.
  • Offset prediction: a standard convolution outputs 2·k² channels: 18 offsets for 3 × 3, or 72 with 4 offset groups.
  • DCNv2 (modulated): each tap is also multiplied by a learned mask mₙ ∈ [0, 1]; weights (2, 3) with masks (0.5, 0) contribute (1, 0).
📝 Quick check (Level 1)
0/3 answered
  1. 1. Bilinear value at (0.5, 0.5) of [[0, 1], [2, 3]]:
  2. 2. Offset channels for a 3 × 3 deformable conv:
  3. 3. Offsets (d − 1)·grid give…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Deformable convolution in code

Exercise 1.

  • With zero offsets the deformable convolution matched a standard 3 × 3 convolution at every interior position.
  • With offsets equal to the grid it matched a dilation-2 convolution.
  • An offset of (0.5, 0.5) averaged four pixels, as bilinear interpolation should.

Practice.

  • torchvision.ops.deform_conv2d(input, offset, weight, mask=None); predict offset (and mask) with a small convolution initialised to zero, so training starts from a standard convolution.
  • Use deformable layers in the later stages of a backbone or in detection heads (DCNv2 replaced the 3 × 3 convolutions in the last three ResNet stages).
  • Offsets usually get a lower learning rate for stability.
offset = self.offset_conv(x)                      # 2*k*k channels, initialised to zero
y = torchvision.ops.deform_conv2d(x, offset, self.weight, padding=1)
🐍 Exercise 1: sampling with offsets
Implement bilinear sampling and a deformable 3 × 3 convolution at one position, and check its special cases.
📝 Quick check (Level 2)
0/3 answered
  1. 1. The offset branch is initialised to…
  2. 2. DCNv2 replaced 3 × 3 convs in…
  3. 3. torchvision's function is…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Differentiability. Bilinear interpolation is piecewise linear in the sampling position, so gradients flow to the offsets: ∂x(p)/∂p is a combination of neighbouring pixel differences. The offsets are trained by ordinary backpropagation, with no extra supervision.

Adaptive receptive fields. Standard convolutions sample a fixed grid, so their receptive field is fixed. Deformable convolutions make it input-dependent: Dai et al. (2017) showed the sampling locations spread over large objects and concentrate on small ones.

Relation to spatial transformers and attention. Spatial transformer networks (Jaderberg et al., 2015) learn one global warp per image; deformable convolution learns local, per-position warps. Deformable attention (Zhu et al., 2021) samples a few learned points per query, combining the idea with attention weights.

Limits. Learned offsets only adapt where training data push them; geometric invariances not present in training are not acquired (exercise 2: the learned detector did not need to give up horizontal lines). Offsets also add compute and irregular memory access.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Offsets receive gradients because bilinear sampling is…
  2. 2. Spatial transformers learn…
  3. 3. Deformable attention samples…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Learning where to look

History.

  • M. Jaderberg et al. (2015) introduced spatial transformer networks.
  • J. Dai et al. (2017) introduced deformable convolutional networks; X. Zhu et al. (2019) DCNv2.
  • X. Zhu et al. (2021) introduced deformable attention (Deformable DETR); W. Wang et al. (2023) scaled deformable convolution (InternImage).

Exercise 2. A fixed horizontal-line detector [[−1, −1, −1], [2, 2, 2], [−1, −1, −1]]/6 with sampling offsets shared by all positions. Offsets are learned by gradient ascent on its maximum response over 6 noisy images of 45° lines. Mean maximum response on 30 new images of each kind:

offsets 45° lines horizontal lines
none (standard) 0.252 1.046
grid rotated by 45° (exact) 0.718 0.184
learned from 45° lines 0.583 0.959
  • The standard detector barely responded to diagonals.
  • Rotating the sampling grid made it a diagonal detector, at the cost of horizontal lines.
  • Learned offsets more than doubled the diagonal response and kept most of the horizontal response, since nothing in training asked them to give it up. Deformable sampling adapts geometry to what the training data reward.
🐍 Exercise 2 (research): learning where to look
Learn sampling offsets for a horizontal-line detector from images of 45° lines, and compare with no offsets and with an exactly rotated grid.

Deformable convolution in AI, robotics and aerospace

  • Aerial and satellite detection of rotated objects (vehicles, ships, aircraft) benefits from adaptive sampling.
  • Driving scenes: deformable layers in detection and lane models handle perspective and curved lanes.
  • Grasp detection for robot arms, where object poses vary widely.

References

  1. J. Dai et al. (2017). “Deformable convolutional networks.” Proc. ICCV.
  2. X. Zhu, H. Hu, S. Lin, J. Dai (2019). “Deformable ConvNets v2: more deformable, better results.” Proc. CVPR.
  3. M. Jaderberg, K. Simonyan, A. Zisserman, K. Kavukcuoglu (2015). “Spatial transformer networks.” Advances in NeurIPS.
  4. X. Zhu et al. (2021). “Deformable DETR: deformable transformers for end-to-end object detection.” Proc. ICLR.