Abdul MoizFractional CTO · Senior EngineerBook a free call

AI models

DETR vs. ViT: Two Different Questions, Not One Architecture Plus a Head

A Vision Transformer classifies an image. DETR finds every object in it. The two models diverge earlier than most explanations admit, and the distinction changes how you should read both papers.

Updated · 4 min read

Fractional CTO · Senior Engineer

Key takeaways

  • ViT classifies a whole image: it cuts the image into patches and returns one answer.
  • DETR detects objects: it uses a fixed set of object queries to return many boxes and labels at once.
  • Choose ViT, or a model built on it, for classification and features, and a DETR-style model for detection.

The common shorthand is that DETR is "a Vision Transformer with a detection head bolted on." It's a convenient sentence, and it's wrong in a way that matters once you try to actually reason about either architecture. The two models aren't the same backbone answering different questions at the head — they're built to answer different questions from the start, and that difference shows up well before the transformer ever sees a token.

DETR vs ViT
ViTDETR
Question it answersWhat is this image?What objects are here, and where?
Input to the transformerImage patchesFeatures from a CNN backbone
Transformer partsEncoder onlyEncoder and decoder
OutputOne set of class scores100 boxes with class scores
Matching during trainingNot neededBipartite matching

What a Vision Transformer (ViT) asks

ViT asks a single question: what is this image?

  1. Image
  2. Patch embedding
  3. Transformer encoder
  4. CLS tokensummary of entire image
  5. Classification head
  6. 1,000 logits

The image is resized to a fixed square (224×224), cut into fixed-size patches, and each patch is embedded as a token. A learned CLS token rides alongside the patch tokens through the encoder and, through self-attention, absorbs a summary of the whole image. By the final layer, that one 768-dimensional vector is treated as a compressed description of everything in the frame.

The math at the head is a single matrix multiply:

text
(1 × 768) × (768 × 1000) = (1 × 1000)

One image goes in. One prediction comes out. There is exactly one thing to be right or wrong about.

What DETR asks: object detection as a set

DETR asks a different question: find every object in this image.

  1. Image
  2. CNN backboneResNet
  3. Feature map
  4. Flatten + positional encoding
  5. Transformer encoder
  6. ~800–1,000 image featuresvaries by image size
  7. Transformer decoder← 100 object queries
  8. 100 object embeddings
  9. Classification head + bounding box head

Instead of one summary vector, DETR carries 100 learned "object queries" through a transformer decoder that cross-attends to the encoded image. Each query is free to attach itself to a different region and, ideally, a different object: one might converge on a dog, another on a person in the background, most on nothing at all — DETR explicitly predicts a "no object" class for queries that don't find anything worth reporting.

The classification head is shared across all 100 queries, but it runs once per query, not once per image:

text
(100 × 256) × (256 × 81) = (100 × 81)

Each of the 100 outputs carries its own class prediction and its own bounding box. Where ViT produces one answer, DETR produces a set of 100 candidate answers and lets a bipartite matching loss during training decide which queries are supposed to be responsible for which ground-truth objects.

DETR vs ViT: two details worth being precise about

The two architectures don't share an "image to tokens" step. ViT patchifies the raw image directly and feeds those patches straight into a transformer encoder. DETR runs a CNN backbone first and only flattens the resulting feature map into a sequence afterward. The divergence isn't at the head — it's at the very first stage, before either model does anything a transformer would recognize as attention.

ViT's patch count is fixed; DETR's token count isn't. ViT resizes every input to the same 224×224 square, so it always produces the same 196 patches, full stop — it's a constant baked into the preprocessing step, not a property of the architecture. DETR keeps roughly the original aspect ratio and downsamples through the CNN backbone at a stride of 32, so the number of tokens fed into the transformer is just the feature map's height times width for that specific image. A wide image and a square image of similar resolution will hand the transformer noticeably different sequence lengths.

The mental model that made it click

ViT is one student who studies the entire image and turns in a single final answer.

DETR is 100 investigators searching the same scene at once, each responsible for finding at most one object — most of them turning up empty, and that's by design, not failure.

Once I stopped treating DETR as "ViT plus another output layer" and started treating it as a fixed set of parallel object hunters operating on a CNN's feature map, the architecture stopped requiring memorization and started following from the question it was built to answer.

Which one should you use?

Start from the output you need.

  • One label per image, such as "defective" or "not defective": use a classifier. A ViT is a good choice when you can start from one pretrained on a large dataset.
  • Where things are, and how many: use a detector. DETR is attractive because it removes hand-designed parts such as anchors and non-max suppression.
  • Features for something else, such as image search: use the encoder of a pretrained ViT and drop the classification head.

You can also combine them. Later detectors often use a ViT-style backbone with a DETR-style decoder, which is one more sign that these are two separate ideas rather than one model and its extension.

For where each piece sits in a full system, see the five-stage pipeline behind modern computer vision.

Common questions

Is DETR based on ViT?

No. Both use transformers, but the original DETR runs a CNN backbone first and feeds its features to an encoder and decoder. ViT cuts the raw image into patches and uses an encoder only. Later detectors do combine ViT backbones with DETR-style heads.

Does DETR need non-max suppression?

No. Its training uses one-to-one matching between predictions and real objects, which teaches it not to produce duplicates. Removing anchors and non-max suppression was one of its main ideas.

What are object queries in DETR?

They are learned vectors, 100 in the original model, that each look for at most one object. Queries that find nothing predict a "no object" class.

Sources and further reading

Abdul MoizFractional CTOSenior Engineer

About the author

I build production-grade AI applications, AI agents and data systems, fix AI-built apps, and help teams as a fractional CTO. I've spent seven years building software people rely on, including a media platform that grew to 2M+ users.

ShareXLinkedIn

Keep reading

Working on something like this?

Tell me about it on a free 30-minute call.