← Connected lessonVision TransformersA gentler route.
Cut a square picture into equal tiles and count how many pairs can interact.
More time for: Patch projection dimensions; position encoding; resolution and memory trade-offs.
Open high-school classroom and assessment ↗
What Are Vision Transformers?
Let's begin by exploring what Vision Transformers are and why they matter in the world of AI.
Transformers are a special technology that can understand all kinds of data — from text to images and even sounds — using the same powerful approach.
Think of it like a master language that works for words, pictures, and music alike.
When it comes to images, these transformers break pictures down into small chunks called tokens, kind of like how we break sentences into words.
This way, the transformer can 'read' an image just like it reads text.
Some well-known examples of this technology are Vision Transformers or ViT for images, CLIP that connects pictures and language, and Whisper for audio processing.
These advancements show the versatility of transformers across different types of data.
In summary, Vision Transformers extend the same architecture we use for reading language to understand visual information, opening up exciting possibilities for AI applications.
What Are Vision Transformers?
• Transformers process text, images, and sounds using the same basic design
• Images get broken into tokens, like words in a sentence
• Examples include Vision Transformers (ViT), CLIP, and Whisper
From Photos to Tokens
Let's dive into how a photo gets turned into something a transformer model can understand. This slide is titled 'From Photos to Tokens'.
We start with an original 224 by 224 pixel image. Think of it like a digital photo that you want to analyze.
Next, this big image is cut into smaller patches, each 16 by 16 pixels, making a total of 196 patches. It's like chopping a big pizza into many small slices.
Each of those 16 by 16 patches is then flattened into a long array of 768 numbers. Imagine turning each pizza slice into a list that describes how it looks.
All these arrays line up in a sequence, similar to words forming a sentence. This sequence of patch arrays is fed to the transformer model as its input 'language'.
So, just like language models read words one after another, this vision transformer reads the image patches one after another to understand the picture.
This method bridges images and language, enabling the model to apply its powerful understanding to photos. Let's keep this in mind as we move forward.
• A 224×224 pixel image is split into 16×16 patches → 196 patches total
• Each patch is flattened into a 768-length array (like a word vector)
• Sequence of these arrays acts like a sentence for the transformer model
16×16 Patches (196 total)
Sequence of 768-length Arrays
Understanding the [CLS] Token
Let's dive into understanding the special [CLS] token, a key concept in how models process images.
First, the [CLS] token is added to the very beginning of the sequence of image patches. Think of it like the team captain who is always at the front.
This token then pays attention to all patches, gathering information from each one, just like our captain listens to every team member to make the best decision.
To help the model understand where each patch belongs in the image, 2D positional embeddings provide spatial clues, like a map showing everyone's position on the field.
Together, the [CLS] token and positional information let the model summarize all patches into a global understanding, which is essential for accurate classification.
So, in short, the [CLS] token acts like a thoughtful captain, gathering and summarizing all the patch info, guided by positional hints.
Understanding the [CLS] Token
• [CLS] token is added at the start of image patches
• It ‘looks’ at all patches to collect global info
• 2D positional embeddings tell the model where each patch is
Vision Transformer vs CNN
Let's start by looking at the main topic on screen: Vision Transformer versus CNN. These are two different ways computers learn to understand images.
First, CNNs or Convolutional Neural Networks focus on small parts of an image at a time, like looking through a little window and sliding it around to capture details locally.
On the other hand, Vision Transformers look at the whole image all at once, like scanning a picture from afar instead of zooming in on tiny spots.
Both approaches have their strengths and weaknesses. CNNs are great at picking up local features quickly, while Vision Transformers capture global information, which can help with understanding bigger patterns.
So, by comparing these methods, we see how different strategies in image understanding can lead to advances in fields like computer vision and AI.
This gives us a foundation to explore how modern AI systems process images differently. Keep this in mind as we move forward!
Vision Transformer vs CNN
CNNs
• Look at images locally
• Use small moving windows
• Like scanning a photo piece by piece
Vision Transformers
• See the whole image at once
• Understand global relationships
• Like looking at a puzzle completely
Different Approaches with Pros & Cons
• CNNs excel at local details but can miss the big picture
• Vision Transformers catch global context but need more data and computing
• Both useful depending on the task and resources
Try Splitting an Image Into Tokens
Let's explore how an image breaks down into tokens using the interactive diagram.
Now notice how your image splits into 16 by 16 pixel patches, like cutting a photo into small squares.
Finally, explore how each patch turns into a list of numbers called tokens, which the computer uses to understand the image.
Why Use 2D Positional Embeddings?
Let's talk about why we use 2D positional embeddings in Vision Transformers. This helps the model understand where each image patch is located.
Transformers naturally treat input data as unordered lists, meaning they don't know the position of each patch just by itself.
Imagine if you scrambled the tiles of a puzzle and asked someone to solve it without knowing their original spots—that’s similar to what the model faces.
To fix this, we add 2D positional embeddings that act like coordinates, giving each patch an X and a Y location on the grid.
This way, the model can grasp the spatial structure of an image, understanding how patches relate to each other across height and width.
With this spatial info, the Vision Transformer can better recognize patterns, shapes, and objects within images.
So, 2D positional embeddings are essential because they provide the context that otherwise the model would miss.
Now that we know why these embeddings matter, we can see how they help the Vision Transformer perform so well in image tasks.
Why Use 2D Positional Embeddings?
• Transformers treat inputs as unordered lists by default
• 2D positional embeddings add X & Y coordinates to each patch
• Helps model learn the image’s spatial layout
Real-World Analogy: Puzzle Pieces
Let's start by thinking about how image patches can be understood with a simple, real-world analogy.
Each patch in an image is like a puzzle piece. Just as you put puzzle pieces together to see the full picture, the model looks at these patches to understand the whole image.
Now, think of the [CLS] token like the box cover of the puzzle. It gives you an overview or a summary of the whole image, just like the picture on a puzzle box helps you see what you're building.
Positional embeddings are the hints that tell each puzzle piece where it fits. Without knowing the right position, the pieces wouldn’t make sense together.
So, by combining all these puzzle pieces with their positions and the overview from the box cover, the model constructs the full picture effectively.
Real-World Analogy: Puzzle Pieces
Each patch is like a puzzle piece
[CLS] token is like the box cover showing the full picture
Positional embeddings tell where each piece fits
Quick Quiz: Vision Transformers Facts
Now it's time for a quick quiz to see how well you’ve grasped the ideas we just covered. Give each question a try on your own, and once you’ve finished, we’ll go over everything together.
Summary & Key Takeaways
Let's wrap up today's lesson with a quick summary of Vision Transformers, or ViT for short.
First, Vision Transformers treat images like puzzles by breaking them into small patches, which act like tokens—similar to words in a sentence.
Notice here the mention of the [CLS] token, this special token summarizes the whole image content, kind of like a headline that tells you what the image is about.
Another important point is the 2D positional embeddings—they help the model remember where each patch came from in the original image, keeping spatial info intact.
Compared to traditional Convolutional Neural Networks, ViT offers a fresh and powerful way to process images using these techniques.
So, to sum up, Vision Transformers bring a new perspective by treating images like puzzles made of tokens, summarizing with a [CLS] token, and keeping track of position to deliver strong performance.
Great work today! Remember these key ideas as they form the foundation for understanding many modern AI models.
• Vision Transformers view images like puzzle pieces, treating small patches as tokens like words in a sentence
• The special [CLS] token acts like a summary card, capturing the whole image’s meaning
• 2D positional embeddings keep track of where each patch fits spatially, like coordinates on a map
• Compared to CNNs, ViT offers a fresh and powerful way to understand images by focusing on relationships across patches