Part IV · Modern Computer Vision · Chapter 10

Attention & Vision Transformers

Instead of peering through tiny sliding spotlights, modern vision systems slice an image into puzzle patches—and let every piece talk directly to every other piece across the entire frame.

When a Small Spotlight Isn't Enough

In the previous two chapters, we watched Convolutional Neural Networks build up understanding like careful bricklayers. They start with tiny local neighborhoods—just a handful of neighboring pixels—and slowly stack layer upon layer until their view widens enough to recognize a whole face or car.

That local-first strategy is remarkably effective, yet it has a quiet blind spot. Imagine looking at a glowing bird perched on a branch beneath a bright moon. If you only peek through a tiny keyhole centered on the bird's beak, all you see is a small golden triangle floating against dark blue sky. Is it a star? A leaf? The corner of a rooftop?

Your own eyes solve this effortlessly. Without thinking, your gaze leaps across the frame—from the beak to the bright eye, out to the sweeping wing, down to the claws gripping the branch, and even up to the moon casting a pale rim of light along the bird's back. You understand each tiny patch by connecting it to faraway clues.

✦ Core Intuition

A Vision Transformer turns an image into a room full of puzzle pieces having a group conversation. Rather than whispering only to their immediate neighbors, every patch can send a glowing thread of attention straight across the picture to whichever other patches help explain what it is.

Letting Every Patch Talk to Every Other Patch

Try hovering or clicking on any square in the scene below. Watch what happens when you inspect the bird's beak, its tail, the branch, or the moon. Then switch to Local Window Only (CNN) to feel the sudden difference when a patch is only allowed to consult its immediate neighbors.

Interactive Lab 10.1 Patch-to-Patch Attention Web

Hover or click any puzzle patch to see where it sends its attention. Brighter, thicker gold arcs carry a larger share of the patch's focus.

Selected Patch: Bird's Beak Top Link: 42% → Eye & Head Reach: Full scene (all 24 patches)

Notice how natural those long-distance bridges feel. When you click the bird's beak in Global mode, the strongest gold arc locks straight onto the eye, while secondary cyan threads stretch all the way across empty sky to the tail feathers and the branch below. Switch to Local Window mode, and those far-off clues vanish—the beak is trapped inside its small neighborhood box.

⇄ Key Relationship

Local Spotlights vs. Global Conversations: A single local filter only sees neighbors right beside it, so information must hop step-by-step across many layers to cross the image. Global attention lets any patch reach 6× farther in a single step—connecting the far left edge to the far right edge immediately, at the cost of comparing every patch against every other patch.

Wait—What Did We Accidentally Lose?

Treating an image as a collection of puzzle patches talking to each other is a superpower. Every patch can freely listen to every other patch, no matter how far apart they sit.

? Pause & Think

Imagine dumping all twenty-four puzzle pieces of our bird scene into a cloth bag and letting them talk to each other inside the bag. Every piece can still hear every other piece—so what crucial visual information has completely disappeared?

Spatial layout—where each piece actually lives! Inside the bag, the machine has no idea whether the moon sits above the branch or below it, or whether the eyes belong above the mouth or scattered around the corners. Without a sense of order, a scrambled pile of face parts looks completely identical to a real face!

Why Order Matters: Positional Stamps

A convolutional spotlight never had to worry about this problem, because it slid systematically left-to-right and top-to-bottom across the grid. A Transformer, by contrast, treats its inputs like an unordered set of cards on a table.

The fix is wonderfully simple: before the conversation begins, the machine presses a subtle Positional Stamp onto the back of every puzzle patch—essentially a tiny map coordinate saying "I came from Row 1, Column 2." Now, even while patches chat freely across the entire frame, each one remembers exactly where it belongs.

Explore the face puzzle below. Try shuffling the patches without stamps and watch the machine's confidence collapse—then add the positional stamps and watch order snap back into focus. You can also click any two tiles to swap them yourself.

Interactive Lab 10.2 Why Order Matters: Positional Stamps

Shuffle the face puzzle without stamps, then attach Positional Stamps to see how coordinates restore meaning. Click any two patches to swap them.

Position Stamps: Off Patches in True Home: 16 of 16 Machine's Reading: Clear smiling face

With those two ideas working together—global attention to connect distant clues and positional stamps to preserve spatial geometry—Vision Transformers reshaped modern computer vision. They also unlocked something even bigger: because both words in a sentence and patches in a photo can be treated as stamped tokens having a conversation, the very same architecture can now read text and see images side by side.

Chapter 10 Takeaways

  • Patching the world: Vision Transformers slice an image into a grid of puzzle patches instead of sliding a tiny window pixel by pixel.
  • Global attention: Every patch can send strong or gentle attention threads directly across the entire image, linking distant clues like a bird's beak, wing, and branch in a single hop.
  • Positional stamps: Because an unordered bag of patches forgets layout, every patch receives a coordinate stamp so the machine knows what sits above, below, and beside what.