Neural Mastery

Vision Architectures: Transformers, Detection & Segmentation

CNNs covered image classification. Real vision systems usually need more: where is the object (detection), which pixels belong to it (segmentation), and increasingly, all of that built on attention rather than convolution.

Vision Transformers, In More Depth

Attention & Transformers introduced the core ViT idea: split an image into patches, treat them as tokens, run a standard Transformer encoder. A few variants matter beyond the base design:

  • ViT (Vision Transformer) — the original: fixed-size non-overlapping patches, a learned [CLS] token whose final representation is used for classification, standard learned or sinusoidal positional embeddings. Needs large-scale pretraining data to beat CNNs, because it lacks convolution's built-in locality bias.
  • DeiT (Data-efficient Image Transformer) — trains a ViT effectively on much smaller datasets (ImageNet-scale rather than requiring hundreds of millions of images) using strong data augmentation and a distillation token: a second special token trained to match a CNN teacher's predictions, injecting some of CNN's inductive bias through distillation rather than architecture.
  • Swin Transformer — computes attention within local, non-overlapping windows (not globally across the whole image, which is what makes plain ViT quadratic in image size), and shifts the window boundaries between layers so information still flows across window edges over depth. This restores a CNN-like efficient, hierarchical structure (small windows early, effectively larger receptive field deeper) while keeping attention's flexibility — the standard backbone choice when ViT's full quadratic global attention is too expensive, e.g. for detection and segmentation on large images.
ViT
DeiT
Swin
Computes attention within local windows (not globally -- avoids plain ViT's quadratic cost), then shifts window boundaries between layers so information still flows across edges over depth. The standard backbone when full global attention is too expensive.
Regular window: attention computed only WITHIN each 3×3 window -- far cheaper than attending globally across all 36 patches, but a patch can't yet see anything outside its own window.

Object Detection

Locate and classify multiple objects in an image, output as bounding boxes plus class labels.

  • R-CNN family (R-CNN → Fast R-CNN → Faster R-CNN): two-stage detectors — first propose candidate regions likely to contain an object (originally via a separate algorithm, then a learned Region Proposal Network in Faster R-CNN), then classify and refine each proposed region. Accurate, but slower than single-stage approaches because of the two-stage pipeline.
  • YOLO (You Only Look Once): a single-stage detector — divides the image into a grid and predicts bounding boxes and class probabilities directly, in one forward pass, no separate proposal stage. Dramatically faster than R-CNN-family models, historically at some accuracy cost (though later YOLO versions have closed most of that gap), making it the standard choice for real-time detection.
  • SSD (Single Shot Detector): similar single-stage philosophy to YOLO, predicting boxes at multiple feature-map scales simultaneously to handle objects of very different sizes in one pass.
1.Divide image into a grid
2.Predict boxes + class probabilities directly, one forward pass
3.No separate proposal stage
YOLO/SSD: dramatically faster than R-CNN-family models -- historically at some accuracy cost, though later versions have closed most of that gap. The standard choice for real-time detection.
  • DETR (Detection Transformer): reformulates detection as a direct set-prediction problem using a Transformer encoder-decoder — a fixed set of learned "object queries" each attend over the image features and directly output one object's box and class, eliminating hand-designed components like anchor boxes and non-max suppression that every prior detector needed.
Image features
(encoder output)
query 1 → box + class
query 2 → box + class
query 3 → "no object"
query 4 → box + class
Every prior detector needed hand-designed components (anchor boxes, non-max suppression to dedupe overlapping predictions) -- DETR eliminates both by directly predicting a fixed-size SET of objects, some of which the model learns to mark as empty.

Segmentation

Classify every pixel, not just draw a box around an object.

  • Semantic segmentation: every pixel gets a class label (e.g. "road," "car," "sky") — objects of the same class aren't distinguished from each other.
  • Instance segmentation: like semantic segmentation, but also distinguishes individual instances of the same class (this car vs. that car).
Instance segmentation: each car gets its OWN mask, distinguished from the other two even though they're the same class.
  • U-Net: an encoder-decoder architecture with an encoder that progressively downsamples (capturing context) and a decoder that progressively upsamples back to full resolution, with skip connections directly linking each encoder stage to its corresponding decoder stage — preserving fine spatial detail that pure downsampling would lose. Originally built for medical image segmentation (where training data is scarce and precise boundaries matter), U-Net's encoder-decoder-with-skip-connections pattern also became the backbone used inside diffusion models (see Generative Models) for iterative image denoising — the same "preserve detail while processing at multiple scales" property applies there too.
Each encoder stage links DIRECTLY to its corresponding decoder stage -- fine spatial detail (exact boundary locations) that pure downsampling would lose gets carried across, exactly what precise segmentation boundaries need.
  • Mask R-CNN: extends Faster R-CNN with a third output branch that predicts a pixel-level segmentation mask for each detected object, in addition to its box and class — the standard architecture for instance segmentation, unifying detection and segmentation in one model.
shared region features
Box branch
Class branch
Mask branch
The one Mask R-CNN adds -- predicts a pixel-level segmentation mask for each detected object, unifying detection and segmentation in one model.

Choosing an Architecture

NeedReach for
Best accuracy, compute isn't tightly constrainedViT / Swin backbone
Real-time detectionYOLO
Highest-accuracy detection, latency less criticalFaster R-CNN, DETR
Pixel-level medical/precise segmentationU-Net
Detect and segment individual object instancesMask R-CNN
Best accuracy, compute not tight
Real-time detection
Highest-accuracy detection
Pixel-level medical segmentation
Detect + segment instances
Reach for: YOLO

Next: Generative Models — GANs and diffusion models, the architectures that generate images rather than just understand them.

Last updated Sep 5, 2026Edit this pageReport an issue
← Previous
Linear Attention & State-Space Models (Mamba)
Next →
Autoencoders & Variational Autoencoders, In Full Depth