Image understanding, generation, and 3D reconstruction · Object detection · VIS008 · Architecture
Deformable DETR
Deformable DETR replaces dense image attention with sparse learned sampling around reference points, improving attention efficiency and multi-scale feature use.
In CodeSplash
Documented in the Academy only. There's no Set up button.
- Availability
- Explained here
- Code example
- Component demo, not a trained model
- Who ran it
- CodeSplash's run and the reference's record
- CodeSplash's run
- The reference's example ran in Python and R here, and they agree.
- The reference's record
- Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.
Purpose
Deformable DETR replaces dense image attention with sparse learned sampling around reference points, improving attention efficiency and multi-scale feature use. It retains the set-prediction and matching framework while changing how queries access image information.
Question / hypotheses
Can a small number of learned multiscale sample points recover useful spatial information for detection? This is a modeling/evaluation question, not a formal null-hypothesis test.
Architecture / computational structure
Queries predict reference locations, offsets and attention weights for a small set of points across feature levels. Bilinear sampling supplies values; iterative box refinement and two-stage variants are explicit configuration choices.
Assumptions
Learned sparse samples can miss informative regions, and coordinate conventions across scales are crucial. Efficient custom kernels require numerical/gradient validation. The original dense-attention DETR and deformable version should not share undocumented implementation assumptions.
Formula / computational definition
. Here l indexes scales, k samples, m heads, and weights normalize over the sampled locations.
Parameters and notation
Notation: Here q is a query, p_q its reference point, phi_l converts it into feature-level coordinates, delta-p gives learned offsets and x_l is a level’s feature map. A gives normalized sampling weights; W and W-prime are learned output/value projections.
Learned or estimated: Reference/offset/attention projections, transformer layers, backbone and prediction heads.
Hyperparameters and effects: Feature levels, sample points per head, heads/depth, query count, box-refinement configuration and matching/loss weights. More samples improve coverage but increase memory and kernel work.
Fitting, fine-tuning, and adaptation
Initialize compatible backbones and query/attention modules, adapt class heads first, then selectively unfreeze at reduced rates. If changing feature scales or sample counts, check offset initialization and weight compatibility. Validate target AP, source retention and attention sampling behavior after adaptation.
How to use and calibrate
Split by independent image source, scene, subject or capture sequence before augmentation; keep near-duplicate frames and crops in the same split. Measure AP by size, convergence, sampled-location distributions, memory and latency. Test bilinear sampling at image boundaries and padded areas; compare matched training budgets with dense DETR.
Example application
Detect objects using sparse multi-scale attention when dense spatial attention is too expensive or slow to train. This is a proposed use case, not a claim of validated performance on supplied real data.
Interpretation / pitfalls
Watch out
A training loss, confidence score or attention map is not a p-value or causal explanation. Confidence calibration, annotation quality and distribution-shift performance need separate assessment. A correct component calculation does not validate the full architecture, checkpoint or deployment pipeline.
Python and R code examples
Scope: One-head, one-scale deformable attention with two continuous sampling points; offsets and weights are supplied, not learned. Use the shared setup. Dependencies: NumPy / base R. Validation: Python executed; R unexecuted.
Full-model route: Python: official Deformable DETR and verified sampling kernels. R: small native interpolation demonstrations or bridge for full training. The example computes a weighted bilinear sample around fixed reference offsets.
Python and R
The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.
Component demo, not a trained model — One-head, one-scale deformable attention with two continuous sampling points; offsets and weights are supplied, not learned. Use the shared setup.
Shared setup for this chapter
import numpy as np
from itertools import permutations
def softmax(x):
x=np.asarray(x,dtype=float)
e=np.exp(x-x.max(axis=-1,keepdims=True))
return e/e.sum(axis=-1,keepdims=True)
def bilinear(a,y,x):
y=float(np.clip(y,0,a.shape[0]-1)); x=float(np.clip(x,0,a.shape[1]-1))
y0=int(np.floor(y)); x0=int(np.floor(x))
y1=min(y0+1,a.shape[0]-1); x1=min(x0+1,a.shape[1]-1)
dy=y-y0; dx=x-x0
return (1-dy)*((1-dx)*a[y0,x0]+dx*a[y0,x1])+dy*((1-dx)*a[y1,x0]+dx*a[y1,x1])
feature=np.arange(9,dtype=float).reshape(3,3)
reference=np.array([1.,1.]); offsets=np.array([[-.5,.25],[.5,-.25]]) # y,x
weights=softmax(np.array([.7,-.2]))
sampled=np.array([bilinear(feature,*(reference+d)) for d in offsets])
out=weights@sampled
assert np.isclose(weights.sum(),1) and sampled.min()<=out<=sampled.max()
print('sampled:',sampled,'attention:',weights,'output:',out)
Component demo, not a trained model — One-head, one-scale deformable attention with two continuous sampling points; offsets and weights are supplied, not learned. Use the shared setup.
Shared setup for this chapter
softmax <- function(x) {
if(is.null(dim(x))) {e<-exp(x-max(x)); return(e/sum(e))}
t(apply(x,1,function(v) {e<-exp(v-max(v)); e/sum(e)}))
}
bilinear <- function(a,y,x) {
y<-min(max(y,0),nrow(a)-1); x<-min(max(x,0),ncol(a)-1)
y0<-floor(y); x0<-floor(x); y1<-min(y0+1,nrow(a)-1); x1<-min(x0+1,ncol(a)-1)
dy<-y-y0; dx<-x-x0
(1-dy)*((1-dx)*a[y0+1,x0+1]+dx*a[y0+1,x1+1])+dy*((1-dx)*a[y1+1,x0+1]+dx*a[y1+1,x1+1])
}
feature<-matrix(0:8,3,byrow=TRUE)
reference<-c(1,1); offsets<-matrix(c(-.5,.25,.5,-.25),2,byrow=TRUE) # y,x
weights<-softmax(c(.7,-.2))
sampled<-apply(offsets,1,function(d) {p<-reference+d; bilinear(feature,p[1],p[2])})
out<-sum(weights*sampled)
stopifnot(abs(sum(weights)-1)<1e-12,out>=min(sampled),out<=max(sampled))
print(list(sampled=sampled,attention=weights,output=out))
Sources
- Source 1arxiv.org
- Source 2github.com
From the CodeSplash reference, entry VIS008.